gtkentrycompletion.c 57.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* gtkentrycompletion.c
 * Copyright (C) 2003  Kristian Rietveld  <kris@gtk.org>
 *
 * 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.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

20
#include <config.h>
Kristian Rietveld's avatar
Kristian Rietveld committed
21 22 23 24 25 26
#include "gtkentrycompletion.h"
#include "gtkentryprivate.h"
#include "gtkcelllayout.h"

#include "gtkintl.h"
#include "gtkcellrenderertext.h"
Matthias Clasen's avatar
Matthias Clasen committed
27
#include "gtkframe.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
28 29 30 31 32 33 34 35 36
#include "gtktreeselection.h"
#include "gtktreeview.h"
#include "gtkscrolledwindow.h"
#include "gtkvbox.h"
#include "gtkwindow.h"
#include "gtkentry.h"
#include "gtkmain.h"
#include "gtksignal.h"
#include "gtkmarshalers.h"
37

38 39 40
#include "gtkprivate.h"
#include "gtkalias.h"

41 42 43 44 45 46
#include <string.h>


/* signals */
enum
{
47
  INSERT_PREFIX,
48 49 50 51 52 53 54 55 56 57
  MATCH_SELECTED,
  ACTION_ACTIVATED,
  LAST_SIGNAL
};

/* properties */
enum
{
  PROP_0,
  PROP_MODEL,
58
  PROP_MINIMUM_KEY_LENGTH,
59 60
  PROP_TEXT_COLUMN,
  PROP_INLINE_COMPLETION,
61
  PROP_POPUP_COMPLETION,
62 63
  PROP_POPUP_SET_WIDTH,
  PROP_POPUP_SINGLE_MATCH
64 65
};

66
#define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

static void     gtk_entry_completion_class_init          (GtkEntryCompletionClass *klass);
static void     gtk_entry_completion_cell_layout_init    (GtkCellLayoutIface      *iface);
static void     gtk_entry_completion_init                (GtkEntryCompletion      *completion);
static void     gtk_entry_completion_set_property        (GObject                 *object,
                                                          guint                    prop_id,
                                                          const GValue            *value,
                                                          GParamSpec              *pspec);
static void     gtk_entry_completion_get_property        (GObject                 *object,
                                                          guint                    prop_id,
                                                          GValue                  *value,
                                                          GParamSpec              *pspec);
static void     gtk_entry_completion_finalize            (GObject                 *object);

static void     gtk_entry_completion_pack_start          (GtkCellLayout           *cell_layout,
                                                          GtkCellRenderer         *cell,
                                                          gboolean                 expand);
static void     gtk_entry_completion_pack_end            (GtkCellLayout           *cell_layout,
                                                          GtkCellRenderer         *cell,
                                                          gboolean                 expand);
static void     gtk_entry_completion_clear               (GtkCellLayout           *cell_layout);
static void     gtk_entry_completion_add_attribute       (GtkCellLayout           *cell_layout,
                                                          GtkCellRenderer         *cell,
                                                          const char              *attribute,
                                                          gint                     column);
static void     gtk_entry_completion_set_cell_data_func  (GtkCellLayout           *cell_layout,
                                                          GtkCellRenderer         *cell,
                                                          GtkCellLayoutDataFunc    func,
                                                          gpointer                 func_data,
                                                          GDestroyNotify           destroy);
static void     gtk_entry_completion_clear_attributes    (GtkCellLayout           *cell_layout,
                                                          GtkCellRenderer         *cell);
Kristian Rietveld's avatar
Kristian Rietveld committed
99 100 101
static void     gtk_entry_completion_reorder             (GtkCellLayout           *cell_layout,
                                                          GtkCellRenderer         *cell,
                                                          gint                     position);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

static gboolean gtk_entry_completion_visible_func        (GtkTreeModel            *model,
                                                          GtkTreeIter             *iter,
                                                          gpointer                 data);
static gboolean gtk_entry_completion_popup_key_press     (GtkWidget               *widget,
                                                          GdkEventKey             *event,
                                                          gpointer                 user_data);
static gboolean gtk_entry_completion_popup_button_press  (GtkWidget               *widget,
                                                          GdkEventButton          *event,
                                                          gpointer                 user_data);
static gboolean gtk_entry_completion_list_button_press   (GtkWidget               *widget,
                                                          GdkEventButton          *event,
                                                          gpointer                 user_data);
static gboolean gtk_entry_completion_action_button_press (GtkWidget               *widget,
                                                          GdkEventButton          *event,
                                                          gpointer                 user_data);
118 119
static void     gtk_entry_completion_selection_changed   (GtkTreeSelection        *selection,
                                                          gpointer                 data);
120 121 122 123 124 125
static gboolean	gtk_entry_completion_list_enter_notify	 (GtkWidget               *widget,
							  GdkEventCrossing        *event,
							  gpointer                 data);
static gboolean gtk_entry_completion_list_motion_notify	 (GtkWidget		  *widget,
							  GdkEventMotion          *event,
							  gpointer 		   data);
126 127
static void     gtk_entry_completion_insert_action       (GtkEntryCompletion      *completion,
                                                          gint                     index,
128
                                                          const gchar             *string,
129 130 131 132 133 134 135
                                                          gboolean                 markup);
static void     gtk_entry_completion_action_data_func    (GtkTreeViewColumn       *tree_column,
                                                          GtkCellRenderer         *cell,
                                                          GtkTreeModel            *model,
                                                          GtkTreeIter             *iter,
                                                          gpointer                 data);

136 137 138
static gboolean gtk_entry_completion_match_selected      (GtkEntryCompletion *completion,
							  GtkTreeModel       *model,
							  GtkTreeIter        *iter);
139
static gboolean gtk_entry_completion_real_insert_prefix  (GtkEntryCompletion *completion,
140
							  const gchar        *prefix);
141

142
static GObjectClass *parent_class = NULL;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static guint entry_completion_signals[LAST_SIGNAL] = { 0 };


GType
gtk_entry_completion_get_type (void)
{
  static GType entry_completion_type = 0;

  if (!entry_completion_type)
    {
      static const GTypeInfo entry_completion_info =
      {
        sizeof (GtkEntryCompletionClass),
        NULL,
        NULL,
        (GClassInitFunc) gtk_entry_completion_class_init,
        NULL,
        NULL,
        sizeof (GtkEntryCompletion),
        0,
        (GInstanceInitFunc) gtk_entry_completion_init
      };

      static const GInterfaceInfo cell_layout_info =
      {
        (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
        NULL,
        NULL
      };

      entry_completion_type =
Matthias Clasen's avatar
Matthias Clasen committed
174
        g_type_register_static (G_TYPE_OBJECT, I_("GtkEntryCompletion"),
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                                &entry_completion_info, 0);

      g_type_add_interface_static (entry_completion_type,
                                   GTK_TYPE_CELL_LAYOUT,
                                   &cell_layout_info);
    }

  return entry_completion_type;
}

static void
gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
{
  GObjectClass *object_class;

190
  parent_class = g_type_class_peek_parent (klass);
191 192 193 194 195 196
  object_class = (GObjectClass *)klass;

  object_class->set_property = gtk_entry_completion_set_property;
  object_class->get_property = gtk_entry_completion_get_property;
  object_class->finalize = gtk_entry_completion_finalize;

197
  klass->match_selected = gtk_entry_completion_match_selected;
198 199 200 201 202 203 204
  klass->insert_prefix = gtk_entry_completion_real_insert_prefix;

  /**
   * GtkEntryCompletion::insert-prefix:
   * @widget: the object which received the signal
   * @prefix: the common prefix of all possible completions
   * 
Matthias Clasen's avatar
Matthias Clasen committed
205 206
   * Gets emitted when the inline autocompletion is triggered. 
   * The default behaviour is to make the entry display the 
207 208 209 210 211 212 213 214 215 216 217 218
   * whole prefix and select the newly inserted part.
   *
   * Applications may connect to this signal in order to insert only a
   * smaller part of the @prefix into the entry - e.g. the entry used in
   * the #GtkFileChooser inserts only the part of the prefix up to the 
   * next '/'.
   *
   * Return value: %TRUE if the signal has been handled
   * 
   * Since: 2.6
   */ 
  entry_completion_signals[INSERT_PREFIX] =
Matthias Clasen's avatar
Matthias Clasen committed
219
    g_signal_new (I_("insert_prefix"),
220 221 222 223 224 225 226
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
                  _gtk_boolean_handled_accumulator, NULL,
                  _gtk_marshal_BOOLEAN__STRING,
                  G_TYPE_BOOLEAN, 1,
                  G_TYPE_STRING);
227

Matthias Clasen's avatar
Matthias Clasen committed
228 229 230 231 232 233
  /**
   * GtkEntryCompletion::match-selected:
   * @widget: the object which received the signal
   * @model: the #GtkTreeModel containing the matches
   * @iter: a #GtkTreeIter positioned at the selected match
   * 
Matthias Clasen's avatar
Matthias Clasen committed
234 235 236 237
   * Gets emitted when a match from the list is selected. 
   * The default behaviour is to replace the contents of the 
   * entry with the contents of the text column in the row 
   * pointed to by @iter.
Matthias Clasen's avatar
Matthias Clasen committed
238 239
   *
   * Return value: %TRUE if the signal has been handled
240 241
   * 
   * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
242
   */ 
243
  entry_completion_signals[MATCH_SELECTED] =
Matthias Clasen's avatar
Matthias Clasen committed
244
    g_signal_new (I_("match_selected"),
245 246 247 248 249 250 251 252
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
                  _gtk_boolean_handled_accumulator, NULL,
                  _gtk_marshal_BOOLEAN__OBJECT_BOXED,
                  G_TYPE_BOOLEAN, 2,
                  GTK_TYPE_TREE_MODEL,
                  GTK_TYPE_TREE_ITER);
Matthias Clasen's avatar
Matthias Clasen committed
253 254 255 256 257 258
		  
  /**
   * GtkEntryCompletion::action-activated:
   * @widget: the object which received the signal
   * @index: the index of the activated action
   *
Matthias Clasen's avatar
Matthias Clasen committed
259
   * Gets emitted when an action is activated.
260 261
   * 
   * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
262
   */
263
  entry_completion_signals[ACTION_ACTIVATED] =
Matthias Clasen's avatar
Matthias Clasen committed
264
    g_signal_new (I_("action_activated"),
265 266 267 268 269 270 271 272 273 274 275
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
                  NULL, NULL,
                  _gtk_marshal_NONE__INT,
                  G_TYPE_NONE, 1,
                  G_TYPE_INT);

  g_object_class_install_property (object_class,
                                   PROP_MODEL,
                                   g_param_spec_object ("model",
276 277
                                                        P_("Completion Model"),
                                                        P_("The model to find matches in"),
278
                                                        GTK_TYPE_TREE_MODEL,
279
                                                        GTK_PARAM_READWRITE));
280 281
  g_object_class_install_property (object_class,
                                   PROP_MINIMUM_KEY_LENGTH,
282
                                   g_param_spec_int ("minimum-key-length",
283 284
                                                     P_("Minimum Key Length"),
                                                     P_("Minimum length of the search key in order to look up matches"),
285
                                                     0,
286 287
                                                     G_MAXINT,
                                                     1,
288
                                                     GTK_PARAM_READWRITE));
289
  /**
290
   * GtkEntryCompletion:text-column:
291 292 293 294 295 296 297
   *
   * The column of the model containing the strings.
   *
   * Since: 2.6
   */
  g_object_class_install_property (object_class,
                                   PROP_TEXT_COLUMN,
298
                                   g_param_spec_int ("text-column",
299 300 301 302 303
                                                     P_("Text column"),
                                                     P_("The column of the model containing the strings."),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
304
                                                     GTK_PARAM_READWRITE));
305

306
  /**
Matthias Clasen's avatar
Matthias Clasen committed
307
   * GtkEntryCompletion:inline-completion:
308
   * 
Matthias Clasen's avatar
Matthias Clasen committed
309
   * Determines whether the common prefix of the possible completions 
310 311 312
   * should be inserted automatically in the entry. Note that this
   * requires text-column to be set, even if you are using a custom
   * match function.
313 314 315 316 317
   *
   * Since: 2.6
   **/
  g_object_class_install_property (object_class,
				   PROP_INLINE_COMPLETION,
318
				   g_param_spec_boolean ("inline-completion",
319 320 321
 							 P_("Inline completion"),
 							 P_("Whether the common prefix should be inserted automatically"),
 							 FALSE,
322
 							 GTK_PARAM_READWRITE));
323
  /**
Matthias Clasen's avatar
Matthias Clasen committed
324
   * GtkEntryCompletion:popup-completion:
325
   * 
Matthias Clasen's avatar
Matthias Clasen committed
326 327
   * Determines whether the possible completions should be 
   * shown in a popup window. 
328 329 330 331 332
   *
   * Since: 2.6
   **/
  g_object_class_install_property (object_class,
				   PROP_POPUP_COMPLETION,
333
				   g_param_spec_boolean ("popup-completion",
334 335 336
 							 P_("Popup completion"),
 							 P_("Whether the completions should be shown in a popup window"),
 							 TRUE,
337
 							 GTK_PARAM_READWRITE));
338

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  /**
   * GtkEntryCompletion:popup-set-width:
   * 
   * Determines whether the completions popup window will be
   * resized to the width of the entry.
   *
   * Since: 2.8
   */
  g_object_class_install_property (object_class,
				   PROP_POPUP_SET_WIDTH,
				   g_param_spec_boolean ("popup-set-width",
 							 P_("Popup set width"),
 							 P_("If TRUE, the popup window will have the same size as the entry"),
 							 TRUE,
 							 GTK_PARAM_READWRITE));

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  /**
   * GtkEntryCompletion:popup-single-match:
   * 
   * Determines whether the completions popup window will shown
   * for a single possible completion. You probably want to set
   * this to %FALSE if you are using 
   * <link linkend="GtkEntryCompletion--inline-completion">inline 
   * completion</link>.
   *
   * Since: 2.8
   */
  g_object_class_install_property (object_class,
				   PROP_POPUP_SINGLE_MATCH,
				   g_param_spec_boolean ("popup-single-match",
 							 P_("Popup single match"),
 							 P_("If TRUE, the popup window will appear for a single match."),
 							 TRUE,
 							 GTK_PARAM_READWRITE));

374 375 376 377 378 379 380 381 382 383 384 385
  g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
}

static void
gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
{
  iface->pack_start = gtk_entry_completion_pack_start;
  iface->pack_end = gtk_entry_completion_pack_end;
  iface->clear = gtk_entry_completion_clear;
  iface->add_attribute = gtk_entry_completion_add_attribute;
  iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
  iface->clear_attributes = gtk_entry_completion_clear_attributes;
Kristian Rietveld's avatar
Kristian Rietveld committed
386
  iface->reorder = gtk_entry_completion_reorder;
387 388 389 390 391 392 393 394
}

static void
gtk_entry_completion_init (GtkEntryCompletion *completion)
{
  GtkCellRenderer *cell;
  GtkTreeSelection *sel;
  GtkEntryCompletionPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
395
  GtkWidget *popup_frame;
396 397 398 399 400 401

  /* yes, also priv, need to keep the code readable */
  priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);

  priv->minimum_key_length = 1;
  priv->text_column = -1;
402 403 404
  priv->has_completion = FALSE;
  priv->inline_completion = FALSE;
  priv->popup_completion = TRUE;
405
  priv->popup_set_width = TRUE;
406
  priv->popup_single_match = TRUE;
407 408 409 410 411 412 413 414

  /* completions */
  priv->filter_model = NULL;

  priv->tree_view = gtk_tree_view_new ();
  g_signal_connect (priv->tree_view, "button_press_event",
                    G_CALLBACK (gtk_entry_completion_list_button_press),
                    completion);
415 416 417 418 419 420
  g_signal_connect (priv->tree_view, "enter_notify_event",
		    G_CALLBACK (gtk_entry_completion_list_enter_notify),
		    completion);
  g_signal_connect (priv->tree_view, "motion_notify_event",
		    G_CALLBACK (gtk_entry_completion_list_motion_notify),
		    completion);
421
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
422
  gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
423 424 425 426

  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
  gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
  gtk_tree_selection_unselect_all (sel);
427 428 429 430
  g_signal_connect (sel, "changed",
                    G_CALLBACK (gtk_entry_completion_selection_changed),
                    completion);
  priv->first_sel_changed = TRUE;
431 432 433 434 435 436 437 438 439

  priv->column = gtk_tree_view_column_new ();
  gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);

  priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
                                  GTK_POLICY_NEVER,
                                  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
