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