2010-12-09 03:05:24 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// An example of using Fl_Menu_Bar's add() to dynamically create menubars
|
2010-12-09 03:05:24 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Menu bars can be created several ways. Using add() allows
|
|
|
|
// dynamically creating a menubar using a 'pathname' syntax.
|
|
|
|
// Use if you're creating items dynamically, or if you're making
|
|
|
|
// menubars by hand (as opposed to using fluid), as it's easier
|
|
|
|
// to type and read.
|
2010-12-09 03:05:24 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// In this case we're using one callback for all items,
|
|
|
|
// but you can make unique callbacks for each item if needed.
|
2010-12-09 03:05:24 +03:00
|
|
|
//
|
|
|
|
// Copyright 2010 Greg Ercolano.
|
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2010-12-09 03:05:24 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2010-12-09 03:05:24 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2010-12-09 03:05:24 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
#include <stdio.h> // fprintf()
|
|
|
|
#include <stdlib.h> // exit()
|
|
|
|
#include <string.h> // strcmp()
|
2010-12-09 03:05:24 +03:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
#include <FL/Fl_Menu_Bar.H>
|
2020-07-01 19:03:10 +03:00
|
|
|
#include <FL/filename.H> // fl_open_uri()
|
2010-12-09 03:05:24 +03:00
|
|
|
|
|
|
|
// This callback is invoked whenever the user clicks an item in the menu bar
|
|
|
|
static void MyMenuCallback(Fl_Widget *w, void *) {
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Menu_Bar *bar = (Fl_Menu_Bar*)w; // Get the menubar widget
|
|
|
|
const Fl_Menu_Item *item = bar->mvalue(); // Get the menu item that was picked
|
2010-12-09 03:05:24 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
char ipath[256]; bar->item_pathname(ipath, sizeof(ipath)); // Get full pathname of picked item
|
2010-12-09 03:05:24 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
fprintf(stderr, "callback: You picked '%s'", item->label()); // Print item picked
|
|
|
|
fprintf(stderr, ", item_pathname() is '%s'", ipath); // ..and full pathname
|
2010-12-09 03:05:24 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if ( item->flags & (FL_MENU_RADIO|FL_MENU_TOGGLE) ) { // Toggle or radio item?
|
|
|
|
fprintf(stderr, ", value is %s", item->value()?"on":"off"); // Print item's value
|
2010-12-09 03:05:24 +03:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
if ( strcmp(item->label(), "Google") == 0 ) { fl_open_uri("http://google.com/"); }
|
|
|
|
if ( strcmp(item->label(), "&Quit") == 0 ) { exit(0); }
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
Fl::scheme("gtk+");
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Window *win = new Fl_Window(400,200, "menubar-simple"); // Create window
|
|
|
|
Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25); // Create menubar, items..
|
2010-12-09 03:05:24 +03:00
|
|
|
menu->add("&File/&Open", "^o", MyMenuCallback);
|
|
|
|
menu->add("&File/&Save", "^s", MyMenuCallback, 0, FL_MENU_DIVIDER);
|
|
|
|
menu->add("&File/&Quit", "^q", MyMenuCallback);
|
|
|
|
menu->add("&Edit/&Copy", "^c", MyMenuCallback);
|
|
|
|
menu->add("&Edit/&Paste", "^v", MyMenuCallback, 0, FL_MENU_DIVIDER);
|
|
|
|
menu->add("&Edit/Radio 1", 0, MyMenuCallback, 0, FL_MENU_RADIO);
|
|
|
|
menu->add("&Edit/Radio 2", 0, MyMenuCallback, 0, FL_MENU_RADIO|FL_MENU_DIVIDER);
|
2020-07-01 19:03:10 +03:00
|
|
|
menu->add("&Edit/Toggle 1", 0, MyMenuCallback, 0, FL_MENU_TOGGLE); // Default: off
|
|
|
|
menu->add("&Edit/Toggle 2", 0, MyMenuCallback, 0, FL_MENU_TOGGLE); // Default: off
|
|
|
|
menu->add("&Edit/Toggle 3", 0, MyMenuCallback, 0, FL_MENU_TOGGLE|FL_MENU_VALUE); // Default: on
|
2010-12-09 03:05:24 +03:00
|
|
|
menu->add("&Help/Google", 0, MyMenuCallback);
|
|
|
|
|
|
|
|
// Example: show how we can dynamically change the state of item Toggle #2 (turn it 'on')
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Menu_Item *item = (Fl_Menu_Item*)menu->find_item("&Edit/Toggle 2"); // Find item
|
|
|
|
if ( item ) item->set(); // Turn it on
|
|
|
|
else fprintf(stderr, "'Toggle 2' item not found?!\n"); // (optional) Not found? complain!
|
2010-12-09 03:05:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
win->end();
|
|
|
|
win->show();
|
|
|
|
return(Fl::run());
|
|
|
|
}
|