On 07/17/12 05:54, M3taSpl0it wrote:
> Good day, I've some confusions related to Fl_Menu_Bar and Fl_Menu_Items. I
> apologize for multiple question in one but they are trivial so I though to
> create only one question.
>
> Question 1) [Using a static menu items array, how do I initialize it in a
> class]
> http://ideone.com/5dL7d
>
> Now suppose I create some GUI class like :
>
> class GUI : public Fl_Window
> {
> Fl_Menu_Items menuItems[10];
> .....
> }
>
> Since array is part of class I can't initialize that array at declaration. so
> what's the best way to add items in that array?
> e.g I don't want to do something like
>
> menuItems[0].callback_ = somefunc; or menuItems[0].callback(somefunc);
> menuItems[0].label("smack that!");
> ....
> ....
>
> What's the idiom to do this?
Regarding #1, myself I always use add() and don't use initialized
arrays.
However, if you want to go the initialized array route, then make the
array
a static global. I believe you can assign the static array to a
menu/menubar
and then make changes to it; internally a copy is made of the array, so
you
can adjust it without affecting the static array's contents. (This way
you can
have several instances of the class with each menu instance using a
'copy' of
the static global array that can be changed on a per-instance basis)
You can use functions like find_item() to avoid having to hard-code
array indexes. See for instance:
http://seriss.com/people/erco/fltk/#Menu_ChangeLabel
In that example I use add(), but the menu I think just as easily have
been initialized from a static array, and then adjusted the same way
after the fact.
> Question 2) To resolve above problem , I head of function Fl_Menu_Bar::add
> function, which adds menu items I guess?
That's what I prefer to use almost always.
> but that leads me to another problem :
>
> menubar = new MenuBar(0, 0, this->w(), 30); //MenuBar is typedef of
> Fl_Menu_Bar
> menubar->add("File", FL_CTRL+'F',nullptr,this,FL_SUBMENU);
> menubar->add("Hola",...);
>
> Now how do I signal fltk that last call to add item is to be shown in "File"
> submenu (i.e "Hola" submenu of "File" submenu)
>
> menubar->add("File/Hola",...); // this is good way to do it?
I would even skip the first add() there, and just use:
menubar->add("File/Hola",...); // this is good way to do it?
This will create the File menu if it doesn't exist, and adds Hola to it.
So for instance:
menubar = new MenuBar(0, 0, this->w(), 30);
menubar->add("File/Open",...);
menubar->add("File/Save",...);
menubar->add("File/Quit",...);
..is a valid way to create a File menu with three items in it.
BTW, you don't need the FL_CTRL+'F' to open the file menu;
If you want the user to be able to open the File menu by hitting Alt-F
(which I think is the default), put an & in front of the 'F' in File,
and it will show up with an underline in front of it, giving the user
the visual hint the menu can be opened from the keyboard. Quoting the
docs for Fl_Menu_Bar:
"Submenus will also pop up in response to shortcuts indicated by putting a '&'
character
in the name field of the menu item. If you put a '&' character in a top-level
"button"
then the shortcut picks it. The '&' character in submenus is ignored until the
menu is popped up. "
So eg:
menubar->add("&File/Open",...);
menubar->add("&File/Save",...);
menubar->add("&File/Quit",...);
> Question 3) How to signal fltk that submenu has ended, now another menu
> begins?
> i.e analogues to {0} in Fl_Menu_Items's array or I don't need to do that when
> I'm using Fl_Menu_Items::add function?
If using add(), you don't need to; each menu is terminated
automatically.
So for instance:
menubar = new MenuBar(0, 0, this->w(), 30);
menubar->add("File/Open",...);
menubar->add("File/Save",...);
menubar->add("File/Quit",...);
menubar->add("Edit/Copy",...);
menubar->add("Edit/Paste",...);
..is the correct way.
> Question 4) In the documentation Fl_Menu_Bar don't have any add member
> function , In actually it has when try to access it from object of it.
Fl_Menu_Bar derives from Fl_Menu_; the docs are there.
The docs for Fl_Menu_Bar just documents the methods it adds to the base
class.
So to fully understand all the methods available to you, you have to
explore
the base classes as well. Often this can go down several levels, with
usually
Fl_Widget being at the bottom.
> so I assume Fl_Menu_Bar is derived from Fl_Menu_Items?
In the docs for Fl_Menu_Bar, you'll see a widget hierarchy tree at the
top
showing what inherits from what:
http://fltk.org/doc-1.3/classFl__Menu__Bar.html
ie.
Fl_Widget
/|\
|
Fl_Menu_
/|\
|
Fl_Menu_Bar
..which shows Fl_Menu_Bar derives from Fl_Menu_, and Fl_Menu_ derives
from Fl_Widget.
So all of the public methods in those three widgets are available to
the user of
Fl_Menu_Bar.
I don't think it's correct to say Fl_Menu_Bar 'derives' from
Fl_Menu_Item
so much as it just uses it as a data type internally. So in other words,
the methods Fl_Menu_Item provide aren't grafted on to the Fl_Menu_Bar
API;
you have to use methods like find_item() or menu() to access the items,
and then when you have a pointer to an item, you can refer to its
methods.
> and so the following code is ok :
>
> Fl_Menu_Bar *menu = new Fl_Menu_Bar(...);
> Fl_Menu_Item *p;
> p = (Fl_Menu_Item*)menu->find_item(...);
Yes, that code is OK.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk