// ----------------------------------------------------------- // gwtiny.cpp definitions for gwtiny.h // ----------------------------------------------------------- #include "gwtiny.h" gwin::gwin(HWND hwndi): hwnd(hwndi) {} void gwin::open(int p) { prn = p; // set printer flag hdc = GetDC(hwnd); SelectObject(hdc,GetStockObject (ANSI_FIXED_FONT)); GetClientRect(hwnd,&rect); mw = rect.right - rect.left; // screen width nw = rect.bottom - rect.top; // screen height mt = mw; nt = nw; // current dimensions for screen hrgn = CreateRectRgnIndirect(&rect); if (prn == 1) // output to printer { const int pnmax = 100; char pname[pnmax]; GetProfileString("windows", "device",pname,pname,pnmax); // Now extract substring up to first comma for (int i = 0; i < pnmax; i++) { if (pname[i] == ',') { pname[i] = '\0'; break;} } { if (MessageBox(hwnd,pname, "Shall we print to ",MB_YESNO) == IDYES) { hdcprn = CreateDC (pname, pname, NULL, NULL) ; int xn,yn; xn = GetDeviceCaps(hdcprn,HORZRES); yn = GetDeviceCaps(hdcprn,VERTRES); static DOCINFO di = { sizeof (DOCINFO), "", NULL } ; StartDoc(hdcprn,&di); StartPage(hdcprn); // Necessary for Win95 setprn(hdcprn,xn,yn); GetTextMetrics (hdcprn, &tm) ; cw = tm.tmAveCharWidth; ch = tm.tmHeight;} else prn = 0; } } } // end of gwin::open() // ----------------------------------------------------------- void gwin::setprn(HDC hi, int xi, int yi) // after gwin::open { prn = 1; hdct = hdc; hdc = hi; // keep old DC in hdct mt = xi; nt = yi; GetTextMetrics (hdc, &tm) ; // default printer font cw = tm.tmAveCharWidth; ch = tm.tmHeight; } void gwin::endprn() { prn = 0; // set flag hdc = hdct; // restore DC mt = mw; nt = nw; SelectObject(hdc,GetStockObject (ANSI_FIXED_FONT)); GetTextMetrics (hdc, &tm) ; cw = tm.tmAveCharWidth; ch = tm.tmHeight; } // ----------------------------------------------------------- void gwin::preframe() {int c = 0; if (clipped == 1) { c = 1; unclip(); } Rectangle(hdc, m0,n1,m1,n0); if (c == 1) clipped = 1; } // ----------------------------------------------------------- void gwin::locate(int a,int b,int c,int d, int cl) { // in the next 4 lines we avoid 'large' integers. m0 = nrect.left = int(mt*0.01*a); // new origin x n1 = nrect.top = int(nt*0.01*(100-d)); // new origin y m1 = nrect.right = int(mt*0.01*b); // new right n0 = nrect.bottom = int(nt*0.01*(100-c)); // new bottom hnrgn = CreateRectRgnIndirect(&nrect); SelectClipRgn(hdc,hnrgn); if (cl == 0) SelectClipRgn(hdc,NULL); pm = m1-m0; // current pixel width and height pn = n0-n1; clipped = cl;} // ----------------------------------------------------------- void gwin::scale(double x0i, double x1i, double y0i, double y1i, int scani) { x0 = x0i; x1 = x1i; y0 = y0i; y1 = y1i; scan = scani; px = (x1 - x0); py = y1 - y0; cx = pm*(1.0/px); cy = pn*(1.0/py); } // ----------------------------------------------------------- double gwin::getscale(int k) { double z = 0; switch(k) { case 1: z = x0; break; case 2: z = x1; break; case 3: z = y0; break; default: z = y1; } return z; } // ------------------------------------------------------------ int gwin::getscan() { return scan; } int gwin::mx(double x) { return int(m0 + cx*(x-x0)); } int gwin::ny(double y) { return int(n0 - cy*(y-y0)); } void gwin::clip() { clipped = 1; SelectClipRgn(hdc,hnrgn); } void gwin::unclip () { clipped = 0; SelectClipRgn(hdc,NULL); } void gwin::frame(){ box(x0,x1,y0,y1); } // ------------------------------------------------------------ void gwin::move(double xi,double yi) { x = xi; y = yi; // keep current position m = mx(x); n = ny(y); MoveToEx(hdc,m,n,NULL);} void gwin::line(double xi,double yi) // clipping by Win32 { x = xi; y = yi; m = mx(x); n = ny(y); LineTo(hdc,m,n); } // ------------------------------------------------------- void gwin::box(double s,double t,double u,double v) { move(s,u); line(t,u); line(t,v); line(s,v); line(s,u); } // ------------------------------------------------------- void gwin::xplotd(fptype1 fun, double xa, double xb, int nn) { double x,dx,y; dx = (xb - xa)/nn; x = xa; y = fun(x); move(x,y); for (int j = 1; j <= nn; j++) { x = xa + dx*j; y = fun(x); line(x,y); } } // ------------------------------------------------------------ void gwin::p(double x, double y, char *c) { int len = strlen(c); TextOut(hdc, mx(x), ny(y), c, len); move(x,y); } // ------------------------------------------------------------ void gwin::pf(double x, double y, char *fmt, ...) // print format strings { char s[80]; va_list argptr; va_start(argptr, fmt); vsprintf(s,fmt, argptr); va_end(argptr); gwin::p(x,y,s);} // ------------------------------------------------------------ void gwin::close() { DeleteObject(hrgn); DeleteObject(hnrgn); if (prn == 1) { EndPage(hdcprn); // Necessary for Win95 EndDoc(hdcprn); DeleteDC(hdcprn); endprn(); MessageBox(hwnd,"Printing is Finished","Note",MB_OK); } ReleaseDC(hwnd,hdc); } // ------------------------------------------------------------- void gwin::cls() { clear(hwnd); } // end decl class gwin // ------- end definitions for class gwin --------------------