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 / classlib / framewin.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  8KB  |  375 lines

  1. /*
  2.  * FRAMEWIN.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Frame window procedure used with the CFrame class as well as
  6.  * the implementation of CDocumentAdviseSink that the frame is
  7.  * interested in.
  8.  *
  9.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Microsoft
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include <windows.h>
  18. #include "classlib.h"
  19.  
  20.  
  21. /*
  22.  * FrameWndProc
  23.  *
  24.  * Purpose:
  25.  *  Frame window class procedure that allows a derivation of these
  26.  *  classes to hook and process any messages desired.  Otherwise this
  27.  *  handles standard commands as well as the status line and menus.
  28.  */
  29.  
  30. LRESULT APIENTRY FrameWndProc(HWND hWnd, UINT iMsg
  31.     , WPARAM wParam, LPARAM lParam)
  32.     {
  33.     PCFrame         pFR;
  34.     PCClient        pCL=NULL;
  35.     WORD            dx, dy;
  36.     LRESULT         lRet;
  37.  
  38.     pFR=(PCFrame)GetWindowLong(hWnd, FRAMEWL_STRUCTURE);
  39.  
  40.     if (NULL!=pFR)
  41.         {
  42.         //Call the hook and return its value if it tells us to.
  43.         if (pFR->FMessageHook(hWnd, iMsg, wParam, lParam, &lRet))
  44.             return lRet;
  45.  
  46.         //Otherwise copy the CClient pointer and continue.
  47.         pCL=pFR->m_pCL;
  48.         }
  49.  
  50.     switch (iMsg)
  51.         {
  52.         case WM_NCCREATE:
  53.             pFR=(PCFrame)((LPCREATESTRUCT)lParam)->lpCreateParams;
  54.  
  55.             SetWindowLong(hWnd, FRAMEWL_STRUCTURE, (LONG)pFR);
  56.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  57.  
  58.         case WM_DESTROY:
  59.             PostQuitMessage(0);
  60.             break;
  61.  
  62.         case WM_CLOSE:
  63.             //Don't close if any children cancel the operation.
  64.             pFR->m_fClosing=TRUE;
  65.  
  66.             if (!pCL->QueryCloseAllDocuments(TRUE, TRUE))
  67.                 {
  68.                 pFR->m_fClosing=FALSE;
  69.                 break;
  70.                 }
  71.  
  72.             //Destroy everything.
  73.             DestroyWindow(hWnd);
  74.             break;
  75.  
  76.  
  77.         case WM_QUERYENDSESSION:
  78.             return pCL->QueryCloseAllDocuments(TRUE, TRUE);
  79.  
  80.  
  81.         case WM_ERASEBKGND:
  82.             //Our client area is never visible, so don't flash
  83.             return TRUE;
  84.  
  85.  
  86.         case WM_SIZE:
  87.             pFR->m_fSizing=TRUE;
  88.             dx=LOWORD(lParam);
  89.             dy=HIWORD(lParam);
  90.  
  91.             //Change the gizmobar and StatStrip widths to match
  92.             pFR->m_pTB->OnSize(hWnd);
  93.             pFR->m_pSL->OnSize(hWnd);
  94.  
  95.             pCL->OnSize(0, pFR->m_cyBar, dx
  96.                 , dy-pFR->m_cyBar-CYSTATSTRIP);
  97.  
  98.             pFR->m_fSizing=FALSE;
  99.             break;
  100.  
  101.  
  102.         case WM_INITMENUPOPUP:
  103.             //Skip the system menu
  104.             if (TRUE==(BOOL)HIWORD(lParam))
  105.                 break;
  106.  
  107.             //Go handle graying menu items appropriately.
  108.             pFR->UpdateMenus((HMENU)wParam, (UINT)LOWORD(lParam));
  109.             break;
  110.  
  111.  
  112.         case WM_MENUSELECT:
  113.             pFR->m_pSL->MenuSelect(wParam, lParam);
  114.             break;
  115.  
  116.  
  117.         case WM_DESTROYCLIPBOARD:
  118.             //When we empty contents, disable the paste button
  119.             pFR->m_pTB->Enable(IDM_EDITPASTE, FALSE);
  120.             break;
  121.  
  122.  
  123.         case WM_COMMAND:
  124.             return pFR->OnCommand(hWnd, wParam, lParam);
  125.  
  126.  
  127.         default:
  128.             //Don't try using CClient unless we have one.
  129.             if (NULL!=pCL)
  130.                 {
  131.                 return pCL->DefaultFrameProc(hWnd, iMsg, wParam
  132.                     , lParam);
  133.                 }
  134.             else
  135.                 return DefWindowProc(hWnd, iMsg, wParam, lParam);
  136.         }
  137.  
  138.     return 0L;
  139.     }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. /*
  147.  * AboutProc
  148.  *
  149.  * Purpose:
  150.  *  Dialog procedure for the omnipresent About box.
  151.  *
  152.  * Parameters:
  153.  *  The standard.
  154.  *
  155.  * Return Value:
  156.  *  The value to be returned through the DialogBox call that
  157.  *  created the dialog.
  158.  *
  159.  */
  160.  
  161. BOOL APIENTRY AboutProc(HWND hDlg, UINT iMsg
  162.     , WPARAM wParam, LPARAM lParam)
  163.     {
  164.     switch (iMsg)
  165.         {
  166.         case WM_INITDIALOG:
  167.             return TRUE;
  168.  
  169.         case WM_COMMAND:
  170.             switch (LOWORD(wParam))
  171.                 {
  172.                 case IDOK:
  173.                     EndDialog(hDlg, TRUE);
  174.                 }
  175.             break;
  176.         }
  177.     return FALSE;
  178.     }
  179.  
  180.  
  181.  
  182.  
  183.  
  184. //CDocumentAdviseSink implementation for the frame window.
  185.  
  186.  
  187.  
  188. /*
  189.  * CDocumentAdviseSink::CDocumentAdviseSink
  190.  *
  191.  * Purpose:
  192.  *  Constructs an advise object storing a pointer for the caller's
  193.  *  use.
  194.  *
  195.  * Parameters:
  196.  *  pv              LPVOID to store in this object
  197.  */
  198.  
  199. CDocumentAdviseSink::CDocumentAdviseSink(LPVOID pv)
  200.     {
  201.     m_pv=pv;
  202.     return;
  203.     }
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * CDocumentAdviseSink::OnDataChange
  210.  *
  211.  * Purpose:
  212.  *  Informs the frame that document data changed so we may need to
  213.  *  update some of our UI.
  214.  *
  215.  * Parameters:
  216.  *  pDoc            PCDocument notifying us.
  217.  *
  218.  * Return Value:
  219.  *  None
  220.  */
  221.  
  222. void CDocumentAdviseSink::OnDataChange(PCDocument pDoc)
  223.     {
  224.     PCFrame     pFR=(PCFrame)m_pv;
  225.  
  226.     pFR->OnDocumentDataChange(pDoc);
  227.     return;
  228.     }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. /*
  238.  * CDocumentAdviseSink::OnCloseRequest
  239.  *
  240.  * Purpose:
  241.  *  A document was closed by the user so we have to effect it here.
  242.  *
  243.  * Parameters:
  244.  *  pDoc            PCDocument notifying us.
  245.  *
  246.  * Return Value:
  247.  *  None
  248.  */
  249.  
  250. void CDocumentAdviseSink::OnCloseRequest(PCDocument pDoc)
  251.     {
  252.     PCFrame     pFR=(PCFrame)m_pv;
  253.  
  254.     //Ask if you want to save.
  255.     if (!pFR->m_pCL->FCleanVerify(pDoc))
  256.         return;
  257.  
  258.     pFR->m_pCL->CloseDocument(pDoc);
  259.     pFR->UpdateToolbar();
  260.  
  261.     return;
  262.     }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269. /*
  270.  * CDocumentAdviseSink::OnSizeChange
  271.  *
  272.  * Purpose:
  273.  *  Notifies us that the document was resized.
  274.  *
  275.  * Parameters:
  276.  *  pDoc            PCDocument notifying us.
  277.  *  pRect           LPRECT of the new document window size.
  278.  *
  279.  * Return Value:
  280.  *  None
  281.  */
  282.  
  283. void CDocumentAdviseSink::OnSizeChange(PCDocument pDoc, LPRECT pRect)
  284.     {
  285.     PCFrame     pFR=(PCFrame)m_pv;
  286.    #ifndef MDI
  287.     RECT        rc;
  288.     DWORD       dwStyle;
  289.    #endif
  290.  
  291.     //MDI, do nothing.  In SDI, resize the frame to match
  292.    #ifdef MDI
  293.     return;
  294.    #else
  295.     //If we caused this from our own WM_SIZE, skip out.
  296.     if (pFR->m_fSizing)
  297.         return;
  298.  
  299.     //Get the document's screen rectangle.
  300.     GetWindowRect(pDoc->Window(), &rc);
  301.  
  302.     //Adjust for GizmoBar
  303.     rc.top-=pFR->m_cyBar;
  304.  
  305.     //Adjust for StatStrip
  306.     rc.bottom+=CYSTATSTRIP;
  307.  
  308.     //Adjust for our window
  309.     dwStyle=GetWindowLong(pFR->m_hWnd, GWL_STYLE);
  310.     AdjustWindowRect(&rc, dwStyle, TRUE);
  311.  
  312.     //This causes sizing of all other tools.
  313.     SetWindowPos(pFR->m_hWnd, NULL, rc.left, rc.top, rc.right-rc.left
  314.         , rc.bottom-rc.top, SWP_NOZORDER | SWP_NOACTIVATE);
  315.  
  316.     return;
  317.    #endif
  318.     }
  319.  
  320.  
  321.  
  322.  
  323. /*
  324.  * CDocumentAdviseSink::OnCaptionChange
  325.  *
  326.  * Purpose:
  327.  *  Notifies us that we should update the caption bars used on this
  328.  *  document.
  329.  *
  330.  * Parameters:
  331.  *  pDoc            PCDocument notifying us.
  332.  *
  333.  * Return Value:
  334.  *  None
  335.  */
  336.  
  337. void CDocumentAdviseSink::OnCaptionChange(PCDocument pDoc)
  338.     {
  339.     PCFrame     pFR=(PCFrame)m_pv;
  340.  
  341.    #ifdef MDI
  342.     pFR->WindowTitleSet(pDoc, TRUE);
  343.    #else
  344.     pFR->WindowTitleSet(pDoc, FALSE);
  345.    #endif
  346.  
  347.     return;
  348.     }
  349.  
  350.  
  351.  
  352.  
  353.  
  354. /*
  355.  * CDocumentAdviseSink::OnActivate
  356.  *
  357.  * Purpose:
  358.  *  Notifies us that we should update any user interface that
  359.  *  depends on the active document.
  360.  *
  361.  * Parameters:
  362.  *  pDoc            PCDocument notifying us.
  363.  *
  364.  * Return Value:
  365.  *  None
  366.  */
  367.  
  368. void CDocumentAdviseSink::OnActivate(PCDocument pDoc)
  369.     {
  370.     PCFrame     pFR=(PCFrame)m_pv;
  371.  
  372.     pFR->OnDocumentActivate(pDoc);
  373.     return;
  374.     }
  375.