Solved

Menu Resource

Posted on 1998-12-20
7
229 Views
Last Modified: 2013-12-26
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?
0
Comment
Question by:dwp090598
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:boonstra
ID: 1291653
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");
 

0
 

Expert Comment

by:paulsat
ID: 1291654
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.
0
 

Author Comment

by:dwp090598
ID: 1291655
After adding the above code I am getting illegal token error - never seen this before
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:boonstra
ID: 1291656
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.  
0
 

Author Comment

by:dwp090598
ID: 1291657
the error was a compile error.
0
 
LVL 3

Accepted Solution

by:
boonstra earned 50 total points
ID: 1291658
dwp reported via email that the sample code dropped him into MacsBug with a hard crash in NQDEmptyRgn that I suspect (from looking at his StdLog) might be related to the Mercutio GDevice unlocking problem, but I'm not certain.  I'm unable to duplicate his crash.  In any case, he reports that: "I did resolve the problem by reloading the menu and inserting the new menu items each time I close any window containing the control. "

So, his problem is fixed.  I still think the code I posted works.  We'll see what kind of grade he gives my answer ....  :-)
0
 

Author Comment

by:dwp090598
ID: 1291659
The code does work and I accept boonstra's answer.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 139
sumNumber challenge 16 140
EvenOdd challenge 10 147
How do I get Window Title of all opened process? 4 39
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question