home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL1.ZIP / OWLDEMOS.ZIP / POPUP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  4.4 KB  |  161 lines

  1. // ObjectWindows - (C) Copyright 1991 by Borland International
  2.  
  3. #include <owl.h>
  4. #include <window.h>
  5.  
  6. #include <string.h>
  7.  
  8. const int CM_WINDOW = 100;
  9.  
  10. enum TSubWinType { SW_CHILD, SW_POPPARENT, SW_POPNOPARENT };
  11.  
  12. class TSubWindow : public TWindow
  13. {
  14. private:
  15.     TSubWinType SubWinType;
  16. public:
  17.     TSubWindow( PTWindowsObject AParent, TSubWinType ASubWinType );
  18.     virtual ~TSubWindow( void );
  19.     virtual void Paint( HDC PaintDC, PAINTSTRUCT& PaintStruct );
  20. };
  21.  
  22. typedef TSubWindow* PSubWindow;
  23.  
  24. class TMainWindow : public TWindow
  25. {
  26. public:
  27.     TMainWindow( LPSTR ATitle );
  28.     void ShowSubWindow( PTWindowsObject AParent, TSubWinType ASubWinType );
  29.     virtual void WMInitMenu( TMessage& Msg ) = [ WM_FIRST + WM_INITMENU ];
  30.     virtual void CMChild( TMessage& Msg ) = [ CM_FIRST + CM_WINDOW + SW_CHILD ];
  31.     virtual void CMPopParent( TMessage& Msg ) = [ CM_FIRST + CM_WINDOW + SW_POPPARENT ];
  32.     virtual void CMPopNoParent( TMessage& Msg ) = [ CM_FIRST + CM_WINDOW + SW_POPNOPARENT ];
  33. };
  34.  
  35. typedef TMainWindow* PMainWindow;
  36.  
  37. class TPopupApp : public TApplication
  38. {
  39. public:
  40.     TPopupApp(LPSTR name, HANDLE hInstance, HANDLE hPrevInstance,
  41.           LPSTR lpCmd, int nCmdShow)
  42.           : TApplication(name, hInstance, hPrevInstance,
  43.                      lpCmd, nCmdShow) {};
  44.     virtual void InitMainWindow( void );
  45. };
  46.  
  47. PSubWindow SubWinPtr[] = { NULL, NULL, NULL };
  48.  
  49. LPSTR SubWinTitle[] = { "Child Window", "Popup with Parent",
  50.     "Popup without Parent" };
  51.  
  52. long SubWinStyle[] = {
  53.     WS_CHILD | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  54.     WS_POPUP | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  55.     WS_POPUP | WS_OVERLAPPEDWINDOW | WS_VISIBLE };
  56.  
  57. POINT SubWinPos[] = { { 10, 10 }, { 34, 72 }, { 54, 92 } };
  58.  
  59. LPSTR SubWinText[] = {
  60.     "Child windows cannot be moved outside their parent window.  When " \
  61.       "minimized, a child window's icon resides within the parent " \
  62.       "window.",
  63.     "Popup windows can be moved outside their parent window.  A popup " \
  64.       "with a parent is always displayed in front of the parent, " \
  65.       "even when the parent is focused.  To test this, click on the " \
  66.       "parent window.  When minimized, popup icons reside on the desktop.",
  67.     "Popup windows can be moved outside their parent window.  A popup " \
  68.       "without a parent allows the parent to be brought to the front " \
  69.       "when focused. To test this, click on the parent window.  When " \
  70.       "minimized, popup icons reside on the desktop." };
  71.  
  72. TSubWindow::TSubWindow( PTWindowsObject AParent, TSubWinType ASubWinType ) :
  73.     TWindow( AParent, SubWinTitle[ ASubWinType ] )
  74. {
  75.     Attr.Style = SubWinStyle[ ASubWinType ];
  76.     Attr.X = SubWinPos[ ASubWinType ].x;
  77.     Attr.Y = SubWinPos[ ASubWinType ].y;
  78.     Attr.W = 300;
  79.     Attr.H = 150;
  80.     SubWinType = ASubWinType;
  81. }
  82.  
  83. TSubWindow::~TSubWindow( void )
  84. {
  85.     SubWinPtr[ SubWinType ] = NULL;
  86. }
  87.  
  88. void TSubWindow::Paint( HDC PaintDC, PAINTSTRUCT& )
  89. {
  90.     LPSTR s;
  91.     RECT rect;
  92.  
  93.     GetClientRect( HWindow, &rect );
  94.     InflateRect( &rect, -2, 0 );
  95.     s = SubWinText[ SubWinType ];
  96.     DrawText( PaintDC, s, _fstrlen( s ), &rect, DT_WORDBREAK );
  97. }
  98.  
  99. TMainWindow::TMainWindow( LPSTR ATitle ) : TWindow( NULL, ATitle )
  100. {
  101.     Attr.X = 0;
  102.     Attr.Y = 0;
  103.     Attr.W = 400;
  104.     Attr.H = 215;
  105.     AssignMenu( "Menu" );
  106. }
  107.  
  108. void TMainWindow::ShowSubWindow( PTWindowsObject AParent,
  109.     TSubWinType ASubWinType )
  110. {
  111.     if ( SubWinPtr[ ASubWinType ] == NULL )
  112.         SubWinPtr[ ASubWinType ] = PSubWindow(
  113.             GetApplication()->MakeWindow(
  114.                 new TSubWindow( AParent, ASubWinType) ) );
  115.     else
  116.         SetFocus( SubWinPtr[ ASubWinType ]->HWindow );
  117. }
  118.  
  119. void TMainWindow::WMInitMenu( TMessage& )
  120. {
  121.     WORD MenuState;
  122.  
  123.     for ( TSubWinType i = SW_CHILD; i <= SW_POPNOPARENT; i++ )
  124.     {
  125.         if ( SubWinPtr[i] == NULL )
  126.             MenuState = MF_UNCHECKED;
  127.         else
  128.             MenuState = MF_CHECKED;
  129.         CheckMenuItem( GetMenu(HWindow), CM_WINDOW + i, MenuState );
  130.     }
  131. }
  132.  
  133. void TMainWindow::CMChild( TMessage& )
  134. {
  135.     ShowSubWindow( this, SW_CHILD );
  136. }
  137.  
  138. void TMainWindow::CMPopParent( TMessage& )
  139. {
  140.     ShowSubWindow( this, SW_POPPARENT );
  141. }
  142.  
  143. void TMainWindow::CMPopNoParent( TMessage& )
  144. {
  145.     ShowSubWindow( NULL, SW_POPNOPARENT );
  146. }
  147.  
  148. void TPopupApp::InitMainWindow( void )
  149. {
  150.     MainWindow = new TMainWindow( "Parent Window" );
  151. }
  152.  
  153. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  154.            LPSTR lpCmd, int nCmdShow)
  155. {
  156.     TPopupApp PopupApp("Popup", hInstance, hPrevInstance,
  157.                lpCmd, nCmdShow);
  158.     PopupApp.Run();
  159.     return ( PopupApp.Status );
  160. }
  161.