home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / POPUP.PAK / POPUP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.4 KB  |  262 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/dc.h>
  9. #include <string.h>
  10.  
  11. #define IDM_COMMANDS 50
  12. const int CM_WINDOW = 100;                // base id for different windows.
  13.  
  14. //
  15. // class TSubWindow
  16. // ~~~~~ ~~~~~~~~~~
  17. class TSubWindow : public TFrameWindow {
  18.   public:
  19.     enum TType { Child, PopParent, PopNoParent };
  20.  
  21.     TSubWindow(TWindow* parent, TType type);
  22.    ~TSubWindow();
  23.  
  24.   protected:
  25.     void EvSize(uint sizeType, TSize& size)
  26.     {
  27.       Invalidate();
  28.       TFrameWindow::EvSize(sizeType, size);
  29.     }
  30.     void Paint(TDC& dc, bool, TRect&);
  31.  
  32.   private:
  33.     TType Type;
  34.  
  35.   DECLARE_RESPONSE_TABLE(TSubWindow);
  36. };
  37.  
  38. DEFINE_RESPONSE_TABLE1(TSubWindow, TFrameWindow)
  39.   EV_WM_SIZE,
  40. END_RESPONSE_TABLE;
  41.  
  42. // pointers to different child windows.
  43. //
  44. TWindow* SubWinPtr[] = { 0, 0, 0 };
  45.  
  46. // Titles for the different child windows.
  47. //
  48. const char* SubWinTitle[] = {
  49.   "Child Window", "Popup with Parent", "Popup without Parent"
  50. };
  51.  
  52. // How the different child windows will be created.
  53. //
  54. long SubWinStyle[] = {
  55.   WS_VISIBLE | WS_CHILD | WS_CAPTION | WS_BORDER
  56.              | WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME,
  57.   WS_VISIBLE | WS_POPUP | WS_OVERLAPPEDWINDOW,
  58.   WS_VISIBLE | WS_POPUP | WS_OVERLAPPEDWINDOW
  59. };
  60.  
  61. // Initial position of child windows.
  62. //
  63. TPoint SubWinPos[] = {
  64.   TPoint(10, 10),
  65.   TPoint(34, 72),
  66.   TPoint(54, 92)
  67. };
  68.  
  69. // Some text to be display in the client area of the child windows.
  70. //
  71. const char* SubWinText[] = {
  72.   "Child windows cannot be moved outside their parent window.  When " \
  73.     "minimized, a child window's icon resides within the parent " \
  74.     "window.",
  75.   "Popup windows can be moved outside their parent window.  A popup " \
  76.     "with a parent is always displayed in front of the parent, " \
  77.     "even when the parent is focused.  To test this, click on the " \
  78.     "parent window.  When minimized, popup icons reside on the desktop.",
  79.   "Popup windows can be moved outside their parent window.  A popup " \
  80.     "without a parent allows the parent to be brought to the front " \
  81.     "when focused. To test this, click on the parent window.  When " \
  82.     "minimized, popup icons reside on the desktop."
  83. };
  84.  
  85. //
  86. // Create window of specified type, indicated by 'type'.
  87. // Title and position of window set according to value of 'type'.
  88. //
  89. TSubWindow::TSubWindow(TWindow* parent, TType type)
  90. :
  91.   TFrameWindow(parent, SubWinTitle[type]),
  92.   Type(type)
  93. {
  94.   Attr.Style = SubWinStyle[Type];
  95.   Attr.X = SubWinPos[Type].x;
  96.   Attr.Y = SubWinPos[Type].y;
  97.   Attr.W = 300;
  98.   Attr.H = 150;
  99. }
  100.  
  101. //
  102. // Destroy window.  SubWinPtr[Type] is set to 0 to indicate that the window
  103. // has be closed.
  104. //
  105. TSubWindow::~TSubWindow()
  106. {
  107.   SubWinPtr[Type] = 0;
  108. }
  109.  
  110. //
  111. // Draw help text in client are of window.
  112. //
  113. void
  114. TSubWindow::Paint(TDC& dc, bool, TRect&)
  115. {
  116.   TRect rect = GetClientRect();
  117.   rect.Inflate(-2, 0);
  118.   dc.DrawText(SubWinText[Type], strlen(SubWinText[Type]), rect, DT_WORDBREAK);
  119. }
  120.  
  121.  
  122. //
  123. // class TMainWindow
  124. // ~~~~~ ~~~~~~~~~~~
  125. class TMainWindow : public TWindow {
  126.   public:
  127.     TMainWindow(TWindow* parent = 0);
  128.  
  129.     void ShowSubWindow(TWindow* parent, TSubWindow::TType type);
  130.     void CmChild();
  131.     void CeChild(TCommandEnabler& ce);
  132.     void CmPopParent();
  133.     void CePopParent(TCommandEnabler& ce);
  134.     void CmPopNoParent();
  135.     void CePopNoParent(TCommandEnabler& ce);
  136.  
  137.   DECLARE_RESPONSE_TABLE(TMainWindow);
  138. };
  139.  
  140. DEFINE_RESPONSE_TABLE1(TMainWindow, TWindow)
  141.   EV_COMMAND       (CM_WINDOW + TSubWindow::Child, CmChild),
  142.   EV_COMMAND_ENABLE(CM_WINDOW + TSubWindow::Child, CeChild),
  143.   EV_COMMAND       (CM_WINDOW + TSubWindow::PopParent, CmPopParent),
  144.   EV_COMMAND_ENABLE(CM_WINDOW + TSubWindow::PopParent, CePopParent),
  145.   EV_COMMAND       (CM_WINDOW + TSubWindow::PopNoParent, CmPopNoParent),
  146.   EV_COMMAND_ENABLE(CM_WINDOW + TSubWindow::PopNoParent, CePopNoParent),
  147. END_RESPONSE_TABLE;
  148.  
  149. TMainWindow::TMainWindow(TWindow* parent)
  150. :
  151.   TWindow(parent, 0)
  152. {
  153. }
  154.  
  155. //
  156. // Create sub-window.  If sub-window, specified by 'type', does not exist
  157. // then create it, else make the sub-window the active window.
  158. //
  159. void
  160. TMainWindow::ShowSubWindow(TWindow* parent, TSubWindow::TType type)
  161. {
  162.   if (!SubWinPtr[type])
  163.     (SubWinPtr[type] = new TSubWindow(parent, type))->Create();
  164.   else {
  165.     SubWinPtr[type]->SetFocus();
  166.     SubWinPtr[type]->SetActiveWindow();
  167.   }
  168. }
  169.  
  170.  
  171. //
  172. // Create the different child windows...
  173. //
  174.  
  175. void
  176. TMainWindow::CmChild()
  177. {
  178.   ShowSubWindow(this, TSubWindow::Child);
  179. }
  180.  
  181. void
  182. TMainWindow::CeChild(TCommandEnabler& ce)
  183. {
  184.   ce.SetCheck(SubWinPtr[TSubWindow::Child] == 0 ?
  185.     TCommandEnabler::Unchecked :
  186.     TCommandEnabler::Checked);
  187. }
  188.  
  189. void
  190. TMainWindow::CmPopParent()
  191. {
  192.   ShowSubWindow(this, TSubWindow::PopParent);
  193. }
  194.  
  195. void
  196. TMainWindow::CePopParent(TCommandEnabler& ce)
  197. {
  198.   ce.SetCheck(SubWinPtr[TSubWindow::PopParent] == 0 ?
  199.     TCommandEnabler::Unchecked :
  200.     TCommandEnabler::Checked);
  201. }
  202.  
  203. void
  204. TMainWindow::CmPopNoParent()
  205. {
  206.   ShowSubWindow(0, TSubWindow::PopNoParent);
  207. }
  208.  
  209. void
  210. TMainWindow::CePopNoParent(TCommandEnabler& ce)
  211. {
  212.   ce.SetCheck(SubWinPtr[TSubWindow::PopNoParent] == 0 ?
  213.     TCommandEnabler::Unchecked :
  214.     TCommandEnabler::Checked);
  215. }
  216.  
  217.  
  218. //
  219. // class TPopupApp
  220. // ~~~~~ ~~~~~~~~~
  221. class TPopupApp : public TApplication {
  222.   public:
  223.     TPopupApp()
  224.     :
  225.       TApplication("Popup")
  226.     {
  227.     }
  228.  
  229.     void InitMainWindow()
  230.     {
  231.       TFrameWindow* frame = new TFrameWindow(0, "Parent Window", new
  232.         TMainWindow);
  233.       frame->Attr.X = 0;
  234.       frame->Attr.Y = 0;
  235.       frame->Attr.W = 400;
  236.       frame->Attr.H = 215;
  237.       frame->AssignMenu(IDM_COMMANDS);
  238.       SetMainWindow(frame);
  239.     }
  240.  
  241.     bool CanClose();
  242. };
  243.  
  244.  
  245. bool
  246. TPopupApp::CanClose()
  247. {
  248.    // In order to avoid debugging kernel warning messages, ensure that
  249.    // the parentless popup window is destroyed before exiting the
  250.    // application.
  251.    //
  252.    if (SubWinPtr[TSubWindow::PopNoParent])
  253.       SubWinPtr[TSubWindow::PopNoParent]->ShutDownWindow();
  254.    return true;
  255. }
  256.  
  257. int
  258. OwlMain(int /*argc*/, char* /*argv*/ [])
  259. {
  260.   return TPopupApp().Run();
  261. }
  262.