gtkcombobox.c 157 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
#include "gtkprivate.h"
42 43 44 45 46

#include <gdk/gdkkeysyms.h>

#include <gobject/gvaluecollector.h>

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

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

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

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

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

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

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

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

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

  gint col_column;
  gint row_column;

  gint wrap_width;

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

  GtkWidget *tree_view;
  GtkTreeViewColumn *column;

  GtkWidget *cell_view;
  GtkWidget *cell_view_frame;

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

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

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

  gint width;
  GSList *cells;

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

  GtkTreeViewRowSeparatorFunc row_separator_func;
  gpointer                    row_separator_data;
  GtkDestroyNotify            row_separator_destroy;
124 125

  gchar *tearoff_title;
126 127
};

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

186 187 188 189 190 191 192 193 194 195 196
enum {
  CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_MODEL,
  PROP_WRAP_WIDTH,
  PROP_ROW_SPAN_COLUMN,
  PROP_COLUMN_SPAN_COLUMN,
197
  PROP_ACTIVE,
198
  PROP_ADD_TEAROFFS,
199
  PROP_TEAROFF_TITLE,
Matthias Clasen's avatar
Matthias Clasen committed
200
  PROP_HAS_FRAME,
201 202
  PROP_FOCUS_ON_CLICK,
  PROP_POPUP_SHOWN
203 204 205 206 207
};

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

#define BONUS_PADDING 4
208
#define SCROLL_TIME  100
209 210

/* common */
211

212
static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
213
static void     gtk_combo_box_cell_editable_init   (GtkCellEditableIface *iface);
214 215
static void     gtk_combo_box_finalize             (GObject          *object);
static void     gtk_combo_box_destroy              (GtkObject        *object);
216 217 218 219 220 221 222 223 224 225

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

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

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);
251 252 253 254 255 256 257 258 259 260
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);
261 262 263 264 265 266 267 268
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);
269
static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
270

271 272
static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);

273 274 275 276 277 278 279 280 281 282
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);
283 284
static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
                                                    GdkEventScroll   *event);
285
static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
286
						    GtkTreePath      *path);
287 288 289
static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
290

291
static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);
292
static gchar *  gtk_combo_box_real_get_active_text (GtkComboBox      *combo_box);
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
/* 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);
311 312 313 314
static void     gtk_combo_box_model_row_expanded   (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gpointer          data);
315

316
/* list */
317 318 319 320 321
static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
						    gint             *x, 
						    gint             *y, 
						    gint             *width,
						    gint             *height);
322 323 324 325 326 327 328 329 330
static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);

static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          data);
static gboolean gtk_combo_box_list_key_press       (GtkWidget        *widget,
                                                    GdkEventKey      *event,
                                                    gpointer          data);
331 332 333 334 335 336 337
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);
338 339 340 341
static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
                                                    GdkEventButton   *event,
                                                    gpointer          data);

342 343 344 345 346 347
static gboolean gtk_combo_box_list_select_func     (GtkTreeSelection *selection,
						    GtkTreeModel     *model,
						    GtkTreePath      *path,
						    gboolean          path_currently_selected,
						    gpointer          data);

348 349 350 351
static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
352
static void     gtk_combo_box_list_popup_resize    (GtkComboBox      *combo_box);
353 354 355

/* menu */
static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
356
                                                    gboolean          add_children);
357
static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
358 359 360
static void     gtk_combo_box_menu_fill_level      (GtkComboBox      *combo_box,
						    GtkWidget        *menu,
						    GtkTreeIter      *iter);
361
static void     gtk_combo_box_update_title         (GtkComboBox      *combo_box);
362 363 364
static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);

static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
365 366 367
						    GtkWidget        *item,
                                                    GtkTreeIter      *iter,
						    GtkWidget        *last);
368 369 370 371 372 373 374 375 376 377 378 379 380 381
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);
382 383 384 385 386
static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
						    GtkTreePath      *path,
						    GtkTreeIter      *iter,
						    gint             *new_order,
						    gpointer          user_data);
387 388 389 390
static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
                                                    GtkTreePath      *path,
                                                    GtkTreeIter      *iter,
                                                    gpointer          data);