Matthias Clasen's avatar
Matthias Clasen committed
440
                                       GTK_SHADOW_NONE);
441

442 443
  /* a nasty hack to get the completions treeview to size nicely */
  gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
444 445 446 447 448 449 450 451 452

  /* actions */
  priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);

  priv->action_view =
    gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
  g_signal_connect (priv->action_view, "button_press_event",
                    G_CALLBACK (gtk_entry_completion_action_button_press),
                    completion);
453 454 455 456 457 458
  g_signal_connect (priv->action_view, "enter_notify_event",
		    G_CALLBACK (gtk_entry_completion_list_enter_notify),
		    completion);
  g_signal_connect (priv->action_view, "motion_notify_event",
		    G_CALLBACK (gtk_entry_completion_list_motion_notify),
		    completion);
459
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
460
  gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
  gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
  gtk_tree_selection_unselect_all (sel);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
                                              0, "",
                                              cell,
                                              gtk_entry_completion_action_data_func,
                                              NULL,
                                              NULL);

  /* pack it all */
  priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
  gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
  g_signal_connect (priv->popup_window, "key_press_event",
                    G_CALLBACK (gtk_entry_completion_popup_key_press),
                    completion);
  g_signal_connect (priv->popup_window, "button_press_event",
                    G_CALLBACK (gtk_entry_completion_popup_button_press),
                    completion);

