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 / chap13 / patron / patron.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  11KB  |  501 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Patron Chapter 13
  4.  *
  5.  * Frame window class for Patron.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #define INITGUIDS
  16. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * WinMain
  21.  *
  22.  * Purpose:
  23.  *  Main entry point of application.  Should register the app class
  24.  *  if a previous instance has not done so and do any other one-time
  25.  *  initializations.
  26.  */
  27.  
  28. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  29.     , LPSTR pszCmdLine, int nCmdShow)
  30.     {
  31.     PCPatronFrame   pFR;
  32.     FRAMEINIT       fi;
  33.     WPARAM          wRet=0;
  34.  
  35.     SETMESSAGEQUEUE;
  36.  
  37.     //Attempt to allocate and initialize the application
  38.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  39.  
  40.     if (NULL==pFR)
  41.         return -1;
  42.  
  43.     fi.idsMin=IDS_FRAMEMIN;
  44.     fi.idsMax=IDS_FRAMEMAX;
  45.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  46.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  47.     fi.idStatMenuMin=ID_MENUFILE;
  48.     fi.idStatMenuMax=ID_MENUHELP;
  49.     fi.iPosWindowMenu=WINDOW_MENU;
  50.     fi.cMenus=CMENUS;
  51.  
  52.     fi.x=CW_USEDEFAULT;
  53.     fi.y=CW_USEDEFAULT;
  54.     fi.cx=CW_USEDEFAULT;
  55.     fi.cy=CW_USEDEFAULT;
  56.  
  57.     //If we can initialize pFR, start chugging messages
  58.     if (pFR->Init(&fi))
  59.         wRet=pFR->MessageLoop();
  60.  
  61.     delete pFR;
  62.     return wRet;
  63.     }
  64.  
  65.  
  66.  
  67.  
  68. /*
  69.  * CPatronFrame::CPatronFrame
  70.  * CPatronFrame::~CPatronFrame
  71.  *
  72.  * Constructor Parameters:
  73.  *  hInst           HINSTANCE from WinMain
  74.  *  hInstPrev       HINSTANCE from WinMain
  75.  *  pszCmdLine      LPSTR from WinMain
  76.  *  nCmdShow        int from WInMain
  77.  */
  78.  
  79. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  80.     , LPSTR pszCmdLine, int nCmdShow)
  81.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  82.     {
  83.     m_fInitialized=FALSE;
  84.     m_pIClassDataTran=NULL;
  85.     return;
  86.     }
  87.  
  88.  
  89. CPatronFrame::~CPatronFrame(void)
  90.     {
  91.     if (NULL!=m_pIClassDataTran)
  92.         {
  93.         m_pIClassDataTran->LockServer(FALSE);
  94.         m_pIClassDataTran->Release();
  95.         }
  96.  
  97.     OleFlushClipboard();
  98.  
  99.     if (m_fInitialized)
  100.         OleUninitialize();
  101.     return;
  102.     }
  103.  
  104.  
  105.  
  106.  
  107. /*
  108.  * CPatronFrame::Init
  109.  *
  110.  * Purpose:
  111.  *  Call OleInitialize then calling down into the base class
  112.  *  initialization.
  113.  *
  114.  * Parameters:
  115.  *  pFI             PFRAMEINIT containing initialization
  116.  *                  parameters.
  117.  *
  118.  * Return Value:
  119.  *  BOOL            TRUE if initialization succeeded,
  120.  *                  FALSE otherwise.
  121.  */
  122.  
  123. BOOL CPatronFrame::Init(PFRAMEINIT pFI)
  124.     {
  125.     HRESULT     hr;
  126.  
  127.     CHECKVER_OLE;
  128.  
  129.     if (FAILED(OleInitialize(NULL)))
  130.         return FALSE;
  131.  
  132.     m_fInitialized=TRUE;
  133.  
  134.     /*
  135.      * Obtain the Data Transfer Object class factory and lock it.
  136.      * This will improve the speed of clipboard and (later)
  137.      * drag & drop operations that involve this class.
  138.      */
  139.     hr=CoGetClassObject(CLSID_DataTransferObject
  140.         , CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory
  141.         , (PPVOID)&m_pIClassDataTran);
  142.  
  143.     if (SUCCEEDED(hr))
  144.         m_pIClassDataTran->LockServer(TRUE);
  145.  
  146.     return CFrame::Init(pFI);
  147.     }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. /*
  154.  * CPatronFrame::CreateCClient
  155.  *
  156.  * Purpose:
  157.  *  Constructs a new client specific to the application.
  158.  *
  159.  * Parameters:
  160.  *  None
  161.  *
  162.  * Return Value:
  163.  *  PCClient        Pointer to the new client object.
  164.  */
  165.  
  166. PCClient CPatronFrame::CreateCClient(void)
  167.     {
  168.     return (PCClient)(new CPatronClient(m_hInst, this));
  169.     }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. /*
  177.  * CPatronFrame::RegisterAllClasses
  178.  *
  179.  * Purpose:
  180.  *  Registers all classes used in this application.
  181.  *
  182.  * Parameters:
  183.  *  None
  184.  *
  185.  * Return Value:
  186.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  187.  */
  188.  
  189. BOOL CPatronFrame::RegisterAllClasses(void)
  190.     {
  191.     WNDCLASS        wc;
  192.  
  193.     //First let the standard frame do its thing
  194.     if (!CFrame::RegisterAllClasses())
  195.         return FALSE;
  196.  
  197.     //We need double-clicks now and for object activation later.
  198.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  199.     wc.hInstance     = m_hInst;
  200.     wc.cbClsExtra    = 0;
  201.     wc.lpfnWndProc   = PagesWndProc;
  202.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  203.     wc.hIcon         = NULL;
  204.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  205.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  206.     wc.lpszMenuName  = NULL;
  207.     wc.lpszClassName = SZCLASSPAGES;
  208.  
  209.     if (!RegisterClass(&wc))
  210.         return FALSE;
  211.  
  212.     return TRUE;
  213.     }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /*
  220.  * CPatronFrame::OnCommand
  221.  *
  222.  * Purpose:
  223.  *  WM_COMMAND handler for the Patron frame window that processes
  224.  *  extra File menu items as well as the Page menu.
  225.  *
  226.  * Parameters:
  227.  *  hWnd            HWND of the frame window.
  228.  *  wParam          WPARAM of the message.
  229.  *  lParam          LPARAM of the message.
  230.  *
  231.  * Return Value:
  232.  *  LRESULT         Return value for the message.
  233.  */
  234.  
  235. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam
  236.     , LPARAM lParam)
  237.     {
  238.     PCPatronDoc     pDoc;
  239.  
  240.     COMMANDPARAMS(wID, wCode, hWndMsg);
  241.  
  242.     /*
  243.      * Don't bother with anything during first initialization,
  244.      * skipping many toolbar notifications.
  245.      */
  246.     if (m_fInit)
  247.         return 0L;
  248.  
  249.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  250.  
  251.     switch (wID)
  252.         {
  253.         case IDM_FILEPRINT:
  254.             pDoc->Print(m_hWnd);
  255.             return 0L;
  256.  
  257.         case IDM_FILEPRINTERSETUP:
  258.             pDoc->PrinterSetup(m_hWnd, FALSE);
  259.             return 0L;
  260.  
  261.         case IDM_EDITPASTESPECIAL:
  262.             pDoc->PasteSpecial(m_hWnd);
  263.             return 0L;
  264.  
  265.         case IDM_EDITDELETEOBJECT:
  266.             pDoc->Delete();
  267.             return 0L;
  268.  
  269.         case IDM_PAGENEWPAGE:
  270.             pDoc->NewPage();
  271.             break;
  272.  
  273.         case IDM_PAGEDELETEPAGE:
  274.             pDoc->DeletePage();
  275.             break;
  276.  
  277.         case IDM_PAGENEXTPAGE:
  278.             pDoc->NextPage();
  279.             break;
  280.  
  281.         case IDM_PAGEPREVIOUSPAGE:
  282.             pDoc->PreviousPage();
  283.             break;
  284.  
  285.         case IDM_PAGEFIRSTPAGE:
  286.             pDoc->FirstPage();
  287.             break;
  288.  
  289.         case IDM_PAGELASTPAGE:
  290.             pDoc->LastPage();
  291.             break;
  292.  
  293.  
  294.         default:
  295.            return CFrame::OnCommand(hWnd, wParam, lParam);
  296.         }
  297.  
  298.     return 0L;
  299.     }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. /*
  309.  * CPatronFrame::CreateToolbar
  310.  *
  311.  * Purpose:
  312.  *  Procedure to create all the necessary toolbar buttons.
  313.  *
  314.  * Parameters:
  315.  *  None
  316.  *
  317.  * Return Value:
  318.  *  UINT            Number of tools added to the bar.
  319.  */
  320.  
  321. UINT CPatronFrame::CreateToolbar(void)
  322.     {
  323.     UINT            iLast;
  324.     UINT            uState=GIZMO_NORMAL;
  325.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  326.  
  327.     //Insert the standard ones.
  328.     iLast=CFrame::CreateToolbar();
  329.  
  330.     //Remove Undo:  we don't use it.
  331.     m_pTB->Remove(IDM_EDITUNDO);
  332.  
  333.     /*
  334.      * Insert Print File Import in the 5th position and account
  335.      * for it in iLast.
  336.      */
  337.     m_pTB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB
  338.         , NULL, NULL, 6, uState);
  339.  
  340.     iLast++;
  341.  
  342.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  343.         , NULL, NULL, 0, uState);
  344.  
  345.     //Add New Page, and Delete Page
  346.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEWPAGE, m_dxB, m_dyB
  347.         , NULL, m_hBmp, 2, uState);
  348.     m_pTB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB
  349.         , NULL, m_hBmp, 3, uState);
  350.  
  351.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  352.         , NULL, NULL, 0, uState);
  353.  
  354.     //First, Prev, Next, Last pages.
  355.     m_pTB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE, m_dxB, m_dyB
  356.         , NULL, m_hBmp, 4, uState);
  357.     m_pTB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB
  358.         , NULL, m_hBmp, 5, uState);
  359.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE, m_dxB, m_dyB
  360.         , NULL, m_hBmp, 6, uState);
  361.     m_pTB->Add(utCmd, iLast++, IDM_PAGELASTPAGE, m_dxB, m_dyB
  362.         , NULL, m_hBmp, 7, uState);
  363.  
  364.     return iLast;
  365.     }
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373. /*
  374.  * CPatronFrame::UpdateMenus
  375.  *
  376.  * Purpose:
  377.  *  Handles the WM_INITMENU message for the frame window.  Depending
  378.  *  on the existence of an active window, menu items are selectively
  379.  *  enabled and disabled.
  380.  *
  381.  * Parameters:
  382.  *  hMenu           HMENU of the menu to intialize
  383.  *  iMenu           UINT position of the menu.
  384.  *
  385.  * Return Value:
  386.  *  None
  387.  */
  388.  
  389. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  390.     {
  391.     PCPatronDoc     pDoc;
  392.     BOOL            fOK=FALSE;
  393.     BOOL            fCallDefault=TRUE;
  394.     UINT            uTemp;
  395.     UINT            uTempE;
  396.     UINT            uTempD;
  397.  
  398.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  399.  
  400.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  401.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  402.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  403.  
  404.     if (m_phMenu[0]==hMenu)
  405.         {
  406.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  407.  
  408.         if (NULL!=pDoc)
  409.             fOK=pDoc->FQueryPrinterSetup();
  410.  
  411.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP
  412.             , (fOK) ? uTempE : uTempD);
  413.         }
  414.  
  415.     if (m_phMenu[1]==hMenu)
  416.         {
  417.         if (NULL!=pDoc)
  418.             fOK=pDoc->FQueryPaste();
  419.  
  420.         EnableMenuItem(hMenu, IDM_EDITPASTE
  421.             , (fOK) ? uTempE : uTempD);
  422.         EnableMenuItem(hMenu, IDM_EDITPASTESPECIAL
  423.             , (fOK) ? uTempE : uTempD);
  424.  
  425.         //Cut, Copy, Delete depends on there being a selection.
  426.         if (NULL!=pDoc)
  427.             fOK=pDoc->FQueryObjectSelected(hMenu);
  428.         else
  429.             fOK=FALSE;
  430.  
  431.         EnableMenuItem(hMenu, IDM_EDITCUT, (fOK) ? uTempE : uTempD);
  432.         EnableMenuItem(hMenu, IDM_EDITCOPY
  433.             , (fOK) ? uTempE : uTempD);
  434.         EnableMenuItem(hMenu, IDM_EDITDELETEOBJECT
  435.             , (fOK) ? uTempE : uTempD);
  436.  
  437.         //We did the whole menu...
  438.         fCallDefault=FALSE;
  439.         }
  440.  
  441.     //Page menu
  442.     if (m_phMenu[2]==hMenu)
  443.         {
  444.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  445.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  446.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  447.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  448.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  449.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  450.         }
  451.  
  452.     if (fCallDefault)
  453.         CFrame::UpdateMenus(hMenu, iMenu);
  454.  
  455.     return;
  456.     }
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463. /*
  464.  * CPatronFrame::UpdateToolbar
  465.  *
  466.  * Purpose:
  467.  *  Enables and disables tools depending on whether we have
  468.  *  a document or not.
  469.  *
  470.  * Parameters:
  471.  *  None
  472.  *
  473.  * Return Value:
  474.  *  None
  475.  */
  476.  
  477. void CPatronFrame::UpdateToolbar(void)
  478.     {
  479.     PCDocument  pDoc;
  480.     BOOL        fEnable;
  481.  
  482.     //Let the default hack on its tools.
  483.     CFrame::UpdateToolbar();
  484.  
  485.     pDoc=m_pCL->ActiveDocument();
  486.     fEnable=(NULL!=pDoc);
  487.  
  488.     //No document, disable just about everything
  489.     m_pTB->Enable(IDM_FILEPRINT,        fEnable);
  490.     m_pTB->Enable(IDM_FILEPRINTERSETUP, fEnable);
  491.  
  492.     m_pTB->Enable(IDM_PAGENEWPAGE,      fEnable);
  493.     m_pTB->Enable(IDM_PAGEDELETEPAGE,   fEnable);
  494.     m_pTB->Enable(IDM_PAGEFIRSTPAGE,    fEnable);
  495.     m_pTB->Enable(IDM_PAGEPREVIOUSPAGE, fEnable);
  496.     m_pTB->Enable(IDM_PAGENEXTPAGE,     fEnable);
  497.     m_pTB->Enable(IDM_PAGELASTPAGE,     fEnable);
  498.  
  499.     return;
  500.     }
  501.