Bug 526987 - GtkCellRendererCombo should allow model to be NULL

2008-06-09  Kristian Rietveld  <kris@gtk.org>

	Bug 526987 - GtkCellRendererCombo should allow model to be NULL

	* gtk/gtkcellrenderercombo.c (gtk_cell_renderer_combo_class_init),
	(gtk_cell_renderer_combo_set_property),
	(gtk_cell_renderer_combo_editing_done),
	(gtk_cell_renderer_combo_start_editing): introduce private class
	struct, allow model to be set to NULL.


svn path=/trunk/; revision=20340
This commit is contained in:
Kristian Rietveld 2008-06-08 22:25:57 +00:00 committed by Kristian Rietveld
parent 6dfbe3b4f8
commit 0e3d5fb732
2 changed files with 75 additions and 16 deletions

View File

@ -1,3 +1,13 @@
2008-06-09 Kristian Rietveld <kris@gtk.org>
Bug 526987 - GtkCellRendererCombo should allow model to be NULL
* gtk/gtkcellrenderercombo.c (gtk_cell_renderer_combo_class_init),
(gtk_cell_renderer_combo_set_property),
(gtk_cell_renderer_combo_editing_done),
(gtk_cell_renderer_combo_start_editing): introduce private class
struct, allow model to be set to NULL.
2008-06-08 Björn Lindqvist <bjourne@gmail.com> 2008-06-08 Björn Lindqvist <bjourne@gmail.com>
Bug 131920 gtkNotebook sends incorrect switch_page value Bug 131920 gtkNotebook sends incorrect switch_page value

View File

@ -32,6 +32,16 @@
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkalias.h" #include "gtkalias.h"
#define GTK_CELL_RENDERER_COMBO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_COMBO, GtkCellRendererComboPrivate))
typedef struct _GtkCellRendererComboPrivate GtkCellRendererComboPrivate;
struct _GtkCellRendererComboPrivate
{
GtkWidget *combo;
};
static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass); static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass);
static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self); static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self);
static void gtk_cell_renderer_combo_finalize (GObject *object); static void gtk_cell_renderer_combo_finalize (GObject *object);
@ -173,6 +183,8 @@ gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
G_TYPE_NONE, 2, G_TYPE_NONE, 2,
G_TYPE_STRING, G_TYPE_STRING,
GTK_TYPE_TREE_ITER); GTK_TYPE_TREE_ITER);
g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
} }
static void static void
@ -257,17 +269,28 @@ gtk_cell_renderer_combo_set_property (GObject *object,
case PROP_MODEL: case PROP_MODEL:
{ {
GObject *object; GObject *object;
GtkCellRendererComboPrivate *priv;
object = g_value_get_object (value); priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell);
g_return_if_fail (GTK_IS_TREE_MODEL (object));
g_object_ref (object);
if (cell->model) if (cell->model)
{ {
if (priv->combo)
gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo), NULL);
g_object_unref (cell->model); g_object_unref (cell->model);
cell->model = NULL; cell->model = NULL;
} }
object = g_value_get_object (value);
if (object)
{
g_return_if_fail (GTK_IS_TREE_MODEL (object));
g_object_ref (object);
cell->model = GTK_TREE_MODEL (object); cell->model = GTK_TREE_MODEL (object);
if (priv->combo)
gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo), cell->model);
}
break; break;
} }
case PROP_TEXT_COLUMN: case PROP_TEXT_COLUMN:
@ -312,8 +335,10 @@ gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
GtkCellRendererCombo *cell; GtkCellRendererCombo *cell;
GtkEntry *entry; GtkEntry *entry;
gboolean canceled; gboolean canceled;
GtkCellRendererComboPrivate *priv;
cell = GTK_CELL_RENDERER_COMBO (data); cell = GTK_CELL_RENDERER_COMBO (data);
priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (data);
if (cell->focus_out_id > 0) if (cell->focus_out_id > 0)
{ {
@ -324,7 +349,10 @@ gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
canceled = _gtk_combo_box_editing_canceled (GTK_COMBO_BOX (combo)); canceled = _gtk_combo_box_editing_canceled (GTK_COMBO_BOX (combo));
gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled); gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
if (canceled) if (canceled)
{
priv->combo = NULL;
return; return;
}
if (GTK_IS_COMBO_BOX_ENTRY (combo)) if (GTK_IS_COMBO_BOX_ENTRY (combo))
{ {
@ -334,13 +362,17 @@ gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
else else
{ {
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo)); model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
if (model
&& gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
gtk_tree_model_get (model, &iter, cell->text_column, &new_text, -1); gtk_tree_model_get (model, &iter, cell->text_column, &new_text, -1);
} }
path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH); path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
g_signal_emit_by_name (cell, "edited", path, new_text); g_signal_emit_by_name (cell, "edited", path, new_text);
priv->combo = NULL;
g_free (new_text); g_free (new_text);
} }
@ -397,18 +429,26 @@ gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
GtkCellRendererText *cell_text; GtkCellRendererText *cell_text;
GtkWidget *combo; GtkWidget *combo;
SearchData search_data; SearchData search_data;
GtkCellRendererComboPrivate *priv;
cell_text = GTK_CELL_RENDERER_TEXT (cell); cell_text = GTK_CELL_RENDERER_TEXT (cell);
if (cell_text->editable == FALSE) if (cell_text->editable == FALSE)
return NULL; return NULL;
cell_combo = GTK_CELL_RENDERER_COMBO (cell); cell_combo = GTK_CELL_RENDERER_COMBO (cell);
if (cell_combo->model == NULL || cell_combo->text_column < 0) if (cell_combo->text_column < 0)
return NULL; return NULL;
priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell_combo);
if (cell_combo->has_entry) if (cell_combo->has_entry)
{ {
combo = gtk_combo_box_entry_new_with_model (cell_combo->model, cell_combo->text_column); combo = gtk_combo_box_entry_new ();
if (cell_combo->model)
gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo),
cell_combo->text_column);
if (cell_text->text) if (cell_text->text)
gtk_entry_set_text (GTK_ENTRY (GTK_BIN (combo)->child), gtk_entry_set_text (GTK_ENTRY (GTK_BIN (combo)->child),
@ -417,13 +457,19 @@ gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
else else
{ {
cell = gtk_cell_renderer_text_new (); cell = gtk_cell_renderer_text_new ();
combo = gtk_combo_box_new_with_model (cell_combo->model);
combo = gtk_combo_box_new ();
if (cell_combo->model)
gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE); gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
cell, "text", cell_combo->text_column, cell, "text", cell_combo->text_column,
NULL); NULL);
/* determine the current value */ /* determine the current value */
if (cell_combo->model)
{
search_data.cell = cell_combo; search_data.cell = cell_combo;
search_data.found = FALSE; search_data.found = FALSE;
gtk_tree_model_foreach (cell_combo->model, find_text, &search_data); gtk_tree_model_foreach (cell_combo->model, find_text, &search_data);
@ -431,6 +477,7 @@ gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
&(search_data.iter)); &(search_data.iter));
} }
}
g_object_set (combo, "has-frame", FALSE, NULL); g_object_set (combo, "has-frame", FALSE, NULL);
g_object_set_data_full (G_OBJECT (combo), g_object_set_data_full (G_OBJECT (combo),
@ -450,6 +497,8 @@ gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
G_CALLBACK (gtk_cell_renderer_combo_focus_out_event), G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
cell_combo); cell_combo);
priv->combo = combo;
return GTK_CELL_EDITABLE (combo); return GTK_CELL_EDITABLE (combo);
} }