home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / ADDON.PAK / TOOLSRVR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  14.4 KB  |  524 lines

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.  
  3.   toolsrvr.cpp
  4.   Created: 10/24/95
  5.   Copyright (c) 1995, Borland International
  6.   $Revision:   1.19  $
  7.    
  8. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/  
  9. #ifndef __AOEXPCH_H
  10.   #include "aoexpch.h"
  11. #endif
  12. #pragma hdrstop
  13.  
  14. #include "toolsrvr.h"
  15.  
  16. //.............................................................................
  17. ToolTestA::ToolTestA() 
  18.   {
  19.   _toolServer = NULL;
  20.   }
  21. //.............................................................................
  22. ToolTestA::~ToolTestA() 
  23.   {
  24.   UnInit();
  25.   }
  26. //.............................................................................
  27. BOOL ToolTestA::Init() 
  28.   {
  29.   if ( !_toolServer ) 
  30.     {
  31.     OutStr( "ToolTestA::Init()" );
  32.     _toolServer = GET_INTERFACE(IToolServer);
  33.     if ( _toolServer == NULL ) 
  34.       {
  35.       return FALSE;
  36.       }
  37.     }
  38.   return TRUE;
  39. }
  40. //.............................................................................
  41. void ToolTestA::UnInit() 
  42.   {
  43.   OutStr( "ToolTestA::UnInit()" );
  44.   if ( _toolServer ) 
  45.     {
  46.     _toolServer->Release();
  47.     _toolServer = NULL;
  48.     }
  49.   }
  50. //.............................................................................
  51. const char * ToolTestA::GetName() 
  52.   {
  53.   return "Tool Test A";
  54.   }
  55. //.............................................................................
  56. const char * ToolTestA::GetTestDescription( int testNum ) 
  57.   {
  58.   switch ( testNum )
  59.     {
  60.     case 1: 
  61.       return "Returns info about the WinSight tool.";
  62.     case 2: 
  63.       return "resource compiles the currently selected .rc node.";
  64.     case 3: 
  65.       return "Grep currently selected nodes.";
  66.     case 4: 
  67.       return "Dump some lines from autoexec.bat to msg window.";
  68.     }
  69.   return "This test not implemented.";
  70.   }  
  71. //.............................................................................
  72. void ToolTestA::DoTest( int testNum ) 
  73.   {
  74.   char buf[ 500 ];
  75.   if ( !_toolServer ) 
  76.     {
  77.     OutStr( "Tool Server not initialized!" );
  78.     return;
  79.     }
  80.   switch ( testNum ) 
  81.     {  
  82.     case 1: 
  83.       {
  84.       ToolObj toolObj = _toolServer->ToolFind( ::MakePolyString ( "WinSight" ) );
  85.       if ( toolObj ) 
  86.         {
  87.         IToolInfo * pToolInfo = _toolServer->QueryToolInfo ( toolObj );
  88.         if ( pToolInfo != NULL )
  89.           {
  90.           ConcatPolyStr ( "Name = ", pToolInfo->GetName(), buf, 255, TRUE );
  91.           ConcatPolyStr ( "Menu Name = ", pToolInfo->GetMenuName(), buf, 255, TRUE );
  92.           ConcatPolyStr ( "Help Hint = ", pToolInfo->GetHelpHint(), buf, 255, TRUE );
  93.           ConcatPolyStr ( "Path = ", pToolInfo->GetPath(), buf, 255, TRUE );
  94.           ConcatPolyStr ( "Cmd Line = ", pToolInfo->GetDefCmdLine(), buf, 255, TRUE );
  95.           ConcatPolyStr ( "Supported Types = ", pToolInfo->GetSupportedTypes(), buf, 255, TRUE );
  96.           ConcatPolyStr ( "DefForTypes = ", pToolInfo->GetDefaultForTypes(), buf, 255, TRUE );
  97.           ConcatPolyStr ( "TranslateTo = ", pToolInfo->GetTranslateTo(), buf, 255, TRUE );
  98.  
  99.           pToolInfo->Release();
  100.           }
  101.         } 
  102.       else 
  103.         {
  104.         OutStr( FormatStr( "Couldn't find tool named %s.", "WinSight" ) );
  105.         }
  106.       break;
  107.       }
  108.  
  109.     case 2:  // cpp compile the currently selected node.
  110.       {
  111.       OutStr( "Invoking the resource compiler..." );
  112.       ToolObj toolObj = _toolServer->ToolFind( 
  113.         ::MakePolyString ( "CompileResources" ) 
  114.         );
  115.       if ( toolObj ) 
  116.         {
  117.         ToolReturn tr = _toolServer->ToolInvoke( toolObj );
  118.         OutStr( FormatStr( "ToolReturn = %d", tr ) );
  119.         }
  120.       else
  121.         {
  122.         OutStr( "CompileResources not found" );
  123.         }
  124.       break;
  125.       }
  126.  
  127.     case 3:  // grep selected nodes.
  128.       {
  129.       OutStr( "Invoking GrepFiles tool..." );
  130.       ToolObj toolObj = _toolServer->ToolFind( 
  131.         ::MakePolyString ( "GrepFiles" )
  132.         );
  133.       if ( toolObj ) 
  134.         {
  135.         ToolReturn tr = _toolServer->ToolInvoke( toolObj );
  136.         OutStr( FormatStr( "ToolReturn = %d", tr ) );
  137.         }
  138.       else
  139.         {
  140.         OutStr( "GrepFiles not found" );
  141.         }
  142.       break;
  143.       }
  144.  
  145.     case 4:  // search for path in autoexec.bat
  146.       {
  147.       OutStr( "Invoking GrepFiles tool..." );
  148.       ToolObj toolObj = _toolServer->ToolFind(
  149.         ::MakePolyString ( "GrepFiles" ) 
  150.         );
  151.       if ( toolObj ) 
  152.         {
  153.         ToolReturn tr = _toolServer->ToolInvokeName(
  154.           toolObj,
  155.           ::MakePolyString ( "" ),
  156.           ::MakePolyString ( 
  157.             "-r+ [A-Za-z] c:\\autoexec.bat $NOSWAP$CAP MSG(GREP2MSG)" )
  158.           );
  159.         OutStr( FormatStr( "ToolReturn = %d", tr ) );
  160.         }
  161.       else
  162.         {
  163.         OutStr( "GrepFiles not found" );
  164.         }
  165.       break;
  166.       }
  167.  
  168.     default: 
  169.       {
  170.       OutStr( FormatStr( "Test #%d Not Implemented!", testNum ) );
  171.       }
  172.     }
  173.   }
  174.  
  175. void ToolTestA::ConcatPolyStr (const char *startStr,
  176.               IPolyString *pPoly, char *buf, int max, bool OutIt )
  177.   {
  178.   strncpy ( buf, startStr, max );
  179.   if ( pPoly != NULL )
  180.     {  
  181.     const char *cstr = pPoly->GetCstr();
  182.     if ( cstr != NULL )
  183.       {
  184.       strncat ( buf, cstr, max );
  185.       }
  186.     pPoly->Release();
  187.     }
  188.   if ( OutIt )
  189.     {
  190.     OutStr( buf );
  191.     }
  192.   }
  193.  
  194. ///////////////////////////////////////////////////////////////////////////////
  195. //
  196. //  Tool Implementors - Callbacks for TestTool1
  197. //
  198. ///////////////////////////////////////////////////////////////////////////////
  199. class TestToolImplOne : public IUnknownImp<IToolImplementor>
  200. {
  201.  
  202.  public:
  203.   TestToolImplOne () : IUNKNOWNIMPL_INIT( IToolImplementor ) {}
  204.  
  205.   virtual ToolReturn Execute( IPolyString * cmdLine, 
  206.                       ProjectNode * nodeArray, 
  207.                       int numNodes );
  208. };
  209.  
  210. ToolReturn TestToolImplOne::Execute( IPolyString *, ProjectNode *, int)
  211.   {
  212.   ::MessageBox(NULL, "Cool Tool", "Addon Message", MB_OK);
  213.   return TR_Success;
  214.   }
  215.  
  216. class TestToolImplTwo : public IUnknownImp<IToolImplementor>
  217. {
  218.  
  219.  public:
  220.   TestToolImplTwo () : IUNKNOWNIMPL_INIT( IToolImplementor ) {}
  221.  
  222.   virtual ToolReturn Execute( IPolyString * cmdLine, 
  223.                       ProjectNode * nodeArray, 
  224.                       int numNodes );
  225. };
  226.  
  227. ToolReturn TestToolImplTwo::Execute( IPolyString *, ProjectNode *, int)
  228.   {
  229.   ::MessageBox(NULL, "Cool Tool 2", "Addon Message", MB_OK);
  230.   return TR_Success;
  231.   }
  232.  
  233.  
  234. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  235.  
  236.   ProjectClientImpl 
  237.   
  238.   Add a tool when a project opens
  239.    
  240. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/  
  241. void ProjectToolClient::OpenNotify( IPolyString * projectName ) 
  242.   {
  243.   if ( _currentProject )
  244.     _currentProject->Release();
  245.   
  246.   _currentProject = projectName;
  247.  
  248.   if ( _currentProject )
  249.     _toolTestB->AddTool();
  250.   }
  251.  
  252. void ProjectToolClient::CloseNotify() 
  253.   {
  254.   if ( _currentProject )
  255.     {
  256.     _currentProject->Release();
  257.     _currentProject = NULL;
  258. //
  259. //     It turns out that this is a bad place to remove tools. In earlier
  260. //     versions of BCW, this event was being fired off too early, when a 
  261. //     project close could still be aborted by the user. Now, removing 
  262. //     the tool here has no effect, since it's too late for the toolset
  263. //     to be updated. Also, this wouldn't prevent the tools from being
  264. //     saved with the project if the user explicitly performs a project
  265. //     save without a project close. So, we use a new interface, 
  266. //     IProjectSaveClient to trigger the tool remove.
  267. //
  268. //     _toolTestB->RemoveTool();
  269.     }
  270.   }
  271.   
  272. void ToolTestB::BeforeSaveNotify() {
  273.   //
  274.   // Don't let the add-on tool be saved with the project.
  275.   //
  276.   RemoveTool();
  277. }
  278. void ToolTestB::AfterSaveNotify() {
  279.   //
  280.   // Put it back, since the project doesn't always close after a save.
  281.   //
  282.   AddTool();
  283. }
  284.   
  285.  
  286. ///////////////////////////////////////////////////////////////////////////////
  287. //
  288. //  ToolTestB
  289. //
  290. ///////////////////////////////////////////////////////////////////////////////
  291.  
  292. //.............................................................................
  293. // Make a project client so we can add a tool when a project opens.
  294. //
  295. ToolTestB::ToolTestB() : IUNKNOWNIMPL_INIT( IProjectSaveClient ) {
  296.   _toolServer = NULL;
  297.   _projectClient = NULL;
  298.   _projectServer = NULL;
  299.  
  300.   _toolServer = GET_INTERFACE(IToolServer);
  301.   if ( _toolServer ) {
  302.     _projectServer = GET_INTERFACE( IProjectServer );
  303.     if ( _projectServer ) {
  304.       _projectClient = new ProjectToolClient( this );
  305.       if ( _projectClient ) {
  306.         _projectClient->AddRef();
  307.         _projectServer->RegisterProjectClient( _projectClient );
  308.       }
  309.       IProjectServer2 * ps2 = GET_INTERFACE( IProjectServer2 );
  310.       if ( ps2 ) {  // this interface not availabe in BCW versions < 5.01
  311.         AddRef();
  312.         ps2->RegisterProjectSaveClient( this );
  313.         ps2->Release();
  314.       }
  315.     }
  316.   }
  317. }
  318.  
  319. //.............................................................................
  320. ToolTestB::~ToolTestB() {
  321.   if ( _projectClient ) {
  322.     if ( _projectServer ) {
  323.       _projectClient->AddRef();
  324.       _projectServer->UnregisterProjectClient( _projectClient );
  325.       IProjectServer2 * ps2 = GET_INTERFACE( IProjectServer2 );
  326.       if ( ps2 ) {  // this interface not availabe in BCW versions < 5.01
  327.         AddRef();
  328.         ps2->UnregisterProjectSaveClient( this );
  329.         ps2->Release();
  330.       }
  331.     }
  332.     _projectClient->Release();
  333.     _projectClient = 0;
  334.   }
  335.  
  336.   if ( _projectServer ) {
  337.     _projectServer->Release();
  338.   }
  339.  
  340.   if ( _toolServer ) {
  341.     _toolServer->Release();
  342.   }
  343. }
  344. //.............................................................................
  345. BOOL ToolTestB::Init() 
  346.   {
  347.   if ( _toolServer && _projectServer && _projectClient ) 
  348.     {
  349.     OutStr( "Init was done at startup (Success)" );
  350.     return TRUE;
  351.     }
  352.   else
  353.     {
  354.     OutStr( "Init was done at startup (Failed)" );
  355.     return FALSE;
  356.     }
  357.   }
  358.  
  359. //.............................................................................
  360. void ToolTestB::UnInit() 
  361.   {
  362.   OutStr( "UnInit will be done at shutdown" );
  363.   }
  364. //.............................................................................
  365. const char * ToolTestB::GetName() 
  366.   {
  367.   return "Test Tool Implementor";
  368.   }
  369. //.............................................................................
  370. const char * ToolTestB::GetTestDescription( int testNum ) 
  371.   {
  372.   switch ( testNum )
  373.     {
  374.     case 1: 
  375.       return "invoke the tool called TestTool1";
  376.     case 2: 
  377.       return "Update TestTool1 with a new callback";
  378.     case 3:
  379.       return "Update TestTool1 so it has no callback";
  380.     }
  381.   return "This test not implemented.";
  382.   }  
  383. //.............................................................................
  384. //
  385. // The tool is added at contructor time. These 2 tests just use it. The first
  386. // invokes it. The second gives it a new implementor.
  387. //
  388. void ToolTestB::DoTest( int testNum ) 
  389.   {
  390.   if ( !_toolServer ) 
  391.     {
  392.     OutStr( "Tool Server not initialized!" );
  393.     return;
  394.     }
  395.   switch ( testNum ) 
  396.     {  
  397.     case 1:
  398.       {
  399.       OutStr( "Invoking TestTool1" );
  400.       ToolObj toolObj = _toolServer->ToolFind( ::MakePolyString ( "TestTool1" ) );
  401.       if ( toolObj ) 
  402.         {
  403.         ToolReturn tr = _toolServer->ToolInvoke( toolObj );
  404.         OutStr( FormatStr( "ToolReturn = %d", tr ) );
  405.         }
  406.       else
  407.         {
  408.         OutStr( "TestTool1 not found" );
  409.         }
  410.       }
  411.       break;
  412.  
  413.     case 2:
  414.       {
  415.       OutStr( "Update TestTool1 with a new callback" );
  416.       IToolInfo * ti = _toolServer->CreateToolInfoInstance();
  417.       if ( ti )
  418.         {
  419.         TestToolImplTwo *testtool = new TestToolImplTwo();
  420.         if ( testtool )
  421.           {
  422.           ti->SetImplementor( testtool );
  423.           ti->SetName ( ::MakePolyString( "TestTool1" ) );
  424.           if ( _toolServer->ToolAdd( ti ) ) // will Release the old implementor
  425.             OutStr( "Add Succeeded" );
  426.           else
  427.             OutStr( "Add Failed" );
  428.           }
  429.         else
  430.           {
  431.           OutStr( "Memory Error" );
  432.           }
  433.         ti->Release();
  434.         }
  435.       else
  436.         {
  437.         OutStr( "Memory Error" );
  438.         }
  439.       }
  440.       break;
  441.  
  442.     case 3:
  443.       {
  444.       OutStr( "Update TestTool1 so it has no callback" );
  445.       IToolInfo * ti = _toolServer->CreateToolInfoInstance();
  446.       if ( ti )
  447.         {
  448.         ti->SetImplementor( NULL );
  449.         ti->SetName ( ::MakePolyString( "TestTool1" ) );
  450.         if ( _toolServer->ToolAdd( ti ) ) // will Release the old implementor
  451.           OutStr( "Add Succeeded" );
  452.         else
  453.           OutStr( "Add Failed" );
  454.         ti->Release();
  455.         }
  456.       else
  457.         {
  458.         OutStr( "Memory Error" );
  459.         }
  460.       }
  461.       break;
  462.  
  463.     default: 
  464.       {
  465.       OutStr( FormatStr( "Test #%d Not Implemented!", testNum ) );
  466.       }
  467.     }
  468.   }
  469.  
  470. //.............................................................................
  471. // Called by the project client to add a tool when the project opens
  472. //
  473. void ToolTestB::AddTool()
  474.   { 
  475.   // Add the tool to _toolProjectName project and no other
  476.   OutStr( "Adding a tool called TestTool1" );
  477.   ToolObj toolObj = _toolServer->ToolFind( ::MakePolyString ( "TestTool1" ) );
  478.   if ( toolObj )
  479.     {
  480.     OutStr( "TestTool1 already exists!. Removing it first." );
  481.     _toolServer->ToolRemove( toolObj );
  482.     }
  483.   IToolInfo * ti = _toolServer->CreateToolInfoInstance();
  484.   if ( ti )
  485.     {
  486.     TestToolImplOne *testtool = new TestToolImplOne();
  487.     if ( testtool )
  488.       {
  489.       ti->SetImplementor( testtool );
  490.       ti->SetName ( ::MakePolyString( "TestTool1" ) );
  491.       if ( _toolServer->ToolAdd( ti ) )
  492.         OutStr( "Add Succeeded" );
  493.       else
  494.         OutStr( "Add Failed" );
  495.       }
  496.     else
  497.       {
  498.       OutStr( "Memory Error" );
  499.       }
  500.     ti->Release();
  501.     }
  502.   else
  503.     {
  504.     OutStr( "Memory Error" );
  505.     }
  506.   }
  507.  
  508. //.............................................................................
  509. // Called by the project client to remove a tool when the project closes
  510. //
  511. void ToolTestB::RemoveTool()
  512.   { 
  513.   OutStr( "Removing ToolTest1" );
  514.   ToolObj toolObj = _toolServer->ToolFind( ::MakePolyString ( "TestTool1" ) );
  515.   if ( toolObj ) 
  516.     {
  517.     _toolServer->ToolRemove( toolObj );
  518.     }
  519.   else
  520.     {
  521.     OutStr( "TestTool1 not found" );
  522.     }
  523.   }
  524.