My objective is to make the slider in the scale be in sync with the value in the spin and vice-versa. I've tried some alternatives, but I am not finding a way of getting the value where the scale is so that I can set the new spin position.
The little exercise has a small check button that should sync both widgets. I am trying to do this sync through the toggled function, but I am stuck now.
Here is the code so far:
uses
Gtk
class TestWindow:Window
_spin:Gtk.SpinButton
_scale:Gtk.Scale
construct ()
//General aspects of the window
title = "Spin and scale"
window_position = WindowPosition.CENTER
destroy.connect( Gtk.main_quit )
//create the spin button
spinAdjustment:Gtk.Adjustment = new Gtk.Adjustment(50, 0, 100, 1, 5, 0)
scaleAdjustment:Gtk.Adjustment = new Gtk.Adjustment(50, 0, 100, 1, 5, 0)
_spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)
//create the horizontal scale
_scale:Scale = new Gtk.Scale(Gtk.Orientation.HORIZONTAL, scaleAdjustment);
//create the check button
var check = new Gtk.CheckButton.with_label ("Sync both scales!")
check.toggled.connect(toggled)
// organize it in a box
var box = new Box( Orientation.VERTICAL, 0 )
box.pack_start(_spin, true, true, 0 )
box.pack_start(_scale, true, true, 0)
box.pack_start(check, true, true, 0)
add( box )
def toggled ()
message(_spin.get_value().to_string())
//if _scale.get_value_pos() != _spin.get_value()
// _scale.set_value_pos(_spin.get_value())
init
Gtk.init( ref args )
var test = new TestWindow()
test.show_all()
Gtk.main()
Can anyone give me a little pointer on how to solve this?
This is a working example based on your code:
Your example is nice and obviously when you run it you will understand how the user interaction can be improved.
For the Genie code itself there are a couple of points:
_spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)
should just be_spin = new Gtk.SpinButton(spinAdjustment, 1.0,1)
because you have already declared_spin
. The same applies to_scale
.get_value_pos
method ofGtk.Scale
and this causes a type mismatch error:Equality operation: 'double' and 'Gtk.PositionType' are incompatible
. The Vala documentation for Gtk.Scale says "To use it, you’ll probably want to investigate the methods on its base class, Range, in addition to the methods for GtkScale itself." UnderGtk.Range
there is a get_value method that returns adouble
, the same type you need to match theget_value
from theSpinButton
. This is the concept of inheritance, which is already being used for theWindow