391 392 393
static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
						    GdkEventKey      *event,
						    gpointer          data);
394 395 396 397 398 399
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);
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/* 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
420 421 422
static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
                                                              GtkCellRenderer       *cell,
                                                              gint                   position);
423 424
static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
							      gboolean      group_cycling);
425

426 427 428 429 430 431 432
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);
433
static void     gtk_combo_box_child_show                     (GtkWidget       *widget,
434
							      GtkComboBox     *combo_box);
435
static void     gtk_combo_box_child_hide                     (GtkWidget       *widget,
436
							      GtkComboBox     *combo_box);
437

438

439 440 441 442 443
/* GtkCellEditable method implementations */
static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
					 GdkEvent        *event);


Matthias Clasen's avatar
Matthias Clasen committed
444 445 446 447
G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
						gtk_combo_box_cell_layout_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
Matthias Clasen's avatar
Matthias Clasen committed
448
						gtk_combo_box_cell_editable_init))
449 450 451 452 453 454

/* common */
static void
gtk_combo_box_class_init (GtkComboBoxClass *klass)
{
  GObjectClass *object_class;
455
  GtkObjectClass *gtk_object_class;
456 457 458
  GtkContainerClass *container_class;
  GtkWidgetClass *widget_class;

459 460
  klass->get_active_text = gtk_combo_box_real_get_active_text;

461 462 463
  container_class = (GtkContainerClass *)klass;
  container_class->forall = gtk_combo_box_forall;
  container_class->add = gtk_combo_box_add;
464
  container_class->remove = gtk_combo_box_remove;
465 466 467 468 469

  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;
470
  widget_class->scroll_event = gtk_combo_box_scroll_event;
471
  widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
472
  widget_class->grab_focus = gtk_combo_box_grab_focus;
473
  widget_class->style_set = gtk_combo_box_style_set;
474
  widget_class->state_changed = gtk_combo_box_state_changed;
475

476 477 478
  gtk_object_class = (GtkObjectClass *)klass;
  gtk_object_class->destroy = gtk_combo_box_destroy;

479
  object_class = (GObjectClass *)klass;
480
  object_class->finalize = gtk_combo_box_finalize;
481 482 483 484
  object_class->set_property = gtk_combo_box_set_property;
  object_class->get_property = gtk_combo_box_get_property;

  /* signals */
Matthias Clasen's avatar
Matthias Clasen committed
485 486 487 488
  /**
   * GtkComboBox::changed:
   * @widget: the object which received the signal
   * 
489
   * The changed signal is emitted when the active
Matthias Clasen's avatar
Matthias Clasen committed
490 491 492
   * item is changed. The can be due to the user selecting
   * a different item from the list, or due to a 
   * call to gtk_combo_box_set_active_iter().
493 494
   * It will also be emitted while typing into a GtkComboBoxEntry, 
   * as well as when selecting an item from the GtkComboBoxEntry's list.
Matthias Clasen's avatar
Matthias Clasen committed
495 496 497
   *
   * Since: 2.4
   */
498
  combo_box_signals[CHANGED] =
499
    g_signal_new (I_("changed"),
500 501 502 503 504 505 506 507
                  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 */
Matthias Clasen's avatar
Matthias Clasen committed
508 509 510 511 512 513 514 515
  /**
   * GtkComboBox:model:
   *
   * The model from which the combo box takes the values shown
   * in the list. 
   *
   * Since: 2.4
   */
516 517 518
  g_object_class_install_property (object_class,
                                   PROP_MODEL,
                                   g_param_spec_object ("model",
519 520
                                                        P_("ComboBox model"),
                                                        P_("The model for the combo box"),
521
                                                        GTK_TYPE_TREE_MODEL,
522
                                                        GTK_PARAM_READWRITE));
523

Matthias Clasen's avatar
Matthias Clasen committed
524 525 526 527 528 529 530 531 532
  /**
   * GtkComboBox:wrap-width:
   *
   * If wrap-width is set to a positive value, the list will be
   * displayed in multiple columns, the number of columns is
   * determined by wrap-width.
   *
   * Since: 2.4
   */
533 534
  g_object_class_install_property (object_class,
                                   PROP_WRAP_WIDTH,
535
                                   g_param_spec_int ("wrap-width",
536
                                                     P_("Wrap width"),
537
                                                     P_("Wrap width for laying out the items in a grid"),
538 539 540
                                                     0,
                                                     G_MAXINT,
                                                     0,
541
                                                     GTK_PARAM_READWRITE));
542

Matthias Clasen's avatar
Matthias Clasen committed
543 544 545 546 547 548 549 550 551 552 553 554 555

  /**
   * GtkComboBox:row-span-column:
   *
   * If this is set to a non-negative value, it must be the index of a column 
   * of type %G_TYPE_INT in the model. 
   *
   * The values of that column are used to determine how many rows a value 
   * in the list will span. Therefore, the values in the model column pointed 
   * to by this property must be greater than zero and not larger than wrap-width.
   *
   * Since: 2.4
   */
556 557
  g_object_class_install_property (object_class,
                                   PROP_ROW_SPAN_COLUMN,
558
                                   g_param_spec_int ("row-span-column",
559 560
                                                     P_("Row span column"),
                                                     P_("TreeModel column containing the row span values"),
561
                                                     -1,
562
                                                     G_MAXINT,
563
                                                     -1,
564
                                                     GTK_PARAM_READWRITE));
565

Matthias Clasen's avatar
Matthias Clasen committed
566 567 568 569 570 571 572 573 574 575 576 577

  /**
   * GtkComboBox:column-span-column:
   *
   * If this is set to a non-negative value, it must be the index of a column 
   * of type %G_TYPE_INT in the model. 
   *
   * The values of that column are used to determine how many columns a value 
   * in the list will span. 
   *
   * Since: 2.4
   */
578 579
  g_object_class_install_property (object_class,
                                   PROP_COLUMN_SPAN_COLUMN,
580
                                   g_param_spec_int ("column-span-column",
581 582
                                                     P_("Column span column"),
                                                     P_("TreeModel column containing the column span values"),
583
                                                     -1,
584
                                                     G_MAXINT,
585
                                                     -1,
586
                                                     GTK_PARAM_READWRITE));
587

588

Matthias Clasen's avatar
Matthias Clasen committed
589 590 591 592 593 594 595 596 597 598
  /**
   * GtkComboBox:active:
   *
   * The item which is currently active. If the model is a non-flat treemodel,
   * and the active item is not an immediate child of the root of the tree,
   * this property has the value <literal>gtk_tree_path_get_indices (path)[0]</literal>,
   * where <literal>path</literal> is the #GtkTreePath of the active item.
   *
   * Since: 2.4
   */
599 600 601
  g_object_class_install_property (object_class,
                                   PROP_ACTIVE,
                                   g_param_spec_int ("active",
602 603
                                                     P_("Active item"),
                                                     P_("The item which is currently active"),
604
                                                     -1,
605
                                                     G_MAXINT,
606
                                                     -1,
607
                                                     GTK_PARAM_READWRITE));
608

609 610 611
  /**
   * GtkComboBox:add-tearoffs:
   *
Matthias Clasen's avatar
Matthias Clasen committed
612
   * The add-tearoffs property controls whether generated menus 
613 614 615 616 617 618 619 620 621 622
   * 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"),
623
							 P_("Whether dropdowns should have a tearoff menu item"),
624
							 FALSE,
625
							 GTK_PARAM_READWRITE));
626
  
627 628 629
  /**
   * GtkComboBox:has-frame:
   *
Matthias Clasen's avatar
Matthias Clasen committed
630
   * The has-frame property controls whether a frame
631 632 633 634 635 636 637 638 639 640
   * 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,
641
							 GTK_PARAM_READWRITE));
642
  
Matthias Clasen's avatar
Matthias Clasen committed
643 644
  g_object_class_install_property (object_class,
                                   PROP_FOCUS_ON_CLICK,
645
                                   g_param_spec_boolean ("focus-on-click",
Matthias Clasen's avatar
Matthias Clasen committed
646 647 648
							 P_("Focus on click"),
							 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
							 TRUE,
649
							 GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
650

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
  /**
   * GtkComboBox:tearoff-title:
   *
   * A title that may be displayed by the window manager 
   * when the popup is torn-off.
   *
   * Since: 2.10
   */
  g_object_class_install_property (object_class,
                                   PROP_TEAROFF_TITLE,
                                   g_param_spec_string ("tearoff-title",
                                                        P_("Tearoff Title"),
                                                        P_("A title that may be displayed by the window manager when the popup is torn-off"),
                                                        "",
                                                        GTK_PARAM_READWRITE));


668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
  /**
   * GtkComboBox:popup-shown:
   *
   * Whether the combo boxes dropdown is popped up. 
   * Note that this property is mainly useful, because
   * it allows you to connect to notify::popup-shown.
   *
   * Since: 2.10
   */
  g_object_class_install_property (object_class,
                                   PROP_POPUP_SHOWN,
                                   g_param_spec_boolean ("popup-shown",
                                                         P_("Popup shown"),
                                                         P_("Whether the combo's dropdown is shown"),
                                                         FALSE,
                                                         GTK_PARAM_READABLE));
  
685
  gtk_widget_class_install_style_property (widget_class,
686 687
                                           g_param_spec_boolean ("appears-as-list",
                                                                 P_("Appears as list"),
688
                                                                 P_("Whether dropdowns should look like lists rather than menus"),
689
                                                                 FALSE,
690
                                                                 GTK_PARAM_READABLE));
691 692 693 694

  g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
}

695 696 697 698 699 700 701 702 703
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
704
  iface->reorder = gtk_combo_box_cell_layout_reorder;
705 706
}

