home *** CD-ROM | disk | FTP | other *** search
/ PC Direkt 1995 March / PCD_395.iso / starview / pm2blci / german / mdidemo.cx_ / MDIDEMO.CXX
Encoding:
C/C++ Source or Header  |  1994-01-18  |  9.6 KB  |  389 lines

  1. /*******************************************************************
  2. *  MDIDEMO.CXX
  3. *  (c) 1992-1994 STAR DIVISION
  4. *******************************************************************/
  5.  
  6. #include <stdlib.h>
  7. #include <sv.hxx>
  8.  
  9. #include "mdidemo.hrc"
  10.  
  11. // --- class MyApp -------------------------------------------------
  12.  
  13. class MyApp : public MDIApplication
  14. {
  15. private:
  16.     MenuBar*        pInitMenu;
  17.     MenuBar*        pHelloMenu;
  18.     MenuBar*        pRectMenu;
  19.  
  20. public:
  21.     virtual void    Main( int, char*[] );
  22.     virtual BOOL    QueryExit() { return CloseAll(); }
  23.  
  24.     long            FileMenu( Menu* pMenu );
  25.     long            ColorMenu( Menu* pMenu );
  26.     long            WindowMenu( Menu* pMenu );
  27.     void            SetMenu( USHORT nId );
  28. };
  29.  
  30. // --- class AppWin ------------------------------------------------
  31.  
  32. class AppWin : public WorkWindow
  33. {
  34. public:
  35.                  AppWin( Window* pParent, WinBits aWinStyle ) :
  36.                      WorkWindow( pParent, aWinStyle ) {}
  37.  
  38.     virtual BOOL Close();
  39. };
  40.  
  41. // --- class HelloWin ----------------------------------------------
  42.  
  43. class HelloWin : public MDIWindow
  44. {
  45. private:
  46.     USHORT          nColorId;
  47.  
  48. public:
  49.                     HelloWin();
  50.                     ~HelloWin();
  51.  
  52.     virtual void    Activate();
  53.     virtual void    Deactivate();
  54.     virtual BOOL    Close();
  55.     virtual void    Paint( const Rectangle& );
  56.     virtual void    Resize() { Invalidate(); }
  57.  
  58.     void            SetColor( USHORT nNewColorId );
  59. };
  60.  
  61. // --- class RectWin -----------------------------------------------
  62.  
  63. class RectWin : public MDIWindow
  64. {
  65. private:
  66.     AutoTimer       aTimer;
  67.  
  68. public:
  69.                     RectWin();
  70.                     ~RectWin();
  71.  
  72.     virtual void    Activate();
  73.     virtual BOOL    Close();
  74.     void            TimeHdl( Timer* );
  75. };
  76.  
  77. // --- aMyApp ------------------------------------------------------
  78.  
  79. MyApp aMyApp;
  80.  
  81. // --- MyApp::Main() -----------------------------------------------
  82.  
  83. void MyApp::Main( int, char*[] )
  84. {
  85.     MenuBar     aMBInit ( ResId( MENU_INIT ) );
  86.     MenuBar     aMBHello( ResId( MENU_HELLO ) );
  87.     MenuBar     aMBRect ( ResId( MENU_RECT ) );
  88.  
  89.     AppWin aAppWin( NULL, WB_APP | WB_STDWORK );
  90.  
  91.     pInitMenu  = &aMBInit;
  92.     pHelloMenu = &aMBHello;
  93.     pRectMenu  = &aMBRect;
  94.  
  95.     aMBInit.GetPopupMenu( MENU_FILE )->
  96.         PushSelectHdl( LINK( this, MyApp, FileMenu ) );
  97.  
  98.     aMBHello.GetPopupMenu( MENU_FILE )->
  99.         PushSelectHdl( LINK( this, MyApp, FileMenu ) );
  100.     aMBHello.GetPopupMenu( MENU_COLOR )->
  101.         PushSelectHdl( LINK( this, MyApp, ColorMenu ) );
  102.     aMBHello.GetPopupMenu( MENU_WINDOW )->
  103.         PushSelectHdl( LINK( this, MyApp, WindowMenu ) );
  104.  
  105.     aMBRect.GetPopupMenu( MENU_FILE )->
  106.         PushSelectHdl( LINK( this, MyApp, FileMenu ) );
  107.     aMBRect.GetPopupMenu( MENU_WINDOW )->
  108.         PushSelectHdl( LINK( this, MyApp, WindowMenu ) );
  109.  
  110.     ChangeAppMenu( &aMBInit );
  111.  
  112.     aAppWin.SetText( "StarView MDI-Demo" );
  113.     aAppWin.Show();
  114.  
  115.     Execute();
  116. }
  117.  
  118. // --- MyApp::FileMenu() -------------------------------------------
  119.  
  120. long MyApp::FileMenu( Menu* pMenu )
  121. {
  122.     switch ( pMenu->GetCurItemId() )
  123.     {
  124.         case MENU_NEWHELLO:
  125.             new HelloWin;
  126.             break;
  127.         case MENU_NEWRECT:
  128.             new RectWin;
  129.             break;
  130.         case MENU_CLOSE:
  131.             if ( GetActiveWindow() )
  132.                 GetActiveWindow()->Close();
  133.             break;
  134.         case MENU_EXIT:
  135.             if ( CloseAll() )
  136.                 Quit();
  137.             break;
  138.     }
  139.  
  140.     return TRUE;
  141. }
  142.  
  143. // --- MyApp::ColorMenu() ------------------------------------------
  144.  
  145. long MyApp::ColorMenu( Menu* pMenu )
  146. {
  147.     ((HelloWin*)GetActiveWindow())->
  148.         SetColor( pMenu->GetCurItemId() );
  149.     return TRUE;
  150. }
  151.  
  152. // --- MyApp::WindowMenu() -----------------------------------------
  153.  
  154. long MyApp::WindowMenu( Menu* pMenu )
  155. {
  156.     switch ( pMenu->GetCurItemId() )
  157.     {
  158.         case MENU_CASCADE:
  159.             Cascade();
  160.             break;
  161.         case MENU_TILE:
  162.             Tile();
  163.             break;
  164.         case MENU_HORIZONTAL:
  165.             Horizontal();
  166.             break;
  167.         case MENU_VERTICAL:
  168.             Vertical();
  169.             break;
  170.         case MENU_ARRANGE:
  171.             Arrange();
  172.             break;
  173.         case MENU_CLOSEALL:
  174.             if ( CloseAll() )
  175.                 SetMenu( MENU_INIT );
  176.             break;
  177.     }
  178.  
  179.     return TRUE;
  180. }
  181.  
  182. // --- MyApp::SetMenu() --------------------------------------------
  183.  
  184. void MyApp::SetMenu( USHORT nId )
  185. {
  186.     switch ( nId )
  187.     {
  188.         case MENU_INIT:
  189.             ChangeAppMenu( pInitMenu );
  190.             break;
  191.         case MENU_HELLO:
  192.             ChangeAppMenu( pHelloMenu );
  193.             ChangeMDIMenu( pHelloMenu->
  194.                            GetPopupMenu( MENU_WINDOW ) );
  195.             break;
  196.         case MENU_RECT:
  197.             ChangeAppMenu( pRectMenu );
  198.             ChangeMDIMenu( pRectMenu->
  199.                            GetPopupMenu( MENU_WINDOW ) );
  200.             break;
  201.     }
  202. }
  203.  
  204. // --- AppWin::Close() ---------------------------------------------
  205.  
  206. BOOL AppWin::Close()
  207. {
  208.     if ( aMyApp.CloseAll() )
  209.         return WorkWindow::Close();
  210.     else
  211.         return FALSE;
  212. }
  213.  
  214. // --- HelloWin::HelloWin() ----------------------------------------
  215.  
  216. HelloWin::HelloWin() : MDIWindow( aMyApp.GetAppWindow(),
  217.                                   WB_CLOSEABLE )
  218. {
  219.     nColorId = MENU_BLACK;
  220.     ChangeIcon( Icon( ICON_DEFAULT ) );
  221.     SetText( "Hello" );
  222.  
  223.     Show();
  224. }
  225.  
  226. // --- HelloWin::~HelloWin() ---------------------------------------
  227.  
  228. HelloWin::~HelloWin()
  229. {
  230.     if ( aMyApp.GetVisibleWindowCount() == 1 )
  231.         aMyApp.SetMenu( MENU_INIT );
  232. }
  233.  
  234. // --- HelloWin::Close() -------------------------------------------
  235.  
  236. BOOL HelloWin::Close()
  237. {
  238.     QueryBox aBox( NULL, WB_YES_NO, "OK to close window ?" );
  239.  
  240.     if ( aBox.Execute() == RET_YES )
  241.     {
  242.         delete this;
  243.         return TRUE;
  244.     }
  245.     else
  246.         return FALSE;
  247. }
  248.  
  249. // --- HelloWin::SetColor() ----------------------------------------
  250.  
  251. void HelloWin::SetColor( USHORT nNewColor )
  252. {
  253.     USHORT nOldId = nColorId;
  254.  
  255.     aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR )->
  256.                              CheckItem( nOldId, FALSE );
  257.  
  258.     if ( nOldId != nNewColor )
  259.     {
  260.         Color aColor;
  261.  
  262.         nColorId = nNewColor;
  263.  
  264.         switch( nNewColor )
  265.         {
  266.             case MENU_BLACK:
  267.                 aColor = Color( COL_BLACK );
  268.                 break;
  269.             case MENU_RED:
  270.                 aColor = Color( COL_LIGHTRED );
  271.                 break;
  272.             case MENU_GREEN:
  273.                 aColor = Color( COL_LIGHTGREEN );
  274.                 break;
  275.             case MENU_BLUE:
  276.                 aColor = Color( COL_LIGHTBLUE );
  277.                 break;
  278.             case MENU_WHITE:
  279.                 aColor = Color( COL_WHITE );
  280.                 break;
  281.         }
  282.  
  283.         Font aFont = GetFont();
  284.         aFont.ChangeColor( aColor );
  285.         ChangeFont( aFont );
  286.         Invalidate();
  287.     }
  288.  
  289.     aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR )->
  290.                              CheckItem( nColorId );
  291. }
  292.  
  293. // --- HelloWin::Activate() ----------------------------------------
  294.  
  295. void HelloWin::Activate()
  296. {
  297.     if ( IsMDIActivate() )
  298.     {
  299.         aMyApp.SetMenu( MENU_HELLO );
  300.         aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR )->
  301.                              CheckItem( nColorId );
  302.     }
  303. }
  304.  
  305. // --- HelloWin::Deactivate() --------------------------------------
  306.  
  307. void HelloWin::Deactivate()
  308. {
  309.     Menu* pMenu;
  310.  
  311.     pMenu = aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR );
  312.     if ( IsMDIActivate() && pMenu )
  313.         pMenu->CheckItem( nColorId, FALSE );
  314. }
  315.  
  316. // --- HelloWin::Paint() -------------------------------------------
  317.  
  318. void HelloWin::Paint( const Rectangle& )
  319. {
  320.     String aText( "Hello World !" );
  321.     Size   aWinSize  = GetOutputSizePixel();
  322.     Size   aTextSize = GetTextSize( aText );
  323.  
  324.     DrawText( Point( aWinSize.Width()   / 2 -
  325.                      aTextSize.Width()  / 2,
  326.                      aWinSize.Height()  / 2 -
  327.                      aTextSize.Height() / 2 ),
  328.               aText );
  329. }
  330.  
  331. // --- RectWin::RectWin() ------------------------------------------
  332.  
  333. RectWin::RectWin() : MDIWindow( aMyApp.GetAppWindow(),
  334.                                 WinBits( WB_CLOSEABLE ) )
  335. {
  336.     ChangeIcon( Icon( ICON_NULL ) );
  337.     SetText( "Rectangles" );
  338.  
  339.     Show();
  340.  
  341.     aTimer.ChangeTimeoutHdl( LINK( this, RectWin, TimeHdl ) );
  342.     aTimer.ChangeTimeout( 250 );
  343.     aTimer.Start();
  344. }
  345.  
  346. // --- RectWin::~RectWin() -----------------------------------------
  347.  
  348. RectWin::~RectWin()
  349. {
  350.     if ( aMyApp.GetVisibleWindowCount() == 1 )
  351.         aMyApp.SetMenu( MENU_INIT );
  352. }
  353.  
  354. // --- RectWin::Activate() -----------------------------------------
  355.  
  356. void RectWin::Activate()
  357. {
  358.     if ( IsMDIActivate() )
  359.         aMyApp.SetMenu( MENU_RECT );
  360. }
  361.  
  362. // --- RectWin::TimeHdl() ------------------------------------------
  363.  
  364. void RectWin::TimeHdl( Timer* )
  365. {
  366.     Size aSize = GetOutputSizePixel();
  367.  
  368.     if ( !aSize.Width() || !aSize.Height() )
  369.         return;
  370.  
  371.     ChangeFillInBrush( Brush( Color( rand()*256,
  372.                                      rand()*256,
  373.                                      rand()*256 ) ) );
  374.  
  375.     Rectangle aRect( rand() % aSize.Width(),
  376.                      rand() % aSize.Height(),
  377.                      rand() % aSize.Width(),
  378.                      rand() % aSize.Height() );
  379.     DrawRect( aRect );
  380. }
  381.  
  382. // --- RectWin::Close() --------------------------------------------
  383.  
  384. BOOL RectWin::Close()
  385. {
  386.     delete this;
  387.     return TRUE;
  388. }
  389.