gtkcombobox.c 146 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 15
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
16 17 18 19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include <config.h>
21
#include "gtkalias.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
22
#include "gtkcombobox.h"
23 24 25

#include "gtkarrow.h"
#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 "gtkeventbox.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
30
#include "gtkframe.h"
31
#include "gtkhbox.h"
32 33 34
#include "gtkliststore.h"
#include "gtkmain.h"
#include "gtkmenu.h"
35
#include "gtkscrolledwindow.h"
36
#include "gtkseparatormenuitem.h"
37
#include "gtktearoffmenuitem.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
38
#include "gtktogglebutton.h"
39
#include "gtktreeselection.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
40 41
#include "gtkvseparator.h"
#include "gtkwindow.h"
42 43 44 45 46

#include <gdk/gdkkeysyms.h>

#include <gobject/gvaluecollector.h>

Kristian Rietveld's avatar
Kristian Rietveld committed
47
#include <string.h>
48 49 50 51 52
#include <stdarg.h>

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

53
#include "gtktreeprivate.h"
54 55 56 57 58 59 60 61 62

/* WELCOME, to THE house of evil code */

typedef struct _ComboCellInfo ComboCellInfo;
struct _ComboCellInfo
{
  GtkCellRenderer *cell;
  GSList *attributes;

63 64 65 66
  GtkCellLayoutDataFunc func;
  gpointer func_data;
  GDestroyNotify destroy;

67 68 69 70
  guint expand : 1;
  guint pack : 1;
};

71 72
#define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))

73 74 75 76 77 78 79 80 81
struct _GtkComboBoxPrivate
{
  GtkTreeModel *model;

  gint col_column;
  gint row_column;

  gint wrap_width;

82
  GtkTreeRowReference *active_row;
83 84 85 86 87 88 89 90

  GtkWidget *tree_view;
  GtkTreeViewColumn *column;

  GtkWidget *cell_view;
  GtkWidget *cell_view_frame;

  GtkWidget *button;
91
  GtkWidget *box;
92 93 94 95 96 97
  GtkWidget *arrow;
  GtkWidget *separator;

  GtkWidget *popup_widget;
  GtkWidget *popup_window;
  GtkWidget *popup_frame;
98
  GtkWidget *scrolled_window;
99 100 101

  guint inserted_id;
  guint deleted_id;
102 103
  guint reordered_id;
  guint changed_id;
104
  guint popup_idle_id;
105
  guint scroll_timer;
106 107 108 109 110

  gint width;
  GSList *cells;

  guint popup_in_progress : 1;
111
  guint destroying : 1;
112
  guint add_tearoffs : 1;
113 114 115
  guint has_frame : 1;
  guint is_cell_renderer : 1;
  guint editing_canceled : 1;
116
  guint auto_scroll : 1;
Matthias Clasen's avatar
Matthias Clasen committed
117
  guint focus_on_click : 1;
118 119 120 121

  GtkTreeViewRowSeparatorFunc row_separator_func;
  gpointer                    row_separator_data;
  GtkDestroyNotify            row_separator_destroy;
122 123
};

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/* While debugging this evil code, I have learned that
 * there are actually 4 modes to this widget, which can
 * be characterized as follows
 * 
 * 1) menu mode, no child added
 *
 * tree_view -> NULL
 * cell_view -> GtkCellView, regular child
 * cell_view_frame -> NULL
 * button -> GtkToggleButton set_parent to combo
 * arrow -> GtkArrow set_parent to button
 * separator -> GtkVSepator set_parent to button
 * popup_widget -> GtkMenu
 * popup_window -> NULL
 * popup_frame -> NULL
139
 * scrolled_window -> NULL
140 141 142 143 144 145 146 147 148 149 150 151
 *
 * 2) menu mode, child added
 * 
 * tree_view -> NULL
 * cell_view -> NULL 
 * cell_view_frame -> NULL
 * button -> GtkToggleButton set_parent to combo
 * arrow -> GtkArrow, child of button
 * separator -> NULL
 * popup_widget -> GtkMenu
 * popup_window -> NULL
 * popup_frame -> NULL
152
 * scrolled_window -> NULL
153 154 155 156 157 158 159 160 161 162 163 164
 *
 * 3) list mode, no child added
 * 
 * tree_view -> GtkTreeView, child of popup_frame
 * cell_view -> GtkCellView, regular child
 * cell_view_frame -> GtkFrame, set parent to combo
 * button -> GtkToggleButton, set_parent to combo
 * arrow -> GtkArrow, child of button
 * separator -> NULL
 * popup_widget -> tree_view
 * popup_window -> GtkWindow
 * popup_frame -> GtkFrame, child of popup_window
165
 * scrolled_window -> GtkScrolledWindow, child of popup_frame
166 167 168 169 170 171 172 173 174 175 176 177
 *
 * 4) list mode, child added
 *
 * tree_view -> GtkTreeView, child of popup_frame
 * cell_view -> NULL
 * cell_view_frame -> NULL
 * button -> GtkToggleButton, set_parent to combo
 * arrow -> GtkArrow, child of button
 * separator -> NULL
 * popup_widget -> tree_view
 * popup_window -> GtkWindow
 * popup_frame -> GtkFrame, child of popup_window
178
 * scrolled_window -> GtkScrolledWindow, child of popup_frame
179 180 181
 * 
 */

182 183 184 185 186 187 188 189 190 191 192
enum {
  CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_MODEL,
  PROP_WRAP_WIDTH,
  PROP_ROW_SPAN_COLUMN,
  PROP_COLUMN_SPAN_COLUMN,
193
  PROP_ACTIVE,
194
  PROP_ADD_TEAROFFS,
Matthias Clasen's avatar
Matthias Clasen committed
195 196
  PROP_HAS_FRAME,
  PROP_FOCUS_ON_CLICK
197 198 199 200 201 202
};

static GtkBinClass *parent_class = NULL;
static guint combo_box_signals[LAST_SIGNAL] = {0,};

#define BONUS_PADDING 4
203
#define SCROLL_TIME  100
204 205 206

