RE: GtkComboBox question



Tony,

Thanks for the response.  However, I have tried something similar.  I have
now created a simple dialog. It contains two rows.  The first row has a
GtkComboBoxEntry widget and the second has a GtkComboBox widget.  When you
press the OKAY button, I transfer the text from the widgets to a label next
to them.  Both types of combo box have the list set to "10 20 30 40".  If
you enter "15" in the edit field of both combo boxes and press okay, you
will see that the labels are set to 15.  I am trying to prevent the user
from being able to type in the edit box, and only be able to select from the
list.

Here is the glade generated interface.c file:

/*
 * DO NOT EDIT THIS FILE - it is generated by Glade.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <bonobo.h>
#include <gnome.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget*
create_dialog1 (void)
{
  GtkWidget *dialog1;
  GtkWidget *dialog_vbox1;
  GtkWidget *table1;
  GtkWidget *label1;
  GtkWidget *label2;
  GtkWidget *ComboBoxEntry;
  GtkWidget *ComboBoxEntryValue;
  GtkWidget *ComboBoxVal;
  GtkWidget *ComboBox;
  GList *ComboBox_items = NULL;
  GtkWidget *combo_entry1;
  GtkWidget *dialog_action_area1;
  GtkWidget *cancelbutton1;
  GtkWidget *okbutton1;

  dialog1 = gtk_dialog_new ();
  gtk_window_set_title (GTK_WINDOW (dialog1), _("dialog1"));
  gtk_window_set_type_hint (GTK_WINDOW (dialog1),
GDK_WINDOW_TYPE_HINT_DIALOG);

  dialog_vbox1 = GTK_DIALOG (dialog1)->vbox;
  gtk_widget_show (dialog_vbox1);

  table1 = gtk_table_new (2, 3, FALSE);
  gtk_widget_show (table1);
  gtk_box_pack_start (GTK_BOX (dialog_vbox1), table1, TRUE, TRUE, 0);
  gtk_table_set_row_spacings (GTK_TABLE (table1), 4);
  gtk_table_set_col_spacings (GTK_TABLE (table1), 4);

  label1 = gtk_label_new (_("GtkComboBoxEntry"));
  gtk_widget_show (label1);
  gtk_table_attach (GTK_TABLE (table1), label1, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);

  label2 = gtk_label_new (_("GtkComboBox"));
  gtk_widget_show (label2);
  gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);

  ComboBoxEntry = gtk_combo_box_entry_new_text ();
  gtk_widget_show (ComboBoxEntry);
  gtk_table_attach (GTK_TABLE (table1), ComboBoxEntry, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (GTK_FILL), 0, 0);
  gtk_combo_box_append_text (GTK_COMBO_BOX (ComboBoxEntry), _("10"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (ComboBoxEntry), _("20"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (ComboBoxEntry), _("30"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (ComboBoxEntry), _("40"));

  ComboBoxEntryValue = gtk_label_new ("");
  gtk_widget_show (ComboBoxEntryValue);
  gtk_table_attach (GTK_TABLE (table1), ComboBoxEntryValue, 2, 3, 0, 1,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (ComboBoxEntryValue), 0, 0.5);
  gtk_label_set_width_chars (GTK_LABEL (ComboBoxEntryValue), 10);

  ComboBoxVal = gtk_label_new ("");
  gtk_widget_show (ComboBoxVal);
  gtk_table_attach (GTK_TABLE (table1), ComboBoxVal, 2, 3, 1, 2,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (ComboBoxVal), 0, 0.5);
  gtk_label_set_width_chars (GTK_LABEL (ComboBoxVal), 0);

  ComboBox = gtk_combo_new ();
  g_object_set_data (G_OBJECT (GTK_COMBO (ComboBox)->popwin),
                     "GladeParentKey", ComboBox);
  gtk_widget_show (ComboBox);
  gtk_table_attach (GTK_TABLE (table1), ComboBox, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  ComboBox_items = g_list_append (ComboBox_items, (gpointer) _("10"));
  ComboBox_items = g_list_append (ComboBox_items, (gpointer) _("20"));
  ComboBox_items = g_list_append (ComboBox_items, (gpointer) _("30"));
  ComboBox_items = g_list_append (ComboBox_items, (gpointer) _("40"));
  gtk_combo_set_popdown_strings (GTK_COMBO (ComboBox), ComboBox_items);
  g_list_free (ComboBox_items);

  combo_entry1 = GTK_COMBO (ComboBox)->entry;
  gtk_widget_show (combo_entry1);
  gtk_entry_set_text (GTK_ENTRY (combo_entry1), _("10"));
  gtk_entry_set_invisible_char (GTK_ENTRY (combo_entry1), 8226);

  dialog_action_area1 = GTK_DIALOG (dialog1)->action_area;
  gtk_widget_show (dialog_action_area1);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1),
GTK_BUTTONBOX_END);

  cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
  gtk_widget_show (cancelbutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog1), cancelbutton1,
GTK_RESPONSE_CANCEL);
  GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);

  okbutton1 = gtk_button_new_from_stock ("gtk-ok");
  gtk_widget_show (okbutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog1), okbutton1,
GTK_RESPONSE_OK);
  GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);

  g_signal_connect ((gpointer) cancelbutton1, "clicked",
                    G_CALLBACK (on_cancelbutton1_clicked),
                    NULL);
  g_signal_connect ((gpointer) okbutton1, "clicked",
                    G_CALLBACK (on_okbutton1_clicked),
                    NULL);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (dialog1, dialog1, "dialog1");
  GLADE_HOOKUP_OBJECT_NO_REF (dialog1, dialog_vbox1, "dialog_vbox1");
  GLADE_HOOKUP_OBJECT (dialog1, table1, "table1");
  GLADE_HOOKUP_OBJECT (dialog1, label1, "label1");
  GLADE_HOOKUP_OBJECT (dialog1, label2, "label2");
  GLADE_HOOKUP_OBJECT (dialog1, ComboBoxEntry, "ComboBoxEntry");
  GLADE_HOOKUP_OBJECT (dialog1, ComboBoxEntryValue, "ComboBoxEntryValue");
  GLADE_HOOKUP_OBJECT (dialog1, ComboBoxVal, "ComboBoxVal");
  GLADE_HOOKUP_OBJECT (dialog1, ComboBox, "ComboBox");
  GLADE_HOOKUP_OBJECT (dialog1, combo_entry1, "combo_entry1");
  GLADE_HOOKUP_OBJECT_NO_REF (dialog1, dialog_action_area1,
"dialog_action_area1");
  GLADE_HOOKUP_OBJECT (dialog1, cancelbutton1, "cancelbutton1");
  GLADE_HOOKUP_OBJECT (dialog1, okbutton1, "okbutton1");

  return dialog1;
}


Here is my callbacks.c

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"


void on_cancelbutton1_clicked(GtkButton *button, gpointer user_data)
{
        gtk_main_quit();
}


void on_okbutton1_clicked(GtkButton *button, gpointer user_data)
{
        GtkWidget *dialog = gtk_widget_get_ancestor(GTK_WIDGET(button),
GTK_TYPE_WINDOW);
        GtkWidget *cb = lookup_widget(dialog, "ComboBoxEntry");
        GtkWidget *label = lookup_widget(dialog, "ComboBoxEntryValue");
        gchar *value = gtk_combo_box_get_active_text(GTK_COMBO_BOX(cb));
        gtk_label_set_text(GTK_LABEL(label), value);
        cb = lookup_widget(dialog, "ComboBox");
        label = lookup_widget(dialog, "ComboBoxVal");
        value = (gchar
*)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(cb)->entry));
        gtk_label_set_text(GTK_LABEL(label), value);
}


And the glade file:

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>
<requires lib="gnome"/>

<widget class="GtkDialog" id="dialog1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">dialog1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>
  <property name="has_separator">True</property>

  <child internal-child="vbox">
    <widget class="GtkVBox" id="dialog-vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child internal-child="action_area">
        <widget class="GtkHButtonBox" id="dialog-action_area1">
          <property name="visible">True</property>
          <property name="layout_style">GTK_BUTTONBOX_END</property>

          <child>
            <widget class="GtkButton" id="cancelbutton1">
              <property name="visible">True</property>
              <property name="can_default">True</property>
              <property name="can_focus">True</property>
              <property name="label">gtk-cancel</property>
              <property name="use_stock">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">True</property>
              <property name="response_id">-6</property>
              <signal name="clicked" handler="on_cancelbutton1_clicked"
last_modification_time="Thu, 21 Dec 2006 17:56:54 GMT"/>
            </widget>
          </child>

          <child>
            <widget class="GtkButton" id="okbutton1">
              <property name="visible">True</property>
              <property name="can_default">True</property>
              <property name="can_focus">True</property>
              <property name="label">gtk-ok</property>
              <property name="use_stock">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">True</property>
              <property name="response_id">-5</property>
              <signal name="clicked" handler="on_okbutton1_clicked"
last_modification_time="Thu, 21 Dec 2006 17:56:47 GMT"/>
            </widget>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">True</property>
          <property name="pack_type">GTK_PACK_END</property>
        </packing>
      </child>

      <child>
        <widget class="GtkTable" id="table1">
          <property name="visible">True</property>
          <property name="n_rows">2</property>
          <property name="n_columns">3</property>
          <property name="homogeneous">False</property>
          <property name="row_spacing">4</property>
          <property name="column_spacing">4</property>

          <child>
            <widget class="GtkLabel" id="label1">
              <property name="visible">True</property>
              <property name="label"
translatable="yes">GtkComboBoxEntry</property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">-1</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="left_attach">0</property>
              <property name="right_attach">1</property>
              <property name="top_attach">0</property>
              <property name="bottom_attach">1</property>
              <property name="x_options">fill</property>
              <property name="y_options"></property>
            </packing>
          </child>

          <child>
            <widget class="GtkLabel" id="label2">
              <property name="visible">True</property>
              <property name="label"
translatable="yes">GtkComboBox</property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">-1</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="left_attach">0</property>
              <property name="right_attach">1</property>
              <property name="top_attach">1</property>
              <property name="bottom_attach">2</property>
              <property name="x_options">fill</property>
              <property name="y_options"></property>
            </packing>
          </child>

          <child>
            <widget class="GtkComboBoxEntry" id="ComboBoxEntry">
              <property name="visible">True</property>
              <property name="items" translatable="yes">10
20
30
40</property>
              <property name="add_tearoffs">False</property>
              <property name="has_frame">True</property>
              <property name="focus_on_click">True</property>
            </widget>
            <packing>
              <property name="left_attach">1</property>
              <property name="right_attach">2</property>
              <property name="top_attach">0</property>
              <property name="bottom_attach">1</property>
              <property name="x_options">fill</property>
              <property name="y_options">fill</property>
            </packing>
          </child>

          <child>
            <widget class="GtkLabel" id="ComboBoxEntryValue">
              <property name="visible">True</property>
              <property name="label" translatable="yes"></property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">10</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="left_attach">2</property>
              <property name="right_attach">3</property>
              <property name="top_attach">0</property>
              <property name="bottom_attach">1</property>
              <property name="y_options"></property>
            </packing>
          </child>

          <child>
            <widget class="GtkLabel" id="ComboBoxVal">
              <property name="visible">True</property>
              <property name="label" translatable="yes"></property>
              <property name="use_underline">False</property>
              <property name="use_markup">False</property>
              <property name="justify">GTK_JUSTIFY_LEFT</property>
              <property name="wrap">False</property>
              <property name="selectable">False</property>
              <property name="xalign">0</property>
              <property name="yalign">0.5</property>
              <property name="xpad">0</property>
              <property name="ypad">0</property>
              <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
              <property name="width_chars">0</property>
              <property name="single_line_mode">False</property>
              <property name="angle">0</property>
            </widget>
            <packing>
              <property name="left_attach">2</property>
              <property name="right_attach">3</property>
              <property name="top_attach">1</property>
              <property name="bottom_attach">2</property>
              <property name="y_options"></property>
            </packing>
          </child>

          <child>
            <widget class="GtkCombo" id="ComboBox">
              <property name="visible">True</property>
              <property name="value_in_list">False</property>
              <property name="allow_empty">True</property>
              <property name="case_sensitive">False</property>
              <property name="enable_arrow_keys">True</property>
              <property name="enable_arrows_always">False</property>

              <child internal-child="entry">
                <widget class="GtkEntry" id="combo-entry1">
                  <property name="visible">True</property>
                  <property name="can_focus">True</property>
                  <property name="editable">True</property>
                  <property name="visibility">True</property>
                  <property name="max_length">0</property>
                  <property name="text" translatable="yes">10</property>
                  <property name="has_frame">True</property>
                  <property name="invisible_char">.</property>
                  <property name="activates_default">False</property>
                </widget>
              </child>

              <child internal-child="list">
                <widget class="GtkList" id="combo-list1">
                  <property name="visible">True</property>
                  <property
name="selection_mode">GTK_SELECTION_BROWSE</property>

                  <child>
                    <widget class="GtkListItem" id="listitem13">
                      <property name="visible">True</property>
                      <property name="can_focus">True</property>
                      <property name="label"
translatable="yes">10</property>
                    </widget>
                  </child>

                  <child>
                    <widget class="GtkListItem" id="listitem14">
                      <property name="visible">True</property>
                      <property name="can_focus">True</property>
                      <property name="label"
translatable="yes">20</property>
                    </widget>
                  </child>

                  <child>
                    <widget class="GtkListItem" id="listitem15">
                      <property name="visible">True</property>
                      <property name="can_focus">True</property>
                      <property name="label"
translatable="yes">30</property>
                    </widget>
                  </child>

                  <child>
                    <widget class="GtkListItem" id="listitem16">
                      <property name="visible">True</property>
                      <property name="can_focus">True</property>
                      <property name="label"
translatable="yes">40</property>
                    </widget>
                  </child>
                </widget>
              </child>
            </widget>
            <packing>
              <property name="left_attach">1</property>
              <property name="right_attach">2</property>
              <property name="top_attach">1</property>
              <property name="bottom_attach">2</property>
              <property name="y_options"></property>
            </packing>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">True</property>
          <property name="fill">True</property>
        </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface> 


Russell Markus
Sr. Software Engineer
IPMobileNet, Inc.
(949)417-4590 
-----Original Message-----
From: Tony Freeman [mailto:tony freeman insightbb com] 
Sent: Wednesday, December 20, 2006 6:19 PM
To: Russell Markus
Cc: gtk-app-devel-list gnome org
Subject: Re: GtkComboBox question

On Tue, 2006-12-19 at 16:21 -0800, Russell Markus wrote:
I am trying to work on an application which has a combo box.  I want 
to restrict the user to values that are included in the drop down list
only.
My thought was to prevent the user from editing the value in the entry 
box, but I can't seem to find a way to do this.

I am running Fedora Core 6, and developed the application using 
glade-2.  I am not using the lib, but working with the generated C code.

Thanks for any assistance.


Hello Russell,

I had to do something similar recently.  I used glade to build the
interface, but then had to go back and remove the GtkComboBoxEntry widgest
and replace them with GtkCombobox widgets.

Here's the code with a note:

Note #1:  localization_sites is a global.  The value is populated by the
user via a configuration file. (gchar **localization_sites;)


void build_wfo_list (GtkWidget *combobox) {
        gint i=0;
        gint count=0;
        
        /* FILL IN THE DROP DOWN LIST */
        count = g_strv_length(localization_sites);
        for (i=0; i<count; i++) {
        gtk_combo_box_append_text(GTK_COMBO_BOX(combobox),
                                  localization_sites[i]);
        }
                
        /* SET A DEFAULT SELECTION */
        gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0); }

-- Tony




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]