home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / WEXAMPLE.ZIP / TODOWIN.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  9.7 KB  |  255 lines

  1. #if !defined( __TODOWIN_H )
  2. #define __TODOWIN_H
  3.  
  4. //---------------------------------------------------------------------
  5. //
  6. //  TODOWIN.H
  7. //
  8. //      Copyright (c) 1991, 1992 by Borland International
  9. //      All Rights Reserved.
  10. //
  11. //  Defines the following classes, which are useful for building
  12. //  general purpose windows applications:
  13. //
  14. //  WinBase - provides basic data and functionality for
  15. //            windows programming.
  16. //
  17. //  ModalDialog - provides for modal dialog boxes.  Inherits from
  18. //            WinBase, which is a virtual base.
  19. //
  20. //  Window  - provides core functionality for a window under Windows.
  21. //            Inherits from WinBase, which is a virtual base.
  22. //
  23. //---------------------------------------------------------------------
  24.  
  25. #if !defined( __WINDOWS_H )
  26. #define STRICT
  27. #include <Windows.h>
  28. #endif  // __WINDOWS_H
  29.  
  30. #if !defined( __ASSERT_H )
  31. #include <assert.h>
  32. #endif  // __ASSERT_H
  33.  
  34. //---------------------------------------------------------------------
  35. //
  36. //  class WinBase
  37. //
  38. //      provides the basic data and functionality for all classes
  39. //      used in the windows application.  It is an abstract base class.
  40. //
  41. //---------------------------------------------------------------------
  42.  
  43. class WinBase
  44. {
  45.  
  46. public:
  47.  
  48.     virtual WORD run() = 0;     // the core function of all windows!  For
  49.                                 // the main application window, this
  50.                                 // provides the message loop.  In modal
  51.                                 // dialogs, it sets up the dialog box,
  52.                                 // calls the dialog proc, and closes down
  53.                                 // the dialog.
  54.  
  55.  
  56.  
  57.     static  HINSTANCE hInst;    // the handle of the current instance
  58.  
  59.     static  HINSTANCE hPrevInst;// the handle of the previous instance
  60.  
  61.     static  LPSTR cmd;          // pointer to the command line
  62.  
  63.     static  int show;           // the nCmdShow parameter of the current
  64.                                 // instance
  65.  
  66.     HWND    hWnd();             // access function
  67.  
  68. protected:
  69.  
  70.     HWND hWindow;               // the window handle of the class.  This is
  71.                                 // accessed through hWnd(), and it will
  72.                                 // provide the correct handle for any
  73.                                 // derived class.
  74.  
  75.                                 // NOTE: this field is not initialized by
  76.                                 // the constructor of this class.  It must
  77.                                 // be initialized by the constructor of a
  78.                                 // class derived from this class.
  79.  
  80. };
  81.  
  82. //---------------------------------------------------------------------
  83. //
  84. //  class ModalDialog
  85. //
  86. //      provides basic functionality for modal dialog boxes.  It is an
  87. //      abstract base class.
  88. //
  89. //---------------------------------------------------------------------
  90.  
  91. class ModalDialog : public virtual WinBase
  92. {
  93.  
  94. public:
  95.  
  96.     ModalDialog( HWND );        // constructor.  Since a modal dialog
  97.                                 // needs to know its owner, the handle
  98.                                 // of the owner is passed as a parameter
  99.                                 // to the constructor.
  100.  
  101.     ~ModalDialog();
  102.  
  103.     virtual WORD run();         // the core of a modal dialog!  It sets
  104.                                 // up the dialog, executes it, and closes
  105.                                 // it down.
  106.  
  107. protected:
  108.  
  109.     virtual BOOL dispatch( HWND, UINT, WPARAM, LPARAM );
  110.                                 // the core of any window.  Whenever
  111.                                 // dlgProc receives a message it passes
  112.                                 // the message on to dispatch().  If
  113.                                 // dispatch() doesn't handle the message
  114.                                 // itself, it should call the dispatch()
  115.                                 // function in its base class.  Ultimately,
  116.                                 // messages not handled higher up are
  117.                                 // handled by this version of dispatch(),
  118.                                 // which just returns FALSE, indicating
  119.                                 // that the message wasn't handled by
  120.                                 // the dialog box at all.
  121.  
  122.             WORD result;        // this holds the value which will be
  123.                                 // returned by run().  If the dialog box
  124.                                 // needs to return a success code, that
  125.                                 // code should be stored here by the
  126.                                 // dialog handler.
  127.  
  128. private:
  129.  
  130.     virtual LPSTR getDialogName() = 0;
  131.                                 // returns a far pointer to a string
  132.                                 // that contains the name of the dialog
  133.                                 // resource for the current dialog box.
  134.                                 // This is used in run() to load the
  135.                                 // resource.
  136.  
  137.     static  BOOL CALLBACK     _export dlgProc( HWND, UINT, WPARAM, LPARAM );
  138.                                 // the dialog proc that windows calls
  139.                                 // when it has messages for the current
  140.                                 // dialog.
  141.  
  142.     static  ModalDialog *curDlg;
  143.                                 // this holds a pointer to the currenly
  144.                                 // active ModalDialog.  It is set up when
  145.                                 // the constructor is called, and is used
  146.                                 // by dlgProc() to route messages to the
  147.                                 // right dispatch() function.
  148.  
  149. };
  150.  
  151. //---------------------------------------------------------------------
  152. //
  153. //  class Window
  154. //
  155. //      provides the basic functionality for a normal window under
  156. //      windows.
  157. //
  158. //---------------------------------------------------------------------
  159.  
  160. class Window : public virtual WinBase
  161. {
  162.  
  163. public:
  164.  
  165.     virtual BOOL create();      // creates the window.  This involves
  166.                                 // registering the window class if
  167.                                 // it hasn't already been registered,
  168.                                 // creating the actual window, and
  169.                                 // inserting the window into the internal
  170.                                 // window list.
  171.  
  172.     virtual WORD run();         // provides the message loop.
  173.  
  174. protected:
  175.  
  176.     virtual LONG dispatch( UINT, WPARAM, LPARAM );
  177.                                 // dispatches messages in a class derived
  178.                                 // from Window.  When wndProc() receives
  179.                                 // a message, it determines which Window
  180.                                 // object the message should be routed
  181.                                 // to, and calls dispatch() for that object.
  182.                                 // If the dispatch() function in a derived
  183.                                 // class does not handle any particular
  184.                                 // message, it should pass that message
  185.                                 // on down to the dispatch() function in
  186.                                 // its base class.  The version of dispatch()
  187.                                 // in Window itself just calls DefWindowProc.
  188.  
  189.     virtual BOOL registerClass() = 0;
  190.                                 // the derived class should override this
  191.                                 // function and register a window class
  192.                                 // defined appropriately for the program.
  193.  
  194.     virtual BOOL createWindow() = 0;
  195.                                 // the derived class should override this
  196.                                 // function and create a window that's
  197.                                 // appropriate for the program.
  198.  
  199.     static  LRESULT CALLBACK   _export wndProc( HWND, UINT, WPARAM, LPARAM );
  200.                                 // the window proc that windows calls
  201.                                 // when it has messages for any window
  202.                                 // in the current application.  wndProc()
  203.                                 // posts the message to the appropriate
  204.                                 // window.
  205.  
  206.             void insert();      // should be called from createWindow()
  207.                                 // after successfully calling the windows
  208.                                 // function CreateWindow().  insert() adds
  209.                                 // the current object to the Window list,
  210.                                 // enabling normal dispatching of messages
  211.                                 // to the current object.
  212.  
  213. private:
  214.  
  215.     static Window *winList;     // have to maintain a list of Windows so
  216.     Window *nextWin;            // that we can dispatch messages to the
  217.                                 // correct one.
  218.  
  219.     static Window *inCreate;    // sort-of kludgy.  But there are messages
  220.                                 // that are sent to a window during the
  221.                                 // call to CreateWindow().  We assume that
  222.                                 // any message received by wndProc() during
  223.                                 // a call to CreateWindow() is meant for
  224.                                 // the window being created, and dispatch
  225.                                 // those messages to that window.
  226.  
  227. };
  228.  
  229. inline HWND WinBase::hWnd()
  230. {
  231.     return hWindow;
  232. }
  233.  
  234. inline ModalDialog::ModalDialog( HWND hOwner ) : result( 0 )
  235. {
  236.     assert( curDlg == 0 );
  237.     curDlg = this;
  238.     hWindow = hOwner;
  239. }
  240.  
  241. inline ModalDialog::~ModalDialog()
  242. {
  243.     assert( curDlg != 0 );
  244.     curDlg = 0;
  245. }
  246.  
  247. inline void Window::insert()
  248. {
  249.     nextWin = winList;
  250.     winList = this;
  251. }
  252.  
  253. #endif  // __TODOWIN_H
  254.  
  255.