707 708 709 710 711 712
static void
gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
{
  iface->start_editing = gtk_combo_box_start_editing;
}

713 714 715 716 717 718
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 ();
719 720
  gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
  GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
721
  gtk_widget_show (combo_box->priv->cell_view);
722 723 724 725

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

726
  combo_box->priv->active_row = NULL;
727 728
  combo_box->priv->col_column = -1;
  combo_box->priv->row_column = -1;
729

730
  combo_box->priv->popup_shown = FALSE;
731 732 733 734
  combo_box->priv->add_tearoffs = FALSE;
  combo_box->priv->has_frame = TRUE;
  combo_box->priv->is_cell_renderer = FALSE;
  combo_box->priv->editing_canceled = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
735 736
  combo_box->priv->auto_scroll = FALSE;
  combo_box->priv->focus_on_click = TRUE;
737 738

  gtk_combo_box_check_appearance (combo_box);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
}

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;

771 772 773 774
      case PROP_ADD_TEAROFFS:
        gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
        break;

775 776 777 778
      case PROP_HAS_FRAME:
        combo_box->priv->has_frame = g_value_get_boolean (value);
        break;

Matthias Clasen's avatar
Matthias Clasen committed
779
      case PROP_FOCUS_ON_CLICK:
780 781
	gtk_combo_box_set_focus_on_click (combo_box, 
					  g_value_get_boolean (value));
