RE: Scrolling a Cairo drawing-area or Gtk layout
Hi Damien Caliste, Jim Charlton, and Pramithesh, that has worked and here is the code I used:window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_default_size(GTK_WINDOW(window2), 1000, 800);grid2 = gtk_grid_new();darea = gtk_drawing_area_new();gtk_widget_set_size_request(darea, 3000, 2000);gtk_grid_attach(GTK_GRID (grid2), darea, 0, 0, 1, 1);g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(on_draw_event), NULL);g_signal_connect(G_OBJECT(darea), "destroy", G_CALLBACK(gtk_widget_destroy), NULL);swin2 = gtk_scrolled_window_new(NULL, NULL);gtk_container_set_border_width(GTK_CONTAINER (swin2), 5);gtk_container_add(GTK_CONTAINER (swin2), grid2);box2 = gtk_box_new (TRUE, 5);gtk_box_pack_start (GTK_BOX (box2), swin2, TRUE, TRUE, 5);gtk_container_add (GTK_CONTAINER (window2), box2);gtk_widget_show_all (window2); > Date: Mon, 27 Jul 2015 10:10:27 +0200 > From: damien.cali...@cea.fr > To: gtk-app-devel-list@gnome.org > Subject: Re: Scrolling a Cairo drawing-area or Gtk layout > > Hello, > > Le 21/07/2015, Roger Matthews 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 ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
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 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 ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Scrolling a Cairo drawing-area or Gtk layout
Hello, Le 21/07/2015, Roger Matthews 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
Scrolling a Cairo drawing-area or Gtk layout
/*Hello, if there is anybody willing to help with this problem I would be very appreciative and thanks in advance. Here is some code and my problem is this: */ /*I create two windows, one with spinbuttons for parameter entry, and the other shows a cairo line which results from a simple straight line equation, and*/ /* that extends beyond the bounds of the window. The first window is scrollable and I would like to be able to scroll to the end of the line in the second window*/ /*but don't seem to be able to work out how. I've tried putting the darea into the scrolled-window as per window1, it compiles and runs but has no scrollbars.*/ /*I've also tried gtk_container_add (darea, swin, layout, and many combination thereof) to window2.*/ /*Having read that a gtk layout supports scrolling natively I assumed it would do this easily. If I declare the layout as GtkWidget then gtk_container_add accepts*/ /*the layout but produces no scrollbars and if I declare it as GtkLayout then gtk_container_add won't accept it, if I use gtk_layout_put there's problems too.*/ /*If an example of a gtk layout (on one, or more windows) with scrolling (on one, or more windows) could be provided I would be happy and could stop tearing*/ /*my hair out, thanks in advance, Roger Matthews.*/ #include #include #include #include /*Global Variables*/ int i; double yvalue1[1001]; int yvalue1_param1 = 1; float yvalue1_param2 = 3.0; float const_asp_ratio = 1.0; /***GTK CALLBACK FUNCTIONS**/ static void get_new_number_integer_yvalue1_param1(GtkWidget *widget, gpointer data) { yvalue1_param1 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)); write_yvalue1params(); printf("yvalue1_param1 = %d\n", yvalue1_param1); } // static void get_new_number_float_yvalue1_param2(GtkWidget *widget, gpointer data) { yvalue1_param2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widget)); write_yvalue1params(); printf("yvalue1_param2 = %f\n", yvalue1_param2); } // static void get_new_number_float_const_asp_ratio(GtkWidget *widget, gpointer data) { const_asp_ratio = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widget)); write_yvalue1params(); printf("const_asp_ratio = %f\n", const_asp_ratio); } // static void do_drawing(cairo_t *, GtkWidget *); static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer user_data) { do_drawing(cr, widget); return FALSE; } static void do_drawing(cairo_t *cr, GtkWidget *widget) { GtkWidget *win = gtk_widget_get_toplevel(widget); int i; double yvalue1[1001]; float const_asp_ratio; char yvalue1paramname1, yvalue1paramname2, yvalue1paramname3; FILE* infile_yvalue1params = fopen("InputParameters/yvalue1params", "r"); fscanf(infile_yvalue1params,"%s %d\n%s %f\n%s %f\n", &yvalue1paramname1, &yvalue1_param1, &yvalue1paramname2, &yvalue1_param2, &yvalue1paramname3, &const_asp_ratio); fclose(infile_yvalue1params); /*Create 'yvalue1' */ for(i = 0; i <= 1000; i++) { yvalue1[i] = (float)i; } /*/ int winwidth, winheight; gtk_window_get_size(GTK_WINDOW(win), &winwidth, &winheight); cairo_set_source_rgb(cr, 0.6, 0.0, 0.0); cairo_set_line_width(cr, 1.0); cairo_move_to(cr, winwidth/8, (float)yvalue1_param1*(winheight/8)+yvalue1_param2); for (i=0; i <= 1000; i++) { cairo_line_to(cr, i+(winwidth/8), (float)yvalue1_param1*(winheight/8)+(yvalue1[i]*const_asp_ratio)+yvalue1_param2); } cairo_stroke(cr); }/* End of do_drawing() */ // int main(int argc, char *argv[]) { GtkWidget *window, *swin, *grid, *box; GtkAdjustment *horizontal, *vertical; GtkWidget *quitbutton; /*/ GtkWidget *label_yvalue1_param1; GtkWidget *spin_int_yvalue1_param1; GtkAdjustment *integer_yvalue1_param1; /*/ GtkWidget *label_yvalue1_param2; GtkWidget *spin_float_yvalue1_param2; GtkAdjustment *float_yvalue1_param2; /*/ GtkWidget *label_const_asp_ratio; GtkWidget *spin_float_const_asp_ratio; GtkAdjustment *float_const_asp_ratio; /*/ GtkWidget *window2; GtkWidget *darea; GtkWidget *layout2; GtkWidget *swin2; GtkWidget *box2; GtkWidget *scrollbar1; // gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "Scrolled Window"); gtk_container_set_border_width (GTK_CONTAINER (window), 10); gtk_widget_set_size_request (window, 200, 100); g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK (gtk_main_quit), NULL); grid = gtk_grid_new (); /**/ label_yvalue1_param1 =