Murga-Projects Forums

Full Version: callback for an individual menu entry
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
While fltk has the ability to define a callback function for every menu entry, I cannot get it to work under murgaLua. Is there a working example somewhere or is this a bug^H^H^H feature?
I haven't come to fully understand the menu classes, and haven't succeeded in setting a callback to a particular menu item. However, you can check for the menu item within the menu's callback and perform an action according to the result:

Code:
my_menu=fltk:Fl_Menu_Bar(0,0,200,30)
my_menu:add("File/Item 1")
my_menu:add("File/Item 2")
my_menu:add("File/Item 3")

my_menu:callback( -- a callback for the whole menu
function()
  if my_menu:text()=="Item 2" then
    print("this is Item 2")
  end
end
)

Defining a callback for a menu-item isn't implemented - Won't work currently.

mikshaw's method works, however sometimes you want menu items with the same name ...

Take a look at "sqlTool.lua" for a solution to this problem along with a few other examples of menu usage.

Cheers
JohnM

Quote:
sometimes you want menu items with the same name ...
Take a look at "sqlTool.lua" for a solution to this problem

It seems that you are using the menu item index. This works for duplicate labels but can be nearly unusable if you have dynamically created menus, or complicated if you decide to change the order of your menu items. I think either method has its benefits and drawbacks.

I was looking into something that uses the find_item() function to allow this pointer to be used, but unfortunately my_pointer:label() returns the same string as my_menu:text(), meaning that neither one uses a full pathname to the item. maybe item_pathname() could be used, but so far it hasn't shown itself to provide very helpful information.

JohnMurga Wrote:
Defining a callback for a menu-item isn't implemented - Won't work currently.


Of course this is just a matter of taste, but I think in some cases a individual callback is preferable to a big block of if, switching through all menu items in one single callback.

JohnMurga Wrote:
mikshaw's method works, however sometimes you want menu items with the same name ...

Take a look at "sqlTool.lua" for a solution to this problem along with a few other examples of menu usage.

Cheers
JohnM


Shouldn't "item_pathname" take care of this case, when you have multiple submenus with the same item names? Unfortunately it looks like item_pathname also does (currently) not work.

Juergen

From my little bit of poking at it, it looks like item_pathname is partially implemented. It will return 0 or -2 depending on how it's used (I haven't seen it return -1), but I couldn't get it to return a string.
item_pathname and menu item callbacks are sort of on my to-do list ...

But WAY, WAY down ... :-(

What's more I haven't had much time to do the stuff I do have planned for the 0.6 release.

Cheers
JohnM
Another Good Thing about development of personal projects: deadlines mean only as much as you want them to =o)

It seems as though there are many separate pieces involved in putting together complete menu support. Considering that and the fact that menus are already in a mostly useable state, I'm not surprised these are low priorities.
Reference URL's