Matthias Clasen's avatar
Matthias Clasen committed
782 783
        break;

784 785 786 787
      case PROP_TEAROFF_TITLE:
	gtk_combo_box_set_title (combo_box, g_value_get_string (value));
        break;

788 789 790 791 792 793 794 795 796 797 798
      case PROP_POPUP_SHOWN:
        if (g_value_get_boolean (value))
          {
            gtk_combo_box_popup (combo_box);
          }
        else
          {
            gtk_combo_box_popdown (combo_box);
          }
        break;

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
      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;

834 835 836 837
      case PROP_ADD_TEAROFFS:
        g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
        break;

838 839 840 841
      case PROP_HAS_FRAME:
        g_value_set_boolean (value, combo_box->priv->has_frame);
        break;

Matthias Clasen's avatar
Matthias Clasen committed
842 843 844 845
      case PROP_FOCUS_ON_CLICK:
        g_value_set_boolean (value, combo_box->priv->focus_on_click);
        break;

846 847 848 849
      case PROP_TEAROFF_TITLE:
        g_value_set_string (value, gtk_combo_box_get_title (combo_box));
        break;

850 851 852 853
      case PROP_POPUP_SHOWN:
        g_value_set_boolean (value, combo_box->priv->popup_shown);
        break;

854 855 856 857 858 859
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
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);
}

876 877 878 879 880 881 882 883 884 885
static void
gtk_combo_box_button_state_changed (GtkWidget    *widget,
				    GtkStateType  previous,
				    gpointer      data)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (data);

  if (GTK_WIDGET_REALIZED (widget))
    {
      if (!combo_box->priv->tree_view && combo_box->priv->cell_view)
886 887 888 889 890 891 892 893 894
	{
	  if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
	      (GTK_WIDGET_STATE (combo_box->priv->cell_view) == GTK_STATE_INSENSITIVE))
	    gtk_widget_set_sensitive (combo_box->priv->cell_view, GTK_WIDGET_SENSITIVE (widget));
	  
	  gtk_widget_set_state (combo_box->priv->cell_view, 
				GTK_WIDGET_STATE (widget));
	  
	}
895 896 897 898 899
    }

  gtk_widget_queue_draw (widget);
}

900
static void
901
gtk_combo_box_check_appearance (GtkComboBox *combo_box)
902
{
903
  gboolean appears_as_list;
904 905 906 907 908

  /* if wrap_width > 0, then we are in grid-mode and forced to use
   * unix style
   */
  if (combo_box->priv->wrap_width)
909 910 911 912 913
    appears_as_list = FALSE;
  else
    gtk_widget_style_get (GTK_WIDGET (combo_box),
			  "appears-as-list", &appears_as_list,
			  NULL);
914

915
  if (appears_as_list)
916
    {
917
      /* Destroy all the menu mode widgets, if they exist. */
918
      if (GTK_IS_MENU (combo_box->priv->popup_widget))
919 920 921 922 923
	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);
924 925 926
    }
  else
    {
927
      /* Destroy all the list mode widgets, if they exist. */
928
      if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
929 930 931 932 933
	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);
934
    }
935 936 937 938 939 940 941 942 943
}

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);
944 945 946 947

  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)]);
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
}

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)
972 973 974 975 976 977 978 979
    {
      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;
980 981 982 983 984 985 986 987 988

  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)
        {
989 990
	  gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
				combo_box->priv->separator);
991
	  combo_box->priv->separator = NULL;
992 993 994 995 996 997 998

          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;
999
          combo_box->priv->box = NULL;
1000 1001 1002 1003
        }
    }
}

1004 1005 1006 1007 1008
static void
gtk_combo_box_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
  GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1009
  GtkTreePath *path;
1010 1011 1012 1013 1014
  gboolean appears_as_list;

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

1015
  if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    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);
1043
      gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (combo_box->priv->cell_view));
1044 1045 1046 1047
    }


  if (appears_as_list)
1048
    gtk_combo_box_list_setup (combo_box); 
1049 1050 1051
  else
    gtk_combo_box_menu_setup (combo_box, TRUE);

1052
  if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1053 1054 1055 1056 1057 1058 1059
    {
      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);
1060 1061
}

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
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;

1072
      if (info && info->cell == cell)
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
        return info;
    }

  return NULL;
}

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

1085 1086
  gtk_combo_box_child_show (menu, user_data);

1087
  combo_box->priv->popup_in_progress = TRUE;
1088 1089
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                TRUE);
1090
  combo_box->priv->popup_in_progress = FALSE;
1091 1092 1093 1094 1095 1096 1097 1098
}

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

1099 1100
  gtk_combo_box_child_hide(menu,user_data);

1101 1102 1103 1104
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                FALSE);
}

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
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);

1116
  g_signal_handlers_disconnect_by_func (menu->toplevel,
1117 1118
					gtk_combo_box_menu_show,
					combo_box);
1119
  g_signal_handlers_disconnect_by_func (menu->toplevel,
1120 1121 1122 1123 1124 1125
					gtk_combo_box_menu_hide,
					combo_box);
  
  combo_box->priv->popup_widget = NULL;
}

