Re: [gtk-list] Re: Changing gtkadjustment upper and lower values



On Thu, 4 Mar 1999, Allan Black wrote:

> > >>         Is there a simple method to change the upper and lower value of a
> > >> GtkAdjustment?  The only method I can see is by accessing the upper and
> > > How about gtk_adjustment_clamp_page ()?
> > This doesn't seem to do anything for me here.  I'm using the adjustment with a
> > spin button, and it just keeps spinning up to the initial limit i set when
> > creating the adjustment.  I'm using gtk+ 1.2.0.  I have successfully reset its
> > limits by accessing its values directly, but that doesn't seem to be proper OO
> > to me.
> 
> >From the GTK+ tutorial:
> 
> 	The first thing you should know is that there aren't any handy-dandy
> 	macros or accessor functions for getting the value out of a
> 	GtkAdjustment, so you'll have to (horror of horrors) do it like a
> 	real C programmer.
> 
> .... so I guess the same applies to setting the adjustment fields :-)
> 
> After you change the fields of the adjustment you have to tell the widget
> that it has changed. You do this by calling:
> 
> 	gtk_signal_emit_by_name(GTK_OBJECT(adjustment), "changed");
> 
> The spin button (as do other range widgets) has a signal handler connected
> to the "changed" signal.

this actually isn't the preferred way to alter object values in OO systems,
but you're right that GtkAdjustment falls short in providing the correct
accessors for this functionality.
also signals should pretty much *never* be emitted from user code directly
and even the GtkAdjustment provides functions to trigger the signal
emissions:
gtk_adjustment_value_changed() and gtk_adjustment_changed().
(the gtkspinbutton.c code has to be changed in this regard).

ok, so here goes the correct accessor for the gtkadjustment fields
(warning, untested source, written off head):

void
gtk_adjustment_configure (GtkAdjustment *adjustment,
                          gfloat         lower,
                          gfloat         upper,
                          gfloat         step_increment,
                          gfloat         page_increment,
                          gfloat         page_size)
{
  g_return_if_fail (adjustment != NULL);
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
  g_return_if_fail (lower < upper);
  
  if (lower != adjustment->lower ||
      upper != adjustment->upper ||
      step_increment != adjustment->step_increment ||
      page_increment != adjustment->page_increment ||
      page_size != adjustment->page_size)
    {
      gboolean need_value_changed;
      
      adjustment->lower = lower;
      adjustment->upper = upper;
      adjustment->step_increment = step_increment;
      adjustment->page_increment = page_increment;
      adjustment->page_size = page_size;
      
      need_value_changed = (adjustment->value < adjustment->lower ||
                            adjustment->value > adjustment->upper);
      if (need_value_changed)
        adjustment->value = CLAMP (adjustment->value,
                                   adjustment->lower,
                                   adjustment->upper);
      gtk_adjustment_changed (adjustment);
      if (need_value_changed)
        gtk_adjustment_value_changed (adjustment);
    }
}



this function can be used in application code directly, until gtk+ provides
this accessor.

> 
> Allan
> 

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]