gtkcombobox.c 141 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
#include <stdarg.h>

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

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

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

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

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

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

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

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

  gint col_column;
  gint row_column;

  gint wrap_width;

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

  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

  GtkTreeViewRowSeparatorFunc row_separator_func;
  gpointer                    row_separator_data;
  GtkDestroyNotify            row_separator_destroy;
120 121
};

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

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

enum {
  PROP_0,
  PROP_MODEL,
  PROP_WRAP_WIDTH,
  PROP_ROW_SPAN_COLUMN,
  PROP_COLUMN_SPAN_COLUMN,
191
  PROP_ACTIVE,
192 193
  PROP_ADD_TEAROFFS,
  PROP_HAS_FRAME
194 195 196 197 198 199
};

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

#define BONUS_PADDING 4
200
#define SCROLL_TIME  100
201 202 203

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

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

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

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

260 261
static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);

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

280 281
static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/* 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);
299 300 301 302
static void     gtk_combo_box_model_row_expanded   (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gpointer          data);
303

304
/* list */
305 306 307 308 309
static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
						    gint             *x, 
						    gint             *y, 
						    gint             *width,
						    gint             *height);
310 311 312
static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);

313 314
static void     gtk_combo_box_list_remove_grabs    (GtkComboBox      *combo_box);

315 316 317 318 319 320
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);
321 322 323 324 325 326 327
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);
328 329 330 331 332 333 334 335
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);
336
static void     gtk_combo_box_list_popup_resize    (GtkComboBox      *combo_box);
337 338 339

/* menu */
static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
340
                                                    gboolean          add_children);
341
static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
342 343 344
static void     gtk_combo_box_menu_fill_level      (GtkComboBox      *combo_box,
						    GtkWidget        *menu,
						    GtkTreeIter      *iter);
345 346 347
static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);

static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
348 349 350
						    GtkWidget        *item,
                                                    GtkTreeIter      *iter,
						    GtkWidget        *last);
351 352 353 354 355 356 357 358 359 360 361 362 363 364
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);
365 366 367 368 369
static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gint             *new_order,
						    gpointer          user_data);
370 371 372 373
static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
374 375 376
static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
377 378 379
static void     gtk_combo_box_menu_state_changed   (GtkWidget        *widget,
			                            GtkStateType      previous,
						    gpointer          data);
380 381 382 383 384 385
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);
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/* 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
406 407 408
static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              gint                   position);
409 410
static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
							      gboolean      group_cycling);
411

412 413 414 415 416 417 418
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);
419

420 421 422 423 424
/* GtkCellEditable method implementations */
static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
					 GdkEvent        *event);


425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
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
        };

445 446 447 448 449 450 451
      static const GInterfaceInfo cell_layout_info =
        {
          (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
          NULL,
          NULL
        };

452 453 454 455 456 457 458
      static const GInterfaceInfo cell_editable_info =
	{
	  (GInterfaceInitFunc) gtk_combo_box_cell_editable_init,
	  NULL,
	  NULL
       };

459 460 461 462
      combo_box_type = g_type_register_static (GTK_TYPE_BIN,
                                               "GtkComboBox",
                                               &combo_box_info,
                                               0);
463 464 465 466

      g_type_add_interface_static (combo_box_type,
                                   GTK_TYPE_CELL_LAYOUT,
                                   &cell_layout_info);
467 468 469 470 471 472 473


      g_type_add_interface_static (combo_box_type,
				   GTK_TYPE_CELL_EDITABLE,
				   &cell_editable_info);
      

474 475 476 477 478 479 480 481 482 483 484
    }

  return combo_box_type;
}

/* common */
static void
gtk_combo_box_class_init (GtkComboBoxClass *klass)
{
  GObjectClass *object_class;
  GtkBindingSet *binding_set;
485
  GtkObjectClass *gtk_object_class;
486 487 488 489 490 491 492 493
  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;
494
  container_class->remove = gtk_combo_box_remove;
495 496 497 498 499

  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;
500
  widget_class->scroll_event = gtk_combo_box_scroll_event;
501
  widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
502
  widget_class->style_set = gtk_combo_box_style_set;
503
  widget_class->state_changed = gtk_combo_box_state_changed;
504

505 506 507
  gtk_object_class = (GtkObjectClass *)klass;
  gtk_object_class->destroy = gtk_combo_box_destroy;

508
  object_class = (GObjectClass *)klass;
509
  object_class->finalize = gtk_combo_box_finalize;
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  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",
529 530
                                                        P_("ComboBox model"),
                                                        P_("The model for the combo box"),
531
                                                        GTK_TYPE_TREE_MODEL,
532
                                                        G_PARAM_READWRITE));
533 534 535 536

  g_object_class_install_property (object_class,
                                   PROP_WRAP_WIDTH,
                                   g_param_spec_int ("wrap_width",
537 538
                                                     P_("Wrap width"),
                                                     P_("Wrap width for layouting the items in a grid"),
539 540 541 542 543 544 545 546
                                                     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",
547 548
                                                     P_("Row span column"),
                                                     P_("TreeModel column containing the row span values"),
549
                                                     -1,
550
                                                     G_MAXINT,
551
                                                     -1,
552 553 554 555 556
                                                     G_PARAM_READWRITE));

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

564 565 566 567

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

575 576 577 578 579 580 581 582 583 584 585 586 587 588
  /**
   * 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"),
589
							 P_("Whether dropdowns should have a tearoff menu item"),
590 591 592
							 FALSE,
							 G_PARAM_READWRITE));
  
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
  /**
   * 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));
  
609
  gtk_widget_class_install_style_property (widget_class,
610 611
                                           g_param_spec_boolean ("appears-as-list",
                                                                 P_("Appears as list"),
612
                                                                 P_("Whether dropdowns should look like lists rather than menus"),
613
                                                                 FALSE,
614
                                                                 G_PARAM_READABLE));
615 616 617 618

  g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
}

619 620 621 622 623 624 625 626 627
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
628
  iface->reorder = gtk_combo_box_cell_layout_reorder;
629 630
}

631 632 633 634 635 636
static void
gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
{
  iface->start_editing = gtk_combo_box_start_editing;
}

637 638 639 640 641 642
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 ();
643 644
  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
645
  gtk_widget_show (combo_box->priv->cell_view);
646 647 648 649

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

650
  combo_box->priv->active_row = NULL;
651 652
  combo_box->priv->col_column = -1;
  combo_box->priv->row_column = -1;
653 654 655 656 657

  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;
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
}

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;

690 691 692 693
      case PROP_ADD_TEAROFFS:
        gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
        break;

694 695 696 697
      case PROP_HAS_FRAME:
        combo_box->priv->has_frame = g_value_get_boolean (value);
        break;

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 723 724 725 726 727 728 729 730 731 732
      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;

733 734 735 736
      case PROP_ADD_TEAROFFS:
        g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
        break;

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

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

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

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

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

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

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);
807 808 809 810

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

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

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

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

867 868 869 870 871
static void
gtk_combo_box_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (container);
872
  GtkTreePath *path;
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
  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);
906
      gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (combo_box->priv->cell_view));
907 908 909 910 911 912 913 914
    }


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

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

925 926 927 928 929 930 931 932 933 934
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;

935
      if (info && info->cell == cell)
936 937 938 939 940 941 942 943 944 945 946 947
        return info;
    }

  return NULL;
}

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

948
  combo_box->priv->popup_in_progress = TRUE;
949 950
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                TRUE);
951
  combo_box->priv->popup_in_progress = FALSE;
952 953 954 955 956 957 958 959 960 961 962 963
}

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

964 965 966 967 968 969 970 971 972 973 974
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);

975
  g_signal_handlers_disconnect_by_func (menu->toplevel,
976 977
					gtk_combo_box_menu_show,
					combo_box);
978
  g_signal_handlers_disconnect_by_func (menu->toplevel,
979 980 981 982 983 984
					gtk_combo_box_menu_hide,
					combo_box);
  
  combo_box->priv->popup_widget = NULL;
}

985 986 987 988 989
static void
gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
                                GtkWidget   *popup)
{
  if (GTK_IS_MENU (combo_box->priv->popup_widget))
990 991 992 993
    {
      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
      combo_box->priv->popup_widget = NULL;
    }
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
  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);
1007 1008
          combo_box->priv->popup_window = NULL;
	  combo_box->priv->popup_frame = NULL;
1009 1010 1011 1012
        }

      combo_box->priv->popup_widget = popup;

1013 1014 1015 1016 1017 1018
      /* 
       * 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",
1019
                        G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1020
      g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1021 1022
                        G_CALLBACK (gtk_combo_box_menu_hide), combo_box);

1023 1024 1025
      gtk_menu_attach_to_widget (GTK_MENU (popup),
				 GTK_WIDGET (combo_box),
				 gtk_combo_box_detacher);
1026 1027 1028 1029 1030 1031
    }
  else
    {
      if (!combo_box->priv->popup_window)
        {
          combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1032
	  gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1033 1034 1035
          gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
                                 gtk_widget_get_screen (GTK_WIDGET (combo_box)));

1036 1037
          combo_box->priv->popup_frame = gtk_frame_new (NULL);
          gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
1038
                                     GTK_SHADOW_ETCHED_IN);
1039 1040
          gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
                             combo_box->priv->popup_frame);
1041

1042
          gtk_widget_show (combo_box->priv->popup_frame);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

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

1058
      gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1059
                         popup);
1060

1061 1062 1063 1064 1065 1066 1067
      gtk_widget_show (popup);
      g_object_ref (G_OBJECT (popup));
      combo_box->priv->popup_widget = popup;
    }
}

static void
1068 1069 1070 1071 1072
gtk_combo_box_menu_position_below (GtkMenu  *menu,
				   gint     *x,
				   gint     *y,
				   gint     *push_in,
				   gpointer  user_data)
1073
{
1074
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1075 1076 1077
  gint sx, sy;
  GtkWidget *child;
  GtkRequisition req;
1078 1079 1080
  GdkScreen *screen;
  gint monitor_num;
  GdkRectangle monitor;
1081
  
1082
  /* FIXME: is using the size request here broken? */
1083 1084 1085 1086
   child = GTK_BIN (combo_box)->child;
   
   gdk_window_get_origin (child->window, &sx, &sy);
   
1087 1088 1089 1090 1091 1092
   if (GTK_WIDGET_NO_WINDOW (child))
      {
	sx += child->allocation.x;
	sy += child->allocation.y;
      }

1093
   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1094

1095
   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1096 1097 1098
     *x = sx;
   else
     *x = sx + child->allocation.width - req.width;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
   *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;
  
1111 1112 1113 1114 1115
  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) 
1116 1117 1118 1119
    *y += child->allocation.height;
  else
    *y -= req.height;

1120
   *push_in = FALSE;
1121
}
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
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);
1145

1146 1147
  gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
  menu_width = requisition.width;
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  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;
    }
1160

1161 1162
  children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
  while (children)
1163
    {
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
      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;
1176 1177
    }

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
  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;

1192 1193 1194
  *push_in = TRUE;
}

1195 1196 1197 1198 1199 1200 1201 1202
static void
gtk_combo_box_menu_position (GtkMenu  *menu,
			     gint     *x,
			     gint     *y,
			     gint     *push_in,
			     gpointer  user_data)
{
  GtkComboBox *combo_box;
1203
  GtkWidget *menu_item;
1204 1205 1206 1207 1208 1209

  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
1210
    {
1211
      /* FIXME handle nested menus better */
1212 1213 1214 1215 1216 1217 1218
      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);
    }
1219 1220 1221

}

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
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;
1233
  GtkRequisition popup_req;
1234
  GtkPolicyType hpolicy, vpolicy;
