Scrolledwindow & canvas scrolling problems



I am trying to scroll the canvas inside a scrolledwindow object. Here my
hacked demo app, based on the gtk tutorial program, displays my problem.

Scrolling the canvas actually seems to move the canvas item in it.
Obviously there is a problem with my code, but none of the examples
involve the canvas.

Is this app being too simplistic ?

/Lukas

CC = gcc
LIBS=-rdynamic -L/usr/lib -L/usr/X11R6/lib -lgnomeui -lart_lgpl -lgdk_imlib -lSM -lICE -lgtk -lgdk -lgmodule -lXext -lX11 -lgnome -lgnomesupport -lesd -lm -ldb1 -lglib -ldl
#LIBS=`gnome-config --libs gnomeui`
scrolledwin: scrolledwin.c 
	$(CC) `gnome-config --cflags gnomeui`  scrolledwin.c -o scrolledwin $(LIBS)

clean: 
	rm -f *.o scrolledwin
/* example-start scrolledwin scrolledwin.c */

#include <gtk/gtk.h>
#include <libgnomeui/gnome-canvas.h>

void destroy(GtkWidget *widget, gpointer data)
{
    gtk_main_quit();
}

int main (int argc, char *argv[])
{
    static GtkWidget *window;
    GtkWidget *scrolled_window;
    GtkWidget* canvas;
    GnomeCanvasGroup *root;
    GtkWidget* button;
    
    gtk_init (&argc, &argv);
    
    /* Create a new dialog window for the scrolled window to be
     * packed into. A dialog is just like a normal window except it has a 
     * vbox and a horizontal separator packed into it. It's just a shortcut
     * for creating dialogs */
    window = gtk_dialog_new ();
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
			(GtkSignalFunc) destroy, NULL);
    gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
    gtk_widget_set_usize(window, 300, 300);
    
    /* create a new scrolled window. */
    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    
    /* the policy is one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
     * GTK_POLICY_AUTOMATIC will automatically decide whether you need
     * scrollbars, whereas GTK_POLICY_ALWAYS will always leave the scrollbars
     * there.  The first one is the horizontal scrollbar, the second, 
     * the vertical. */
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                    GTK_POLICY_AUTOMATIC,
                                    GTK_POLICY_AUTOMATIC);
    /* The dialog window is created with a vbox packed into it. */								
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, 
			TRUE, TRUE, 0);
    
    /*create canvas */
    canvas = gnome_canvas_new();
    
    root = gnome_canvas_root (GNOME_CANVAS (canvas));
    gnome_canvas_item_new (root,
                           gnome_canvas_rect_get_type (),
                           "x1", 200.0,
                           "y1", 230.0,
                           "x2", 270.0,
                           "y2", 260.0,
                           "outline_color", "red",
                           "fill_color", "yellow",
                           "width_pixels", 8,
                           NULL);
    gnome_canvas_set_scroll_region (GNOME_CANVAS (canvas), 0, 0, 600, 450);

    gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window),
                               gtk_layout_get_hadjustment(GTK_LAYOUT(canvas)));
    gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window),
                               gtk_layout_get_vadjustment(GTK_LAYOUT(canvas)));
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
                                          canvas);

    gtk_widget_show (canvas);
    gtk_widget_show (scrolled_window);
    
    /* Add a "close" button to the bottom of the dialog */
    button = gtk_button_new_with_label ("close");
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			       (GtkSignalFunc) gtk_widget_destroy,
			       GTK_OBJECT (window));
    
    /* this makes it so the button is the default. */
    GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), button, TRUE, TRUE, 0);
    
    /* This grabs this button to be the default button. Simply hitting
     * the "Enter" key will cause this button to activate. */
    gtk_widget_grab_default (button);
    gtk_widget_show (button);
    
    gtk_widget_show (window);
    
    gtk_main();
    
    return(0);
}
/* example-end */


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