[comp.sys.mac.programmer] Problems With Menus

borcelf@jacobs.cs.orst.edu (Fernando Borcel) (01/29/89)

Hi.  I'm new with LSC, and I just came across my first problem.  I'm getting
my windows and menus from resources I added to the project, which seems to 
work fine.  I didn't have any problems with windows or other stuff, but wasn't
so lucky with menus.  When I call the function below, I just get a system
error ID=25.  I made sure all the numbers I used were consistent both in my
resource file and my C code, so, What am I doing wrong?
Here is the code (which I translated from LightSpeed Pascal):

#include <menumgr.h>
enum
{   AppleID=128,
   FileID,
   EditID
};

enum
{   AppleM=1,
   FileM,
   EditM
};

#define      NumOfMenus   3
MenuHandle   MyMenus[NumOfMenus]

void InitMyMenus()
{
   int   i;
   
   MyMenus[AppleM] = GetMenu(AppleID);
   AddResMenu(MyMenus[AppleM], 'DRVR');
   MyMenus[FileM] = GetMenu(FileID);
   MyMenus[EditM] = GetMenu(EditID);
   
   for (i=1; i>NumOfMenus;i++)
      InsertMenu(MyMenus[i],0);
   
   DisableItem(MyMenus[EditM], UndoItem);
   DrawMenuBar();
}
 ------
Thanks for your help!

		-Fernando Borcel
"Tact is the ability to tell a man he has an open mind when he has a
hole in his head."  Internet:     borcelf@jacobs.cs.ORST.EDU
                    UUCP:{tektronix,hp-pcd}!orstcs!jacobs.cs.orst.edu!borcelf

tim@hoptoad.uucp (Tim Maroney) (01/29/89)

In article <8534@orstcs.CS.ORST.EDU> borcelf@jacobs.cs.orst.edu (Fernando
Borcel) writes:
>   
>   for (i=1; i>NumOfMenus;i++)
>      InsertMenu(MyMenus[i],0);

I wish they were all this easy!  Use a less than sign, not a greater than
sign.  The loop is quitting right away, so the menu doesn't get installed;
then when you try to operate on it, there's a glitch because it's not in
the menu bar....
-- 
Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim
"Religion flourishes in greater purity without than with the aid
 of government." -- James Madison

oster@dewey.soe.berkeley.edu (David Phillip Oster) (01/30/89)

I wish people would stop reading and copying all those stupid example
programs that allocate an array, define one set of menu numbers for
resources, a second set for indices into the array, and have a loop that
gets the menu and inserts the menu. It is miles more maintable to just do:

	SetMenuBar(GetNewMBar(128));
	DrawMenuBar();

and when the program needs to reference a menu handle, do:
	GetMHandle(APPLEMENU);

Also, you might as well number your menus consequatively starting from 1.

GetNMBar(), in case you missed it reading inside Mac, takes an MBAR
resource that is a list of menu ids, and it builds a menu bar of those
menus in that order. In Rmaker, I usually just say:

Type MBAR=GNRL
  ,128 (32)
.I
6 1 2 3 4 5 6

;; the first integer is the count.


--- David Phillip Oster            --"When we replace the mouse with a pen,
Arpa: oster@dewey.soe.berkeley.edu --3 button mouse fans will need saxophone
Uucp: {uwvax,decvax}!ucbvax!oster%dewey.soe.berkeley.edu --lessons." - Gasee

pratt@boulder.Colorado.EDU (Jonathan Pratt) (01/30/89)

In article <6400@hoptoad.uucp> tim@hoptoad.UUCP (Tim Maroney) writes:
>In article <8534@orstcs.CS.ORST.EDU> borcelf@jacobs.cs.orst.edu (Fernando
>Borcel) writes:
>>   
>>   for (i=1; i>NumOfMenus;i++)
>>      InsertMenu(MyMenus[i],0);
>
>I wish they were all this easy!  Use a less than sign, not a greater than
>sign.  The loop is quitting right away, so the menu doesn't get installed;

The summary line says it all; you must also change the intialization of
the "for" to i=0.  (And fix the enum, etc. to reflect this)

Jonathan
pratt@boulder.colorado.edu

beard@ux1.lbl.gov (Patrick C Beard) (01/30/89)

In article <8534@orstcs.CS.ORST.EDU> borcelf@jacobs.cs.orst.edu (Fernando Borcel) writes:
>   for (i=1; i>NumOfMenus;i++)
>      InsertMenu(MyMenus[i],0);

The limits of your for loop are screwed up.  Should be i<NumOfMenus.

Patrick Beard
Berkeley Systems

lsr@Apple.COM (Larry Rosenstein) (01/31/89)

In article <27812@ucbvax.BERKELEY.EDU> oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) writes:
>I wish people would stop reading and copying all those stupid example
>programs that allocate an array, define one set of menu numbers for

Agreed.

>gets the menu and inserts the menu. It is miles more maintable to just do:
>
>	SetMenuBar(GetNewMBar(128));
>	DrawMenuBar();

That's what MacApp does.  We even take this a step further and have up to 3
MBAR resources: (1) menus that are loaded and displayed, (2) menus that are
loaded but not displayed right away, and (3) menus that are loaded and added
to the hierarchical section of the menu bar.

>and when the program needs to reference a menu handle, do:
>	GetMHandle(APPLEMENU);

This works as long as that menu is in the menu bar.  GetMHandle searches
through the menus in the menu bar only (which makes it more efficient that
using the Resource Manager).  If you don't know for sure that the menu is in
the menu bar, then you can call the Resource Manager.

(Don't call GetMenu, because that assumes that the menu is in its original
form.  GetMenu replaces the menu procID with a handle to the defproc.
Therefore, GetMenu can't be called more than once with the same menu, unless
you take special steps.)

leonardr@uxe.cso.uiuc.edu (02/01/89)

lsr@Apple.COM(Larry Rosenstein) writes in comp.sys.mac.programmer

>>and when the program needs to reference a menu handle, do:
>>	GetMHandle(APPLEMENU);
>
>This works as long as that menu is in the menu bar.  GetMHandle searches
>through the menus in the menu bar only (which makes it more efficient that
>using the Resource Manager).  If you don't know for sure that the menu is in
>the menu bar, then you can call the Resource Manager.
>
	I believe that what you meant to say Larry, was that it searches the menus
in the MENU LIST not the Menu Bar.  The difference being that PopUps and HMenus
are installed into the MenuList using InsertMenu(theMenu, -1) so that they are
not put into the menu bar itself, but put into the MenuList, where GetMHandle
can find them as well.

+---------------------------------+-----------------------------------+
+   Leonard Rosenthol             +  fact, then again you might decide+
+   President, LazerWare, inc.    +  that it really isn't, so you     +
+                                 +  never know, do you??             +
+   leonardr@uxe.cso.uiuc.edu     +                                   +
+   GEnie:  MACgician             +  MacNET: MACgician                +
+   Delphi: MACgician             +  AppleLink: D0025                 +
+---------------------------------+-----------------------------------+