home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************
- * SVRDEMO.CXX
- * (c) 1992-1994 STAR DIVISION
- *******************************************************************/
-
- #ifdef MAC
- #include <mac_start.h>
- #include <SCM.h>
- #include <ole.h>
- #include <mac_end.h>
- #include <sysdep.hxx>
- #endif
-
- #include <sv.hxx>
- #include <so.hxx>
-
- #include "svrdemo.hrc"
-
- #define MAXPOINTS 500
-
- class MyServer;
-
- // --- class MyApp -------------------------------------------------
-
- class MyApp : public Application
- {
- public:
- virtual void Main( int, char*[] );
- };
-
- // --- Instance of Application Class -------------------------------
-
- MyApp aMyApp;
-
- short VERSION = 0x0100;
-
- // --- class MyWindow ----------------------------------------------
-
- class MyWindow : public WorkWindow
- {
- private:
- MyServer* pSvr;
- Size aSize;
- short nPointCount;
- Point aPointArray[MAXPOINTS];
-
- public:
- MyWindow();
-
- virtual void Resize();
- virtual BOOL Close();
- virtual void MouseButtonDown( const MouseEvent& rMEvt );
- virtual void MouseMove( const MouseEvent& rMEvt );
- virtual void MouseButtonUp( const MouseEvent& rMEvt );
- virtual void Paint( const Rectangle& rRect );
-
- void SetServer( MyServer& p ) { pSvr = &p; }
- long MenuCommand( Menu* pMenu );
- void PaintIt();
- BOOL ReceiveData( MemoryBlock& rBlock );
- BOOL TransmitData( MemoryBlock& rBlock );
- BOOL TransmitPicture( GDIMetaFile& rMtf );
- void RecalcData( const Size& rSize );
- };
-
-
- // --- class MyServer ----------------------------------------------
-
- class MyServer : public StarObjectServer
- {
- private:
- MyWindow* pWin;
-
- public:
- MyServer( MyWindow& );
-
- virtual BOOL Create() { return Action( 0 ); }
- virtual BOOL Action( USHORT nAction );
- virtual void NewNames( const String&, const String& );
- virtual BOOL ReceiveData( MemoryBlock& rBlock );
- virtual BOOL TransmitData( MemoryBlock& rBlock );
- virtual BOOL TransmitPicture( GDIMetaFile& rMtf );
- };
-
- // --- MyWindow::MyWindow() ----------------------------------------
-
- MyWindow::MyWindow() : WorkWindow( NULL, WB_APP | WB_STDWORK )
- {
- pSvr = NULL;
- nPointCount = 0;
- SetText( "Connect" );
- ChangeMapMode( MapMode( MAP_100TH_MM ) );
- }
-
- // --- MyWindow::MenuCommand() -------------------------------------
-
- long MyWindow::MenuCommand( Menu* pMenu )
- {
- switch ( pMenu->GetCurItemId() )
- {
- case IDM_EXIT:
- Close();
- break;
- }
-
- return TRUE;
- }
-
- // --- MyWindow::MouseMove() ---------------------------------------
-
- void MyWindow::MouseMove( const MouseEvent& rMEvt )
- {
- if ( rMEvt.IsLeft() && (nPointCount < MAXPOINTS) )
- {
- Point p( rMEvt.GetPosPixel() );
- if ( p.X() >= 0 && p.Y() >= 0)
- aPointArray[ nPointCount ] = PixelToLogic( p );
- nPointCount++;
- Invalidate();
- }
- }
-
- // --- MyWindow::MouseButtonDown() ---------------------------------
-
- void MyWindow::MouseButtonDown( const MouseEvent& rMEvt )
- {
- if ( rMEvt.IsLeft() )
- CaptureMouse();
- else
- {
- nPointCount = 0;
- Invalidate();
- Update();
- }
- }
-
- // --- MyWindow::MouseButtonUp() -----------------------------------
-
- void MyWindow::MouseButtonUp( const MouseEvent& rMEvt )
- {
- if( rMEvt.IsLeft() )
- ReleaseMouse();
- }
-
- // --- MyWindow::Paint() -------------------------------------------
-
- void MyWindow::Paint( const Rectangle& )
- {
- PaintIt();
- }
-
- // --- MyWindow::Resize() ------------------------------------------
-
- void MyWindow::Resize()
- {
- Size s = PixelToLogic( GetOutputSizePixel(),
- MapMode( MAP_100TH_MM ) );
-
- if ( s.Width() && s.Height() )
- RecalcData( s );
- }
-
- // --- MyWindow::PaintIt() -----------------------------------------
-
- void MyWindow::PaintIt()
- {
- for ( short i = 0; i < (nPointCount-1); i++ )
- for ( short j = 0; j < nPointCount; j++ )
- DrawLine( aPointArray[ i ], aPointArray[ j ] );
- }
-
- // --- MyWindow::ReceiveData() -------------------------------------
-
- BOOL MyWindow::ReceiveData( MemoryBlock& rData )
- {
- BOOL bRes = FALSE;
- short n = 0;
- rData >> n;
- if ( n <= VERSION )
- {
- rData >> aSize >> nPointCount;
- for( int i = 0; i < nPointCount; i++ )
- rData >> aPointArray[i];
- bRes = BOOL( rData.good() );
- }
-
- if ( bRes )
- ChangeOutputSizePixel( LogicToPixel( aSize ) );
- else
- nPointCount = 0;
-
- return bRes;
- }
-
- // --- MyWindow::TransmitData() ------------------------------------
-
- BOOL MyWindow::TransmitData( MemoryBlock& rData )
- {
- rData << VERSION << aSize << nPointCount;
- for( int i = 0; i < nPointCount; i++ )
- rData << aPointArray[i];
-
- return BOOL( rData.good() );
- }
-
- // --- MyWindow::TransmitPicture() ---------------------------------
-
- BOOL MyWindow::TransmitPicture( GDIMetaFile& aPic )
- {
- MapMode aMM( MAP_100TH_MM );
- aPic.ChangePrefSize( PixelToLogic( GetOutputSizePixel(), aMM ) );
- aPic.ChangePrefMapMode( aMM );
- aPic.Record( this );
- ChangePen( Pen( Color( COL_BLACK ) ) );
- PaintIt();
- aPic.Stop();
-
- return TRUE;
- }
-
- // --- MyWindow::RecalcData() --------------------------------------
-
- void MyWindow::RecalcData( const Size& rSize )
- {
- if ( nPointCount && aSize.Width() &&
- aSize.Height() && aSize != rSize )
- {
- short dx100 = (short)((long)rSize.Width() * 100 / aSize.Width());
- short dy100 = (short)((long)rSize.Height() * 100 / aSize.Height());
-
- for ( short i = 0; i < nPointCount; i++ )
- {
- aPointArray[i].X() =
- (short)((long)aPointArray[i].X() * dx100 / 100);
- aPointArray[i].Y() =
- (short)((long)aPointArray[i].Y() * dy100 / 100);
- }
-
- Invalidate();
- }
-
- aSize = rSize;
- }
-
- // --- MyWindow::Close() --------------------------------------
-
- BOOL MyWindow::Close()
- {
- if ( !pSvr->IsServer() )
- {
- pApp->Quit(); return TRUE;
- }
-
- short nRet = MessBox( this, WB_YES_NO_CANCEL | WB_DEF_YES,
- "Connect",
- String( ResId( IDS_UPDATE ) ) ).Execute();
-
- switch ( nRet )
- {
- case RET_YES:
- pSvr->Update();
- case RET_NO:
- pApp->Quit();
- return TRUE;
-
- default:
- return FALSE;
- }
- }
-
- // --- MyServer::MyServer() ----------------------------------------
-
- MyServer::MyServer( MyWindow& aWindow ) :
- StarObjectServer()
- {
- pWin = &aWindow;
- }
-
- // --- MyServer::ReceiveData() -------------------------------------
-
- BOOL MyServer::ReceiveData( MemoryBlock& rData )
- {
- return pWin->ReceiveData( rData );
- }
-
- // --- MyServer::TransmitData() ------------------------------------
-
- BOOL MyServer::TransmitData( MemoryBlock& rData )
- {
- return pWin->TransmitData( rData );
- }
-
- // --- MyServer::TransmitPicture() ----------------------------------
-
- BOOL MyServer::TransmitPicture( GDIMetaFile& rPic )
- {
- return pWin->TransmitPicture( rPic );
- }
-
- // --- MyServer::NewNames() ----------------------------------------
-
- void MyServer::NewNames( const String& rApp, const String& rObj )
- {
- String aTitle( "Connect - " );
- aTitle += rObj;
- aTitle += " in ";
- aTitle += rApp;
- pWin->SetText( aTitle );
-
- String s( ResId( IDS_BACK ) );
- s += rApp;
- pApp->GetAppMenu()->
- GetPopupMenu( IDM_FILE )->SetItemText( IDM_EXIT, s );
- }
-
- // --- MyServer::Action() ------------------------------------------
-
- BOOL MyServer::Action( USHORT nAction )
- {
- if( nAction == 0 )
- {
- Size s( GetSize () );
- if ( s.Width() && s.Height() )
- pWin->ChangeOutputSizePixel( pWin->LogicToPixel( s ) );
- pWin->Show();
- return TRUE;
- }
- else
- return FALSE;
- }
-
- // --- MyApp:Main() ------------------------------------------------
-
- void MyApp::Main( int, char*[] )
- {
- #ifdef MAC
- Sysdepen::InitOLE();
- if ( !Sysdepen::IsOLEInit() )
- pApp->Abort("Unable to initialize OLE !");
-
- OLEREG_HKEY aHKey;
- OLELONG nValue;
-
- aHKey = OleregOpenRegistration();
-
- if ( OLEREG_OK != OleregGetValue("Connect", OLEREGSVR_HUMAN_READABLE,
- 0, NULL, &nValue ) )
- {
- long aSig;
-
- aSig = Sysdepen::GetCreator();
- OleregSetValue("Connect", OLEREGSVR_HUMAN_READABLE,
- REG_IGNORE, "StarView Connect", 8 );
- OleregSetValue("Connect", OLEREGSVR_SIGNATURE,
- REG_IGNORE, (char*) &aSig, 4 );
- OleregSetValue("Connect", OLEREGSVR_VERB,
- REG_REPLACE, "Edit", 4 );
- }
-
- OleregCloseRegistration(aHKey);
- #endif
-
- ChangeAppName( "Connect" );
- MyWindow aWindow;
- aWindow.ChangeOutputSizePixel( Size( 300, 200 ) );
-
- MenuBar aBar( ResId( IDM_MENU ) );
- ChangeAppMenu( &aBar );
- aBar.PushSelectHdl( LINK( &aWindow, MyWindow, MenuCommand ) );
-
- MyServer aServer( aWindow );
- aWindow.SetServer( aServer );
- if ( !aServer.IsServer() )
- aWindow.Show();
-
- Execute();
- }
-