gtk draw "expose-event" and redraw

Posted by warem on Stack Overflow See other posts from Stack Overflow or by warem
Published on 2012-07-06T10:15:12Z Indexed on 2012/07/06 15:16 UTC
Read the original article Hit count: 216

Filed under:
|

I want to use expose-event to draw something then update or redraw. That's to say, there are a drawing area and a button in window. When clicking button, the drawing area will be redrawn accordingly. My problems are

  1. Following code worked but it only had a drawing area no button. If I add the button(cancel the comment for button), nothing is drawn. What's the reason?

  2. In the following code, if I changed gtk_container_add (GTK_CONTAINER (box), canvas); to gtk_box_pack_start(GTK_BOX(box), canvas, FALSE, FALSE, 0);, nothing is drawn. Usually we use gtk_box_pack_start to add something into box. Why doesn't it work this time?

  3. The function build_ACC_axis refreshed drawing area and prepared for new draw. I google it but I didn't know if it worked. Could you please comment on it?

If the source file is test.c, then compilation is

gcc -o test test.c `pkg-config --cflags --libs gtk+-2.0`

The code is below:

#include <gtk/gtk.h>
#include <glib.h>

static void draw (GdkDrawable *d, GdkGC *gc)
{
  /* Draw with GDK */
  gdk_draw_line (d, gc, 0, 0, 50, 50);
  gdk_draw_line (d, gc, 50, 50, 50, 150);
  gdk_draw_line (d, gc, 50, 150, 0, 200);

  gdk_draw_line (d, gc, 200, 0, 150, 50);
  gdk_draw_line (d, gc, 150, 50, 150, 150);
  gdk_draw_line (d, gc, 150, 150, 200, 200);

  gdk_draw_line (d, gc, 50, 50, 150, 50);
  gdk_draw_line (d, gc, 50, 150, 150, 150);
}

static gboolean expose_cb (GtkWidget *canvas, GdkEventExpose *event, gpointer user_data)
{
  GdkGC *gc;

  gc = gdk_gc_new (canvas->window);
  draw (canvas->window, gc);
  g_object_unref (gc);

  return FALSE;
}

void build_ACC_axis (GtkWidget *button, GtkWidget *widget)
{
  GdkRegion *region;

  GtkWidget *canvas = g_object_get_data(G_OBJECT(widget), "plat_GA_canvas");

  region = gdk_drawable_get_visible_region(canvas->window);
  gdk_window_invalidate_region(canvas->window, region, TRUE);
  gtk_widget_queue_draw(canvas);
  /* gdk_window_process_updates(canvas->window, TRUE); */
  gdk_region_destroy (region);
}

int main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *canvas, *box, *button;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);

  box = gtk_vbox_new(FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), box);

  canvas = gtk_drawing_area_new ();
  g_object_set_data(G_OBJECT(window), "plat_GA_canvas", canvas);
  /* gtk_box_pack_start(GTK_BOX(box), canvas, FALSE, FALSE, 0); */
  gtk_container_add (GTK_CONTAINER (box), canvas);
  g_signal_connect (G_OBJECT (canvas), "expose-event", G_CALLBACK (expose_cb), NULL);

  /* button = gtk_button_new_with_label ("ok");
  gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
  |+ gtk_container_add (GTK_CONTAINER (box), button); +|
  gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(build_ACC_axis), window); */

  gtk_widget_show_all (window);

  gtk_main ();
}

© Stack Overflow or respective owner

Related posts about gtk

Related posts about redraw