gtkcombobox.c 131 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>
Kristian Rietveld's avatar
Kristian Rietveld committed
21
#include "gtkcombobox.h"
22 23 24

#include "gtkarrow.h"
#include "gtkbindings.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
25
#include "gtkcelllayout.h"
26
#include "gtkcellrenderertext.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
27
#include "gtkcellview.h"
28
#include "gtkeventbox.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
29
#include "gtkframe.h"
30
#include "gtkhbox.h"
31 32 33
#include "gtkliststore.h"
#include "gtkmain.h"
#include "gtkmenu.h"
34
#include "gtkscrolledwindow.h"
35
#include "gtkseparatormenuitem.h"
36
#include "gtktearoffmenuitem.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
37
#include "gtktogglebutton.h"
38
#include "gtktreeselection.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
39 40
#include "gtkvseparator.h"
#include "gtkwindow.h"
41 42 43 44 45

#include <gdk/gdkkeysyms.h>

#include <gobject/gvaluecollector.h>

Kristian Rietveld's avatar
Kristian Rietveld committed
46
#include <string.h>
47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <stdarg.h>

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


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

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

61 62 63 64
  GtkCellLayoutDataFunc func;
  gpointer func_data;
  GDestroyNotify destroy;

65 66 67 68
  guint expand : 1;
  guint pack : 1;
};

69 70
#define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))

71 72 73 74 75 76
struct _GtkComboBoxPrivate
{
  GtkTreeModel *model;

  gint col_column;
  gint row_column;
77
  gint separator_column;
78 79 80 81 82 83 84 85 86 87 88 89

  gint wrap_width;

  gint active_item;

  GtkWidget *tree_view;
  GtkTreeViewColumn *column;

  GtkWidget *cell_view;
  GtkWidget *cell_view_frame;

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

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

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

  gint width;
  GSList *cells;

  guint popup_in_progress : 1;
110
  guint destroying : 1;
111
  guint add_tearoffs : 1;
112 113 114
  guint has_frame : 1;
  guint is_cell_renderer : 1;
  guint editing_canceled : 1;
115
  guint auto_scroll : 1;
116 117
};

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/* 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
133
 * scrolled_window -> NULL
134 135 136 137 138 139 140 141 142 143 144 145
 *
 * 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
146
 * scrolled_window -> NULL
147 148 149 150 151 152 153 154 155 156 157 158
 *
 * 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
159
 * scrolled_window -> GtkScrolledWindow, child of popup_frame
160 161 162 163 164 165 166 167 168 169 170 171
 *
 * 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
172
 * scrolled_window -> GtkScrolledWindow, child of popup_frame
173 174 175
 * 
 */

176 177 178 179 180 181 182 183 184 185 186
enum {
  CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_MODEL,
  PROP_WRAP_WIDTH,
  PROP_ROW_SPAN_COLUMN,
  PROP_COLUMN_SPAN_COLUMN,
187
  PROP_ROW_SEPARATOR_COLUMN,
188
  PROP_ACTIVE,
189 190
  PROP_ADD_TEAROFFS,
  PROP_HAS_FRAME
191 192 193 194 195 196
};

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

#define BONUS_PADDING 4
197
#define SCROLL_TIME  100
198 199 200

/* common */
static void     gtk_combo_box_class_init           (GtkComboBoxClass *klass);
201
static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
202
static void     gtk_combo_box_cell_editable_init   (GtkCellEditableIface *iface);
203
static void     gtk_combo_box_init                 (GtkComboBox      *combo_box);
204 205
static void     gtk_combo_box_finalize             (GObject          *object);
static void     gtk_combo_box_destroy              (GtkObject        *object);
206 207 208 209 210 211 212 213 214 215

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

216 217
static void     gtk_combo_box_state_changed        (GtkWidget        *widget,
			                            GtkStateType      previous);
218
static void     gtk_combo_box_style_set            (GtkWidget       *widget,
219
                                                    GtkStyle        *previous);
220 221 222 223
static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
                                                    gpointer         data);
