Ruby/Gtk Tutorial | ||
---|---|---|
<<< Previous | Adjustments |
Ok, what if I want to create my own handlers to respond to when the user adjusts a range widget, or a spin button?
First of all, you can read and change the value of an adjustment with the value method:
require 'gtk' adj = Gtk::Adjustment.new 0, 0, 10, 1, 0, 0 puts adj.value adj.value = 3 puts adj.value adj.set_value 4 puts adj.value |
As mentioned earlier, adjustment emit signals, just like all Gtk::Objects. This is, of course, why updates happen automagically when you share an adjustment object between a scrollbar and another adjustable widget; all adjustable widgets connect signal handlers to their adjustment's value_changed signal, as can your program.
The various widgets that use the Adjustment object will emit this signal on an adjustment whenever they change its value. This happens both when user input causes the slider to move on a range widget, as well as when the program explicitly changes the value with set_value.
For example, if you have a scale widget, and you want to change the rotation of a picture whenever its value changes, you would create a callback like this:
adj.signal_connect('value_changed') { picture_rotation(picture, adj.value) } |
What about when a widget reconfigures the upper or lower fields of its adjustment, such as when a user adds more text to a text widget? In this case, it emits the changed signal.
Range widgets typically connect a handler to this signal, which changes their appearance to reflect the change - for example, the size of the slider in a scrollbar will grow or shrink in inverse proportion to the difference between the lower and upper values of its adjustment.
<<< Previous | Home | |
Using Adjustments the Easy Way | Up |