]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcellrenderercombo.c
bin: initialize out variables to zero for get_preferred_ functions.
[~andy/gtk] / gtk / gtkcellrenderercombo.c
index 13ad0304e44a1f9960f36569be3caa833d6ec8e8..0518d1430727d06c01c2e2c641ff1fb56d5f0c72 100644 (file)
@@ -12,9 +12,7 @@
  * 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, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 #include "gtkcellrenderercombo.h"
 #include "gtkcellrenderertext.h"
 #include "gtkcombobox.h"
-#include "gtkcomboboxentry.h"
 #include "gtkmarshalers.h"
 #include "gtkprivate.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;
+/**
+ * SECTION:gtkcellrenderercombo
+ * @Short_description: Renders a combobox in a cell
+ * @Title: GtkCellRendererCombo
+ *
+ * #GtkCellRendererCombo renders text in a cell like #GtkCellRendererText from
+ * which it is derived. But while #GtkCellRendererText offers a simple entry to
+ * edit the text, #GtkCellRendererCombo offers a #GtkComboBox
+ * widget to edit the text. The values to display in the combo box are taken from
+ * the tree model specified in the #GtkCellRendererCombo:model property.
+ *
+ * The combo cell renderer takes care of adding a text cell renderer to the combo
+ * box and sets it to display the column specified by its
+ * #GtkCellRendererCombo:text-column property. Further properties of the combo box
+ * can be set in a handler for the #GtkCellRenderer::editing-started signal.
+ *
+ * The #GtkCellRendererCombo cell renderer was added in GTK+ 2.6.
+ */
+
+
 struct _GtkCellRendererComboPrivate
 {
+  GtkTreeModel *model;
+
   GtkWidget *combo;
+
+  gboolean has_entry;
+
+  gint text_column;
+
+  gulong focus_out_id;
 };
 
 
@@ -54,12 +77,12 @@ static void gtk_cell_renderer_combo_set_property (GObject      *object,
                                                  GParamSpec   *pspec);
 
 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer     *cell,