static void     gtk_combo_box_add                  (GtkContainer    *container,
                                                    GtkWidget       *widget);
224 225
static void     gtk_combo_box_remove               (GtkContainer    *container,
                                                    GtkWidget       *widget);
226 227 228 229 230 231 232 233 234 235 236

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);
237 238 239 240 241 242 243 244 245 246
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);
247 248 249 250 251 252 253 254
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);
255
static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
256

257 258
static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);

259 260 261 262 263 264 265 266 267 268
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);
269 270
static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
                                                    GdkEventScroll   *event);
271 272
static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
						    gint              index);
273 274 275
static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
276

277 278
static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/* 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);

297
/* list */
298 299 300 301 302
static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
						    gint             *x, 
						    gint             *y, 
						    gint             *width,
						    gint             *height);
303 304 305
static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);

306 307
static void     gtk_combo_box_list_remove_grabs    (GtkComboBox      *combo_box);

308 309 310 311 312 313
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);
314 315 316 317 318 319 320
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);
321 322 323 324 325 326 327 328 329 330 331
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);

/* menu */
static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
332
                                                    gboolean          add_children);
333
static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);

static void     gtk_combo_box_item_get_size        (GtkComboBox      *combo_box,
                                                    gint              index,
                                                    gint             *cols,
                                                    gint             *rows);
static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
                                                    gint              index);
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);
356 357 358 359 360
static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gint             *new_order,
						    gpointer          user_data);
361 362 363 364
static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
365 366 367
static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
368 369 370
static void     gtk_combo_box_menu_state_changed   (GtkWidget        *widget,
			                            GtkStateType      previous,
						    gpointer          data);
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/* 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
391 392 393
static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              gint                   position);
394 395
static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
							      gboolean      group_cycling);
396

397 398
static void     cell_view_sync_cells (GtkComboBox *combo_box,
                                      GtkCellView *cell_view);
399

400 401 402 403 404
/* GtkCellEditable method implementations */
static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
					 GdkEvent        *event);


405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
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
        };

425 426 427 428 429 430 431
      static const GInterfaceInfo cell_layout_info =
        {
          (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
          NULL,
          NULL
        };

432 433 434 435 436 437 438
      static const GInterfaceInfo cell_editable_info =
	{
	  (GInterfaceInitFunc) gtk_combo_box_cell_editable_init,
	  NULL,
	  NULL
       };

439 440 441 442
      combo_box_type = g_type_register_static (GTK_TYPE_BIN,
                                               "GtkComboBox",
                                               &combo_box_info,
                                               0);
443 444 445 446

      g_type_add_interface_static (combo_box_type,
                                   GTK_TYPE_CELL_LAYOUT,
                                   &cell_layout_info);
447 448 449 450 451 452 453


      g_type_add_interface_static (combo_box_type,
				   GTK_TYPE_CELL_EDITABLE,
				   &cell_editable_info);
      

454 455 456 457 458 459 460 461 462 463 464
    }

  return combo_box_type;
}

/* common */
static void
gtk_combo_box_class_init (GtkComboBoxClass *klass)
{
  GObjectClass *object_class;
  GtkBindingSet *binding_set;
465
  GtkObjectClass *gtk_object_class;
466 467 468 469 470 471 472 473
  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;
474
  container_class->remove = gtk_combo_box_remove;
475 476 477 478 479

  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;
480
  widget_class->scroll_event = gtk_combo_box_scroll_event;
481
  widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
482
  widget_class->style_set = gtk_combo_box_style_set;
483
  widget_class->state_changed = gtk_combo_box_state_changed;
484

485 486 487
  gtk_object_class = (GtkObjectClass *)klass;
  gtk_object_class->destroy = gtk_combo_box_destroy;

488
  object_class = (GObjectClass *)klass;
489
  object_class->finalize = gtk_combo_box_finalize;
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  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",
509 510
                                                        P_("ComboBox model"),
                                                        P_("The model for the combo box"),
511
                                                        GTK_TYPE_TREE_MODEL,
512
                                                        G_PARAM_READWRITE));
