Solved

Menu Resource

Posted on 1998-12-20
7
218 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

774 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