Re: Feedback updating scrollbar



>I have a scrollbar that represents the position in a file. When the user
>changes the position, the program reacts via the callback. Easy. 
>
>But when the position in the file changes for other reasons, and the
>program therefore sets the value of the scrollbar accordingly (using
>gtk_adjustment_set_value), the callback is also called. What with rounding
>errors, this means that the position is changed yet again, but not quite
>to the right value. 
>
>How can I distinguish between the callback being a reaction to user input,
>and the callback being the reaction to gtk_adjustment_set_value? 

Standard Model-View-Controller-meets-GTK question. Maintain an
independent variable indicating position in the file. Keep the
adjustment and this other value in sync. Some pseudo-code:

gfloat file_position;
GtkAdjustment *scrollbar_adjustment;

gint
adjustment_changed_handler (GtkAdjustment *adj, GtkEventFoobar *ev)

{
  gfloat adjustment_value = gtk_adjustment_get_value (adj);
   
  if (adjustment_value != file_position) {
       ... adjust the file position ...
  }

  return TRUE;
}

void
other_file_position_changed_handler (...)
{
  gfloat adjustment_value = gtk_adjustment_get_value (scrollbar_adjustment);

  if (adjustment_value != file_position) {
       gtk_adjustment_set_value (scrollbar_adjustment, file_position);
  }
}    

--p

ps. sometimes, i feel it would be useful to have a version of
    GtkAdjustment that had only a pointer to gfloat, rather than its
    own copy of the value.




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