513 514 515 516

  g_object_class_install_property (object_class,
                                   PROP_WRAP_WIDTH,
                                   g_param_spec_int ("wrap_width",
517 518
                                                     P_("Wrap width"),
                                                     P_("Wrap width for layouting the items in a grid"),
519 520 521 522 523 524 525 526
                                                     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",
527 528
                                                     P_("Row span column"),
                                                     P_("TreeModel column containing the row span values"),
529
                                                     -1,
530
                                                     G_MAXINT,
531
                                                     -1,
532 533 534 535 536
                                                     G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_COLUMN_SPAN_COLUMN,
                                   g_param_spec_int ("column_span_column",
537 538
                                                     P_("Column span column"),
                                                     P_("TreeModel column containing the column span values"),
539
                                                     -1,
540
                                                     G_MAXINT,
541 542 543 544 545 546 547 548 549 550 551
                                                     -1,
                                                     G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_ROW_SEPARATOR_COLUMN,
                                   g_param_spec_int ("row_separator_column",
                                                     P_("Row separator column"),
                                                     P_("Boolean TreeModel column specifying which rows are separators"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
552 553 554 555 556
                                                     G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_ACTIVE,
                                   g_param_spec_int ("active",
557 558
                                                     P_("Active item"),
                                                     P_("The item which is currently active"),
559
                                                     -1,
560
                                                     G_MAXINT,
561
                                                     -1,
562 563
                                                     G_PARAM_READWRITE));

564 565 566 567 568 569 570 571 572 573 574 575 576 577
  /**
   * 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"),
578
							 P_("Whether dropdowns should have a tearoff menu item"),
579 580 581
							 FALSE,
							 G_PARAM_READWRITE));
  
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  /**
   * 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));
  
598
  gtk_widget_class_install_style_property (widget_class,
599 600
                                           g_param_spec_boolean ("appears-as-list",
                                                                 P_("Appears as list"),
601
                                                                 P_("Whether dropdowns should look like lists rather than menus"),
602
                                                                 FALSE,
603
                                                                 G_PARAM_READABLE));
604 605 606 607

  g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
}

608 609 610 611 612 613 614 615 616
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
617
  iface->reorder = gtk_combo_box_cell_layout_reorder;
618 619
}

620 621 622 623 624 625
static void
gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
{
  iface->start_editing = gtk_combo_box_start_editing;
}

626 627 628 629 630 631
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 ();
632 633
  gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
  GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
Kristian Rietveld's avatar
Kristian Rietveld committed
634
  gtk_widget_show (combo_box->priv->cell_view);
635 636 637 638 639 640 641

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

  combo_box->priv->active_item = -1;
  combo_box->priv->col_column = -1;
  combo_box->priv->row_column = -1;
642
  combo_box->priv->separator_column = -1;
643 644 645 646 647

  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;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
}

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;

676 677 678 679
      case PROP_ROW_SEPARATOR_COLUMN:
        gtk_combo_box_set_row_separator_column (combo_box, g_value_get_int (value));
        break;

680 681 682 683
      case PROP_ACTIVE:
        gtk_combo_box_set_active (combo_box, g_value_get_int (value));
        break;

684 685 686 687
      case PROP_ADD_TEAROFFS:
        gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
        break;

688 689 690 691
      case PROP_HAS_FRAME:
        combo_box->priv->has_frame = g_value_get_boolean (value);
        break;

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
      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;

723 724 725 726
      case PROP_ROW_SEPARATOR_COLUMN:
        g_value_set_int (value, combo_box->priv->separator_column);
        break;

727 728 729 730
      case PROP_ACTIVE:
        g_value_set_int (value, gtk_combo_box_get_active (combo_box));
        break;

731 732 733 734
      case PROP_ADD_TEAROFFS:
        g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
        break;

735 736 737 738
      case PROP_HAS_FRAME:
        g_value_set_boolean (value, combo_box->priv->has_frame);
        break;

739 740 741 742 743 744
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
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);
}

761
static void
762
gtk_combo_box_check_appearance (GtkComboBox *combo_box)
763
{
764
  gboolean appears_as_list;
765 766 767 768 769

  /* if wrap_width > 0, then we are in grid-mode and forced to use
   * unix style
   */
  if (combo_box->priv->wrap_width)
770 771 772 773 774
    appears_as_list = FALSE;
  else
    gtk_widget_style_get (GTK_WIDGET (combo_box),
			  "appears-as-list", &appears_as_list,
			  NULL);
775

776
  if (appears_as_list)
777
    {
778
      /* Destroy all the menu mode widgets, if they exist. */
779
      if (GTK_IS_MENU (combo_box->priv->popup_widget))
780 781 782 783 784
	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);
785 786 787
    }
  else
    {
788
      /* Destroy all the list mode widgets, if they exist. */
789
      if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
790 791 792 793 794
	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);
795
    }
796 797 798 799 800 801 802 803 804
}

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);
805 806 807 808

  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)]);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
}

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)
833 834 835 836 837 838 839 840
    {
      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;
841 842 843 844 845 846 847 848 849

  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)
        {
850 851
	  gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
				combo_box->priv->separator);
