Re: scrolledwindow auto scroll!



Hi David,
I think gtk_container_set_focus_vadjustment() is supposed to help here, but it's never worked that well for me. Perhaps someone could explain how to use it?

I hacked in my own scheme: look at the scrolledwindow adjustments to see what part of the window is visible, look at the widget allocation to see where it is and what size, then make the smallest change to the adjustment which will bring the widget into the visible area and signal "value_changed". Ugly code at the end.

John

David Kinyanjui wrote:
I am using a scrolledWindow and want to know how to
automatically make the scrollbars especially the
vertical one, position itself such that the last widget
set active or which is currently grabbing the focus is
always visible at the bottom of the scrolled window.

Or in case of text widgets, the last item inserted
into the text entry (within the scrolled window) is always
visible at the bottom of the scrolled window.

/* Scroll to make an xywh area visible. If the area is larger than the
* viewport, position the view at the bottom left if the xywh area ... * this is usually right for workspaces.
*/
void
workspaceview_scroll( Workspaceview *wview, int x, int y, int w, int h )
{
       const int margin = 64;          /* overscroll by this much */
       GtkAdjustment *hadj = gtk_scrolled_window_get_hadjustment(
               GTK_SCROLLED_WINDOW( wview->window ) );
       GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
               GTK_SCROLLED_WINDOW( wview->window ) );
       float nx, ny;

#ifdef DEBUG
       printf( "workspaceview_scroll: x=%d, y=%d, w=%d, h=%d\n", x, y, w, h );
#endif /*DEBUG*/

       nx = hadj->value;
       if( x + w < wview->vp.left + margin )
               nx = IM_MAX( 0, x - margin );
       else if( x > IM_RECT_RIGHT( &wview->vp ) - margin )
               nx = IM_MIN( IM_MAX( 0, x - margin ),
                       x + w - wview->vp.width + margin );

       ny = vadj->value;
       if( y + h < wview->vp.top + margin )
               ny = IM_MAX( 0, y - margin );
       else if( y > IM_RECT_BOTTOM( &wview->vp ) - margin )
               ny = IM_MIN( IM_MAX( 0, y - margin ),
                       y + h - wview->vp.height + margin );

       adjustments_set_value( hadj, vadj, nx, ny );
}




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