Path: utzoo!attcan!uunet!husc6!endor!stew From: stew@endor.harvard.edu (Stew Rubenstein) Newsgroups: comp.sys.mac.programmer Subject: Re: Changing a menu using a resource Keywords: str255 Message-ID: <4602@husc6.harvard.edu> Date: 17 May 88 13:54:26 GMT References: <346@manta.NOSC.MIL> Sender: news@husc6.harvard.edu Reply-To: stew@endor.UUCP (Stew Rubenstein) Organization: Aiken Computation Lab Harvard, Cambridge, MA Lines: 56 In article <346@manta.NOSC.MIL> lulue@manta.NOSC.MIL (Dan Lulue ) writes: >I want to be toggle a menu item from "Turn Herman On" to "Turn Herman >Of", and back. [omitted] >Is this correct so far? Yup, perfect. >Now GetResource returns a handle to the requested resource. My question >is how to map that handle to the strings stored in the resource? Once I >have a handle, what do I pass to SetItem?? Here is my GetResource call: > > Handle theHandle; > theHandle = GetResource ('STR ', OFFID); A Handle is just a pointer to a pointer. SetItem takes a StringPtr, so if you have a menu called Albert with items Herman, George, etc., you typically do something like: #define MENU_albert 1 MenuHandle albertMenu; enum { /* Items in the Albert menu */ albertHerman = 1, albertGeorge, albertDashedLine1, /* Don't forget placeholders */ albertFred, ... }; and then to set the item: theHandle = GetResource ('STR ', OFFID); HLock (theHandle) SetItem (albertMenu, albertHerman, (StringPtr) *theHandle); HPurge (theHandle); One other thing to worry about, depending on what development system you're using - SetItem may expect a C string (null-terminated) and GetResource returns a handle to a Pascal string (count byte prefixed). LightSpeed C does it right - SetItem expects a P string so the code above is correct. The standard MPW interfaces do this silly conversion, so you have to PtoCstr(*theHandle); before and CtoPstr(*theHandle) after the SetItem. >A more generic question is "How does one figure out how to do something >the proper way when IM does not tell you how to do it the proper way? This is the kind of thing you learn by examining sample code. Also, I can highly recommend Knaster's books (How to Write Macintosh Programs and Macintosh Programming Secrets, I think they're called). Stew Rubenstein Cambridge Scientific Computing, Inc. UUCPnet: seismo!harvard!rubenstein CompuServe: 76525,421 Internet: rubenstein@harvard.harvard.edu MCIMail: CSC