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