Matthias Clasen's avatar
Matthias Clasen committed
484 485 486 487 488 489
  popup_frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
			     GTK_SHADOW_ETCHED_IN);
  gtk_widget_show (popup_frame);
  gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
  
490
  priv->vbox = gtk_vbox_new (FALSE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
491
  gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

  gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
  gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
                      TRUE, TRUE, 0);

  /* we don't want to see the action treeview when no actions have
   * been inserted, so we pack the action treeview after the first
   * action has been added
   */
}

static void
gtk_entry_completion_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
510
  GtkEntryCompletionPrivate *priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
511 512 513 514 515 516 517 518 519 520 521 522 523

  switch (prop_id)
    {
      case PROP_MODEL:
        gtk_entry_completion_set_model (completion,
                                        g_value_get_object (value));
        break;

      case PROP_MINIMUM_KEY_LENGTH:
        gtk_entry_completion_set_minimum_key_length (completion,
                                                     g_value_get_int (value));
        break;

524 525 526 527
      case PROP_TEXT_COLUMN:
	priv->text_column = g_value_get_int (value);
        break;

528 529 530 531 532 533 534 535
      case PROP_INLINE_COMPLETION:
	priv->inline_completion = g_value_get_boolean (value);
        break;

      case PROP_POPUP_COMPLETION:
	priv->popup_completion = g_value_get_boolean (value);
        break;

536 537 538 539
      case PROP_POPUP_SET_WIDTH:
	priv->popup_set_width = g_value_get_boolean (value);
        break;

540 541 542 543
      case PROP_POPUP_SINGLE_MATCH:
	priv->popup_single_match = g_value_get_boolean (value);
        break;

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_entry_completion_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);

  switch (prop_id)
    {
      case PROP_MODEL:
        g_value_set_object (value,
                            gtk_entry_completion_get_model (completion));
        break;

      case PROP_MINIMUM_KEY_LENGTH:
        g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
        break;

569 570 571 572
      case PROP_TEXT_COLUMN:
        g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
        break;

573 574 575 576 577 578 579 580
      case PROP_INLINE_COMPLETION:
        g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
        break;

      case PROP_POPUP_COMPLETION:
        g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
        break;

581 582 583 584
      case PROP_POPUP_SET_WIDTH:
        g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
        break;

585 586 587 588
      case PROP_POPUP_SINGLE_MATCH:
        g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
        break;

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_entry_completion_finalize (GObject *object)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);

  if (completion->priv->tree_view)
    gtk_widget_destroy (completion->priv->tree_view);

  if (completion->priv->entry)
    gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);

  if (completion->priv->actions)
    g_object_unref (completion->priv->actions);

  if (completion->priv->case_normalized_key)
    g_free (completion->priv->case_normalized_key);

  if (completion->priv->popup_window)
    gtk_widget_destroy (completion->priv->popup_window);