852
	  combo_box->priv->separator = NULL;
853 854 855 856 857 858 859

          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;
860
          combo_box->priv->box = NULL;
861 862 863 864
        }
    }
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
static void
gtk_combo_box_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (container);
  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);
      cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
    }


  if (appears_as_list)
    gtk_combo_box_list_setup (combo_box);
  else
    gtk_combo_box_menu_setup (combo_box, TRUE);

  gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
}

915 916 917 918 919 920 921 922 923 924
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;

925
      if (info && info->cell == cell)
926 927 928 929 930 931 932 933 934 935 936 937
        return info;
    }

  return NULL;
}

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

938
  combo_box->priv->popup_in_progress = TRUE;
939 940
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                TRUE);
941
  combo_box->priv->popup_in_progress = FALSE;
942 943 944 945 946 947 948 949 950 951 952 953
}

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

954 955 956 957 958 959 960 961 962 963 964
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);

965
  g_signal_handlers_disconnect_by_func (menu->toplevel,
966 967
					gtk_combo_box_menu_show,
					combo_box);
968
  g_signal_handlers_disconnect_by_func (menu->toplevel,
969 970 971 972 973 974
					gtk_combo_box_menu_hide,
					combo_box);
  
  combo_box->priv->popup_widget = NULL;
}

975 976 977 978 979
static void
gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
                                GtkWidget   *popup)
{
  if (GTK_IS_MENU (combo_box->priv->popup_widget))
980 981 982 983
    {
      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
      combo_box->priv->popup_widget = NULL;
    }
984 985 986 987 988 989 990 991 992 993 994 995 996
  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);
997 998
          combo_box->priv->popup_window = NULL;
	  combo_box->priv->popup_frame = NULL;
999 1000 1001 1002
        }

      combo_box->priv->popup_widget = popup;

1003 1004 1005 1006 1007 1008
      /* 
       * 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",
1009
                        G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1010
      g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1011 1012
                        G_CALLBACK (gtk_combo_box_menu_hide), combo_box);

1013 1014 1015
      gtk_menu_attach_to_widget (GTK_MENU (popup),
				 GTK_WIDGET (combo_box),
				 gtk_combo_box_detacher);
1016 1017 1018 1019 1020 1021
    }
  else
    {
      if (!combo_box->priv->popup_window)
        {
          combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1022
	  gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1023 1024 1025
          gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
                                 gtk_widget_get_screen (GTK_WIDGET (combo_box)));

1026 1027
          combo_box->priv->popup_frame = gtk_frame_new (NULL);
          gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
1028
                                     GTK_SHADOW_ETCHED_IN);
1029 1030
          gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
                             combo_box->priv->popup_frame);
1031

1032
          gtk_widget_show (combo_box->priv->popup_frame);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

	  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);
1046 1047
        }

1048
      gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1049
                         popup);
1050

1051 1052 1053 1054 1055 1056 1057
      gtk_widget_show (popup);
      g_object_ref (G_OBJECT (popup));
      combo_box->priv->popup_widget = popup;
    }
}

static void
1058 1059 1060 1061 1062
gtk_combo_box_menu_position_below (GtkMenu  *menu,
				   gint     *x,
				   gint     *y,
				   gint     *push_in,
				   gpointer  user_data)
1063
{
1064
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1065 1066 1067
  gint sx, sy;
  GtkWidget *child;
  GtkRequisition req;
1068 1069 1070
  GdkScreen *screen;
  gint monitor_num;
  GdkRectangle monitor;
1071
  
1072
  /* FIXME: is using the size request here broken? */
