Solved

Menu Resource

Posted on 1998-12-20
7
206 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now