Re: Lucky B.C.'s reply to GtkDrawingArea "width" and "height" <properties>
- From: cecashon aol com
- To: roger matthews hotmail com, gtk-app-devel-list gnome org
- Subject: Re: Lucky B.C.'s reply to GtkDrawingArea "width" and "height" <properties>
- Date: Tue, 6 Mar 2018 18:15:47 -0500
Hi Roger,
Not sure about your builder question and setting the width and height of the drawing area there. For drawing
lines though it looks like you are trying to get the width and height of the GtkWindow instead of the drawing
area. The drawing area widget is passed to your "draw" callback so you can get the width and height there.
For updating the drawing with the spin buttons you would need to send the drawing area to those callbacks and
call gtk_widget_queue_draw(da) to update your drawing.
Can you put gtk_widget_set_size_request() right after
drawing1 = gtk_builder_get_object (builder1, "drawing1");
in your code? Will that work?
Eric
Some test code.
//gcc -Wall grid_da1.c -o grid_da1 `pkg-config --cflags --libs gtk+-3.0`
#include<gtk/gtk.h>
static gint rows=4;
static gint columns=5;
static void rows_spin_changed(GtkSpinButton *spin_button, GtkWidget *da);
static void columns_spin_changed(GtkSpinButton *spin_button, GtkWidget *da);
static gboolean draw_lines(GtkWidget *widget, cairo_t *cr, gpointer data);
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
gtk_window_set_title(GTK_WINDOW(window), "Draw Grid");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *da=gtk_drawing_area_new();
gtk_widget_set_size_request(da, 600, 600);
g_signal_connect(da, "draw", G_CALLBACK(draw_lines), NULL);
GtkWidget *view=gtk_viewport_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(view), da);
GtkWidget *scroll=gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scroll), view);
GtkAdjustment *adjustment1=gtk_adjustment_new(4, 3, 15, 1, 0, 0);
GtkAdjustment *adjustment2=gtk_adjustment_new(5, 3, 15, 1, 0, 0);
GtkWidget *rows_spin_label=gtk_label_new("Rows");
GtkWidget *rows_spin=gtk_spin_button_new(adjustment1, 1, 0);
g_signal_connect(rows_spin, "value-changed", G_CALLBACK(rows_spin_changed), da);
GtkWidget *columns_spin_label=gtk_label_new("Columns");
GtkWidget *columns_spin=gtk_spin_button_new(adjustment2, 1, 0);
g_signal_connect(columns_spin, "value-changed", G_CALLBACK(columns_spin_changed), da);
GtkWidget *grid=gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 8);
gtk_grid_attach(GTK_GRID(grid), rows_spin_label, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), rows_spin, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), columns_spin_label, 0, 2, 1, 1);
gtk_grid_attach(GTK_GRID(grid), columns_spin, 0, 3, 1, 1);
GtkWidget *paned1=gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
gtk_paned_pack1(GTK_PANED(paned1), grid, FALSE, TRUE);
gtk_paned_pack2(GTK_PANED(paned1), scroll, TRUE, TRUE);
gtk_paned_set_position(GTK_PANED(paned1), 200);
gtk_container_add(GTK_CONTAINER(window), paned1);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void rows_spin_changed(GtkSpinButton *spin_button, GtkWidget *da)
{
rows=gtk_spin_button_get_value_as_int(spin_button);
gtk_widget_queue_draw(da);
}
static void columns_spin_changed(GtkSpinButton *spin_button, GtkWidget *da)
{
columns=gtk_spin_button_get_value_as_int(spin_button);
gtk_widget_queue_draw(da);
}
static gboolean draw_lines(GtkWidget *da, cairo_t *cr, gpointer data)
{
g_print("Drawing Area Width %i, Height %i\n", gtk_widget_get_allocated_width(da),
gtk_widget_get_allocated_height(da));
gint i=0;
gdouble width=(gdouble)gtk_widget_get_allocated_width(da);
gdouble height=(gdouble)gtk_widget_get_allocated_height(da);
gdouble w1=width/10.0;
gdouble h1=height/10.0;
gdouble w2=8.0*w1/(gdouble)columns;
gdouble h2=8.0*h1/(gdouble)rows;
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_paint(cr);
cairo_set_line_width(cr, 3.0);
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
for(i=0;i<rows+1;i++)
{
cairo_move_to(cr, w1, h1+(gdouble)i*h2);
cairo_line_to(cr, 9.0*w1, h1+(gdouble)i*h2);
cairo_stroke(cr);
}
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
for(i=0;i<columns+1;i++)
{
cairo_move_to(cr, w1+(gdouble)i*w2, h1);
cairo_line_to(cr, w1+(gdouble)i*w2, 9.0*h1);
cairo_stroke(cr);
}
return FALSE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]