1126 1127 1128 1129 1130
static void
gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
                                GtkWidget   *popup)
{
  if (GTK_IS_MENU (combo_box->priv->popup_widget))
1131 1132 1133 1134
    {
      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
      combo_box->priv->popup_widget = NULL;
    }
1135 1136 1137 1138
  else if (combo_box->priv->popup_widget)
    {
      gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
                            combo_box->priv->popup_widget);
1139
      g_object_unref (combo_box->priv->popup_widget);
1140 1141 1142 1143 1144 1145 1146 1147
      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);
1148 1149
          combo_box->priv->popup_window = NULL;
	  combo_box->priv->popup_frame = NULL;
1150 1151 1152 1153
        }

      combo_box->priv->popup_widget = popup;

1154 1155 1156 1157 1158 1159
      /* 
       * 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",
1160
                        G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1161
      g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1162 1163
                        G_CALLBACK (gtk_combo_box_menu_hide), combo_box);

1164 1165 1166
      gtk_menu_attach_to_widget (GTK_MENU (popup),
				 GTK_WIDGET (combo_box),
				 gtk_combo_box_detacher);
1167 1168 1169 1170 1171
    }
  else
    {
      if (!combo_box->priv->popup_window)
        {
1172 1173
	  GtkWidget *toplevel;
	  
1174
          combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1175

1176 1177 1178
	  gtk_window_set_type_hint (GTK_WINDOW (combo_box->priv->popup_window),
				    GDK_WINDOW_TYPE_HINT_COMBO);

1179 1180 1181 1182 1183 1184 1185
	  g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"show",
			    G_CALLBACK (gtk_combo_box_child_show),
			    combo_box);
	  g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"hide",
			    G_CALLBACK (gtk_combo_box_child_hide),
			    combo_box);
  	  
1186
	  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1187
	  if (GTK_IS_WINDOW (toplevel))
1188 1189 1190 1191 1192 1193
	    {
	      gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
					   GTK_WINDOW (combo_box->priv->popup_window));
	      gtk_window_set_transient_for (GTK_WINDOW (combo_box->priv->popup_window),
					    GTK_WINDOW (toplevel));
	    }
1194

1195
	  gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1196 1197 1198
          gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
                                 gtk_widget_get_screen (GTK_WIDGET (combo_box)));

1199 1200
          combo_box->priv->popup_frame = gtk_frame_new (NULL);
          gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
1201
                                     GTK_SHADOW_ETCHED_IN);
1202 1203
          gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
                             combo_box->priv->popup_frame);
1204

1205
          gtk_widget_show (combo_box->priv->popup_frame);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

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

1221
      gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1222
                         popup);
1223

1224
      gtk_widget_show (popup);
1225
      g_object_ref (popup);
1226 1227 1228 1229 1230
      combo_box->priv->popup_widget = popup;
    }
}

static void
1231 1232 1233 1234 1235
gtk_combo_box_menu_position_below (GtkMenu  *menu,
				   gint     *x,
				   gint     *y,
				   gint     *push_in,
				   gpointer  user_data)
1236
{
1237
  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1238 1239 1240
  gint sx, sy;
  GtkWidget *child;
  GtkRequisition req;
1241 1242 1243
  GdkScreen *screen;
  gint monitor_num;
  GdkRectangle monitor;
1244
  
1245
  /* FIXME: is using the size request here broken? */
1246 1247 1248 1249
   child = GTK_BIN (combo_box)->child;
   
   gdk_window_get_origin (child->window, &sx, &sy);
   
1250 1251 1252 1253 1254 1255
   if (GTK_WIDGET_NO_WINDOW (child))
      {
	sx += child->allocation.x;
	sy += child->allocation.y;
      }

1256
   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1257

1258
   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1259 1260 1261
     *x = sx;
   else
     *x = sx + child->allocation.width - req.width;
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
   *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;
  
1274 1275 1276 1277 1278
  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) 
1279 1280 1281 1282
    *y += child->allocation.height;
  else
    *y -= req.height;

1283
   *push_in = FALSE;
