menus.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. (* Menus
  2. *
  3. * There are several widgets involved in displaying menus. The
  4. * GtkMenuBar widget is a horizontal menu bar, which normally appears
  5. * at the top of an application. The GtkMenu widget is the actual menu
  6. * that pops up. Both GtkMenuBar and GtkMenu are subclasses of
  7. * GtkMenuShell; a GtkMenuShell contains menu items
  8. * (GtkMenuItem). Each menu item contains text and/or images and can
  9. * be selected by the user.
  10. *
  11. * There are several kinds of menu item, including plain GtkMenuItem,
  12. * GtkCheckMenuItem which can be checked/unchecked, GtkRadioMenuItem
  13. * which is a check menu item that's in a mutually exclusive group,
  14. * GtkSeparatorMenuItem which is a separator bar, GtkTearoffMenuItem
  15. * which allows a GtkMenu to be torn off, and GtkImageMenuItem which
  16. * can place a GtkImage or other widget next to the menu text.
  17. *
  18. * A GtkMenuItem can have a submenu, which is simply a GtkMenu to pop
  19. * up when the menu item is selected. Typically, all menu items in a menu bar
  20. * have submenus.
  21. *
  22. * The GtkOptionMenu widget is a button that pops up a GtkMenu when clicked.
  23. * It's used inside dialogs and such.
  24. *
  25. * GtkItemFactory provides a higher-level interface for creating menu bars
  26. * and menus; while you can construct menus manually, most people don't
  27. * do that. There's a separate demo for GtkItemFactory.
  28. *
  29. *)
  30. var
  31. menu_window : PGtkWidget;
  32. function create_menu (depth : gint;
  33. tearoff : gboolean): PGtkWidget;
  34. var
  35. menu,
  36. menuitem : PGtkWidget;
  37. group : PGSList;
  38. buf : pgchar;
  39. i, j : gint;
  40. begin
  41. if depth < 1 then
  42. exit (NULL);
  43. menu := gtk_menu_new ();
  44. group := NULL;
  45. if tearoff then
  46. begin
  47. menuitem := gtk_tearoff_menu_item_new ();
  48. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  49. gtk_widget_show (menuitem);
  50. end;
  51. j:=1;
  52. for i:=0 to 4 do
  53. begin
  54. buf := g_strdup_printf ('item %2d - %d', [depth, j]);
  55. menuitem := gtk_radio_menu_item_new_with_label (group, buf);
  56. group := gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
  57. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  58. gtk_widget_show (menuitem);
  59. if i = 3 then
  60. gtk_widget_set_sensitive (menuitem, FALSE);
  61. gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (depth - 1, TRUE));
  62. g_free (buf);
  63. inc (j);
  64. end;
  65. create_menu := menu;
  66. end;
  67. function do_menus : PGtkWidget;
  68. var
  69. box1,
  70. box2,
  71. button,
  72. optionmenu,
  73. separator,
  74. menubar,
  75. menu,
  76. menuitem : PGtkWidget;
  77. accel_group : PGtkAccelGroup;
  78. begin
  79. if menu_window = NULL then
  80. begin
  81. menu_window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  82. g_signal_connect (menu_window, 'destroy',
  83. G_CALLBACK(@gtk_widget_destroyed), @menu_window);
  84. g_signal_connect (menu_window, 'delete-event',
  85. G_CALLBACK (@gtk_true), NULL);
  86. accel_group := gtk_accel_group_new ();
  87. gtk_window_add_accel_group (GTK_WINDOW (menu_window), accel_group);
  88. gtk_window_set_title (GTK_WINDOW (menu_window), 'Menus');
  89. gtk_container_set_border_width (GTK_CONTAINER (menu_window), 0);
  90. box1 := gtk_vbox_new (FALSE, 0);
  91. gtk_container_add (GTK_CONTAINER (menu_window), box1);
  92. gtk_widget_show (box1);
  93. menubar := gtk_menu_bar_new ();
  94. gtk_box_pack_start (GTK_BOX (box1), menubar, FALSE, TRUE, 0);
  95. gtk_widget_show (menubar);
  96. menu := create_menu (2, TRUE);
  97. menuitem := gtk_menu_item_new_with_label ('test'#13#10'line2');
  98. gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
  99. gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
  100. gtk_widget_show (menuitem);
  101. menuitem := gtk_menu_item_new_with_label ('foo');
  102. gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (3, TRUE));
  103. gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
  104. gtk_widget_show (menuitem);
  105. menuitem := gtk_menu_item_new_with_label ('bar');
  106. gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (4, TRUE));
  107. gtk_menu_item_set_right_justified (GTK_MENU_ITEM (menuitem), TRUE);
  108. gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
  109. gtk_widget_show (menuitem);
  110. box2 := gtk_vbox_new (FALSE, 10);
  111. gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  112. gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
  113. gtk_widget_show (box2);
  114. menu := create_menu (1, FALSE);
  115. gtk_menu_set_accel_group (GTK_MENU (menu), accel_group);
  116. menuitem := gtk_separator_menu_item_new ();
  117. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  118. gtk_widget_show (menuitem);
  119. menuitem := gtk_check_menu_item_new_with_label ('Accelerate Me');
  120. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  121. gtk_widget_show (menuitem);
  122. gtk_widget_add_accelerator (menuitem,
  123. 'activate',
  124. accel_group,
  125. GDK_KEY_F1,
  126. 0,
  127. GTK_ACCEL_VISIBLE);
  128. menuitem := gtk_check_menu_item_new_with_label ('Accelerator Locked');
  129. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  130. gtk_widget_show (menuitem);
  131. gtk_widget_add_accelerator (menuitem,
  132. 'activate',
  133. accel_group,
  134. GDK_KEY_F2,
  135. 0,
  136. GTK_ACCEL_VISIBLE or GTK_ACCEL_LOCKED);
  137. menuitem := gtk_check_menu_item_new_with_label ('Accelerators Frozen');
  138. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  139. gtk_widget_show (menuitem);
  140. gtk_widget_add_accelerator (menuitem,
  141. 'activate',
  142. accel_group,
  143. GDK_KEY_F2,
  144. 0,
  145. GTK_ACCEL_VISIBLE);
  146. gtk_widget_add_accelerator (menuitem,
  147. 'activate',
  148. accel_group,
  149. GDK_KEY_F3,
  150. 0,
  151. GTK_ACCEL_VISIBLE);
  152. optionmenu := gtk_option_menu_new ();
  153. gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);
  154. gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), 3);
  155. gtk_box_pack_start (GTK_BOX (box2), optionmenu, TRUE, TRUE, 0);
  156. gtk_widget_show (optionmenu);
  157. separator := gtk_hseparator_new ();
  158. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  159. gtk_widget_show (separator);
  160. box2 := gtk_vbox_new (FALSE, 10);
  161. gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  162. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  163. gtk_widget_show (box2);
  164. button := gtk_button_new_with_label ('close');
  165. g_signal_connect_swapped (button, 'clicked',
  166. G_CALLBACK(@gtk_widget_destroy), menu_window);
  167. gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
  168. GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  169. gtk_widget_grab_default (button);
  170. gtk_widget_show (button);
  171. end;
  172. if not GTK_WIDGET_VISIBLE (menu_window) then
  173. gtk_widget_show (menu_window)
  174. else begin
  175. gtk_widget_destroy (menu_window);
  176. menu_window := NULL;
  177. end;
  178. do_menus := menu_window;
  179. end;