1073 1074 1075 1076
   child = GTK_BIN (combo_box)->child;
   
   gdk_window_get_origin (child->window, &sx, &sy);
   
1077 1078 1079 1080 1081 1082
   if (GTK_WIDGET_NO_WINDOW (child))
      {
	sx += child->allocation.x;
	sy += child->allocation.y;
      }

1083
   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1084

1085
   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1086 1087 1088
     *x = sx;
   else
     *x = sx + child->allocation.width - req.width;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
   *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;
  
1101 1102 1103 1104 1105
  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) 
1106 1107 1108 1109
    *y += child->allocation.height;
  else
    *y -= req.height;

1110
   *push_in = FALSE;
1111
}
1112

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
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);
1135

1136 1137
  gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
  menu_width = requisition.width;
1138

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  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;
    }
1150

1151 1152
  children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
  while (children)
1153
    {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
      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;
1166 1167
    }

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
  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;

1182 1183 1184
  *push_in = TRUE;
}

1185 1186 1187 1188 1189 1190 1191 1192
static void
gtk_combo_box_menu_position (GtkMenu  *menu,
			     gint     *x,
			     gint     *y,
			     gint     *push_in,
			     gpointer  user_data)
{
  GtkComboBox *combo_box;
1193
  GtkWidget *menu_item;
1194 1195 1196 1197 1198 1199

  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
1200 1201 1202 1203 1204 1205 1206 1207
    {
      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);
    }
1208 1209 1210

}

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
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;
1222
  GtkRequisition popup_req;
1223
  GtkPolicyType hpolicy, vpolicy;
1224 1225 1226 1227 1228
  
  sample = GTK_BIN (combo_box)->child;

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

1229 1230 1231 1232 1233 1234 1235 1236
  if (GTK_WIDGET_NO_WINDOW (sample))
    {
      *x += sample->allocation.x;
      *y += sample->allocation.y;
    }
  
  *width = sample->allocation.width;
  
1237
  if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1238 1239 1240 1241 1242 1243 1244
    {
       *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);
    }

1245 1246 1247 1248 1249 1250
  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)
1251
    {
1252 1253 1254 1255
      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);
1256
    }
1257 1258 1259

  *height = popup_req.height;

1260 1261 1262 1263
  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);
1264

1265 1266 1267 1268
  if (*x < monitor.x)
    *x = monitor.x;
  else if (*x + *width > monitor.x + monitor.width)
    *x = monitor.x + monitor.width - *width;
1269 1270 1271
  
  if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
    *y += sample->allocation.height;
1272
  else if (*y - *height >= monitor.y)
1273
    *y -= *height;
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
  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);
    }
1292 1293
} 

1294 1295 1296 1297 1298 1299 1300
static gboolean
menu_row_is_sensitive (GtkComboBox *combo_box,
		       GtkWidget   *item)
{
  GtkWidget *cell_view;
  GList *cells, *list;
  gboolean sensitive;
1301
  
1302
  if (!GTK_IS_CELL_VIEW (GTK_BIN (item)->child))
1303 1304
    return FALSE;
    
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
  cell_view = gtk_bin_get_child (GTK_BIN (item));
  
  gtk_cell_view_set_cell_data (GTK_CELL_VIEW (cell_view));
  cells = gtk_cell_view_get_cell_renderers (GTK_CELL_VIEW (cell_view));

  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;

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  if (combo_box->priv->separator_column != -1)
    {
      gboolean is_separator;

      gtk_tree_model_get (combo_box->priv->model,
			  iter,
			  combo_box->priv->separator_column, &is_separator,
			  -1);

      if (is_separator)
	return FALSE;
    }

1350 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 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  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;
}
		       