614 615

  G_OBJECT_CLASS (parent_class)->finalize (object);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
}

/* implement cell layout interface */
static void
gtk_entry_completion_pack_start (GtkCellLayout   *cell_layout,
                                 GtkCellRenderer *cell,
                                 gboolean         expand)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_tree_view_column_pack_start (priv->column, cell, expand);
}

static void
gtk_entry_completion_pack_end (GtkCellLayout   *cell_layout,
                               GtkCellRenderer *cell,
                               gboolean         expand)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_tree_view_column_pack_end (priv->column, cell, expand);
}

static void
gtk_entry_completion_clear (GtkCellLayout *cell_layout)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_tree_view_column_clear (priv->column);
}

static void
gtk_entry_completion_add_attribute (GtkCellLayout   *cell_layout,
                                    GtkCellRenderer *cell,
                                    const gchar     *attribute,
                                    gint             column)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
}

static void
gtk_entry_completion_set_cell_data_func (GtkCellLayout          *cell_layout,
                                         GtkCellRenderer        *cell,
                                         GtkCellLayoutDataFunc   func,
                                         gpointer                func_data,
                                         GDestroyNotify          destroy)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
                                      cell, func, func_data, destroy);
}

static void
gtk_entry_completion_clear_attributes (GtkCellLayout   *cell_layout,
                                       GtkCellRenderer *cell)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_tree_view_column_clear_attributes (priv->column, cell);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717
static void
gtk_entry_completion_reorder (GtkCellLayout   *cell_layout,
                              GtkCellRenderer *cell,
                              gint             position)
{
  GtkEntryCompletionPrivate *priv;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));

  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);

  gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
}

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
/* all those callbacks */
static gboolean
gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
                                              const gchar        *key,
                                              GtkTreeIter        *iter,
                                              gpointer            user_data)
{
  gchar *item = NULL;
  gchar *normalized_string;
  gchar *case_normalized_string;

  gboolean ret = FALSE;

  GtkTreeModel *model;

  model = gtk_tree_model_filter_get_model (completion->priv->filter_model);

735 736 737
  g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING, 
			FALSE);

738 739 740 741
  gtk_tree_model_get (model, iter,
                      completion->priv->text_column, &item,
                      -1);

742 743 744 745 746 747 748 749 750 751 752 753
  if (item != NULL)
    {
      normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
      case_normalized_string = g_utf8_casefold (normalized_string, -1);
      
      if (!strncmp (key, case_normalized_string, strlen (key)))
	ret = TRUE;
      
      g_free (item);
      g_free (normalized_string);
      g_free (case_normalized_string);
    }
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

  return ret;
}

static gboolean
gtk_entry_completion_visible_func (GtkTreeModel *model,
                                   GtkTreeIter  *iter,
                                   gpointer      data)
{
  gboolean ret = FALSE;

  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);

  if (!completion->priv->case_normalized_key)
    return ret;

770
  if (completion->priv->match_func)
771 772 773 774
    ret = (* completion->priv->match_func) (completion,
                                            completion->priv->case_normalized_key,
                                            iter,
                                            completion->priv->match_data);
775 776 777 778 779
  else if (completion->priv->text_column >= 0)
    ret = gtk_entry_completion_default_completion_func (completion,
                                                        completion->priv->case_normalized_key,
                                                        iter,
                                                        NULL);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

  return ret;
}