1284
}
1285

1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
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);
1308

1309 1310
  gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
  menu_width = requisition.width;
1311

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
  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;
    }
1323

1324 1325
  children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
  while (children)
1326
    {
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
      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;
1339 1340
    }

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  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;

1355 1356 1357
  *push_in = TRUE;
}

1358 1359 1360 1361 1362 1363 1364 1365
static void
gtk_combo_box_menu_position (GtkMenu  *menu,
			     gint     *x,
			     gint     *y,
			     gint     *push_in,
			     gpointer  user_data)
{
  GtkComboBox *combo_box;
1366
  GtkWidget *menu_item;
1367 1368 1369 1370 1371 1372

  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
1373
    {
1374
      /* FIXME handle nested menus better */
1375 1376 1377 1378 1379 1380 1381
      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);
    }
1382 1383 1384

}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
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;
1396
  GtkRequisition popup_req;
1397
  GtkPolicyType hpolicy, vpolicy;
1398 1399 1400 1401 1402
  
  sample = GTK_BIN (combo_box)->child;

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

1403 1404 1405 1406 1407 1408 1409 1410
  if (GTK_WIDGET_NO_WINDOW (sample))
    {
      *x += sample->allocation.x;
      *y += sample->allocation.y;
    }
  
  *width = sample->allocation.width;
  
1411
  if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1412 1413 1414 1415 1416 1417 1418
    {
       *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);
    }

1419 1420 1421 1422 1423 1424
  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)
1425
    {
1426 1427 1428 1429
      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);
1430
    }
1431 1432 1433

  *height = popup_req.height;

1434 1435 1436 1437
  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);
1438

1439 1440 1441 1442
  if (*x < monitor.x)
    *x = monitor.x;
  else if (*x + *width > monitor.x + monitor.width)
    *x = monitor.x + monitor.width - *width;
1443 1444 1445
  
  if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
    *y += sample->allocation.height;
1446
  else if (*y - *height >= monitor.y)
1447
    *y -= *height;
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
  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);
    }
1466 1467
} 

1468
static gboolean
1469
cell_view_is_sensitive (GtkCellView *cell_view)
1470 1471 1472
{
  GList *cells, *list;
  gboolean sensitive;
1473
  
1474
  cells = gtk_cell_view_get_cell_renderers (cell_view);
1475 1476 1477 1478 1479

  sensitive = FALSE;
  list = cells;
  while (list)
    {
1480
      g_object_get (list->data, "sensitive", &sensitive, NULL);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
      
      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;

1502
  if (combo_box->priv->row_separator_func)
1503
    {
1504 1505
      if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
						  combo_box->priv->row_separator_data))
1506 1507 1508
	return FALSE;
    }

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
  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)
    {
1519
      g_object_get (list->data, "sensitive", &sensitive, NULL);
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
      
      if (sensitive)
	break;

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

  return sensitive;
}
1530 1531 1532 1533

static void
update_menu_sensitivity (GtkComboBox *combo_box,
			 GtkWidget   *menu)
1534
{
1535 1536 1537
  GList *children, *child;
  GtkWidget *item, *submenu, *separator;
  GtkWidget *cell_view;
1538 1539 1540
  gboolean sensitive;

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

1543
  children = gtk_container_get_children (GTK_CONTAINER (menu));
1544

1545
  for (child = children; child; child = child->next)
1546
    {
1547 1548
      item = GTK_WIDGET (child->data);
      cell_view = GTK_BIN (item)->child;
1549

1550 1551 1552 1553 1554 1555 1556 1557 1558
      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);
	}
1559
      else
1560 1561
	{
	  sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1562

1563 1564 1565
	  if (menu != combo_box->priv->popup_widget && child == children)
	    {
	      separator = GTK_WIDGET (child->next->data);
1566 1567
	      g_object_set (item, "visible", sensitive, NULL);
	      g_object_set (separator, "visible", sensitive, NULL);
1568 1569 1570 1571
	    }
	  else
	    gtk_widget_set_sensitive (item, sensitive);
	}
1572 1573
    }

1574
  g_list_free (children);
1575 1576
}