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 / classlib.h < prev    next >
C/C++ Source or Header  |  1995-05-03  |  17KB  |  632 lines

  1. /*
  2.  * CLASSLIB.H
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Master include file for our own Class Library containing:
  6.  *  CStringTable    Class that manages a stringtable.
  7.  *  CHourglass      Class to manage the hourglass.
  8.  *
  9.  *  CWindow         Basic window class for most other classes in here
  10.  *  CToolBar        Wrapper for a toolbar control
  11.  *  CStatusLine     Wrapper for a status lib control
  12.  *
  13.  *  CFrame          Frame window
  14.  *  CClient         Client window with a uniform interface for
  15.  *                  MDI and SDI frames.
  16.  *  CDocument       Document window, also with a uniform interface
  17.  *                  for MDI and SDI.
  18.  *
  19.  *  CDocumentAdviseSink Object implemented in the frame that the
  20.  *                      document notifies of important events.
  21.  *
  22.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  23.  *
  24.  * Kraig Brockschmidt, Microsoft
  25.  * Internet  :  kraigb@microsoft.com
  26.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  27.  */
  28.  
  29.  
  30. #ifndef _CLASSLIB_H_
  31. #define _CLASSLIB_H_
  32.  
  33. #include <book1632.h>
  34.  
  35. //Get default resource definitions
  36. #include "classres.h"
  37.  
  38.  
  39. class CStringTable;
  40. class CHourglass;
  41. class CToolBar;
  42. class CStatusLine;
  43. class CWindow;
  44. class CFrame;
  45. class CClient;
  46. class CDocument;
  47. class CDocumentAdviseSink;
  48. class CHatchWin;
  49.  
  50.  
  51. /**
  52.  ** CStringTable providing string table management for anyone.
  53.  **/
  54.  
  55. class CStringTable
  56.     {
  57.     protected:
  58.         HINSTANCE       m_hInst;
  59.         UINT            m_idsMin;
  60.         UINT            m_idsMax;
  61.         USHORT          m_cStrings;
  62.         LPTSTR          m_pszStrings;
  63.         LPTSTR         *m_ppszTable;
  64.  
  65.     public:
  66.         CStringTable(HINSTANCE);
  67.         ~CStringTable(void);
  68.  
  69.         BOOL Init(UINT, UINT);
  70.  
  71.         //Function to resolve an ID into a string pointer.
  72.         const LPTSTR operator [](const UINT) const;
  73.     };
  74.  
  75.  
  76. typedef CStringTable *PCStringTable;
  77.  
  78. /*
  79.  * Typical classes that initialize a stringtable should have an m_pST
  80.  * member, then they can use this macro conveniently.
  81.  */
  82.  
  83. #define PSZ(i) ((*m_pST)[i])
  84. #define CCHSTRINGMAX            256
  85.  
  86.  
  87. /**
  88.  ** CHourglass:  Manages the hourglass and mouse capture
  89.  **/
  90.  
  91. class CHourglass
  92.     {
  93.     protected:
  94.         HWND            m_hWndCapture;      //Window with capture
  95.         HCURSOR         m_hCur;             //Cursor held.
  96.  
  97.     public:
  98.         CHourglass(void);
  99.         CHourglass(HWND);
  100.         ~CHourglass(void);
  101.     };
  102.  
  103. typedef CHourglass *PCHourglass;
  104.  
  105.  
  106.  
  107. /**
  108.  ** CWindow:  Our basic window class.
  109.  **/
  110.  
  111. class CWindow
  112.     {
  113.     protected:
  114.         HINSTANCE   m_hInst;            //Task instance
  115.         HWND        m_hWnd;             //Window handle of the window
  116.  
  117.     public:
  118.         //Standard Class Functions
  119.         CWindow(HINSTANCE);
  120.         ~CWindow(void);
  121.  
  122.         //Just returns members.  No need to modify
  123.         HWND        Window(void);
  124.         HINSTANCE   Instance(void);
  125.     };
  126.  
  127.  
  128. typedef CWindow * PCWindow;
  129.  
  130.  
  131.  
  132.  
  133. /**
  134.  ** CToolBar encapculates a toolbar control
  135.  **/
  136.  
  137. #include <bttncur.h>
  138. #include <gizmobar.h>
  139.  
  140.  
  141. class CToolBar : public CWindow
  142.     {
  143.     protected:
  144.         UINT            m_cyBar;
  145.  
  146.     public:
  147.         CToolBar(HINSTANCE);
  148.         ~CToolBar(void);
  149.  
  150.         BOOL   Init(HWND, UINT, UINT);
  151.  
  152.         //Window message wrappers
  153.         void   OnSize(HWND);
  154.         void   FontSet(HFONT, BOOL);
  155.         HFONT  FontGet(void);
  156.         void   Enable(BOOL);
  157.  
  158.         //ToolBar function wrappers
  159.         HWND   HwndAssociateSet(HWND);
  160.         HWND   HwndAssociateGet(void);
  161.  
  162.         BOOL   Add(UINT, UINT, UINT, UINT, UINT, LPTSTR, HBITMAP
  163.                    , UINT, UINT);
  164.         BOOL   Remove(UINT);
  165.  
  166.         LONG   SendMessage(UINT, UINT, WPARAM, LPARAM);
  167.  
  168.         BOOL   Show(UINT, BOOL);
  169.         BOOL   Enable(UINT, BOOL);
  170.         BOOL   Check(UINT, BOOL);
  171.         UINT   FocusSet(UINT);
  172.         BOOL   Exist(UINT);
  173.  
  174.         int    TypeGet(UINT);
  175.  
  176.         DWORD  DataSet(UINT, DWORD);
  177.         DWORD  DataGet(UINT);
  178.         BOOL   NotifySet(UINT, BOOL);
  179.         BOOL   NotifyGet(UINT);
  180.  
  181.         int    TextGet(UINT, LPTSTR, UINT);
  182.         void   TextSet(UINT, LPTSTR);
  183.         UINT   IntGet(UINT, BOOL *, BOOL);
  184.         void   IntSet(UINT, int, BOOL);
  185.     };
  186.  
  187.  
  188. typedef CToolBar *PCToolBar;
  189.  
  190.  
  191.  
  192.  
  193.  
  194. /**
  195.  ** CStatusLine encapsulates a status line control.
  196.  **/
  197.  
  198. #include <stastrip.h>
  199.  
  200.  
  201. class CStatusLine : public CWindow
  202.     {
  203.     protected:
  204.         UINT        m_cy;           //Control height
  205.  
  206.     public:
  207.         CStatusLine(HINSTANCE);
  208.         ~CStatusLine(void);
  209.  
  210.         BOOL  Init(HWND, UINT, UINT);
  211.  
  212.         void  OnSize(HWND);
  213.         void  MessageSet(LPTSTR);
  214.         UINT  MessageGet(LPTSTR, UINT);
  215.         UINT  MessageGetLength(void);
  216.         void  FontSet(HFONT, BOOL);
  217.         HFONT FontGet(void);
  218.         void  Enable(BOOL);
  219.  
  220.         BOOL  MessageMap(HWND, HINSTANCE, UINT, UINT, UINT, UINT
  221.                   , UINT, UINT, UINT, UINT, UINT);
  222.         void  MenuSelect(WPARAM, LPARAM);
  223.         void  MessageDisplay(UINT);
  224.     };
  225.  
  226. typedef CStatusLine *PCStatusLine;
  227.  
  228.  
  229.  
  230.  
  231.  
  232. /**
  233.  ** CFrame encapsulates a main application window
  234.  **/
  235.  
  236. //FRAMEWIN.CPP:  Standard window procedure and AboutProc
  237. LRESULT APIENTRY FrameWndProc(HWND, UINT, WPARAM, LPARAM);
  238. BOOL    APIENTRY AboutProc(HWND, UINT, WPARAM, LPARAM);
  239.  
  240. #define CBFRAMEWNDEXTRA     sizeof(LONG)
  241. #define FRAMEWL_STRUCTURE   0
  242.  
  243.  
  244. /*
  245.  * Structure containing resource ranges for initialization of
  246.  * a CFrame object through its Init member.
  247.  */
  248.  
  249. typedef struct tagFRAMEINIT
  250.     {
  251.     UINT    idsMin;         //Stringtable start and end
  252.     UINT    idsMax;
  253.  
  254.     UINT    idsStatMin;     //StatStrip stringtable start and end
  255.     UINT    idsStatMax;
  256.  
  257.     UINT    idStatMenuMin;  //Start and end IDs for StatStrip
  258.     UINT    idStatMenuMax;  //popup menu IDs
  259.  
  260.     UINT    iPosWindowMenu; //Position of the Window menu (MDI init)
  261.     UINT    cMenus;         //Number of popup menus we have.
  262.  
  263.     int     x;              //Window positioning.
  264.     int     y;
  265.     int     cx;
  266.     int     cy;
  267.     } FRAMEINIT, *PFRAMEINIT;
  268.  
  269.  
  270.  
  271. //FRAME.CPP:  Frame object that creates a main window
  272.  
  273. class CFrame : public CWindow
  274.     {
  275.     //Let our window procedure look in our private variables.
  276.     friend LRESULT APIENTRY FrameWndProc(HWND, UINT, WPARAM, LPARAM);
  277.     friend class CDocumentAdviseSink;
  278.  
  279.     protected:
  280.         HINSTANCE       m_hInstPrev;        //WinMain parameters
  281.         LPTSTR          m_pszCmdLine;
  282.         int             m_nCmdShow;
  283.  
  284.         LPTSTR         *m_ppszCmdArgs;      //Command line arguments
  285.         UINT            m_cCmdArgs;
  286.         BOOL            m_fCmdsParsed;      //Been parsed yet?
  287.  
  288.         BOOL            m_fInit;            //Initializing or closing
  289.         BOOL            m_fSizing;          //Inside WM_SIZE
  290.         BOOL            m_fClosing;         //In WM_CLOSE
  291.  
  292.         BOOL            m_fLastEnable;      //Toolbar enable state
  293.         BOOL            m_fLastPaste;
  294.  
  295.         HMENU          *m_phMenu;           //Popup menu handles
  296.         HMENU           m_hMenuWindow;      //"Window" menu
  297.  
  298.         HACCEL          m_hAccel;           //Accelerators
  299.         HBITMAP         m_hBmp;             //Toolbar images
  300.         UINT            m_cyBar;            //Toolbar height
  301.         UINT            m_dxB;              //Toolbar button sizes
  302.         UINT            m_dyB;
  303.  
  304.         PCToolBar       m_pTB;              //Toolbar
  305.         PCStatusLine    m_pSL;              //Status line
  306.  
  307.         PCStringTable   m_pST;              //Stringtable.
  308.         CClient        *m_pCL;              //SDI/MDI client window
  309.  
  310.     protected:
  311.         virtual BOOL     ParseCommandLine(void);
  312.         virtual CClient *CreateCClient(void);
  313.  
  314.         virtual BOOL     RegisterAllClasses(void);
  315.         virtual BOOL     PreShowInit(void);
  316.         virtual void     OpenInitialFiles(void);
  317.         virtual UINT     CreateToolbar(void);
  318.  
  319.         virtual BOOL     FMessageHook(HWND, UINT, WPARAM, LPARAM
  320.                              , LRESULT *);
  321.         virtual LRESULT  OnCommand(HWND, WPARAM, LPARAM);
  322.         virtual void     OnDocumentDataChange(CDocument *);
  323.         virtual void     OnDocumentActivate(CDocument *);
  324.  
  325.         virtual BOOL     SaveOpenDialog(LPTSTR, UINT, UINT, BOOL
  326.                              , UINT *);
  327.         virtual UINT     ReplaceCharWithNull(LPTSTR, int);
  328.         virtual LPTSTR   PszWhiteSpaceScan(LPTSTR, BOOL);
  329.  
  330.     public:
  331.         CFrame(HINSTANCE, HINSTANCE, LPSTR, int);
  332.         virtual ~CFrame(void);
  333.  
  334.         virtual BOOL     Init(PFRAMEINIT);
  335.  
  336.         virtual WPARAM   MessageLoop(void);
  337.         virtual BOOL     AskAndSave(LPTSTR);
  338.         virtual void     UpdateMenus(HMENU, UINT);
  339.         virtual void     UpdateToolbar(void);
  340.         virtual void     WindowTitleSet(CDocument *, BOOL);
  341.  
  342.         virtual PCStatusLine inline StatusLine(void);
  343.     };
  344.  
  345.  
  346. typedef CFrame *PCFrame;
  347.  
  348.  
  349. //Other Miscellaneous CFrame definitions
  350.  
  351. //ToolBar and StatStrip IDs used in this frame
  352. #define ID_GIZMOBAR         11
  353. #define ID_STATSTRIP        12
  354.  
  355. //Classname
  356. #define SZCLASSFRAME        TEXT("Frame")
  357.  
  358.  
  359.  
  360.  
  361.  
  362. /**
  363.  ** CClient encapsulates an MDI or SDI client window
  364.  **/
  365.  
  366. //Forward reference
  367. class CDocumentAdviseSink;
  368. typedef CDocumentAdviseSink *PCDocumentAdviseSink;
  369.  
  370.  
  371. //CLIENT.CPP
  372. LRESULT APIENTRY SDIClientWndProc(HWND, UINT, WPARAM, LPARAM);
  373.  
  374. #define CBCLIENTWNDEXTRA    sizeof(LONG)
  375. #define CLIENTWL_HWNDDOC    0
  376.  
  377.  
  378. class CClient : public CWindow
  379.     {
  380.     friend LRESULT APIENTRY SDIClientWndProc(HWND, UINT, WPARAM
  381.         , LPARAM);
  382.  
  383.     protected:
  384.         PCFrame         m_pFR;          //Frame window information
  385.         UINT            m_cDoc;         //Count of open documents
  386.         CDocument *     m_pDocLast;     //Last created document
  387.  
  388.         HWND            m_hListDocs;    //List of documents
  389.  
  390.         //This is created to send notifications to the frame
  391.         PCDocumentAdviseSink m_pAdv;
  392.  
  393.     private:
  394.         //This non-overridable sets m_pDocLast
  395.         CDocument         * CreateDoc(void);
  396.  
  397.     protected:
  398.         //Overridable for creating a new CDocument
  399.         virtual CDocument * CreateCDocument(void);
  400.  
  401.     public:
  402.         CClient(HINSTANCE, PCFrame);
  403.         virtual ~CClient(void);
  404.  
  405.         PCFrame                 Frame(void);
  406.         UINT                    DocumentCount(void);
  407.         HWND                    DocumentList(void);
  408.  
  409.         virtual BOOL            Init(HMENU, LPRECT);
  410.  
  411.         virtual BOOL            TranslateAccelerator(LPMSG);
  412.         virtual LRESULT         DefaultFrameProc(HWND, UINT, WPARAM
  413.                                     , LPARAM);
  414.         virtual void            OnWindowCommand(UINT, UINT);
  415.         virtual void            OnSize(UINT, UINT, UINT, UINT);
  416.  
  417.         virtual CDocument *     NewDocument(BOOL);
  418.         virtual CDocument *     ActiveDocument(void);
  419.         virtual BOOL            ShowDocument(CDocument *, BOOL);
  420.         virtual BOOL            SDIVerify(void);
  421.         virtual UINT            CloseDocument(CDocument *);
  422.         virtual BOOL            QueryCloseAllDocuments(BOOL, BOOL);
  423.         virtual BOOL            FCleanVerify(CDocument *);
  424.     };
  425.  
  426.  
  427. typedef CClient *PCClient;
  428.  
  429.  
  430. //Other Miscellaneous CFrame definitions
  431.  
  432. //Document window identifiers
  433. #define ID_DOCUMENT         999     //Internal ID for all documents
  434. #define ID_MDICHILDMIN      1000    //Starting point for MDI doc IDs
  435.  
  436. #define SZCLASSSDICLIENT    TEXT("SDIClient")
  437.  
  438.  
  439.  
  440.  
  441. /**
  442.  ** CDocument encapsulates an MDI or SDI child window
  443.  **/
  444.  
  445.  
  446. //DOCWIN.CPP
  447. LRESULT APIENTRY DocumentWndProc(HWND, UINT, WPARAM, LPARAM);
  448.  
  449.  
  450. //DOCUMENT.CPP
  451. //File-related string lengths.
  452. #define CCHPATHMAX          256
  453. #define CCHFILENAMEMAX      15
  454.  
  455.  
  456. //Window extra bytes and offsets
  457. #define CBDOCUMENTWNDEXTRA  sizeof(LONG)
  458. #define DOCWL_STRUCTURE     0
  459.  
  460.  
  461. //Error codes from Load and Save functions
  462. #define DOCERR_NONE                     0
  463. #define DOCERR_NOFILE                   1
  464. #define DOCERR_COULDNOTOPEN             2
  465. #define DOCERR_READFAILURE              3
  466. #define DOCERR_UNSUPPORTEDVERSION       4
  467. #define DOCERR_WRITEFAILURE             5
  468. #define DOCERR_CANCELLED                6
  469. #define DOCERR_STDMAX                   6
  470.  
  471.  
  472.  
  473.  
  474. /*
  475.  * Structure containing resource ranges and other data for
  476.  * initialization of a CDocument object through its Init member.
  477.  */
  478.  
  479. typedef struct tagDOCUMENTINIT
  480.     {
  481.     UINT        idsMin;    //Stringtable start and end
  482.     UINT        idsMax;
  483.     HWND        hWndDoc;   //HWND of this document, created in client
  484.     } DOCUMENTINIT, *PDOCUMENTINIT;
  485.  
  486.  
  487.  
  488. class CDocument : public CWindow
  489.     {
  490.     friend LRESULT APIENTRY DocumentWndProc(HWND, UINT, WPARAM
  491.         , LPARAM);
  492.  
  493.     protected:
  494.         PCFrame         m_pFR;                //Back pointer
  495.         UINT            m_cf;                 //Clipboard format
  496.         BOOL            m_fDirty;             //Is file dirty?
  497.         BOOL            m_fNoDirty;           //Don't touch dirty flag
  498.         BOOL            m_fNoSize;            //Prevent sizing
  499.         BOOL            m_fFileKnown;         //File/Save allowed?
  500.         TCHAR           m_szFile[CCHPATHMAX]; //Filename for Save
  501.  
  502.         PCStringTable   m_pST;                //Document strings
  503.  
  504.         /*
  505.          * If someone above us wants information, they'll
  506.          * give us this object.
  507.          */
  508.         PCDocumentAdviseSink m_pAdv;
  509.  
  510.     protected:
  511.         virtual BOOL     FMessageHook(HWND, UINT, WPARAM, LPARAM
  512.                              , LRESULT *);
  513.  
  514.     public:
  515.         CDocument(HINSTANCE, PCFrame, PCDocumentAdviseSink);
  516.         virtual ~CDocument(void);
  517.  
  518.         virtual BOOL     Init(PDOCUMENTINIT);
  519.  
  520.         virtual BOOL     FDirtySet(BOOL);
  521.         virtual BOOL     FDirtyGet(void);
  522.         virtual void     Clear(void);
  523.  
  524.         virtual UINT     Load(BOOL, LPTSTR);
  525.         virtual UINT     Save(UINT, LPTSTR);
  526.         virtual void     ErrorMessage(UINT);
  527.  
  528.         virtual BOOL     Clip(HWND, BOOL);
  529.         virtual HGLOBAL  RenderFormat(UINT);
  530.         virtual BOOL     FQueryPaste(void);
  531.         virtual BOOL     Paste(HWND);
  532.         virtual void     Undo(void);
  533.  
  534.         virtual BOOL     FQuerySave(void);
  535.         virtual void     Rename(LPTSTR);
  536.         virtual UINT     FilenameGet(LPTSTR, UINT);
  537.         virtual PCFrame  FrameGet(void);
  538.     };
  539.  
  540. typedef CDocument *PCDocument;
  541.  
  542.  
  543. //Classname for documents, not localized
  544. #define SZCLASSDOCUMENT     TEXT("document")
  545.  
  546.  
  547. //Macro that isolates us from MDI or SDI Def procs
  548. #ifdef MDI
  549. #define DEFDOCUMENTPROC     DefMDIChildProc
  550. #else
  551. #define DEFDOCUMENTPROC     DefWindowProc
  552. #endif
  553.  
  554.  
  555. //Message to get PCDocument from hWnd
  556. #define DOCM_PDOCUMENT      (WM_USER+0)
  557.  
  558.  
  559. /**
  560.  ** CDocumentAdviseSink through which a document notifies the
  561.  ** frame or important events.
  562.  **/
  563.  
  564. class CDocumentAdviseSink
  565.     {
  566.     protected:
  567.         LPVOID      m_pv;               //Customizable structure
  568.  
  569.     public:
  570.         CDocumentAdviseSink(LPVOID);
  571.  
  572.         virtual void OnDataChange(PCDocument);
  573.         virtual void OnCloseRequest(PCDocument);
  574.         virtual void OnSizeChange(PCDocument, LPRECT);
  575.         virtual void OnCaptionChange(PCDocument);
  576.         virtual void OnActivate(PCDocument);
  577.     };
  578.  
  579.  
  580. /**
  581.  ** CHatchWin when used as a parent window creates a thin
  582.  ** hatch border around the child window.
  583.  **/
  584.  
  585. BOOL HatchWindowRegister(HINSTANCE);
  586.  
  587. //Classname
  588. #define SZCLASSHATCHWIN                 TEXT("hatchwin")
  589.  
  590. //Window extra bytes and offsets
  591. #define CBHATCHWNDEXTRA                 (sizeof(LONG))
  592. #define HWWL_STRUCTURE                  0
  593.  
  594. //Notification codes for WM_COMMAND messages
  595. #define HWN_BORDERDOUBLECLICKED         1
  596.  
  597. //Width of the border
  598. #define HATCHWIN_BORDERWIDTHDEFAULT     4
  599.  
  600. class CHatchWin : public CWindow
  601.     {
  602.     friend LRESULT APIENTRY HatchWndProc(HWND, UINT, WPARAM, LPARAM);
  603.  
  604.     public:
  605.         int         m_dBorder;
  606.         int         m_dBorderOrg;
  607.         UINT        m_uID;
  608.         HWND        m_hWndKid;
  609.         HWND        m_hWndAssociate;
  610.         RECT        m_rcPos;
  611.         RECT        m_rcClip;
  612.  
  613.     public:
  614.         CHatchWin(HINSTANCE);
  615.         ~CHatchWin(void);
  616.  
  617.         BOOL        Init(HWND, UINT, HWND);
  618.  
  619.         HWND        HwndAssociateSet(HWND);
  620.         HWND        HwndAssociateGet(void);
  621.  
  622.         void        RectsSet(LPRECT, LPRECT);
  623.         void        ChildSet(HWND);
  624.         void        ShowHatch(BOOL);
  625.     };
  626.  
  627. typedef CHatchWin *PCHatchWin;
  628.  
  629.  
  630.  
  631. #endif //_CLASSLIB_H_
  632.