0

接下来是中的布局gtk+

+----------------------------+------+------+------+
|                            |Page 1|Page 2|Page 3|
|                            +------+------+------+
|                            |                    |
|                            |                    |
|     Canvas area            |                    |
|                            |                    |
|                            |                    |
|                            |                    |
|                            |                    |
|                            |                    |
+------------------+---------+                    |
|                  | button  |                    |
|                  +---------+                    |
|                            |                    |
|                            |                    |
|                            |                    |
|                            |                    |
+----------------------------+--------------------+

-> 左侧的“画布区域”和“按钮”位于垂直框中。右侧的“Page 1~3”是另一个垂直框中的笔记本页面。

-> 左侧的“画布区域”由gtk_drawing_area_newsignal 创建并连接expose-event

-> 在“第 3 页”中,有一个“画布区域”,其创建方式与上面类似。

-> 每次按“按钮”时,左侧的“画布区域”会立即刷新。

问题是

-> 如果第3页是当前页,当按下“按钮”时,第3页中的“画布区域”不会刷新,直到我切换到另一个页面,例如第1页或第2页,然后再切换回第3页.如果一直停留在“第3页”,似乎expose-event不会发出信号。

-> Page 3 是current页面时是否可以刷新Page 3 中的“Canvas 区域”,就像左边的“Canvas 区域”在按下“按钮”时立即刷新一样?

-> 我试过了gdk_threads_entergdk_threads_leave但是没有用。

请帮忙。谢谢你。


下面是一个小例子。它运作良好,我的意思是上面提到的问题不会出现。问题出在函数“button_pressed”中,我错过了“gdk_invalidate_region (g_object_get_data(G_OBJECT(widget), “right_canvas”));” 在我的应用程序中。

#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; i < 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..\" to 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; i < 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..\" to 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;
}
4

0 回答 0