Page 1 of 1

Intuition BOOPSI Menu support code

Posted: Thu Sep 10, 2015 8:47 pm
by mritter0
I updated my GadTools menu support code for the new Intuition BOOPSI Menus. Maybe someone will find this handy:

Code: Select all

VOID VARARGS68K
SAK_SetMenuAttrs(Object *MenuObj,...)
{
	va_list                         VarArgs;
	struct TagItem                  *TagList, *ThisTag;
	Object							*item=NULL;

	va_startlinear(VarArgs,MenuObj);
	TagList=(struct TagItem *)va_getlinearva(VarArgs,struct TagItem *);

	while((ThisTag=IUtility->NextTagItem(&TagList)))
	{
		switch(ThisTag->ti_Tag)
		{
			case MA_ID:
				item=(Object *)IIntuition->IDoMethod(MenuObj,MM_FINDID,0,(uint32)ThisTag->ti_Data);
				break;

			case MA_Label:
				if (item)
				{
					IIntuition->SetAttrs(item,
						MA_Label,							(STRPTR)ThisTag->ti_Data,
					TAG_END);
				}
				break;

			case MA_Disabled:
				if (item)
				{
					IIntuition->SetAttrs(item,
						MA_Disabled,						(BOOL)ThisTag->ti_Data,
					TAG_END);
				}
				break;

			case MA_Selected:
				if (item)
				{
					IIntuition->SetAttrs(item,
						MA_Selected,						(BOOL)ThisTag->ti_Data,
					TAG_END);
				}
				break;

			case MA_UserData:
				if (item)
				{
					IIntuition->SetAttrs(item,
						MA_UserData,						(uint32)ThisTag->ti_Data,
					TAG_END);
				}
				break;
		}
	}

	va_end(VarArgs);
}
This will allow you to set the flags for one or several menu items at once:

Code: Select all

	SAK_SetMenuAttrs(MenuStripObj,
		MA_ID,								MEN_PANELS_BOOKMARKS,
			MA_Label,							"New label text",
			MA_Disabled,						TRUE,
			MA_Selected,						TRUE,
			MA_UserData,						5000,

		MA_ID,								MEN_PANELS_NAVIGATION,
			MA_Label,							"New label text again",
			MA_Disabled,						TRUE,
			MA_Selected,						TRUE,
			MA_UserData,						5001,
	TAG_DONE);
Share any thoughts and/or improvements.

Re: Intuition BOOPSI Menu support code

Posted: Thu Sep 10, 2015 9:35 pm
by mritter0
Add and remove items:

Code: Select all

BOOL
SAK_AddMenuItem(Object *MenuObj,uint32 Parent,Object *AddItem)
{
	Object				*item;

	if ((item=(Object *)IIntuition->IDoMethod(MenuObj,MM_FINDID,0,Parent)))
	{
		IIntuition->SetAttrs(item,
			MA_AddChild,						AddItem,
		TAG_END);

		return(TRUE);
	}

	return(FALSE);
}


VOID
SAK_RemoveMenuItem(Object *MenuObj,uint32 ID)
{
	Object				*item;

	if ((item=(Object *)IIntuition->IDoMethod(MenuObj,MM_FINDID,0,ID)))
	{
		IIntuition->SetAttrs(MenuObj,
			MA_RemoveChild,						item,
		TAG_END);
		IIntuition->DisposeObject(item);
	}
}
Use like this:

Code: Select all

Object *AddItem;
AddItem=MItem("New item added"), MA_ID, 6000, MEnd;
SAK_AddMenuItem(MenuStripObj,MEN_GO_MENU,AddItem);


SAK_RemoveMenuItem(MenuStripObj,MEN_ABOUT);

Re: Intuition BOOPSI Menu support code

Posted: Thu Sep 10, 2015 10:01 pm
by trixie
@mritter0
I updated my GadTools menu support code for the new Intuition BOOPSI Menus.
So where's the GadTools part? :-)

Re: Intuition BOOPSI Menu support code

Posted: Thu Sep 10, 2015 10:53 pm
by mritter0
Maybe "replaced my GadTools code" with code for the new menus is better wording. I still have it if you want it.

Re: Intuition BOOPSI Menu support code

Posted: Fri Sep 11, 2015 7:29 am
by trixie
@mritter0

Sorry I was just being cheeky :-) I'm glad we no longer need GadTools now that the Menu Class is in place.

Re: Intuition BOOPSI Menu support code

Posted: Fri Sep 11, 2015 8:47 am
by thomasrapp
mritter0 wrote:

Code: Select all

	va_startlinear(VarArgs,MenuObj);
	TagList=(struct TagItem *)va_getlinearva(VarArgs,(struct TagItem *));
	va_end(VarArgs);

	while((ThisTag=IUtility->NextTagItem(&TagList)))
	{
I am not sure if this is legal usage of linear var args. I would put the va_end at the end of the function, otherwise the tag list could be destroyed while you use it.

Re: Intuition BOOPSI Menu support code

Posted: Fri Sep 11, 2015 9:02 am
by salass00
As thomasrapp says I think the va_end() should be at the end of the function to be correct as now you are using the varargs still after calling va_end().

Also the FoundID variable seems completely unnecessary. Just initialize the item variable to NULL on function start and check if it's non-NULL in the same way you check FoundID now.