gtkcomboboxentry.c 13.1 KB
Newer Older
1 2 3
/* gtkcomboboxentry.c
 * Copyright (C) 2002, 2003  Kristian Rietveld <kris@gtk.org>
 *
4 5 6 7
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10 11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Library General Public License for more details.
13
 *
14 15
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
16 17 18 19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include <config.h>
Kristian Rietveld's avatar
Kristian Rietveld committed
21 22
#include "gtkcomboboxentry.h"
#include "gtkcelllayout.h"
23

Kristian Rietveld's avatar
Kristian Rietveld committed
24 25
#include "gtkentry.h"
#include "gtkcellrenderertext.h"
26

27
#include "gtkprivate.h"
28
#include "gtkintl.h"
29
#include "gtkalias.h"
30

31 32
#define GTK_COMBO_BOX_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX_ENTRY, GtkComboBoxEntryPrivate))

33 34 35 36
struct _GtkComboBoxEntryPrivate
{
  GtkWidget *entry;

37
  GtkCellRenderer *text_renderer;
38 39 40 41 42 43
  gint text_column;
};

static void gtk_combo_box_entry_class_init       (GtkComboBoxEntryClass *klass);
static void gtk_combo_box_entry_init             (GtkComboBoxEntry      *entry_box);

44 45 46 47 48 49 50 51 52
static void gtk_combo_box_entry_set_property     (GObject               *object,
                                                  guint                  prop_id,
                                                  const GValue          *value,
                                                  GParamSpec            *pspec);
static void gtk_combo_box_entry_get_property     (GObject               *object,
                                                  guint                  prop_id,
                                                  GValue                *value,
                                                  GParamSpec            *pspec);

53
static gchar *gtk_combo_box_entry_get_active_text (GtkComboBox *combo_box);
54 55 56 57
static void gtk_combo_box_entry_active_changed   (GtkComboBox           *combo_box,
                                                  gpointer               user_data);
static void gtk_combo_box_entry_contents_changed (GtkEntry              *entry,
                                                  gpointer               user_data);
58 59
static gboolean gtk_combo_box_entry_mnemonic_activate (GtkWidget        *entry,
						       gboolean          group_cycling);
60
static void gtk_combo_box_entry_grab_focus       (GtkWidget *widget);
61 62 63
static void has_frame_changed                    (GtkComboBoxEntry      *entry_box,
						  GParamSpec            *pspec,
						  gpointer               data);
64 65 66 67 68 69

enum
{
  PROP_0,
  PROP_TEXT_COLUMN
};
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92


GType
gtk_combo_box_entry_get_type (void)
{
  static GType combo_box_entry_type = 0;

  if (!combo_box_entry_type)
    {
      static const GTypeInfo combo_box_entry_info =
        {
          sizeof (GtkComboBoxEntryClass),
          NULL, /* base_init */
          NULL, /* base_finalize */
          (GClassInitFunc) gtk_combo_box_entry_class_init,
          NULL, /* class_finalize */
          NULL, /* class_data */
          sizeof (GtkComboBoxEntry),
          0,
          (GInstanceInitFunc) gtk_combo_box_entry_init
        };

      combo_box_entry_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
93
                                                     I_("GtkComboBoxEntry"),
94 95 96 97 98 99 100 101 102 103
                                                     &combo_box_entry_info,
                                                     0);
    }

  return combo_box_entry_type;
}

static void
gtk_combo_box_entry_class_init (GtkComboBoxEntryClass *klass)
{
104
  GObjectClass *object_class;
105
  GtkWidgetClass *widget_class;
106 107
  GtkComboBoxClass *combo_class;
  
108 109 110 111
  object_class = (GObjectClass *)klass;
  object_class->set_property = gtk_combo_box_entry_set_property;
  object_class->get_property = gtk_combo_box_entry_get_property;

112 113
  widget_class = (GtkWidgetClass *)klass;
  widget_class->mnemonic_activate = gtk_combo_box_entry_mnemonic_activate;
114
  widget_class->grab_focus = gtk_combo_box_entry_grab_focus;
115

116 117 118
  combo_class = (GtkComboBoxClass *)klass;
  combo_class->get_active_text = gtk_combo_box_entry_get_active_text;
  
119 120
  g_object_class_install_property (object_class,
                                   PROP_TEXT_COLUMN,
121
                                   g_param_spec_int ("text-column",
122 123
                                                     P_("Text Column"),
                                                     P_("A column in the data source model to get the strings from"),
124 125 126
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
127
                                                     GTK_PARAM_READWRITE));
128

129 130 131 132 133 134 135 136
  g_type_class_add_private ((GObjectClass *) klass,
                            sizeof (GtkComboBoxEntryPrivate));
}