/* common */
static void     gtk_combo_box_class_init           (GtkComboBoxClass *klass);
207
static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
208
static void     gtk_combo_box_cell_editable_init   (GtkCellEditableIface *iface);
209
static void     gtk_combo_box_init                 (GtkComboBox      *combo_box);
210 211
static void     gtk_combo_box_finalize             (GObject          *object);
static void     gtk_combo_box_destroy              (GtkObject        *object);
212 213 214 215 216 217 218 219 220 221

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);

222 223
static void     gtk_combo_box_state_changed        (GtkWidget        *widget,
			                            GtkStateType      previous);
224
static void     gtk_combo_box_style_set            (GtkWidget       *widget,
225
                                                    GtkStyle        *previous);
226 227
static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
                                                    gpointer         data);
228 229 230
static void     gtk_combo_box_button_state_changed (GtkWidget       *widget,
			                            GtkStateType     previous,
						    gpointer         data);
231 232
static void     gtk_combo_box_add                  (GtkContainer    *container,
                                                    GtkWidget       *widget);
233 234
static void     gtk_combo_box_remove               (GtkContainer    *container,
                                                    GtkWidget       *widget);
235 236 237 238 239 240 241 242 243 244 245

static ComboCellInfo *gtk_combo_box_get_cell_info  (GtkComboBox      *combo_box,
                                                    GtkCellRenderer  *cell);

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);
246 247 248 249 250 251 252 253 254 255
static void     gtk_combo_box_menu_position_below  (GtkMenu          *menu,
                                                    gint             *x,
                                                    gint             *y,
                                                    gint             *push_in,
                                                    gpointer          user_data);
static void     gtk_combo_box_menu_position_over   (GtkMenu          *menu,
                                                    gint             *x,
                                                    gint             *y,
                                                    gint             *push_in,
                                                    gpointer          user_data);
256 257 258 259 260 261 262 263
static void     gtk_combo_box_menu_position        (GtkMenu          *menu,
                                                    gint             *x,
                                                    gint             *y,
                                                    gint             *push_in,
                                                    gpointer          user_data);

static gint     gtk_combo_box_calc_requested_width (GtkComboBox      *combo_box,
                                                    GtkTreePath      *path);
264
static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
265

266 267
static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);

268 269 270 271 272 273 274 275 276 277
static void     gtk_combo_box_size_request         (GtkWidget        *widget,
                                                    GtkRequisition   *requisition);
static void     gtk_combo_box_size_allocate        (GtkWidget        *widget,
                                                    GtkAllocation    *allocation);
static void     gtk_combo_box_forall               (GtkContainer     *container,
                                                    gboolean          include_internals,
                                                    GtkCallback       callback,
                                                    gpointer          callback_data);
static gboolean gtk_combo_box_expose_event         (GtkWidget        *widget,
                                                    GdkEventExpose   *event);
278 279
static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
                                                    GdkEventScroll   *event);
280
static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
281
						    GtkTreePath      *path);
282 283 284
static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
285

286 287
static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/* listening to the model */
static void     gtk_combo_box_model_row_inserted   (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gpointer          user_data);
static void     gtk_combo_box_model_row_deleted    (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    gpointer          user_data);
static void     gtk_combo_box_model_rows_reordered (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gint             *new_order,
						    gpointer          user_data);
static void     gtk_combo_box_model_row_changed    (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gpointer          data);
305 306 307 308
static void     gtk_combo_box_model_row_expanded   (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gpointer          data);
309

310
/* list */
311 312 313 314 315
static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
						    gint             *x, 
						    gint             *y, 
						    gint             *width,
						    gint             *height);
316 317 318
static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);

319 320
static void     gtk_combo_box_list_remove_grabs    (GtkComboBox      *combo_box);

321 322 323 324 325 326
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);
327 328 329 330 331 332 333
static gboolean gtk_combo_box_list_enter_notify    (GtkWidget        *widget,
                                                    GdkEventCrossing *event,
                                                    gpointer          data);
static void     gtk_combo_box_list_auto_scroll     (GtkComboBox   *combo,
						    gint           x,
						    gint           y);
static gboolean gtk_combo_box_list_scroll_timeout  (GtkComboBox   *combo);
334 335 336 337 338 339 340 341
static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          data);

static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
342
static void     gtk_combo_box_list_popup_resize    (GtkComboBox      *combo_box);
343 344 345

/* menu */
static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
346
                                                    gboolean          add_children);
347
static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
348 349 350
static void     gtk_combo_box_menu_fill_level      (GtkComboBox      *combo_box,
						    GtkWidget        *menu,
						    GtkTreeIter      *iter);
351 352 353
static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);

static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
354 355 356
						    GtkWidget        *item,
                                                    GtkTreeIter      *iter,
						    GtkWidget        *last);
357 358 359 360 361 362 363 364 365 366 367 368 369 370
static void     gtk_combo_box_relayout             (GtkComboBox      *combo_box);

static gboolean gtk_combo_box_menu_button_press    (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          user_data);
static void     gtk_combo_box_menu_item_activate   (GtkWidget        *item,
                                                    gpointer          user_data);
static void     gtk_combo_box_menu_row_inserted    (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          user_data);
static void     gtk_combo_box_menu_row_deleted     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    gpointer          user_data);
371 372 373 374 375
static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gint             *new_order,
						    gpointer          user_data);
376 377 378 379
static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
380 381 382
static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
383 384 385 386 387 388
static void     gtk_combo_box_menu_popup           (GtkComboBox      *combo_box,
						    guint             button, 
						    guint32           activate_time);
static GtkWidget *gtk_cell_view_menu_item_new      (GtkComboBox      *combo_box,
						    GtkTreeModel     *model,
						    GtkTreeIter      *iter);
389

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
/* cell layout */
static void     gtk_combo_box_cell_layout_pack_start         (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              gboolean               expand);
static void     gtk_combo_box_cell_layout_pack_end           (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              gboolean               expand);
static void     gtk_combo_box_cell_layout_clear              (GtkCellLayout         *layout);
static void     gtk_combo_box_cell_layout_add_attribute      (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              const gchar           *attribute,
                                                              gint                   column);
