2010-05-19 15:27:47 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Christian Dywan
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcomboboxtext.h"
|
|
|
|
#include "gtkcombobox.h"
|
|
|
|
#include "gtkcellrenderertext.h"
|
|
|
|
#include "gtkcelllayout.h"
|
|
|
|
|
2010-10-15 20:52:25 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gtkcomboboxtext
|
|
|
|
* @Short_description: A simple, text-only combo box
|
|
|
|
* @Title: GtkComboBoxText
|
2010-10-23 00:52:24 +00:00
|
|
|
* @See_also: #GtkComboBox
|
2010-10-15 20:52:25 +00:00
|
|
|
*
|
|
|
|
* A GtkComboBoxText is a simple variant of #GtkComboBox that hides
|
|
|
|
* the model-view complexity for simple text-only use cases.
|
|
|
|
*
|
|
|
|
* To create a GtkComboBoxText, use gtk_combo_box_text_new() or
|
|
|
|
* gtk_combo_box_text_new_with_entry().
|
|
|
|
*
|
|
|
|
* You can add items to a GtkComboBoxText with
|
|
|
|
* gtk_combo_box_text_append_text(), gtk_combo_box_text_insert_text()
|
|
|
|
* or gtk_combo_box_text_prepend_text() and remove options with
|
2010-10-16 00:07:01 +00:00
|
|
|
* gtk_combo_box_text_remove().
|
2010-12-13 20:45:07 +00:00
|
|
|
*
|
|
|
|
* If the GtkComboBoxText contains an entry (via the 'has-entry' property),
|
|
|
|
* its contents can be retrieved using gtk_combo_box_text_get_active_text().
|
|
|
|
* The entry itself can be accessed by calling gtk_bin_get_child() on the
|
|
|
|
* combo box.
|
2010-10-15 20:52:25 +00:00
|
|
|
*/
|
|
|
|
|
2010-05-19 15:27:47 +00:00
|
|
|
G_DEFINE_TYPE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX);
|
|
|
|
|
2010-10-18 12:13:41 +00:00
|
|
|
static GObject *
|
|
|
|
gtk_combo_box_text_constructor (GType type,
|
|
|
|
guint n_construct_properties,
|
|
|
|
GObjectConstructParam *construct_properties)
|
2010-05-19 15:27:47 +00:00
|
|
|
{
|
2010-10-18 12:13:41 +00:00
|
|
|
GObject *object;
|
|
|
|
|
|
|
|
object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
|
|
|
|
(type, n_construct_properties, construct_properties);
|
|
|
|
|
|
|
|
if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
|
|
|
|
{
|
|
|
|
GtkCellRenderer *cell;
|
|
|
|
|
|
|
|
cell = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
|
|
|
|
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
|
|
|
|
"text", 0,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
2010-05-19 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_combo_box_text_init (GtkComboBoxText *combo_box)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
|
2010-11-30 05:07:08 +00:00
|
|
|
store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
|
2010-05-19 15:27:47 +00:00
|
|
|
gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
|
|
|
|
g_object_unref (store);
|
2010-10-18 12:13:41 +00:00
|
|
|
}
|
2010-05-19 15:27:47 +00:00
|
|
|
|
2010-10-18 12:13:41 +00:00
|
|
|
static void
|
|
|
|
gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class;
|
|
|
|
|
|
|
|
object_class = (GObjectClass *)klass;
|
|
|
|
object_class->constructor = gtk_combo_box_text_constructor;
|
2010-05-19 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 12:13:41 +00:00
|
|
|
|
2010-05-19 15:27:47 +00:00
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_new:
|
|
|
|
*
|
|
|
|
* Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
|
|
|
|
* strings. See gtk_combo_box_entry_new_with_text().
|
|
|
|
*
|
|
|
|
* Return value: A new #GtkComboBoxText
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
GtkWidget *
|
|
|
|
gtk_combo_box_text_new (void)
|
|
|
|
{
|
2010-10-16 12:17:34 +00:00
|
|
|
return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
|
|
|
|
"entry-text-column", 0,
|
2010-11-30 05:07:08 +00:00
|
|
|
"id-column", 1,
|
2010-10-16 12:17:34 +00:00
|
|
|
NULL);
|
2010-05-19 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2010-10-15 20:16:45 +00:00
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_new_with_entry:
|
|
|
|
*
|
|
|
|
* Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
|
|
|
|
* strings. The combo box created by this function has an entry.
|
|
|
|
*
|
|
|
|
* Return value: a new #GtkComboBoxText
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
GtkWidget *
|
|
|
|
gtk_combo_box_text_new_with_entry (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
|
|
|
|
"has-entry", TRUE,
|
|
|
|
"entry-text-column", 0,
|
2010-11-30 05:07:08 +00:00
|
|
|
"id-column", 1,
|
2010-10-15 20:16:45 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2010-05-19 15:27:47 +00:00
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_append_text:
|
|
|
|
* @combo_box: A #GtkComboBoxText
|
|
|
|
* @text: A string
|
|
|
|
*
|
2010-10-23 01:03:50 +00:00
|
|
|
* Appends @text to the list of strings stored in @combo_box.
|
2010-05-19 15:27:47 +00:00
|
|
|
*
|
2010-11-30 05:07:08 +00:00
|
|
|
* This is the same as calling gtk_combo_box_text_insert_text() with a
|
|
|
|
* position of -1.
|
|
|
|
*
|
2010-05-19 15:27:47 +00:00
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
|
|
|
|
const gchar *text)
|
|
|
|
{
|
2010-11-30 05:07:08 +00:00
|
|
|
gtk_combo_box_text_insert (combo_box, -1, NULL, text);
|
2010-10-22 21:33:30 +00:00
|
|
|
}
|
2010-05-19 15:27:47 +00:00
|
|
|
|
2010-10-22 21:33:30 +00:00
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_prepend_text:
|
|
|
|
* @combo_box: A #GtkComboBox
|
|
|
|
* @text: A string
|
|
|
|
*
|
2010-10-23 00:52:24 +00:00
|
|
|
* Prepends @text to the list of strings stored in @combo_box.
|
2010-10-22 21:33:30 +00:00
|
|
|
*
|
2010-11-30 05:07:08 +00:00
|
|
|
* This is the same as calling gtk_combo_box_text_insert_text() with a
|
|
|
|
* position of 0.
|
|
|
|
*
|
2010-10-22 21:33:30 +00:00
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
|
|
|
|
const gchar *text)
|
|
|
|
{
|
2010-11-30 05:07:08 +00:00
|
|
|
gtk_combo_box_text_insert (combo_box, 0, NULL, text);
|
2010-05-19 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_insert_text:
|
|
|
|
* @combo_box: A #GtkComboBoxText
|
|
|
|
* @position: An index to insert @text
|
|
|
|
* @text: A string
|
|
|
|
*
|
2010-10-23 00:52:24 +00:00
|
|
|
* Inserts @text at @position in the list of strings stored in @combo_box.
|
2010-05-19 15:27:47 +00:00
|
|
|
*
|
2010-11-30 05:07:08 +00:00
|
|
|
* If @position is negative then @text is appended.
|
|
|
|
*
|
|
|
|
* This is the same as calling gtk_combo_box_text_insert() with a %NULL
|
|
|
|
* ID string.
|
|
|
|
*
|
2010-05-19 15:27:47 +00:00
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
|
|
|
|
gint position,
|
|
|
|
const gchar *text)
|
2010-11-30 05:07:08 +00:00
|
|
|
{
|
|
|
|
gtk_combo_box_text_insert (combo_box, position, NULL, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_append:
|
|
|
|
* @combo_box: A #GtkComboBoxText
|
|
|
|
* @text: A string
|
|
|
|
*
|
|
|
|
* Appends @text to the list of strings stored in @combo_box. If @id is
|
|
|
|
* non-%NULL then it is used as the ID of the row.
|
|
|
|
*
|
|
|
|
* This is the same as calling gtk_combo_box_text_insert() with a
|
|
|
|
* position of -1.
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_append (GtkComboBoxText *combo_box,
|
|
|
|
const gchar *id,
|
|
|
|
const gchar *text)
|
|
|
|
{
|
|
|
|
gtk_combo_box_text_insert (combo_box, -1, id, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_prepend:
|
|
|
|
* @combo_box: A #GtkComboBox
|
|
|
|
* @text: A string
|
|
|
|
*
|
|
|
|
* Prepends @text to the list of strings stored in @combo_box. If @id
|
|
|
|
* is non-%NULL then it is used as the ID of the row.
|
|
|
|
*
|
|
|
|
* This is the same as calling gtk_combo_box_text_insert() with a
|
|
|
|
* position of 0.
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
|
|
|
|
const gchar *id,
|
|
|
|
const gchar *text)
|
|
|
|
{
|
|
|
|
gtk_combo_box_text_insert (combo_box, 0, id, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_insert:
|
|
|
|
* @combo_box: A #GtkComboBoxText
|
|
|
|
* @position: An index to insert @text
|
|
|
|
* @id: a string ID for this value, or %NULL
|
|
|
|
* @text: A string to display
|
|
|
|
*
|
|
|
|
* Inserts @text at @position in the list of strings stored in @combo_box.
|
|
|
|
* If @id is non-%NULL then it is used as the ID of the row. See
|
|
|
|
* #GtkComboBox::id-column.
|
|
|
|
*
|
|
|
|
* If @position is negative then @text is appended.
|
|
|
|
*
|
|
|
|
* Since: 3.0
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_insert (GtkComboBoxText *combo_box,
|
|
|
|
gint position,
|
|
|
|
const gchar *id,
|
|
|
|
const gchar *text)
|
2010-05-19 15:27:47 +00:00
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
2010-10-15 13:26:00 +00:00
|
|
|
gint text_column;
|
2010-05-19 15:27:47 +00:00
|
|
|
gint column_type;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
|
|
|
|
g_return_if_fail (text != NULL);
|
|
|
|
|
2010-11-30 05:07:08 +00:00
|
|
|
if (position < 0)
|
|
|
|
position = G_MAXINT;
|
|
|
|
|
2010-05-19 15:27:47 +00:00
|
|
|
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
|
|
|
|
g_return_if_fail (GTK_IS_LIST_STORE (store));
|
2010-10-15 20:16:45 +00:00
|
|
|
text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
|
2010-12-11 14:12:47 +00:00
|
|
|
g_return_if_fail (text_column >= 0);
|
2010-10-15 13:26:00 +00:00
|
|
|
column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
|
2010-05-19 15:27:47 +00:00
|
|
|
g_return_if_fail (column_type == G_TYPE_STRING);
|
|
|
|
|
2010-10-22 21:33:30 +00:00
|
|
|
gtk_list_store_insert (store, &iter, position);
|
2010-10-23 17:28:05 +00:00
|
|
|
gtk_list_store_set (store, &iter, text_column, text, -1);
|
2010-11-30 05:07:08 +00:00
|
|
|
|
|
|
|
if (id != NULL)
|
|
|
|
{
|
|
|
|
gint id_column;
|
|
|
|
|
|
|
|
id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
|
2010-12-11 14:12:47 +00:00
|
|
|
g_return_if_fail (id_column >= 0);
|
2010-11-30 05:07:08 +00:00
|
|
|
column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
|
|
|
|
g_return_if_fail (column_type == G_TYPE_STRING);
|
|
|
|
|
|
|
|
gtk_list_store_set (store, &iter, id_column, id, -1);
|
|
|
|
}
|
2010-05-19 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_remove:
|
|
|
|
* @combo_box: A #GtkComboBox
|
|
|
|
* @position: Index of the item to remove
|
|
|
|
*
|
|
|
|
* Removes the string at @position from @combo_box.
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
|
|
|
|
gint position)
|
|
|
|
{
|
|
|
|
GtkTreeModel *model;
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
|
|
|
|
g_return_if_fail (position >= 0);
|
|
|
|
|
|
|
|
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
|
|
|
|
store = GTK_LIST_STORE (model);
|
|
|
|
g_return_if_fail (GTK_IS_LIST_STORE (store));
|
|
|
|
|
|
|
|
if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
|
|
|
|
gtk_list_store_remove (store, &iter);
|
|
|
|
}
|
|
|
|
|
2010-10-21 12:25:08 +00:00
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_remove_all:
|
|
|
|
* @combo_box: A #GtkComboBoxText
|
|
|
|
*
|
|
|
|
* Removes all the text entries from the combo box.
|
|
|
|
*
|
|
|
|
* Since: 3.0
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
|
|
|
|
|
|
|
|
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
|
|
|
|
gtk_list_store_clear (store);
|
|
|
|
}
|
|
|
|
|
2010-05-19 15:27:47 +00:00
|
|
|
/**
|
|
|
|
* gtk_combo_box_text_get_active_text:
|
|
|
|
* @combo_box: A #GtkComboBoxText
|
|
|
|
*
|
2010-12-13 20:45:07 +00:00
|
|
|
* Returns the currently active string in @combo_box, or %NULL if none
|
|
|
|
* is selected. If @combo_box contains an entry, this function will return
|
|
|
|
* its contents (which will not necessarily be an item from the list).
|
2010-05-19 15:27:47 +00:00
|
|
|
*
|
|
|
|
* Returns: a newly allocated string containing the currently active text.
|
|
|
|
* Must be freed with g_free().
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gchar *text = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
|
|
|
|
|
|
|
|
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
|
|
|
|
{
|
|
|
|
GtkTreeModel *model;
|
2010-10-15 13:26:00 +00:00
|
|
|
gint text_column;
|
2010-05-19 15:27:47 +00:00
|
|
|
gint column_type;
|
|
|
|
|
|
|
|
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
|
|
|
|
g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
|
2010-10-15 13:26:00 +00:00
|
|
|
text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
|
2010-12-11 14:12:47 +00:00
|
|
|
g_return_val_if_fail (text_column >= 0, NULL);
|
2010-10-15 13:26:00 +00:00
|
|
|
column_type = gtk_tree_model_get_column_type (model, text_column);
|
2010-05-19 15:27:47 +00:00
|
|
|
g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
|
2010-10-15 13:26:00 +00:00
|
|
|
gtk_tree_model_get (model, &iter, text_column, &text, -1);
|
2010-05-19 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|