static void
gtk_combo_box_entry_init (GtkComboBoxEntry *entry_box)
{
  entry_box->priv = GTK_COMBO_BOX_ENTRY_GET_PRIVATE (entry_box);
137 138 139
  entry_box->priv->text_column = -1;

  entry_box->priv->entry = gtk_entry_new ();
140 141 142
  /* this flag is a hack to tell the entry to fill its allocation.
   */
  GTK_ENTRY (entry_box->priv->entry)->is_cell_renderer = TRUE;
143 144 145 146 147 148 149
  gtk_container_add (GTK_CONTAINER (entry_box), entry_box->priv->entry);
  gtk_widget_show (entry_box->priv->entry);

  entry_box->priv->text_renderer = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (entry_box),
                              entry_box->priv->text_renderer, TRUE);

150
  gtk_combo_box_set_active (GTK_COMBO_BOX (entry_box), -1);
151

152 153 154 155 156
  g_signal_connect (entry_box->priv->entry, "changed",
                    G_CALLBACK (gtk_combo_box_entry_contents_changed),
                    entry_box);
  g_signal_connect (entry_box, "changed",
                    G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
157 158
  has_frame_changed (entry_box, NULL, NULL);
  g_signal_connect (entry_box, "notify::has-frame", G_CALLBACK (has_frame_changed), NULL);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

static void
gtk_combo_box_entry_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);

  switch (prop_id)
    {
      case PROP_TEXT_COLUMN:
        gtk_combo_box_entry_set_text_column (entry_box,
                                             g_value_get_int (value));
        break;

      default:
        break;
    }
}

static void
gtk_combo_box_entry_get_property (GObject    *object,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);

  switch (prop_id)
    {
      case PROP_TEXT_COLUMN:
        g_value_set_int (value, entry_box->priv->text_column);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
199 200 201 202 203 204 205
}

static void
gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
                                    gpointer     user_data)
{
  GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (combo_box);
206 207 208
  GtkTreeModel *model;
  GtkTreeIter iter;
  gchar *str = NULL;
209

210
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
211
    {
212 213 214
      g_signal_handlers_block_by_func (entry_box->priv->entry,
				       gtk_combo_box_entry_contents_changed,
				       combo_box);
215 216 217

      model = gtk_combo_box_get_model (combo_box);

218 219 220
      gtk_tree_model_get (model, &iter, 
			  entry_box->priv->text_column, &str, 
			  -1);
221 222 223
      gtk_entry_set_text (GTK_ENTRY (entry_box->priv->entry), str);
      g_free (str);

224 225 226 227
      g_signal_handlers_unblock_by_func (entry_box->priv->entry,
					 gtk_combo_box_entry_contents_changed,
					 combo_box);
    }
228 229
}

230 231 232 233 234 235 236
static void 
has_frame_changed (GtkComboBoxEntry *entry_box,
		   GParamSpec       *pspec,
		   gpointer          data)
{
  gboolean has_frame;
  
237
  g_object_get (entry_box, "has-frame", &has_frame, NULL);
238 239 240 241

  gtk_entry_set_has_frame (GTK_ENTRY (entry_box->priv->entry), has_frame);
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static void
gtk_combo_box_entry_contents_changed (GtkEntry *entry,
                                      gpointer  user_data)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);

  g_signal_handlers_block_by_func (combo_box,
                                   gtk_combo_box_entry_active_changed,
                                   NULL);
  gtk_combo_box_set_active (combo_box, -1);
  g_signal_handlers_unblock_by_func (combo_box,
                                     gtk_combo_box_entry_active_changed,
                                     NULL);
}

/* public API */
258 259 260

/**
 * gtk_combo_box_entry_new:
261 262 263 264 265 266 267 268 269 270 271 272
 *
 * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child. After
 * construction, you should set a model using gtk_combo_box_set_model() and a
 * text_column * using gtk_combo_box_entry_set_text_column().
 *
 * Return value: A new #GtkComboBoxEntry.
 *
 * Since: 2.4
 */
GtkWidget *
gtk_combo_box_entry_new (void)
{
273
  return g_object_new (gtk_combo_box_entry_get_type (), NULL);
274 275 276 277
}

