home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / todo.pak / TODOWIN.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  10KB  |  265 lines

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