static gboolean
row_is_sensitive (GtkComboBox *combo_box,
		  gint         index)
{
  gboolean sensitive;

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

  if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
      GtkWidget *item;
      GList *children, *child;

      children = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
      child = children;
      if (GTK_IS_TEAROFF_MENU_ITEM (child->data))
	child = child->next;
      child = g_list_nth (child, index);
      item = GTK_WIDGET (child->data);
      g_list_free (children);

      sensitive = menu_row_is_sensitive (combo_box, item);
    }
  else
    {
      GtkTreePath *path;
      GtkTreeIter iter;

      path = gtk_tree_path_new_from_indices (index, -1);
      if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
	sensitive = tree_column_row_is_sensitive (combo_box, &iter);
      else
	sensitive = TRUE;

      gtk_tree_path_free (path);

    }

  return sensitive;
}

static void
update_menu_sensitivity (GtkComboBox *combo_box)
{
  gint i, items;
  GtkWidget *menu;
  GList *children, *child;

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

  items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
  menu = combo_box->priv->popup_widget;

  children = gtk_container_get_children (GTK_CONTAINER (menu));
  child = children;

  for (i = 0; i < items; i++, child = child->next)
    {
      GtkWidget *item = GTK_WIDGET (child->data);
      gboolean sensitive;

1435
      if (!GTK_IS_CELL_VIEW (GTK_BIN (item)->child))
1436 1437
	continue;

1438 1439 1440 1441 1442 1443
      sensitive = menu_row_is_sensitive (combo_box, item);
      gtk_widget_set_sensitive (item, sensitive);
    }
  g_list_free (children);
}

1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
/**
 * 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
1456 1457
gtk_combo_box_popup (GtkComboBox *combo_box)
{
1458
  gint x, y, width, height;
1459
  
1460 1461
  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));

1462 1463 1464 1465 1466
  if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
    return;

  if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
1467
      update_menu_sensitivity (combo_box);
1468 1469
      gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
			   combo_box->priv->active_item);
1470

1471 1472
      if (combo_box->priv->wrap_width == 0)
	{
1473 1474
          GtkRequisition requisition;

1475
	  width = GTK_WIDGET (combo_box)->allocation.width;
1476 1477 1478 1479
          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);
1480 1481
	}
      
1482
      gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1483 1484 1485
		      NULL, NULL,
		      gtk_combo_box_menu_position, combo_box,
		      0, 0);
1486 1487 1488
      return;
    }

1489
  gtk_widget_show_all (combo_box->priv->popup_frame);
1490
  gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1491
  
1492
  gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);  
1493
  gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1494 1495

  /* popup */
1496
  gtk_widget_show (combo_box->priv->popup_window);
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507

  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);
    }
1508 1509 1510 1511 1512 1513 1514

  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);
1515 1516
}

1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
/**
 * 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
1529 1530
gtk_combo_box_popdown (GtkComboBox *combo_box)
{
1531 1532
  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));

1533
  if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
Matthias Clasen's avatar
Matthias Clasen committed
1534 1535
    return;

1536 1537 1538 1539 1540 1541
  if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
      gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
      return;
    }

1542
  gtk_combo_box_list_remove_grabs (combo_box);
1543

1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
  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;

1556 1557 1558 1559 1560 1561
  if (combo_box->priv->cell_view)
    gtk_widget_style_get (combo_box->priv->cell_view,
                          "focus-line-width", &padding,
                          NULL);
  else
    padding = 0;
1562 1563 1564 1565

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

1566 1567 1568 1569 1570
  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;
1571 1572 1573 1574 1575

  return req.width + padding;
}

static void
1576
gtk_combo_box_remeasure (GtkComboBox *combo_box)
1577 1578 1579 1580
{
  GtkTreeIter iter;
  GtkTreePath *path;