You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.9KB

  1. //
  2. // "$Id: menubar-add.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
  3. //
  4. // An example of using Fl_Menu_Bar's add() to dynamically create menubars
  5. //
  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
  10. // to type and read.
  11. //
  12. // In this case we're using one callback for all items,
  13. // but you can make unique callbacks for each item if needed.
  14. //
  15. // Copyright 2010 Greg Ercolano.
  16. // Copyright 1998-2010 by Bill Spitzak and others.
  17. //
  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.
  22. //
  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.
  27. //
  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
  31. // USA.
  32. //
  33. // Please report all bugs and problems on the following page:
  34. //
  35. // http://www.fltk.org/str.php
  36. //
  37. #include <stdio.h> // fprintf()
  38. #include <stdlib.h> // exit()
  39. #include <string.h> // strcmp()
  40. #include <FL/Fl.H>
  41. #include <FL/Fl_Window.H>
  42. #include <FL/Fl_Menu_Bar.H>
  43. #include <FL/filename.H> // fl_open_uri()
  44. // This callback is invoked whenever the user clicks an item in the menu bar
  45. static void MyMenuCallback(Fl_Widget *w, void *) {
  46. Fl_Menu_Bar *bar = (Fl_Menu_Bar*)w; // Get the menubar widget
  47. const Fl_Menu_Item *item = bar->mvalue(); // Get the menu item that was picked
  48. char ipath[256]; bar->item_pathname(ipath, sizeof(ipath)); // Get full pathname of picked item
  49. fprintf(stderr, "callback: You picked '%s'", item->label()); // Print item picked
  50. fprintf(stderr, ", item_pathname() is '%s'", ipath); // ..and full pathname
  51. if ( item->flags & (FL_MENU_RADIO|FL_MENU_TOGGLE) ) { // Toggle or radio item?
  52. fprintf(stderr, ", value is %s", item->value()?"on":"off"); // Print item's value
  53. }
  54. fprintf(stderr, "\n");
  55. if ( strcmp(item->label(), "Google") == 0 ) { fl_open_uri("http://google.com/"); }
  56. if ( strcmp(item->label(), "&Quit") == 0 ) { exit(0); }
  57. }
  58. int main() {
  59. Fl::scheme("gtk+");
  60. Fl_Window *win = new Fl_Window(400,200, "menubar-simple"); // Create window
  61. Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25); // Create menubar, items..
  62. menu->add("&File/&Open", "^o", MyMenuCallback);
  63. menu->add("&File/&Save", "^s", MyMenuCallback, 0, FL_MENU_DIVIDER);
  64. menu->add("&File/&Quit", "^q", MyMenuCallback);
  65. menu->add("&Edit/&Copy", "^c", MyMenuCallback);
  66. menu->add("&Edit/&Paste", "^v", MyMenuCallback, 0, FL_MENU_DIVIDER);
  67. menu->add("&Edit/Radio 1", 0, MyMenuCallback, 0, FL_MENU_RADIO);
  68. menu->add("&Edit/Radio 2", 0, MyMenuCallback, 0, FL_MENU_RADIO|FL_MENU_DIVIDER);
  69. menu->add("&Edit/Toggle 1", 0, MyMenuCallback, 0, FL_MENU_TOGGLE); // Default: off
  70. menu->add("&Edit/Toggle 2", 0, MyMenuCallback, 0, FL_MENU_TOGGLE); // Default: off
  71. menu->add("&Edit/Toggle 3", 0, MyMenuCallback, 0, FL_MENU_TOGGLE|FL_MENU_VALUE); // Default: on
  72. menu->add("&Help/Google", 0, MyMenuCallback);
  73. // Example: show how we can dynamically change the state of item Toggle #2 (turn it 'on')
  74. {
  75. Fl_Menu_Item *item = (Fl_Menu_Item*)menu->find_item("&Edit/Toggle 2"); // Find item
  76. if ( item ) item->set(); // Turn it on
  77. else fprintf(stderr, "'Toggle 2' item not found?!\n"); // (optional) Not found? complain!
  78. }
  79. win->end();
  80. win->show();
  81. return(Fl::run());
  82. }
  83. //
  84. // End of "$Id: menubar-add.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".
  85. //