home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 6.ddi / OWLDEMOS.ZIP / POPUP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.7 KB  |  172 lines

  1. // ObjectWindows - (C) Copyright 1992 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, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  41.           LPSTR lpCmd, int nCmdShow)
  42.           : TApplication(name, hInstance, hPrevInstance,
  43.                      lpCmd, nCmdShow) {};
  44.     virtual void InitMainWindow( void );
  45.    virtual BOOL CanClose();
  46. };
  47.  
  48. PSubWindow SubWinPtr[] = { NULL, NULL, NULL };
  49.  
  50. LPSTR SubWinTitle[] = { "Child Window", "Popup with Parent",
  51.     "Popup without Parent" };
  52.  
  53. long SubWinStyle[] = {
  54.     WS_CHILD | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  55.     WS_POPUP | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  56.     WS_POPUP | WS_OVERLAPPEDWINDOW | WS_VISIBLE };
  57.  
  58. POINT SubWinPos[] = { { 10, 10 }, { 34, 72 }, { 54, 92 } };
  59.  
  60. LPSTR SubWinText[] = {
  61.     "Child windows cannot be moved outside their parent window.  When " \
  62.       "minimized, a child window's icon resides within the parent " \
  63.       "window.",
  64.     "Popup windows can be moved outside their parent window.  A popup " \
  65.       "with a parent is always displayed in front of the parent, " \
  66.       "even when the parent is focused.  To test this, click on the " \
  67.       "parent window.  When minimized, popup icons reside on the desktop.",
  68.     "Popup windows can be moved outside their parent window.  A popup " \
  69.       "without a parent allows the parent to be brought to the front " \
  70.       "when focused. To test this, click on the parent window.  When " \
  71.       "minimized, popup icons reside on the desktop." };
  72.  
  73. TSubWindow::TSubWindow( PTWindowsObject AParent, TSubWinType ASubWinType ) :
  74.     TWindow( AParent, SubWinTitle[ ASubWinType ] )
  75. {
  76.     Attr.Style = SubWinStyle[ ASubWinType ];
  77.     Attr.X = SubWinPos[ ASubWinType ].x;
  78.     Attr.Y = SubWinPos[ ASubWinType ].y;
  79.     Attr.W = 300;
  80.     Attr.H = 150;
  81.     SubWinType = ASubWinType;
  82. }
  83.  
  84. TSubWindow::~TSubWindow( void )
  85. {
  86.     SubWinPtr[ SubWinType ] = NULL;
  87. }
  88.  
  89. void TSubWindow::Paint( HDC PaintDC, PAINTSTRUCT& )
  90. {
  91.     LPSTR s;
  92.     RECT rect;
  93.  
  94.     GetClientRect( HWindow, &rect );
  95.     InflateRect( &rect, -2, 0 );
  96.     s = SubWinText[ SubWinType ];
  97.     DrawText( PaintDC, s, _fstrlen( s ), &rect, DT_WORDBREAK );
  98. }
  99.  
  100. TMainWindow::TMainWindow( LPSTR ATitle ) : TWindow( NULL, ATitle )
  101. {
  102.     Attr.X = 0;
  103.     Attr.Y = 0;
  104.     Attr.W = 400;
  105.     Attr.H = 215;
  106.     AssignMenu( "COMMANDS" );
  107. }
  108.  
  109. void TMainWindow::ShowSubWindow( PTWindowsObject AParent,
  110.     TSubWinType ASubWinType )
  111. {
  112.     if ( SubWinPtr[ ASubWinType ] == NULL )
  113.         SubWinPtr[ ASubWinType ] = PSubWindow(
  114.             GetApplication()->MakeWindow(
  115.                 new TSubWindow( AParent, ASubWinType) ) );
  116.     else
  117.         SetFocus( SubWinPtr[ ASubWinType ]->HWindow );
  118. }
  119.  
  120. void TMainWindow::WMInitMenu( TMessage& )
  121. {
  122.     WORD MenuState;
  123.  
  124.     for ( int i = SW_CHILD; i <= SW_POPNOPARENT; i++ )
  125.     {
  126.         if ( SubWinPtr[i] == NULL )
  127.             MenuState = MF_UNCHECKED;
  128.         else
  129.             MenuState = MF_CHECKED;
  130.         CheckMenuItem( GetMenu(HWindow), CM_WINDOW + i, MenuState );
  131.     }
  132. }
  133.  
  134. void TMainWindow::CMChild( TMessage& )
  135. {
  136.     ShowSubWindow( this, SW_CHILD );
  137. }
  138.  
  139. void TMainWindow::CMPopParent( TMessage& )
  140. {
  141.     ShowSubWindow( this, SW_POPPARENT );
  142. }
  143.  
  144. void TMainWindow::CMPopNoParent( TMessage& )
  145. {
  146.     ShowSubWindow( NULL, SW_POPNOPARENT );
  147. }
  148.  
  149. void TPopupApp::InitMainWindow( void )
  150. {
  151.     MainWindow = new TMainWindow( "Parent Window" );
  152. }
  153.  
  154. BOOL TPopupApp::CanClose(void)
  155. {
  156.    // In order to avoid debugging kernel warning messages, ensure that
  157.    // the parentless popup window is destroyed before exiting the
  158.    // application.
  159.    if (SubWinPtr[SW_POPNOPARENT] != NULL )
  160.       SubWinPtr[SW_POPNOPARENT]->CloseWindow();
  161.    return(TRUE);
  162. }
  163.  
  164. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  165.            LPSTR lpCmd, int nCmdShow)
  166. {
  167.     TPopupApp PopupApp("Popup", hInstance, hPrevInstance,
  168.                lpCmd, nCmdShow);
  169.     PopupApp.Run();
  170.     return ( PopupApp.Status );
  171. }
  172.