2 // "$Id: menubar-add.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
4 // An example of using Fl_Menu_Bar's add() to dynamically create menubars
6 // Menu bars can be created several ways. Using add() allows
7 // dynamically creating a menubar using a 'pathname' syntax.
8 // Use if you're creating items dynamically, or if you're making
9 // menubars by hand (as opposed to using fluid), as it's easier
12 // In this case we're using one callback for all items,
13 // but you can make unique callbacks for each item if needed.
15 // Copyright 2010 Greg Ercolano.
16 // Copyright 1998-2010 by Bill Spitzak and others.
18 // This library is free software; you can redistribute it and/or
19 // modify it under the terms of the GNU Library General Public
20 // License as published by the Free Software Foundation; either
21 // version 2 of the License, or (at your option) any later version.
23 // This library is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 // Library General Public License for more details.
28 // You should have received a copy of the GNU Library General Public
29 // License along with this library; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 // Please report all bugs and problems on the following page:
35 // http://www.fltk.org/str.php
37 #include <stdio.h> // fprintf()
38 #include <stdlib.h> // exit()
39 #include <string.h> // strcmp()
41 #include <FL/Fl_Window.H>
42 #include <FL/Fl_Menu_Bar.H>
43 #include <FL/filename.H> // fl_open_uri()
45 // This callback is invoked whenever the user clicks an item in the menu bar
46 static void MyMenuCallback(Fl_Widget
*w
, void *) {
47 Fl_Menu_Bar
*bar
= (Fl_Menu_Bar
*)w
; // Get the menubar widget
48 const Fl_Menu_Item
*item
= bar
->mvalue(); // Get the menu item that was picked
50 char ipath
[256]; bar
->item_pathname(ipath
, sizeof(ipath
)); // Get full pathname of picked item
52 fprintf(stderr
, "callback: You picked '%s'", item
->label()); // Print item picked
53 fprintf(stderr
, ", item_pathname() is '%s'", ipath
); // ..and full pathname
55 if ( item
->flags
& (FL_MENU_RADIO
|FL_MENU_TOGGLE
) ) { // Toggle or radio item?
56 fprintf(stderr
, ", value is %s", item
->value()?"on":"off"); // Print item's value
58 fprintf(stderr
, "\n");
59 if ( strcmp(item
->label(), "Google") == 0 ) { fl_open_uri("http://google.com/"); }
60 if ( strcmp(item
->label(), "&Quit") == 0 ) { exit(0); }
65 Fl_Window
*win
= new Fl_Window(400,200, "menubar-simple"); // Create window
66 Fl_Menu_Bar
*menu
= new Fl_Menu_Bar(0,0,400,25); // Create menubar, items..
67 menu
->add("&File/&Open", "^o", MyMenuCallback
);
68 menu
->add("&File/&Save", "^s", MyMenuCallback
, 0, FL_MENU_DIVIDER
);
69 menu
->add("&File/&Quit", "^q", MyMenuCallback
);
70 menu
->add("&Edit/&Copy", "^c", MyMenuCallback
);
71 menu
->add("&Edit/&Paste", "^v", MyMenuCallback
, 0, FL_MENU_DIVIDER
);
72 menu
->add("&Edit/Radio 1", 0, MyMenuCallback
, 0, FL_MENU_RADIO
);
73 menu
->add("&Edit/Radio 2", 0, MyMenuCallback
, 0, FL_MENU_RADIO
|FL_MENU_DIVIDER
);
74 menu
->add("&Edit/Toggle 1", 0, MyMenuCallback
, 0, FL_MENU_TOGGLE
); // Default: off
75 menu
->add("&Edit/Toggle 2", 0, MyMenuCallback
, 0, FL_MENU_TOGGLE
); // Default: off
76 menu
->add("&Edit/Toggle 3", 0, MyMenuCallback
, 0, FL_MENU_TOGGLE
|FL_MENU_VALUE
); // Default: on
77 menu
->add("&Help/Google", 0, MyMenuCallback
);
79 // Example: show how we can dynamically change the state of item Toggle #2 (turn it 'on')
81 Fl_Menu_Item
*item
= (Fl_Menu_Item
*)menu
->find_item("&Edit/Toggle 2"); // Find item
82 if ( item
) item
->set(); // Turn it on
83 else fprintf(stderr
, "'Toggle 2' item not found?!\n"); // (optional) Not found? complain!
92 // End of "$Id: menubar-add.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".