Re: automatic scrolling window (scrolledwindow)



Hi David,
David Kinyanjui wrote:
Anyhow, I'm still trying to find help on how to automatically
make the scrollbars in a scrolledwindow (especially
the vertical scrollbar), position itself such that the current
focused widget (grabbing focus) is always visible at the bottom of the scrolled window.

I think this does what you want. Click on a button to warp the focus to another random button. The window will scroll to show the focussed button. Use cursor up and down to navigate, and the window will auto-scroll.

John



#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

#define NUM_BUTTONS (100)

void button_click( GtkWidget *button, GtkWidget *all_buttons[] )
{
       int n = rand() % NUM_BUTTONS;

       gtk_widget_grab_focus( all_buttons[n] );
}

int main( int argc, char **argv )
{
       GtkWidget *window;
       GtkWidget *swin;
       GtkWidget *vbox;
       GtkAdjustment *adj;
       int i;

       GtkWidget *all_buttons[NUM_BUTTONS];

       gtk_set_locale();
       gtk_init( &argc, &argv );

       window = gtk_window_new( GTK_WINDOW_TOPLEVEL );

       swin = gtk_scrolled_window_new( NULL, NULL );
       gtk_container_add( GTK_CONTAINER( window ), swin );

       vbox = gtk_vbox_new( FALSE, 2 );
       gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( swin ),
               vbox );

       for( i = 0; i < NUM_BUTTONS; i++ ) {
               char text[256];

               snprintf( text, 256, "button %d", i );
               all_buttons[i] = gtk_button_new_with_label( text );
gtk_box_pack_end( GTK_BOX( vbox ), all_buttons[i], FALSE, FALSE, 0 );
               gtk_signal_connect( GTK_OBJECT( all_buttons[i] ), "clicked",
                       GTK_SIGNAL_FUNC( button_click ), all_buttons );
       }

adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW( swin ) );
       gtk_container_set_focus_vadjustment( GTK_CONTAINER( vbox ), adj );

       gtk_widget_show_all( window );

       gtk_main();

       return( 0 );
}





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