Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Cyberdog Programmer's Kit / Part 2 - Programming in Cyberdog
Chapter 3 - Adding Cyberdog Features to an OpenDoc Part


Initializing and Releasing Cyberdog Objects

Your part must take several actions when it is created or destroyed to support Cyberdog and its menus. When your part is created, it must call the InitCyberdog function to initialize Cyberdog. It should also set up Cyberdog's menus. Before your part is released or destroyed, it must delete the Cyberdog service menu object and release the Cyberdog item, if it exists.

In the button part, the initialization is performed by the InitPart method, shown in Listing 3-5 (page 97), and the InitPartFromStorage method (not shown). These methods call the Initialize method (Listing 3-2) to actually perform the required initialization. The Initialize method obtains the OpenDoc session and calls the InitCyberdog function to initialize Cyberdog. It calls the CreateMenus method to create the menus; for more information, see "Displaying Cyberdog Menus" (page 105).

Note
In this example, a Cyberdog item is created initially by the InitPart method. For information about creating Cyberdog items, see "Creating a Cyberdog Item From a URL" (page 97). u
Listing 3-2 Initializing the button part

void CybTxtBtn::Initialize( Environment* ev, ODStorageUnit* storageUnit)
{
   ...
   // Initialize Cyberdog.
   ODSession* session = ODGetSession(ev,fSelf);
   OSErr err = InitCyberdog(ev, session);
   ...
   CreateMenus(ev);
   ...}
When a part is destroyed, its ReleaseAll method is called. This method is a good place to delete the Cyberdog item and the Cyberdog service menu object and release the menu bar with which the menu object is associated. Listing 3-3 shows how the button part handles these actions.

Listing 3-3 The button part's ReleaseAll method

void CybTxtBtn::ReleaseAll( Environment* ev )
{
   TRY
      if (fCyberServiceMenu != kODNULL)
      {
         delete fCyberServiceMenu;
         fCyberServiceMenu = kODNULL;
      }
      if (fMenuBar != kODNULL)
      {
         fMenuBar->Release(ev);
         fMenuBar = kODNULL;
      }
      if (fCyberItem != kODNULL)
      {
         fCyberItem->Release(ev);
         fCyberItem = kODNULL;
      }
      ...
   CATCH_ALL
      RERAISE; 
   ENDTRY
}
The destructor for your part class should release any objects that the part still references. You should release the Cyberdog item. Releasing the item decrements its reference count and causes it to be deleted. Listing 3-4 shows the destructor for the CybTxtBtn part class.

Listing 3-4 The CybTxtBtn destructor

CybTxtBtn::~CybTxtBtn()
{
   Environment* ev = somGetGlobalEnvironment();
   if (fCyberItem != kODNULL)
   {
      TRY
         fCyberItem->Release(ev);
      CATCH_ALL
      ENDTRY
      
      fCyberItem = kODNULL;
   }
}

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996