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

  1. /*
  2.  * CDOCUMNT.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Implementation of the CDocument class.
  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. #include <windows.h>
  16. #include <string.h>
  17. #include "classlib.h"
  18.  
  19.  
  20. /*
  21.  * CDocument::CDocument
  22.  * CDocument::~CDocument
  23.  *
  24.  * Constructor Parameters:
  25.  *  hInst           HINSTANCE of the application.
  26.  *  pFR             PCFrame, a back pointer to the frame window.
  27.  *  pAdv            PCDocumentAdviseSink to notify on events.
  28.  */
  29.  
  30. CDocument::CDocument(HINSTANCE hInst, PCFrame pFR
  31.     , PCDocumentAdviseSink pAdv)
  32.     : CWindow(hInst)
  33.     {
  34.     m_pFR=pFR;
  35.     m_pST=NULL;
  36.     m_pAdv=pAdv;
  37.     return;
  38.     }
  39.  
  40.  
  41. CDocument::~CDocument(void)
  42.     {
  43.     if (NULL!=m_pST)
  44.         delete m_pST;
  45.  
  46.     return;
  47.     }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /*
  55.  * CDocument::Init
  56.  *
  57.  * Purpose:
  58.  *  Initializes an already created document window.  The client
  59.  *  actually creates the window then passes that here for further
  60.  *  initialization.
  61.  *
  62.  * Parameters:
  63.  *  pDI             PDOCUMENTINIT containing initialization params
  64.  *
  65.  * Return Value:
  66.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  67.  */
  68.  
  69. BOOL CDocument::Init(PDOCUMENTINIT pDI)
  70.     {
  71.     if (NULL==pDI)
  72.         return FALSE;
  73.  
  74.     if (NULL==pDI->hWndDoc)
  75.         return FALSE;
  76.  
  77.     //Create our stringtable
  78.     m_pST=new CStringTable(m_hInst);
  79.  
  80.     if (!m_pST->Init(pDI->idsMin, pDI->idsMax))
  81.         return FALSE;
  82.  
  83.     m_hWnd=pDI->hWndDoc;
  84.     m_cf=RegisterClipboardFormat(PSZ(IDS_CLIPBOARDFORMAT));
  85.  
  86.     m_fDirty=FALSE;
  87.     m_fNoDirty=FALSE;
  88.     m_fNoSize=FALSE;
  89.  
  90.     m_fFileKnown=FALSE;
  91.     m_szFile[0]=0;
  92.  
  93.     return TRUE;
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. /*
  104.  * CDocument::FMessageHook
  105.  *
  106.  * Purpose:
  107.  *  Provides a derivation of the base CDocument class to hook all
  108.  *  messages to the window procedure for special processing,
  109.  *  including WM_COMMAND since that is much less common for a
  110.  *  document than for a frame (see CFrame::OnCommand).
  111.  *
  112.  * Parameters:
  113.  *  <WndProc Parameters>
  114.  *  pLRes           LRESULT * in which to store the return value
  115.  *                  for the message.
  116.  *
  117.  * Return Value:
  118.  *  BOOL            TRUE to prevent further processing, FALSE
  119.  *                  otherwise.
  120.  */
  121.  
  122. BOOL CDocument::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  123.     , LPARAM lParam, LRESULT *pLRes)
  124.     {
  125.     *pLRes=0;
  126.     return FALSE;
  127.     }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.  * CDocument::FDirtySet
  136.  *
  137.  * Purpose:
  138.  *  Sets or clears the document 'dirty' flag returning the previous
  139.  *  state of that same flag.
  140.  *
  141.  * Parameters:
  142.  *  fDirty          BOOL indicating the new contents of the dirty
  143.  *                  flag.
  144.  *
  145.  * Return Value:
  146.  *  BOOL            Previous value of the dirty flag.
  147.  */
  148.  
  149. BOOL CDocument::FDirtySet(BOOL fDirty)
  150.     {
  151.     BOOL        fPrevious;
  152.  
  153.     /*
  154.      * If we are a hidden window, then there's nothing that could
  155.      * make us dirty since there cannot be any user interaction here.
  156.      */
  157.     if (!IsWindowVisible(m_hWnd))
  158.         return m_fDirty;
  159.  
  160.     //Ignore the initial WM_SIZE on creation.
  161.     if (m_fNoDirty)
  162.         return m_fDirty;
  163.  
  164.     fPrevious=m_fDirty;
  165.     m_fDirty=fDirty;
  166.  
  167.     return fPrevious;
  168.     }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /*
  175.  * CDocument::FDirtyGet
  176.  *
  177.  * Purpose:
  178.  *  Returns the current dirty status of the document.
  179.  *
  180.  * Parameters:
  181.  *  None
  182.  *
  183.  * Return Value:
  184.  *  BOOL            TRUE if the file is clean, FALSE otherwise.
  185.  */
  186.  
  187. BOOL CDocument::FDirtyGet()
  188.     {
  189.     return m_fDirty;
  190.     }
  191.  
  192.  
  193.  
  194.  
  195.  
  196. /*
  197.  * CDocument::Clear
  198.  *
  199.  * Purpose:
  200.  *  Sets all contents in the document back to defaults with no
  201.  *  filename.
  202.  *
  203.  * Paramters:
  204.  *  None
  205.  *
  206.  * Return Value:
  207.  *  None
  208.  */
  209.  
  210. void CDocument::Clear()
  211.     {
  212.     FDirtySet(FALSE);
  213.     Rename(NULL);
  214.     return;
  215.     }
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. /*
  223.  * CDocument::Load
  224.  *
  225.  * Purpose:
  226.  *  Function that derived classes override.  Does nothing in base
  227.  *  class.
  228.  *
  229.  * Parameters:
  230.  *  fChangeFile     BOOL indicating if we're to update the window
  231.  *                  title and the filename from using this file.
  232.  *  pszFile         LPTSTR to the filename to load.  If NULL then this
  233.  *                  is a new, untitled file, so perform any
  234.  *                  initialization for such a case.
  235.  *
  236.  * Return Value:
  237.  *  UINT            An error value from DOCERR_.  Always
  238.  *                  DOCERR_NOFILE here.
  239.  */
  240.  
  241. UINT CDocument::Load(BOOL fChangeFile, LPTSTR pszFile)
  242.     {
  243.     Rename(NULL);
  244.     return DOCERR_NOFILE;
  245.     }
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. /*
  254.  * CDocument::Save
  255.  *
  256.  * Purpose:
  257.  *  Function that derived classes override.  Does nothing in base
  258.  *  class.
  259.  *
  260.  * Parameters:
  261.  *  uType           UINT type of file to save (from Save As dialog).
  262.  *  pszFile         LPTSTR providing the name under which we should
  263.  *                  save.  If NULL, then use the current name.  If
  264.  *                  non-NULL, then call Rename if save is successful
  265.  *                  and the name has changed.
  266.  * Return Value:
  267.  *  UINT            An error value from DOCERR_.  Always
  268.  *                  DOCERR_NOFILE here.
  269.  */
  270.  
  271. UINT CDocument::Save(UINT uType, LPTSTR pszFile)
  272.     {
  273.     return DOCERR_NOFILE;
  274.     }
  275.  
  276.  
  277.  
  278.  
  279.  
  280. /*
  281.  * CDocument::ErrorMessage
  282.  *
  283.  * Purpose:
  284.  *  Displays an error message to the user appropriate for a given
  285.  *  error code.  If the code is DOCERR_NONE then this is a NOP.
  286.  *
  287.  * Parameters:
  288.  *  uErr            UINT identifying the error code.
  289.  *
  290.  * Return Value:
  291.  *  None
  292.  */
  293.  
  294. void CDocument::ErrorMessage(UINT uErr)
  295.     {
  296.     LPTSTR      psz;
  297.  
  298.     switch (uErr)
  299.         {
  300.         case DOCERR_NONE:
  301.             psz=NULL;
  302.             break;
  303.  
  304.         case DOCERR_NOFILE:
  305.             psz=PSZ(IDS_FILEDOESNOTEXIST);
  306.             break;
  307.  
  308.         case DOCERR_COULDNOTOPEN:
  309.             psz=PSZ(IDS_FILEOPENERROR);
  310.             break;
  311.  
  312.         case DOCERR_READFAILURE:
  313.             psz=PSZ(IDS_FILELOADERROR);
  314.             break;
  315.  
  316.         case DOCERR_UNSUPPORTEDVERSION:
  317.             psz=PSZ(IDS_VERSIONMISMATCH);
  318.             break;
  319.  
  320.         case DOCERR_WRITEFAILURE:
  321.             psz=PSZ(IDS_FILESAVEERROR);
  322.             break;
  323.  
  324.         case DOCERR_CANCELLED:
  325.             //No message on this.
  326.             return;
  327.  
  328.         default:
  329.             psz=PSZ(IDS_UNKNOWNERROR);
  330.             break;
  331.         }
  332.  
  333.     if (NULL!=psz)
  334.         MessageBox(m_hWnd, psz, PSZ(IDS_DOCUMENTCAPTION), MB_OK);
  335.  
  336.     return;
  337.     }
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348. /*
  349.  * CDocument::Clip
  350.  *
  351.  * Purpose:
  352.  *  Places document data on the clipboard, optionally implementing
  353.  *  Cut by deleting the data in the current window after rendering.
  354.  *
  355.  * Parameters:
  356.  *  hWndFrame       HWND of the main window.
  357.  *  fCut            BOOL indicating cut (TRUE) or copy (FALSE).
  358.  *
  359.  * Return Value:
  360.  *  BOOL            TRUE if successful, FALSE otherwise.
  361.  */
  362.  
  363. BOOL CDocument::Clip(HWND hWndFrame, BOOL fCut)
  364.     {
  365.     return FALSE;
  366.     }
  367.  
  368.  
  369.  
  370.  
  371. /*
  372.  * CDocument::RenderFormat
  373.  *
  374.  * Purpose:
  375.  *  Returns a global memory handle containing a specific clipboard
  376.  *  format.
  377.  *
  378.  * Parameters:
  379.  *  cf              UINT format to render
  380.  *
  381.  * Return Value:
  382.  *  HGLOBAL         Memory handle of the rendering.
  383.  */
  384.  
  385. HGLOBAL CDocument::RenderFormat(UINT cf)
  386.     {
  387.     return NULL;
  388.     }
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395. /*
  396.  * CDocument::FQueryPaste
  397.  *
  398.  * Purpose:
  399.  *  Determines if we can paste data from the clipboard.
  400.  *
  401.  * Parameters:
  402.  *  None
  403.  *
  404.  * Return Value:
  405.  *  BOOL            TRUE if data is available, FALSE otherwise.
  406.  */
  407.  
  408. BOOL CDocument::FQueryPaste(void)
  409.     {
  410.     return IsClipboardFormatAvailable(m_cf);
  411.     }
  412.  
  413.  
  414.  
  415.  
  416.  
  417. /*
  418.  * CDocument::Paste
  419.  *
  420.  * Purpose:
  421.  *  Retrieves the private data format from the clipboard and sets it
  422.  *  as the current data.
  423.  *
  424.  *  Note that if this function is called, then the clipboard format
  425.  *  is available because the Paste menu item is only enabled if the
  426.  *  format is present.
  427.  *
  428.  * Parameters:
  429.  *  hWndFrame       HWND of the main window
  430.  *
  431.  * Return Value:
  432.  *  BOOL            TRUE if successful, FALSE otherwise.
  433.  */
  434.  
  435. BOOL CDocument::Paste(HWND hWndFrame)
  436.     {
  437.     return FALSE;
  438.     }
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446. /*
  447.  * CDocument::Undo
  448.  *
  449.  * Purpose:
  450.  *  Reverses a previous action.
  451.  *
  452.  * Parameters:
  453.  *  None
  454.  *
  455.  * Return Value:
  456.  *  None
  457.  */
  458.  
  459. void CDocument::Undo(void)
  460.     {
  461.     return;
  462.     }
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. /*
  470.  * CDocument::FQuerySave
  471.  *
  472.  * Purpose:
  473.  *  Returns whether or not a call to FSave will work.
  474.  *
  475.  * Parameters:
  476.  *  None
  477.  *
  478.  * Return Value:
  479.  *  BOOL            TRUE if we have a known file, FALSE otherwise.
  480.  */
  481.  
  482. BOOL CDocument::FQuerySave(void)
  483.     {
  484.     return m_fFileKnown;
  485.     }
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492. /*
  493.  * CDocument::Rename
  494.  *
  495.  * Purpose:
  496.  *  Changes the name of the current file we're dealing with but
  497.  *  does no disk action.  FSave should be used after this
  498.  *  call to implement a Save As operation.
  499.  *
  500.  * Parameters:
  501.  *  pszFile         LPTSTR to the filename to save ourselves under.
  502.  *
  503.  * Return Value:
  504.  *  None
  505.  */
  506.  
  507. void CDocument::Rename(LPTSTR pszFile)
  508.     {
  509.     //Clear out the filename on pszFile==NULL.
  510.     if (NULL==pszFile)
  511.         {
  512.         m_szFile[0]=0;
  513.         m_fFileKnown=FALSE;
  514.  
  515.         //Tell the associate window to change captions.
  516.         if (NULL!=m_pAdv)
  517.             m_pAdv->OnCaptionChange(this);
  518.         }
  519.  
  520.     //First, check if anything changed
  521.     if (NULL!=pszFile && 0!=lstrcmpi(m_szFile, pszFile))
  522.         {
  523.         /*
  524.          * Copy the new filename to the document structure and inform
  525.          * associate
  526.          */
  527.         lstrcpyn(m_szFile, pszFile, CCHPATHMAX);
  528.  
  529.         if (NULL!=m_pAdv)
  530.             m_pAdv->OnCaptionChange(this);
  531.  
  532.         m_fFileKnown=TRUE;
  533.         }
  534.  
  535.     return;
  536.     }
  537.  
  538.  
  539.  
  540.  
  541.  
  542. /*
  543.  * CDocument::FilenameGet
  544.  *
  545.  * Purpose:
  546.  *  Retrieves the current filename used in the document.
  547.  *
  548.  * Parameters:
  549.  *  psz             LPTSTR in which to store the filename.
  550.  *  cchMax          UINT maximum number of characters to copy.
  551.  *
  552.  * Return Value:
  553.  *  UINT            Number of characters copied.
  554.  */
  555.  
  556. UINT CDocument::FilenameGet(LPTSTR psz, UINT cchMax)
  557.     {
  558.     UINT        uRet=0;
  559.  
  560.     if (NULL!=psz)
  561.         {
  562.         lstrcpyn(psz, m_szFile, cchMax);
  563.         uRet=lstrlen(m_szFile);
  564.         }
  565.  
  566.     return uRet;
  567.     }
  568.  
  569.  
  570.  
  571.  
  572. /*
  573.  * CDocument::FrameGet
  574.  *
  575.  * Purpose:
  576.  *  Returns the CFrame pointer of the application frame.
  577.  *
  578.  * Parameters:
  579.  *  None
  580.  *
  581.  * Return Value:
  582.  *  PCFrame         Pointer to the frame object.
  583.  */
  584.  
  585. PCFrame CDocument::FrameGet(void)
  586.     {
  587.     return m_pFR;
  588.     }
  589.