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.SpinButton; 26 27 private import glib.ConstructionException; 28 private import gobject.ObjectG; 29 private import gobject.Signals; 30 private import gtk.Adjustment; 31 private import gtk.Entry; 32 private import gtk.OrientableIF; 33 private import gtk.OrientableT; 34 private import gtk.Widget; 35 public import gtkc.gdktypes; 36 private import gtkc.gtk; 37 public import gtkc.gtktypes; 38 39 40 /** 41 * A #GtkSpinButton is an ideal way to allow the user to set the value of 42 * some attribute. Rather than having to directly type a number into a 43 * #GtkEntry, GtkSpinButton allows the user to click on one of two arrows 44 * to increment or decrement the displayed value. A value can still be 45 * typed in, with the bonus that it can be checked to ensure it is in a 46 * given range. 47 * 48 * The main properties of a GtkSpinButton are through an adjustment. 49 * See the #GtkAdjustment section for more details about an adjustment's 50 * properties. 51 * 52 * ## Using a GtkSpinButton to get an integer 53 * 54 * |[<!-- language="C" --> 55 * // Provides a function to retrieve an integer value from a GtkSpinButton 56 * // and creates a spin button to model percentage values. 57 * 58 * gint 59 * grab_int_value (GtkSpinButton *button, 60 * gpointer user_data) 61 * { 62 * return gtk_spin_button_get_value_as_int (button); 63 * } 64 * 65 * void 66 * create_integer_spin_button (void) 67 * { 68 * 69 * GtkWidget *window, *button; 70 * GtkAdjustment *adjustment; 71 * 72 * adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0); 73 * 74 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 75 * gtk_container_set_border_width (GTK_CONTAINER (window), 5); 76 * 77 * // creates the spinbutton, with no decimal places 78 * button = gtk_spin_button_new (adjustment, 1.0, 0); 79 * gtk_container_add (GTK_CONTAINER (window), button); 80 * 81 * gtk_widget_show_all (window); 82 * } 83 * ]| 84 * 85 * ## Using a GtkSpinButton to get a floating point value 86 * 87 * |[<!-- language="C" --> 88 * // Provides a function to retrieve a floating point value from a 89 * // GtkSpinButton, and creates a high precision spin button. 90 * 91 * gfloat 92 * grab_float_value (GtkSpinButton *button, 93 * gpointer user_data) 94 * { 95 * return gtk_spin_button_get_value (button); 96 * } 97 * 98 * void 99 * create_floating_spin_button (void) 100 * { 101 * GtkWidget *window, *button; 102 * GtkAdjustment *adjustment; 103 * 104 * adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0); 105 * 106 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 107 * gtk_container_set_border_width (GTK_CONTAINER (window), 5); 108 * 109 * // creates the spinbutton, with three decimal places 110 * button = gtk_spin_button_new (adjustment, 0.001, 3); 111 * gtk_container_add (GTK_CONTAINER (window), button); 112 * 113 * gtk_widget_show_all (window); 114 * } 115 * ]| 116 */ 117 public class SpinButton : Entry, OrientableIF 118 { 119 /** the main Gtk struct */ 120 protected GtkSpinButton* gtkSpinButton; 121 122 /** Get the main Gtk struct */ 123 public GtkSpinButton* getSpinButtonStruct() 124 { 125 return gtkSpinButton; 126 } 127 128 /** the main Gtk struct as a void* */ 129 protected override void* getStruct() 130 { 131 return cast(void*)gtkSpinButton; 132 } 133 134 protected override void setStruct(GObject* obj) 135 { 136 gtkSpinButton = cast(GtkSpinButton*)obj; 137 super.setStruct(obj); 138 } 139 140 /** 141 * Sets our main struct and passes it to the parent class. 142 */ 143 public this (GtkSpinButton* gtkSpinButton, bool ownedRef = false) 144 { 145 this.gtkSpinButton = gtkSpinButton; 146 super(cast(GtkEntry*)gtkSpinButton, ownedRef); 147 } 148 149 // add the Orientable capabilities 150 mixin OrientableT!(GtkSpinButton); 151 152 /** 153 */ 154 155 public static GType getType() 156 { 157 return gtk_spin_button_get_type(); 158 } 159 160 /** 161 * Creates a new #GtkSpinButton. 162 * 163 * Params: 164 * adjustment = the #GtkAdjustment object that this spin 165 * button should use, or %NULL 166 * climbRate = specifies how much the spin button changes when an arrow 167 * is clicked on 168 * digits = the number of decimal places to display 169 * 170 * Return: The new spin button as a #GtkWidget 171 * 172 * Throws: ConstructionException GTK+ fails to create the object. 173 */ 174 public this(Adjustment adjustment, double climbRate, uint digits) 175 { 176 auto p = gtk_spin_button_new((adjustment is null) ? null : adjustment.getAdjustmentStruct(), climbRate, digits); 177 178 if(p is null) 179 { 180 throw new ConstructionException("null returned by new"); 181 } 182 183 this(cast(GtkSpinButton*) p); 184 } 185 186 /** 187 * This is a convenience constructor that allows creation of a numeric 188 * #GtkSpinButton without manually creating an adjustment. The value is 189 * initially set to the minimum value and a page increment of 10 * @step 190 * is the default. The precision of the spin button is equivalent to the 191 * precision of @step. 192 * 193 * Note that the way in which the precision is derived works best if @step 194 * is a power of ten. If the resulting precision is not suitable for your 195 * needs, use gtk_spin_button_set_digits() to correct it. 196 * 197 * Params: 198 * min = Minimum allowable value 199 * max = Maximum allowable value 200 * step = Increment added or subtracted by spinning the widget 201 * 202 * Return: The new spin button as a #GtkWidget 203 * 204 * Throws: ConstructionException GTK+ fails to create the object. 205 */ 206 public this(double min, double max, double step) 207 { 208 auto p = gtk_spin_button_new_with_range(min, max, step); 209 210 if(p is null) 211 { 212 throw new ConstructionException("null returned by new_with_range"); 213 } 214 215 this(cast(GtkSpinButton*) p); 216 } 217 218 /** 219 * Changes the properties of an existing spin button. The adjustment, 220 * climb rate, and number of decimal places are all changed accordingly, 221 * after this function call. 222 * 223 * Params: 224 * adjustment = a #GtkAdjustment 225 * climbRate = the new climb rate 226 * digits = the number of decimal places to display in the spin button 227 */ 228 public void configure(Adjustment adjustment, double climbRate, uint digits) 229 { 230 gtk_spin_button_configure(gtkSpinButton, (adjustment is null) ? null : adjustment.getAdjustmentStruct(), climbRate, digits); 231 } 232 233 /** 234 * Get the adjustment associated with a #GtkSpinButton 235 * 236 * Return: the #GtkAdjustment of @spin_button 237 */ 238 public Adjustment getAdjustment() 239 { 240 auto p = gtk_spin_button_get_adjustment(gtkSpinButton); 241 242 if(p is null) 243 { 244 return null; 245 } 246 247 return ObjectG.getDObject!(Adjustment)(cast(GtkAdjustment*) p); 248 } 249 250 /** 251 * Fetches the precision of @spin_button. See gtk_spin_button_set_digits(). 252 * 253 * Return: the current precision 254 */ 255 public uint getDigits() 256 { 257 return gtk_spin_button_get_digits(gtkSpinButton); 258 } 259 260 /** 261 * Gets the current step and page the increments used by @spin_button. See 262 * gtk_spin_button_set_increments(). 263 * 264 * Params: 265 * step = location to store step increment, or %NULL 266 * page = location to store page increment, or %NULL 267 */ 268 public void getIncrements(out double step, out double page) 269 { 270 gtk_spin_button_get_increments(gtkSpinButton, &step, &page); 271 } 272 273 /** 274 * Returns whether non-numeric text can be typed into the spin button. 275 * See gtk_spin_button_set_numeric(). 276 * 277 * Return: %TRUE if only numeric text can be entered 278 */ 279 public bool getNumeric() 280 { 281 return gtk_spin_button_get_numeric(gtkSpinButton) != 0; 282 } 283 284 /** 285 * Gets the range allowed for @spin_button. 286 * See gtk_spin_button_set_range(). 287 * 288 * Params: 289 * min = location to store minimum allowed value, or %NULL 290 * max = location to store maximum allowed value, or %NULL 291 */ 292 public void getRange(out double min, out double max) 293 { 294 gtk_spin_button_get_range(gtkSpinButton, &min, &max); 295 } 296 297 /** 298 * Returns whether the values are corrected to the nearest step. 299 * See gtk_spin_button_set_snap_to_ticks(). 300 * 301 * Return: %TRUE if values are snapped to the nearest step 302 */ 303 public bool getSnapToTicks() 304 { 305 return gtk_spin_button_get_snap_to_ticks(gtkSpinButton) != 0; 306 } 307 308 /** 309 * Gets the update behavior of a spin button. 310 * See gtk_spin_button_set_update_policy(). 311 * 312 * Return: the current update policy 313 */ 314 public GtkSpinButtonUpdatePolicy getUpdatePolicy() 315 { 316 return gtk_spin_button_get_update_policy(gtkSpinButton); 317 } 318 319 /** 320 * Get the value in the @spin_button. 321 * 322 * Return: the value of @spin_button 323 */ 324 public double getValue() 325 { 326 return gtk_spin_button_get_value(gtkSpinButton); 327 } 328 329 /** 330 * Get the value @spin_button represented as an integer. 331 * 332 * Return: the value of @spin_button 333 */ 334 public int getValueAsInt() 335 { 336 return gtk_spin_button_get_value_as_int(gtkSpinButton); 337 } 338 339 /** 340 * Returns whether the spin button’s value wraps around to the 341 * opposite limit when the upper or lower limit of the range is 342 * exceeded. See gtk_spin_button_set_wrap(). 343 * 344 * Return: %TRUE if the spin button wraps around 345 */ 346 public bool getWrap() 347 { 348 return gtk_spin_button_get_wrap(gtkSpinButton) != 0; 349 } 350 351 /** 352 * Replaces the #GtkAdjustment associated with @spin_button. 353 * 354 * Params: 355 * adjustment = a #GtkAdjustment to replace the existing adjustment 356 */ 357 public void setAdjustment(Adjustment adjustment) 358 { 359 gtk_spin_button_set_adjustment(gtkSpinButton, (adjustment is null) ? null : adjustment.getAdjustmentStruct()); 360 } 361 362 /** 363 * Set the precision to be displayed by @spin_button. Up to 20 digit precision 364 * is allowed. 365 * 366 * Params: 367 * digits = the number of digits after the decimal point to be displayed for the spin button’s value 368 */ 369 public void setDigits(uint digits) 370 { 371 gtk_spin_button_set_digits(gtkSpinButton, digits); 372 } 373 374 /** 375 * Sets the step and page increments for spin_button. This affects how 376 * quickly the value changes when the spin button’s arrows are activated. 377 * 378 * Params: 379 * step = increment applied for a button 1 press. 380 * page = increment applied for a button 2 press. 381 */ 382 public void setIncrements(double step, double page) 383 { 384 gtk_spin_button_set_increments(gtkSpinButton, step, page); 385 } 386 387 /** 388 * Sets the flag that determines if non-numeric text can be typed 389 * into the spin button. 390 * 391 * Params: 392 * numeric = flag indicating if only numeric entry is allowed 393 */ 394 public void setNumeric(bool numeric) 395 { 396 gtk_spin_button_set_numeric(gtkSpinButton, numeric); 397 } 398 399 /** 400 * Sets the minimum and maximum allowable values for @spin_button. 401 * 402 * If the current value is outside this range, it will be adjusted 403 * to fit within the range, otherwise it will remain unchanged. 404 * 405 * Params: 406 * min = minimum allowable value 407 * max = maximum allowable value 408 */ 409 public void setRange(double min, double max) 410 { 411 gtk_spin_button_set_range(gtkSpinButton, min, max); 412 } 413 414 /** 415 * Sets the policy as to whether values are corrected to the 416 * nearest step increment when a spin button is activated after 417 * providing an invalid value. 418 * 419 * Params: 420 * snapToTicks = a flag indicating if invalid values should be corrected 421 */ 422 public void setSnapToTicks(bool snapToTicks) 423 { 424 gtk_spin_button_set_snap_to_ticks(gtkSpinButton, snapToTicks); 425 } 426 427 /** 428 * Sets the update behavior of a spin button. 429 * This determines whether the spin button is always updated 430 * or only when a valid value is set. 431 * 432 * Params: 433 * policy = a #GtkSpinButtonUpdatePolicy value 434 */ 435 public void setUpdatePolicy(GtkSpinButtonUpdatePolicy policy) 436 { 437 gtk_spin_button_set_update_policy(gtkSpinButton, policy); 438 } 439 440 /** 441 * Sets the value of @spin_button. 442 * 443 * Params: 444 * value = the new value 445 */ 446 public void setValue(double value) 447 { 448 gtk_spin_button_set_value(gtkSpinButton, value); 449 } 450 451 /** 452 * Sets the flag that determines if a spin button value wraps 453 * around to the opposite limit when the upper or lower limit 454 * of the range is exceeded. 455 * 456 * Params: 457 * wrap = a flag indicating if wrapping behavior is performed 458 */ 459 public void setWrap(bool wrap) 460 { 461 gtk_spin_button_set_wrap(gtkSpinButton, wrap); 462 } 463 464 /** 465 * Increment or decrement a spin button’s value in a specified 466 * direction by a specified amount. 467 * 468 * Params: 469 * direction = a #GtkSpinType indicating the direction to spin 470 * increment = step increment to apply in the specified direction 471 */ 472 public void spin(GtkSpinType direction, double increment) 473 { 474 gtk_spin_button_spin(gtkSpinButton, direction, increment); 475 } 476 477 /** 478 * Manually force an update of the spin button. 479 */ 480 public void update() 481 { 482 gtk_spin_button_update(gtkSpinButton); 483 } 484 485 int[string] connectedSignals; 486 487 void delegate(GtkScrollType, SpinButton)[] onChangeValueListeners; 488 /** 489 * The ::change-value signal is a [keybinding signal][GtkBindingSignal] 490 * which gets emitted when the user initiates a value change. 491 * 492 * Applications should not connect to it, but may emit it with 493 * g_signal_emit_by_name() if they need to control the cursor 494 * programmatically. 495 * 496 * The default bindings for this signal are Up/Down and PageUp and/PageDown. 497 * 498 * Params: 499 * scroll = a #GtkScrollType to specify the speed and amount of change 500 */ 501 void addOnChangeValue(void delegate(GtkScrollType, SpinButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 502 { 503 if ( "change-value" !in connectedSignals ) 504 { 505 Signals.connectData( 506 this, 507 "change-value", 508 cast(GCallback)&callBackChangeValue, 509 cast(void*)this, 510 null, 511 connectFlags); 512 connectedSignals["change-value"] = 1; 513 } 514 onChangeValueListeners ~= dlg; 515 } 516 extern(C) static void callBackChangeValue(GtkSpinButton* spinbuttonStruct, GtkScrollType scroll, SpinButton _spinbutton) 517 { 518 foreach ( void delegate(GtkScrollType, SpinButton) dlg; _spinbutton.onChangeValueListeners ) 519 { 520 dlg(scroll, _spinbutton); 521 } 522 } 523 524 int delegate(void*, SpinButton)[] onInputListeners; 525 /** 526 * The ::input signal can be used to influence the conversion of 527 * the users input into a double value. The signal handler is 528 * expected to use gtk_entry_get_text() to retrieve the text of 529 * the entry and set @new_value to the new value. 530 * 531 * The default conversion uses g_strtod(). 532 * 533 * Params: 534 * newValue = return location for the new value 535 * 536 * Return: %TRUE for a successful conversion, %FALSE if the input 537 * was not handled, and %GTK_INPUT_ERROR if the conversion failed. 538 */ 539 void addOnInput(int delegate(void*, SpinButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 540 { 541 if ( "input" !in connectedSignals ) 542 { 543 Signals.connectData( 544 this, 545 "input", 546 cast(GCallback)&callBackInput, 547 cast(void*)this, 548 null, 549 connectFlags); 550 connectedSignals["input"] = 1; 551 } 552 onInputListeners ~= dlg; 553 } 554 extern(C) static int callBackInput(GtkSpinButton* spinbuttonStruct, void* newValue, SpinButton _spinbutton) 555 { 556 return _spinbutton.onInputListeners[0](newValue, _spinbutton); 557 } 558 559 bool delegate(SpinButton)[] onOutputListeners; 560 /** 561 * The ::output signal can be used to change to formatting 562 * of the value that is displayed in the spin buttons entry. 563 * |[<!-- language="C" --> 564 * // show leading zeros 565 * static gboolean 566 * on_output (GtkSpinButton *spin, 567 * gpointer data) 568 * { 569 * GtkAdjustment *adjustment; 570 * gchar *text; 571 * int value; 572 * 573 * adjustment = gtk_spin_button_get_adjustment (spin); 574 * value = (int)gtk_adjustment_get_value (adjustment); 575 * text = g_strdup_printf ("%02d", value); 576 * gtk_entry_set_text (GTK_ENTRY (spin), text); 577 * g_free (text); 578 * 579 * return TRUE; 580 * } 581 * ]| 582 * 583 * Return: %TRUE if the value has been displayed 584 */ 585 void addOnOutput(bool delegate(SpinButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 586 { 587 if ( "output" !in connectedSignals ) 588 { 589 Signals.connectData( 590 this, 591 "output", 592 cast(GCallback)&callBackOutput, 593 cast(void*)this, 594 null, 595 connectFlags); 596 connectedSignals["output"] = 1; 597 } 598 onOutputListeners ~= dlg; 599 } 600 extern(C) static int callBackOutput(GtkSpinButton* spinbuttonStruct, SpinButton _spinbutton) 601 { 602 foreach ( bool delegate(SpinButton) dlg; _spinbutton.onOutputListeners ) 603 { 604 if ( dlg(_spinbutton) ) 605 { 606 return 1; 607 } 608 } 609 610 return 0; 611 } 612 613 void delegate(SpinButton)[] onValueChangedListeners; 614 /** 615 * The ::value-changed signal is emitted when the value represented by 616 * @spinbutton changes. Also see the #GtkSpinButton::output signal. 617 */ 618 void addOnValueChanged(void delegate(SpinButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 619 { 620 if ( "value-changed" !in connectedSignals ) 621 { 622 Signals.connectData( 623 this, 624 "value-changed", 625 cast(GCallback)&callBackValueChanged, 626 cast(void*)this, 627 null, 628 connectFlags); 629 connectedSignals["value-changed"] = 1; 630 } 631 onValueChangedListeners ~= dlg; 632 } 633 extern(C) static void callBackValueChanged(GtkSpinButton* spinbuttonStruct, SpinButton _spinbutton) 634 { 635 foreach ( void delegate(SpinButton) dlg; _spinbutton.onValueChangedListeners ) 636 { 637 dlg(_spinbutton); 638 } 639 } 640 641 void delegate(SpinButton)[] onWrappedListeners; 642 /** 643 * The ::wrapped signal is emitted right after the spinbutton wraps 644 * from its maximum to minimum value or vice-versa. 645 * 646 * Since: 2.10 647 */ 648 void addOnWrapped(void delegate(SpinButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 649 { 650 if ( "wrapped" !in connectedSignals ) 651 { 652 Signals.connectData( 653 this, 654 "wrapped", 655 cast(GCallback)&callBackWrapped, 656 cast(void*)this, 657 null, 658 connectFlags); 659 connectedSignals["wrapped"] = 1; 660 } 661 onWrappedListeners ~= dlg; 662 } 663 extern(C) static void callBackWrapped(GtkSpinButton* spinbuttonStruct, SpinButton _spinbutton) 664 { 665 foreach ( void delegate(SpinButton) dlg; _spinbutton.onWrappedListeners ) 666 { 667 dlg(_spinbutton); 668 } 669 } 670 }