/**
 * gtk_combo_box_entry_new_with_model:
278 279 280 281 282 283
 * @model: A #GtkTreeModel.
 * @text_column: A column in @model to get the strings from.
 *
 * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child and a list
 * of strings as popup. You can get the #GtkEntry from a #GtkComboBoxEntry
 * using GTK_ENTRY (GTK_BIN (combo_box_entry)->child). To add and remove
284
 * strings from the list, just modify @model using its data manipulation
285 286 287 288 289 290
 * API.
 *
 * Return value: A new #GtkComboBoxEntry.
 *
 * Since: 2.4
 */
291
GtkWidget *
292 293
gtk_combo_box_entry_new_with_model (GtkTreeModel *model,
                                    gint          text_column)
294 295 296 297 298 299 300 301 302
{
  GtkWidget *ret;

  g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
  g_return_val_if_fail (text_column >= 0, NULL);
  g_return_val_if_fail (text_column < gtk_tree_model_get_n_columns (model), NULL);

  ret = g_object_new (gtk_combo_box_entry_get_type (),
                      "model", model,
303
                      "text-column", text_column,
304 305 306 307 308
                      NULL);

  return ret;
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/**
 * gtk_combo_box_entry_set_text_column:
 * @entry_box: A #GtkComboBoxEntry.
 * @text_column: A column in @model to get the strings from.
 *
 * Sets the model column which @entry_box should use to get strings from
 * to be @text_column.
 *
 * Since: 2.4.
 */
void
gtk_combo_box_entry_set_text_column (GtkComboBoxEntry *entry_box,
                                     gint              text_column)
{
  g_return_if_fail (text_column >= 0);
  g_return_if_fail (text_column < gtk_tree_model_get_n_columns (gtk_combo_box_get_model (GTK_COMBO_BOX (entry_box))));
  g_return_if_fail (entry_box->priv->text_column == -1);

  entry_box->priv->text_column = text_column;

  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (entry_box),
                                  entry_box->priv->text_renderer,
                                  "text", text_column,
                                  NULL);
}

335 336 337 338 339 340 341 342 343 344
/**
 * gtk_combo_box_entry_get_text_column:
 * @entry_box: A #GtkComboBoxEntry.
 *
 * Returns the column which @entry_box is using to get the strings from.
 *
 * Return value: A column in the data source model of @entry_box.
 *
 * Since: 2.4
 */
345 346 347 348 349 350 351
gint
gtk_combo_box_entry_get_text_column (GtkComboBoxEntry *entry_box)
{
  g_return_val_if_fail (GTK_IS_COMBO_BOX_ENTRY (entry_box), 0);

  return entry_box->priv->text_column;
}
352 353 354 355 356 357 358 359 360 361 362

static gboolean
gtk_combo_box_entry_mnemonic_activate (GtkWidget *widget,
				       gboolean   group_cycling)
{
  GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (widget);

  gtk_widget_grab_focus (entry_box->priv->entry);

  return TRUE;
}
363

364 365 366 367 368 369 370 371 372
static void
gtk_combo_box_entry_grab_focus (GtkWidget *widget)
{
  GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (widget);

  gtk_widget_grab_focus (entry_box->priv->entry);
}


373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

/* convenience API for simple text combos */

/**
 * gtk_combo_box_entry_new_text:
 *
 * Convenience function which constructs a new editable text combo box, which 
 * is a #GtkComboBoxEntry just displaying strings. If you use this function to
 * create a text combo box, you should only manipulate its data source with
 * the following convenience functions: gtk_combo_box_append_text(),
 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
 * gtk_combo_box_remove_text().
 *
 * Return value: A new text #GtkComboBoxEntry.
 *
 * Since: 2.4
 */
GtkWidget *
gtk_combo_box_entry_new_text (void)
{
  GtkWidget *entry_box;
  GtkListStore *store;

  store = gtk_list_store_new (1, G_TYPE_STRING);
  entry_box = gtk_combo_box_entry_new_with_model (GTK_TREE_MODEL (store), 0);
398
  g_object_unref (store);
399 400 401

  return entry_box;
}
402

403 404 405 406 407 408
static gchar *
gtk_combo_box_entry_get_active_text (GtkComboBox *combo_box)
{
  GtkComboBoxEntry *combo = GTK_COMBO_BOX_ENTRY (combo_box);

  if (combo->priv->entry)
409
    return g_strdup (gtk_entry_get_text (GTK_ENTRY (combo->priv->entry)));
410 411 412 413

  return NULL;
}

414 415
#define __GTK_COMBO_BOX_ENTRY_C__
#include "gtkaliasdef.c"