Commit 9fb36fff authored by Matthias Clasen's avatar Matthias Clasen

Add a way to associate numeric ids with combobox values

This will let us use combo boxes as property editors for
GSettings enmerations easily.
parent 38dbb840
......@@ -804,6 +804,8 @@ gtk_combo_box_get_button_sensitivity
gtk_combo_box_get_has_entry
gtk_combo_box_set_entry_text_column
gtk_combo_box_get_entry_text_column
gtk_combo_box_set_id_column
gtk_combo_box_get_id_column
<SUBSECTION Standard>
GTK_TYPE_COMBO_BOX
GTK_COMBO_BOX
......@@ -823,8 +825,9 @@ GtkComboBoxText
gtk_combo_box_text_new
gtk_combo_box_text_new_with_entry
gtk_combo_box_text_append_text
gtk_combo_box_text_insert_text
gtk_combo_box_text_prepend_text
gtk_combo_box_text_insert_text
gtk_combo_box_text_insert_text_with_id
gtk_combo_box_text_remove
gtk_combo_box_text_remove_all
gtk_combo_box_text_get_active_text
......
......@@ -801,6 +801,7 @@ gtk_combo_box_get_column_span_column
gtk_combo_box_get_entry_text_column
gtk_combo_box_get_focus_on_click
gtk_combo_box_get_has_entry
gtk_combo_box_get_id_column
gtk_combo_box_get_model
gtk_combo_box_get_popup_accessible
gtk_combo_box_get_popup_fixed_width
......@@ -822,6 +823,7 @@ gtk_combo_box_set_add_tearoffs
gtk_combo_box_set_column_span_column
gtk_combo_box_set_entry_text_column
gtk_combo_box_set_focus_on_click
gtk_combo_box_set_id_column
gtk_combo_box_set_model
gtk_combo_box_set_popup_fixed_width
gtk_combo_box_set_row_separator_func
......@@ -838,6 +840,7 @@ gtk_combo_box_text_append_text
gtk_combo_box_text_get_active_text
gtk_combo_box_text_get_type G_GNUC_CONST
gtk_combo_box_text_insert_text
gtk_combo_box_text_insert_text_with_id
gtk_combo_box_text_new
gtk_combo_box_text_new_with_entry
gtk_combo_box_text_prepend_text
......
......@@ -143,6 +143,8 @@ struct _GtkComboBoxPrivate
gint text_column;
GtkCellRenderer *text_renderer;
gint id_column;
GSList *cells;
guint popup_in_progress : 1;
......@@ -245,7 +247,8 @@ enum {
PROP_EDITING_CANCELED,
PROP_HAS_ENTRY,
PROP_ENTRY_TEXT_COLUMN,
PROP_POPUP_FIXED_WIDTH
PROP_POPUP_FIXED_WIDTH,
PROP_ID_COLUMN
};
static guint combo_box_signals[LAST_SIGNAL] = {0,};
......@@ -948,6 +951,23 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE));
/**
* GtkComboBox:id-column:
*
* The column in the combo box's model that provides numeric
* IDs for the values in the model, if != -1.
*
* Since: 3.0
*/
g_object_class_install_property (object_class,
PROP_ID_COLUMN,
g_param_spec_int ("id-column",
P_("ID Column"),
P_("The column in the combo box's model that provides "
"numeric IDs for the values in the model"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE));
/**
* GtkComboBox:popup-fixed-width:
*
......@@ -1077,6 +1097,7 @@ gtk_combo_box_init (GtkComboBox *combo_box)
priv->text_column = -1;
priv->text_renderer = NULL;
priv->id_column = -1;
gtk_combo_box_check_appearance (combo_box);
}
......@@ -1168,6 +1189,10 @@ gtk_combo_box_set_property (GObject *object,
gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
break;
case PROP_ID_COLUMN:
gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
......@@ -1245,6 +1270,10 @@ gtk_combo_box_get_property (GObject *object,
g_value_set_int (value, priv->text_column);
break;
case PROP_ID_COLUMN:
g_value_set_int (value, priv->id_column);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
......@@ -6544,14 +6573,14 @@ gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
xpad = 2 * (border_width + cell_style->xthickness);
ypad = 2 * (border_width + cell_style->ythickness);
}
}
size -= but_width;
size -= 2 * focus_width;
size -= xpad;
gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
min_height = MAX (min_height, but_height);
nat_height = MAX (nat_height, but_height);
......@@ -6573,3 +6602,50 @@ gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
if (natural_size)
*natural_size = nat_height;
}
/**
* gtk_combo_box_set_id_column:
* @combo_box: A #GtkComboBox
* @id_column: A column in @model to get numeric IDs for values from
*
* Sets the model column which @combo_box should use to get numeric IDs
* for values from. The column @id_column in the model of @combo_box
* must be of type %G_TYPE_INT.
*
* Since: 3.0
*/
void
gtk_combo_box_set_id_column (GtkComboBox *combo_box,
gint id_column)
{
GtkComboBoxPrivate *priv = combo_box->priv;
GtkTreeModel *model;
g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
model = gtk_combo_box_get_model (combo_box);
g_return_if_fail (id_column >= 0);
g_return_if_fail (model == NULL || id_column < gtk_tree_model_get_n_columns (model));
priv->id_column = id_column;
}
/**
* gtk_combo_box_get_id_column:
* @combo_box: A #GtkComboBox
*
* Returns the column which @combo_box is using to get numeric IDs
* for values from.
*
* Return value: A column in the data source model of @combo_box.
*
* Since: 3.0
*/
gint
gtk_combo_box_get_id_column (GtkComboBox *combo_box)
{
g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
return combo_box->priv->id_column;
}
......@@ -133,6 +133,9 @@ void gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
void gtk_combo_box_popdown (GtkComboBox *combo_box);
AtkObject* gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box);
gint gtk_combo_box_get_id_column (GtkComboBox *combo_box);
void gtk_combo_box_set_id_column (GtkComboBox *combo_box,
gint id_column);
G_END_DECLS
......
......@@ -72,7 +72,7 @@ gtk_combo_box_text_init (GtkComboBoxText *combo_box)
{
GtkListStore *store;
store = gtk_list_store_new (1, G_TYPE_STRING);
store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
g_object_unref (store);
}
......@@ -102,6 +102,7 @@ gtk_combo_box_text_new (void)
{
return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
"entry-text-column", 0,
"id-column", 1,
NULL);
}
......@@ -121,6 +122,7 @@ gtk_combo_box_text_new_with_entry (void)
return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
"has-entry", TRUE,
"entry-text-column", 0,
"id-column", 1,
NULL);
}
......@@ -137,22 +139,23 @@ void
gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
const gchar *text)
{
GtkListStore *store;
GtkTreeIter iter;
gint text_column;
gint column_type;
g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
g_return_if_fail (text != NULL);
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
g_return_if_fail (GTK_IS_LIST_STORE (store));
text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
g_return_if_fail (column_type == G_TYPE_STRING);
gtk_combo_box_text_insert_text (combo_box, G_MAXINT, text);
}
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, text_column, text, -1);
/**
* gtk_combo_box_text_prepend_text:
* @combo_box: A #GtkComboBox
* @text: A string
*
* Prepends @string to the list of strings stored in @combo_box.
*
* Since: 2.24
*/
void
gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
const gchar *text)
{
gtk_combo_box_text_insert_text (combo_box, 0, text);
}
/**
......@@ -170,44 +173,35 @@ gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
gint position,
const gchar *text)
{
GtkListStore *store;
GtkTreeIter iter;
gint text_column;
gint column_type;
g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
g_return_if_fail (position >= 0);
g_return_if_fail (text != NULL);
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
g_return_if_fail (GTK_IS_LIST_STORE (store));
text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
g_return_if_fail (column_type == G_TYPE_STRING);
gtk_list_store_insert (store, &iter, position);
gtk_list_store_set (store, &iter, text_column, text, -1);
gtk_combo_box_text_insert_text_with_id (combo_box, position, text, 0);
}
/**
* gtk_combo_box_text_prepend_text:
* @combo_box: A #GtkComboBox
* gtk_combo_box_text_insert_text_with_id:
* @combo_box: A #GtkComboBoxText
* @position: An index to insert @text
* @text: A string
* @id: a numeric ID for this value
*
* Prepends @string to the list of strings stored in @combo_box.
* Inserts @string at @position in the list of strings stored in @combo_box,
* and sets its numeric ID to @id. See #GtkComboBox::id-column.
*
* Since: 2.24
* Since: 3.0
*/
void
gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
const gchar *text)
gtk_combo_box_text_insert_text_with_id (GtkComboBoxText *combo_box,
gint position,
const gchar *text,
gint id)
{
GtkListStore *store;
GtkTreeIter iter;
gint text_column;
gint id_column;
gint column_type;
g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
g_return_if_fail (position >= 0);
g_return_if_fail (text != NULL);
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
......@@ -215,11 +209,18 @@ gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
g_return_if_fail (column_type == G_TYPE_STRING);
id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
if (id_column != -1)
{
column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
g_return_if_fail (column_type == G_TYPE_INT);
}
gtk_list_store_prepend (store, &iter);
gtk_list_store_set (store, &iter, text_column, text, -1);
gtk_list_store_insert (store, &iter, position);
gtk_list_store_set (store, &iter, text_column, text, id_column, id, -1);
}
/**
* gtk_combo_box_text_remove:
* @combo_box: A #GtkComboBox
......
......@@ -72,6 +72,10 @@ void gtk_combo_box_text_remove (GtkComboBoxText *combo_box
void gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box);
gchar *gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box);
void gtk_combo_box_text_insert_text_with_id (GtkComboBoxText *combo_box,
gint position,
const gchar *text,
gint id);
G_END_DECLS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment