gtkcombobox.c 159 KB
Newer Older
1 2 3
/* gtkcombobox.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
 * You should have received a copy of the GNU Library General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "config.h"
19

Kristian Rietveld's avatar
Kristian Rietveld committed
20
#include "gtkcombobox.h"
21 22

#include "gtkadjustment.h"
23 24
#include "gtkcellareabox.h"
#include "gtktreemenu.h"
25
#include "gtkbindings.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
26
#include "gtkcelllayout.h"
27
#include "gtkcellrenderertext.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
28
#include "gtkcellview.h"
29
#include "gtkcsscustomgadgetprivate.h"
30
#include "gtkeventbox.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
31
#include "gtkframe.h"
32
#include "gtkiconprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
33
#include "gtkbox.h"
34 35
#include "gtkliststore.h"
#include "gtkmain.h"
36
#include "gtkmenuprivate.h"
37
#include "gtkmenushellprivate.h"
38
#include "gtkscrolledwindow.h"
39
#include "deprecated/gtktearoffmenuitem.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
40
#include "gtktogglebutton.h"
41
#include "gtktreeselection.h"
42
#include "gtkwidgetpath.h"
43
#include "gtkwidgetprivate.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
44
#include "gtkwindow.h"
45
#include "gtktypebuiltins.h"
46
#include "gtkprivate.h"
47
#include "gtkcomboboxprivate.h"
48 49 50

#include <gobject/gvaluecollector.h>

Kristian Rietveld's avatar
Kristian Rietveld committed
51
#include <string.h>
52 53 54 55 56
#include <stdarg.h>

#include "gtkmarshalers.h"
#include "gtkintl.h"

57
#include "gtkentryprivate.h"
58
#include "gtktreeprivate.h"
59
#include "a11y/gtkcomboboxaccessible.h"
60

61 62 63 64 65

/**
 * SECTION:gtkcombobox
 * @Short_description: A widget used to choose from a list of items
 * @Title: GtkComboBox
66
 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 *
 * A GtkComboBox is a widget that allows the user to choose from a list of
 * valid choices. The GtkComboBox displays the selected choice. When
 * activated, the GtkComboBox displays a popup which allows the user to
 * make a new choice. The style in which the selected value is displayed,
 * and the style of the popup is determined by the current theme. It may
 * be similar to a Windows-style combo box.
 *
 * The GtkComboBox uses the model-view pattern; the list of valid choices
 * is specified in the form of a tree model, and the display of the choices
 * can be adapted to the data in the model by using cell renderers, as you
 * would in a tree view. This is possible since GtkComboBox implements the
 * #GtkCellLayout interface. The tree model holding the valid choices is
 * not restricted to a flat list, it can be a real tree, and the popup will
 * reflect the tree structure.
 *
83
 * To allow the user to enter values not in the model, the “has-entry”
84 85 86
 * property allows the GtkComboBox to contain a #GtkEntry. This entry
 * can be accessed by calling gtk_bin_get_child() on the combo box.
 *
87 88
 * For a simple list of textual choices, the model-view API of GtkComboBox
 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
89 90
 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
 * an entry.
91 92 93
 *
 * # CSS nodes
 *
94 95
 * |[<!-- language="plain" -->
 * combobox
96 97
 * ├── box.linked
 * │   ╰── button.combo
98 99 100
 * │       ╰── box
 * │           ├── cellview
 * │           ╰── arrow
101
 * ╰── window.popup
102 103
 * ]|
 *
104 105 106 107 108 109 110 111 112
 * A normal combobox contains a box with the .linked class, a button
 * with the .combo class and inside those buttons, there are a cellview and
 * an arrow.
 *
 * |[<!-- language="plain" -->
 * combobox
 * ├── box.linked
 * │   ├── entry.combo
 * │   ╰── button.combo
113 114
 * │       ╰── box
 * │           ╰── arrow
115 116 117 118
 * ╰── window.popup
 * ]|
 *
 * A GtkComboBox with an entry has a single CSS node with name combobox. It
119
 * contains a box with the .linked class. That box contains an entry and a
120
 * button, both with the .combo class added.
121
 * The button also contains another node with name arrow.
122 123 124
 */


125
/* WELCOME, to THE house of evil code */
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
struct _GtkComboBoxPrivate
{
  GtkTreeModel *model;

  GtkCellArea *area;

  gint col_column;
  gint row_column;

  gint wrap_width;

  gint active; /* Only temporary */
  GtkTreeRowReference *active_row;

  GtkWidget *tree_view;

  GtkWidget *cell_view;

144
  GtkWidget *box;
145 146 147 148 149 150 151
  GtkWidget *button;
  GtkWidget *arrow;

  GtkWidget *popup_widget;
  GtkWidget *popup_window;
  GtkWidget *scrolled_window;

152 153
  GtkCssGadget *gadget;

154
  guint popup_idle_id;
155
  GdkEvent *trigger_event;
156 157 158 159
  guint scroll_timer;
  guint resize_idle_id;

  /* For "has-entry" specific behavior we track
160 161
   * an automated cell renderer and text column
   */
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
  gint  text_column;
  GtkCellRenderer *text_renderer;

  gint id_column;

  guint popup_in_progress : 1;
  guint popup_shown : 1;
  guint add_tearoffs : 1;
  guint has_frame : 1;
  guint is_cell_renderer : 1;
  guint editing_canceled : 1;
  guint auto_scroll : 1;
  guint button_sensitivity : 2;
  guint has_entry : 1;
  guint popup_fixed_width : 1;

  GtkTreeViewRowSeparatorFunc row_separator_func;
  gpointer                    row_separator_data;
  GDestroyNotify              row_separator_destroy;

  GdkDevice *grab_pointer;

  gchar *tearoff_title;
};

