home *** CD-ROM | disk | FTP | other *** search
/ PC Direkt 1995 March / PCD_395.iso / starview / pm2blci / german / svrdemo.cx_ / SVRDEMO.CXX
Encoding:
C/C++ Source or Header  |  1994-02-02  |  9.4 KB  |  378 lines

  1. /*******************************************************************
  2. *  SVRDEMO.CXX
  3. *  (c) 1992-1994 STAR DIVISION
  4. *******************************************************************/
  5.  
  6. #ifdef MAC
  7. #include <mac_start.h>
  8. #include <SCM.h>
  9. #include <ole.h>
  10. #include <mac_end.h>
  11. #include <sysdep.hxx>
  12. #endif
  13.  
  14. #include <sv.hxx>
  15. #include <so.hxx>
  16.  
  17. #include "svrdemo.hrc"
  18.  
  19. #define MAXPOINTS 500
  20.  
  21. class MyServer;
  22.  
  23. // --- class MyApp -------------------------------------------------
  24.  
  25. class MyApp : public Application
  26. {
  27. public:
  28.     virtual void    Main( int, char*[] );
  29. };
  30.  
  31. // --- Instance of Application Class -------------------------------
  32.  
  33. MyApp aMyApp;
  34.  
  35. short VERSION = 0x0100;
  36.  
  37. // --- class MyWindow ----------------------------------------------
  38.  
  39. class MyWindow : public WorkWindow
  40. {
  41. private:
  42.     MyServer*       pSvr;
  43.     Size            aSize;
  44.     short           nPointCount;
  45.     Point           aPointArray[MAXPOINTS];
  46.  
  47. public:
  48.                     MyWindow();
  49.  
  50.     virtual void    Resize();
  51.     virtual BOOL    Close();
  52.     virtual void    MouseButtonDown( const MouseEvent& rMEvt );
  53.     virtual void    MouseMove( const MouseEvent& rMEvt );
  54.     virtual void    MouseButtonUp( const MouseEvent& rMEvt );
  55.     virtual void    Paint( const Rectangle& rRect );
  56.  
  57.     void            SetServer( MyServer& p ) { pSvr = &p; }
  58.     long            MenuCommand( Menu* pMenu );
  59.     void            PaintIt();
  60.     BOOL            ReceiveData( MemoryBlock& rBlock );
  61.     BOOL            TransmitData( MemoryBlock& rBlock );
  62.     BOOL            TransmitPicture( GDIMetaFile& rMtf );
  63.     void            RecalcData( const Size& rSize );
  64. };
  65.  
  66.  
  67. // --- class MyServer ----------------------------------------------
  68.  
  69. class MyServer : public StarObjectServer
  70. {
  71. private:
  72.     MyWindow* pWin;
  73.  
  74. public:
  75.                     MyServer( MyWindow& );
  76.  
  77.     virtual BOOL    Create() { return Action( 0 ); }
  78.     virtual BOOL    Action( USHORT nAction );
  79.     virtual void    NewNames( const String&, const String& );
  80.     virtual BOOL    ReceiveData( MemoryBlock& rBlock );
  81.     virtual BOOL    TransmitData( MemoryBlock& rBlock );
  82.     virtual BOOL    TransmitPicture( GDIMetaFile& rMtf );
  83. };
  84.  
  85. // --- MyWindow::MyWindow() ----------------------------------------
  86.  
  87. MyWindow::MyWindow() : WorkWindow( NULL, WB_APP | WB_STDWORK )
  88. {
  89.     pSvr = NULL;
  90.     nPointCount = 0;
  91.     SetText( "Connect" );
  92.     ChangeMapMode( MapMode( MAP_100TH_MM ) );
  93. }
  94.  
  95. // --- MyWindow::MenuCommand() -------------------------------------
  96.  
  97. long MyWindow::MenuCommand( Menu* pMenu )
  98. {
  99.     switch ( pMenu->GetCurItemId() )
  100.     {
  101.         case IDM_EXIT:
  102.             Close();
  103.             break;
  104.     }
  105.  
  106.     return TRUE;
  107. }
  108.  
  109. // --- MyWindow::MouseMove() ---------------------------------------
  110.  
  111. void MyWindow::MouseMove( const MouseEvent& rMEvt )
  112. {
  113.     if ( rMEvt.IsLeft() && (nPointCount < MAXPOINTS) )
  114.     {
  115.         Point p( rMEvt.GetPosPixel() );
  116.         if ( p.X() >= 0 && p.Y() >= 0)
  117.             aPointArray[ nPointCount ] = PixelToLogic( p );
  118.         nPointCount++;
  119.         Invalidate();
  120.     }
  121. }
  122.  
  123. // --- MyWindow::MouseButtonDown() ---------------------------------
  124.  
  125. void MyWindow::MouseButtonDown( const MouseEvent& rMEvt )
  126. {
  127.     if ( rMEvt.IsLeft() )
  128.         CaptureMouse();
  129.     else
  130.     {
  131.         nPointCount = 0;
  132.         Invalidate();
  133.         Update();
  134.     }
  135. }
  136.  
  137. // --- MyWindow::MouseButtonUp() -----------------------------------
  138.  
  139. void MyWindow::MouseButtonUp( const MouseEvent& rMEvt )
  140. {
  141.     if( rMEvt.IsLeft() )
  142.         ReleaseMouse();
  143. }
  144.  
  145. // --- MyWindow::Paint() -------------------------------------------
  146.  
  147. void MyWindow::Paint( const Rectangle& )
  148. {
  149.     PaintIt();
  150. }
  151.  
  152. // --- MyWindow::Resize() ------------------------------------------
  153.  
  154. void MyWindow::Resize()
  155. {
  156.     Size s = PixelToLogic( GetOutputSizePixel(),
  157.                            MapMode( MAP_100TH_MM ) );
  158.  
  159.     if ( s.Width() && s.Height() )
  160.         RecalcData( s );
  161. }
  162.  
  163. // --- MyWindow::PaintIt() -----------------------------------------
  164.  
  165. void MyWindow::PaintIt()
  166. {
  167.     for ( short i = 0; i < (nPointCount-1); i++ )
  168.         for ( short j = 0; j < nPointCount; j++ )
  169.             DrawLine( aPointArray[ i ], aPointArray[ j ] );
  170. }
  171.  
  172. // --- MyWindow::ReceiveData() -------------------------------------
  173.  
  174. BOOL MyWindow::ReceiveData( MemoryBlock& rData )
  175. {
  176.     BOOL bRes = FALSE;
  177.     short n = 0;
  178.     rData >> n;
  179.     if ( n <= VERSION )
  180.     {
  181.         rData >> aSize >> nPointCount;
  182.         for( int i = 0; i < nPointCount; i++ )
  183.             rData >> aPointArray[i];
  184.         bRes = BOOL( rData.good() );
  185.     }
  186.  
  187.     if ( bRes )
  188.         ChangeOutputSizePixel( LogicToPixel( aSize ) );
  189.     else
  190.         nPointCount = 0;
  191.  
  192.     return bRes;
  193. }
  194.  
  195. // --- MyWindow::TransmitData() ------------------------------------
  196.  
  197. BOOL MyWindow::TransmitData( MemoryBlock& rData )
  198. {
  199.     rData << VERSION << aSize << nPointCount;
  200.     for( int i = 0; i < nPointCount; i++ )
  201.         rData << aPointArray[i];
  202.  
  203.     return BOOL( rData.good() );
  204. }
  205.  
  206. // --- MyWindow::TransmitPicture() ---------------------------------
  207.  
  208. BOOL MyWindow::TransmitPicture( GDIMetaFile& aPic )
  209. {
  210.     MapMode aMM( MAP_100TH_MM );
  211.     aPic.ChangePrefSize( PixelToLogic( GetOutputSizePixel(), aMM ) );
  212.     aPic.ChangePrefMapMode( aMM );
  213.     aPic.Record( this );
  214.     ChangePen( Pen( Color( COL_BLACK ) ) );
  215.     PaintIt();
  216.     aPic.Stop();
  217.  
  218.     return TRUE;
  219. }
  220.  
  221. // --- MyWindow::RecalcData() --------------------------------------
  222.  
  223. void MyWindow::RecalcData( const Size& rSize )
  224. {
  225.     if ( nPointCount && aSize.Width() &&
  226.          aSize.Height() && aSize != rSize )
  227.     {
  228.         short dx100 = (short)((long)rSize.Width() * 100 / aSize.Width());
  229.         short dy100 = (short)((long)rSize.Height() * 100 / aSize.Height());
  230.  
  231.         for ( short i = 0; i < nPointCount; i++ )
  232.         {
  233.             aPointArray[i].X() =
  234.                     (short)((long)aPointArray[i].X() * dx100 / 100);
  235.             aPointArray[i].Y() =
  236.                     (short)((long)aPointArray[i].Y() * dy100 / 100);
  237.         }
  238.  
  239.         Invalidate();
  240.     }
  241.  
  242.     aSize = rSize;
  243. }
  244.  
  245. // --- MyWindow::Close() --------------------------------------
  246.  
  247. BOOL MyWindow::Close()
  248. {
  249.     if ( !pSvr->IsServer() )
  250.     {
  251.         pApp->Quit(); return TRUE;
  252.     }
  253.  
  254.     short nRet = MessBox( this, WB_YES_NO_CANCEL | WB_DEF_YES,
  255.                           "Connect",
  256.                           String( ResId( IDS_UPDATE ) ) ).Execute();
  257.  
  258.     switch ( nRet )
  259.     {
  260.         case RET_YES:
  261.             pSvr->Update();
  262.         case RET_NO:
  263.             pApp->Quit();
  264.             return TRUE;
  265.  
  266.         default:
  267.             return FALSE;
  268.     }
  269. }
  270.  
  271. // --- MyServer::MyServer() ----------------------------------------
  272.  
  273. MyServer::MyServer( MyWindow& aWindow ) :
  274.             StarObjectServer()
  275. {
  276.     pWin = &aWindow;
  277. }
  278.  
  279. // --- MyServer::ReceiveData() -------------------------------------
  280.  
  281. BOOL MyServer::ReceiveData( MemoryBlock& rData )
  282. {
  283.     return pWin->ReceiveData( rData );
  284. }
  285.  
  286. // --- MyServer::TransmitData() ------------------------------------
  287.  
  288. BOOL MyServer::TransmitData( MemoryBlock& rData )
  289. {
  290.     return pWin->TransmitData( rData );
  291. }
  292.  
  293. // --- MyServer::TransmitPicture() ----------------------------------
  294.  
  295. BOOL MyServer::TransmitPicture( GDIMetaFile& rPic )
  296. {
  297.     return pWin->TransmitPicture( rPic );
  298. }
  299.  
  300. // --- MyServer::NewNames() ----------------------------------------
  301.  
  302. void MyServer::NewNames( const String& rApp, const String& rObj )
  303. {
  304.     String aTitle( "Connect - " );
  305.     aTitle += rObj;
  306.     aTitle += " in ";
  307.     aTitle += rApp;
  308.     pWin->SetText( aTitle );
  309.  
  310.     String s( ResId( IDS_BACK ) );
  311.     s += rApp;
  312.     pApp->GetAppMenu()->
  313.             GetPopupMenu( IDM_FILE )->SetItemText( IDM_EXIT, s );
  314. }
  315.  
  316. // --- MyServer::Action() ------------------------------------------
  317.  
  318. BOOL MyServer::Action( USHORT nAction )
  319. {
  320.     if( nAction == 0 )
  321.     {
  322.         Size s( GetSize () );
  323.         if ( s.Width() && s.Height() )
  324.             pWin->ChangeOutputSizePixel( pWin->LogicToPixel( s ) );
  325.         pWin->Show();
  326.         return TRUE;
  327.     }
  328.     else
  329.         return FALSE;
  330. }
  331.  
  332. // --- MyApp:Main() ------------------------------------------------
  333.  
  334. void MyApp::Main( int, char*[] )
  335. {
  336. #ifdef MAC
  337.     Sysdepen::InitOLE();
  338.     if ( !Sysdepen::IsOLEInit() )
  339.         pApp->Abort("Unable to initialize OLE !");
  340.  
  341.     OLEREG_HKEY aHKey;
  342.     OLELONG     nValue;
  343.  
  344.     aHKey = OleregOpenRegistration();
  345.  
  346.     if ( OLEREG_OK != OleregGetValue("Connect", OLEREGSVR_HUMAN_READABLE,
  347.                                      0, NULL, &nValue ) )
  348.     {
  349.         long aSig;
  350.  
  351.         aSig = Sysdepen::GetCreator();
  352.         OleregSetValue("Connect", OLEREGSVR_HUMAN_READABLE,
  353.                        REG_IGNORE, "StarView Connect", 8 );
  354.         OleregSetValue("Connect", OLEREGSVR_SIGNATURE,
  355.                        REG_IGNORE, (char*) &aSig, 4 );
  356.         OleregSetValue("Connect", OLEREGSVR_VERB,
  357.                        REG_REPLACE, "Edit", 4 );
  358.     }
  359.  
  360.     OleregCloseRegistration(aHKey);
  361. #endif
  362.  
  363.     ChangeAppName( "Connect" );
  364.     MyWindow aWindow;
  365.     aWindow.ChangeOutputSizePixel( Size( 300, 200 ) );
  366.  
  367.     MenuBar aBar( ResId( IDM_MENU ) );
  368.     ChangeAppMenu( &aBar );
  369.     aBar.PushSelectHdl( LINK( &aWindow, MyWindow, MenuCommand ) );
  370.  
  371.     MyServer aServer( aWindow );
  372.     aWindow.SetServer( aServer );
  373.     if ( !aServer.IsServer() )
  374.         aWindow.Show();
  375.  
  376.     Execute();
  377. }
  378.