home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap18 / cosmo1.0 / cosmo.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  10KB  |  389 lines

  1. /*
  2.  * COSMO.C
  3.  *
  4.  * Basic Windows application code, with only a few specifics for handling
  5.  * blocking as far as OLE goes.  Most OLE interaction is directed through
  6.  * other functions in other source files that actually make the OLE library
  7.  * calls.
  8.  *
  9.  * Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
  10.  * Win32 version, January 1994
  11.  */
  12.  
  13. #include <windows.h>
  14. #include <ole.h>
  15. #include "cosmo.h"
  16. #include "oleglobl.h"
  17.  
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  * Array of pointers to strings loaded from the resource file.
  24.  * Pointers can be near since we'll use LocalAlloc for
  25.  * the string space.
  26.  */
  27.  
  28. char NEAR   *rgpsz[CSTRINGS];
  29.  
  30. //Global variable block.
  31. GLOBALS     stGlobals;
  32. LPGLOBALS   pGlob=&stGlobals;
  33.  
  34.  
  35.  
  36.  
  37. /*
  38.  * WinMain
  39.  *
  40.  * Purpose:
  41.  *  Main entry point of application.   Should register the app class
  42.  *  if a previous instance has not done so and do any other one-time
  43.  *  initializations.
  44.  *
  45.  * Parameters:
  46.  *  See Windows SDK Guide to Programming, page 2-3
  47.  *
  48.  * Return Value:
  49.  *  Value to return to Windows--termination code.
  50.  *
  51.  */
  52.  
  53. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance
  54.     , LPSTR lpszCmdLine, int nCmdShow)
  55.     {
  56.     HACCEL      hAccel;
  57.     HWND        hWnd;
  58.     MSG         msg;
  59.  
  60.  
  61.     pGlob->hInst=hInstance;
  62.     pGlob->pszCmdLine=lpszCmdLine;
  63.     pGlob->nCmdShow=nCmdShow;
  64.  
  65.     /*
  66.      * Try to initialize; on failure, clean up anything that might have
  67.      * been allocated.
  68.      */
  69.     if (!FApplicationInit(pGlob, hPrevInstance))
  70.         {
  71.         FApplicationExit(pGlob);
  72.         return FALSE;
  73.         }
  74.  
  75.     hWnd=CreateWindow(rgpsz[IDS_CLASSCOSMO], rgpsz[IDS_CAPTION]
  76.         , WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT
  77.         , CW_USEDEFAULT, 400, 350, NULL, NULL, hInstance, NULL);
  78.  
  79.     if (NULL==hWnd)
  80.         {
  81.         FApplicationExit(pGlob);
  82.         return FALSE;
  83.         }
  84.  
  85.     //Prevent the WM_SIZE from ShowWindow from making us dirty.
  86.     pGlob->fNoDirty=TRUE;
  87.     ShowWindow(hWnd, pGlob->nCmdShow);
  88.     pGlob->fNoDirty=FALSE;
  89.  
  90.     if (SW_HIDE != pGlob->nCmdShow)
  91.         UpdateWindow(hWnd);
  92.  
  93.     hAccel=LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATORS));
  94.  
  95.     //Non-OLE message loop.
  96.     while (GetMessage(&msg, NULL, 0,0 ))
  97.         {
  98.         if (!TranslateAccelerator(hWnd, hAccel, &msg))
  99.             {
  100.             TranslateMessage(&msg);
  101.             DispatchMessage(&msg);
  102.             }
  103.         }
  104.  
  105.     FApplicationExit(pGlob);
  106.     return msg.wParam;
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. /*
  114.  * CosmoWndProc
  115.  *
  116.  * Purpose:
  117.  *  Window class procedure.  Standard callback.
  118.  *
  119.  * Parameters:
  120.  *  The standard.
  121.  *
  122.  * Return Value:
  123.  *  The standard.
  124.  *
  125.  */
  126.  
  127. LRESULT WINAPI CosmoWndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  128.     {
  129.     static BOOL fNoSizePoly;
  130.     RECT        rc;
  131.     BOOL        fOK;
  132.     UINT        uTemp;
  133.     DWORD       dwStyle;
  134.     WORD        wID=LOWORD(wParam);
  135.  
  136.    #ifdef WIN32
  137.     WORD        wCode=HIWORD(wParam);
  138.     HWND        hWndMsg=(HWND)(UINT)lParam;
  139.    #else
  140.     WORD        wCode=HIWORD(lParam);
  141.     HWND        hWndMsg=(HWND)(UINT)lParam;
  142.    #endif
  143.  
  144.     switch (iMsg)
  145.         {
  146.         case WM_CREATE:
  147.             //Set global variable defaults that pertain to this window.
  148.             pGlob->hWnd=hWnd;
  149.             pGlob->fOpenFile=FALSE;
  150.             pGlob->fNoDirty=FALSE;
  151.  
  152.             FDirtySet(FALSE);
  153.  
  154.             //Create the secondary edit window.
  155.             pGlob->hWndPolyline=HPolylineWindowCreate(hWnd, pGlob->hInst);
  156.  
  157.             //Set the initial window title.
  158.             WindowTitleSet(pGlob->hWnd, rgpsz[IDS_UNTITLED]);
  159.  
  160.             if (!FFileInit(pGlob))
  161.                 PostMessage(hWnd, WM_CLOSE, 0, 0L);
  162.  
  163.             fNoSizePoly=FALSE;
  164.             break;
  165.  
  166.  
  167.         case WM_SIZE:
  168.             /* If we are getting WM_SIZE in response to a Polyline
  169.              * notification, then don't resize it again.
  170.              */
  171.  
  172.             if (fNoSizePoly)
  173.                 break;
  174.  
  175.             //Resize the polyline editor window to fit the new client area.
  176.             GetClientRect(hWnd, &rc);
  177.             InflateRect(&rc, -8, -8);
  178.  
  179.             //Tell the Polyline window to resize without notifying us.
  180.             SendMessage(pGlob->hWndPolyline, PLM_RECTSET, FALSE
  181.                 , (LONG)(LPSTR)&rc);
  182.  
  183.             /*
  184.              * We consider sizing something that makes the file dirty, but
  185.              * not until we've finished the create process, which is why
  186.              * we set fDirty to FALSE after the ShowWindow above, because
  187.              * ShowWindow sends a WM_SIZE when the window first appears.
  188.              */
  189.             FDirtySet(TRUE);
  190.             break;
  191.  
  192.         case WM_GETMINMAXINFO:
  193.             {
  194.             //Limit the smallest tracking size allowable.
  195.            #ifdef WIN32
  196.             LPMINMAXINFO    pmmi=(LPMINMAXINFO)lParam;
  197.  
  198.             pmmi->ptMinTrackSize.x=158;
  199.             pmmi->ptMinTrackSize.y=168;
  200.            #else
  201.             LPPOINT     ppt=(LPPOINT)lParam;
  202.  
  203.             ppt[3].x=158;
  204.             ppt[3].y=168;
  205.            #endif
  206.             }
  207.             break;
  208.  
  209.         case WM_CLOSE:
  210.             if (!FFileExit(pGlob))
  211.                 break;
  212.  
  213.             DestroyWindow(hWnd);    //Same as DefWindowProc's action.
  214.             break;
  215.  
  216.  
  217.         case WM_DESTROY:
  218.             PostQuitMessage(0);
  219.             break;
  220.  
  221.  
  222.         case WM_INITMENUPOPUP:
  223.             /*
  224.              * Check for Save possibility in File menu position 0.
  225.              * In and OLE situation, this is always enabled.
  226.              */
  227.             if (!pGlob->fOLE && 0==LOWORD(lParam))
  228.                 {
  229.                 uTemp=(pGlob->fOpenFile) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  230.                 EnableMenuItem((HMENU)wParam, IDM_FILESAVE, uTemp | MF_BYCOMMAND);
  231.                 }
  232.  
  233.             //Check for possibility of Paste for edit menu position 1
  234.             if (1==LOWORD(lParam))
  235.                 {
  236.                 fOK=IsClipboardFormatAvailable(pGlob->cfCosmo);
  237.                 uTemp=(fOK) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  238.                 EnableMenuItem((HMENU)wParam, IDM_EDITPASTE, uTemp | MF_BYCOMMAND);
  239.                 }
  240.  
  241.             break;
  242.  
  243.  
  244.         case WM_COMMAND:
  245.             switch (wID)
  246.                 {
  247.                 case ID_POLYLINE:
  248.                     if (PLN_POINTCHANGE==wCode)
  249.                         {
  250.                         FDirtySet(TRUE);
  251.                         break;
  252.                         }
  253.  
  254.                     /*
  255.                      * Polyline window is informing us that it changed size
  256.                      * in response to setting it's data.  Therefore we
  257.                      * have to size ourselves accordingly.
  258.                      */
  259.                     if (PLN_SIZECHANGE==wCode)
  260.                         {
  261.                         //Calculate new DIMENSIONS (we won't move)
  262.                         GetWindowRect(pGlob->hWndPolyline, &rc);
  263.                         InflateRect(&rc, 8, 8);
  264.  
  265.                         dwStyle=GetWindowLong(pGlob->hWnd, GWL_STYLE);
  266.                         AdjustWindowRect(&rc, dwStyle, TRUE);
  267.  
  268.                         fNoSizePoly=TRUE;
  269.                         SetWindowPos(pGlob->hWnd, NULL, 0, 0
  270.                             , rc.right-rc.left, rc.bottom-rc.top
  271.                             , SWP_NOMOVE | SWP_NOZORDER);
  272.  
  273.                         fNoSizePoly=FALSE;
  274.                         FDirtySet(TRUE);
  275.                         }
  276.                     break;
  277.  
  278.  
  279.                 case IDM_FILENEW:
  280.                     FFileNew(pGlob);
  281.                     break;
  282.  
  283.  
  284.                 case IDM_FILEOPEN:
  285.                     FFileOpen(pGlob, FALSE);
  286.                     break;
  287.  
  288.  
  289.                 case IDM_FILESAVE:
  290.                     FFileSave(pGlob);
  291.                     break;
  292.  
  293.  
  294.                 case IDM_FILESAVEAS:
  295.                     fOK=FFileSaveAs(pGlob);
  296.                     return MAKELONG(fOK, 0);
  297.  
  298.  
  299.                 case IDM_FILEIMPORT:
  300.                     FFileOpen(pGlob, TRUE);
  301.                     break;
  302.  
  303.  
  304.                 case IDM_FILEEXIT:
  305.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  306.                     break;
  307.  
  308.  
  309.                 case IDM_EDITUNDO:
  310.                     SendMessage(pGlob->hWndPolyline, PLM_BACKUPUNDO, 0, 0L);
  311.                     break;
  312.  
  313.                 case IDM_EDITCUT:
  314.                     FEditCut(pGlob);
  315.                     break;
  316.  
  317.                 case IDM_EDITCOPY:
  318.                     FEditCopy(pGlob, FALSE);
  319.                     break;
  320.  
  321.                 case IDM_EDITPASTE:
  322.                     FEditPaste(pGlob);
  323.                     break;
  324.  
  325.  
  326.                 case IDM_HELPABOUT:
  327.                    #ifdef WIN32
  328.                     DialogBox(pGlob->hInst, MAKEINTRESOURCE(IDD_ABOUT)
  329.                         , pGlob->hWnd, AboutProc);
  330.                    #else
  331.                     {
  332.                     DLGPROC     lpfn;
  333.  
  334.                     lpfn=(DLGPROC)MakeProcInstance((FARPROC)AboutProc, pGlob->hInst);
  335.                     DialogBox(pGlob->hInst, MAKEINTRESOURCE(IDD_ABOUT)
  336.                         , pGlob->hWnd, lpfn);
  337.                     FreeProcInstance((FARPROC)lpfn);
  338.                     }
  339.                    #endif
  340.  
  341.                     break;
  342.                 }
  343.             break;
  344.  
  345.         default:
  346.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  347.         }
  348.  
  349.     return 0L;
  350.     }
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357. /*
  358.  * AboutProc
  359.  *
  360.  * Purpose:
  361.  *  Dialog procedure for the omnipresent About box.
  362.  *
  363.  * Parameters:
  364.  *  The standard.
  365.  *
  366.  * Return Value:
  367.  *  The value to be returned through the DialogBox call that
  368.  *  created the dialog.
  369.  *
  370.  */
  371.  
  372. BOOL WINAPI AboutProc(HWND hDlg, UINT iMsg, UINT wParam, LONG lParam)
  373.     {
  374.     switch (iMsg)
  375.         {
  376.         case WM_INITDIALOG:
  377.             return TRUE;
  378.  
  379.         case WM_COMMAND:
  380.             switch (LOWORD(wParam))
  381.                 {
  382.                 case IDOK:
  383.                     EndDialog(hDlg, TRUE);
  384.                 }
  385.             break;
  386.         }
  387.     return FALSE;
  388.     }
  389.