187 188 189
/* While debugging this evil code, I have learned that
 * there are actually 4 modes to this widget, which can
 * be characterized as follows
Daniel Boles's avatar
Daniel Boles committed
190
 *
191 192 193 194 195 196 197 198
 * 1) menu mode, no child added
 *
 * tree_view -> NULL
 * cell_view -> GtkCellView, regular child
 * button -> GtkToggleButton set_parent to combo
 * arrow -> GtkArrow set_parent to button
 * popup_widget -> GtkMenu
 * popup_window -> NULL
199
 * scrolled_window -> NULL
200 201
 *
 * 2) menu mode, child added
Daniel Boles's avatar
Daniel Boles committed
202
 *
203
 * tree_view -> NULL
Daniel Boles's avatar
Daniel Boles committed
204
 * cell_view -> NULL
205 206 207 208
 * button -> GtkToggleButton set_parent to combo
 * arrow -> GtkArrow, child of button
 * popup_widget -> GtkMenu
 * popup_window -> NULL
209
 * scrolled_window -> NULL
210 211
 *
 * 3) list mode, no child added
Daniel Boles's avatar
Daniel Boles committed
212
 *
213
 * tree_view -> GtkTreeView, child of scrolled_window
214 215 216 217 218
 * cell_view -> GtkCellView, regular child
 * button -> GtkToggleButton, set_parent to combo
 * arrow -> GtkArrow, child of button
 * popup_widget -> tree_view
 * popup_window -> GtkWindow
219
 * scrolled_window -> GtkScrolledWindow, child of popup_window
220 221 222
 *
 * 4) list mode, child added
 *
223
 * tree_view -> GtkTreeView, child of scrolled_window
224 225 226 227 228
 * cell_view -> NULL
 * button -> GtkToggleButton, set_parent to combo
 * arrow -> GtkArrow, child of button
 * popup_widget -> tree_view
 * popup_window -> GtkWindow
229
 * scrolled_window -> GtkScrolledWindow, child of popup_window
Daniel Boles's avatar
Daniel Boles committed
230
 *
231 232
 */

233 234 235 236 237
enum {
  CHANGED,
  MOVE_ACTIVE,
  POPUP,
  POPDOWN,
238
  FORMAT_ENTRY_TEXT,
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_MODEL,
  PROP_WRAP_WIDTH,
  PROP_ROW_SPAN_COLUMN,
  PROP_COLUMN_SPAN_COLUMN,
  PROP_ACTIVE,
  PROP_ADD_TEAROFFS,
  PROP_TEAROFF_TITLE,
  PROP_HAS_FRAME,
  PROP_POPUP_SHOWN,
  PROP_BUTTON_SENSITIVITY,
  PROP_EDITING_CANCELED,
  PROP_HAS_ENTRY,
  PROP_ENTRY_TEXT_COLUMN,
  PROP_POPUP_FIXED_WIDTH,
  PROP_ID_COLUMN,
  PROP_ACTIVE_ID,
  PROP_CELL_AREA
};

static guint combo_box_signals[LAST_SIGNAL] = {0,};

#define SCROLL_TIME  100

/* common */

static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
static void     gtk_combo_box_cell_editable_init   (GtkCellEditableIface *iface);
271
static void     gtk_combo_box_constructed          (GObject          *object);
272
static void     gtk_combo_box_dispose              (GObject          *object);
273
static void     gtk_combo_box_finalize             (GObject          *object);
274
static void     gtk_combo_box_unmap                (GtkWidget        *widget);
275 276
static void     gtk_combo_box_destroy              (GtkWidget        *widget);

277 278 279 280 281 282 283 284 285
static void     gtk_combo_box_set_property         (GObject         *object,
                                                    guint            prop_id,
                                                    const GValue    *value,
                                                    GParamSpec      *spec);
static void     gtk_combo_box_get_property         (GObject         *object,
                                                    guint            prop_id,
                                                    GValue          *value,
                                                    GParamSpec      *spec);

286 287 288 289 290 291 292
static void     gtk_combo_box_grab_focus           (GtkWidget       *widget);
static void     gtk_combo_box_style_updated        (GtkWidget       *widget);
static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
                                                    gpointer         data);
static void     gtk_combo_box_button_state_flags_changed (GtkWidget     *widget,
                                                          GtkStateFlags  previous,
                                                          gpointer       data);
293 294
static void     gtk_combo_box_add                  (GtkContainer    *container,
                                                    GtkWidget       *widget);
295 296
static void     gtk_combo_box_remove               (GtkContainer    *container,
                                                    GtkWidget       *widget);
297 298 299 300 301 302 303 304 305

static void     gtk_combo_box_menu_show            (GtkWidget        *menu,
                                                    gpointer          user_data);
static void     gtk_combo_box_menu_hide            (GtkWidget        *menu,
                                                    gpointer          user_data);

static void     gtk_combo_box_set_popup_widget     (GtkComboBox      *combo_box,
                                                    GtkWidget        *popup);

306 307
static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);

308 309 310 311 312 313
static void     gtk_combo_box_forall               (GtkContainer     *container,
                                                    gboolean          include_internals,
                                                    GtkCallback       callback,
                                                    gpointer          callback_data);
static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
                                                    GdkEventScroll   *event);
314
static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
315
                                                    GtkTreePath      *path);
316

317
static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);
318 319 320 321
static void     gtk_combo_box_real_move_active     (GtkComboBox      *combo_box,
                                                    GtkScrollType     scroll);
static void     gtk_combo_box_real_popup           (GtkComboBox      *combo_box);
static gboolean gtk_combo_box_real_popdown         (GtkComboBox      *combo_box);
322

323 324
/* listening to the model */
static void     gtk_combo_box_model_row_inserted   (GtkTreeModel     *model,
325 326 327
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          user_data);
328
static void     gtk_combo_box_model_row_deleted    (GtkTreeModel     *model,
329 330
                                                    GtkTreePath      *path,
                                                    gpointer          user_data);
331
static void     gtk_combo_box_model_rows_reordered (GtkTreeModel     *model,
332 333 334 335
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gint             *new_order,
                                                    gpointer          user_data);
336
static void     gtk_combo_box_model_row_changed    (GtkTreeModel     *model,
337 338 339
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
340
static void     gtk_combo_box_model_row_expanded   (GtkTreeModel     *model,
341 342 343
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
344

345
/* list */
346 347 348 349 350
static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box,
                                                    gint             *x,
                                                    gint             *y,
                                                    gint             *width,
                                                    gint             *height);
351 352 353 354 355 356 357 358 359
static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);

static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          data);
static gboolean gtk_combo_box_list_key_press       (GtkWidget        *widget,
                                                    GdkEventKey      *event,
                                                    gpointer          data);
360 361 362 363
static gboolean gtk_combo_box_list_enter_notify    (GtkWidget        *widget,
                                                    GdkEventCrossing *event,
                                                    gpointer          data);
static void     gtk_combo_box_list_auto_scroll     (GtkComboBox   *combo,
364 365
                                                    gint           x,
                                                    gint           y);
366
static gboolean gtk_combo_box_list_scroll_timeout  (GtkComboBox   *combo);
367 368 369 370
static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          data);

371
static gboolean gtk_combo_box_list_select_func     (GtkTreeSelection *selection,
372 373 374 375
                                                    GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    gboolean          path_currently_selected,
                                                    gpointer          data);
376

377 378 379 380
static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
381
static void     gtk_combo_box_list_popup_resize    (GtkComboBox      *combo_box);
382 383

/* menu */
384
static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box);
385
static void     gtk_combo_box_update_title         (GtkComboBox      *combo_box);
386 387 388
static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);


389 390 391
static gboolean gtk_combo_box_menu_button_press    (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          user_data);
392
static void     gtk_combo_box_menu_activate        (GtkWidget        *menu,
393 394
                                                    const gchar      *path,
                                                    GtkComboBox      *combo_box);
395
static void     gtk_combo_box_update_sensitivity   (GtkComboBox      *combo_box);
396
static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
397 398
                                                    GdkEventKey      *event,
                                                    gpointer          data);
399
static void     gtk_combo_box_menu_popup           (GtkComboBox      *combo_box,
400
                                                    const GdkEvent   *trigger_event);
401

402
/* cell layout */
403
static GtkCellArea *gtk_combo_box_cell_layout_get_area       (GtkCellLayout    *cell_layout);
404

405
static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
406
                                                              gboolean      group_cycling);
407

408
static void     gtk_combo_box_child_show                     (GtkWidget       *widget,
409
                                                              GtkComboBox     *combo_box);
410
static void     gtk_combo_box_child_hide                     (GtkWidget       *widget,
411
                                                              GtkComboBox     *combo_box);
412

413 414
/* GtkComboBox:has-entry callbacks */
static void     gtk_combo_box_entry_contents_changed         (GtkEntry        *entry,
415
                                                              gpointer         user_data);
416
static void     gtk_combo_box_entry_active_changed           (GtkComboBox     *combo_box,
417
                                                              gpointer         user_data);
418
static gchar   *gtk_combo_box_format_entry_text              (GtkComboBox     *combo_box,
Daniel Boles's avatar
Daniel Boles committed
419
                                                              const gchar     *path);
420 421 422 423

/* GtkBuildable method implementation */
static GtkBuildableIface *parent_buildable_iface;

424
static void     gtk_combo_box_buildable_init                 (GtkBuildableIface *iface);
425 426 427 428
static void     gtk_combo_box_buildable_add_child            (GtkBuildable  *buildable,
                                                              GtkBuilder    *builder,
                                                              GObject       *child,
                                                              const gchar   *type);
429
static gboolean gtk_combo_box_buildable_custom_tag_start     (GtkBuildable  *buildable,
430 431 432 433 434
                                                              GtkBuilder    *builder,
                                                              GObject       *child,
                                                              const gchar   *tagname,
                                                              GMarkupParser *parser,
                                                              gpointer      *data);
435
static void     gtk_combo_box_buildable_custom_tag_end       (GtkBuildable  *buildable,
436 437 438 439
                                                              GtkBuilder    *builder,
                                                              GObject       *child,
                                                              const gchar   *tagname,
                                                              gpointer      *data);
440
static GObject *gtk_combo_box_buildable_get_internal_child   (GtkBuildable *buildable,
441 442
                                                              GtkBuilder   *builder,
                                                              const gchar  *childname);
443

444 445

/* GtkCellEditable method implementations */
446
static void     gtk_combo_box_start_editing                  (GtkCellEditable *cell_editable,
447 448
                                                              GdkEvent        *event);

449
G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
450
                         G_ADD_PRIVATE (GtkComboBox)
451 452 453 454 455 456
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
                                                gtk_combo_box_cell_layout_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
                                                gtk_combo_box_cell_editable_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_combo_box_buildable_init))
457

458

459
/* common */
460 461 462 463 464 465 466 467 468 469 470
static void
gtk_combo_box_measure (GtkCssGadget   *gadget,
                       GtkOrientation  orientation,
                       int             size,
                       int            *minimum,
                       int            *natural,
                       int            *minimum_baseline,
                       int            *natural_baseline,
                       gpointer        data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
471 472
  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
  GtkComboBoxPrivate *priv = combo_box->priv;
473

474 475 476 477 478
  _gtk_widget_get_preferred_size_for_size (priv->box,
                                           orientation,
                                           size,
                                           minimum, natural,
                                           minimum_baseline, natural_baseline);
479 480 481 482 483 484 485 486 487 488 489 490 491
}

static void
gtk_combo_box_allocate (GtkCssGadget        *gadget,
                        const GtkAllocation *allocation,
                        int                  baseline,
                        GtkAllocation       *out_clip,
                        gpointer             data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
  GtkComboBoxPrivate *priv = combo_box->priv;

492 493
  gtk_widget_size_allocate_with_baseline (priv->box, (GtkAllocation *) allocation, baseline);
  gtk_widget_get_clip (priv->box, out_clip);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

  if (!priv->tree_view)
    {
      if (gtk_widget_get_visible (priv->popup_widget))
        {
          gint menu_width;

          if (priv->wrap_width == 0)
            {
              gtk_widget_set_size_request (priv->popup_widget, -1, -1);

              if (priv->popup_fixed_width)
                gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
              else
                gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);

              gtk_widget_set_size_request (priv->popup_widget,
                                           MAX (allocation->width, menu_width), -1);
            }

          /* reposition the menu after giving it a new width */
          gtk_menu_reposition (GTK_MENU (priv->popup_widget));
        }
    }
  else
    {
      if (gtk_widget_get_visible (priv->popup_window))
        {
          gint x, y, width, height;
          gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
          gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
          gtk_widget_set_size_request (priv->popup_window, width, height);
        }
    }
}

static gboolean
gtk_combo_box_render (GtkCssGadget *gadget,
                      cairo_t      *cr,
                      int           x,
                      int           y,
                      int           width,
                      int           height,
                      gpointer      data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
  GtkComboBoxPrivate *priv = combo_box->priv;

  gtk_container_propagate_draw (GTK_CONTAINER (widget),
544
                                priv->box, cr);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639

  return FALSE;
}