-                                                              GdkEvent            *event,
-                                                              GtkWidget           *widget,
-                                                              const gchar         *path,
-                                                              GdkRectangle        *background_area,
-                                                              GdkRectangle        *cell_area,
-                                                              GtkCellRendererState flags);
+                                                               GdkEvent            *event,
+                                                               GtkWidget           *widget,
+                                                               const gchar         *path,
+                                                               const GdkRectangle  *background_area,
+                                                               const GdkRectangle  *cell_area,
+                                                               GtkCellRendererState flags);
 
 enum {
   PROP_0,
@@ -188,10 +211,17 @@ gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
 static void
 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
 {
-  self->model = NULL;
-  self->text_column = -1;
-  self->has_entry = TRUE;
-  self->focus_out_id = 0;
+  GtkCellRendererComboPrivate *priv;
+
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+                                            GTK_TYPE_CELL_RENDERER_COMBO,
+                                            GtkCellRendererComboPrivate);
+  priv = self->priv;
+
+  priv->model = NULL;
+  priv->text_column = -1;
+  priv->has_entry = TRUE;
+  priv->focus_out_id = 0;
 }
 
 /**
@@ -219,11 +249,12 @@ static void
 gtk_cell_renderer_combo_finalize (GObject *object)
 {
   GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
+  GtkCellRendererComboPrivate *priv = cell->priv;
   
-  if (cell->model)
+  if (priv->model)
     {
-      g_object_unref (cell->model);
-      cell->model = NULL;
+      g_object_unref (priv->model);
+      priv->model = NULL;
     }
   
   G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
@@ -236,17 +267,18 @@ gtk_cell_renderer_combo_get_property (GObject    *object,
                                      GParamSpec *pspec)
 {
   GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
+  GtkCellRendererComboPrivate *priv = cell->priv;
 
   switch (prop_id)
     {
     case PROP_MODEL:
-      g_value_set_object (value, cell->model);
+      g_value_set_object (value, priv->model);
       break; 
     case PROP_TEXT_COLUMN:
-      g_value_set_int (value, cell->text_column);
+      g_value_set_int (value, priv->text_column);
       break;
     case PROP_HAS_ENTRY:
-      g_value_set_boolean (value, cell->has_entry);
+      g_value_set_boolean (value, priv->has_entry);
       break;
    default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -261,27 +293,24 @@ gtk_cell_renderer_combo_set_property (GObject      *object,
                                      GParamSpec   *pspec)
 {
   GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
+  GtkCellRendererComboPrivate *priv = cell->priv;
 
   switch (prop_id)
     {
     case PROP_MODEL:
       {
-        GtkCellRendererComboPrivate *priv;
-
-        priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell);
-
-        if (cell->model)
-          g_object_unref (cell->model);
-        cell->model = GTK_TREE_MODEL (g_value_get_object (value));
-        if (cell->model)
-          g_object_ref (cell->model);
+        if (priv->model)
+          g_object_unref (priv->model);
+        priv->model = GTK_TREE_MODEL (g_value_get_object (value));
+        if (priv->model)
+          g_object_ref (priv->model);
         break;
       }
     case PROP_TEXT_COLUMN:
-      cell->text_column = g_value_get_int (value);
+      priv->text_column = g_value_get_int (value);
       break;
     case PROP_HAS_ENTRY:
-      cell->has_entry = g_value_get_boolean (value);
+      priv->has_entry = g_value_get_boolean (value);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -322,12 +351,12 @@ gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
   GtkCellRendererComboPrivate *priv;
 
   cell = GTK_CELL_RENDERER_COMBO (data);
-  priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (data);
+  priv = cell->priv;
 
-  if (cell->focus_out_id > 0)
+  if (priv->focus_out_id > 0)
     {
-      g_signal_handler_disconnect (combo, cell->focus_out_id);
-      cell->focus_out_id = 0;
+      g_signal_handler_disconnect (combo, priv->focus_out_id);
+      priv->focus_out_id = 0;
     }
 
   g_object_get (combo,
@@ -340,7 +369,7 @@ gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
       return;
     }
 
-  if (GTK_IS_COMBO_BOX_ENTRY (combo))
+  if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo)))
     {
       entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
       new_text = g_strdup (gtk_entry_get_text (entry));
@@ -351,7 +380,7 @@ gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
 
       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, priv->text_column, &new_text, -1);
     }
 
   path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
@@ -386,17 +415,23 @@ find_text (GtkTreeModel *model,
           GtkTreeIter  *iter, 
           gpointer      data)
 {
+  GtkCellRendererComboPrivate *priv;
   SearchData *search_data = (SearchData *)data;
-  gchar *text;
+  gchar *text, *cell_text;
+
+  priv = search_data->cell->priv;
   
-  gtk_tree_model_get (model, iter, search_data->cell->text_column, &text, -1);
-  if (text && GTK_CELL_RENDERER_TEXT (search_data->cell)->text &&
-      strcmp (text, GTK_CELL_RENDERER_TEXT (search_data->cell)->text) == 0)
+  gtk_tree_model_get (model, iter, priv->text_column, &text, -1);
+  g_object_get (GTK_CELL_RENDERER_TEXT (search_data->cell),
+                "text", &cell_text,
+                NULL);
+  if (text && cell_text && g_strcmp0 (text, cell_text) == 0)
     {
       search_data->iter = *iter;
       search_data->found = TRUE;
     }
 
+  g_free (cell_text);
   g_free (text);
   
   return search_data->found;
@@ -404,61 +439,66 @@ find_text (GtkTreeModel *model,
 
 static GtkCellEditable *
 gtk_cell_renderer_combo_start_editing (GtkCellRenderer     *cell,
-                                      GdkEvent            *event,
-                                      GtkWidget           *widget,
-                                      const gchar         *path,
-                                      GdkRectangle        *background_area,
-                                      GdkRectangle        *cell_area,
-                                      GtkCellRendererState flags)
+                                       GdkEvent            *event,
+                                       GtkWidget           *widget,
+                                       const gchar         *path,
+                                       const GdkRectangle  *background_area,
+                                       const GdkRectangle  *cell_area,
+                                       GtkCellRendererState flags)
 {
   GtkCellRendererCombo *cell_combo;
   GtkCellRendererText *cell_text;
   GtkWidget *combo;
   SearchData search_data;
   GtkCellRendererComboPrivate *priv;
+  gboolean editable;
+  gchar *text;
 
   cell_text = GTK_CELL_RENDERER_TEXT (cell);
-  if (cell_text->editable == FALSE)
+  g_object_get (cell_text, "editable", &editable, NULL);
+  if (editable == FALSE)
     return NULL;
 
   cell_combo = GTK_CELL_RENDERER_COMBO (cell);
-  if (cell_combo->text_column < 0)
-    return NULL;
+  priv = cell_combo->priv;
 
-  priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell_combo);
+  if (priv->text_column < 0)
+    return NULL;
 
-  if (cell_combo->has_entry) 
+  if (priv->has_entry)
     {
-      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)
-       gtk_entry_set_text (GTK_ENTRY (GTK_BIN (combo)->child), 
-                           cell_text->text);
+      combo = g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
+
+      if (priv->model)
+        gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
+      gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (combo),
+                                           priv->text_column);
+
+      g_object_get (cell_text, "text", &text, NULL);
+      if (text)
+       gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))),
+                           text);
+      g_free (text);
     }
   else
     {
       cell = gtk_cell_renderer_text_new ();
 
       combo = gtk_combo_box_new ();
-      if (cell_combo->model)
-        gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
+      if (priv->model)
+        gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
 
       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), 
-                                     cell, "text", cell_combo->text_column, 
+                                     cell, "text", priv->text_column,
                                      NULL);
 
       /* determine the current value */
-      if (cell_combo->model)
+      if (priv->model)
         {
           search_data.cell = cell_combo;
           search_data.found = FALSE;
-          gtk_tree_model_foreach (cell_combo->model, find_text, &search_data);
+          gtk_tree_model_foreach (priv->model, find_text, &search_data);
           if (search_data.found)
             gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
                                            &(search_data.iter));
@@ -478,10 +518,9 @@ gtk_cell_renderer_combo_start_editing (GtkCellRenderer     *cell,
   g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
                    G_CALLBACK (gtk_cell_renderer_combo_changed),
                    cell_combo);
-  cell_combo->focus_out_id = 
-    g_signal_connect (combo, "focus-out-event",
-                     G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
-                     cell_combo);
+  priv->focus_out_id = g_signal_connect (combo, "focus-out-event",
+                                         G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
+                                         cell_combo);
 
   priv->combo = combo;