Re: Scrolling a Cairo drawing-area or Gtk layout



I did some testing of the code and also found that getting scroll bars to appear was not so easy. The only way that I could get them to appear was to modify the code with a container layout of:
layout2->viewport->scrolled window->window2.

Here is the section of the code that I altered.
*********
 window2 =
gtk_window_new(GTK_WINDOW_TOPLEVEL);

  layout2 =
gtk_layout_new(NULL, NULL);

GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
GtkWidget *viewport = gtk_viewport_new (NULL,NULL);

gtk_scrolled_window_set_policy (sw, GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS);
gtk_widget_set_size_request (layout2, 800, 600);

g_signal_connect(G_OBJECT(layout2), "draw",
G_CALLBACK(on_draw_event), NULL);


g_signal_connect(G_OBJECT(layout2), "destroy",
G_CALLBACK(gtk_widget_destroy), NULL);


gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);


gtk_window_set_title(GTK_WINDOW(window2), "Graph");

gtk_container_add(GTK_CONTAINER (viewport), layout2);
gtk_container_add(GTK_CONTAINER (sw), viewport);

gtk_container_add(GTK_CONTAINER (window2), sw);


gtk_widget_show_all(window2);

***********************

I am using GTK3 and the documentation says that specifically including the viewport is not needed as it *should* be included automatically when it is needed. But that was not my experience in this case.

I also agree with Damien that the goal of the code is unclear. You cannot scroll to a part of the plot that is not drawn. In the code above, you can only scroll around within the dimensions of layout2. Is that what you wanted?

jim charlton

On 15-07-27 01:10 AM, Damien Caliste wrote:
Hello,

Le 21/07/2015, Roger Matthews <roger matthews hotmail com> a écrit :
which results from a simple straight line equation, and*/ /* that
extends beyond the bounds of the window.
Reading your code quickly, it seems to me that, you're drawing indeed
the line outside the drawing area, *but* the drawing area has the size
of your window, so GTK is not displaying any scrollbars.

In my opinion, you should :
- in main, declare your drawing area to a fixed size (let say
   1200×1200) and declare your window size at 600×400 as you're doing ;
- in the drawing method (do_drawing()), don't use the window size, but
   the drawing size as bounds from your drwaing commands. It's useless
   to draw lines or whatever outside the cairo drawing area, they will
   be cropped anyway. So don't reach for the top level there, use the
   size of the widget itself.

As a schemme :
- your implementation : |#####|
- the way to do it : |----######----------------|
   - # means GTK scrollview, - means drawing area
   - call cairo drawing primitives in the coordinates of the drawing area
     (-), but not outside.

Damien.
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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