static gboolean
gtk_entry_completion_popup_key_press (GtkWidget   *widget,
                                      GdkEventKey *event,
                                      gpointer     user_data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);

  if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
    return FALSE;

  /* propagate event to the entry */
  gtk_widget_event (completion->priv->entry, (GdkEvent *)event);

  return TRUE;
}

static gboolean
gtk_entry_completion_popup_button_press (GtkWidget      *widget,
                                         GdkEventButton *event,
                                         gpointer        user_data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);

  if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
    return FALSE;

  /* if we come here, it's usually time to popdown */
  _gtk_entry_completion_popdown (completion);

  return TRUE;
}

static gboolean
gtk_entry_completion_list_button_press (GtkWidget      *widget,
                                        GdkEventButton *event,
                                        gpointer        user_data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
  GtkTreePath *path = NULL;

  if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
    return FALSE;

  if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
                                     event->x, event->y,
                                     &path, NULL, NULL, NULL))
    {
      GtkTreeIter iter;
832
      gboolean entry_set;
833 834 835 836 837

      gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
                               &iter, path);
      gtk_tree_path_free (path);

838 839
      g_signal_handler_block (completion->priv->entry,
			      completion->priv->changed_id);
840 841 842
      g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
                     0, GTK_TREE_MODEL (completion->priv->filter_model),
                     &iter, &entry_set);
843 844
      g_signal_handler_unblock (completion->priv->entry,
				completion->priv->changed_id);
845 846

      _gtk_entry_completion_popdown (completion);
847

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
      return TRUE;
    }

  return FALSE;
}

static gboolean
gtk_entry_completion_action_button_press (GtkWidget      *widget,
                                          GdkEventButton *event,
                                          gpointer        user_data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
  GtkTreePath *path = NULL;

  if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
    return FALSE;

  if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
                                     event->x, event->y,
                                     &path, NULL, NULL, NULL))
    {
      g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
                     0, gtk_tree_path_get_indices (path)[0]);
      gtk_tree_path_free (path);

      _gtk_entry_completion_popdown (completion);
      return TRUE;
    }

  return FALSE;
}

static void
gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
                                       GtkCellRenderer   *cell,
                                       GtkTreeModel      *model,
                                       GtkTreeIter       *iter,
                                       gpointer           data)
{
  gchar *string = NULL;
  gboolean markup;

  gtk_tree_model_get (model, iter,
                      0, &string,
                      1, &markup,
                      -1);

  if (!string)
    return;

  if (markup)
899
    g_object_set (cell,
900 901 902 903
                  "text", NULL,
                  "markup", string,
                  NULL);
  else
904
    g_object_set (cell,
905 906 907
                  "markup", NULL,
                  "text", string,
                  NULL);
908 909

  g_free (string);
910 911
}

912 913 914 915 916 917 918 919 920
static void
gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
                                        gpointer          data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);

  if (completion->priv->first_sel_changed)
    {
      completion->priv->first_sel_changed = FALSE;
921 922
      if (gtk_widget_is_focus (completion->priv->tree_view))
        gtk_tree_selection_unselect_all (selection);
923 924 925
    }
}

926
/* public API */
Kristian Rietveld's avatar
Kristian Rietveld committed
927 928 929 930 931 932 933 934 935 936

/**
 * gtk_entry_completion_new:
 *
 * Creates a new #GtkEntryCompletion object.
 *
 * Return value: A newly created #GtkEntryCompletion object.
 *
 * Since: 2.4
 */
937 938 939 940 941 942 943 944 945 946
GtkEntryCompletion *
gtk_entry_completion_new (void)
{
  GtkEntryCompletion *completion;

  completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);

  return completion;
}

Kristian Rietveld's avatar
Kristian Rietveld committed
947 948 949 950 951 952 953 954 955 956
/**
 * gtk_entry_completion_get_entry:
 * @completion: A #GtkEntryCompletion.
 *
 * Gets the entry @completion has been attached to.
 *
 * Return value: The entry @completion has been attached to.
 *
 * Since: 2.4
 */
957 958 959 960 961 962 963 964
GtkWidget *
gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
{
  g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);

  return completion->priv->entry;
}

Kristian Rietveld's avatar
Kristian Rietveld committed
965 966 967 968 969 970 971
/**
 * gtk_entry_completion_set_model:
 * @completion: A #GtkEntryCompletion.
 * @model: The #GtkTreeModel.
 *
 * Sets the model for a #GtkEntryCompletion. If @completion already has
 * a model set, it will remove it before setting the new model.
972
 * If model is %NULL, then it will unset the model.
Kristian Rietveld's avatar
Kristian Rietveld committed
973 974 975
 *
 * Since: 2.4
 */
976 977 978 979 980
void
gtk_entry_completion_set_model (GtkEntryCompletion *completion,
                                GtkTreeModel       *model)
{
  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
981
  g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
982

983 984 985 986 987 988 989 990 991
  if (!model)
    {
      gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
			       NULL);
      _gtk_entry_completion_popdown (completion);
      completion->priv->filter_model = NULL;
      return;
    }
     
