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

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Patron Chapter 1
  4.  *
  5.  * Implementation of the CPatronDoc derivation of CDocument that
  6.  * manages pages for us.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17. #include <memory.h>
  18. #include <dlgs.h>       //Pring Dlg button IDs
  19.  
  20.  
  21. /*
  22.  * CPatronDoc::CPatronDoc
  23.  * CPatronDoc::~CPatronDoc
  24.  *
  25.  * Constructor Parameters:
  26.  *  hInst           HINSTANCE of the application.
  27.  *  pFR             PCFrame of the frame object.
  28.  *  pAdv            PCDocumentAdviseSink to notify on events.
  29.  */
  30.  
  31. CPatronDoc::CPatronDoc(HINSTANCE hInst, PCFrame pFR
  32.     , PCDocumentAdviseSink pAdv)
  33.     : CDocument(hInst, pFR, pAdv)
  34.     {
  35.     m_pPG=NULL;
  36.     m_lVer=VERSIONCURRENT;
  37.     return;
  38.     }
  39.  
  40.  
  41. CPatronDoc::~CPatronDoc(void)
  42.     {
  43.     if (NULL!=m_pPG)
  44.         delete m_pPG;
  45.  
  46.     return;
  47.     }
  48.  
  49.  
  50.  
  51.  
  52.  
  53. /*
  54.  * CPatronDoc::Init
  55.  *
  56.  * Purpose:
  57.  *  Initializes an already created document window.  The client
  58.  *  actually creates the window for us, then passes that here for
  59.  *  further initialization.
  60.  *
  61.  * Parameters:
  62.  *  pDI             PDOCUMENTINIT containing initialization
  63.  *                  parameters.
  64.  *
  65.  * Return Value:
  66.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  67.  */
  68.  
  69. BOOL CPatronDoc::Init(PDOCUMENTINIT pDI)
  70.     {
  71.     //Change the stringtable range to our customization.
  72.     pDI->idsMin=IDS_DOCUMENTMIN;
  73.     pDI->idsMax=IDS_DOCUMENTMAX;
  74.  
  75.     //Do default initialization
  76.     if (!CDocument::Init(pDI))
  77.         return FALSE;
  78.  
  79.     //Pages are created when we get a Load later.
  80.     return TRUE;
  81.     }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. /*
  89.  * CPatronDoc::FMessageHook
  90.  *
  91.  * Purpose:
  92.  *  Processes WM_SIZE for the document so we can resize the Pages
  93.  *  window.
  94.  *
  95.  * Parameters:
  96.  *  <WndProc Parameters>
  97.  *  pLRes           LRESULT * in which to store the return
  98.  *                  value for the message.
  99.  *
  100.  * Return Value:
  101.  *  BOOL            TRUE to prevent further processing,
  102.  *                  FALSE otherwise.
  103.  */
  104.  
  105. BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  106.     , LPARAM lParam, LRESULT *pLRes)
  107.     {
  108.     UINT        dx, dy;
  109.     RECT        rc;
  110.  
  111.     *pLRes=0;
  112.  
  113.     //Eat to prevent flickering
  114.     if (WM_ERASEBKGND==iMsg)
  115.         return TRUE;
  116.  
  117.     if (WM_SIZE==iMsg && NULL!=m_pPG)
  118.         {
  119.         dx=LOWORD(lParam);
  120.         dy=HIWORD(lParam);
  121.  
  122.         if (SIZE_MINIMIZED!=wParam)
  123.             {
  124.             //Resize Pages window to fit the new document size.
  125.             GetClientRect(hWnd, &rc);
  126.             m_pPG->RectSet(&rc, FALSE);
  127.             }
  128.         }
  129.  
  130.     /*
  131.      * We return FALSE even on WM_SIZE so we can let the default
  132.      * procedure handle maximized MDI child windows appropriately.
  133.      */
  134.     return FALSE;
  135.     }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. /*
  142.  * CPatronDoc::Clear
  143.  *
  144.  * Purpose:
  145.  *  Sets all contents in the document back to defaults with no
  146.  *  filename.
  147.  *
  148.  * Paramters:
  149.  *  None
  150.  *
  151.  * Return Value:
  152.  *  None
  153.  */
  154.  
  155. void CPatronDoc::Clear(void)
  156.     {
  157.     //Completely reset the pages
  158.     m_pPG->New();
  159.  
  160.     CDocument::Clear();
  161.     m_lVer=VERSIONCURRENT;
  162.     return;
  163.     }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /*
  170.  * CPatronDoc::Load
  171.  *
  172.  * Purpose:
  173.  *  Loads a given document without any user interface overwriting
  174.  *  the previous contents of the editor.
  175.  *
  176.  * Parameters:
  177.  *  fChangeFile     BOOL indicating if we're to update the window
  178.  *                  title and the filename from using this file.
  179.  *  pszFile         LPTSTR to the filename to load.  Could be NULL
  180.  *                  for an untitled document.
  181.  *
  182.  * Return Value:
  183.  *  UINT            An error value from DOCERR_*
  184.  */
  185.  
  186. UINT CPatronDoc::Load(BOOL fChangeFile, LPTSTR pszFile)
  187.     {
  188.     RECT        rc;
  189.  
  190.     //We don't support opening anything yet.
  191.     if (NULL!=pszFile)
  192.         return DOCERR_NONE;
  193.  
  194.     //Attempt to create our contained Pages window.
  195.     m_pPG=new CPages(m_hInst);
  196.     GetClientRect(m_hWnd, &rc);
  197.  
  198.     if (!m_pPG->Init(m_hWnd, &rc, WS_CHILD | WS_VISIBLE
  199.         , ID_PAGES, NULL))
  200.         return DOCERR_NOFILE;
  201.  
  202.     //Go initialize the Pages for the default printer.
  203.     if (!PrinterSetup(NULL, TRUE))
  204.         return DOCERR_COULDNOTOPEN;
  205.  
  206.     Rename(NULL);
  207.  
  208.     //Go create an initial page.
  209.     m_pPG->PageInsert(0);
  210.  
  211.     FDirtySet(FALSE);
  212.     return DOCERR_NONE;
  213.     }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /*
  220.  * CPatronDoc::NewPage
  221.  *
  222.  * Purpose:
  223.  *  Creates a new page in the document's pages control after the
  224.  *  current page.
  225.  *
  226.  * Parameters:
  227.  *  None
  228.  *
  229.  * Return Value:
  230.  *  UINT            Index of the new page.
  231.  */
  232.  
  233. UINT CPatronDoc::NewPage(void)
  234.     {
  235.     FDirtySet(TRUE);
  236.     return m_pPG->PageInsert(0);
  237.     }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. /*
  246.  * CPatronDoc::DeletePage
  247.  *
  248.  * Purpose:
  249.  *  Deletes the current page from the document.
  250.  *
  251.  * Parameters:
  252.  *  None
  253.  *
  254.  * Return Value:
  255.  *  UINT            Index of the now current page.
  256.  */
  257.  
  258. UINT CPatronDoc::DeletePage(void)
  259.     {
  260.     FDirtySet(TRUE);
  261.     return m_pPG->PageDelete(0);
  262.     }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. /*
  271.  * CPatronDoc::NextPage
  272.  *
  273.  * Purpose:
  274.  *  Shows the next page in the pages window.
  275.  *
  276.  * Parameters:
  277.  *  None
  278.  *
  279.  * Return Value:
  280.  *  UINT            Index of the new page.
  281.  */
  282.  
  283. UINT CPatronDoc::NextPage(void)
  284.     {
  285.     UINT        iPage;
  286.  
  287.     iPage=m_pPG->CurPageGet();
  288.     return m_pPG->CurPageSet(++iPage);
  289.     }
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297. /*
  298.  * CPatronDoc::PreviousPage
  299.  *
  300.  * Purpose:
  301.  *  Shows the previous page in the pages window.
  302.  *
  303.  * Parameters:
  304.  *  None
  305.  *
  306.  * Return Value:
  307.  *  UINT            Index of the new page.
  308.  */
  309.  
  310. UINT CPatronDoc::PreviousPage(void)
  311.     {
  312.     UINT        iPage;
  313.  
  314.     //If iPage is zero, then we wrap around to the end.
  315.     iPage=m_pPG->CurPageGet();
  316.     return m_pPG->CurPageSet(--iPage);
  317.     }
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324. /*
  325.  * CPatronDoc::FirstPage
  326.  *
  327.  * Purpose:
  328.  *  Shows the first page page in the pages window.
  329.  *
  330.  * Parameters:
  331.  *  None
  332.  *
  333.  * Return Value:
  334.  *  UINT            Index of the new page.
  335.  */
  336.  
  337. UINT CPatronDoc::FirstPage(void)
  338.     {
  339.     return m_pPG->CurPageSet(0);
  340.     }
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347. /*
  348.  * CPatronDoc::LastPage
  349.  *
  350.  * Purpose:
  351.  *  Shows the last page in the pages window.
  352.  *
  353.  * Parameters:
  354.  *  None
  355.  *
  356.  * Return Value:
  357.  *  UINT            Index of the last page.
  358.  */
  359.  
  360. UINT CPatronDoc::LastPage(void)
  361.     {
  362.     return m_pPG->CurPageSet(NOVALUE);
  363.     }
  364.