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.MenuItem; 26 27 private import glib.ConstructionException; 28 private import glib.Str; 29 private import gobject.ObjectG; 30 private import gobject.Signals; 31 private import gtk.AccelGroup; 32 private import gtk.ActionableIF; 33 private import gtk.ActionableT; 34 private import gtk.ActivatableIF; 35 private import gtk.ActivatableT; 36 private import gtk.Bin; 37 private import gtk.Menu; 38 private import gtk.Widget; 39 public import gtkc.gdktypes; 40 private import gtkc.gtk; 41 public import gtkc.gtktypes; 42 43 44 /** 45 * The #GtkMenuItem widget and the derived widgets are the only valid 46 * children for menus. Their function is to correctly handle highlighting, 47 * alignment, events and submenus. 48 * 49 * As a GtkMenuItem derives from #GtkBin it can hold any valid child widget, 50 * although only a few are really useful. 51 * 52 * By default, a GtkMenuItem sets a #GtkAccelLabel as its child. 53 * GtkMenuItem has direct functions to set the label and its mnemonic. 54 * For more advanced label settings, you can fetch the child widget from the GtkBin. 55 * 56 * An example for setting markup and accelerator on a MenuItem: 57 * |[<!-- language="C" --> 58 * GtkWidget *child = gtk_bin_get_child (GTK_BIN (menu_item)); 59 * gtk_label_set_markup (GTK_LABEL (child), "<i>new label</i> with <b>markup</b>"); 60 * gtk_accel_label_set_accel (GTK_ACCEL_LABEL (child), GDK_KEY_1, 0); 61 * ]| 62 * 63 * # GtkMenuItem as GtkBuildable 64 * 65 * The GtkMenuItem implementation of the #GtkBuildable interface supports 66 * adding a submenu by specifying “submenu” as the “type” attribute of 67 * a <child> element. 68 * 69 * An example of UI definition fragment with submenus: 70 * |[ 71 * <object class="GtkMenuItem"> 72 * <child type="submenu"> 73 * <object class="GtkMenu"/> 74 * </child> 75 * </object> 76 * ]| 77 */ 78 public class MenuItem : Bin, ActionableIF, ActivatableIF 79 { 80 /** the main Gtk struct */ 81 protected GtkMenuItem* gtkMenuItem; 82 83 /** Get the main Gtk struct */ 84 public GtkMenuItem* getMenuItemStruct() 85 { 86 return gtkMenuItem; 87 } 88 89 /** the main Gtk struct as a void* */ 90 protected override void* getStruct() 91 { 92 return cast(void*)gtkMenuItem; 93 } 94 95 protected override void setStruct(GObject* obj) 96 { 97 gtkMenuItem = cast(GtkMenuItem*)obj; 98 super.setStruct(obj); 99 } 100 101 /** 102 * Sets our main struct and passes it to the parent class. 103 */ 104 public this (GtkMenuItem* gtkMenuItem, bool ownedRef = false) 105 { 106 this.gtkMenuItem = gtkMenuItem; 107 super(cast(GtkBin*)gtkMenuItem, ownedRef); 108 } 109 110 // add the Actionable capabilities 111 mixin ActionableT!(GtkMenuItem); 112 113 // add the Activatable capabilities 114 mixin ActivatableT!(GtkMenuItem); 115 116 /** store the action code passed in by the applcation */ 117 private string actionLabel; 118 119 /** Gets the application set action code */ 120 public string getActionName() 121 { 122 if ( actionLabel is null ) 123 { 124 actionLabel = ""; 125 } 126 return actionLabel; 127 } 128 129 /** 130 * Creates a new menu item with a label and a listener and a action. 131 * used for backward compatibily with DUI. 132 */ 133 this(string label, void delegate(MenuItem)dlg, string action) 134 { 135 this(label); 136 this.actionLabel = action; 137 addOnActivate(dlg); 138 } 139 140 /** 141 * Creates a new Item associated with a "activate" delegate and with a action code 142 * and optionally accelGroup 143 */ 144 public this(void delegate(MenuItem) dlg, string label, string action, 145 bool mnemonic=true, 146 AccelGroup accelGroup=null, 147 char accelKey='\0', 148 GdkModifierType modifierType=GdkModifierType.CONTROL_MASK, 149 GtkAccelFlags accelFlags=GtkAccelFlags.VISIBLE 150 ) 151 { 152 this(label, mnemonic); 153 this.actionLabel = action; 154 addOnActivate(dlg); 155 if ( accelGroup !is null && accelKey != '\0' ) 156 { 157 addAccelerator("activate",accelGroup,accelKey,modifierType,accelFlags); 158 } 159 } 160 161 /** 162 * Creates a new Item associated with a "activate" delegate 163 */ 164 public this(void delegate(MenuItem) dlg, string label, bool mnemonic=true) 165 { 166 this(label, mnemonic); 167 addOnActivate(dlg); 168 } 169 170 /** 171 * Creates a new GtkMenuItem whose child is a GtkLabel. 172 * Params: 173 * label = the text for the label 174 * mnemonic = if true the label 175 * will be created using gtk_label_new_with_mnemonic(), so underscores 176 * in label indicate the mnemonic for the menu item. 177 * Throws: ConstructionException GTK+ fails to create the object. 178 */ 179 public this (string label, bool mnemonic=true) 180 { 181 GtkMenuItem* p; 182 183 if ( mnemonic ) 184 { 185 // GtkWidget* gtk_menu_item_new_with_mnemonic (const gchar *label); 186 p = cast(GtkMenuItem*)gtk_menu_item_new_with_mnemonic(Str.toStringz(label)); 187 } 188 else 189 { 190 // GtkWidget* gtk_menu_item_new_with_label (const gchar *label); 191 p = cast(GtkMenuItem*)gtk_menu_item_new_with_label(Str.toStringz(label)); 192 } 193 194 if(p is null) 195 { 196 throw new ConstructionException("null returned by gtk_menu_item_new_with_"); 197 } 198 199 this(p); 200 201 setName(label); 202 } 203 204 /** 205 */ 206 207 public static GType getType() 208 { 209 return gtk_menu_item_get_type(); 210 } 211 212 /** 213 * Creates a new #GtkMenuItem. 214 * 215 * Return: the newly created #GtkMenuItem 216 * 217 * Throws: ConstructionException GTK+ fails to create the object. 218 */ 219 public this() 220 { 221 auto p = gtk_menu_item_new(); 222 223 if(p is null) 224 { 225 throw new ConstructionException("null returned by new"); 226 } 227 228 this(cast(GtkMenuItem*) p); 229 } 230 231 /** 232 * Emits the #GtkMenuItem::activate signal on the given item 233 */ 234 public void itemActivate() 235 { 236 gtk_menu_item_activate(gtkMenuItem); 237 } 238 239 /** 240 * Emits the #GtkMenuItem::deselect signal on the given item. 241 */ 242 public void deselect() 243 { 244 gtk_menu_item_deselect(gtkMenuItem); 245 } 246 247 /** 248 * Retrieve the accelerator path that was previously set on @menu_item. 249 * 250 * See gtk_menu_item_set_accel_path() for details. 251 * 252 * Return: the accelerator path corresponding to this menu 253 * item’s functionality, or %NULL if not set 254 * 255 * Since: 2.14 256 */ 257 public string getAccelPath() 258 { 259 return Str.toString(gtk_menu_item_get_accel_path(gtkMenuItem)); 260 } 261 262 /** 263 * Sets @text on the @menu_item label 264 * 265 * Return: The text in the @menu_item label. This is the internal 266 * string used by the label, and must not be modified. 267 * 268 * Since: 2.16 269 */ 270 public string getLabel() 271 { 272 return Str.toString(gtk_menu_item_get_label(gtkMenuItem)); 273 } 274 275 /** 276 * Returns whether the @menu_item reserves space for 277 * the submenu indicator, regardless if it has a submenu 278 * or not. 279 * 280 * Return: %TRUE if @menu_item always reserves space for the 281 * submenu indicator 282 * 283 * Since: 3.0 284 */ 285 public bool getReserveIndicator() 286 { 287 return gtk_menu_item_get_reserve_indicator(gtkMenuItem) != 0; 288 } 289 290 /** 291 * Gets whether the menu item appears justified at the right 292 * side of the menu bar. 293 * 294 * Deprecated: See gtk_menu_item_set_right_justified() 295 * 296 * Return: %TRUE if the menu item will appear at the 297 * far right if added to a menu bar. 298 */ 299 public bool getRightJustified() 300 { 301 return gtk_menu_item_get_right_justified(gtkMenuItem) != 0; 302 } 303 304 /** 305 * Gets the submenu underneath this menu item, if any. 306 * See gtk_menu_item_set_submenu(). 307 * 308 * Return: submenu for this menu item, or %NULL if none 309 */ 310 public Widget getSubmenu() 311 { 312 auto p = gtk_menu_item_get_submenu(gtkMenuItem); 313 314 if(p is null) 315 { 316 return null; 317 } 318 319 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 320 } 321 322 /** 323 * Checks if an underline in the text indicates the next character 324 * should be used for the mnemonic accelerator key. 325 * 326 * Return: %TRUE if an embedded underline in the label 327 * indicates the mnemonic accelerator key. 328 * 329 * Since: 2.16 330 */ 331 public bool getUseUnderline() 332 { 333 return gtk_menu_item_get_use_underline(gtkMenuItem) != 0; 334 } 335 336 /** 337 * Emits the #GtkMenuItem::select signal on the given item. 338 */ 339 public void select() 340 { 341 gtk_menu_item_select(gtkMenuItem); 342 } 343 344 /** 345 * Set the accelerator path on @menu_item, through which runtime 346 * changes of the menu item’s accelerator caused by the user can be 347 * identified and saved to persistent storage (see gtk_accel_map_save() 348 * on this). To set up a default accelerator for this menu item, call 349 * gtk_accel_map_add_entry() with the same @accel_path. See also 350 * gtk_accel_map_add_entry() on the specifics of accelerator paths, 351 * and gtk_menu_set_accel_path() for a more convenient variant of 352 * this function. 353 * 354 * This function is basically a convenience wrapper that handles 355 * calling gtk_widget_set_accel_path() with the appropriate accelerator 356 * group for the menu item. 357 * 358 * Note that you do need to set an accelerator on the parent menu with 359 * gtk_menu_set_accel_group() for this to work. 360 * 361 * Note that @accel_path string will be stored in a #GQuark. 362 * Therefore, if you pass a static string, you can save some memory 363 * by interning it first with g_intern_static_string(). 364 * 365 * Params: 366 * accelPath = accelerator path, corresponding to this menu 367 * item’s functionality, or %NULL to unset the current path. 368 */ 369 public void setAccelPath(string accelPath) 370 { 371 gtk_menu_item_set_accel_path(gtkMenuItem, Str.toStringz(accelPath)); 372 } 373 374 /** 375 * Sets @text on the @menu_item label 376 * 377 * Params: 378 * label = the text you want to set 379 * 380 * Since: 2.16 381 */ 382 public void setLabel(string label) 383 { 384 gtk_menu_item_set_label(gtkMenuItem, Str.toStringz(label)); 385 } 386 387 /** 388 * Sets whether the @menu_item should reserve space for 389 * the submenu indicator, regardless if it actually has 390 * a submenu or not. 391 * 392 * There should be little need for applications to call 393 * this functions. 394 * 395 * Params: 396 * reserve = the new value 397 * 398 * Since: 3.0 399 */ 400 public void setReserveIndicator(bool reserve) 401 { 402 gtk_menu_item_set_reserve_indicator(gtkMenuItem, reserve); 403 } 404 405 /** 406 * Sets whether the menu item appears justified at the right 407 * side of a menu bar. This was traditionally done for “Help” 408 * menu items, but is now considered a bad idea. (If the widget 409 * layout is reversed for a right-to-left language like Hebrew 410 * or Arabic, right-justified-menu-items appear at the left.) 411 * 412 * Deprecated: If you insist on using it, use 413 * gtk_widget_set_hexpand() and gtk_widget_set_halign(). 414 * 415 * Params: 416 * rightJustified = if %TRUE the menu item will appear at the 417 * far right if added to a menu bar 418 */ 419 public void setRightJustified(bool rightJustified) 420 { 421 gtk_menu_item_set_right_justified(gtkMenuItem, rightJustified); 422 } 423 424 /** 425 * Sets or replaces the menu item’s submenu, or removes it when a %NULL 426 * submenu is passed. 427 * 428 * Params: 429 * submenu = the submenu, or %NULL 430 */ 431 public void setSubmenu(Menu submenu) 432 { 433 gtk_menu_item_set_submenu(gtkMenuItem, (submenu is null) ? null : cast(GtkWidget*)submenu.getMenuStruct()); 434 } 435 436 /** 437 * If true, an underline in the text indicates the next character 438 * should be used for the mnemonic accelerator key. 439 * 440 * Params: 441 * setting = %TRUE if underlines in the text indicate mnemonics 442 * 443 * Since: 2.16 444 */ 445 public void setUseUnderline(bool setting) 446 { 447 gtk_menu_item_set_use_underline(gtkMenuItem, setting); 448 } 449 450 /** 451 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item. 452 * 453 * Params: 454 * allocation = the allocation to use as signal data. 455 */ 456 public void toggleSizeAllocate(int allocation) 457 { 458 gtk_menu_item_toggle_size_allocate(gtkMenuItem, allocation); 459 } 460 461 /** 462 * Emits the #GtkMenuItem::toggle-size-request signal on the given item. 463 * 464 * Params: 465 * requisition = the requisition to use as signal data. 466 */ 467 public void toggleSizeRequest(ref int requisition) 468 { 469 gtk_menu_item_toggle_size_request(gtkMenuItem, &requisition); 470 } 471 472 int[string] connectedSignals; 473 474 void delegate(MenuItem)[] onActivateListeners; 475 /** 476 * Emitted when the item is activated. 477 */ 478 void addOnActivate(void delegate(MenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 479 { 480 if ( "activate" !in connectedSignals ) 481 { 482 Signals.connectData( 483 this, 484 "activate", 485 cast(GCallback)&callBackActivate, 486 cast(void*)this, 487 null, 488 connectFlags); 489 connectedSignals["activate"] = 1; 490 } 491 onActivateListeners ~= dlg; 492 } 493 extern(C) static void callBackActivate(GtkMenuItem* menuitemStruct, MenuItem _menuitem) 494 { 495 foreach ( void delegate(MenuItem) dlg; _menuitem.onActivateListeners ) 496 { 497 dlg(_menuitem); 498 } 499 } 500 501 void delegate(MenuItem)[] onActivateItemListeners; 502 /** 503 * Emitted when the item is activated, but also if the menu item has a 504 * submenu. For normal applications, the relevant signal is 505 * #GtkMenuItem::activate. 506 */ 507 void addOnActivateItem(void delegate(MenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 508 { 509 if ( "activate-item" !in connectedSignals ) 510 { 511 Signals.connectData( 512 this, 513 "activate-item", 514 cast(GCallback)&callBackActivateItem, 515 cast(void*)this, 516 null, 517 connectFlags); 518 connectedSignals["activate-item"] = 1; 519 } 520 onActivateItemListeners ~= dlg; 521 } 522 extern(C) static void callBackActivateItem(GtkMenuItem* menuitemStruct, MenuItem _menuitem) 523 { 524 foreach ( void delegate(MenuItem) dlg; _menuitem.onActivateItemListeners ) 525 { 526 dlg(_menuitem); 527 } 528 } 529 530 void delegate(MenuItem)[] onDeselectListeners; 531 void addOnDeselect(void delegate(MenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 532 { 533 if ( "deselect" !in connectedSignals ) 534 { 535 Signals.connectData( 536 this, 537 "deselect", 538 cast(GCallback)&callBackDeselect, 539 cast(void*)this, 540 null, 541 connectFlags); 542 connectedSignals["deselect"] = 1; 543 } 544 onDeselectListeners ~= dlg; 545 } 546 extern(C) static void callBackDeselect(GtkMenuItem* menuitemStruct, MenuItem _menuitem) 547 { 548 foreach ( void delegate(MenuItem) dlg; _menuitem.onDeselectListeners ) 549 { 550 dlg(_menuitem); 551 } 552 } 553 554 void delegate(MenuItem)[] onSelectListeners; 555 void addOnSelect(void delegate(MenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 556 { 557 if ( "select" !in connectedSignals ) 558 { 559 Signals.connectData( 560 this, 561 "select", 562 cast(GCallback)&callBackSelect, 563 cast(void*)this, 564 null, 565 connectFlags); 566 connectedSignals["select"] = 1; 567 } 568 onSelectListeners ~= dlg; 569 } 570 extern(C) static void callBackSelect(GtkMenuItem* menuitemStruct, MenuItem _menuitem) 571 { 572 foreach ( void delegate(MenuItem) dlg; _menuitem.onSelectListeners ) 573 { 574 dlg(_menuitem); 575 } 576 } 577 578 void delegate(int, MenuItem)[] onToggleSizeAllocateListeners; 579 void addOnToggleSizeAllocate(void delegate(int, MenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 580 { 581 if ( "toggle-size-allocate" !in connectedSignals ) 582 { 583 Signals.connectData( 584 this, 585 "toggle-size-allocate", 586 cast(GCallback)&callBackToggleSizeAllocate, 587 cast(void*)this, 588 null, 589 connectFlags); 590 connectedSignals["toggle-size-allocate"] = 1; 591 } 592 onToggleSizeAllocateListeners ~= dlg; 593 } 594 extern(C) static void callBackToggleSizeAllocate(GtkMenuItem* menuitemStruct, int object, MenuItem _menuitem) 595 { 596 foreach ( void delegate(int, MenuItem) dlg; _menuitem.onToggleSizeAllocateListeners ) 597 { 598 dlg(object, _menuitem); 599 } 600 } 601 602 void delegate(void*, MenuItem)[] onToggleSizeRequestListeners; 603 void addOnToggleSizeRequest(void delegate(void*, MenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 604 { 605 if ( "toggle-size-request" !in connectedSignals ) 606 { 607 Signals.connectData( 608 this, 609 "toggle-size-request", 610 cast(GCallback)&callBackToggleSizeRequest, 611 cast(void*)this, 612 null, 613 connectFlags); 614 connectedSignals["toggle-size-request"] = 1; 615 } 616 onToggleSizeRequestListeners ~= dlg; 617 } 618 extern(C) static void callBackToggleSizeRequest(GtkMenuItem* menuitemStruct, void* object, MenuItem _menuitem) 619 { 620 foreach ( void delegate(void*, MenuItem) dlg; _menuitem.onToggleSizeRequestListeners ) 621 { 622 dlg(object, _menuitem); 623 } 624 } 625 }