home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************
- * MDIDEMO.CXX
- * (c) 1992-1994 STAR DIVISION
- *******************************************************************/
-
- #include <stdlib.h>
- #include <sv.hxx>
-
- #include "mdidemo.hrc"
-
- // --- class MyApp -------------------------------------------------
-
- class MyApp : public MDIApplication
- {
- private:
- MenuBar* pInitMenu;
- MenuBar* pHelloMenu;
- MenuBar* pRectMenu;
-
- public:
- virtual void Main( int, char*[] );
- virtual BOOL QueryExit() { return CloseAll(); }
-
- long FileMenu( Menu* pMenu );
- long ColorMenu( Menu* pMenu );
- long WindowMenu( Menu* pMenu );
- void SetMenu( USHORT nId );
- };
-
- // --- class AppWin ------------------------------------------------
-
- class AppWin : public WorkWindow
- {
- public:
- AppWin( Window* pParent, WinBits aWinStyle ) :
- WorkWindow( pParent, aWinStyle ) {}
-
- virtual BOOL Close();
- };
-
- // --- class HelloWin ----------------------------------------------
-
- class HelloWin : public MDIWindow
- {
- private:
- USHORT nColorId;
-
- public:
- HelloWin();
- ~HelloWin();
-
- virtual void Activate();
- virtual void Deactivate();
- virtual BOOL Close();
- virtual void Paint( const Rectangle& );
- virtual void Resize() { Invalidate(); }
-
- void SetColor( USHORT nNewColorId );
- };
-
- // --- class RectWin -----------------------------------------------
-
- class RectWin : public MDIWindow
- {
- private:
- AutoTimer aTimer;
-
- public:
- RectWin();
- ~RectWin();
-
- virtual void Activate();
- virtual BOOL Close();
- void TimeHdl( Timer* );
- };
-
- // --- aMyApp ------------------------------------------------------
-
- MyApp aMyApp;
-
- // --- MyApp::Main() -----------------------------------------------
-
- void MyApp::Main( int, char*[] )
- {
- MenuBar aMBInit ( ResId( MENU_INIT ) );
- MenuBar aMBHello( ResId( MENU_HELLO ) );
- MenuBar aMBRect ( ResId( MENU_RECT ) );
-
- AppWin aAppWin( NULL, WB_APP | WB_STDWORK );
-
- pInitMenu = &aMBInit;
- pHelloMenu = &aMBHello;
- pRectMenu = &aMBRect;
-
- aMBInit.GetPopupMenu( MENU_FILE )->
- PushSelectHdl( LINK( this, MyApp, FileMenu ) );
-
- aMBHello.GetPopupMenu( MENU_FILE )->
- PushSelectHdl( LINK( this, MyApp, FileMenu ) );
- aMBHello.GetPopupMenu( MENU_COLOR )->
- PushSelectHdl( LINK( this, MyApp, ColorMenu ) );
- aMBHello.GetPopupMenu( MENU_WINDOW )->
- PushSelectHdl( LINK( this, MyApp, WindowMenu ) );
-
- aMBRect.GetPopupMenu( MENU_FILE )->
- PushSelectHdl( LINK( this, MyApp, FileMenu ) );
- aMBRect.GetPopupMenu( MENU_WINDOW )->
- PushSelectHdl( LINK( this, MyApp, WindowMenu ) );
-
- ChangeAppMenu( &aMBInit );
-
- aAppWin.SetText( "StarView MDI-Demo" );
- aAppWin.Show();
-
- Execute();
- }
-
- // --- MyApp::FileMenu() -------------------------------------------
-
- long MyApp::FileMenu( Menu* pMenu )
- {
- switch ( pMenu->GetCurItemId() )
- {
- case MENU_NEWHELLO:
- new HelloWin;
- break;
- case MENU_NEWRECT:
- new RectWin;
- break;
- case MENU_CLOSE:
- if ( GetActiveWindow() )
- GetActiveWindow()->Close();
- break;
- case MENU_EXIT:
- if ( CloseAll() )
- Quit();
- break;
- }
-
- return TRUE;
- }
-
- // --- MyApp::ColorMenu() ------------------------------------------
-
- long MyApp::ColorMenu( Menu* pMenu )
- {
- ((HelloWin*)GetActiveWindow())->
- SetColor( pMenu->GetCurItemId() );
- return TRUE;
- }
-
- // --- MyApp::WindowMenu() -----------------------------------------
-
- long MyApp::WindowMenu( Menu* pMenu )
- {
- switch ( pMenu->GetCurItemId() )
- {
- case MENU_CASCADE:
- Cascade();
- break;
- case MENU_TILE:
- Tile();
- break;
- case MENU_HORIZONTAL:
- Horizontal();
- break;
- case MENU_VERTICAL:
- Vertical();
- break;
- case MENU_ARRANGE:
- Arrange();
- break;
- case MENU_CLOSEALL:
- if ( CloseAll() )
- SetMenu( MENU_INIT );
- break;
- }
-
- return TRUE;
- }
-
- // --- MyApp::SetMenu() --------------------------------------------
-
- void MyApp::SetMenu( USHORT nId )
- {
- switch ( nId )
- {
- case MENU_INIT:
- ChangeAppMenu( pInitMenu );
- break;
- case MENU_HELLO:
- ChangeAppMenu( pHelloMenu );
- ChangeMDIMenu( pHelloMenu->
- GetPopupMenu( MENU_WINDOW ) );
- break;
- case MENU_RECT:
- ChangeAppMenu( pRectMenu );
- ChangeMDIMenu( pRectMenu->
- GetPopupMenu( MENU_WINDOW ) );
- break;
- }
- }
-
- // --- AppWin::Close() ---------------------------------------------
-
- BOOL AppWin::Close()
- {
- if ( aMyApp.CloseAll() )
- return WorkWindow::Close();
- else
- return FALSE;
- }
-
- // --- HelloWin::HelloWin() ----------------------------------------
-
- HelloWin::HelloWin() : MDIWindow( aMyApp.GetAppWindow(),
- WB_CLOSEABLE )
- {
- nColorId = MENU_BLACK;
- ChangeIcon( Icon( ICON_DEFAULT ) );
- SetText( "Hello" );
-
- Show();
- }
-
- // --- HelloWin::~HelloWin() ---------------------------------------
-
- HelloWin::~HelloWin()
- {
- if ( aMyApp.GetVisibleWindowCount() == 1 )
- aMyApp.SetMenu( MENU_INIT );
- }
-
- // --- HelloWin::Close() -------------------------------------------
-
- BOOL HelloWin::Close()
- {
- QueryBox aBox( NULL, WB_YES_NO, "OK to close window ?" );
-
- if ( aBox.Execute() == RET_YES )
- {
- delete this;
- return TRUE;
- }
- else
- return FALSE;
- }
-
- // --- HelloWin::SetColor() ----------------------------------------
-
- void HelloWin::SetColor( USHORT nNewColor )
- {
- USHORT nOldId = nColorId;
-
- aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR )->
- CheckItem( nOldId, FALSE );
-
- if ( nOldId != nNewColor )
- {
- Color aColor;
-
- nColorId = nNewColor;
-
- switch( nNewColor )
- {
- case MENU_BLACK:
- aColor = Color( COL_BLACK );
- break;
- case MENU_RED:
- aColor = Color( COL_LIGHTRED );
- break;
- case MENU_GREEN:
- aColor = Color( COL_LIGHTGREEN );
- break;
- case MENU_BLUE:
- aColor = Color( COL_LIGHTBLUE );
- break;
- case MENU_WHITE:
- aColor = Color( COL_WHITE );
- break;
- }
-
- Font aFont = GetFont();
- aFont.ChangeColor( aColor );
- ChangeFont( aFont );
- Invalidate();
- }
-
- aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR )->
- CheckItem( nColorId );
- }
-
- // --- HelloWin::Activate() ----------------------------------------
-
- void HelloWin::Activate()
- {
- if ( IsMDIActivate() )
- {
- aMyApp.SetMenu( MENU_HELLO );
- aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR )->
- CheckItem( nColorId );
- }
- }
-
- // --- HelloWin::Deactivate() --------------------------------------
-
- void HelloWin::Deactivate()
- {
- Menu* pMenu;
-
- pMenu = aMyApp.GetAppMenu()->GetPopupMenu( MENU_COLOR );
- if ( IsMDIActivate() && pMenu )
- pMenu->CheckItem( nColorId, FALSE );
- }
-
- // --- HelloWin::Paint() -------------------------------------------
-
- void HelloWin::Paint( const Rectangle& )
- {
- String aText( "Hello World !" );
- Size aWinSize = GetOutputSizePixel();
- Size aTextSize = GetTextSize( aText );
-
- DrawText( Point( aWinSize.Width() / 2 -
- aTextSize.Width() / 2,
- aWinSize.Height() / 2 -
- aTextSize.Height() / 2 ),
- aText );
- }
-
- // --- RectWin::RectWin() ------------------------------------------
-
- RectWin::RectWin() : MDIWindow( aMyApp.GetAppWindow(),
- WinBits( WB_CLOSEABLE ) )
- {
- ChangeIcon( Icon( ICON_NULL ) );
- SetText( "Rectangles" );
-
- Show();
-
- aTimer.ChangeTimeoutHdl( LINK( this, RectWin, TimeHdl ) );
- aTimer.ChangeTimeout( 250 );
- aTimer.Start();
- }
-
- // --- RectWin::~RectWin() -----------------------------------------
-
- RectWin::~RectWin()
- {
- if ( aMyApp.GetVisibleWindowCount() == 1 )
- aMyApp.SetMenu( MENU_INIT );
- }
-
- // --- RectWin::Activate() -----------------------------------------
-
- void RectWin::Activate()
- {
- if ( IsMDIActivate() )
- aMyApp.SetMenu( MENU_RECT );
- }
-
- // --- RectWin::TimeHdl() ------------------------------------------
-
- void RectWin::TimeHdl( Timer* )
- {
- Size aSize = GetOutputSizePixel();
-
- if ( !aSize.Width() || !aSize.Height() )
- return;
-
- ChangeFillInBrush( Brush( Color( rand()*256,
- rand()*256,
- rand()*256 ) ) );
-
- Rectangle aRect( rand() % aSize.Width(),
- rand() % aSize.Height(),
- rand() % aSize.Width(),
- rand() % aSize.Height() );
- DrawRect( aRect );
- }
-
- // --- RectWin::Close() --------------------------------------------
-
- BOOL RectWin::Close()
- {
- delete this;
- return TRUE;
- }
-