1235 1236 1237 1238 1239
  
  sample = GTK_BIN (combo_box)->child;

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

1240 1241 1242 1243 1244 1245 1246 1247
  if (GTK_WIDGET_NO_WINDOW (sample))
    {
      *x += sample->allocation.x;
      *y += sample->allocation.y;
    }
  
  *width = sample->allocation.width;
  
1248
  if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1249 1250 1251 1252 1253 1254 1255
    {
       *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);
    }

1256 1257 1258 1259 1260 1261
  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)
1262
    {
1263 1264 1265 1266
      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);
1267
    }
1268 1269 1270

  *height = popup_req.height;

1271 1272 1273 1274
  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);
1275

1276 1277 1278 1279
  if (*x < monitor.x)
    *x = monitor.x;
  else if (*x + *width > monitor.x + monitor.width)
    *x = monitor.x + monitor.width - *width;
1280 1281 1282
  
  if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
    *y += sample->allocation.height;
1283
  else if (*y - *height >= monitor.y)
1284
    *y -= *height;
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  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);
    }
1303 1304
} 

1305
static gboolean
1306
cell_view_is_sensitive (GtkCellView *cell_view)
1307 1308 1309
{
  GList *cells, *list;
  gboolean sensitive;
1310
  
1311 1312
  gtk_cell_view_set_cell_data (cell_view);
  cells = gtk_cell_view_get_cell_renderers (cell_view);
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

  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;

1340
  if (combo_box->priv->row_separator_func)
1341
    {
1342 1343
      if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
						  combo_box->priv->row_separator_data))
1344 1345 1346
	return FALSE;
    }

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
  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;
}
1368 1369 1370 1371

static void
update_menu_sensitivity (GtkComboBox *combo_box,
			 GtkWidget   *menu)
1372
{
1373 1374 1375
  GList *children, *child;
  GtkWidget *item, *submenu, *separator;
  GtkWidget *cell_view;
1376 1377 1378
  gboolean sensitive;

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

1381
  children = gtk_container_get_children (GTK_CONTAINER (menu));
1382

1383
  for (child = children; child; child = child->next)
1384
    {
1385 1386
      item = GTK_WIDGET (child->data);
      cell_view = GTK_BIN (item)->child;
1387

1388 1389 1390 1391 1392 1393 1394 1395 1396
      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);
	}
1397
      else
1398 1399
	{
	  sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1400

1401 1402 1403 1404 1405 1406 1407 1408 1409
	  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);
	}
1410 1411
    }

1412
  g_list_free (children);
1413 1414
}

1415 1416 1417 1418
static void 
gtk_combo_box_menu_popup (GtkComboBox *combo_box,
			  guint        button, 
			  guint32      activate_time)
1419
{
1420 1421 1422 1423 1424 1425
  GtkTreePath *path;
  gint active_item;
  GtkRequisition requisition;
  gint width;
  
  update_menu_sensitivity (combo_box, combo_box->priv->popup_widget);
1426

1427 1428
  active_item = -1;
  if (combo_box->priv->active_row)
1429
    {
1430 1431 1432 1433 1434
      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);
1435

1436 1437 1438 1439
	  if (combo_box->priv->add_tearoffs)
	    active_item++;
	}
    }
1440

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
  /* 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);
1451
    }
1452 1453 1454 1455 1456
  
  gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
		  NULL, NULL,
		  gtk_combo_box_menu_position, combo_box,
		  button, activate_time);
1457 1458
}

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
/**
 * 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
1471 1472
gtk_combo_box_popup (GtkComboBox *combo_box)
{
1473
  gint x, y, width, height;
1474
  GtkTreePath *path, *ppath;
1475
  
1476 1477
  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));

1478 1479 1480 1481 1482
  if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
    return;

  if (GTK_IS_MENU (combo_box->priv->popup_widget))
    {
1483
      gtk_combo_box_menu_popup (combo_box, 0, 0);
1484 1485 1486
      return;
    }

1487
  gtk_widget_show_all (combo_box->priv->popup_frame);