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