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

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