[comp.sys.mac.programmer] followup: Add1ResMenu

engber@ils.nwu.edu (Mike Engber) (04/23/91)

Thanks for all the responses. The trick to avoiding unnecessary loading
of PICTs was to temporarily SetResLoad(false). Someone also pointed out
the AddResMenu alphabetizes. Someone else pointed out I should intially
add the menu item with a dummy title and then set it's title to the
resource name in order to avoid having funny characters in the resource
name being interpreted with special meaning.

So here is the result. Two files, MenuUtils.h & MenuUtils.c. They
provide Add1ResMenu & Insert1ResMenu. I wrote and tested them in THINK
C, but I don't think anything makes them THINK C specific (maybe a
missing #include ?) Feel free to use them as you wish. Send me any
bugs,improvements,suggestions.

-ME

-cut-here----------------------------------------------------------------------

/*
 * MenuUtils.h
 */

#ifndef H_MenuUtils
#define H_MenuUtils

/* Add1ResMenu and Insert1ResMenu are designed to behave the same as
 * AddResMenu and InsertResMenu except that only the current resource
 * file is searched.
 */
extern pascal void Add1ResMenu(MenuHandle theMenu, ResType theType);
extern pascal void Insert1ResMenu(MenuHandle theMenu, ResType theType, short afterItem);


#endif


-cut-here----------------------------------------------------------------------


/*
 * MenuUtils.c
 */
 

#include "MenuUtils.h"

pascal void Insert1ResMenu(MenuHandle theMenu, ResType theType, short afterItem){
	short	i;
	short	beforeItem;
	short	resCount = Count1Resources(theType);

	i = CountMItems(theMenu);
	if(afterItem>i || afterItem<0) afterItem = i;
	beforeItem = afterItem + 1;
	
	SetResLoad(false);
		for(i=1; i<=resCount; ++i){
		short	resId;
		ResType	resType;
		Str255	resName;
		Handle	resHandle;
		
		resHandle = Get1IndResource(theType,i);
		if(resHandle){
			GetResInfo(resHandle,&resId,&resType,resName);
			if(resName[0]>0 && resName[1]!='.' && resName[1]!='%'){
				short	theItem = afterItem + 1;
				
				while(theItem < beforeItem){
					Str255	itemText;
					GetItem(theMenu,theItem+1,itemText);
					if(RelString(resName,itemText,false,true) == sortsBefore) break;
					++theItem;
				}
				++beforeItem;
				InsMenuItem(theMenu,"\p ",theItem - 1);
				SetItem(theMenu,theItem,resName);
			}
		}
	}
	SetResLoad(true);
} 


pascal void Add1ResMenu(MenuHandle theMenu, ResType theType){
	Insert1ResMenu(theMenu,theType,CountMItems(theMenu));
}