992 993 994 995 996 997 998
  /* code will unref the old filter model (if any) */
  completion->priv->filter_model =
    GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
  gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
                                          gtk_entry_completion_visible_func,
                                          completion,
                                          NULL);
999

1000 1001
  gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
                           GTK_TREE_MODEL (completion->priv->filter_model));
1002
  g_object_unref (completion->priv->filter_model);
1003

1004 1005
  g_object_notify (G_OBJECT (completion), "model");

1006 1007
  if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
    _gtk_entry_completion_resize_popup (completion);
1008 1009
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
/**
 * gtk_entry_completion_get_model:
 * @completion: A #GtkEntryCompletion.
 *
 * Returns the model the #GtkEntryCompletion is using as data source.
 * Returns %NULL if the model is unset.
 *
 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
 *
 * Since: 2.4
 */
1021 1022 1023 1024 1025
GtkTreeModel *
gtk_entry_completion_get_model (GtkEntryCompletion *completion)
{
  g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);

1026 1027 1028
  if (!completion->priv->filter_model)
    return NULL;
  
1029 1030 1031
  return gtk_tree_model_filter_get_model (completion->priv->filter_model);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
/**
 * gtk_entry_completion_set_match_func:
 * @completion: A #GtkEntryCompletion.
 * @func: The #GtkEntryCompletionMatchFunc to use.
 * @func_data: The user data for @func.
 * @func_notify: Destroy notifier for @func_data.
 *
 * Sets the match function for @completion to be @func. The match function
 * is used to determine if a row should or should not be in the completion
 * list.
 *
 * Since: 2.4.
 */
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
void
gtk_entry_completion_set_match_func (GtkEntryCompletion          *completion,
                                     GtkEntryCompletionMatchFunc  func,
                                     gpointer                     func_data,
                                     GDestroyNotify               func_notify)
{
  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));

  if (completion->priv->match_notify)
    (* completion->priv->match_notify) (completion->priv->match_data);

  completion->priv->match_func = func;
  completion->priv->match_data = func_data;
  completion->priv->match_notify = func_notify;
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
/**
 * gtk_entry_completion_set_minimum_key_length:
 * @completion: A #GtkEntryCompletion.
 * @length: The minimum length of the key in order to start completing.
 *
 * Requires the length of the search key for @completion to be at least
 * @length. This is useful for long lists, where completing using a small
 * key takes a lot of time and will come up with meaningless results anyway
 * (ie, a too large dataset).
 *
 * Since: 2.4
 */
1073 1074 1075 1076 1077
void
gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
                                             gint                length)
{
  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1078
  g_return_if_fail (length >= 0);
1079

1080 1081 1082 1083
  if (completion->priv->minimum_key_length != length)
    {
      completion->priv->minimum_key_length = length;
     
1084
      g_object_notify (G_OBJECT (completion), "minimum-key-length");
1085
    }
1086 1087
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
/**
 * gtk_entry_completion_get_minimum_key_length:
 * @completion: A #GtkEntryCompletion.
 *
 * Returns the minimum key length as set for @completion.
 *
 * Return value: The currently used minimum key length.
 *
 * Since: 2.4
 */
1098 1099 1100 1101 1102 1103 1104 1105
gint
gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
{
  g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);

  return completion->priv->minimum_key_length;
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
/**
 * gtk_entry_completion_complete:
 * @completion: A #GtkEntryCompletion.
 *
 * Requests a completion operation, or in other words a refiltering of the
 * current list with completions, using the current key. The completion list
 * view will be updated accordingly.
 *
 * Since: 2.4
 */
1116 1117 1118 1119 1120 1121 1122
void
gtk_entry_completion_complete (GtkEntryCompletion *completion)
{
  gchar *tmp;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));

1123 1124 1125
  if (!completion->priv->filter_model)
    return;
  
1126 1127 1128 1129 1130 1131 1132 1133 1134
  if (completion->priv->case_normalized_key)
    g_free (completion->priv->case_normalized_key);

  tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
                          -1, G_NORMALIZE_ALL);
  completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
  g_free (tmp);

  gtk_tree_model_filter_refilter (completion->priv->filter_model);
1135 1136 1137

  if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
    _gtk_entry_completion_resize_popup (completion);
1138 1139 1140 1141 1142
}

static void
gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
                                    gint                index,