static void     gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              GtkCellLayoutDataFunc  func,
                                                              gpointer               func_data,
                                                              GDestroyNotify         destroy);
static void     gtk_combo_box_cell_layout_clear_attributes   (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell);
Kristian Rietveld's avatar
Kristian Rietveld committed
409 410 411
static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              gint                   position);
412 413
static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
							      gboolean      group_cycling);
414

415 416 417 418 419 420 421
static void     gtk_combo_box_sync_cells                     (GtkComboBox   *combo_box,
					                      GtkCellLayout *cell_layout);
static void     combo_cell_data_func                         (GtkCellLayout   *cell_layout,
							      GtkCellRenderer *cell,
							      GtkTreeModel    *tree_model,
							      GtkTreeIter     *iter,
							      gpointer         data);
422

423 424 425 426 427
/* GtkCellEditable method implementations */
static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
					 GdkEvent        *event);


428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
GType
gtk_combo_box_get_type (void)
{
  static GType combo_box_type = 0;

  if (!combo_box_type)
    {
      static const GTypeInfo combo_box_info =
        {
          sizeof (GtkComboBoxClass),
          NULL, /* base_init */
          NULL, /* base_finalize */
          (GClassInitFunc) gtk_combo_box_class_init,
          NULL, /* class_finalize */
          NULL, /* class_data */
          sizeof (GtkComboBox),
          0,
          (GInstanceInitFunc) gtk_combo_box_init
        };

448 449 450 451 452 453 454
      static const GInterfaceInfo cell_layout_info =
        {
          (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
          NULL,
          NULL
        };

455 456 457 458 459 460 461
      static const GInterfaceInfo cell_editable_info =
	{
	  (GInterfaceInitFunc) gtk_combo_box_cell_editable_init,
	  NULL,
	  NULL
       };

462 463 464 465
      combo_box_type = g_type_register_static (GTK_TYPE_BIN,
                                               "GtkComboBox",
                                               &combo_box_info,
                                               0);
466 467 468 469

      g_type_add_interface_static (combo_box_type,
                                   GTK_TYPE_CELL_LAYOUT,
                                   &cell_layout_info);
470 471 472 473 474 475 476


      g_type_add_interface_static (combo_box_type,
				   GTK_TYPE_CELL_EDITABLE,
				   &cell_editable_info);
      

477 478 479 480 481 482 483 484 485 486 487
    }

  return combo_box_type;
}

/* common */
static void
gtk_combo_box_class_init (GtkComboBoxClass *klass)
{
  GObjectClass *object_class;
  GtkBindingSet *binding_set;
488
  GtkObjectClass *gtk_object_class;
489 490 491 492 493 494 495 496
  GtkContainerClass *container_class;
  GtkWidgetClass *widget_class;

  binding_set = gtk_binding_set_by_class (klass);

  container_class = (GtkContainerClass *)klass;
  container_class->forall = gtk_combo_box_forall;
  container_class->add = gtk_combo_box_add;
497
  container_class->remove = gtk_combo_box_remove;
498 499 500 501 502

  widget_class = (GtkWidgetClass *)klass;
  widget_class->size_allocate = gtk_combo_box_size_allocate;
  widget_class->size_request = gtk_combo_box_size_request;
  widget_class->expose_event = gtk_combo_box_expose_event;
503
  widget_class->scroll_event = gtk_combo_box_scroll_event;
504
  widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
505
  widget_class->style_set = gtk_combo_box_style_set;
506
  widget_class->state_changed = gtk_combo_box_state_changed;
507

508 509 510
  gtk_object_class = (GtkObjectClass *)klass;
  gtk_object_class->destroy = gtk_combo_box_destroy;

511
  object_class = (GObjectClass *)klass;
512
  object_class->finalize = gtk_combo_box_finalize;
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
  object_class->set_property = gtk_combo_box_set_property;
  object_class->get_property = gtk_combo_box_get_property;

  parent_class = g_type_class_peek_parent (klass);

  /* signals */
  combo_box_signals[CHANGED] =
    g_signal_new ("changed",
                  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);

  /* properties */
  g_object_class_install_property (object_class,
                                   PROP_MODEL,
                                   g_param_spec_object ("model",
532 533
                                                        P_("ComboBox model"),
                                                        P_("The model for the combo box"),
534
                                                        GTK_TYPE_TREE_MODEL,
535
                                                        G_PARAM_READWRITE));
536 537 538 539

  g_object_class_install_property (object_class,
                                   PROP_WRAP_WIDTH,
                                   g_param_spec_int ("wrap_width",
540 541
                                                     P_("Wrap width"),
                                                     P_("Wrap width for layouting the items in a grid"),
542 543 544 545 546 547 548 549
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_ROW_SPAN_COLUMN,
                                   g_param_spec_int ("row_span_column",
550 551
                                                     P_("Row span column"),
                                                     P_("TreeModel column containing the row span values"),
552
                                                     -1,
553
                                                     G_MAXINT,
554
                                                     -1,
555 556 557 558 559
                                                     G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_COLUMN_SPAN_COLUMN,
                                   g_param_spec_int ("column_span_column",
560 561
                                                     P_("Column span column"),
                                                     P_("TreeModel column containing the column span values"),
562
                                                     -1,
563
                                                     G_MAXINT,
564 565 566
                                                     -1,
                                                     G_PARAM_READWRITE));

567 568 569 570

  g_object_class_install_property (object_class,
                                   PROP_ACTIVE,
                                   g_param_spec_int ("active",
571 572
                                                     P_("Active item"),
                                                     P_("The item which is currently active"),
573
                                                     -1,
574
                                                     G_MAXINT,
575
                                                     -1,
576 577
                                                     G_PARAM_READWRITE));

578 579 580 581 582 583 584 585 586 587 588 589 590 591
  /**
   * GtkComboBox:add-tearoffs:
   *
   * The "add-tearoffs" property controls whether generated menus 
   * have tearoff menu items. 
   *
   * Note that this only affects menu style combo boxes.
   *
   * Since: 2.6
   */
  g_object_class_install_property (object_class,
                                   PROP_ADD_TEAROFFS,
				   g_param_spec_boolean ("add-tearoffs",
							 P_("Add tearoffs to menus"),
592
							 P_("Whether dropdowns should have a tearoff menu item"),
593 594 595
							 FALSE,
							 G_PARAM_READWRITE));
  
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
  /**
   * GtkComboBox:has-frame:
   *
   * The :has-frame property controls whether a frame
   * is drawn around the entry.
   *
   * Since: 2.6
   */
  g_object_class_install_property (object_class,
                                   PROP_HAS_FRAME,
				   g_param_spec_boolean ("has-frame",
							 P_("Has Frame"),
							 P_("Whether the combo box draws a frame around the child"),
							 TRUE,
							 G_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
612 613 614 615 616 617 618 619
  g_object_class_install_property (object_class,
                                   PROP_FOCUS_ON_CLICK,
                                   g_param_spec_boolean ("focus_on_click",
							 P_("Focus on click"),
							 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
							 TRUE,
							 G_PARAM_READWRITE));

620
  gtk_widget_class_install_style_property (widget_class,
621 622
                                           g_param_spec_boolean ("appears-as-list",
                                                                 P_("Appears as list"),
623
                                                                 P_("Whether dropdowns should look like lists rather than menus"),
624
                                                                 FALSE,
625
                                                                 G_PARAM_READABLE));
626 627 628 629

  g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
}

630 631 632 633 634 635 636 637 638
static void
gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
{
  iface->pack_start = gtk_combo_box_cell_layout_pack_start;
  iface->pack_end = gtk_combo_box_cell_layout_pack_end;
  iface->clear = gtk_combo_box_cell_layout_clear;
  iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
  iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
  iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
Kristian Rietveld's avatar
Kristian Rietveld committed
639
  iface->reorder = gtk_combo_box_cell_layout_reorder;
640 641
}

642 643 644 645 646 647
static void
gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
{
  iface->start_editing = gtk_combo_box_start_editing;
}

648 649 650 651 652 653
static void
gtk_combo_box_init (GtkComboBox *combo_box)
{
  combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);

  combo_box->priv->cell_view = gtk_cell_view_new ();
654 655
  gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
  GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
656
  gtk_widget_show (combo_box->priv->cell_view);
657 658 659 660

  combo_box->priv->width = 0;
  combo_box->priv->wrap_width = 0;

661
  combo_box->priv->active_row = NULL;
662 663
  combo_box->priv->col_column = -1;
  combo_box->priv->row_column = -1;
664 665 666 667 668

  combo_box->priv->add_tearoffs = FALSE;
  combo_box->priv->has_frame = TRUE;
  combo_box->priv->is_cell_renderer = FALSE;
  combo_box->priv->editing_canceled = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
669 670
  combo_box->priv->auto_scroll = FALSE;
  combo_box->priv->focus_on_click = TRUE;
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
}

static void
gtk_combo_box_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (object);

  switch (prop_id)
    {
      case PROP_MODEL:
        gtk_combo_box_set_model (combo_box, g_value_get_object (value));
        break;

      case PROP_WRAP_WIDTH:
        gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
        break;

      case PROP_ROW_SPAN_COLUMN:
        gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
        break;

      case PROP_COLUMN_SPAN_COLUMN:
        gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
        break;

      case PROP_ACTIVE:
        gtk_combo_box_set_active (combo_box, g_value_get_int (value));
        break;

703 704 705 706
      case PROP_ADD_TEAROFFS:
        gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
        break;

707 708 709 710
      case PROP_HAS_FRAME:
        combo_box->priv->has_frame = g_value_get_boolean (value);
        break;

Matthias Clasen's avatar
Matthias Clasen committed
711 712 713 714
      case PROP_FOCUS_ON_CLICK:
        combo_box->priv->focus_on_click = g_value_get_boolean (value);
        break;

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
      default:
        break;
    }
}

static void
gtk_combo_box_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (object);

  switch (prop_id)
    {
      case PROP_MODEL:
        g_value_set_object (value, combo_box->priv->model);
        break;

      case PROP_WRAP_WIDTH:
        g_value_set_int (value, combo_box->priv->wrap_width);
        break;

      case PROP_ROW_SPAN_COLUMN:
        g_value_set_int (value, combo_box->priv->row_column);
        break;

      case PROP_COLUMN_SPAN_COLUMN:
        g_value_set_int (value, combo_box->priv->col_column);
        break;

      case PROP_ACTIVE:
        g_value_set_int (value, gtk_combo_box_get_active (combo_box));
        break;

750 751 752 753
      case PROP_ADD_TEAROFFS:
        g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
        break;

754 755 756 757
      case PROP_HAS_FRAME:
        g_value_set_boolean (value, combo_box->priv->has_frame);
        break;

Matthias Clasen's avatar
Matthias Clasen committed
758 759 760 761
      case PROP_FOCUS_ON_CLICK:
        g_value_set_boolean (value, combo_box->priv->focus_on_click);
        break;

762 763 764 765 766 767
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
static void
gtk_combo_box_state_changed (GtkWidget    *widget,
			     GtkStateType  previous)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);

  if (GTK_WIDGET_REALIZED (widget))
    {
      if (combo_box->priv->tree_view && combo_box->priv->cell_view)
	gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
					    &widget->style->base[GTK_WIDGET_STATE (widget)]);
    }

  gtk_widget_queue_draw (widget);
}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
static void
gtk_combo_box_button_state_changed (GtkWidget    *widget,
				    GtkStateType  previous,
				    gpointer      data)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (data);

  if (GTK_WIDGET_REALIZED (widget))
    {
      if (!combo_box->priv->tree_view && combo_box->priv->cell_view)
	gtk_widget_set_state (combo_box->priv->cell_view, 
			      GTK_WIDGET_STATE (widget));
    }

  gtk_widget_queue_draw (widget);
}

801
static void
802
gtk_combo_box_check_appearance (GtkComboBox *combo_box)
803
{
804
  gboolean appears_as_list;
805 806 807 808 809

  /* if wrap_width > 0, then we are in grid-mode and forced to use
   * unix style
   */
  if (combo_box->priv->wrap_width)
810 811 812 813 814
    appears_as_list = FALSE;
  else
    gtk_widget_style_get (GTK_WIDGET (combo_box),
			  "appears-as-list", &appears_as_list,
			  NULL);
815

816
  if (appears_as_list)
817
    {
818
      /* Destroy all the menu mode widgets, if they exist. */
819
      if (GTK_IS_MENU (combo_box->priv->popup_widget))
820 821 822 823 824
	gtk_combo_box_menu_destroy (combo_box);

      /* Create the list mode widgets, if they don't already exist. */
      if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
	gtk_combo_box_list_setup (combo_box);
825 826 827
    }
  else
    {
828
      /* Destroy all the list mode widgets, if they exist. */
829
      if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
830 831 832 833 834
	gtk_combo_box_list_destroy (combo_box);

      /* Create the menu mode widgets, if they don't already exist. */
      if (!GTK_IS_MENU (combo_box->priv->popup_widget))
	gtk_combo_box_menu_setup (combo_box, TRUE);
835
    }
836 837 838 839 840 841 842 843 844
}

static void
gtk_combo_box_style_set (GtkWidget *widget,
                         GtkStyle  *previous)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);

  gtk_combo_box_check_appearance (combo_box);
845 846 847 848

  if (combo_box->priv->tree_view && combo_box->priv->cell_view)
    gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
					&widget->style->base[GTK_WIDGET_STATE (widget)]);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
}

static void
gtk_combo_box_button_toggled (GtkWidget *widget,
                              gpointer   data)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (data);

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    {
      if (!combo_box->priv->popup_in_progress)
        gtk_combo_box_popup (combo_box);
    }
  else
    gtk_combo_box_popdown (combo_box);
}

static void
gtk_combo_box_add (GtkContainer *container,
                   GtkWidget    *widget)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (container);

  if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
873 874 875 876 877 878 879 880
    {
      gtk_widget_unparent (combo_box->priv->cell_view);
      GTK_BIN (container)->child = NULL;
      gtk_widget_queue_resize (GTK_WIDGET (container));
    }
  
  gtk_widget_set_parent (widget, GTK_WIDGET (container));
  GTK_BIN (container)->child = widget;
881 882 883 884 885 886 887 888 889

  if (combo_box->priv->cell_view &&
      widget != combo_box->priv->cell_view)
    {
      /* since the cell_view was unparented, it's gone now */
      combo_box->priv->cell_view = NULL;

      if (!combo_box->priv->tree_view && combo_box->priv->separator)
        {
890 891
	  gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
				combo_box->priv->separator);
892
	  combo_box->priv->separator = NULL;
893 894 895 896 897 898 899

          gtk_widget_queue_resize (GTK_WIDGET (container));
        }
      else if (combo_box->priv->cell_view_frame)
        {
          gtk_widget_unparent (combo_box->priv->cell_view_frame);
          combo_box->priv->cell_view_frame = NULL;
900
          combo_box->priv->box = NULL;
901 902 903 904
        }
    }
}

905 906 907 908 909
static void
gtk_combo_box_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (container);
910
  GtkTreePath *path;
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
  gboolean appears_as_list;

  gtk_widget_unparent (widget);
  GTK_BIN (container)->child = NULL;

  if (combo_box->priv->destroying)
    return;

  gtk_widget_queue_resize (GTK_WIDGET (container));

  if (!combo_box->priv->tree_view)
    appears_as_list = FALSE;
  else
    appears_as_list = TRUE;
  
  if (appears_as_list)
    gtk_combo_box_list_destroy (combo_box);
  else if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
      gtk_combo_box_menu_destroy (combo_box);
      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
      combo_box->priv->popup_widget = NULL;
    }

  if (!combo_box->priv->cell_view)
    {
      combo_box->priv->cell_view = gtk_cell_view_new ();
      gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
      GTK_BIN (container)->child = combo_box->priv->cell_view;
      
      gtk_widget_show (combo_box->priv->cell_view);
      gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
			       combo_box->priv->model);
944
      gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (combo_box->priv->cell_view));
945 946 947 948
    }


  if (appears_as_list)
949
    gtk_combo_box_list_setup (combo_box); 
950 951 952
  else
    gtk_combo_box_menu_setup (combo_box, TRUE);

953 954 955 956 957 958 959 960
  if (combo_box->priv->active_row)
    {
      path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
      gtk_combo_box_set_active_internal (combo_box, path);
      gtk_tree_path_free (path);
    }
  else
    gtk_combo_box_set_active_internal (combo_box, NULL);
961 962
}

963 964 965 966 967 968 969 970 971 972
static ComboCellInfo *
gtk_combo_box_get_cell_info (GtkComboBox     *combo_box,
                             GtkCellRenderer *cell)
{
  GSList *i;

  for (i = combo_box->priv->cells; i; i = i->next)
    {
      ComboCellInfo *info = (ComboCellInfo *)i->data;

973
      if (info && info->cell == cell)
974 975 976 977 978 979 980 981 982 983 984 985
        return info;
    }

  return NULL;
}

static void
gtk_combo_box_menu_show (GtkWidget *menu,
                         gpointer   user_data)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);

986
  combo_box->priv->popup_in_progress = TRUE;
987 988
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                TRUE);
989
  combo_box->priv->popup_in_progress = FALSE;
990 991 992 993 994 995 996 997 998 999 1000 1001
}

static void
gtk_combo_box_menu_hide (GtkWidget *menu,
                         gpointer   user_data)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                FALSE);
}

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
static void
gtk_combo_box_detacher (GtkWidget *widget,
			GtkMenu	  *menu)
{
  GtkComboBox *combo_box;

  g_return_if_fail (GTK_IS_COMBO_BOX (widget));

  combo_box = GTK_COMBO_BOX (widget);
  g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);

1013
  g_signal_handlers_disconnect_by_func (menu->toplevel,
1014 1015
					gtk_combo_box_menu_show,
					combo_box);
1016
  g_signal_handlers_disconnect_by_func (menu->toplevel,
1017 1018 1019 1020 1021 1022
					gtk_combo_box_menu_hide,
					combo_box);
  
  combo_box->priv->popup_widget = NULL;
}

1023 1024 1025 1026 1027
static void
gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
                                GtkWidget   *popup)
{
  if (GTK_IS_MENU (combo_box->priv->popup_widget))
1028 1029 1030 1031
    {
      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
      combo_box->priv->popup_widget = NULL;
    }
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  else if (combo_box->priv->popup_widget)
    {
      gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
                            combo_box->priv->popup_widget);
      g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
      combo_box->priv->popup_widget = NULL;
    }

  if (GTK_IS_MENU (popup))
    {
      if (combo_box->priv->popup_window)
        {
          gtk_widget_destroy (combo_box->priv->popup_window);
1045 1046
          combo_box->priv->popup_window = NULL;
	  combo_box->priv->popup_frame = NULL;
1047 1048 1049 1050
        }

      combo_box->priv->popup_widget = popup;

1051 1052 1053 1054 1055 1056
      /* 
       * Note that we connect to show/hide on the toplevel, not the
       * menu itself, since the menu is not shown/hidden when it is
       * popped up while torn-off.
       */
      g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1057
                        G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1058
      g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1059 1060
                        G_CALLBACK (gtk_combo_box_menu_hide), combo_box);

1061 1062 1063
      gtk_menu_attach_to_widget (GTK_MENU (popup),
				 GTK_WIDGET (combo_box),
				 gtk_combo_box_detacher);
1064 1065 1066 1067 1068 1069
    }
  else
    {
      if (!combo_box->priv->popup_window)
        {
          combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1070
	  gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1071 1072 1073
          gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
                                 gtk_widget_get_screen (GTK_WIDGET (combo_box)));

1074 1075
          combo_box->priv->popup_frame = gtk_frame_new (NULL);
          gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
1076
                                     GTK_SHADOW_ETCHED_IN);
1077 1078
          gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
                             combo_box->priv->popup_frame);
1079

1080
          gtk_widget_show (combo_box->priv->popup_frame);
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

	  combo_box->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	  
	  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
					  GTK_POLICY_NEVER,
					  GTK_POLICY_NEVER);
	  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
					       GTK_SHADOW_NONE);

          gtk_widget_show (combo_box->priv->scrolled_window);
	  
	  gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
			     combo_box->priv->scrolled_window);
1094 1095
        }

1096
      gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1097
                         popup);
1098

1099 1100 1101 1102 1103 1104 1105
      gtk_widget_show (popup);
      g_object_ref (G_OBJECT (popup));
      combo_box->priv->popup_widget = popup;
    }
}

static void
1106 1107 1108 1109 1110
gtk_combo_box_menu_position_below (GtkMenu  *menu,
				   gint     *x,
				   gint     *y,
				   gint     *push_in,
				   gpointer  user_data)
1111
{
1112
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1113 1114 1115
  gint sx, sy;
  GtkWidget *child;
  GtkRequisition req;
1116 1117 1118
  GdkScreen *screen;
  gint monitor_num;
  GdkRectangle monitor;
1119
  
1120
  /* FIXME: is using the size request here broken? */
1121 1122 1123 1124
   child = GTK_BIN (combo_box)->child;
   
   gdk_window_get_origin (child->window, &sx, &sy);
   
1125 1126 1127 1128 1129 1130
   if (GTK_WIDGET_NO_WINDOW (child))
      {
	sx += child->allocation.x;
	sy += child->allocation.y;
      }

1131
   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1132

1133
   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1134 1135 1136
     *x = sx;
   else
     *x = sx + child->allocation.width - req.width;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
   *y = sy;

  screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
  monitor_num = gdk_screen_get_monitor_at_window (screen, 
						  GTK_WIDGET (combo_box)->window);
  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
  
  if (*x < monitor.x)
    *x = monitor.x;
  else if (*x + req.width > monitor.x + monitor.width)
    *x = monitor.x + monitor.width - req.width;
  
1149 1150 1151 1152 1153
  if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
    *y += child->allocation.height;
  else if (*y - monitor.y >= req.height)
    *y -= req.height;
  else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y) 
1154 1155 1156 1157
    *y += child->allocation.height;
  else
    *y -= req.height;

1158
   *push_in = FALSE;
1159
}
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
static void
gtk_combo_box_menu_position_over (GtkMenu  *menu,
				  gint     *x,
				  gint     *y,
				  gboolean *push_in,
				  gpointer  user_data)
{
  GtkComboBox *combo_box;
  GtkWidget *active;
  GtkWidget *child;
  GtkWidget *widget;
  GtkRequisition requisition;
  GList *children;
  gint screen_width;
  gint menu_xpos;
  gint menu_ypos;
  gint menu_width;

  g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
  
  combo_box = GTK_COMBO_BOX (user_data);
  widget = GTK_WIDGET (combo_box);
1183

1184 1185
  gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
  menu_width = requisition.width;
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
  active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
  gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);

  menu_xpos += widget->allocation.x;
  menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;

  if (active != NULL)
    {
      gtk_widget_get_child_requisition (active, &requisition);
      menu_ypos -= requisition.height / 2;
    }
1198

1199 1200
  children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
  while (children)
1201
    {
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
      child = children->data;

      if (active == child)
	break;

      if (GTK_WIDGET_VISIBLE (child))
	{
	  gtk_widget_get_child_requisition (child, &requisition);
	  menu_ypos -= requisition.height;
	}

      children = children->next;
1214 1215
    }

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
    menu_xpos = menu_xpos + widget->allocation.width - menu_width;

  /* Clamp the position on screen */
  screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
  
  if (menu_xpos < 0)
    menu_xpos = 0;
  else if ((menu_xpos + menu_width) > screen_width)
    menu_xpos -= ((menu_xpos + menu_width) - screen_width);

  *x = menu_xpos;
  *y = menu_ypos;

