The GtkCombo Widget

only for RuBoard - do not distribute or recompile

The GtkCombo Widget

The GtkCombo widget is the GTK+ answer to the combo box. It has an entry widget with a drop-down list of values. The user can enter values directly into the entry widget, and if the entered value matches a value already in the list, the widget can be set to automatically fill in the value. One of the interesting things about the GtkCombo widget is that the list of values is actually maintained separately from the widget itself. Figure 3.1 shows the running program compiled from Listing 3.1. Note that the comments in the code in Listing 3.1 tell what is coming up rather than what just happened .

Figure 3.1. Combo widget demo program.
graphics/03fig01.gif
Listing 3.1 GtkCombo Widget Demo Program
 #include <gtk/gtk.h>  GtkWidget *frm_combo;  GtkWidget *cbo_first;  void destroy_main();  void cmd_set_use_arrows_false_clicked();  void cmd_set_use_arrows_true_clicked();  void cmd_set_use_arrows_always_false_clicked();  void cmd_set_use_arrows_always_true_clicked();  void cmd_set_case_sensitive_true_clicked();  void cmd_set_case_sensitive_false_clicked();  void cmd_get_text_clicked();  gint main(gint argc, gchar *argv[])  {    GtkWidget *vbox_main;     GtkWidget *cmd_set_use_arrows_false, *cmd_set_use_arrows_true;     GtkWidget *cmd_set_use_arrows_always_false, *cmd_set_use_arrows_always_true;     GtkWidget *cmd_set_case_sensitive_true, *cmd_set_case_sensitive_false;     GtkWidget *cmd_get_text;  /* Notice that the list for the combo box is maintained separately   * from the combo box itself. GList is a GLib data type.   */  GList *list_of_months = NULL;     gtk_init(&argc, &argv);     frm_combo = gtk_window_new(GTK_WINDOW_TOPLEVEL);     gtk_window_set_title(GTK_WINDOW(frm_combo), "Combo and Options Demo");  gtk_signal_connect(GTK_OBJECT(frm_combo),       "destroy",       GTK_SIGNAL_FUNC(destroy_main),       NULL);  vbox_main = gtk_vbox_new(TRUE, 0);  /* For the combo box, when the cursor is in the entry part of   * the widget, pressing the Enter key displays the list, and   * pressing the Esc key collapses the list.   */  cbo_first = gtk_combo_new();  list_of_months = g_list_append(list_of_months, "January");  list_of_months = g_list_append(list_of_months, "February");  list_of_months = g_list_append(list_of_months, "March");  list_of_months = g_list_append(list_of_months, "April");  list_of_months = g_list_append(list_of_months, "May");  list_of_months = g_list_append(list_of_months, "June");  list_of_months = g_list_append(list_of_months, "July");  list_of_months = g_list_append(list_of_months, "August");  list_of_months = g_list_append(list_of_months, "AUGUST");  gtk_combo_set_popdown_strings(GTK_COMBO(cbo_first), list_of_months);  /* If you wait until after the GList of values has   * been set, you can explicitly set the displayed value.   * Note that if the following line had been before the   *  gtk_combo_set_popdown_strings()  call, the combo box   * would have defaulted to the first item in the GList   * ("January"), not to the value that you explicitly set.   */  gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(cbo_first)->entry), "October");  /* The previous call is the first example so far of accessing a   * "sub-object." In this case, the combo box is a composite widget,   * and one of the child widgets is an entry box. The -  > is C  * pointer syntax.   *   * If the current value shown in the combo widget is not   * in the GList for that widget, the up and down arrows   * are disabled. This is because the up and down arrows work   * by comparing the current value against the values in the   * list and then selecting the correct replacement value   * each time the key is pushed.   */   /* Here are some buttons to demonstrate the functionality: */  cmd_set_use_arrows_true = gtk_button_new_with_label("Set Use Arrows TRUE");  gtk_signal_connect(GTK_OBJECT(cmd_set_use_arrows_true),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_set_use_arrows_true_clicked),                     NULL);  cmd_set_use_arrows_false = gtk_button_new_with_label("Set Use Arrows FALSE");  gtk_signal_connect(GTK_OBJECT(cmd_set_use_arrows_false),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_set_use_arrows_false_clicked),                     NULL);  cmd_set_use_arrows_always_true = gtk_button_new_with_label("Set Always TRUE");  gtk_signal_connect(GTK_OBJECT(cmd_set_use_arrows_always_true),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_set_use_arrows_always_true_clicked),                     NULL);  cmd_set_use_arrows_always_false = gtk_button_new_with_label("Set Always  FALSE");  gtk_signal_connect(GTK_OBJECT(cmd_set_use_arrows_always_false),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_set_use_arrows_always_false_clicked),                     NULL);  cmd_set_case_sensitive_true = gtk_button_new_with_label("Case Sensitive TRUE");  gtk_signal_connect(GTK_OBJECT(cmd_set_case_sensitive_true),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_set_case_sensitive_true_clicked),                     NULL);  cmd_set_case_sensitive_false = gtk_button_new_with_label("Case Sensitive  FALSE");  gtk_signal_connect(GTK_OBJECT(cmd_set_case_sensitive_false),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_set_case_sensitive_false_clicked),                     NULL);  cmd_get_text = gtk_button_new_with_label("Get Text");  gtk_signal_connect(GTK_OBJECT(cmd_get_text),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_get_text_clicked),                     NULL);  /* Now assemble the interface */  gtk_box_pack_start(GTK_BOX(vbox_main), cbo_first, TRUE, TRUE, 0);  gtk_box_pack_start(GTK_BOX(vbox_main), cmd_set_use_arrows_true,                     TRUE, TRUE, 0);  gtk_box_pack_start(GTK_BOX(vbox_main), cmd_set_use_arrows_false,                     TRUE, TRUE, 0);  gtk_box_pack_start(GTK_BOX(vbox_main), cmd_set_use_arrows_always_true,                     TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_set_use_arrows_always_false,                        TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_set_case_sensitive_true,                        TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_set_case_sensitive_false,                        TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_get_text, TRUE, TRUE, 0);     gtk_container_add(GTK_CONTAINER(frm_combo), vbox_main);     gtk_widget_show_all (frm_combo);     gtk_main ();     return 0;  }  void destroy_main()  {   gtk_main_quit();  }  void cmd_set_use_arrows_true_clicked()  {  /* This is the default. */  gtk_combo_set_use_arrows(GTK_COMBO(cbo_first), TRUE);  }  void cmd_set_use_arrows_false_clicked()  {    gtk_combo_set_use_arrows(GTK_COMBO(cbo_first), FALSE);  }  void cmd_set_use_arrows_always_true_clicked()  {  /* This is the default. */  gtk_combo_set_use_arrows_always(GTK_COMBO(cbo_first), TRUE);  /* This function essentially sets the wrapping of the drop-down   * list when the user presses the up and down keys. When   * set to TRUE, the use of the up or down keys stops when   * the value displayed is the first or last. From there,   * pressing the up or down key moves the focus to   * the next widget.   */  }  void cmd_set_use_arrows_always_false_clicked()  {    gtk_combo_set_use_arrows_always(GTK_COMBO(cbo_first), FALSE);  /* When set to FALSE, pressing the up or down arrow in the   * the combo box "wraps" the value shown. Try it.   */  }  void cmd_set_case_sensitive_true_clicked()  {    gtk_combo_set_case_sensitive(GTK_COMBO(cbo_first), TRUE);  }  void cmd_set_case_sensitive_false_clicked()  {  /* This is the default. */   /* This option will select the first input in the list for   * you that matches your input if you enter custom text in   * the combo widget. For example, if you manually enter   * "AuGuSt," it will be changed to "August".   *   * Also, note that if you bring up the drop-down box and   * press the down arrow key to the bottom, the selection   * will stop at "August" and not move down to   * select "AUGUST."   */  gtk_combo_set_case_sensitive(GTK_COMBO(cbo_first), FALSE);  }  void cmd_get_text_clicked()  {    g_print("Combo is: %s\n",              gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(cbo_first)->entry)));  } 
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