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 static OnChangedDelegateWrapper[] listeners; 217 void delegate(ComboBoxText) dlg; 218 gulong handlerId; 219 220 this(void delegate(ComboBoxText) dlg) 221 { 222 this.dlg = dlg; 223 this.listeners ~= this; 224 } 225 226 void remove(OnChangedDelegateWrapper source) 227 { 228 foreach(index, wrapper; listeners) 229 { 230 if (wrapper.handlerId == source.handlerId) 231 { 232 listeners[index] = null; 233 listeners = std.algorithm.remove(listeners, index); 234 break; 235 } 236 } 237 } 238 } 239 240 /** 241 * The changed signal is emitted when the active 242 * item is changed. The can be due to the user selecting 243 * a different item from the list, or due to a 244 * call to gtk_combo_box_set_active_iter(). 245 * It will also be emitted while typing into the entry of a combo box 246 * with an entry. 247 * 248 * Since: 2.4 249 */ 250 gulong addOnChanged(void delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 251 { 252 auto wrapper = new OnChangedDelegateWrapper(dlg); 253 wrapper.handlerId = Signals.connectData( 254 this, 255 "changed", 256 cast(GCallback)&callBackChanged, 257 cast(void*)wrapper, 258 cast(GClosureNotify)&callBackChangedDestroy, 259 connectFlags); 260 return wrapper.handlerId; 261 } 262 263 extern(C) static void callBackChanged(GtkComboBoxText* comboboxTextStruct, OnChangedDelegateWrapper wrapper) 264 { 265 wrapper.dlg(wrapper.outer); 266 } 267 268 extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure) 269 { 270 wrapper.remove(wrapper); 271 } 272 273 protected class OnFormatEntryTextDelegateWrapper 274 { 275 static OnFormatEntryTextDelegateWrapper[] listeners; 276 string delegate(string, ComboBoxText) dlg; 277 gulong handlerId; 278 279 this(string delegate(string, ComboBoxText) dlg) 280 { 281 this.dlg = dlg; 282 this.listeners ~= this; 283 } 284 285 void remove(OnFormatEntryTextDelegateWrapper source) 286 { 287 foreach(index, wrapper; listeners) 288 { 289 if (wrapper.handlerId == source.handlerId) 290 { 291 listeners[index] = null; 292 listeners = std.algorithm.remove(listeners, index); 293 break; 294 } 295 } 296 } 297 } 298 299 /** 300 * For combo boxes that are created with an entry (See GtkComboBox:has-entry). 301 * 302 * A signal which allows you to change how the text displayed in a combo box's 303 * entry is displayed. 304 * 305 * Connect a signal handler which returns an allocated string representing 306 * @path. That string will then be used to set the text in the combo box's entry. 307 * The default signal handler uses the text from the GtkComboBox::entry-text-column 308 * model column. 309 * 310 * Here's an example signal handler which fetches data from the model and 311 * displays it in the entry. 312 * |[<!-- language="C" --> 313 * static gchar* 314 * format_entry_text_callback (GtkComboBox *combo, 315 * const gchar *path, 316 * gpointer user_data) 317 * { 318 * GtkTreeIter iter; 319 * GtkTreeModel model; 320 * gdouble value; 321 * 322 * model = gtk_combo_box_get_model (combo); 323 * 324 * gtk_tree_model_get_iter_from_string (model, &iter, path); 325 * gtk_tree_model_get (model, &iter, 326 * THE_DOUBLE_VALUE_COLUMN, &value, 327 * -1); 328 * 329 * return g_strdup_printf ("%g", value); 330 * } 331 * ]| 332 * 333 * Params: 334 * path = the GtkTreePath string from the combo box's current model to format text for 335 * 336 * Return: a newly allocated string representing @path 337 * for the current GtkComboBox model. 338 * 339 * Since: 3.4 340 */ 341 gulong addOnFormatEntryText(string delegate(string, ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 342 { 343 auto wrapper = new OnFormatEntryTextDelegateWrapper(dlg); 344 wrapper.handlerId = Signals.connectData( 345 this, 346 "format-entry-text", 347 cast(GCallback)&callBackFormatEntryText, 348 cast(void*)wrapper, 349 cast(GClosureNotify)&callBackFormatEntryTextDestroy, 350 connectFlags); 351 return wrapper.handlerId; 352 } 353 354 extern(C) static string callBackFormatEntryText(GtkComboBoxText* comboboxStructText, char* path,OnFormatEntryTextDelegateWrapper wrapper) 355 { 356 return wrapper.dlg(Str.toString(path), wrapper.outer); 357 } 358 359 extern(C) static void callBackFormatEntryTextDestroy(OnFormatEntryTextDelegateWrapper wrapper, GClosure* closure) 360 { 361 wrapper.remove(wrapper); 362 } 363 364 protected class OnMoveActiveDelegateWrapper 365 { 366 static OnMoveActiveDelegateWrapper[] listeners; 367 void delegate(GtkScrollType, ComboBoxText) dlg; 368 gulong handlerId; 369 370 this(void delegate(GtkScrollType, ComboBoxText) dlg) 371 { 372 this.dlg = dlg; 373 this.listeners ~= this; 374 } 375 376 void remove(OnMoveActiveDelegateWrapper source) 377 { 378 foreach(index, wrapper; listeners) 379 { 380 if (wrapper.handlerId == source.handlerId) 381 { 382 listeners[index] = null; 383 listeners = std.algorithm.remove(listeners, index); 384 break; 385 } 386 } 387 } 388 } 389 390 /** 391 * The ::move-active signal is a 392 * [keybinding signal][GtkBindingSignal] 393 * which gets emitted to move the active selection. 394 * 395 * Params: 396 * scrollType = a #GtkScrollType 397 * 398 * Since: 2.12 399 */ 400 gulong addOnMoveActive(void delegate(GtkScrollType, ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 401 { 402 auto wrapper = new OnMoveActiveDelegateWrapper(dlg); 403 wrapper.handlerId = Signals.connectData( 404 this, 405 "move-active", 406 cast(GCallback)&callBackMoveActive, 407 cast(void*)wrapper, 408 cast(GClosureNotify)&callBackMoveActiveDestroy, 409 connectFlags); 410 return wrapper.handlerId; 411 } 412 413 extern(C) static void callBackMoveActive(GtkComboBoxText* comboboxTextStruct, GtkScrollType scrollType,OnMoveActiveDelegateWrapper wrapper) 414 { 415 wrapper.dlg(scrollType, wrapper.outer); 416 } 417 418 extern(C) static void callBackMoveActiveDestroy(OnMoveActiveDelegateWrapper wrapper, GClosure* closure) 419 { 420 wrapper.remove(wrapper); 421 } 422 423 protected class OnPopdownDelegateWrapper 424 { 425 static OnPopdownDelegateWrapper[] listeners; 426 bool delegate(ComboBoxText) dlg; 427 gulong handlerId; 428 429 this(bool delegate(ComboBoxText) dlg) 430 { 431 this.dlg = dlg; 432 this.listeners ~= this; 433 } 434 435 void remove(OnPopdownDelegateWrapper source) 436 { 437 foreach(index, wrapper; listeners) 438 { 439 if (wrapper.handlerId == source.handlerId) 440 { 441 listeners[index] = null; 442 listeners = std.algorithm.remove(listeners, index); 443 break; 444 } 445 } 446 } 447 } 448 449 /** 450 * The ::popdown signal is a 451 * [keybinding signal][GtkBindingSignal] 452 * which gets emitted to popdown the combo box list. 453 * 454 * The default bindings for this signal are Alt+Up and Escape. 455 * 456 * Since: 2.12 457 */ 458 gulong addOnPopdown(bool delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 459 { 460 auto wrapper = new OnPopdownDelegateWrapper(dlg); 461 wrapper.handlerId = Signals.connectData( 462 this, 463 "popdown", 464 cast(GCallback)&callBackPopdown, 465 cast(void*)wrapper, 466 cast(GClosureNotify)&callBackPopdownDestroy, 467 connectFlags); 468 return wrapper.handlerId; 469 } 470 471 extern(C) static int callBackPopdown(GtkComboBoxText* comboboxTextStruct,OnPopdownDelegateWrapper wrapper) 472 { 473 return wrapper.dlg(wrapper.outer); 474 } 475 476 extern(C) static void callBackPopdownDestroy(OnPopdownDelegateWrapper wrapper, GClosure* closure) 477 { 478 wrapper.remove(wrapper); 479 } 480 481 protected class OnPopupDelegateWrapper 482 { 483 static OnPopupDelegateWrapper[] listeners; 484 void delegate(ComboBoxText) dlg; 485 gulong handlerId; 486 487 this(void delegate(ComboBoxText) dlg) 488 { 489 this.dlg = dlg; 490 this.listeners ~= this; 491 } 492 493 void remove(OnPopupDelegateWrapper source) 494 { 495 foreach(index, wrapper; listeners) 496 { 497 if (wrapper.handlerId == source.handlerId) 498 { 499 listeners[index] = null; 500 listeners = std.algorithm.remove(listeners, index); 501 break; 502 } 503 } 504 } 505 } 506 507 /** 508 * The ::popup signal is a 509 * [keybinding signal][GtkBindingSignal] 510 * which gets emitted to popup the combo box list. 511 * 512 * The default binding for this signal is Alt+Down. 513 * 514 * Since: 2.12 515 */ 516 gulong addOnPopup(void delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 517 { 518 auto wrapper = new OnPopupDelegateWrapper(dlg); 519 wrapper.handlerId = Signals.connectData( 520 this, 521 "popup", 522 cast(GCallback)&callBackPopup, 523 cast(void*)wrapper, 524 cast(GClosureNotify)&callBackPopupDestroy, 525 connectFlags); 526 return wrapper.handlerId; 527 } 528 529 extern(C) static void callBackPopup(GtkComboBoxText* comboboxTextStruct,OnPopupDelegateWrapper wrapper) 530 { 531 wrapper.dlg(wrapper.outer); 532 } 533 534 extern(C) static void callBackPopupDestroy(OnPopupDelegateWrapper wrapper, GClosure* closure) 535 { 536 wrapper.remove(wrapper); 537 } 538 539 /** 540 */ 541 542 /** */ 543 public static GType getType() 544 { 545 return gtk_combo_box_text_get_type(); 546 } 547 548 /** 549 * Appends @text to the list of strings stored in @combo_box. 550 * If @id is non-%NULL then it is used as the ID of the row. 551 * 552 * This is the same as calling gtk_combo_box_text_insert() with a 553 * position of -1. 554 * 555 * Params: 556 * id = a string ID for this value, or %NULL 557 * text = A string 558 * 559 * Since: 2.24 560 */ 561 public void append(string id, string text) 562 { 563 gtk_combo_box_text_append(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text)); 564 } 565 566 /** 567 * Appends @text to the list of strings stored in @combo_box. 568 * 569 * This is the same as calling gtk_combo_box_text_insert_text() with a 570 * position of -1. 571 * 572 * Params: 573 * text = A string 574 * 575 * Since: 2.24 576 */ 577 public void appendText(string text) 578 { 579 gtk_combo_box_text_append_text(gtkComboBoxText, Str.toStringz(text)); 580 } 581 582 /** 583 * Returns the currently active string in @combo_box, or %NULL 584 * if none is selected. If @combo_box contains an entry, this 585 * function will return its contents (which will not necessarily 586 * be an item from the list). 587 * 588 * Returns: a newly allocated string containing the 589 * currently active text. Must be freed with g_free(). 590 * 591 * Since: 2.24 592 */ 593 public string getActiveText() 594 { 595 auto retStr = gtk_combo_box_text_get_active_text(gtkComboBoxText); 596 597 scope(exit) Str.freeString(retStr); 598 return Str.toString(retStr); 599 } 600 601 /** 602 * Inserts @text at @position in the list of strings stored in @combo_box. 603 * If @id is non-%NULL then it is used as the ID of the row. See 604 * #GtkComboBox:id-column. 605 * 606 * If @position is negative then @text is appended. 607 * 608 * Params: 609 * position = An index to insert @text 610 * id = a string ID for this value, or %NULL 611 * text = A string to display 612 * 613 * Since: 3.0 614 */ 615 public void insert(int position, string id, string text) 616 { 617 gtk_combo_box_text_insert(gtkComboBoxText, position, Str.toStringz(id), Str.toStringz(text)); 618 } 619 620 /** 621 * Inserts @text at @position in the list of strings stored in @combo_box. 622 * 623 * If @position is negative then @text is appended. 624 * 625 * This is the same as calling gtk_combo_box_text_insert() with a %NULL 626 * ID string. 627 * 628 * Params: 629 * position = An index to insert @text 630 * text = A string 631 * 632 * Since: 2.24 633 */ 634 public void insertText(int position, string text) 635 { 636 gtk_combo_box_text_insert_text(gtkComboBoxText, position, Str.toStringz(text)); 637 } 638 639 /** 640 * Prepends @text to the list of strings stored in @combo_box. 641 * If @id is non-%NULL then it is used as the ID of the row. 642 * 643 * This is the same as calling gtk_combo_box_text_insert() with a 644 * position of 0. 645 * 646 * Params: 647 * id = a string ID for this value, or %NULL 648 * text = a string 649 * 650 * Since: 2.24 651 */ 652 public void prepend(string id, string text) 653 { 654 gtk_combo_box_text_prepend(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text)); 655 } 656 657 /** 658 * Prepends @text to the list of strings stored in @combo_box. 659 * 660 * This is the same as calling gtk_combo_box_text_insert_text() with a 661 * position of 0. 662 * 663 * Params: 664 * text = A string 665 * 666 * Since: 2.24 667 */ 668 public void prependText(string text) 669 { 670 gtk_combo_box_text_prepend_text(gtkComboBoxText, Str.toStringz(text)); 671 } 672 673 /** 674 * Removes the string at @position from @combo_box. 675 * 676 * Params: 677 * position = Index of the item to remove 678 * 679 * Since: 2.24 680 */ 681 public void remove(int position) 682 { 683 gtk_combo_box_text_remove(gtkComboBoxText, position); 684 } 685 686 /** 687 * Removes all the text entries from the combo box. 688 * 689 * Since: 3.0 690 */ 691 public override void removeAll() 692 { 693 gtk_combo_box_text_remove_all(gtkComboBoxText); 694 } 695 }