static void
gtk_combo_box_get_preferred_width (GtkWidget *widget,
                                   gint      *minimum_size,
                                   gint      *natural_size)
{
  gint dummy;

  /* https://bugzilla.gnome.org/show_bug.cgi?id=729496 */
  if (natural_size == NULL)
    natural_size = &dummy;

  gtk_css_gadget_get_preferred_size (GTK_COMBO_BOX (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum_size, natural_size,
                                     NULL, NULL);
}

static void
gtk_combo_box_get_preferred_height (GtkWidget *widget,
                                    gint      *minimum_size,
                                    gint      *natural_size)
{
  gint min_width;

  /* Combo box is height-for-width only
   * (so we always just reserve enough height for the minimum width) */
  gtk_css_gadget_get_preferred_size (GTK_COMBO_BOX (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     &min_width, NULL,
                                     NULL, NULL);
  gtk_css_gadget_get_preferred_size (GTK_COMBO_BOX (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     min_width,
                                     minimum_size, natural_size,
                                     NULL, NULL);
}

static void
gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
                                              gint       avail_size,
                                              gint      *minimum_size,
                                              gint      *natural_size)
{
  /* Combo box is height-for-width only
   * (so we assume we always reserved enough height for the minimum width) */
  gtk_css_gadget_get_preferred_size (GTK_COMBO_BOX (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     avail_size,
                                     minimum_size, natural_size,
                                     NULL, NULL);
}

static void
gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
                                              gint       avail_size,
                                              gint      *minimum_size,
                                              gint      *natural_size)
{
  gtk_css_gadget_get_preferred_size (GTK_COMBO_BOX (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     avail_size,
                                     minimum_size, natural_size,
                                     NULL, NULL);
}

static void
gtk_combo_box_size_allocate (GtkWidget     *widget,
                             GtkAllocation *allocation)
{
  GtkAllocation clip;

  gtk_widget_set_allocation (widget, allocation);

  gtk_css_gadget_allocate (GTK_COMBO_BOX (widget)->priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
}

static gboolean
gtk_combo_box_draw (GtkWidget *widget,
                    cairo_t   *cr)
{
  gtk_css_gadget_draw (GTK_COMBO_BOX (widget)->priv->gadget, cr);
  return FALSE;
}

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
static void
gtk_combo_box_compute_expand (GtkWidget *widget,
                              gboolean  *hexpand,
                              gboolean  *vexpand)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
  GtkComboBoxPrivate *priv = combo_box->priv;
  GtkWidget *child;

  child = gtk_bin_get_child (GTK_BIN (combo_box));
  if (child && child != priv->cell_view)
    {
      *hexpand = gtk_widget_compute_expand (child, GTK_ORIENTATION_HORIZONTAL);
      *vexpand = gtk_widget_compute_expand (child, GTK_ORIENTATION_VERTICAL);
    }
  else
    {
      *hexpand = FALSE;
      *vexpand = FALSE;
    }
}

662 663 664 665 666 667 668
static void
gtk_combo_box_class_init (GtkComboBoxClass *klass)
{
  GObjectClass *object_class;
  GtkContainerClass *container_class;
  GtkWidgetClass *widget_class;
  GtkBindingSet *binding_set;
669

670 671 672 673
  container_class = (GtkContainerClass *)klass;
  container_class->forall = gtk_combo_box_forall;
  container_class->add = gtk_combo_box_add;
  container_class->remove = gtk_combo_box_remove;
674

675 676
  gtk_container_class_handle_border_width (container_class);

677 678 679 680 681 682 683 684 685 686 687
  widget_class = (GtkWidgetClass *)klass;
  widget_class->size_allocate = gtk_combo_box_size_allocate;
  widget_class->draw = gtk_combo_box_draw;
  widget_class->scroll_event = gtk_combo_box_scroll_event;
  widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
  widget_class->grab_focus = gtk_combo_box_grab_focus;
  widget_class->style_updated = gtk_combo_box_style_updated;
  widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
  widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
  widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
688
  widget_class->unmap = gtk_combo_box_unmap;
689
  widget_class->destroy = gtk_combo_box_destroy;
690
  widget_class->compute_expand = gtk_combo_box_compute_expand;
691

692
  object_class = (GObjectClass *)klass;
693
  object_class->constructed = gtk_combo_box_constructed;
694 695 696 697
  object_class->dispose = gtk_combo_box_dispose;
  object_class->finalize = gtk_combo_box_finalize;
  object_class->set_property = gtk_combo_box_set_property;
  object_class->get_property = gtk_combo_box_get_property;
698

699 700
  klass->format_entry_text = gtk_combo_box_format_entry_text;

701 702 703
  /* signals */
  /**
   * GtkComboBox::changed:
Matthias Clasen's avatar
Matthias Clasen committed
704
   * @widget: the object which received the signal
Daniel Boles's avatar
Daniel Boles committed
705
   *
706
   * The changed signal is emitted when the active
Matthias Clasen's avatar
Matthias Clasen committed
707
   * item is changed. The can be due to the user selecting
708
   * a different item from the list, or due to a
Matthias Clasen's avatar
Matthias Clasen committed
709
   * call to gtk_combo_box_set_active_iter().
710 711
   * It will also be emitted while typing into the entry of a combo box
   * with an entry.
Matthias Clasen's avatar
Matthias Clasen committed
712 713 714
   *
   * Since: 2.4
   */
715
  combo_box_signals[CHANGED] =
716
    g_signal_new (I_("changed"),
717 718 719 720 721 722
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkComboBoxClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
723 724 725 726 727
  /**
   * GtkComboBox::move-active:
   * @widget: the object that received the signal
   * @scroll_type: a #GtkScrollType
   *
728
   * The ::move-active signal is a
729
   * [keybinding signal][GtkBindingSignal]
730 731 732 733
   * which gets emitted to move the active selection.
   *
   * Since: 2.12
   */
734
  combo_box_signals[MOVE_ACTIVE] =
735 736 737 738 739 740 741 742
    g_signal_new_class_handler (I_("move-active"),
                                G_OBJECT_CLASS_TYPE (klass),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_combo_box_real_move_active),
                                NULL, NULL,
                                g_cclosure_marshal_VOID__ENUM,
                                G_TYPE_NONE, 1,
                                GTK_TYPE_SCROLL_TYPE);
743

744 745 746 747
  /**
   * GtkComboBox::popup:
   * @widget: the object that received the signal
   *
748
   * The ::popup signal is a
749
   * [keybinding signal][GtkBindingSignal]
750 751 752 753 754 755
   * which gets emitted to popup the combo box list.
   *
   * The default binding for this signal is Alt+Down.
   *
   * Since: 2.12
   */
756
  combo_box_signals[POPUP] =
757 758 759 760 761 762 763
    g_signal_new_class_handler (I_("popup"),
                                G_OBJECT_CLASS_TYPE (klass),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_combo_box_real_popup),
                                NULL, NULL,
                                g_cclosure_marshal_VOID__VOID,
                                G_TYPE_NONE, 0);
764 765 766 767
  /**
   * GtkComboBox::popdown:
   * @button: the object which received the signal
   *
768
   * The ::popdown signal is a
769
   * [keybinding signal][GtkBindingSignal]
770 771 772 773 774 775
   * which gets emitted to popdown the combo box list.
   *
   * The default bindings for this signal are Alt+Up and Escape.
   *
   * Since: 2.12
   */
776
  combo_box_signals[POPDOWN] =
777 778 779 780 781 782 783
    g_signal_new_class_handler (I_("popdown"),
                                G_OBJECT_CLASS_TYPE (klass),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_combo_box_real_popdown),
                                NULL, NULL,
                                _gtk_marshal_BOOLEAN__VOID,
                                G_TYPE_BOOLEAN, 0);
784

785 786 787 788 789 790 791 792 793 794 795 796
  /**
   * GtkComboBox::format-entry-text:
   * @combo: the object which received the signal
   * @path: the GtkTreePath string from the combo box's current model to format text for
   *
   * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
   *
   * A signal which allows you to change how the text displayed in a combo box's
   * entry is displayed.
   *
   * Connect a signal handler which returns an allocated string representing
   * @path. That string will then be used to set the text in the combo box's entry.
Daniel Boles's avatar
Daniel Boles committed
797
   * The default signal handler uses the text from the GtkComboBox::entry-text-column
798 799 800 801
   * model column.
   *
   * Here's an example signal handler which fetches data from the model and
   * displays it in the entry.
802
   * |[<!-- language="C" -->
803 804 805 806 807 808 809 810
   * static gchar*
   * format_entry_text_callback (GtkComboBox *combo,
   *                             const gchar *path,
   *                             gpointer     user_data)
   * {
   *   GtkTreeIter iter;
   *   GtkTreeModel model;
   *   gdouble      value;
Daniel Boles's avatar
Daniel Boles committed
811
   *
812 813 814
   *   model = gtk_combo_box_get_model (combo);
   *
   *   gtk_tree_model_get_iter_from_string (model, &iter, path);
Daniel Boles's avatar
Daniel Boles committed
815
   *   gtk_tree_model_get (model, &iter,
816 817 818
   *                       THE_DOUBLE_VALUE_COLUMN, &value,
   *                       -1);
   *
819
   *   return g_strdup_printf ("%g", value);
820 821 822
   * }
   * ]|
   *
Daniel Boles's avatar
Daniel Boles committed
823
   * Returns: (transfer full): a newly allocated string representing @path
824 825 826 827 828 829 830 831 832 833 834 835 836
   * for the current GtkComboBox model.
   *
   * Since: 3.4
   */
  combo_box_signals[FORMAT_ENTRY_TEXT] =
    g_signal_new (I_("format-entry-text"),
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text),
                  _gtk_single_string_accumulator, NULL,
                  _gtk_marshal_STRING__STRING,
                  G_TYPE_STRING, 1, G_TYPE_STRING);

837 838 839
  /* key bindings */
  binding_set = gtk_binding_set_by_class (widget_class);

840
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
841
                                "popup", 0);
842
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
843
                                "popup", 0);
844

845
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
846
                                "popdown", 0);
847
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
848
                                "popdown", 0);
849
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
850
                                "popdown", 0);
851

852
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
853 854
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
855
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
856 857
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
858
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
859 860
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
861
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
862 863
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
864
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
865 866
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
867
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
868 869
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
870

871
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
872 873
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
874
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
875 876
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
877
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
878 879
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
880
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
881 882
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
883
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
884 885
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
886
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
887 888
                                "move-active", 1,
                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
889

890
  /* properties */
