1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gtk.Menu; 26 27 private import gdk.Device; 28 private import gdk.Screen; 29 private import gio.MenuModel; 30 private import glib.ConstructionException; 31 private import glib.ListG; 32 private import glib.Str; 33 private import gobject.ObjectG; 34 private import gobject.Signals; 35 private import gtk.AccelGroup; 36 private import gtk.MenuItem; 37 private import gtk.MenuShell; 38 private import gtk.Widget; 39 public import gtkc.gdktypes; 40 private import gtkc.gtk; 41 public import gtkc.gtktypes; 42 43 44 /** 45 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu 46 * consisting of a list of #GtkMenuItem objects which can be navigated 47 * and activated by the user to perform application functions. 48 * 49 * A #GtkMenu is most commonly dropped down by activating a 50 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a 51 * #GtkMenuItem in another #GtkMenu. 52 * 53 * A #GtkMenu can also be popped up by activating a #GtkComboBox. 54 * Other composite widgets such as the #GtkNotebook can pop up a 55 * #GtkMenu as well. 56 * 57 * Applications can display a #GtkMenu as a popup menu by calling the 58 * gtk_menu_popup() function. The example below shows how an application 59 * can pop up a menu when the 3rd mouse button is pressed. 60 * 61 * ## Connecting the popup signal handler. 62 * 63 * |[<!-- language="C" --> 64 * // connect our handler which will popup the menu 65 * g_signal_connect_swapped (window, "button_press_event", 66 * G_CALLBACK (my_popup_handler), menu); 67 * ]| 68 * 69 * ## Signal handler which displays a popup menu. 70 * 71 * |[<!-- language="C" --> 72 * static gint 73 * my_popup_handler (GtkWidget *widget, GdkEvent *event) 74 * { 75 * GtkMenu *menu; 76 * GdkEventButton *event_button; 77 * 78 * g_return_val_if_fail (widget != NULL, FALSE); 79 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE); 80 * g_return_val_if_fail (event != NULL, FALSE); 81 * 82 * // The "widget" is the menu that was supplied when 83 * // g_signal_connect_swapped() was called. 84 * menu = GTK_MENU (widget); 85 * 86 * if (event->type == GDK_BUTTON_PRESS) 87 * { 88 * event_button = (GdkEventButton *) event; 89 * if (event_button->button == GDK_BUTTON_SECONDARY) 90 * { 91 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL, 92 * event_button->button, event_button->time); 93 * return TRUE; 94 * } 95 * } 96 * 97 * return FALSE; 98 * } 99 * ]| 100 * 101 * # CSS nodes 102 * 103 * |[<!-- language="plain" --> 104 * menu 105 * ├── arrow.top 106 * ├── <child> 107 * ┊ 108 * ├── <child> 109 * ╰── arrow.bottom 110 * ]| 111 * 112 * The main CSS node of GtkMenu has name menu, and there are two subnodes 113 * with name arrow, for scrolling menu arrows. These subnodes get the 114 * .top and .bottom style classes. 115 */ 116 public class Menu : MenuShell 117 { 118 /** the main Gtk struct */ 119 protected GtkMenu* gtkMenu; 120 121 /** Get the main Gtk struct */ 122 public GtkMenu* getMenuStruct() 123 { 124 return gtkMenu; 125 } 126 127 /** the main Gtk struct as a void* */ 128 protected override void* getStruct() 129 { 130 return cast(void*)gtkMenu; 131 } 132 133 protected override void setStruct(GObject* obj) 134 { 135 gtkMenu = cast(GtkMenu*)obj; 136 super.setStruct(obj); 137 } 138 139 /** 140 * Sets our main struct and passes it to the parent class. 141 */ 142 public this (GtkMenu* gtkMenu, bool ownedRef = false) 143 { 144 this.gtkMenu = gtkMenu; 145 super(cast(GtkMenuShell*)gtkMenu, ownedRef); 146 } 147 148 /** 149 * Popups up this menu 150 * Params: 151 * button = you can pass a button number here 152 * activateTime = you can pass the time from an event here 153 */ 154 void popup(uint button, uint activateTime) 155 { 156 popup(null, null, null, null, button, activateTime); 157 } 158 159 /** 160 * Creates and append a submenu to this menu. 161 * This menu item that actualy has the sub menu is also created. 162 * Params: 163 * label = the sub menu item label 164 * Returns: the new menu 165 */ 166 Menu appendSubmenu(string label) 167 { 168 MenuItem item = new MenuItem(label); 169 append(item); 170 Menu submenu = new Menu(); 171 item.setSubmenu(submenu); 172 return submenu; 173 } 174 175 /** */ 176 void appendSubmenu(string label, Menu submenu) 177 { 178 MenuItem item = new MenuItem(label); 179 append(item); 180 item.setSubmenu(submenu); 181 } 182 183 /** */ 184 Menu prependSubmenu(string label) 185 { 186 MenuItem item = new MenuItem(label); 187 prepend(item); 188 Menu submenu = new Menu(); 189 item.setSubmenu(submenu); 190 return submenu; 191 } 192 193 /** 194 */ 195 196 /** */ 197 public static GType getType() 198 { 199 return gtk_menu_get_type(); 200 } 201 202 /** 203 * Creates a new #GtkMenu 204 * 205 * Return: a new #GtkMenu 206 * 207 * Throws: ConstructionException GTK+ fails to create the object. 208 */ 209 public this() 210 { 211 auto p = gtk_menu_new(); 212 213 if(p is null) 214 { 215 throw new ConstructionException("null returned by new"); 216 } 217 218 this(cast(GtkMenu*) p); 219 } 220 221 /** 222 * Creates a #GtkMenu and populates it with menu items and 223 * submenus according to @model. 224 * 225 * The created menu items are connected to actions found in the 226 * #GtkApplicationWindow to which the menu belongs - typically 227 * by means of being attached to a widget (see gtk_menu_attach_to_widget()) 228 * that is contained within the #GtkApplicationWindows widget hierarchy. 229 * 230 * Actions can also be added using gtk_widget_insert_action_group() on the menu's 231 * attach widget or on any of its parent widgets. 232 * 233 * Params: 234 * model = a #GMenuModel 235 * 236 * Return: a new #GtkMenu 237 * 238 * Since: 3.4 239 * 240 * Throws: ConstructionException GTK+ fails to create the object. 241 */ 242 public this(MenuModel model) 243 { 244 auto p = gtk_menu_new_from_model((model is null) ? null : model.getMenuModelStruct()); 245 246 if(p is null) 247 { 248 throw new ConstructionException("null returned by new_from_model"); 249 } 250 251 this(cast(GtkMenu*) p); 252 } 253 254 /** 255 * Returns a list of the menus which are attached to this widget. 256 * This list is owned by GTK+ and must not be modified. 257 * 258 * Params: 259 * widget = a #GtkWidget 260 * 261 * Return: the list 262 * of menus attached to his widget. 263 * 264 * Since: 2.6 265 */ 266 public static ListG getForAttachWidget(Widget widget) 267 { 268 auto p = gtk_menu_get_for_attach_widget((widget is null) ? null : widget.getWidgetStruct()); 269 270 if(p is null) 271 { 272 return null; 273 } 274 275 return new ListG(cast(GList*) p); 276 } 277 278 /** 279 * Adds a new #GtkMenuItem to a (table) menu. The number of “cells” that 280 * an item will occupy is specified by @left_attach, @right_attach, 281 * @top_attach and @bottom_attach. These each represent the leftmost, 282 * rightmost, uppermost and lower column and row numbers of the table. 283 * (Columns and rows are indexed from zero). 284 * 285 * Note that this function is not related to gtk_menu_detach(). 286 * 287 * Params: 288 * child = a #GtkMenuItem 289 * leftAttach = The column number to attach the left side of the item to 290 * rightAttach = The column number to attach the right side of the item to 291 * topAttach = The row number to attach the top of the item to 292 * bottomAttach = The row number to attach the bottom of the item to 293 * 294 * Since: 2.4 295 */ 296 public void attach(Widget child, uint leftAttach, uint rightAttach, uint topAttach, uint bottomAttach) 297 { 298 gtk_menu_attach(gtkMenu, (child is null) ? null : child.getWidgetStruct(), leftAttach, rightAttach, topAttach, bottomAttach); 299 } 300 301 /** 302 * Attaches the menu to the widget and provides a callback function 303 * that will be invoked when the menu calls gtk_menu_detach() during 304 * its destruction. 305 * 306 * If the menu is attached to the widget then it will be destroyed 307 * when the widget is destroyed, as if it was a child widget. 308 * An attached menu will also move between screens correctly if the 309 * widgets moves between screens. 310 * 311 * Params: 312 * attachWidget = the #GtkWidget that the menu will be attached to 313 * detacher = the user supplied callback function 314 * that will be called when the menu calls gtk_menu_detach() 315 */ 316 public void attachToWidget(Widget attachWidget, GtkMenuDetachFunc detacher) 317 { 318 gtk_menu_attach_to_widget(gtkMenu, (attachWidget is null) ? null : attachWidget.getWidgetStruct(), detacher); 319 } 320 321 /** 322 * Detaches the menu from the widget to which it had been attached. 323 * This function will call the callback function, @detacher, provided 324 * when the gtk_menu_attach_to_widget() function was called. 325 */ 326 public void detach() 327 { 328 gtk_menu_detach(gtkMenu); 329 } 330 331 /** 332 * Gets the #GtkAccelGroup which holds global accelerators for the 333 * menu. See gtk_menu_set_accel_group(). 334 * 335 * Return: the #GtkAccelGroup associated with the menu 336 */ 337 public AccelGroup getAccelGroup() 338 { 339 auto p = gtk_menu_get_accel_group(gtkMenu); 340 341 if(p is null) 342 { 343 return null; 344 } 345 346 return ObjectG.getDObject!(AccelGroup)(cast(GtkAccelGroup*) p); 347 } 348 349 /** 350 * Retrieves the accelerator path set on the menu. 351 * 352 * Return: the accelerator path set on the menu. 353 * 354 * Since: 2.14 355 */ 356 public string getAccelPath() 357 { 358 return Str.toString(gtk_menu_get_accel_path(gtkMenu)); 359 } 360 361 /** 362 * Returns the selected menu item from the menu. This is used by the 363 * #GtkComboBox. 364 * 365 * Return: the #GtkMenuItem that was last selected 366 * in the menu. If a selection has not yet been made, the 367 * first menu item is selected. 368 */ 369 public Widget getActive() 370 { 371 auto p = gtk_menu_get_active(gtkMenu); 372 373 if(p is null) 374 { 375 return null; 376 } 377 378 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 379 } 380 381 /** 382 * Returns the #GtkWidget that the menu is attached to. 383 * 384 * Return: the #GtkWidget that the menu is attached to 385 */ 386 public Widget getAttachWidget() 387 { 388 auto p = gtk_menu_get_attach_widget(gtkMenu); 389 390 if(p is null) 391 { 392 return null; 393 } 394 395 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 396 } 397 398 /** 399 * Retrieves the number of the monitor on which to show the menu. 400 * 401 * Return: the number of the monitor on which the menu should 402 * be popped up or -1, if no monitor has been set 403 * 404 * Since: 2.14 405 */ 406 public int getMonitor() 407 { 408 return gtk_menu_get_monitor(gtkMenu); 409 } 410 411 /** 412 * Returns whether the menu reserves space for toggles and 413 * icons, regardless of their actual presence. 414 * 415 * Return: Whether the menu reserves toggle space 416 * 417 * Since: 2.18 418 */ 419 public bool getReserveToggleSize() 420 { 421 return gtk_menu_get_reserve_toggle_size(gtkMenu) != 0; 422 } 423 424 /** 425 * Returns whether the menu is torn off. 426 * See gtk_menu_set_tearoff_state(). 427 * 428 * Return: %TRUE if the menu is currently torn off. 429 */ 430 public bool getTearoffState() 431 { 432 return gtk_menu_get_tearoff_state(gtkMenu) != 0; 433 } 434 435 /** 436 * Returns the title of the menu. See gtk_menu_set_title(). 437 * 438 * Return: the title of the menu, or %NULL if the menu 439 * has no title set on it. This string is owned by GTK+ 440 * and should not be modified or freed. 441 */ 442 public string getTitle() 443 { 444 return Str.toString(gtk_menu_get_title(gtkMenu)); 445 } 446 447 /** 448 * Removes the menu from the screen. 449 */ 450 public void popdown() 451 { 452 gtk_menu_popdown(gtkMenu); 453 } 454 455 /** 456 * Displays a menu and makes it available for selection. 457 * 458 * Applications can use this function to display context-sensitive 459 * menus, and will typically supply %NULL for the @parent_menu_shell, 460 * @parent_menu_item, @func and @data parameters. The default menu 461 * positioning function will position the menu at the current mouse 462 * cursor position. 463 * 464 * The @button parameter should be the mouse button pressed to initiate 465 * the menu popup. If the menu popup was initiated by something other 466 * than a mouse button press, such as a mouse button release or a keypress, 467 * @button should be 0. 468 * 469 * The @activate_time parameter is used to conflict-resolve initiation 470 * of concurrent requests for mouse/keyboard grab requests. To function 471 * properly, this needs to be the timestamp of the user event (such as 472 * a mouse click or key press) that caused the initiation of the popup. 473 * Only if no such event is available, gtk_get_current_event_time() can 474 * be used instead. 475 * 476 * Params: 477 * parentMenuShell = the menu shell containing the 478 * triggering menu item, or %NULL 479 * parentMenuItem = the menu item whose activation 480 * triggered the popup, or %NULL 481 * func = a user supplied function used to position 482 * the menu, or %NULL 483 * data = user supplied data to be passed to @func. 484 * button = the mouse button which was pressed to initiate the event. 485 * activateTime = the time at which the activation event occurred. 486 */ 487 public void popup(Widget parentMenuShell, Widget parentMenuItem, GtkMenuPositionFunc func, void* data, uint button, uint activateTime) 488 { 489 gtk_menu_popup(gtkMenu, (parentMenuShell is null) ? null : parentMenuShell.getWidgetStruct(), (parentMenuItem is null) ? null : parentMenuItem.getWidgetStruct(), func, data, button, activateTime); 490 } 491 492 /** 493 * Displays a menu and makes it available for selection. 494 * 495 * Applications can use this function to display context-sensitive menus, 496 * and will typically supply %NULL for the @parent_menu_shell, 497 * @parent_menu_item, @func, @data and @destroy parameters. The default 498 * menu positioning function will position the menu at the current position 499 * of @device (or its corresponding pointer). 500 * 501 * The @button parameter should be the mouse button pressed to initiate 502 * the menu popup. If the menu popup was initiated by something other than 503 * a mouse button press, such as a mouse button release or a keypress, 504 * @button should be 0. 505 * 506 * The @activate_time parameter is used to conflict-resolve initiation of 507 * concurrent requests for mouse/keyboard grab requests. To function 508 * properly, this needs to be the time stamp of the user event (such as 509 * a mouse click or key press) that caused the initiation of the popup. 510 * Only if no such event is available, gtk_get_current_event_time() can 511 * be used instead. 512 * 513 * Params: 514 * device = a #GdkDevice 515 * parentMenuShell = the menu shell containing the triggering 516 * menu item, or %NULL 517 * parentMenuItem = the menu item whose activation triggered 518 * the popup, or %NULL 519 * func = a user supplied function used to position the menu, 520 * or %NULL 521 * data = user supplied data to be passed to @func 522 * destroy = destroy notify for @data 523 * button = the mouse button which was pressed to initiate the event 524 * activateTime = the time at which the activation event occurred 525 * 526 * Since: 3.0 527 */ 528 public void popupForDevice(Device device, Widget parentMenuShell, Widget parentMenuItem, GtkMenuPositionFunc func, void* data, GDestroyNotify destroy, uint button, uint activateTime) 529 { 530 gtk_menu_popup_for_device(gtkMenu, (device is null) ? null : device.getDeviceStruct(), (parentMenuShell is null) ? null : parentMenuShell.getWidgetStruct(), (parentMenuItem is null) ? null : parentMenuItem.getWidgetStruct(), func, data, destroy, button, activateTime); 531 } 532 533 /** 534 * Moves @child to a new @position in the list of @menu 535 * children. 536 * 537 * Params: 538 * child = the #GtkMenuItem to move 539 * position = the new position to place @child. 540 * Positions are numbered from 0 to n - 1 541 */ 542 public void reorderChild(Widget child, int position) 543 { 544 gtk_menu_reorder_child(gtkMenu, (child is null) ? null : child.getWidgetStruct(), position); 545 } 546 547 /** 548 * Repositions the menu according to its position function. 549 */ 550 public void reposition() 551 { 552 gtk_menu_reposition(gtkMenu); 553 } 554 555 /** 556 * Set the #GtkAccelGroup which holds global accelerators for the 557 * menu. This accelerator group needs to also be added to all windows 558 * that this menu is being used in with gtk_window_add_accel_group(), 559 * in order for those windows to support all the accelerators 560 * contained in this group. 561 * 562 * Params: 563 * accelGroup = the #GtkAccelGroup to be associated 564 * with the menu. 565 */ 566 public void setAccelGroup(AccelGroup accelGroup) 567 { 568 gtk_menu_set_accel_group(gtkMenu, (accelGroup is null) ? null : accelGroup.getAccelGroupStruct()); 569 } 570 571 /** 572 * Sets an accelerator path for this menu from which accelerator paths 573 * for its immediate children, its menu items, can be constructed. 574 * The main purpose of this function is to spare the programmer the 575 * inconvenience of having to call gtk_menu_item_set_accel_path() on 576 * each menu item that should support runtime user changable accelerators. 577 * Instead, by just calling gtk_menu_set_accel_path() on their parent, 578 * each menu item of this menu, that contains a label describing its 579 * purpose, automatically gets an accel path assigned. 580 * 581 * For example, a menu containing menu items “New” and “Exit”, will, after 582 * `gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");` has been 583 * called, assign its items the accel paths: `"<Gnumeric-Sheet>/File/New"` 584 * and `"<Gnumeric-Sheet>/File/Exit"`. 585 * 586 * Assigning accel paths to menu items then enables the user to change 587 * their accelerators at runtime. More details about accelerator paths 588 * and their default setups can be found at gtk_accel_map_add_entry(). 589 * 590 * Note that @accel_path string will be stored in a #GQuark. Therefore, 591 * if you pass a static string, you can save some memory by interning 592 * it first with g_intern_static_string(). 593 * 594 * Params: 595 * accelPath = a valid accelerator path 596 */ 597 public void setAccelPath(string accelPath) 598 { 599 gtk_menu_set_accel_path(gtkMenu, Str.toStringz(accelPath)); 600 } 601 602 /** 603 * Selects the specified menu item within the menu. This is used by 604 * the #GtkComboBox and should not be used by anyone else. 605 * 606 * Params: 607 * index = the index of the menu item to select. Index values are 608 * from 0 to n-1 609 */ 610 public void setActive(uint index) 611 { 612 gtk_menu_set_active(gtkMenu, index); 613 } 614 615 /** 616 * Informs GTK+ on which monitor a menu should be popped up. 617 * See gdk_screen_get_monitor_geometry(). 618 * 619 * This function should be called from a #GtkMenuPositionFunc 620 * if the menu should not appear on the same monitor as the pointer. 621 * This information can’t be reliably inferred from the coordinates 622 * returned by a #GtkMenuPositionFunc, since, for very long menus, 623 * these coordinates may extend beyond the monitor boundaries or even 624 * the screen boundaries. 625 * 626 * Params: 627 * monitorNum = the number of the monitor on which the menu should 628 * be popped up 629 * 630 * Since: 2.4 631 */ 632 public void setMonitor(int monitorNum) 633 { 634 gtk_menu_set_monitor(gtkMenu, monitorNum); 635 } 636 637 /** 638 * Sets whether the menu should reserve space for drawing toggles 639 * or icons, regardless of their actual presence. 640 * 641 * Params: 642 * reserveToggleSize = whether to reserve size for toggles 643 * 644 * Since: 2.18 645 */ 646 public void setReserveToggleSize(bool reserveToggleSize) 647 { 648 gtk_menu_set_reserve_toggle_size(gtkMenu, reserveToggleSize); 649 } 650 651 /** 652 * Sets the #GdkScreen on which the menu will be displayed. 653 * 654 * Params: 655 * screen = a #GdkScreen, or %NULL if the screen should be 656 * determined by the widget the menu is attached to 657 * 658 * Since: 2.2 659 */ 660 public void setScreen(Screen screen) 661 { 662 gtk_menu_set_screen(gtkMenu, (screen is null) ? null : screen.getScreenStruct()); 663 } 664 665 /** 666 * Changes the tearoff state of the menu. A menu is normally 667 * displayed as drop down menu which persists as long as the menu is 668 * active. It can also be displayed as a tearoff menu which persists 669 * until it is closed or reattached. 670 * 671 * Params: 672 * tornOff = If %TRUE, menu is displayed as a tearoff menu. 673 */ 674 public void setTearoffState(bool tornOff) 675 { 676 gtk_menu_set_tearoff_state(gtkMenu, tornOff); 677 } 678 679 /** 680 * Sets the title string for the menu. 681 * 682 * The title is displayed when the menu is shown as a tearoff 683 * menu. If @title is %NULL, the menu will see if it is attached 684 * to a parent menu item, and if so it will try to use the same 685 * text as that menu item’s label. 686 * 687 * Params: 688 * title = a string containing the title for the menu 689 */ 690 public void setTitle(string title) 691 { 692 gtk_menu_set_title(gtkMenu, Str.toStringz(title)); 693 } 694 695 int[string] connectedSignals; 696 697 void delegate(GtkScrollType, Menu)[] onMoveScrollListeners; 698 /** */ 699 void addOnMoveScroll(void delegate(GtkScrollType, Menu) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 700 { 701 if ( "move-scroll" !in connectedSignals ) 702 { 703 Signals.connectData( 704 this, 705 "move-scroll", 706 cast(GCallback)&callBackMoveScroll, 707 cast(void*)this, 708 null, 709 connectFlags); 710 connectedSignals["move-scroll"] = 1; 711 } 712 onMoveScrollListeners ~= dlg; 713 } 714 extern(C) static void callBackMoveScroll(GtkMenu* menuStruct, GtkScrollType scrollType, Menu _menu) 715 { 716 foreach ( void delegate(GtkScrollType, Menu) dlg; _menu.onMoveScrollListeners ) 717 { 718 dlg(scrollType, _menu); 719 } 720 } 721 }