Added GtkComboBoxText api

This is a derived class of GtkComboBox to replace the
gtk_combo_box_*_text() convenience API.

Fixes https://bugzilla.gnome.org/show_bug.cgi?id=612396

Signed-off-by: Javier Jardón <jjardon@gnome.org>
(cherry picked from commit 7a5a5e9c5d)
This commit is contained in:
Christian Dywan 2010-05-19 17:27:47 +02:00 committed by Matthias Clasen
parent 44e186658b
commit 406c9c9c68
6 changed files with 349 additions and 5 deletions

View File

@ -196,6 +196,7 @@ gtk_public_h_sources = \
gtkcolorseldialog.h \
gtkcombobox.h \
gtkcomboboxentry.h \
gtkcomboboxtext.h \
gtkcontainer.h \
gtkdebug.h \
gtkdialog.h \
@ -455,6 +456,7 @@ gtk_base_c_sources = \
gtkcolorseldialog.c \
gtkcombobox.c \
gtkcomboboxentry.c \
gtkcomboboxtext.c \
gtkcontainer.c \
gtkdialog.c \
gtkdrawingarea.c \

View File

@ -71,6 +71,7 @@
#include <gtk/gtkcolorseldialog.h>
#include <gtk/gtkcombobox.h>
#include <gtk/gtkcomboboxentry.h>
#include <gtk/gtkcomboboxtext.h>
#include <gtk/gtkcontainer.h>
#include <gtk/gtkdebug.h>
#include <gtk/gtkdialog.h>

View File

@ -978,6 +978,19 @@ gtk_combo_box_set_wrap_width
#endif
#endif
#if IN_HEADER(__GTK_COMBO_BOX_TEXT_H__)
#if IN_FILE(__GTK_COMBO_BOX_TEXT_C__)
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_new
gtk_combo_box_text_new_with_entry
gtk_combo_box_text_prepend_text
gtk_combo_box_text_remove
#endif
#endif
#if IN_HEADER(__GTK_COMBO_BOX_ENTRY_H__)
#if IN_FILE(__GTK_COMBO_BOX_ENTRY_C__)
#ifndef GTK_DISABLE_DEPRECATED

View File

@ -879,7 +879,7 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
*
* Whether the combo box has an entry.
*
* Since: 3.0
* Since: 2.24
*/
g_object_class_install_property (object_class,
PROP_HAS_ENTRY,
@ -895,7 +895,7 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
* The column in the combo box's model to associate with strings from the entry
* if the combo was created with #GtkComboBox:has-entry = %TRUE.
*
* Since: 3.0
* Since: 2.24
*/
g_object_class_install_property (object_class,
PROP_ENTRY_TEXT_COLUMN,
@ -1171,6 +1171,7 @@ gtk_combo_box_get_property (GObject *object,
case PROP_ENTRY_TEXT_COLUMN:
g_value_set_int (value, priv->text_column);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -6141,7 +6142,7 @@ gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
*
* Return Value: whether there is an entry in @combo_box.
*
* Since: 3.0
* Since: 2.24
**/
gboolean
gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
@ -6161,7 +6162,7 @@ gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
*
* @combo_box must be created with GtkComboBox:has-entry as %TRUE.
*
* Since: 3.0
* Since: 2.24
*/
void
gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
@ -6194,7 +6195,7 @@ gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
*
* Return value: A column in the data source model of @combo_box.
*
* Since: 3.0
* Since: 2.24
*/
gint
gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)

250
gtk/gtkcomboboxtext.c Normal file
View File

@ -0,0 +1,250 @@
/* 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"
#include "gtkalias.h"
G_DEFINE_TYPE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX);
static void
gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
{
}
static void
gtk_combo_box_text_init (GtkComboBoxText *combo_box)
{
GtkListStore *store;
GtkCellRenderer *cell;
store = gtk_list_store_new (1, G_TYPE_STRING);
gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
g_object_unref (store);
cell = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
"text", 0,
NULL);
}
/**
* 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)
{
return g_object_new (GTK_TYPE_COMBO_BOX_TEXT, NULL);
}
/**
* 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,
NULL);
}
/**
* gtk_combo_box_text_append_text:
* @combo_box: A #GtkComboBoxText
* @text: A string
*
* Appends @string to the list of strings stored in @combo_box.
*
* Since: 2.24
*/
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_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, text_column, text, -1);
}
/**
* gtk_combo_box_text_insert_text:
* @combo_box: A #GtkComboBoxText
* @position: An index to insert @text
* @text: A string
*
* Inserts @string at @position in the list of strings stored in @combo_box.
*
* Since: 2.24
*/
void
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_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)
{
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_list_store_prepend (store, &iter);
gtk_list_store_set (store, &iter, text_column, text, -1);
}
/**
* 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);
}
/**
* gtk_combo_box_text_get_active_text:
* @combo_box: A #GtkComboBoxText
*
* Returns the currently active string in @combo_box or %NULL if none
* is selected.
*
* 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;
gint text_column;
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);
text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
column_type = gtk_tree_model_get_column_type (model, text_column);
g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
gtk_tree_model_get (model, &iter, text_column, &text, -1);
}
return text;
}
#define __GTK_COMBO_BOX_TEXT_C__
#include "gtkaliasdef.c"

77
gtk/gtkcomboboxtext.h Normal file
View File

@ -0,0 +1,77 @@
/* 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.1 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/>.
*/
#if defined(GTK_DISABLE_SINGLE_INCLUDES) && !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#ifndef __GTK_COMBO_BOX_TEXT_H__
#define __GTK_COMBO_BOX_TEXT_H__
#include <gtk/gtkcombobox.h>
G_BEGIN_DECLS
#define GTK_TYPE_COMBO_BOX_TEXT (gtk_combo_box_text_get_type ())
#define GTK_COMBO_BOX_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_COMBO_BOX_TEXT, GtkComboBoxText))
#define GTK_COMBO_BOX_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_COMBO_BOX_TEXT, GtkComboBoxTextClass))
#define GTK_IS_COMBO_BOX_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_COMBO_BOX_TEXT))
#define GTK_IS_COMBO_BOX_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_COMBO_BOX_TEXT))
#define GTK_COMBO_BOX_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_COMBO_BOX_TEXT, GtkComboBoxTextClass))
typedef struct _GtkComboBoxText GtkComboBoxText;
typedef struct _GtkComboBoxTextPrivate GtkComboBoxTextPrivate;
typedef struct _GtkComboBoxTextClass GtkComboBoxTextClass;
struct _GtkComboBoxText
{
/* <private> */
GtkComboBox parent_instance;
GtkComboBoxTextPrivate *priv;
};
struct _GtkComboBoxTextClass
{
GtkComboBoxClass parent_class;
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_combo_box_text_get_type (void) G_GNUC_CONST;
GtkWidget* gtk_combo_box_text_new (void);
GtkWidget* gtk_combo_box_text_new_with_entry (void);
void gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
const gchar *text);
void gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
gint position,
const gchar *text);
void gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
const gchar *text);
void gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
gint position);
gchar *gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box);
G_END_DECLS
#endif /* __GTK_COMBO_BOX_TEXT_H__ */