1230 1231 1232
  *push_in = TRUE;
}

1233 1234 1235 1236 1237 1238 1239 1240
static void
gtk_combo_box_menu_position (GtkMenu  *menu,
			     gint     *x,
			     gint     *y,
			     gint     *push_in,
			     gpointer  user_data)
{
  GtkComboBox *combo_box;
1241
  GtkWidget *menu_item;
1242 1243 1244 1245 1246 1247

  combo_box = GTK_COMBO_BOX (user_data);

  if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)	
    gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
  else
1248
    {
1249
      /* FIXME handle nested menus better */
1250 1251 1252 1253 1254 1255 1256
      menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
      if (menu_item)
	gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget), 
				    menu_item);

      gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
    }
1257 1258 1259

}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
static void
gtk_combo_box_list_position (GtkComboBox *combo_box, 
			     gint        *x, 
			     gint        *y, 
			     gint        *width,
			     gint        *height)
{
  GtkWidget *sample;
  GdkScreen *screen;
  gint monitor_num;
  GdkRectangle monitor;
1271
  GtkRequisition popup_req;
1272
  GtkPolicyType hpolicy, vpolicy;
1273 1274 1275 1276 1277
  
  sample = GTK_BIN (combo_box)->child;

  gdk_window_get_origin (sample->window, x, y);

1278 1279 1280 1281 1282 1283 1284 1285
  if (GTK_WIDGET_NO_WINDOW (sample))
    {
      *x += sample->allocation.x;
      *y += sample->allocation.y;
    }
  
  *width = sample->allocation.width;
  
1286
  if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1287 1288 1289 1290 1291 1292 1293
    {
       *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
	     GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
       *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
            GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
    }

1294 1295 1296 1297 1298 1299
  hpolicy = vpolicy = GTK_POLICY_NEVER;
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
				  hpolicy, vpolicy);
  gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);

  if (popup_req.width > *width)
1300
    {
1301 1302 1303 1304
      hpolicy = GTK_POLICY_ALWAYS;
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
				      hpolicy, vpolicy);
      gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1305
    }
1306 1307 1308

  *height = popup_req.height;

1309 1310 1311 1312
  screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
  monitor_num = gdk_screen_get_monitor_at_window (screen, 
						  GTK_WIDGET (combo_box)->window);
  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1313

1314 1315 1316 1317
  if (*x < monitor.x)
    *x = monitor.x;
  else if (*x + *width > monitor.x + monitor.width)
    *x = monitor.x + monitor.width - *width;
1318 1319 1320
  
  if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
    *y += sample->allocation.height;
1321
  else if (*y - *height >= monitor.y)
1322
    *y -= *height;
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
  else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
    {
      *y += sample->allocation.height;
      *height = monitor.y + monitor.height - *y;
    }
  else 
    {
      *height = *y - monitor.y;
      *y = monitor.y;
    }

  if (popup_req.height > *height)
    {
      vpolicy = GTK_POLICY_ALWAYS;
      
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
				      hpolicy, vpolicy);
    }
1341 1342
} 

1343
static gboolean
1344
cell_view_is_sensitive (GtkCellView *cell_view)
1345 1346 1347
{
  GList *cells, *list;
  gboolean sensitive;
1348
  
1349 1350
  gtk_cell_view_set_cell_data (cell_view);
  cells = gtk_cell_view_get_cell_renderers (cell_view);
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

  sensitive = FALSE;
  list = cells;
  while (list)
    {
      g_object_get (G_OBJECT (list->data), "sensitive", &sensitive, NULL);
      
      if (sensitive)
	break;

      list = list->next;
    }
  g_list_free (cells);

  return sensitive;
}

static gboolean
tree_column_row_is_sensitive (GtkComboBox *combo_box,
			      GtkTreeIter *iter)
{
  GList *cells, *list;
  gboolean sensitive;

  if (!combo_box->priv->column)
    return TRUE;

1378
  if (combo_box->priv->row_separator_func)
1379
    {
1380 1381
      if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
						  combo_box->priv->row_separator_data))
1382 1383 1384
	return FALSE;
    }

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
  gtk_tree_view_column_cell_set_cell_data (combo_box->priv->column,
					   combo_box->priv->model,
					   iter, FALSE, FALSE);

  cells = gtk_tree_view_column_get_cell_renderers (combo_box->priv->column);

  sensitive = FALSE;
  list = cells;
  while (list)
    {
      g_object_get (G_OBJECT (list->data), "sensitive", &sensitive, NULL);
      
      if (sensitive)
	break;

      list = list->next;
    }
  g_list_free (cells);

  return sensitive;
}
1406 1407 1408 1409

static void
update_menu_sensitivity (GtkComboBox *combo_box,
			 GtkWidget   *menu)
1410
{
1411 1412 1413
  GList *children, *child;
  GtkWidget *item, *submenu, *separator;
  GtkWidget *cell_view;
1414 1415 1416
  gboolean sensitive;

  if (!combo_box->priv->model)
1417
    return;
1418

1419
  children = gtk_container_get_children (GTK_CONTAINER (menu));
1420

1421
  for (child = children; child; child = child->next)
1422
    {
1423 1424
      item = GTK_WIDGET (child->data);
      cell_view = GTK_BIN (item)->child;
1425

1426 1427 1428 1429 1430 1431 1432 1433 1434
      if (!GTK_IS_CELL_VIEW (cell_view))
	continue;
      
      submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
      if (submenu != NULL)
	{
	  gtk_widget_set_sensitive (item, TRUE);
	  update_menu_sensitivity (combo_box, submenu);
	}
1435
      else
1436 1437
	{
	  sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1438

1439 1440 1441 1442 1443 1444 1445 1446 1447
	  if (menu != combo_box->priv->popup_widget && child == children)
	    {
	      separator = GTK_WIDGET (child->next->data);
	      g_object_set (G_OBJECT (item), "visible", sensitive, NULL);
	      g_object_set (G_OBJECT (separator), "visible", sensitive, NULL);
	    }
	  else
	    gtk_widget_set_sensitive (item, sensitive);
	}
1448 1449
    }

1450
  g_list_free (children);
1451 1452
}

