home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Containers / SimpleApp / App.cpp next >
Encoding:
C/C++ Source or Header  |  1996-12-21  |  4.9 KB  |  202 lines  |  [TEXT/CWIE]

  1. #include "headers.h"
  2. #include <drag.h>
  3. #include "Util.h"
  4. #include "CContainer.h"
  5. #include "CSimpleAppSite.h"
  6. #include "CArgumentParser.h"
  7. #include "CDragDrop.h"
  8. #include "App.h"
  9. #include <ctype.h>
  10. #include "urlapi.h"
  11.  
  12. #define DEFAULT_WIDTH    150
  13. #define DEFAULT_HEIGHT    150
  14.  
  15. CContainer*     gContainer = nil;
  16. CSimpleAppSite* gSite;
  17. IControl*        gControl;
  18. CDragDrop*        gDragDrop = nil;
  19. Rect            gControlRect = {50, 50, 200, 200};
  20.  
  21. static void DoContextChange(UInt32 ContextID, ContextCommand Command);
  22. static void    Inval(void);
  23.  
  24. void InitApp(AppData* AppDataPtr)
  25. {
  26.     // Initialize ActiveX
  27.     CoInitialize(nil);
  28.     OpenUrlMoniker(nil);
  29.     
  30.     // Initalize drag and drop
  31.     gDragDrop = CDragDrop::NewDragDrop(AppDataPtr->Window);
  32.     
  33.     // Create the one and only container
  34.     // Real container apps will instantiate one of these per container
  35.     gContainer = new CContainer(nil, AppDataPtr->Window);
  36.     
  37.     // Create the one and only site
  38.     // Real container apps will have one of these per figure or object
  39.     gSite = new CSimpleAppSite();
  40.     
  41.     // Add the site to the container
  42.     // Use the site object to manage control enumeration
  43.     gContainer->AddSite(gSite);
  44.     
  45.     // Tell the site about our IContainer interface
  46.     // Pass the IContainer interface and any container-specific data you want
  47.     gSite->SetContainerData((IContainer*) gContainer, AppDataPtr);
  48.     
  49. }
  50.  
  51. void ExitApp(void)
  52. {
  53.     RemoveControl();            // Remove the control
  54.     
  55.     gSite->Release();            // Release the site
  56.     gContainer->Release();        // Release the container
  57.     
  58.     if ( gDragDrop )            // Dispose of drag and drop
  59.         gDragDrop->DisposeDragDrop();
  60.         
  61.     CloseUrlMoniker();
  62.     CoUninitialize();            // Release COM
  63. }
  64.  
  65.  
  66. static void toLower(char* src);
  67. static void toLower(char* src)
  68. {
  69.     for(char * p = src;*p; p++){
  70.         *p = tolower(*p);
  71.     }
  72. }
  73.  
  74. void CreateControlFromArgs(Char8 **inArgNames, Char8 **inArgValues, Int16 inArgCount)
  75. {
  76.     short i;
  77.     char    Test[256];
  78.     short    Width = DEFAULT_WIDTH, Height = DEFAULT_HEIGHT;    
  79.     
  80.     RemoveControl();
  81.  
  82.     // Set the pseudo OBJECT tag parameters
  83.     // Real apps would likely have an OBJECT tag to pass
  84.     for ( i = 0; i < inArgCount; i++ )
  85.     {
  86.         gSite->AddParam(*(inArgNames+i), *(inArgValues+i));
  87.         
  88.         // See if we can pull the size out
  89.         strcpy(Test, *(inArgNames+i));
  90.         toLower(Test);
  91.         
  92.         if ( strcmp(Test, "width") == 0 )
  93.             Width = atoi(*(inArgValues+i));
  94.         else if ( strcmp(Test, "height") == 0 )
  95.             Height = atoi(*(inArgValues+i));
  96.     }
  97.     
  98.     // Adjust the size of the control rect
  99.     gControlRect.right = gControlRect.left + Width;
  100.     gControlRect.bottom = gControlRect.top + Height;
  101.     
  102.     // Ask the site to create the control
  103.     // Request that the control be created, downloaded if necessary
  104.     gSite->CreateControl(true);
  105.     
  106.     // Get the IControl interface for the control
  107.     gSite->QueryInterface(IID_IControl, &gControl);
  108.     
  109.     // Add the only DrawContext to the control
  110.     // Some apps may have multiple contexts in which this site is displayed
  111.     DoContextChange(WindowContextID, AddContext);
  112.     if (gAppData.Offscreen)
  113.         DoContextChange(OffscreenContextID, AddContext);
  114.     
  115.     // Activate the context
  116.     DoContextChange(WindowContextID, ActivateContext);
  117. }
  118.  
  119. void CreateControlFromFileSpec(FSSpecPtr inFileSpec)
  120. {
  121.     RemoveControl();
  122.  
  123.     Boolean8        GotArgs = false;
  124.     Char8            **ArgNames;
  125.     Char8            **ArgValues;
  126.     Int16            ArgCount;
  127.     Ptr                ArgBufferP = NULL;
  128.     Int16            ResFile = ::FSpOpenResFile(inFileSpec, fsRdPerm);
  129.     StringHandle    StrHandle = NULL;
  130.  
  131.     if (::ResError() == noErr)
  132.     {
  133.         if ((StrHandle = ::GetString(128)) != NULL)
  134.         {
  135.             CArgumentParser    Parser;
  136.             Int16            i = 0;
  137.             Int16            Len = **StrHandle;
  138.  
  139.             while (++i <= Len)
  140.                 Parser.ProcessChar((*StrHandle)[i]);
  141.             Parser.ProcessChar('\0');
  142.             ::ReleaseResource(Handle(StrHandle));
  143.             GotArgs = Parser.GetArguments(&ArgBufferP,&ArgNames, &ArgValues, &ArgCount);
  144.         }
  145.         ::CloseResFile(ResFile);
  146.     }
  147.  
  148.     //    for now we need to close the res file before we instantiate the control
  149.     //    so that the resource fork chain is straight forward. I hope to fix this.
  150.  
  151.     if (GotArgs)
  152.     {
  153.         Handle    BaseURL = FileURLFromFileSpec(inFileSpec);
  154.         if ( BaseURL )
  155.         {
  156.             ::HLock(BaseURL);
  157.             gSite->SetBaseURL(*BaseURL);
  158.             ::HUnlock(BaseURL);
  159.             ::DisposeHandle(BaseURL);
  160.         }
  161.  
  162.         CreateControlFromArgs(ArgNames, ArgValues, ArgCount);
  163.         ::DisposePtr(ArgBufferP);
  164.     }
  165.     else
  166.         ::SysBeep(0);
  167. }
  168.  
  169.  
  170. void RemoveControl(void)
  171. {
  172.     if ( gControl )
  173.     {
  174.  
  175.         // Remove the only context we added
  176.         DoContextChange(1, RemoveContext);
  177.         
  178.         gControl->Release();        // Release our reference on IControl
  179.         gSite->DestroyControl();    // Destroy the control
  180.  
  181.         gControl = nil;
  182.     }
  183. }
  184.  
  185.  
  186. void DoContextChange(UInt32 ContextID , ContextCommand Command)
  187. {
  188.     DrawContext    Context = { BeginPortType };
  189.  
  190.     if ( gControl )
  191.     {    
  192.         // Add the only DrawContext to the control
  193.         // Some apps may have multiple contexts in which this site is displayed
  194.         gSite->AcquireContext(ContextID, &Context);
  195.         ::EraseRect(&(Context.Port->portRect));
  196.         if (Context.PortType == QDWindowPortType)
  197.             ::InvalRect(&(Context.Port->portRect));
  198.         gSite->ReleaseContext(&Context);
  199.         gControl->OnContextChange(ContextID, Command);
  200.     }    
  201. }
  202.