1143
                                    const gchar        *string,
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
                                    gboolean            markup)
{
  GtkTreeIter iter;

  gtk_list_store_insert (completion->priv->actions, &iter, index);
  gtk_list_store_set (completion->priv->actions, &iter,
                      0, string,
                      1, markup,
                      -1);

  if (!completion->priv->action_view->parent)
    {
      GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);

      gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
                                path, NULL, FALSE);
      gtk_tree_path_free (path);

      gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
                          completion->priv->action_view, FALSE, FALSE, 0);
      gtk_widget_show (completion->priv->action_view);
    }
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1168 1169 1170
/**
 * gtk_entry_completion_insert_action_text:
 * @completion: A #GtkEntryCompletion.
1171
 * @index_: The index of the item to insert.
Kristian Rietveld's avatar
Kristian Rietveld committed
1172 1173
 * @text: Text of the item to insert.
 *
1174
 * Inserts an action in @completion's action item list at position @index_
Kristian Rietveld's avatar
Kristian Rietveld committed
1175 1176 1177 1178 1179
 * with text @text. If you want the action item to have markup, use
 * gtk_entry_completion_insert_action_markup().
 *
 * Since: 2.4
 */
1180 1181
void
gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1182
                                         gint                index_,
1183
                                         const gchar        *text)
1184 1185 1186 1187
{
  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
  g_return_if_fail (text != NULL);

1188
  gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1189 1190
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1191 1192 1193
/**
 * gtk_entry_completion_insert_action_markup:
 * @completion: A #GtkEntryCompletion.
1194
 * @index_: The index of the item to insert.
Kristian Rietveld's avatar
Kristian Rietveld committed
1195 1196
 * @markup: Markup of the item to insert.
 *
1197
 * Inserts an action in @completion's action item list at position @index_
Kristian Rietveld's avatar
Kristian Rietveld committed
1198 1199 1200 1201
 * with markup @markup.
 *
 * Since: 2.4
 */
1202 1203
void
gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1204
                                           gint                index_,
1205
                                           const gchar        *markup)
1206 1207 1208 1209
{
  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
  g_return_if_fail (markup != NULL);

1210
  gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1211 1212
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1213 1214 1215
/**
 * gtk_entry_completion_delete_action:
 * @completion: A #GtkEntryCompletion.
1216
 * @index_: The index of the item to Delete.
Kristian Rietveld's avatar
Kristian Rietveld committed
1217
 *
1218
 * Deletes the action at @index_ from @completion's action list.
Kristian Rietveld's avatar
Kristian Rietveld committed
1219 1220 1221
 *
 * Since: 2.4
 */
1222 1223
void
gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1224
                                    gint                index_)
1225 1226 1227 1228
{
  GtkTreeIter iter;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1229
  g_return_if_fail (index_ >= 0);
1230 1231

  gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1232
                                 &iter, NULL, index_);
1233 1234 1235
  gtk_list_store_remove (completion->priv->actions, &iter);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1236 1237 1238 1239 1240
/**
 * gtk_entry_completion_set_text_column:
 * @completion: A #GtkEntryCompletion.
 * @column: The column in the model of @completion to get strings from.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1241
 * Convenience function for setting up the most used case of this code: a
Kristian Rietveld's avatar
Kristian Rietveld committed
1242 1243 1244 1245
 * completion list with just strings. This function will set up @completion
 * to have a list displaying all (and just) strings in the completion list,
 * and to get those strings from @column in the model of @completion.
 *
1246
 * This functions creates and adds a #GtkCellRendererText for the selected 
Matthias Clasen's avatar
Matthias Clasen committed
1247 1248
 * column. If you need to set the text column, but don't want the cell 
 * renderer, use g_object_set() to set the ::text_column property directly.
1249
 * 
Kristian Rietveld's avatar
Kristian Rietveld committed
1250 1251
 * Since: 2.4
 */
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
void
gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
                                      gint                column)
{
  GtkCellRenderer *cell;

  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
  g_return_if_fail (column >= 0);

  completion->priv->text_column = column;

  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
                              cell, TRUE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
                                 cell,
                                 "text", column);
1269

1270
  g_object_notify (G_OBJECT (completion), "text-column");
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
}

/**
 * gtk_entry_completion_get_text_column:
 * @completion: a #GtkEntryCompletion
 * 
 * Returns the column in the model of @completion to get strings from.
 * 
 * Return value: the column containing the strings
 *
 * Since: 2.6
 **/
gint
gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
{
Matthias Clasen's avatar
Matthias Clasen committed
1286
  g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1287 1288

  return completion->priv->text_column;  
1289 1290 1291 1292
}

/* private */

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
static gboolean
gtk_entry_completion_list_enter_notify (GtkWidget        *widget,
					GdkEventCrossing *event,
					gpointer          data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
  
  return completion->priv->ignore_enter;
}

static gboolean
gtk_entry_completion_list_motion_notify (GtkWidget      *widget,
					 GdkEventMotion *event,
					 gpointer        data)
{
  GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);

  completion->priv->ignore_enter = FALSE; 
  
  return FALSE;
}


1316
/* some nasty size requisition */
Matthias Clasen's avatar
Matthias Clasen committed
1317
gboolean
1318
_gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1319
{
1320
  gint x, y;
1321
  gint matches, items, height, x_border, y_border;
1322 1323
  GdkScreen