Scrolling a Cairo drawing-area or Gtk layout
- From: Roger Matthews <roger matthews hotmail com>
- To: "gtk-app-devel-list gnome org" <gtk-app-devel-list gnome org>
- Subject: Scrolling a Cairo drawing-area or Gtk layout
- Date: Tue, 21 Jul 2015 13:12:00 +0930
/*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 <stdio.h>
#include <math.h>
#include <gtk/gtk.h>
#include <cairo.h>
/*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 = gtk_label_new("param1");
gtk_grid_attach (GTK_GRID (grid), label_yvalue1_param1, 0, 0, 1,
1);
/******/
integer_yvalue1_param1 = GTK_ADJUSTMENT (gtk_adjustment_new
(yvalue1_param1, 0.0, 10.0, 1.0, 1.0, 1.0));
spin_int_yvalue1_param1 = gtk_spin_button_new
(integer_yvalue1_param1, 2.0, 0);
g_signal_connect (spin_int_yvalue1_param1,
"value-changed", G_CALLBACK
(get_new_number_integer_yvalue1_param1), NULL);
gtk_grid_attach (GTK_GRID (grid), spin_int_yvalue1_param1, 0, 1,
1, 1);
/**********************************************/
label_yvalue1_param2 = gtk_label_new ("param2");
gtk_grid_attach (GTK_GRID (grid), label_yvalue1_param2, 1, 0, 1,
1);
/******/
float_yvalue1_param2 = GTK_ADJUSTMENT (gtk_adjustment_new
(yvalue1_param2, 0.0, 10.0, 1.0, 1.0, 1.0));
spin_float_yvalue1_param2 = gtk_spin_button_new
(float_yvalue1_param2, 1.0, 0);
g_signal_connect (spin_float_yvalue1_param2,
"value-changed", G_CALLBACK(get_new_number_float_yvalue1_param2),
NULL);
gtk_grid_attach (GTK_GRID (grid), spin_float_yvalue1_param2, 1,
1, 1, 1);
/**********************************************/
label_const_asp_ratio = gtk_label_new ("Gradient");
gtk_grid_attach (GTK_GRID (grid), label_const_asp_ratio, 2, 0,
1, 1);
/******/
float_const_asp_ratio = GTK_ADJUSTMENT (gtk_adjustment_new
(const_asp_ratio, 0.1, 10.0, 0.1, 0.1, 0.1));
spin_float_const_asp_ratio = gtk_spin_button_new
(float_const_asp_ratio, 1.0, 1);
g_signal_connect (spin_float_const_asp_ratio,
"value-changed", G_CALLBACK(get_new_number_float_const_asp_ratio),
NULL);
gtk_grid_attach (GTK_GRID (grid), spin_float_const_asp_ratio, 2,
1, 1, 1);
/**********************************************/
quitbutton = gtk_button_new_with_label ("Quit");
g_signal_connect (quitbutton, "clicked", G_CALLBACK
(gtk_main_quit), NULL);
gtk_grid_attach (GTK_GRID (grid), quitbutton, 0, 6, 1, 1);
/**********************************************/
swin = gtk_scrolled_window_new (NULL, NULL);
horizontal = gtk_scrolled_window_get_hadjustment
(GTK_SCROLLED_WINDOW (swin));
vertical = gtk_scrolled_window_get_vadjustment
(GTK_SCROLLED_WINDOW (swin));
gtk_container_set_border_width (GTK_CONTAINER (swin), 5);
gtk_container_add (GTK_CONTAINER (swin), grid);
/*Pack the widgets into a GtkBox and then into the window.*/
box = gtk_box_new (TRUE, 5);
gtk_box_pack_start (GTK_BOX (box), swin, TRUE, TRUE, 5);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show_all (window);
/*Write inputparams files with default values */
write_yvalue1params();
/*****************************************************************/
/*window2 =
gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea =
gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window2), darea);
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);
gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
gtk_window_set_title(GTK_WINDOW(window2), "Graph");
gtk_widget_show_all(window2);*/
window2 =
gtk_window_new(GTK_WINDOW_TOPLEVEL);
layout2 =
gtk_layout_new(NULL, NULL);
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 (window2), layout2);
gtk_widget_show_all(window2);
/*swin2 =
gtk_scrolled_window_new (NULL, NULL);
horizontal =
gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (swin2));
vertical =
gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swin2));
gtk_container_set_border_width
(GTK_CONTAINER (swin2), 5);
gtk_container_add
(GTK_CONTAINER (swin2), /*grid*//*darea*//*layout2);
/*Pack the widgets into a GtkBox and then into the window.*/
/*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);*/
/**************************************************************/
gtk_main();
/**************************************************************/
return 0;
} /* End of main() */
/**************************************************************/
int write_yvalue1params()
{
FILE*
outfile_yvalue1params = fopen("InputParameters/yvalue1params",
"w");
fprintf(outfile_yvalue1params,"yvalue1_param1,
%d\nyvalue1_param2, %f\nconst_asp_ratio, %f\n", yvalue1_param1,
yvalue1_param2, const_asp_ratio);
fclose(outfile_yvalue1params);
return;
} /* End of
write_yvalue1params() */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]