Link to home
Start Free TrialLog in
Avatar of dwp090598
dwp090598

asked on

Menu Resource

I creating multiple windows each containing the same System 7 pop-up menu control, I append several menu items to the menu  when creating window. If I Dispose or Close the window the appended menu items disappear in all of the windows. Apparently when I DisposeWindow or CloseWindow (tried both) a window it disposes the menu? How can I prevents this?
Avatar of boonstra
boonstra

You're running into the fact that DisposeWindow and CloseWindow automatically remove all controls associated with the window.  Since you've added the same control to multiple windows, closing one gets rid of the modified popup for all windows.

The solution is to clone the popup for each window.  The following code is skanky, but it works:

static ControlHandle DupPopUpControl(short controlID,
    short newControlID, short newMenuID, WindowPtr wind) {
  ControlHandle controlHdl;
  Handle hdl;
  MenuHandle mHandle;
  short mID;
 
  /* get control resource */
  hdl = GetResource('CNTL',controlID);
 
  /* extract menu ID from control resource */
  mID = *(short *)(*hdl+14);

  /* copy MENU resource */
  mHandle = (MenuHandle)GetResource('MENU',mID);
  (**mHandle).menuID = newMenuID;
  DetachResource((Handle)mHandle);
  AddResource((Handle)mHandle,'MENU',newMenuID,"\p");
 
  /* copy CNTL resource with new MENU ID */
  DetachResource((Handle)hdl);
  *(short *)(*hdl+14) = newMenuID;
  AddResource((Handle)hdl,'CNTL',newControlID,"\p");
 
  /* finally, attached the cloned control to the window */
  controlHdl = GetNewControl(newControlID,wind);
  return controlHdl;
}

/* assume CNTL 128 is the popup control you want to clone */
/* 301, 3001, 302, 3002 are arbitrary, but they must be different for each window and unique from other CNTL and MENU items in your resource file */
  controlHdl = DupPopUpControl(128,301,3001,gWindowPtr[0]);
  mHandle = (**((PopupPrivateData **)((**controlHdl).contrlData))).mHandle;
  AppendMenu(mHandle,"\pAdded Item 1");
 
  controlHdl = DupPopUpControl(128,302,3002,gWindowPtr[1]);
  mHandle = (**((PopupPrivateData **)((**controlHdl).contrlData))).mHandle;
  AppendMenu(mHandle,"\pAdded Item 2");
 

I wonder what would happen if you set the ControlList member of the window record to zero just prior to disposing of it.  I would think it would not be able to dispose the controls on the window.  But then you would have a bunch of control handle memory leaks.  If you do this, you would need to copy the control handles somewhere and dispose of them yourself at some point.

I think boonstra has the best solution and the safest.
Avatar of dwp090598

ASKER

After adding the above code I am getting illegal token error - never seen this before
Not sure what you mean by this - is it a compile error or an execution error?  By private email I've sent the test code I used to generate my answer.  
the error was a compile error.
ASKER CERTIFIED SOLUTION
Avatar of boonstra
boonstra

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The code does work and I accept boonstra's answer.