home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / OpenDoc / CreditNow! / CreditNow! Source / $Sources / CPCreditNowDemo.InEx.cpp < prev    next >
Encoding:
Text File  |  1995-04-30  |  14.6 KB  |  424 lines  |  [TEXT/MPCC]

  1. //----------------------------------------------------------------------------------------
  2. // UI Events protocol
  3. //----------------------------------------------------------------------------------------
  4.  
  5. #ifndef _CREDITNOWDEMO_
  6. #include "CPCreditNowDemo.h"
  7. #endif
  8. #include "Launchit.h"
  9.  
  10. //----------------------------------------------------------------------------------------
  11.  
  12. void CPCreditNowDemo::InitPart(Environment* ev, ODStorageUnit* storageUnit, ODPart* self)
  13. {
  14.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_InitPart
  15.     EnteringMethod("\pCPCreditNowDemo::InitPart");
  16.  
  17.     this->PrivInitCreditNowDemo(ev, storageUnit, self);
  18.  
  19.     {
  20.         CUsingLibraryResources fil;
  21.         ::GetIndString(fTextData, kContentStringResID, kContentStringID);
  22.     }        // Use CUsingLibraryResources to use your part's resource fork.
  23.             // Make it go out of scope when you are done.
  24.  
  25.     // Initialize your other fields here.
  26.     fDirty = kODTrue;
  27.         // Make sure our content is written out at least once.
  28. }
  29.  
  30. //----------------------------------------------------------------------------------------
  31.  
  32. void CPCreditNowDemo::InitPartFromStorage(Environment* ev, ODStorageUnit* storageUnit, ODPart* self)
  33. {
  34.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_InitPartFromStorage
  35.     EnteringMethod("\pCPCreditNowDemo::InitPartFromStorage");
  36.  
  37.     TRY
  38.         // To allow editor swapping (translation) at runtime, OpenDoc requires
  39.         // that we pass in a "reference" to ourselves when interacting with the
  40.         // API (ie. WindowState::RegisterWindow(), Dispatcher::RegisterIdle, etc).
  41.         // The "partWrapper" passed to us here and in InitPart is the
  42.         // "reference" OpenDoc is asking us to use.
  43.         fSelf = self;
  44.             
  45.         // Are we being opened from a read-only draft? If so, we cannot
  46.         // write anything back out to our storage unit.
  47.         fReadOnlyStorage = ( storageUnit->GetDraft(ev)->
  48.                                         GetPermissions(ev) == kDPReadOnly );
  49.             
  50.         // Call the common initialization code to get set up.
  51.         this->PrivInitCreditNowDemo(ev, storageUnit, self);
  52.     
  53.         // Read in the state the part was in when it was last Externalized.
  54.         // This allows the part to present the same "environment" the user
  55.         // had the part set up in the last time it was edited.
  56.         this->PrivInternalizeStateInfo(ev, storageUnit);
  57.     
  58.         // Read in the contents for your part editor.
  59.         this->PrivInternalizeContent(ev, storageUnit);
  60.  
  61.         SetTarget( 'CNOW', false );
  62.  
  63.     CATCH_ALL
  64.         ODSetSOMException(ev, ErrorCode());
  65.  
  66.         // Clean up will occur in the destructor which will be called
  67.         // shortly after we THROW the error.
  68.     ENDTRY
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------
  72.  
  73. void CPCreditNowDemo::PrivInitCreditNowDemo(Environment* ev, ODStorageUnit* storageUnit, ODPart* self)
  74. {
  75.     EnteringMethod("\pCPCreditNowDemo::PrivInitCreditNowDemo");
  76.  
  77.     // Save off some references.
  78.     fSelf = self;
  79.     fStorageUnit = storageUnit;
  80.  
  81.     fSession = fStorageUnit->GetSession(ev);
  82.         // As your part grows, add your initialization that is common
  83.         // between InitPart and InitPartFromStorage here.
  84.         // Note that GetStorageUnit can be used, since just prior to
  85.         // calling this method, InitPart calledInitPersistentObject or
  86.         // InitPartFromStorage called InitPersistentObjectFromStorage.
  87.  
  88.     fSelectionFocus = fSession->Tokenize(ev, kODSelectionFocus);
  89.     fMenuFocus      = fSession->Tokenize(ev, kODMenuFocus);
  90.     fKeyFocus       = fSession->Tokenize(ev, kODKeyFocus);
  91.     fModalFocus     = fSession->Tokenize(ev, kODModalFocus);
  92.  
  93.     fFocusSet = fSession->GetArbitrator(ev)->CreateFocusSet(ev);
  94.     fFocusSet->Add(ev, fKeyFocus);
  95.     fFocusSet->Add(ev, fMenuFocus);
  96.     fFocusSet->Add(ev, fSelectionFocus);
  97.  
  98.     fMenuBar = fSession->GetWindowState(ev)->CopyBaseMenuBar(ev);
  99.  
  100. #if 0
  101. rgac
  102.     {
  103.         CUsingLibraryResources fil;
  104.         fMenu = ::GetMenu(kMenuID);
  105.         if (fMenu)
  106.             ::DetachResource((Handle)fMenu);        // Must detach it!
  107.     }
  108.  
  109.     if (!fMenu)
  110.         DebugStr("\pCPCreditNowDemo::PrivInitCreditNowDemo -- couldn't create menu.");
  111.             // We are using debug messages
  112.             // for these types of errors.  When SOM is available, this issue
  113.             // will be addressed correctly.
  114.  
  115.     fMenuBar->AddMenuLast(ev, kMenuID, fMenu, fSelf);
  116. #endif
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120.  
  121. void CPCreditNowDemo::PrivInternalizeStateInfo(Environment* ev, ODStorageUnit* storageUnit)
  122. {
  123.     // Description:    This method could be used to read in settings
  124.     //                information for the part. This would be information
  125.     //                related to the workings of the part editor, not the
  126.     //                content.
  127.     
  128.     EnteringMethod("\pPrivInternalizeStateInfo");
  129.  
  130.     ODStorageUnitRef    weakRef;
  131.     ODULong                size;
  132.  
  133.     // Internalize the part's display frame list.
  134.  
  135.     if ( storageUnit->Exists(ev, kODPropDisplayFrames, kODWeakStorageUnitRefs, 0) )
  136.     {
  137.         storageUnit->Focus(ev, kODPropDisplayFrames, kODPosUndefined,
  138.                                     kODWeakStorageUnitRefs, 0, kODPosUndefined);
  139.                                     
  140.         size = storageUnit->GetSize(ev);
  141.         storageUnit->SetOffset(ev, 0);    
  142.     
  143.         for (ODULong offset = 0; offset < size; offset += 4)
  144.         {    
  145.             TRY    
  146.                 StorageUnitGetValue(storageUnit, ev, sizeof(ODStorageUnitRef), (ODPtr)&weakRef);
  147.                 
  148.                 if ( storageUnit->IsValidStorageUnitRef(ev, weakRef) )
  149.                 {        
  150.                     ODFrame *frame = storageUnit->GetDraft(ev)->
  151.                         GetFrame(ev, storageUnit->GetIDFromStorageUnitRef(ev, weakRef));
  152.                     fDisplayFrames.Add(frame);
  153.                 }
  154.             SOM_ENDTRY
  155.         }
  156.     }
  157. }
  158.  
  159. //----------------------------------------------------------------------------------------
  160.  
  161. void CPCreditNowDemo::PrivInternalizeContent(Environment* ev, ODStorageUnit* storageUnit)
  162. {
  163.     // Description:    This method is called during interalization of the
  164.     //                part. The content of the part should be read in.
  165.     EnteringMethod("\pInternalizeContent");
  166.     
  167.     storageUnit->Focus(ev, kODPropContents, kODPosSame,
  168.                        kCreditNowDemoKind, 0, kODPosSame);
  169.     fTextData[0] = (unsigned char)storageUnit->GetSize(ev);
  170.     StorageUnitGetValue(storageUnit, ev, fTextData[0], &fTextData[1]);
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void CPCreditNowDemo::Externalize(Environment* ev)
  176. {
  177.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_Externalize
  178.     EnteringMethod("\pCPCreditNowDemo::Externalize");
  179.  
  180.     if (fDirty && !fReadOnlyStorage)
  181.     {
  182.         // Get our storage unit.
  183.         ODStorageUnit* storageUnit = fSelf->GetStorageUnit(ev);
  184.  
  185.         // Verify that the storage unit has the appropriate properties
  186.         // and values to allow us to run. If not, add them.
  187.         this->PrivCheckAndAddProperties(ev, storageUnit);
  188.     
  189.         // Verify that there are no "bogus" values in the Content
  190.         // property.
  191.         this->PrivCleanContentProperty(ev, storageUnit);
  192.     
  193.         // Write out the part's state information.
  194.         this->PrivExternalizeStateInfo(ev, storageUnit, 0, kODNULL);
  195.             
  196.         // Write out the part's content.
  197.         this->PrivExternalizeContent(ev, storageUnit);
  198.  
  199.         // Update the "last modified" date and other part annotations.
  200.         UpdateModificationInfo(ev, storageUnit);
  201.  
  202.         // Flag our part as no longer being dirty.
  203.         fDirty = kODFalse;
  204.     }
  205. }
  206.  
  207. //----------------------------------------------------------------------------------------
  208.  
  209. static ODBoolean PrivGetUserCatFromCat(ODNameSpaceManager* theNmSpcMgr, 
  210.                                     ODType category, ODIText** name)
  211. {
  212.     ODBoolean result = kODFalse ;
  213.  
  214.     Environment* ev = somGetGlobalEnvironment();
  215.  
  216.         // look it up in the spaceName namespace
  217.     ODObjectNameSpace* userStringNameSpace = 
  218.         (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODCategoryUserString );
  219.  
  220.     if (userStringNameSpace)
  221.         result = userStringNameSpace->GetEntry( ev, category, (ODObject**)name );
  222.     return result ;
  223. }
  224.  
  225. void CPCreditNowDemo::PrivCheckAndAddProperties(Environment* ev, ODStorageUnit* storageUnit)
  226. {
  227.     // Description:    This method is called during the creation of
  228.     //                stationery, to prepare a storage unit, and during
  229.     //                externalization, to verify that all the properties we
  230.     //                need are present.
  231.     //
  232.     //                The part adds the default content property, a
  233.     //                preferred editor property (to aid in part binding), 
  234.     //                and a default name for the part.
  235.  
  236.     EnteringMethod("\pPrivCheckAndAddProperties");
  237.  
  238.     // Create our content property and preferred content property kind.
  239.  
  240.     if ( !storageUnit->Exists(ev, kODPropContents, kODNULL, 0) )
  241.         storageUnit->AddProperty(ev, kODPropContents);
  242.     if ( !storageUnit->Exists(ev, kODPropContents, kCreditNowDemoKind, 0) )
  243.         storageUnit->AddValue(ev, kCreditNowDemoKind);
  244.  
  245.     if ( !storageUnit->Exists(ev, kODPropPreferredKind, kODNULL, 0) )
  246.         storageUnit->AddProperty(ev, kODPropPreferredKind);
  247.     if ( !storageUnit->Exists(ev, kODPropPreferredKind, kODISOStr, 0) )
  248.     {
  249.         storageUnit->AddValue(ev, kODISOStr);
  250.         
  251.         // Since we are setting up the preferred kind property, we just write
  252.         // out our default "kind" for the editor. We can write out the user
  253.         // chosen kind in the PrivExternalizeStateInfo method.
  254.  
  255.         StorageUnitSetValue(storageUnit, ev, ODISOStrLength(kCreditNowDemoKind), kCreditNowDemoKind);
  256.     }
  257.     
  258.     // Add our display frame list.
  259.     if ( !storageUnit->Exists(ev, kODPropDisplayFrames, kODNULL, 0) )
  260.         storageUnit->AddProperty(ev, kODPropDisplayFrames);
  261.     if ( !storageUnit->Exists(ev, kODPropDisplayFrames, kODWeakStorageUnitRefs, 0) )
  262.         storageUnit->AddValue(ev, kODWeakStorageUnitRefs);
  263.  
  264.     // Parts by default have no name, though one is visible from 
  265.     // the part info dialog and is required during Drag&Drop to the
  266.     // Finder. We check for existence of the name property. If there
  267.     // is none, we add it; if there is, we focus to it.
  268.  
  269.     if ( !storageUnit->Exists(ev, kODPropName, kODMacIText, 0) )
  270.     {
  271.         storageUnit->AddProperty(ev, kODPropName)->AddValue(ev, kODMacIText);
  272.     
  273.         // Generate a default name for the part using the category user string.
  274.         ODNameSpaceManager* nsMgr = fSelf->GetStorageUnit(ev)->
  275.                     GetSession(ev)->GetNameSpaceManager(ev);
  276.                     
  277.         // Get the category string from the category name space.
  278.         ODIText* defaultName;
  279.         if ( PrivGetUserCatFromCat(nsMgr, kODCategorySampledSound, &defaultName) )
  280.         {
  281.             if ( storageUnit->Exists(ev, kODPropName, kODMacIText, 0) )
  282.             {
  283.                 ODSetITextProp(ev, storageUnit, kODPropName, kODMacIText, defaultName);
  284.             }
  285.         }
  286.                 
  287.         // Do not dispose defaultName!!!
  288.     }
  289. }
  290.  
  291. //----------------------------------------------------------------------------------------
  292.  
  293. void CPCreditNowDemo::PrivCleanContentProperty(Environment* ev, ODStorageUnit* storageUnit)
  294. {
  295.     // Description:    This method is called during exteralization of the
  296.     //                part. The part should remove any value in the content property
  297.     //                that it cannot "accurately" write to.
  298.     EnteringMethod("\pPrivCleanContentProperty");
  299.  
  300.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kODNULL, 0, kODPosAll);
  301.     
  302.     ODULong numValues = storageUnit->CountValues(ev);
  303.     
  304.     for (ODULong index = numValues; index > 1; index--)
  305.     {
  306.         // Index from 1 to n through the values.
  307.         storageUnit->Focus(ev, kODPropContents, kODPosUndefined, 
  308.                                 kODNULL, index, kODPosUndefined);
  309.         ODValueType value = storageUnit->GetType(ev);
  310.         
  311.         // If the value type is not one we support, remove it.
  312.         if ( ODISOStrCompare(value, kCreditNowDemoKind) != 0 )
  313.             storageUnit->Remove(ev);
  314.     }
  315. }
  316.  
  317. //----------------------------------------------------------------------------------------
  318.  
  319. void CPCreditNowDemo::PrivExternalizeStateInfo(Environment*        ev,
  320.                                        ODStorageUnit*    storageUnit,
  321.                                        ODDraftKey        key,
  322.                                        ODFrame*            scopeFrame)
  323. {
  324.     // Description:    This method is called during externalization of the
  325.     //                part. The current "state" of the editor should be
  326.     //                written out. This "state" information may be lost
  327.     //                during Data Interchange operations, so the part needs
  328.     //                to recover gracefully if information is missing or
  329.     //                incomplete.
  330.     
  331.     EnteringMethod("\pPrivExternalizeStateInfo");
  332.  
  333.     ODStorageUnitRef    weakRef;
  334.     ODFrame*            frame;
  335.     ODID                frameID;
  336.     ODID                scopeFrameID = ( scopeFrame ? scopeFrame->GetID(ev) : 0 );
  337.     ODDraft*            fromDraft = fSelf->GetStorageUnit(ev)->GetDraft(ev);
  338.     
  339.     // Externalize the part's display frame list.
  340.     storageUnit->Focus(ev, kODPropDisplayFrames, kODPosUndefined,
  341.                                 kODWeakStorageUnitRefs, 0, kODPosUndefined);
  342.     
  343.     // Persistent object references are stored in a side table, rather than
  344.     // in the property/value stream. Thus, deleting the contents of a value
  345.     // will not "delete" the references previously written to that value. To
  346.     // completely "delete" all references written to the value, we must
  347.     // remove the value and add it back.
  348.     storageUnit->Remove(ev);
  349.     storageUnit->AddValue(ev, kODWeakStorageUnitRefs);
  350.  
  351.     for (FrameLink* link = fDisplayFrames.First(); link->Frame(); link = link->Next() )
  352.     {
  353.         frame = (ODFrame*) link->Frame();
  354.         frameID = frame->GetID(ev);
  355.         
  356.         // If a draft key exists, then we are being cloned to another draft.
  357.         // We must "weak" clone our display frame and reference the cloned
  358.         // frame. The part re-uses the frameID variable so there aren't two
  359.         // different GetWeakStorageUnitRef calls.
  360.         if ( key )
  361.             frameID = fromDraft->WeakClone(ev, key, frameID, 0, scopeFrameID);
  362.         
  363.         // Write out weak references to each of the part's display frames.
  364.         storageUnit->GetWeakStorageUnitRef(ev, frameID, weakRef);
  365.         TRY
  366.             StorageUnitSetValue(storageUnit, ev, sizeof(ODStorageUnitRef), (ODPtr)&weakRef);
  367.         SOM_ENDTRY
  368.     }
  369. }
  370.  
  371. //----------------------------------------------------------------------------------------
  372.  
  373. void CPCreditNowDemo::PrivExternalizeContent(Environment* ev, ODStorageUnit* storageUnit)
  374. {
  375.     // Description:    This method is called during externalization of the
  376.     //                part. The content of the part should be written out.
  377.     EnteringMethod("\pPrivExternalizeContent");
  378.  
  379.     storageUnit->Focus(ev, kODPropContents, kODPosSame,
  380.                        kCreditNowDemoKind, 0, kODPosSame);
  381.         // Focus on content property.
  382.  
  383.     StorageUnitSetValue(fStorageUnit, ev, fTextData[0], &fTextData[1]);
  384.         // Now we write out the property.
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388.  
  389. void CPCreditNowDemo::PrivSetDirty(Environment* ev)
  390. {
  391.     fDirty = kODTrue;
  392.     fStorageUnit->GetDraft(ev)->SetChangedFromPrev(ev);
  393. }
  394.  
  395.  
  396.  
  397. //AET<<
  398. //----------------------------------------------------------------------------------------
  399. // CPCreditNowDemo::ExternalizeSelection
  400. //----------------------------------------------------------------------------------------
  401.  
  402. void CPCreditNowDemo::ExternalizeSelection(Environment* ev, ODStorageUnit* su, ODCloneKind cloneKind)
  403. {
  404. ODUnused(cloneKind);
  405.     EnteringMethod("\pExternalizeSelection");
  406.  
  407.     Ptr        data;
  408.     long    length;
  409.     Handle    text;
  410.     
  411.     if (fCurrentField->GetTextField()->HaveSelection())
  412.     {
  413.         su->Focus(ev, kODPropContents, kODPosSame, kCreditNowDemoKind, 0, kODPosSame);
  414.             // First we focus on the property we want to write out.
  415.         
  416.         text = fCurrentField->GetTextField()->GetSelectedPtr(&data, &length);
  417.         HLock(text);
  418.         StorageUnitSetValue(su, ev, length, data);
  419.             // Now we write out the property.
  420.         HUnlock(text);
  421.     }
  422. }
  423. //AET>>
  424.