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.ComboBoxText; 26 27 private import glib.ConstructionException; 28 private import glib.Str; 29 private import gobject.Signals; 30 private import gtk.ComboBox; 31 private import gtk.TreeIter; 32 private import gtk.TreeModelIF; 33 public import gtkc.gdktypes; 34 private import gtkc.gtk; 35 public import gtkc.gtktypes; 36 private import std.algorithm; 37 38 39 /** 40 * A GtkComboBoxText is a simple variant of #GtkComboBox that hides 41 * the model-view complexity for simple text-only use cases. 42 * 43 * To create a GtkComboBoxText, use gtk_combo_box_text_new() or 44 * gtk_combo_box_text_new_with_entry(). 45 * 46 * You can add items to a GtkComboBoxText with 47 * gtk_combo_box_text_append_text(), gtk_combo_box_text_insert_text() 48 * or gtk_combo_box_text_prepend_text() and remove options with 49 * gtk_combo_box_text_remove(). 50 * 51 * If the GtkComboBoxText contains an entry (via the “has-entry” property), 52 * its contents can be retrieved using gtk_combo_box_text_get_active_text(). 53 * The entry itself can be accessed by calling gtk_bin_get_child() on the 54 * combo box. 55 * 56 * You should not call gtk_combo_box_set_model() or attempt to pack more cells 57 * into this combo box via its GtkCellLayout interface. 58 * 59 * # GtkComboBoxText as GtkBuildable 60 * 61 * The GtkComboBoxText implementation of the GtkBuildable interface supports 62 * adding items directly using the <items> element and specifying <item> 63 * elements for each item. Each <item> element can specify the “id” 64 * corresponding to the appended text and also supports the regular 65 * translation attributes “translatable”, “context” and “comments”. 66 * 67 * Here is a UI definition fragment specifying GtkComboBoxText items: 68 * |[ 69 * <object class="GtkComboBoxText"> 70 * <items> 71 * <item translatable="yes" id="factory">Factory</item> 72 * <item translatable="yes" id="home">Home</item> 73 * <item translatable="yes" id="subway">Subway</item> 74 * </items> 75 * </object> 76 * ]| 77 * 78 * # CSS nodes 79 * 80 * |[<!-- language="plain" --> 81 * combobox 82 * ╰── box.linked 83 * ├── entry.combo 84 * ├── button.combo 85 * ╰── window.popup 86 * ]| 87 * 88 * GtkComboBoxText has a single CSS node with name combobox. It adds 89 * the style class .combo to the main CSS nodes of its entry and button 90 * children, and the .linked class to the node of its internal box. 91 */ 92 public class ComboBoxText : ComboBox 93 { 94 /** the main Gtk struct */ 95 protected GtkComboBoxText* gtkComboBoxText; 96 97 /** Get the main Gtk struct */ 98 public GtkComboBoxText* getComboBoxTextStruct() 99 { 100 return gtkComboBoxText; 101 } 102 103 /** the main Gtk struct as a void* */ 104 protected override void* getStruct() 105 { 106 return cast(void*)gtkComboBoxText; 107 } 108 109 protected override void setStruct(GObject* obj) 110 { 111 gtkComboBoxText = cast(GtkComboBoxText*)obj; 112 super.setStruct(obj); 113 } 114 115 /** 116 * Sets our main struct and passes it to the parent class. 117 */ 118 public this (GtkComboBoxText* gtkComboBoxText, bool ownedRef = false) 119 { 120 this.gtkComboBoxText = gtkComboBoxText; 121 super(cast(GtkComboBox*)gtkComboBoxText, ownedRef); 122 } 123 124 /** 125 * Creates a new ComboBoxText, which is a ComboBox just displaying strings. 126 * Params: 127 * entry = If true, create an ComboBox with an entry. 128 * Throws: ConstructionException GTK+ fails to create the object. 129 */ 130 public this (bool entry=true) 131 { 132 GtkComboBoxText* p; 133 if ( entry ) 134 { 135 // GtkWidget* gtk_combo_box_text_new_with_entry (void); 136 p = cast(GtkComboBoxText*)gtk_combo_box_text_new_with_entry(); 137 } 138 else 139 { 140 // GtkWidget* gtk_combo_box_text_new (void); 141 p = cast(GtkComboBoxText*)gtk_combo_box_text_new(); 142 } 143 144 if(p is null) 145 { 146 throw new ConstructionException("null returned by gtk_combo_box_new"); 147 } 148 149 this(p); 150 } 151 152 /** */ 153 public void setActiveText(string text, bool insert=false) 154 { 155 int active = 0; 156 setActive(0); 157 while ( getActive() >= 0 ) // returns -1 if end of list if reached 158 { 159 if( text == getActiveText() ) return; 160 ++active; 161 setActive(active); 162 } 163 // was not found, the combo has now nothing selected 164 if ( insert ) 165 { 166 append("", text); 167 setActive(active); 168 } 169 } 170 171 /** */ 172 int getIndex(string text) 173 { 174 TreeIter iter; 175 TreeModelIF model = getModel(); 176 int index = 0; 177 bool found = false; 178 bool end = false; 179 if ( model.getIterFirst(iter) ) 180 { 181 iter.setModel(model); 182 while ( !end && iter !is null && !found ) 183 { 184 found = iter.getValueString(0) == text; 185 if ( !found ) 186 { 187 end = !model.iterNext(iter); 188 ++index; 189 } 190 } 191 } 192 else 193 { 194 end = true; 195 } 196 return end ? -1 : index; 197 } 198 199 /** */ 200 void prependOrReplaceText(string text) 201 { 202 int index = getIndex(text); 203 if ( index > 0 ) 204 { 205 remove(index); 206 prepend("", text); 207 } 208 else if ( index == -1 ) 209 { 210 prepend("", text); 211 } 212 } 213 214 protected class OnChangedDelegateWrapper 215 { 216 void delegate(ComboBoxText) dlg; 217 gulong handlerId; 218 ConnectFlags flags; 219 this(void delegate(ComboBoxText) dlg, gulong handlerId, ConnectFlags flags) 220 { 221 this.dlg = dlg; 222 this.handlerId = handlerId; 223 this.flags = flags; 224 } 225 } 226 protected OnChangedDelegateWrapper[] onChangedListeners; 227 228 /** 229 * The changed signal is emitted when the active 230 * item is changed. The can be due to the user selecting 231 * a different item from the list, or due to a 232 * call to gtk_combo_box_set_active_iter(). 233 * It will also be emitted while typing into the entry of a combo box 234 * with an entry. 235 * 236 * Since: 2.4 237 */ 238 gulong addOnChanged(void delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 239 { 240 onChangedListeners ~= new OnChangedDelegateWrapper(dlg, 0, connectFlags); 241 onChangedListeners[onChangedListeners.length - 1].handlerId = Signals.connectData( 242 this, 243 "changed", 244 cast(GCallback)&callBackChanged, 245 cast(void*)onChangedListeners[onChangedListeners.length - 1], 246 cast(GClosureNotify)&callBackChangedDestroy, 247 connectFlags); 248 return onChangedListeners[onChangedListeners.length - 1].handlerId; 249 } 250 251 extern(C) static void callBackChanged(GtkComboBoxText* comboboxTextStruct, OnChangedDelegateWrapper wrapper) 252 { 253 wrapper.dlg(wrapper.outer); 254 } 255 256 extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure) 257 { 258 wrapper.outer.internalRemoveOnChanged(wrapper); 259 } 260 261 protected void internalRemoveOnChanged(OnChangedDelegateWrapper source) 262 { 263 foreach(index, wrapper; onChangedListeners) 264 { 265 if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId) 266 { 267 onChangedListeners[index] = null; 268 onChangedListeners = std.algorithm.remove(onChangedListeners, index); 269 break; 270 } 271 } 272 } 273 274 protected class OnFormatEntryTextDelegateWrapper 275 { 276 string delegate(string, ComboBoxText) dlg; 277 gulong handlerId; 278 ConnectFlags flags; 279 this(string delegate(string, ComboBoxText) dlg, gulong handlerId, ConnectFlags flags) 280 { 281 this.dlg = dlg; 282 this.handlerId = handlerId; 283 this.flags = flags; 284 } 285 } 286 protected OnFormatEntryTextDelegateWrapper[] onFormatEntryTextListeners; 287 288 /** 289 * For combo boxes that are created with an entry (See GtkComboBox:has-entry). 290 * 291 * A signal which allows you to change how the text displayed in a combo box's 292 * entry is displayed. 293 * 294 * Connect a signal handler which returns an allocated string representing 295 * @path. That string will then be used to set the text in the combo box's entry. 296 * The default signal handler uses the text from the GtkComboBox::entry-text-column 297 * model column. 298 * 299 * Here's an example signal handler which fetches data from the model and 300 * displays it in the entry. 301 * |[<!-- language="C" --> 302 * static gchar* 303 * format_entry_text_callback (GtkComboBox *combo, 304 * const gchar *path, 305 * gpointer user_data) 306 * { 307 * GtkTreeIter iter; 308 * GtkTreeModel model; 309 * gdouble value; 310 * 311 * model = gtk_combo_box_get_model (combo); 312 * 313 * gtk_tree_model_get_iter_from_string (model, &iter, path); 314 * gtk_tree_model_get (model, &iter, 315 * THE_DOUBLE_VALUE_COLUMN, &value, 316 * -1); 317 * 318 * return g_strdup_printf ("%g", value); 319 * } 320 * ]| 321 * 322 * Params: 323 * path = the GtkTreePath string from the combo box's current model to format text for 324 * 325 * Return: a newly allocated string representing @path 326 * for the current GtkComboBox model. 327 * 328 * Since: 3.4 329 */ 330 gulong addOnFormatEntryText(string delegate(string, ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 331 { 332 onFormatEntryTextListeners ~= new OnFormatEntryTextDelegateWrapper(dlg, 0, connectFlags); 333 onFormatEntryTextListeners[onFormatEntryTextListeners.length - 1].handlerId = Signals.connectData( 334 this, 335 "format-entry-text", 336 cast(GCallback)&callBackFormatEntryText, 337 cast(void*)onFormatEntryTextListeners[onFormatEntryTextListeners.length - 1], 338 cast(GClosureNotify)&callBackFormatEntryTextDestroy, 339 connectFlags); 340 return onFormatEntryTextListeners[onFormatEntryTextListeners.length - 1].handlerId; 341 } 342 343 extern(C) static string callBackFormatEntryText(GtkComboBoxText* comboboxStructText, char* path,OnFormatEntryTextDelegateWrapper wrapper) 344 { 345 return wrapper.dlg(Str.toString(path), wrapper.outer); 346 } 347 348 extern(C) static void callBackFormatEntryTextDestroy(OnFormatEntryTextDelegateWrapper wrapper, GClosure* closure) 349 { 350 wrapper.outer.internalRemoveOnFormatEntryText(wrapper); 351 } 352 353 protected void internalRemoveOnFormatEntryText(OnFormatEntryTextDelegateWrapper source) 354 { 355 foreach(index, wrapper; onFormatEntryTextListeners) 356 { 357 if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId) 358 { 359 onFormatEntryTextListeners[index] = null; 360 onFormatEntryTextListeners = std.algorithm.remove(onFormatEntryTextListeners, index); 361 break; 362 } 363 } 364 } 365 366 protected class OnMoveActiveDelegateWrapper 367 { 368 void delegate(GtkScrollType, ComboBoxText) dlg; 369 gulong handlerId; 370 ConnectFlags flags; 371 this(void delegate(GtkScrollType, ComboBoxText) dlg, gulong handlerId, ConnectFlags flags) 372 { 373 this.dlg = dlg; 374 this.handlerId = handlerId; 375 this.flags = flags; 376 } 377 } 378 protected OnMoveActiveDelegateWrapper[] onMoveActiveListeners; 379 380 /** 381 * The ::move-active signal is a 382 * [keybinding signal][GtkBindingSignal] 383 * which gets emitted to move the active selection. 384 * 385 * Params: 386 * scrollType = a #GtkScrollType 387 * 388 * Since: 2.12 389 */ 390 gulong addOnMoveActive(void delegate(GtkScrollType, ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 391 { 392 onMoveActiveListeners ~= new OnMoveActiveDelegateWrapper(dlg, 0, connectFlags); 393 onMoveActiveListeners[onMoveActiveListeners.length - 1].handlerId = Signals.connectData( 394 this, 395 "move-active", 396 cast(GCallback)&callBackMoveActive, 397 cast(void*)onMoveActiveListeners[onMoveActiveListeners.length - 1], 398 cast(GClosureNotify)&callBackMoveActiveDestroy, 399 connectFlags); 400 return onMoveActiveListeners[onMoveActiveListeners.length - 1].handlerId; 401 } 402 403 extern(C) static void callBackMoveActive(GtkComboBoxText* comboboxTextStruct, GtkScrollType scrollType,OnMoveActiveDelegateWrapper wrapper) 404 { 405 wrapper.dlg(scrollType, wrapper.outer); 406 } 407 408 extern(C) static void callBackMoveActiveDestroy(OnMoveActiveDelegateWrapper wrapper, GClosure* closure) 409 { 410 wrapper.outer.internalRemoveOnMoveActive(wrapper); 411 } 412 413 protected void internalRemoveOnMoveActive(OnMoveActiveDelegateWrapper source) 414 { 415 foreach(index, wrapper; onMoveActiveListeners) 416 { 417 if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId) 418 { 419 onMoveActiveListeners[index] = null; 420 onMoveActiveListeners = std.algorithm.remove(onMoveActiveListeners, index); 421 break; 422 } 423 } 424 } 425 426 protected class OnPopdownDelegateWrapper 427 { 428 bool delegate(ComboBoxText) dlg; 429 gulong handlerId; 430 ConnectFlags flags; 431 this(bool delegate(ComboBoxText) dlg, gulong handlerId, ConnectFlags flags) 432 { 433 this.dlg = dlg; 434 this.handlerId = handlerId; 435 this.flags = flags; 436 } 437 } 438 protected OnPopdownDelegateWrapper[] onPopdownListeners; 439 440 /** 441 * The ::popdown signal is a 442 * [keybinding signal][GtkBindingSignal] 443 * which gets emitted to popdown the combo box list. 444 * 445 * The default bindings for this signal are Alt+Up and Escape. 446 * 447 * Since: 2.12 448 */ 449 gulong addOnPopdown(bool delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 450 { 451 onPopdownListeners ~= new OnPopdownDelegateWrapper(dlg, 0, connectFlags); 452 onPopdownListeners[onPopdownListeners.length - 1].handlerId = Signals.connectData( 453 this, 454 "popdown", 455 cast(GCallback)&callBackPopdown, 456 cast(void*)onPopdownListeners[onPopdownListeners.length - 1], 457 cast(GClosureNotify)&callBackPopdownDestroy, 458 connectFlags); 459 return onPopdownListeners[onPopdownListeners.length - 1].handlerId; 460 } 461 462 extern(C) static int callBackPopdown(GtkComboBoxText* comboboxTextStruct,OnPopdownDelegateWrapper wrapper) 463 { 464 return wrapper.dlg(wrapper.outer); 465 } 466 467 extern(C) static void callBackPopdownDestroy(OnPopdownDelegateWrapper wrapper, GClosure* closure) 468 { 469 wrapper.outer.internalRemoveOnPopdown(wrapper); 470 } 471 472 protected void internalRemoveOnPopdown(OnPopdownDelegateWrapper source) 473 { 474 foreach(index, wrapper; onPopdownListeners) 475 { 476 if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId) 477 { 478 onPopdownListeners[index] = null; 479 onPopdownListeners = std.algorithm.remove(onPopdownListeners, index); 480 break; 481 } 482 } 483 } 484 485 486 protected class OnPopupDelegateWrapper 487 { 488 void delegate(ComboBoxText) dlg; 489 gulong handlerId; 490 ConnectFlags flags; 491 this(void delegate(ComboBoxText) dlg, gulong handlerId, ConnectFlags flags) 492 { 493 this.dlg = dlg; 494 this.handlerId = handlerId; 495 this.flags = flags; 496 } 497 } 498 protected OnPopupDelegateWrapper[] onPopupListeners; 499 500 /** 501 * The ::popup signal is a 502 * [keybinding signal][GtkBindingSignal] 503 * which gets emitted to popup the combo box list. 504 * 505 * The default binding for this signal is Alt+Down. 506 * 507 * Since: 2.12 508 */ 509 gulong addOnPopup(void delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 510 { 511 onPopupListeners ~= new OnPopupDelegateWrapper(dlg, 0, connectFlags); 512 onPopupListeners[onPopupListeners.length - 1].handlerId = Signals.connectData( 513 this, 514 "popup", 515 cast(GCallback)&callBackPopup, 516 cast(void*)onPopupListeners[onPopupListeners.length - 1], 517 cast(GClosureNotify)&callBackPopupDestroy, 518 connectFlags); 519 return onPopupListeners[onPopupListeners.length - 1].handlerId; 520 } 521 522 extern(C) static void callBackPopup(GtkComboBoxText* comboboxTextStruct,OnPopupDelegateWrapper wrapper) 523 { 524 wrapper.dlg(wrapper.outer); 525 } 526 527 extern(C) static void callBackPopupDestroy(OnPopupDelegateWrapper wrapper, GClosure* closure) 528 { 529 wrapper.outer.internalRemoveOnPopup(wrapper); 530 } 531 532 protected void internalRemoveOnPopup(OnPopupDelegateWrapper source) 533 { 534 foreach(index, wrapper; onPopupListeners) 535 { 536 if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId) 537 { 538 onPopupListeners[index] = null; 539 onPopupListeners = std.algorithm.remove(onPopupListeners, index); 540 break; 541 } 542 } 543 } 544 545 546 /** 547 */ 548 549 /** */ 550 public static GType getType() 551 { 552 return gtk_combo_box_text_get_type(); 553 } 554 555 /** 556 * Appends @text to the list of strings stored in @combo_box. 557 * If @id is non-%NULL then it is used as the ID of the row. 558 * 559 * This is the same as calling gtk_combo_box_text_insert() with a 560 * position of -1. 561 * 562 * Params: 563 * id = a string ID for this value, or %NULL 564 * text = A string 565 * 566 * Since: 2.24 567 */ 568 public void append(string id, string text) 569 { 570 gtk_combo_box_text_append(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text)); 571 } 572 573 /** 574 * Appends @text to the list of strings stored in @combo_box. 575 * 576 * This is the same as calling gtk_combo_box_text_insert_text() with a 577 * position of -1. 578 * 579 * Params: 580 * text = A string 581 * 582 * Since: 2.24 583 */ 584 public void appendText(string text) 585 { 586 gtk_combo_box_text_append_text(gtkComboBoxText, Str.toStringz(text)); 587 } 588 589 /** 590 * Returns the currently active string in @combo_box, or %NULL 591 * if none is selected. If @combo_box contains an entry, this 592 * function will return its contents (which will not necessarily 593 * be an item from the list). 594 * 595 * Return: a newly allocated string containing the 596 * currently active text. Must be freed with g_free(). 597 * 598 * Since: 2.24 599 */ 600 public string getActiveText() 601 { 602 auto retStr = gtk_combo_box_text_get_active_text(gtkComboBoxText); 603 604 scope(exit) Str.freeString(retStr); 605 return Str.toString(retStr); 606 } 607 608 /** 609 * Inserts @text at @position in the list of strings stored in @combo_box. 610 * If @id is non-%NULL then it is used as the ID of the row. See 611 * #GtkComboBox:id-column. 612 * 613 * If @position is negative then @text is appended. 614 * 615 * Params: 616 * position = An index to insert @text 617 * id = a string ID for this value, or %NULL 618 * text = A string to display 619 * 620 * Since: 3.0 621 */ 622 public void insert(int position, string id, string text) 623 { 624 gtk_combo_box_text_insert(gtkComboBoxText, position, Str.toStringz(id), Str.toStringz(text)); 625 } 626 627 /** 628 * Inserts @text at @position in the list of strings stored in @combo_box. 629 * 630 * If @position is negative then @text is appended. 631 * 632 * This is the same as calling gtk_combo_box_text_insert() with a %NULL 633 * ID string. 634 * 635 * Params: 636 * position = An index to insert @text 637 * text = A string 638 * 639 * Since: 2.24 640 */ 641 public void insertText(int position, string text) 642 { 643 gtk_combo_box_text_insert_text(gtkComboBoxText, position, Str.toStringz(text)); 644 } 645 646 /** 647 * Prepends @text to the list of strings stored in @combo_box. 648 * If @id is non-%NULL then it is used as the ID of the row. 649 * 650 * This is the same as calling gtk_combo_box_text_insert() with a 651 * position of 0. 652 * 653 * Params: 654 * id = a string ID for this value, or %NULL 655 * text = a string 656 * 657 * Since: 2.24 658 */ 659 public void prepend(string id, string text) 660 { 661 gtk_combo_box_text_prepend(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text)); 662 } 663 664 /** 665 * Prepends @text to the list of strings stored in @combo_box. 666 * 667 * This is the same as calling gtk_combo_box_text_insert_text() with a 668 * position of 0. 669 * 670 * Params: 671 * text = A string 672 * 673 * Since: 2.24 674 */ 675 public void prependText(string text) 676 { 677 gtk_combo_box_text_prepend_text(gtkComboBoxText, Str.toStringz(text)); 678 } 679 680 /** 681 * Removes the string at @position from @combo_box. 682 * 683 * Params: 684 * position = Index of the item to remove 685 * 686 * Since: 2.24 687 */ 688 public void remove(int position) 689 { 690 gtk_combo_box_text_remove(gtkComboBoxText, position); 691 } 692 693 /** 694 * Removes all the text entries from the combo box. 695 * 696 * Since: 3.0 697 */ 698 public override void removeAll() 699 { 700 gtk_combo_box_text_remove_all(gtkComboBoxText); 701 } 702 }