<< Back to man.ChinaUnix.net

8.3. Adjustment Internals

Ok, you say, that's nice, but what if I want to create my own handlers to respond when the user adjusts a range widget or a spin button, and how do I get at the value of the adjustment in these handlers? To answer these questions and more, let's start by taking a look at struct _GtkAdjustment itself:

struct _GtkAdjustment
  GtkObject parent_instance;
  gdouble lower;
  gdouble upper;
  gdouble value;
  gdouble step_increment;
  gdouble page_increment;
  gdouble page_size;

If you don't like to poke directly at struct internals like a real C programmer, you can use the following accessor to inspect the value of an adjustment:

gdouble	gtk_adjustment_get_value( GtkAdjustment *adjustment);

Since, when you set the value of an Adjustment, you generally want the change to be reflected by every widget that uses this adjustment, GTK provides this convenience function to do this:

void gtk_adjustment_set_value( GtkAdjustment *adjustment,
                               gdouble       value );

As mentioned earlier, Adjustment is a subclass of Object just like all the various widgets, and thus it is able to emit signals. 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. Here's the definition of this signal in struct _GtkAdjustmentClass:

  void (* value_changed) (GtkAdjustment *adjustment);

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 gtk_adjustment_set_value(). So, 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:

void cb_rotate_picture (GtkAdjustment *adj, GtkWidget *picture)
  set_picture_rotation (picture, gtk_adjustment_get_value (adj));

and connect it to the scale widget's adjustment like this:

g_signal_connect (G_OBJECT (adj), "value_changed",
	          G_CALLBACK (cb_rotate_picture), (gpointer) picture);

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, which looks like this:

  void (* changed) (GtkAdjustment *adjustment);

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.

You probably won't ever need to attach a handler to this signal, unless you're writing a new type of range widget. However, if you change any of the values in a Adjustment directly, you should emit this signal on it to reconfigure whatever widgets are using it, like this:

g_signal_emit_by_name (G_OBJECT (adjustment), "changed");

Now go forth and adjust!