GtkTree and GtkCTree

only for RuBoard - do not distribute or recompile

GtkTree and GtkCTree

The GtkTree and GtkCTree widgets are standard tree-type controls. The C in GtkCTree stands for column(s) ; you could say it is a cross between a tree control and a data control (see Figure 4.2 and Listing 4.2). In terms of the GTK+ widget tree, GtkContainer is the parent widget of GtkList, GtkCList, and GtkTree. GtkCTree is the child of GtkCList. Although you can think of GtkCTree as being derived from GtkCList, the same does not necessarily apply to GtkTree and GtkList.

Figure 4.2. The Tree widgets demo program running.
graphics/04fig02.gif

In the following program, the GtkTree and GtkCTree widgets are presented side by side for comparison. The important difference between them is that the GtkCTree widget can accept only pixmaps and text as input, whereas the GtkTree widget can take any type of child widget.

Listing 4.2 GtkTree and GtkCTree Demo
 #include <gtk/gtk.h>  GtkWidget *frm_trees;  GtkWidget *tree_left, *ctree_right;  GtkWidget *subtree_item2;  GtkWidget *subtree_item3;  GtkWidget *subtree_widgets;  void destroy_main();  void cmd_cycle_line_style_clicked();  void cmd_cycle_expander_clicked();  gint main(gint argc, gchar *argv[])  {    GtkWidget *hbox_main, *vbox_left, *vbox_right;     GtkWidget *lvl1_item1, *lvl1_item2;     GtkWidget *widgets_item;     GtkWidget *label_widget, *button_widget, *check_box_widget;     GtkWidget *item2_item1, *item2_item1_item1;     GtkWidget *label_item, *button_item, *check_box_item;     GtkWidget *cmd_cycle_line_style, *cmd_cycle_expander;     GtkCTreeNode *root_node_1;     GtkCTreeNode *root_node_2;     GtkCTreeNode *root_node_3;  /* The following sets the titles for the CTree widget.   * The width of the columns will be determined by   * the column headers, not the data in the columns.   */  gchar *column_titles[2] = {"Position", "Info"};     gchar *root1_data[2] = {"root1", "test"};     gchar *root2_data[2] = {"root2", "This one has subnodes."};     gchar *root3_data[2] = {"subnode1", "Subnode."};     gtk_init(&argc, &argv);     frm_trees = gtk_window_new(GTK_WINDOW_TOPLEVEL);     gtk_window_set_title(GTK_WINDOW(frm_trees), "Tree Widgets Demo");     gtk_signal_connect(GTK_OBJECT(frm_trees),          "destroy",          GTK_SIGNAL_FUNC(destroy_main),          NULL);     hbox_main = gtk_hbox_new(TRUE, 0);     vbox_left = gtk_vbox_new(TRUE, 0);     vbox_right = gtk_vbox_new(FALSE, 0);  /* The fundamental differences between GtkTree and GtkCTree   * are similar to the differences between GtkList and GtkCList.   * That is, GtkTree can accept any widget as a tree item,   * whereas GtkCTree can accept only pixmaps or text.   */  tree_left = gtk_tree_new();  /* A tree isn't much good if it doesn't have anything in it.   * Fortunately, adding a "tree item" widget is quite simple   */  lvl1_item1 = gtk_tree_item_new_with_label("Level 1, Item 1");     lvl1_item2 = gtk_tree_item_new_with_label("Level 1, Item 2");     gtk_tree_append(GTK_TREE(tree_left), lvl1_item1);     gtk_tree_append(GTK_TREE(tree_left), lvl1_item2);  /* Unfortunately, this creates a tree that imitates a list   * box. The tree widget is not useful unless (and until) you start   * adding subtrees. Note that this is different from adding nodes   * and pointers, which is taken care of for you by the subtree   * structure.   *   * You don't add a tree to a tree; you add a tree to a "tree   * item."   */  subtree_item2 = gtk_tree_new();     item2_item1 = gtk_tree_item_new_with_label("Level 2, Item 1");     gtk_tree_append(GTK_TREE(subtree_item2), item2_item1);     gtk_tree_item_set_subtree(GTK_TREE_ITEM(lvl1_item2), subtree_item2);  /* You have to explicitly show the subtree tree items.   *   * Note that if the user has selected items in a subtree and then   * collapses that branch of the tree, the items remain selected.   */  gtk_widget_show(item2_item1);     subtree_item3 = gtk_tree_new();     item2_item1_item1 = gtk_tree_item_new_with_label("Level 3, Item 1");     gtk_tree_append(GTK_TREE(subtree_item3), item2_item1_item1);       gtk_tree_item_set_subtree(GTK_TREE_ITEM(item2_item1), subtree_item3);     gtk_widget_show(item2_item1_item1);     widgets_item = gtk_tree_item_new_with_label("Widgets");     gtk_tree_append(GTK_TREE(tree_left), widgets_item);     subtree_widgets = gtk_tree_new();     gtk_tree_item_set_subtree(GTK_TREE_ITEM(widgets_item), subtree_widgets);  /* Add some widgets to the tree.   *   * First, construct a label widget and add it to the tree.   */  label_item = gtk_tree_item_new();     label_widget = gtk_label_new("This is a label widget.");     gtk_container_add(GTK_CONTAINER(label_item), label_widget);     gtk_tree_append(GTK_TREE(subtree_widgets), label_item);     gtk_widget_show(label_widget);     gtk_widget_show(label_item);  /* Next, add a command button to the tree.   */  button_item = gtk_tree_item_new();     button_widget = gtk_button_new_with_label("This is a button widget.");     gtk_container_add(GTK_CONTAINER(button_item), button_widget);     gtk_tree_append(GTK_TREE(subtree_widgets), button_item);     gtk_widget_show(button_widget);     gtk_widget_show(button_item);  /* And now a check box   */  check_box_item = gtk_tree_item_new();     check_box_widget = gtk_check_button_new_with_label("This is a check box  widget.");     gtk_container_add(GTK_CONTAINER(check_box_item), check_box_widget);     gtk_tree_append(GTK_TREE(subtree_widgets), check_box_item);     gtk_widget_show(check_box_widget);     gtk_widget_show(check_box_item);  /* Now look at the CTree widget for comparison.   */  ctree_right = gtk_ctree_new_with_titles(2, 0, column_titles);     root_node_1 = gtk_ctree_insert_node(GTK_CTREE(ctree_right), NULL, NULL,  root1_data,                     0, NULL, NULL, NULL, NULL, FALSE, TRUE);     root_node_2 = gtk_ctree_insert_node(GTK_CTREE(ctree_right), NULL, NULL,  root2_data,                     0, NULL, NULL, NULL, NULL, FALSE, TRUE);     root_node_3 = gtk_ctree_insert_node(GTK_CTREE(ctree_right), root_node_2, NULL,  root3_data,                     0, NULL, NULL, NULL, NULL, FALSE, TRUE);  /* As you can see, the construction of a CTree is much simpler.   * Once you have the CTree widget instantiated, you essentially   * create new nodes and attach them to existing nodes. The   * parameters for the  gtk_ctree_insert_node  are as follows:   *  ?  ctree  The ctree object in question.   *  ?  parent  The parent node to attach to; NULL for root level.   *  ?  sibling  The sibling node; NULL for end of branch.   *  ?  text  A text array of values for the ctree columns; must   *          contain the same number of values as the ctree   *          contains columns.   *  ?  spacing  Number of pixels between the tree-structure   *             pixmap and the text.   *  ?  pixmap_closed  \  *  bitmask_closed  \  Pixmap and bitmap pointers to display when   *  ?  pixmap_opened  /  the node is expanded or collapsed.   *  bitmask_opened  /  *  ?  is_leaf  Boolean indicating whether or not this node is a   *             leaf; if TRUE, it doesn't contain subnodes,   *             and you won't be able to add subnodes.   *  ?  expanded  Boolean indicating the default expanded/non-expanded   *              state of the node.   */  cmd_cycle_line_style = gtk_button_new_with_label("Cycle Line Style");     gtk_signal_connect(GTK_OBJECT(cmd_cycle_line_style),          "clicked",          GTK_SIGNAL_FUNC(cmd_cycle_line_style_clicked),          NULL);     cmd_cycle_expander = gtk_button_new_with_label("Cycle Expander Style");     gtk_signal_connect(GTK_OBJECT(cmd_cycle_expander),          "clicked",          GTK_SIGNAL_FUNC(cmd_cycle_expander_clicked),          NULL);     gtk_box_pack_start(GTK_BOX(vbox_left), tree_left, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_right), ctree_right, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_right), cmd_cycle_line_style, TRUE, FALSE, 0);     gtk_box_pack_start(GTK_BOX(vbox_right), cmd_cycle_expander, TRUE, FALSE, 0);     gtk_box_pack_start(GTK_BOX(hbox_main), vbox_left, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(hbox_main), vbox_right, TRUE, TRUE, 0);     gtk_container_add(GTK_CONTAINER(frm_trees), hbox_main);       gtk_widget_show_all (frm_trees);     gtk_main ();     return 0;  }  void destroy_main()  {   gtk_main_quit();  }  void cmd_cycle_line_style_clicked()  {    static gint line_style = 1;     if (line_style == 4)         line_style = 1;     else line_style++;     switch (line_style)      {       case 1: gtk_ctree_set_line_style(GTK_CTREE(ctree_right),                                         GTK_CTREE_LINES_NONE);                break;        case 2: gtk_ctree_set_line_style(GTK_CTREE(ctree_right),                                         GTK_CTREE_LINES_SOLID);                break;        case 3: gtk_ctree_set_line_style(GTK_CTREE(ctree_right),                                         GTK_CTREE_LINES_DOTTED);                break;        case 4: gtk_ctree_set_line_style(GTK_CTREE(ctree_right),                                         GTK_CTREE_LINES_TABBED);                break;        default: g_print("Should not see this line!!");                 break;      }  }  void cmd_cycle_expander_clicked()  {    static gint expander_style = 1;     if (expander_style == 4)         expander_style = 1;     else expander_style++;     switch (expander_style)      {       case 1: gtk_ctree_set_expander_style(GTK_CTREE(ctree_right),                                             GTK_CTREE_EXPANDER_NONE);                break;        case 2: gtk_ctree_set_expander_style(GTK_CTREE(ctree_right),                                             GTK_CTREE_EXPANDER_SQUARE);                break;        case 3: gtk_ctree_set_expander_style(GTK_CTREE(ctree_right),                                             GTK_CTREE_EXPANDER_TRIANGLE);                break;        case 4: gtk_ctree_set_expander_style(GTK_CTREE(ctree_right),                                             GTK_CTREE_EXPANDER_CIRCULAR);                break;        default: g_print("Should not see this line!!");                 break;      }  } 
only for RuBoard - do not distribute or recompile


MySQL Building User Interfaces
MySQL: Building User Interfaces (Landmark)
ISBN: 073571049X
EAN: 2147483647
Year: 2001
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net