891 892 893 894
  g_object_class_override_property (object_class,
                                    PROP_EDITING_CANCELED,
                                    "editing-canceled");

Matthias Clasen's avatar
Matthias Clasen committed
895 896 897 898
  /**
   * GtkComboBox:model:
   *
   * The model from which the combo box takes the values shown
899
   * in the list.
Matthias Clasen's avatar
Matthias Clasen committed
900 901 902
   *
   * Since: 2.4
   */
903 904 905
  g_object_class_install_property (object_class,
                                   PROP_MODEL,
                                   g_param_spec_object ("model",
906 907
                                                        P_("ComboBox model"),
                                                        P_("The model for the combo box"),
908
                                                        GTK_TYPE_TREE_MODEL,
909
                                                        GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
910

Matthias Clasen's avatar
Matthias Clasen committed
911 912 913
  /**
   * GtkComboBox:wrap-width:
   *
914 915
   * If wrap-width is set to a positive value, items in the popup will be laid
   * out along multiple columns, starting a new row on reaching the wrap width.
Matthias Clasen's avatar
Matthias Clasen committed
916 917 918
   *
   * Since: 2.4
   */
919 920
  g_object_class_install_property (object_class,
                                   PROP_WRAP_WIDTH,
921
                                   g_param_spec_int ("wrap-width",
922
                                                     P_("Wrap width"),
923
                                                     P_("Wrap width for laying out the items in a grid"),
924 925 926
                                                     0,
                                                     G_MAXINT,
                                                     0,
927
                                                     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
928

Matthias Clasen's avatar
Matthias Clasen committed
929 930 931 932

  /**
   * GtkComboBox:row-span-column:
   *
933
   * If this is set to a non-negative value, it must be the index of a column
934
   * of type %G_TYPE_INT in the model. The value in that column for each item
935
   * will determine how many rows that item will span in the popup. Therefore,
936
   * values in this column must be greater than zero.
Matthias Clasen's avatar
Matthias Clasen committed
937 938 939
   *
   * Since: 2.4
   */
940 941
  g_object_class_install_property (object_class,
                                   PROP_ROW_SPAN_COLUMN,
942
                                   g_param_spec_int ("row-span-column",
943 944
                                                     P_("Row span column"),
                                                     P_("TreeModel column containing the row span values"),
945
                                                     -1,
946
                                                     G_MAXINT,
947
                                                     -1,
948
                                                     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
949

Matthias Clasen's avatar
Matthias Clasen committed
950 951 952 953

  /**
   * GtkComboBox:column-span-column:
   *
954
   * If this is set to a non-negative value, it must be the index of a column
955 956 957 958
   * of type %G_TYPE_INT in the model. The value in that column for each item
   * will determine how many columns that item will span in the popup.
   * Therefore, values in this column must be greater than zero, and the sum of
   * an item’s column position + span should not exceed #GtkComboBox:wrap-width.
Matthias Clasen's avatar
Matthias Clasen committed
959 960 961
   *
   * Since: 2.4
   */
962 963
  g_object_class_install_property (object_class,
                                   PROP_COLUMN_SPAN_COLUMN,
964
                                   g_param_spec_int ("column-span-column",
965 966
                                                     P_("Column span column"),
                                                     P_("TreeModel column containing the column span values"),
967
                                                     -1,
968
                                                     G_MAXINT,
969
                                                     -1,
970
                                                     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
971

972

Matthias Clasen's avatar
Matthias Clasen committed
973 974 975 976 977
  /**
   * GtkComboBox:active:
   *
   * The item which is currently active. If the model is a non-flat treemodel,
   * and the active item is not an immediate child of the root of the tree,
978
   * this property has the value
979 980
   * `gtk_tree_path_get_indices (path)[0]`,
   * where `path` is the #GtkTreePath of the active item.
Matthias Clasen's avatar
Matthias Clasen committed
981 982 983
   *
   * Since: 2.4
   */
984 985 986
  g_object_class_install_property (object_class,
                                   PROP_ACTIVE,
                                   g_param_spec_int ("active",
987 988
                                                     P_("Active item"),
                                                     P_("The item which is currently active"),
989
                                                     -1,
990
                                                     G_MAXINT,
991
                                                     -1,
992
                                                     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
993

994 995 996
  /**
   * GtkComboBox:add-tearoffs:
   *
997 998
   * The add-tearoffs property controls whether generated menus
   * have tearoff menu items.
999 1000 1001 1002
   *
   * Note that this only affects menu style combo boxes.
   *
   * Since: 2.6
1003 1004
   *
   * Deprecated: 3.10
1005 1006 1007
   */
  g_object_class_install_property (object_class,
                                   PROP_ADD_TEAROFFS,
1008 1009 1010 1011
                                   g_param_spec_boolean ("add-tearoffs",
                                                         P_("Add tearoffs to menus"),
                                                         P_("Whether dropdowns should have a tearoff menu item"),
                                                         FALSE,
1012
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY|G_PARAM_DEPRECATED));
1013

1014 1015 1016
  /**
   * GtkComboBox:has-frame:
   *
Matthias Clasen's avatar
Matthias Clasen committed
1017
   * The has-frame property controls whether a frame
1018 1019 1020 1021 1022 1023
   * is drawn around the entry.
   *
   * Since: 2.6
   */
  g_object_class_install_property (object_class,
                                   PROP_HAS_FRAME,
1024 1025 1026 1027
                                   g_param_spec_boolean ("has-frame",
                                                         P_("Has Frame"),
                                                         P_("Whether the combo box draws a frame around the child"),
                                                         TRUE,
1028
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
1029

1030 1031 1032
  /**
   * GtkComboBox:tearoff-title:
   *
1033
   * A title that may be displayed by the window manager
1034 1035 1036
   * when the popup is torn-off.
   *
   * Since: 2.10
1037 1038
   *
   * Deprecated: 3.10
1039 1040 1041 1042 1043 1044
   */
  g_object_class_install_property (object_class,
                                   PROP_TEAROFF_TITLE,
                                   g_param_spec_string ("tearoff-title",
                                                        P_("Tearoff Title"),
                                                        P_("A title that may be displayed by the window manager when the popup is torn-off"),
1045
                                                        NULL,
1046
                                                        GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY|G_PARAM_DEPRECATED));
1047 1048


1049 1050 1051
  /**
   * GtkComboBox:popup-shown:
   *
1052
   * Whether the combo boxes dropdown is popped up.
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
   * Note that this property is mainly useful, because
   * it allows you to connect to notify::popup-shown.
   *
   * Since: 2.10
   */
  g_object_class_install_property (object_class,
                                   PROP_POPUP_SHOWN,
                                   g_param_spec_boolean ("popup-shown",
                                                         P_("Popup shown"),
                                                         P_("Whether the combo's dropdown is shown"),
                                                         FALSE,
                                                         GTK_PARAM_READABLE));
1065

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

   /**
    * GtkComboBox:button-sensitivity:
    *
    * Whether the dropdown button is sensitive when
    * the model is empty.
    *
    * Since: 2.14
    */
   g_object_class_install_property (object_class,
                                    PROP_BUTTON_SENSITIVITY,
                                    g_param_spec_enum ("button-sensitivity",
                                                       P_("Button Sensitivity"),
                                                       P_("Whether the dropdown button is sensitive when the model is empty"),
                                                       GTK_TYPE_SENSITIVITY_TYPE,
                                                       GTK_SENSITIVITY_AUTO,
1082
                                                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
1083

1084 1085 1086 1087 1088
   /**
    * GtkComboBox:has-entry:
    *
    * Whether the combo box has an entry.
    *
1089
    * Since: 2.24
1090 1091 1092 1093
    */
   g_object_class_install_property (object_class,
                                    PROP_HAS_ENTRY,
                                    g_param_spec_boolean ("has-entry",
1094 1095 1096
                                                          P_("Has Entry"),
                                                          P_("Whether combo box has an entry"),
                                                          FALSE,
1097
                                                          GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
1098 1099 1100 1101 1102 1103 1104

   /**
    * GtkComboBox:entry-text-column:
    *
    * 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.
    *
1105
    * Since: 2.24
1106 1107 1108 1109
    */
   g_object_class_install_property (object_class,
                                    PROP_ENTRY_TEXT_COLUMN,
                                    g_param_spec_int ("entry-text-column",
1110 1111 1112 1113 1114
                                                      P_("Entry Text Column"),
                                                      P_("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"),
                                                      -1, G_MAXINT, -1,
1115
                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
1116

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
   /**
    * GtkComboBox:id-column:
    *
    * The column in the combo box's model that provides string
    * IDs for the values in the model, if != -1.
    *
    * Since: 3.0
    */
   g_object_class_install_property (object_class,
                                    PROP_ID_COLUMN,
                                    g_param_spec_int ("id-column",
                                                      P_("ID Column"),
                                                      P_("The column in the combo box's model that provides "
                                                      "string IDs for the values in the model"),
                                                      -1, G_MAXINT, -1,
1132
                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

   /**
    * GtkComboBox:active-id:
    *
    * The value of the ID column of the active row.
    *
    * Since: 3.0
    */
   g_object_class_install_property (object_class,
                                    PROP_ACTIVE_ID,
                                    g_param_spec_string ("active-id",
                                                         P_("Active id"),
                                                         P_("The value of the id column "
                                                         "for the active row"),
1147 1148
                                                         NULL,
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
1149

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
   /**
    * GtkComboBox:popup-fixed-width:
    *
    * Whether the popup's width should be a fixed width matching the
    * allocated width of the combo box.
    *
    * Since: 3.0
    */
   g_object_class_install_property (object_class,
                                    PROP_POPUP_FIXED_WIDTH,
                                    g_param_spec_boolean ("popup-fixed-width",
1161 1162 1163 1164 1165
                                                          P_("Popup Fixed Width"),
                                                          P_("Whether the popup's width should be a "
                                                             "fixed width matching the allocated width "
                                                             "of the combo box"),
                                                          TRUE,
1166
                                                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
1167

1168 1169 1170 1171 1172
   /**
    * GtkComboBox:cell-area:
    *
    * The #GtkCellArea used to layout cell renderers for this combo box.
    *
Daniel Boles's avatar
Daniel Boles committed
1173
    * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
1174 1175
    * a horizontally oriented #GtkCellAreaBox will be used.
    *
1176 1177 1178 1179 1180
    * Since: 3.0
    */
   g_object_class_install_property (object_class,
                                    PROP_CELL_AREA,
                                    g_param_spec_object ("cell-area",
1181 1182 1183
                                                         P_("Cell Area"),
                                                         P_("The GtkCellArea used to layout cells"),
                                                         GTK_TYPE_CELL_AREA,
1184
                                                         GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
1185

1186
  gtk_widget_class_install_style_property (widget_class,
1187 1188
                                           g_param_spec_boolean ("appears-as-list",
                                                                 P_("Appears as list"),
1189
                                                                 P_("Whether dropdowns should look like lists rather than menus"),
1190
                                                                 FALSE,
1191
                                                                 GTK_PARAM_READABLE));
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201
  /**
   * GtkComboBox:arrow-size:
   *
   * Sets the minimum size of the arrow in the combo box.  Note
   * that the arrow size is coupled to the font size, so in case
   * a larger font is used, the arrow will be larger than set
   * by arrow size.
   *
   * Since: 2.12
1202 1203 1204
   *
   * Deprecated: 3.20: use the standard min-width/min-height CSS properties on
   *   the arrow node; the value of this style property is ignored.
1205 1206
   */
  gtk_widget_class_install_style_property (widget_class,
1207 1208 1209 1210 1211 1212
                                           g_param_spec_int ("arrow-size",
                                                             P_("Arrow Size"),
                                                             P_("The minimum size of the arrow in the combo box"),
                                                             0,
                                                             G_MAXINT,
                                                             15,
1213
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
1214

1215 1216 1217 1218 1219 1220
  /**
   * GtkComboBox:arrow-scaling:
   *
   * Sets the amount of space used up by the combobox arrow,
   * proportional to the font size.
   *
1221 1222
   * Deprecated: 3.20: use the standard min-width/min-height CSS properties on
   *   the arrow node; the value of this style property is ignored.
1223 1224 1225 1226 1227 1228 1229 1230
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_float ("arrow-scaling",
                                                               P_("Arrow Scaling"),
                                                               P_("The amount of space used by the arrow"),
                                                             0,
                                                             2.0,
                                                             1.0,
1231
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
1232

1233 1234 1235 1236 1237 1238
  /**
   * GtkComboBox:shadow-type:
   *
   * Which kind of shadow to draw around the combo box.
   *
   * Since: 2.12
1239 1240 1241
   *
   * Deprecated: 3.20: use CSS styling to change the appearance of the combobox
   *   frame; the value of this style property is ignored.
1242 1243 1244 1245 1246 1247 1248
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_enum ("shadow-type",
                                                              P_("Shadow type"),
                                                              P_("Which kind of shadow to draw around the combo box"),
                                                              GTK_TYPE_SHADOW_TYPE,
                                                              GTK_SHADOW_NONE,
1249
                                                              GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
1250

1251
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkcombobox.ui");
1252
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkComboBox, box);
1253 1254 1255 1256
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkComboBox, button);
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkComboBox, arrow);
  gtk_widget_class_bind_template_callback (widget_class, gtk_combo_box_button_toggled);

1257
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1258
  gtk_widget_class_set_css_name (widget_class, "combobox");
1259 1260
}

1261 1262 1263 1264
static void
gtk_combo_box_buildable_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
1265
  iface->add_child = gtk_combo_box_buildable_add_child;
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
  iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
  iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
  iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
}

static void
gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
{
  iface->get_area = gtk_combo_box_cell_layout_get_area;
}

static void
gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
{
  iface->start_editing = gtk_combo_box_start_editing;
}

1283 1284 1285
static void
gtk_combo_box_init (GtkComboBox *combo_box)
{
1286
  GtkComboBoxPrivate *priv;
1287
  GtkCssNode *widget_node;
1288
  GtkStyleContext *context;
1289

1290
  combo_box->priv = gtk_combo_box_get_instance_private (combo_box);
1291
  priv = combo_box->priv;
1292

1293
  priv->wrap_width = 0;
1294

1295
  priv->active = -1;
1296 1297 1298
  priv->active_row = NULL;
  priv->col_column = -1;
  priv->row_column = -1;
1299

1300 1301 1302 1303 1304 1305
  priv->popup_shown = FALSE;
  priv->add_tearoffs = FALSE;
  priv->has_frame = TRUE;
  priv->is_cell_renderer = FALSE;
  priv->editing_canceled = FALSE;
  priv->auto_scroll = FALSE;
1306
  priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1307
  priv->has_entry = FALSE;
1308
  priv->popup_fixed_width = TRUE;
1309 1310 1311

  priv->text_column = -1;
  priv->text_renderer = NULL;
1312
  priv->id_column = -1;
1313

1314
  g_type_ensure (GTK_TYPE_ICON);
1315
  gtk_widget_init_template (GTK_WIDGET (combo_box));
1316 1317

  gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
1318 1319 1320 1321

  context = gtk_widget_get_style_context (priv->button);
  gtk_style_context_remove_class (context, "toggle");
  gtk_style_context_add_class (context, "combo");
1322 1323 1324 1325 1326 1327 1328 1329

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (combo_box));
  priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                     GTK_WIDGET (combo_box),
                                                     gtk_combo_box_measure,
                                                     gtk_combo_box_allocate,
                                                     gtk_combo_box_render,
                                                     NULL, NULL);
1330 1331
}

1332 1333 1334 1335 1336
static void
gtk_combo_box_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
1337
{
1338
  GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1339
  GtkComboBoxPrivate *priv = combo_box->priv;
1340
  GtkCellArea *area;
1341

1342
  switch (prop_id)
1343
    {
1344 1345 1346
    case PROP_MODEL:
      gtk_combo_box_set_model (combo_box, g_value_get_object (value));
      break;
1347

1348 1349 1350
    case PROP_WRAP_WIDTH:
      gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
      break;
1351

1352 1353 1354
    case PROP_ROW_SPAN_COLUMN:
      gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
      break;
1355

1356 1357 1358
    case PROP_COLUMN_SPAN_COLUMN:
      gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
      break;
1359

1360 1361 1362
    case PROP_ACTIVE:
      gtk_combo_box_set_active (combo_box, g_value_get_int (value));
      break;
1363

1364
    case PROP_ADD_TEAROFFS:
1365
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1366
      gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1367
G_GNUC_END_IGNORE_DEPRECATIONS;
1368
      break;
1369

1370
    case PROP_HAS_FRAME:
1371
      if (priv->has_frame != g_value_get_boolean (value))
1372
        {
1373 1374 1375 1376 1377
          priv->has_frame = g_value_get_boolean (value);
          if (priv->has_entry)
            gtk_entry_set_has_frame (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box))),
                                     priv->has_frame);
          g_object_notify (object, "has-frame");
1378 1379
        }
      break;
1380

1381
    case PROP_TEAROFF_TITLE:
1382
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1383
      gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1384
G_GNUC_END_IGNORE_DEPRECATIONS;
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
      break;

    case PROP_POPUP_SHOWN:
      if (g_value_get_boolean (value))
        gtk_combo_box_popup (combo_box);
      else
        gtk_combo_box_popdown (combo_box);
      break;

    case PROP_BUTTON_SENSITIVITY:
      gtk_combo_box_set_button_sensitivity (combo_box,
                                            g_value_get_enum (value));
      break;