1453 1454 1455 1456
static void 
gtk_combo_box_menu_popup (GtkComboBox *combo_box,
			  guint        button, 
			  guint32      activate_time)
1457
{
1458 1459 1460 1461 1462 1463
  GtkTreePath *path;
  gint active_item;
  GtkRequisition requisition;
  gint width;
  
  update_menu_sensitivity (combo_box, combo_box->priv->popup_widget);
1464

1465 1466
  active_item = -1;
  if (combo_box->priv->active_row)
1467
    {
1468 1469 1470 1471 1472
      path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
      if (path)
	{
	  active_item = gtk_tree_path_get_indices (path)[0];
	  gtk_tree_path_free (path);
1473

1474 1475 1476 1477
	  if (combo_box->priv->add_tearoffs)
	    active_item++;
	}
    }
1478

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
  /* FIXME handle nested menus better */
  gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), active_item);
  
  if (combo_box->priv->wrap_width == 0)
    {
      width = GTK_WIDGET (combo_box)->allocation.width;
      gtk_widget_size_request (combo_box->priv->popup_widget, &requisition);
      
      gtk_widget_set_size_request (combo_box->priv->popup_widget,
				   MAX (width, requisition.width), -1);
1489
    }
1490 1491 1492 1493 1494
  
  gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
		  NULL, NULL,
		  gtk_combo_box_menu_position, combo_box,
		  button, activate_time);
1495 1496
}

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
/**
 * gtk_combo_box_popup:
 * @combo_box: a #GtkComboBox
 * 
 * Pops up the menu or dropdown list of @combo_box. 
 *
 * This function is mostly intended for use by accessibility technologies;
 * applications should have little use for it.
 *
 * Since: 2.4
 **/
void
1509 1510
gtk_combo_box_popup (GtkComboBox *combo_box)
{
1511
  gint x, y, width, height;
1512
  GtkTreePath *path, *ppath;
1513
  
1514 1515
  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));

1516 1517 1518 1519 1520
  if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
    return;

  if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
1521
      gtk_combo_box_menu_popup (combo_box, 0, 0);
1522 1523 1524
      return;
    }

1525
  gtk_widget_show_all (combo_box->priv->popup_frame);
1526
  gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1527
  
1528
  gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);  
1529
  gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1530

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
  if (combo_box->priv->active_row)
    {
      path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
      ppath = gtk_tree_path_copy (path);
      if (gtk_tree_path_up (ppath))
	gtk_tree_view_expand_to_path (GTK_TREE_VIEW (combo_box->priv->tree_view),
				      ppath);
      gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
				path, NULL, FALSE);
      gtk_tree_path_free (path);
      gtk_tree_path_free (ppath);
    }
  gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
				  TRUE);
  
1546
  /* popup */
1547
  gtk_widget_show (combo_box->priv->popup_window);
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558

  gtk_widget_grab_focus (combo_box->priv->popup_window);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                TRUE);

  if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
    {
      gdk_keyboard_grab (combo_box->priv->popup_window->window,
                         FALSE, GDK_CURRENT_TIME);
      gtk_widget_grab_focus (combo_box->priv->tree_view);
    }
1559 1560 1561 1562 1563 1564 1565

  gtk_grab_add (combo_box->priv->popup_window);
  gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
                    GDK_BUTTON_PRESS_MASK |
                    GDK_BUTTON_RELEASE_MASK |
                    GDK_POINTER_MOTION_MASK,
                    NULL, NULL, GDK_CURRENT_TIME);
1566 1567
}

1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
/**
 * gtk_combo_box_popdown:
 * @combo_box: a #GtkComboBox
 * 
 * Hides the menu or dropdown list of @combo_box.
 *
 * This function is mostly intended for use by accessibility technologies;
 * applications should have little use for it.
 *
 * Since: 2.4
 **/
void
1580 1581
gtk_combo_box_popdown (GtkComboBox *combo_box)
{
1582 1583
  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));

1584
  if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1585 1586
    return;

1587 1588 1589 1590 1591 1592
  if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
      gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
      return;
    }

1593
  gtk_combo_box_list_remove_grabs (combo_box);
1594

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
  gtk_widget_hide_all (combo_box->priv->popup_window);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                FALSE);
}

static gint
gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
                                    GtkTreePath *path)
{
  gint padding;
  GtkRequisition req;

1607 1608 1609 1610 1611 1612
  if (combo_box->priv->cell_view)
    gtk_widget_style_get (combo_box->priv->cell_view,
                          "focus-line-width", &padding,
                          NULL);
  else
    padding = 0;
1613 1614 1615 1616

  /* add some pixels for good measure */
  padding += BONUS_PADDING;

1617 1618 1619 1620 1621
  if (combo_box->priv->cell_view)
    gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
                                   path, &req);
  else
    req.width = 0;
1622 1623 1624 1625 1626

  return req.width + padding;
}

static void
1627
gtk_combo_box_remeasure (GtkComboBox *combo_box)
1628 1629 1630 1631
{
  GtkTreeIter iter;
  GtkTreePath *path;

1632 1633
  if (!combo_box->priv->model ||
      !gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1634 1635
    return;

1636 1637
  combo_box->priv->width = 0;

1638 1639 1640 1641 1642 1643
  path = gtk_tree_path_new_from_indices (0, -1);

  do
    {
      GtkRequisition req;

1644
      if (combo_box->priv->cell_view)
1645
	gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
1646 1647 1648
                                       path, &req);
      else
        req.width = 0;
1649

1650
      combo_box->priv->width = MAX (combo_box->priv->width, req.width);
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663

      gtk_tree_path_next (path);
    }
  while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));

  gtk_tree_path_free (path);
}

static void
gtk_combo_box_size_request (GtkWidget      *widget,
                            GtkRequisition *requisition)
{
  gint width, height;
1664
  gint focus_width, focus_pad;
1665 1666 1667 1668
  GtkRequisition bin_req;

  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);