gtk+: how to refresh canvas immediately (closed) -
followed layout in gtk+
.
+----------------------------+------+------+------+ | |page 1|page 2|page 3| | +------+------+------+ | | | | | | | canvas area | | | | | | | | | | | | | | | | | +------------------+---------+ | | | button | | | +---------+ | | | | | | | | | | | | | +----------------------------+--------------------+
-> "canvas area" , "button" on left in vertical box. "page 1~3" on right pages of notebook in vertical box.
-> "canvas area" on left created gtk_drawing_area_new
, connected signal expose-event
.
-> in "page 3", there "canvas area", created above.
-> every time when press "button", "canvas area" on left refreshed @ once.
the problem is
-> if page 3 current page, when pressing "button", "canvas area" in page 3 won't refresh until switch page, e.g., page 1 or page 2, switch page 3. seemed signal expose-event
won't emit if staying "page 3".
-> possible refresh "canvas area" in page 3 when page 3 current
page, "canvas area" on left refreshes when pressing "button"?
-> haved tried gdk_threads_enter
, gdk_threads_leave
, didn't work.
please help. thank you.
followed small example. works well, mean problem mentioned above won't come up. problem in function "button_pressed" missed "gdk_invalidate_region (g_object_get_data(g_object(widget), "right_canvas"));" in application.
#include <gtk/gtk.h> int num, if_draw = 0; void gdk_invalidate_region (gtkwidget *obj) { gdkregion *region; region = gdk_drawable_get_visible_region(obj->window); gdk_window_invalidate_region(obj->window, region, true); gdk_region_destroy (region); gtk_widget_queue_draw(obj); /* gdk_window_process_updates(obj->window, true); */ } void num_changed (gtkwidget *box, gtkwidget *window) { num = gtk_option_menu_get_history((gtkoptionmenu *)box) + 1; if_draw = 0; gdk_invalidate_region (g_object_get_data(g_object(window), "left_canvas")); gdk_invalidate_region (g_object_get_data(g_object(window), "right_canvas")); } gboolean draw_expose_left (gtkwidget *canvas, gdkeventexpose *event, gpointer user_data) { cairo_t *cr; cr = gdk_cairo_create (canvas->window); if(if_draw) { int i; cairo_set_line_width (cr, 0.7); for(i = 0; < num; i++) { printf("i=%d\n", i); cairo_move_to(cr, 50, (i + 1) * 50); cairo_rel_line_to(cr, 150, 0); cairo_stroke(cr); } } else { cairo_select_font_face(cr, "sans", cairo_font_slant_normal, cairo_font_weight_bold); cairo_set_font_size(cr, 0.35); cairo_move_to(cr, 5, canvas->allocation.height / 2); cairo_show_text(cr, "press button \"press..\" update!!"); } cairo_destroy (cr); return false; } gboolean draw_expose_right (gtkwidget *canvas, gdkeventexpose *event, gpointer user_data) { cairo_t *cr; cr = gdk_cairo_create (canvas->window); if(if_draw) { int i; cairo_set_line_width (cr, 0.7); for(i = 0; < num; i++) { printf("i=%d\n", i); cairo_move_to(cr, (i + 1) * 50, 50); cairo_rel_line_to(cr, 0, 100); cairo_stroke(cr); } } else { cairo_select_font_face(cr, "sans", cairo_font_slant_normal, cairo_font_weight_bold); cairo_set_font_size(cr, 0.35); cairo_move_to(cr, 5, canvas->allocation.height / 2); cairo_show_text(cr, "press button \"press..\" update!!"); } cairo_destroy (cr); return false; } void button_pressed (gtkwidget *button, gtkwidget *widget) { gdk_invalidate_region (g_object_get_data(g_object(widget), "left_canvas")); gdk_invalidate_region (g_object_get_data(g_object(widget), "right_canvas")); if_draw = 1; } int main (int argc, char *argv[]) { gtkwidget *window, *main_box, *box, *hbox, *entry, *menu, *menu_item; gtkwidget *notebook, *label; gtk_init(&argc, &argv); window = gtk_window_new (gtk_window_toplevel); /* gtk_window_set_default_size(gtk_window(window), 300, 200); */ g_signal_connect(g_object(window), "destroy", g_callback(gtk_main_quit), null); main_box = gtk_hbox_new(false, 0); gtk_container_add(gtk_container(window), main_box); gtk_widget_show(main_box); /***** left **********/ box = gtk_vbox_new(false, 0); gtk_box_pack_start(gtk_box(main_box), box, false, true, 0); gtk_widget_show(box); entry = gtk_drawing_area_new(); gtk_widget_set_size_request(entry, 400, 200); g_object_set_data(g_object(window), "left_canvas", entry); gtk_widget_show(entry); gtk_box_pack_start(gtk_box(box), entry, true, true, 0); g_signal_connect(g_object(entry), "expose-event", g_callback(draw_expose_left), window); hbox = gtk_hbox_new(false, 0); gtk_box_pack_start(gtk_box(box), hbox, false, true, 0); gtk_widget_show(hbox); entry = gtk_label_new("num: "); gtk_box_pack_start(gtk_box(hbox), entry, true, true, 0); gtk_widget_show(entry); entry = gtk_option_menu_new(); gtk_widget_set_size_request(entry, 40, -1); gtk_box_pack_start(gtk_box(hbox), entry, true, true, 0); gtk_widget_show(entry); menu = gtk_menu_new(); menu_item = gtk_menu_item_new_with_label("1"); gtk_widget_show(menu_item); gtk_menu_shell_append(gtk_menu_shell(menu), menu_item); menu_item = gtk_menu_item_new_with_label("2"); gtk_widget_show(menu_item); gtk_menu_shell_append(gtk_menu_shell(menu), menu_item); menu_item = gtk_menu_item_new_with_label("3"); gtk_widget_show(menu_item); gtk_menu_shell_append(gtk_menu_shell(menu), menu_item); gtk_option_menu_set_menu(gtk_option_menu(entry), menu); gtk_option_menu_set_history((gtkoptionmenu *)entry, 1); num = 2; gtk_signal_connect(gtk_object(entry), "changed", gtk_signal_func(num_changed), window); entry = gtk_button_new_with_label("press.."); gtk_box_pack_start(gtk_box(hbox), entry, true, true, 0); gtk_signal_connect(gtk_object(entry), "clicked", gtk_signal_func(button_pressed), window); gtk_widget_show(entry); /*********************/ /***** right *********/ box = gtk_vbox_new(false, 0); gtk_box_pack_start(gtk_box(main_box), box, false, true, 0); gtk_widget_show(box); notebook = gtk_notebook_new(); gtk_notebook_set_tab_pos(gtk_notebook(notebook), gtk_pos_top); gtk_box_pack_start(gtk_box(box), notebook, true, true, 0); /* g_signal_connect(g_object(notebook), "switch-page", g_callback(qqaa_show_page_status_acc_platform), window); */ gtk_widget_show(notebook); entry = gtk_label_new("one"); label = gtk_label_new("page 1"); gtk_notebook_append_page(gtk_notebook(notebook), entry, label); entry = gtk_label_new("two"); label = gtk_label_new("page 2"); gtk_notebook_append_page(gtk_notebook(notebook), entry, label); entry = gtk_drawing_area_new(); gtk_widget_set_size_request(entry, 400, 200); g_object_set_data(g_object(window), "right_canvas", entry); gtk_widget_show(entry); g_signal_connect(g_object(entry), "expose-event", g_callback(draw_expose_right), window); label = gtk_label_new("page 3"); gtk_notebook_append_page(gtk_notebook(notebook), entry, label); /*********************/ gtk_widget_show_all(window); gtk_main(); return 0; }
Comments
Post a Comment