Page 1 of 1

PopupMenu hook (how to get PMIA_Title string)

Posted: Wed Oct 30, 2013 11:52 pm
by javierdlr
Hi, using popupmenu hook I'm too dumb (99% possibilities) to get the PMIA_Title string I have of my menus.
The function 'IIntuition->GetAttrs(item, PMIA_ID,&item_ID, PMIA_Title,&item_Title, TAG_END)' should save the menu ID on item_ID (OK) and menu Title on item_Title (I always get and empty string).

Code: Select all

Object *chooseMenu;
struct Hook MenuHandler;

uint32 MenuHandlerFunc(struct Hook *hook, Object *item, APTR reserved);


void OpenDockMenu(struct DockyData *dd)
{
 MenuHandler.h_Entry = (HOOKFUNC)MenuHandlerFunc;

 chooseMenu = IIntuition->NewObject(NULL, "popupmenu.class",
                                    PMA_MenuHandler,&MenuHandler, TAG_DONE);

 Object *item1 = IIntuition->NewObject(NULL, "popupmenuitem.class",
                              PMIA_Title, dd->keymap1Name,  // char [64] to "usa1","gb","e_ISO-8859-15",... (devs:keymaps)
                              PMIA_ID,    MENU_KM1, // 0
                              PMIA_Icon, OBJ(GID_BITMAP1) = IIntuition->NewObject(NULL, "bitmap.image",
                                   BITMAP_SourceFile, keymap1Flag,
                                   BITMAP_Screen,     dd->scr,
                                   BITMAP_Masking,    TRUE,
                                  TAG_DONE),
                             TAG_END);

 Object *item2 = IIntuition->NewObject(NULL, "popupmenuitem.class",
                              PMIA_Title, dd->keymap2Name,  // same as dd->keymap1Name
                              PMIA_ID,    MENU_KM2, // 1
                              PMIA_Disabled, TRUE,
                              PMIA_Icon, OBJ(GID_BITMAP2) = IIntuition->NewObject(NULL, "bitmap.image",
                                   BITMAP_SourceFile, keymap2Flag,
                                   BITMAP_Screen,     dd->scr,
                                   BITMAP_Masking,    TRUE,
                                  TAG_DONE),
                             TAG_END);

 // language2 valid/available
 if( OBJ(GID_BITMAP2) ) IIntuition->SetAttrs(item2, PMIA_Disabled,FALSE, TAG_DONE);

 IIntuition->IDoMethod(chooseMenu, OM_ADDMEMBER, item1); // lang1
 IIntuition->IDoMethod(chooseMenu, OM_ADDMEMBER, item2); // lang2

 IIntuition->IDoMethod(chooseMenu, PM_OPEN, dd->win);

 IIntuition->DisposeObject(chooseMenu);
}


uint32 MenuHandlerFunc(struct Hook *hook, Object *item, APTR reserved)
{
 uint32 item_ID = 0;
 STRPTR item_Title = NULL;

 if( IIntuition->GetAttrs(item, PMIA_ID, &item_ID,
                                PMIA_Title, &item_Title,
                         TAG_END) )
 {
IDOS->Printf("keymap%ldName: ",item_ID+1);
  switch(item_ID)
  {
   case MENU_KM1:
    IDOS->Printf("'%s'\n",item_Title);
   break;
   case MENU_KM2:
    IDOS->Printf("'%s'\n",item_Title);
   break;
  }
 }
 return(0);
}
TIA

Re: PopupMenu hook (how to get PMIA_Title string)

Posted: Thu Oct 31, 2013 12:07 am
by javierdlr
Solved (hope so) by adding to the hook function:
'MenuHandler.h_Data = (struct DockyData *)dd;' into 'OpenDockMenu()' function

'struct DockyData *DD = (struct DockyData *)hook->h_Data;' into 'MenuHandlerFunc()' function.

so now I have, and get keymap name so it can be changed:
..
switch(item_ID)
{
case MENU_KM1:
IDOS->Printf("'%s'\n",DD->keymap1Name);
break;
case MENU_KM2:
IDOS->Printf("'%s'\n",DD->keymap2Name);
break;
}
..

BTW is it "safer"/better to change the hook code to 'IExec->AllocSysObjectTags(ASOT_HOOK,..'?

Re: PopupMenu hook (how to get PMIA_Title string)

Posted: Thu Oct 31, 2013 9:02 am
by kas1e
@Javier
BTW is it "safer"/better to change the hook code to 'IExec->AllocSysObjectTags(ASOT_HOOK,..'?
As far as i aware, there is 2 ways to use hooks in os4 now : static way and dinamic way. Static one its when you do something like:
struct Hook h;

h.h_Entry = MyFunction;
h.h_SubEntry = NULL;
h.h_Data = data;
And dinamic one, is when you do something like:
struct Hook *h = AllocSysObjectTags(ASOT_HOOK, ASOHOOK_Entry, MyFunction,TAG_END);

Usage of dynamic (i.e. when you use AllocSysObjectTags() / ASOT_HOOK ) is better because:

1. You don't have to take assumptions about the size and layout of a hook
2. Because of 1, it is easy to enhance hooks in the future, without breaking old software.

Re: PopupMenu hook (how to get PMIA_Title string)

Posted: Thu Oct 31, 2013 10:39 am
by trixie
@javierdlr
is it "safer"/ better to change the hook code to 'IExec->AllocSysObjectTags(ASOT_HOOK,..'?
Absolutely!!! If you want to keep your stuff future-proof, always use AllocSysObject() for all supported types of Exec objects (hooks, lists, message ports etc.)

Re: PopupMenu hook (how to get PMIA_Title string)

Posted: Sat Nov 02, 2013 1:40 am
by javierdlr
trixie wrote:@javierdlr
is it "safer"/ better to change the hook code to 'IExec->AllocSysObjectTags(ASOT_HOOK,..'?
Absolutely!!! If you want to keep your stuff future-proof, always use AllocSysObject() for all supported types of Exec objects (hooks, lists, message ports etc.)
OK,changed now to use 'AllocSysObjectTags(ASOT_HOOK..'. THX