Chinaunix首页 | 论坛 | 博客
  • 博客访问: 543035
  • 博文数量: 104
  • 博客积分: 2089
  • 博客等级: 大尉
  • 技术积分: 1691
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-29 08:48
文章分类

全部博文(104)

文章存档

2015年(1)

2013年(13)

2012年(31)

2011年(59)

分类: 嵌入式

2011-09-14 18:07:55

gtk-demo中的例子都是在一个框架中的,如果想自己单拿出来运行,要添加主函数,大多如下:
  1. int
  2. main(int argc, char **argv)
  3. {
  4.         GtkWidget *window;

  5.         gtk_init(&argc, &argv);
  6.         window = demo-入口函数(NULL);
  7.         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  8.         gtk_main();
  9.         return 0;
  10. }
例如在"editable cells"例子中源码:
  1. #include <gtk/gtk.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. static GtkWidget *window = NULL;

  5. typedef struct
  6. {
  7.     gint number;
  8.     gchar *product;
  9.     gint yummy;
  10. }
  11. Item;

  12. enum
  13. {
  14.     COLUMN_ITEM_NUMBER,
  15.     COLUMN_ITEM_PRODUCT,
  16.     COLUMN_ITEM_YUMMY,
  17.     NUM_ITEM_COLUMNS
  18. };

  19. enum
  20. {
  21.     COLUMN_NUMBER_TEXT,
  22.     NUM_NUMBER_COLUMNS
  23. };

  24. static GArray *articles = NULL;

  25.     static void
  26. add_items (void)
  27. {
  28.     Item foo;

  29.     g_return_if_fail (articles != NULL);

  30.     foo.number = 3;
  31.     foo.product = g_strdup ("bottles of coke");
  32.     foo.yummy = 20;
  33.     g_array_append_vals (articles, &foo, 1);

  34.     foo.number = 5;
  35.     foo.product = g_strdup ("packages of noodles");
  36.     foo.yummy = 50;
  37.     g_array_append_vals (articles, &foo, 1);

  38.     foo.number = 2;
  39.     foo.product = g_strdup ("packages of chocolate chip cookies");
  40.     foo.yummy = 90;
  41.     g_array_append_vals (articles, &foo, 1);

  42.     foo.number = 1;
  43.     foo.product = g_strdup ("can vanilla ice cream");
  44.     foo.yummy = 60;
  45.     g_array_append_vals (articles, &foo, 1);

  46.     foo.number = 6;
  47.     foo.product = g_strdup ("eggs");
  48.     foo.yummy = 10;
  49.     g_array_append_vals (articles, &foo, 1);
  50. }

  51.     static GtkTreeModel *
  52. create_items_model (void)
  53. {
  54.     gint i = 0;
  55.     GtkListStore *model;
  56.     GtkTreeIter iter;

  57.     /* create array */
  58.     articles = g_array_sized_new (FALSE, FALSE, sizeof (Item), 1);

  59.     add_items ();

  60.     /* create list store */
  61.     model = gtk_list_store_new (NUM_ITEM_COLUMNS, G_TYPE_INT, G_TYPE_STRING,
  62.             G_TYPE_INT, G_TYPE_BOOLEAN);

  63.     /* add items */
  64.     for (i = 0; i < articles->len; i++)
  65.     {
  66.         gtk_list_store_append (model, &iter);

  67.         gtk_list_store_set (model, &iter,
  68.                 COLUMN_ITEM_NUMBER,
  69.                 g_array_index (articles, Item, i).number,
  70.                 COLUMN_ITEM_PRODUCT,
  71.                 g_array_index (articles, Item, i).product,
  72.                 COLUMN_ITEM_YUMMY,
  73.                 g_array_index (articles, Item, i).yummy,
  74.                 -1);
  75.     }

  76.     return GTK_TREE_MODEL (model);
  77. }

  78.     static GtkTreeModel *
  79. create_numbers_model (void)
  80. {
  81. #define N_NUMBERS 10
  82.     gint i = 0;
  83.     GtkListStore *model;
  84.     GtkTreeIter iter;

  85.     /* create list store */
  86.     model = gtk_list_store_new (NUM_NUMBER_COLUMNS, G_TYPE_STRING, G_TYPE_INT);

  87.     /* add numbers */
  88.     for (i = 0; i < N_NUMBERS; i++)
  89.     {
  90.         char str[2];

  91.         str[0] = '0' + i;
  92.         str[1] = '\0';

  93.         gtk_list_store_append (model, &iter);

  94.         gtk_list_store_set (model, &iter,
  95.                 COLUMN_NUMBER_TEXT, str,
  96.                 -1);
  97.     }

  98.     return GTK_TREE_MODEL (model);

  99. #undef N_NUMBERS
  100. }

  101.     static void
  102. add_item (GtkWidget *button, gpointer data)
  103. {
  104.     Item foo;
  105.     GtkTreeIter iter;
  106.     GtkTreeModel *model = (GtkTreeModel *)data;

  107.     g_return_if_fail (articles != NULL);

  108.     foo.number = 0;
  109.     foo.product = g_strdup ("Description here");
  110.     foo.yummy = 50;
  111.     g_array_append_vals (articles, &foo, 1);

  112.     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  113.     gtk_list_store_set (GTK_LIST_STORE (model), &iter,
  114.             COLUMN_ITEM_NUMBER, foo.number,
  115.             COLUMN_ITEM_PRODUCT, foo.product,
  116.             COLUMN_ITEM_YUMMY, foo.yummy,
  117.             -1);
  118. }

  119.     static void
  120. remove_item (GtkWidget *widget, gpointer data)
  121. {
  122.     GtkTreeIter iter;
  123.     GtkTreeView *treeview = (GtkTreeView *)data;
  124.     GtkTreeModel *model = gtk_tree_view_get_model (treeview);
  125.     GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);

  126.     if (gtk_tree_selection_get_selected (selection, NULL, &iter))
  127.     {
  128.         gint i;
  129.         GtkTreePath *path;

  130.         path = gtk_tree_model_get_path (model, &iter);
  131.         i = gtk_tree_path_get_indices (path)[0];
  132.         gtk_list_store_remove (GTK_LIST_STORE (model), &iter);

  133.         g_array_remove_index (articles, i);

  134.         gtk_tree_path_free (path);
  135.     }
  136. }

  137.     static gboolean
  138. separator_row (GtkTreeModel *model,
  139.         GtkTreeIter *iter,
  140.         gpointer data)
  141. {
  142.     GtkTreePath *path;
  143.     gint idx;

  144.     path = gtk_tree_model_get_path (model, iter);
  145.     idx = gtk_tree_path_get_indices (path)[0];

  146.     gtk_tree_path_free (path);

  147.     return idx == 5;
  148. }

  149.     static void
  150. editing_started (GtkCellRenderer *cell,
  151.         GtkCellEditable *editable,
  152.         const gchar *path,
  153.         gpointer data)
  154. {
  155.     gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (editable),
  156.             separator_row, NULL, NULL);
  157. }

  158.     static void
  159. cell_edited (GtkCellRendererText *cell,
  160.         const gchar *path_string,
  161.         const gchar *new_text,
  162.         gpointer data)
  163. {
  164.     GtkTreeModel *model = (GtkTreeModel *)data;
  165.     GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  166.     GtkTreeIter iter;

  167.     gint column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column"));

  168.     gtk_tree_model_get_iter (model, &iter, path);

  169.     switch (column)
  170.     {
  171.         case COLUMN_ITEM_NUMBER:
  172.             {
  173.                 gint i;

  174.                 i = gtk_tree_path_get_indices (path)[0];
  175.                 g_array_index (articles, Item, i).number = atoi (new_text);

  176.                 gtk_list_store_set (GTK_LIST_STORE (model), &iter, column,
  177.                         g_array_index (articles, Item, i).number, -1);
  178.             }
  179.             break;

  180.         case COLUMN_ITEM_PRODUCT:
  181.             {
  182.                 gint i;
  183.                 gchar *old_text;

  184.                 gtk_tree_model_get (model, &iter, column, &old_text, -1);
  185.                 g_free (old_text);

  186.                 i = gtk_tree_path_get_indices (path)[0];
  187.                 g_free (g_array_index (articles, Item, i).product);
  188.                 g_array_index (articles, Item, i).product = g_strdup (new_text);

  189.                 gtk_list_store_set (GTK_LIST_STORE (model), &iter, column,
  190.                         g_array_index (articles, Item, i).product, -1);
  191.             }
  192.             break;
  193.     }

  194.     gtk_tree_path_free (path);
  195. }

  196.     static void
  197. add_columns (GtkTreeView *treeview,
  198.         GtkTreeModel *items_model,
  199.         GtkTreeModel *numbers_model)
  200. {
  201.     GtkCellRenderer *renderer;

  202.     /* number column */
  203.     renderer = gtk_cell_renderer_combo_new ();
  204.     g_object_set (renderer,
  205.             "model", numbers_model,
  206.             "text-column", COLUMN_NUMBER_TEXT,
  207.             "has-entry", FALSE,
  208.             "editable", TRUE,
  209.             NULL);
  210.     g_signal_connect (renderer, "edited",
  211.             G_CALLBACK (cell_edited), items_model);
  212.     g_signal_connect (renderer, "editing-started",
  213.             G_CALLBACK (editing_started), NULL);
  214.     g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_NUMBER));

  215.     gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
  216.             -1, "Number", renderer,
  217.             "text", COLUMN_ITEM_NUMBER,
  218.             NULL);

  219.     /* product column */
  220.     renderer = gtk_cell_renderer_text_new ();
  221.     g_object_set (renderer,
  222.             "editable", TRUE,
  223.             NULL);
  224.     g_signal_connect (renderer, "edited",
  225.             G_CALLBACK (cell_edited), items_model);
  226.     g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_PRODUCT));

  227.     gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
  228.             -1, "Product", renderer,
  229.             "text", COLUMN_ITEM_PRODUCT,
  230.             NULL);

  231.     /* yummy column */
  232.     renderer = gtk_cell_renderer_progress_new ();
  233.     g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (COLUMN_ITEM_YUMMY));

  234.     gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
  235.             -1, "Yummy", renderer,
  236.             "value", COLUMN_ITEM_YUMMY,
  237.             NULL);


  238. }

  239.     GtkWidget *
  240. do_editable_cells (GtkWidget *do_widget)
  241. {
  242.     if (!window)
  243.     {
  244.         GtkWidget *vbox;
  245.         GtkWidget *hbox;
  246.         GtkWidget *sw;
  247.         GtkWidget *treeview;
  248.         GtkWidget *button;
  249.         GtkTreeModel *items_model;
  250.         GtkTreeModel *numbers_model;

  251.         /* create window, etc */
  252.         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  253.         gtk_window_set_screen (GTK_WINDOW (window),
  254.                 gtk_widget_get_screen (do_widget));
  255.         gtk_window_set_title (GTK_WINDOW (window), "Shopping list");
  256.         gtk_container_set_border_width (GTK_CONTAINER (window), 5);
  257.         g_signal_connect (window, "destroy",
  258.                 G_CALLBACK (gtk_widget_destroyed), &window);

  259.         vbox = gtk_vbox_new (FALSE, 5);
  260.         gtk_container_add (GTK_CONTAINER (window), vbox);

  261.         gtk_box_pack_start (GTK_BOX (vbox),
  262.                 gtk_label_new ("Shopping list (you can edit the cells!)"),
  263.                 FALSE, FALSE, 0);

  264.         sw = gtk_scrolled_window_new (NULL, NULL);
  265.         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
  266.                 GTK_SHADOW_ETCHED_IN);
  267.         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
  268.                 GTK_POLICY_AUTOMATIC,
  269.                 GTK_POLICY_AUTOMATIC);
  270.         gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);

  271.         /* create models */
  272.         items_model = create_items_model ();
  273.         numbers_model = create_numbers_model ();

  274.         /* create tree view */
  275.         treeview = gtk_tree_view_new_with_model (items_model);
  276.         gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
  277.         gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
  278.                 GTK_SELECTION_SINGLE);

  279.         add_columns (GTK_TREE_VIEW (treeview), items_model, numbers_model);

  280.         g_object_unref (numbers_model);
  281.         g_object_unref (items_model);

  282.         gtk_container_add (GTK_CONTAINER (sw), treeview);

  283.         /* some buttons */
  284.         hbox = gtk_hbox_new (TRUE, 4);
  285.         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  286.         button = gtk_button_new_with_label ("Add item");
  287.         g_signal_connect (button, "clicked",
  288.                 G_CALLBACK (add_item), items_model);
  289.         gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  290.         button = gtk_button_new_with_label ("Remove item");
  291.         g_signal_connect (button, "clicked",
  292.                 G_CALLBACK (remove_item), treeview);
  293.         gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  294.         gtk_window_set_default_size (GTK_WINDOW (window), 320, 200);
  295.     }

  296.     if (!GTK_WIDGET_VISIBLE (window))
  297.         gtk_widget_show_all (window);
  298.     else
  299.     {
  300.         gtk_widget_destroy (window);
  301.         window = NULL;
  302.     }

  303.     return window;
  304. }


例如存入t_editable_cells.c中,在代码底部插入如下代码
  1. int
  2. main(int argc, char **argv)
  3. {
  4.     GtkWidget *window;

  5.     gtk_init(&argc, &argv);
  6.     window = do_editable_cells(NULL);
  7.     g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  8.     gtk_main();
  9.     return 0;
  10. }

  1. gcc `pkg-config --libs --cflags gtk+-2.0` -o t_editable_cells t_editable_cells.c
即可

阅读(4487) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

wxwpxh2014-01-01 23:07:03

不错,编译的命令改为如下后通过:
gcc -o t_editable_cells t_editable_cells.c `pkg-config --cflags --libs gtk+-2.0`