home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / plotting / pcgplots / gptmeta.cpp < prev    next >
C/C++ Source or Header  |  1992-04-24  |  8KB  |  322 lines

  1. // GptMeta.cpp - Windows Metafile Playing
  2. // copyright 1992 Pittsburgh Supercomputing Center
  3. #include "gpt.h"
  4.  
  5. char WMetaWindow::szClass[]     = "gptWMeta";
  6. char WMetaWindow::szMenu[]      = "gptMenuWMeta";
  7. HMENU WMetaWindow::hMenu        = NULL;
  8. HMENU WMetaWindow::hMenuWindow  = NULL;
  9. // Metafile Enumeration  Callback Function
  10. int FAR PASCAL WMetaWindow::MetaEnum(HDC hDC, LPHANDLETABLE lpHTable,
  11.                         LPMETARECORD lpMFR, int nObj,BYTE FAR * lpClientData)
  12. {
  13. int *extent = (int *)lpClientData;
  14.  
  15. int foo;
  16. foo = 1; // Keep going until find first Rectangle
  17.  
  18. if ( lpMFR->rdFunction == META_RECTANGLE && !extent[2] ) // get extent info from bgrnd rect
  19.     {
  20.     int bot   = lpMFR->rdParm[0];
  21.     int right = lpMFR->rdParm[1];
  22.     extent[0] = right;
  23.     extent[1] = bot;
  24.     extent[2] = 1;  // so only get first rectangle
  25.     //foo = 0;    ... if stop craps up everything!!!!!
  26.     }
  27. return foo;
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. // ******************* WMetaWindow Declarations *****************
  43. void WMetaWindow::DoCaption ( LPSTR mesg)
  44.       {
  45.       char szCaption [80] ;
  46.  
  47.       wsprintf (szCaption, "%s - %s", (LPSTR) mesg,
  48.                     (LPSTR) (FObj->GetFileName() )) ;
  49.  
  50.       SetWindowText (hWndThis, szCaption) ;
  51.       }
  52.  
  53.  
  54.  
  55.  
  56. // *** WMetaWindow::WndProc
  57. long WMetaWindow::WndProc(HWND hWnd, WORD iMessage, WORD wParam,
  58.                                                                                      LONG lParam )
  59.     {
  60.     char tmp[256];
  61.     this->iMessage = iMessage;
  62.     this->wParam   = wParam;
  63.     this->lParam   = lParam;
  64.     switch (iMessage)
  65.         {
  66.         case WM_CREATE:
  67.         hWndThis = hWnd;
  68.         SetExtents();
  69.         SBars = new Scroller(this, MetaX, MetaY );
  70.         SBars->SetXRatio((float)MetaX/(float)DrawRect.right);
  71.         SBars->SetYRatio((float)MetaY/(float)DrawRect.bottom);
  72.  
  73.         DoCaption("Metafile Paint");
  74.         return 0;
  75.  
  76.  
  77.         case WM_INITMENUPOPUP:
  78.         return 0;
  79.  
  80.  
  81.         case WM_PAINT:
  82.         Paint();
  83.         //DrawMenuBar(Appl::hWndFrame);
  84.         return 0;
  85.  
  86.         case WM_SIZE:
  87.         Size();
  88.         break;
  89.  
  90.         case WM_VSCROLL:
  91.         case WM_HSCROLL:
  92.         SBars->Scroll();
  93.         break;
  94.  
  95.         case WM_COMMAND:
  96.         return 0;
  97.  
  98.         case WM_MDIACTIVATE:
  99.         if (wParam == TRUE)    Activate();
  100.         else DeActivate();
  101.         return 0;
  102.  
  103.         case WM_QUERYENDSESSION:
  104.         case WM_CLOSE:
  105.         SendMessage (GetParent (hWnd), WM_MDIRESTORE, hWnd, 0L) ;
  106.         if (IDOK != MessageBox(hWnd,"OK to close window??","Metafile Close",
  107.                                   MB_ICONQUESTION | MB_OKCANCEL)) return 0;
  108.         break;
  109.  
  110.         case WM_NCDESTROY:
  111.         delete this;
  112.         break;
  113.         }
  114.     return DefMDIChildProc( hWnd, iMessage, wParam, lParam );
  115.     }
  116.  
  117.  
  118.  
  119. void WMetaWindow::Size()
  120.     {
  121.     LONG lSize = this->lParam;
  122.     WORD wType = this->wParam;
  123.     cxClient = LOWORD(lSize);
  124.     cyClient = HIWORD(lSize);
  125.     int LogicalX = (float)MetaX *((float)cxClient/DrawRect.right);
  126.     int LogicalY = (float)MetaX *((float)cyClient/DrawRect.right);
  127.     //if (SBars) SBars->NewSize(cxClient, cyClient);
  128.     if (SBars) SBars->NewSize(LogicalX, LogicalY);
  129.     }
  130.  
  131.  
  132. void WMetaWindow::Paint( void )
  133.     {
  134.     RECT rect;
  135.     PAINTSTRUCT ps;
  136.  
  137.     HDC hdc = BeginPaint(hWndThis, &ps);
  138.     if (!hMemDC)
  139.         {
  140.         SetMapMode(hdc, MM_ANISOTROPIC);
  141.         SetWindowExt(hdc,MetaX, MetaY);
  142.         SetViewportExt(hdc, DrawRect.right, DrawRect.bottom);
  143.  
  144.         SetWindowOrg(hdc,+SBars->getnHscrollPos(),+SBars->getnVscrollPos());
  145.  
  146.  
  147.         //if (!hmf) { EndPaint(hWndThis, &ps); return;}
  148.         BOOL metaStat = PlayMetaFile(hdc, hmf);
  149.         EndPaint(hWndThis,&ps);
  150.         if (!metaStat) return;
  151.         hdc         = GetDC(hWndThis);
  152.         //hCompBit = CreateCompatibleBitmap(hdc,DrawRect.right, DrawRect.bottom);
  153.         hCompBit = CreateCompatibleBitmap(hdc,MetaX, MetaY);
  154.         hMemDC   = CreateCompatibleDC(hdc);
  155.         hOldBit = SelectObject( hMemDC, hCompBit);
  156.         SetMapMode(hMemDC, MM_ANISOTROPIC);
  157.  
  158.         SetWindowExt(hMemDC,MetaX, MetaY);
  159.         //SetViewportExt(hMemDC, DrawRect.right, DrawRect.bottom);
  160.         SetViewportExt(hMemDC, MetaX, MetaY);   // So clip OK
  161.         PlayMetaFile(hMemDC,hmf);    // Play metafile in memory
  162.         ReleaseDC(hWndThis, hdc);
  163.         return;
  164.         }
  165.     else
  166.         {
  167.         //SetMapMode(hdc, MM_TEXT);
  168.         SetMapMode(hdc, MM_ANISOTROPIC);
  169.  
  170.         SetWindowExt(hdc,MetaX, MetaY);
  171.         SetViewportExt(hdc, DrawRect.right, DrawRect.bottom);
  172.         int XSrc = +SBars->getnHscrollPos();
  173.         int YSrc = +SBars->getnVscrollPos();
  174.         SetWindowOrg(hdc,XSrc,YSrc);
  175.         BOOL moo =
  176.         // StretchBlt( hdc, 0, 0,DrawRect.right-XSrc, DrawRect.bottom-YSrc,
  177.         ///              hMemDC,  XSrc, YSrc,    MetaX-XSrc,MetaY-YSrc,SRCCOPY);
  178.          StretchBlt( hdc, XSrc, YSrc,MetaX -XSrc, MetaY -YSrc,
  179.                       hMemDC,  XSrc, YSrc,    MetaX-XSrc,MetaY-YSrc,SRCCOPY);
  180.         EndPaint(hWndThis, &ps);
  181.         //MakeBitmapMeta(hWnd, rect, sbars);  OBSOLETE CODE
  182.         return;
  183.         }
  184. //****************** */
  185.     }
  186.  
  187. void WMetaWindow::SetExtents()
  188.     {
  189.     char filename[150];
  190.     MetaX = DrawRect.right;
  191.     MetaY = DrawRect.bottom;
  192.  
  193.     strcpy(filename,  FObj->GetFileName() );
  194.     hmf = GetMetaFile( filename);
  195.     if (hmf)
  196.         {
  197.         FARPROC lpEnumFunc;
  198.         lpEnumFunc = MakeProcInstance( (FARPROC) WMetaWindow::MetaEnum,
  199.                                                          Appl::hInstance );
  200.         HDC hdcX        = GetDC(hWndThis);
  201.         int Extents[3];
  202.         Extents[0] = MetaX;
  203.         Extents[1] = MetaY;
  204.         Extents[2] = 0;  // a flag for enum proc
  205.         BYTE FAR *  lpClientData = (BYTE FAR *)Extents;
  206.         //int nSavedDC = SaveDC(hdcX);
  207.         EnumMetaFile(hdcX, hmf, lpEnumFunc, lpClientData) ;
  208.         //RestoreDC(hdcX, nSavedDC);
  209.         MetaX = Extents[0];
  210.         MetaY = Extents[1];
  211.         ReleaseDC(hWndThis, hdcX);
  212.         FreeProcInstance( lpEnumFunc );
  213.         //DeleteMetaFile(hmfX);  // close old one
  214.  
  215.         }    //    MetaX = 1024/640;    MetaY = 714/442;
  216.     }
  217.  
  218.  
  219. // *** WMetaWindow constructor
  220. // Constructor
  221. WMetaWindow::WMetaWindow(FileObjectPt myfile )
  222.     {
  223.     MDICREATESTRUCT mdicreate;
  224.     hCompBit = NULL;
  225.     hMemDC   = NULL;
  226.     hOldBit = NULL;
  227.  
  228.     SBars = NULL;
  229.     errors = 0;
  230.     severe_error = FALSE;
  231.     ErrMsg1 = NULL;
  232.     ErrMsg2 = NULL;
  233.     FObj = myfile;
  234.     DrawRect.top = DrawRect.left = 0;
  235.     DrawRect.right  =   GetSystemMetrics(SM_CXSCREEN);
  236.     DrawRect.bottom =   GetSystemMetrics(SM_CYSCREEN)
  237.                                 - GetSystemMetrics(SM_CYCAPTION)
  238.                                 - GetSystemMetrics(SM_CYMENU);
  239.     // FObj->DialogFileInfo( );
  240.     hmf = NULL;    // never been painted
  241.  
  242.     mdicreate.szClass = szClass;
  243.     mdicreate.szTitle = "Metafile" ;
  244.     mdicreate.hOwner  = Appl::hInstance ;
  245.     mdicreate.x       = CW_USEDEFAULT ;
  246.     mdicreate.y       = CW_USEDEFAULT ;
  247.     mdicreate.cx      = CW_USEDEFAULT ;
  248.     mdicreate.cy      = CW_USEDEFAULT ;
  249.     mdicreate.style   = WS_VSCROLL | WS_HSCROLL ;
  250.     mdicreate.lParam  = (LONG) this ;
  251.  
  252.     HWND hWndChild = SendMessage (Appl::hWndClient, WM_MDICREATE, 0,
  253.                             (LONG) (LPMDICREATESTRUCT) &mdicreate) ;
  254.     if ( ! hWndChild ) exit( FALSE );
  255.  
  256.     }
  257.  
  258. // WMetaWindow Destructor
  259. WMetaWindow::~WMetaWindow( void )
  260.     {
  261.     DeleteMetaFile(hmf);
  262.     delete SBars;
  263.     delete FObj;    // also closes open file
  264.  
  265.     DeleteSz(ErrMsg1);
  266.     DeleteSz(ErrMsg2);
  267.      if (hMemDC)
  268.         {
  269.         SelectObject(hMemDC, hOldBit);
  270.         DeleteDC(hMemDC);
  271.         DeleteObject(hCompBit);
  272.         }
  273.  
  274.     }
  275.  
  276. void  WMetaWindow::LoadMenu(HANDLE hInstance)
  277.     {
  278.     hMenu = ::LoadMenu( hInstance,szMenu);
  279.     hMenuWindow =
  280.          GetSubMenu( hMenu,WMETA_MENU_POS);
  281.     }
  282.  
  283. // *** WMetaWindow::Activate
  284. void WMetaWindow::Activate( void )
  285.     {
  286.     SendMessage(Appl::hWndClient, WM_MDISETMENU,0,
  287.                                                           MAKELONG( hMenu, hMenuWindow));
  288.  
  289.     DrawMenuBar(Appl::hWndFrame);
  290.     }
  291.  
  292. // *** WMetaWindow::DeActivate
  293. void WMetaWindow::DeActivate( void )
  294.     {
  295.     Appl::theFrame->InitMenu();
  296.     DrawMenuBar(Appl::hWndFrame);
  297.     }
  298.  
  299. // *** WMetaWindow::Register - static window class register
  300. void WMetaWindow::Register( void )
  301.     {
  302.     WNDCLASS wndclass;   // Structure used to register Windows class.
  303.  
  304.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  305.     wndclass.lpfnWndProc   = ::gptWndProc;     // the global one
  306.     wndclass.cbClsExtra    = 0;
  307.     // Reserve extra bytes for each instance of the window;
  308.     // we will use these bytes to store a pointer to the C++
  309.     // (WMetaWindow) object corresponding to the window.
  310.     // the size of a 'this' pointer depends on the memory model.
  311.     wndclass.cbWndExtra    = sizeof( WMetaWindow * );
  312.     wndclass.hInstance     = Appl::hInstance;
  313.     wndclass.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
  314.     wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  315.     wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
  316.     wndclass.lpszMenuName  = "gptMenuWMeta";
  317.     wndclass.lpszClassName = szClass;
  318.  
  319.     if ( ! RegisterClass( &wndclass ) )    exit( FALSE );
  320.     }
  321.  
  322.