]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcombobox.c
Don't unref model if it is NULL. (#139770)
[~andy/gtk] / gtk / gtkcombobox.c
index ab2b9ea99624201be7fb80aa7090b27a404df504..79942ee42c0f415e12452bcfb3941534aa0674d5 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+#include <config.h>
 #include "gtkcombobox.h"
+
+#include "gtkarrow.h"
+#include "gtkbindings.h"
 #include "gtkcelllayout.h"
+#include "gtkcellrenderertext.h"
 #include "gtkcellview.h"
 #include "gtkcellviewmenuitem.h"
-
-#include "gtktreeselection.h"
+#include "gtkeventbox.h"
 #include "gtkframe.h"
+#include "gtkliststore.h"
+#include "gtkmain.h"
+#include "gtkmenu.h"
 #include "gtktogglebutton.h"
+#include "gtktreeselection.h"
 #include "gtkvseparator.h"
-#include "gtkarrow.h"
-#include "gtkmenu.h"
-#include "gtkmain.h"
-#include "gtkeventbox.h"
-#include "gtkcellrenderertext.h"
-#include "gtkbindings.h"
-#include "gtkliststore.h"
 #include "gtkwindow.h"
 
 #include <gdk/gdkkeysyms.h>
@@ -48,7 +49,6 @@
 
 /* WELCOME, to THE house of evil code */
 
-
 typedef struct _ComboCellInfo ComboCellInfo;
 struct _ComboCellInfo
 {
@@ -80,8 +80,6 @@ struct _GtkComboBoxPrivate
   GtkTreeViewColumn *column;
 
   GtkWidget *cell_view;
-
-  GtkWidget *hbox;
   GtkWidget *cell_view_frame;
 
   GtkWidget *button;
@@ -94,15 +92,70 @@ struct _GtkComboBoxPrivate
 
   guint inserted_id;
   guint deleted_id;
+  guint reordered_id;
+  guint changed_id;
 
   gint width;
   GSList *cells;
 
-  guint changed_id;
-
   guint popup_in_progress : 1;
+  guint destroying : 1;
 };
 
+/* 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
+ *
+ * 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
+ *
+ * 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
+ *
+ * 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
+ * 
+ */
+
 enum {
   CHANGED,
   LAST_SIGNAL
@@ -127,6 +180,8 @@ static guint combo_box_signals[LAST_SIGNAL] = {0,};
 static void     gtk_combo_box_class_init           (GtkComboBoxClass *klass);
 static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
 static void     gtk_combo_box_init                 (GtkComboBox      *combo_box);
+static void     gtk_combo_box_finalize             (GObject          *object);
+static void     gtk_combo_box_destroy              (GtkObject        *object);
 
 static void     gtk_combo_box_set_property         (GObject         *object,
                                                     guint            prop_id,
@@ -137,13 +192,16 @@ static void     gtk_combo_box_get_property         (GObject         *object,
                                                     GValue          *value,
                                                     GParamSpec      *spec);
 
+static void     gtk_combo_box_state_changed        (GtkWidget        *widget,
+                                                   GtkStateType      previous);
 static void     gtk_combo_box_style_set            (GtkWidget       *widget,
-                                                    GtkStyle        *previous_style,
-                                                    gpointer         data);
+                                                    GtkStyle        *previous);
 static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
                                                     gpointer         data);
 static void     gtk_combo_box_add                  (GtkContainer    *container,
                                                     GtkWidget       *widget);
+static void     gtk_combo_box_remove               (GtkContainer    *container,
+                                                    GtkWidget       *widget);
 
 static ComboCellInfo *gtk_combo_box_get_cell_info  (GtkComboBox      *combo_box,
                                                     GtkCellRenderer  *cell);
@@ -155,20 +213,27 @@ static void     gtk_combo_box_menu_hide            (GtkWidget        *menu,
 
 static void     gtk_combo_box_set_popup_widget     (GtkComboBox      *combo_box,
                                                     GtkWidget        *popup);
+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);
 static void     gtk_combo_box_menu_position        (GtkMenu          *menu,
                                                     gint             *x,
                                                     gint             *y,
                                                     gint             *push_in,
                                                     gpointer          user_data);
-static void     gtk_combo_box_popup                (GtkComboBox      *combo_box);
-static void     gtk_combo_box_popdown              (GtkComboBox      *combo_box);
 
 static gint     gtk_combo_box_calc_requested_width (GtkComboBox      *combo_box,
                                                     GtkTreePath      *path);
 static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
 
 static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);
-static void     gtk_combo_box_set_model_internal   (GtkComboBox      *combo_box);
 
 static void     gtk_combo_box_size_request         (GtkWidget        *widget,
                                                     GtkRequisition   *requisition);
@@ -182,11 +247,41 @@ static gboolean gtk_combo_box_expose_event         (GtkWidget        *widget,
                                                     GdkEventExpose   *event);
 static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
                                                     GdkEventScroll   *event);
+static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
+                                                   gint              index);
+static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
+                                                   GdkEventKey      *event,
+                                                   gpointer          data);
+
+/* 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);
 
 /* list */
+static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
+                                                   gint             *x, 
+                                                   gint             *y, 
+                                                   gint             *width,
+                                                   gint             *height);
 static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
 static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);
 
+static void     gtk_combo_box_list_remove_grabs    (GtkComboBox      *combo_box);
+
 static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
                                                     GdkEventButton   *event,
                                                     gpointer          data);
@@ -204,7 +299,7 @@ static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
 
 /* menu */
 static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
-                                                    gboolean          add_childs);
+                                                    gboolean          add_children);
 static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
 static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);
 
@@ -228,10 +323,18 @@ static void     gtk_combo_box_menu_row_inserted    (GtkTreeModel     *model,
 static void     gtk_combo_box_menu_row_deleted     (GtkTreeModel     *model,
                                                     GtkTreePath      *path,
                                                     gpointer          user_data);
+static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
+                                                   GtkTreePath      *path,
+                                                   GtkTreeIter      *iter,
+                                                   gint             *new_order,
+                                                   gpointer          user_data);
 static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
                                                     GtkTreePath      *path,
                                                     GtkTreeIter      *iter,
                                                     gpointer          data);
+static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
+                                                   GdkEventKey      *event,
+                                                   gpointer          data);
 
 /* cell layout */
 static void     gtk_combo_box_cell_layout_pack_start         (GtkCellLayout         *layout,
@@ -255,7 +358,11 @@ static void     gtk_combo_box_cell_layout_clear_attributes   (GtkCellLayout
 static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
                                                               GtkCellRenderer       *cell,
                                                               gint                   position);
+static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
+                                                             gboolean      group_cycling);
 
+static void     cell_view_sync_cells (GtkComboBox *combo_box,
+                                      GtkCellView *cell_view);
 
 GType
 gtk_combo_box_get_type (void)
@@ -303,6 +410,7 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
 {
   GObjectClass *object_class;
   GtkBindingSet *binding_set;
+  GtkObjectClass *gtk_object_class;
   GtkContainerClass *container_class;
   GtkWidgetClass *widget_class;
 
@@ -311,14 +419,22 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
   container_class = (GtkContainerClass *)klass;
   container_class->forall = gtk_combo_box_forall;
   container_class->add = gtk_combo_box_add;
+  container_class->remove = gtk_combo_box_remove;
 
   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;
   widget_class->scroll_event = gtk_combo_box_scroll_event;
+  widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
+  widget_class->style_set = gtk_combo_box_style_set;
+  widget_class->state_changed = gtk_combo_box_state_changed;
+
+  gtk_object_class = (GtkObjectClass *)klass;
+  gtk_object_class->destroy = gtk_combo_box_destroy;
 
   object_class = (GObjectClass *)klass;
+  object_class->finalize = gtk_combo_box_finalize;
   object_class->set_property = gtk_combo_box_set_property;
   object_class->get_property = gtk_combo_box_get_property;
 
@@ -338,16 +454,16 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
   g_object_class_install_property (object_class,
                                    PROP_MODEL,
                                    g_param_spec_object ("model",
-                                                        _("ComboBox model"),
-                                                        _("The model for the combo box"),
+                                                        P_("ComboBox model"),
+                                                        P_("The model for the combo box"),
                                                         GTK_TYPE_TREE_MODEL,
                                                         G_PARAM_READWRITE));
 
   g_object_class_install_property (object_class,
                                    PROP_WRAP_WIDTH,
                                    g_param_spec_int ("wrap_width",
-                                                     _("Wrap width"),
-                                                     _("Wrap width for layouting the items in a grid"),
+                                                     P_("Wrap width"),
+                                                     P_("Wrap width for layouting the items in a grid"),
                                                      0,
                                                      G_MAXINT,
                                                      0,
@@ -356,8 +472,8 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
   g_object_class_install_property (object_class,
                                    PROP_ROW_SPAN_COLUMN,
                                    g_param_spec_int ("row_span_column",
-                                                     _("Row span column"),
-                                                     _("TreeModel column containing the row span values"),
+                                                     P_("Row span column"),
+                                                     P_("TreeModel column containing the row span values"),
                                                      0,
                                                      G_MAXINT,
                                                      0,
@@ -366,8 +482,8 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
   g_object_class_install_property (object_class,
                                    PROP_COLUMN_SPAN_COLUMN,
                                    g_param_spec_int ("column_span_column",
-                                                     _("Column span column"),
-                                                     _("TreeModel column containing the column span values"),
+                                                     P_("Column span column"),
+                                                     P_("TreeModel column containing the column span values"),
                                                      0,
                                                      G_MAXINT,
                                                      0,
@@ -376,17 +492,17 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
   g_object_class_install_property (object_class,
                                    PROP_ACTIVE,
                                    g_param_spec_int ("active",
-                                                     _("Active item"),
-                                                     _("The item which is currently active"),
+                                                     P_("Active item"),
+                                                     P_("The item which is currently active"),
                                                      0,
                                                      G_MAXINT,
                                                      0,
                                                      G_PARAM_READWRITE));
 
   gtk_widget_class_install_style_property (widget_class,
-                                           g_param_spec_boolean ("appearance",
-                                                                 _("ComboBox appareance"),
-                                                                 _("ComboBox appearance, where TRUE means Windows-style."),
+                                           g_param_spec_boolean ("appears-as-list",
+                                                                 P_("Appears as list"),
+                                                                 P_("Whether combobox dropdowns should look like lists rather than menus"),
                                                                  FALSE,
                                                                  G_PARAM_READWRITE));
 
@@ -410,11 +526,9 @@ gtk_combo_box_init (GtkComboBox *combo_box)
 {
   combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
 
-  g_signal_connect (combo_box, "style_set",
-                    G_CALLBACK (gtk_combo_box_style_set), NULL);
-
   combo_box->priv->cell_view = gtk_cell_view_new ();
-  gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
+  gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
+  GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
   gtk_widget_show (combo_box->priv->cell_view);
 
   combo_box->priv->width = 0;
@@ -497,40 +611,71 @@ gtk_combo_box_get_property (GObject    *object,
 }
 
 static void
-gtk_combo_box_style_set (GtkWidget *widget,
-                         GtkStyle  *previous_style,
-                         gpointer   data)
+gtk_combo_box_state_changed (GtkWidget    *widget,
+                            GtkStateType  previous)
 {
-  gboolean appearance;
   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
 
-  gtk_widget_queue_resize (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);
+}
+
+static void
+gtk_combo_box_check_appearance (GtkComboBox *combo_box)
+{
+  gboolean appears_as_list;
 
   /* if wrap_width > 0, then we are in grid-mode and forced to use
    * unix style
    */
   if (combo_box->priv->wrap_width)
-    return;
-
-  gtk_widget_style_get (widget,
-                        "appearance", &appearance,
-                        NULL);
+    appears_as_list = FALSE;
+  else
+    gtk_widget_style_get (GTK_WIDGET (combo_box),
+                         "appears-as-list", &appears_as_list,
+                         NULL);
 
-  /* TRUE is windows style */
-  if (appearance)
+  if (appears_as_list)
     {
+      /* Destroy all the menu mode widgets, if they exist. */
       if (GTK_IS_MENU (combo_box->priv->popup_widget))
-        gtk_combo_box_menu_destroy (combo_box);
-      gtk_combo_box_list_setup (combo_box);
+       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);
     }
   else
     {
+      /* Destroy all the list mode widgets, if they exist. */
       if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
-        gtk_combo_box_list_destroy (combo_box);
-      gtk_combo_box_menu_setup (combo_box, TRUE);
+       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);
     }
 }
 
+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);
+
+  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)]);
+}
+
 static void
 gtk_combo_box_button_toggled (GtkWidget *widget,
                               gpointer   data)
@@ -554,13 +699,13 @@ gtk_combo_box_add (GtkContainer *container,
 
   if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
     {
-      gtk_container_remove (container, combo_box->priv->cell_view);
-      (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
-    }
-  else
-    {
-      (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
+      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;
 
   if (combo_box->priv->cell_view &&
       widget != combo_box->priv->cell_view)
@@ -571,6 +716,7 @@ gtk_combo_box_add (GtkContainer *container,
       if (!combo_box->priv->tree_view && combo_box->priv->separator)
         {
           gtk_widget_unparent (combo_box->priv->separator);
+         combo_box->priv->separator = NULL;
 
           g_object_ref (G_OBJECT (combo_box->priv->arrow));
           gtk_widget_unparent (combo_box->priv->arrow);
@@ -588,6 +734,56 @@ gtk_combo_box_add (GtkContainer *container,
     }
 }
 
+static void
+gtk_combo_box_remove (GtkContainer *container,
+                     GtkWidget    *widget)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (container);
+  gboolean appears_as_list;
+
+  gtk_widget_unparent (widget);
+  GTK_BIN (container)->child = NULL;
+
+  if (combo_box->priv->destroying)
+    return;
+
+  gtk_widget_queue_resize (GTK_WIDGET (container));
+
+  if (!combo_box->priv->tree_view)
+    appears_as_list = FALSE;
+  else
+    appears_as_list = TRUE;
+  
+  if (appears_as_list)
+    gtk_combo_box_list_destroy (combo_box);
+  else if (GTK_IS_MENU (combo_box->priv->popup_widget))
+    {
+      gtk_combo_box_menu_destroy (combo_box);
+      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
+      combo_box->priv->popup_widget = NULL;
+    }
+
+  if (!combo_box->priv->cell_view)
+    {
+      combo_box->priv->cell_view = gtk_cell_view_new ();
+      gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
+      GTK_BIN (container)->child = combo_box->priv->cell_view;
+      
+      gtk_widget_show (combo_box->priv->cell_view);
+      gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
+                              combo_box->priv->model);
+      cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
+    }
+
+
+  if (appears_as_list)
+    gtk_combo_box_list_setup (combo_box);
+  else
+    gtk_combo_box_menu_setup (combo_box, TRUE);
+
+  gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
+}
+
 static ComboCellInfo *
 gtk_combo_box_get_cell_info (GtkComboBox     *combo_box,
                              GtkCellRenderer *cell)
@@ -598,7 +794,7 @@ gtk_combo_box_get_cell_info (GtkComboBox     *combo_box,
     {
       ComboCellInfo *info = (ComboCellInfo *)i->data;
 
-      if (info->cell == cell)
+      if (info && info->cell == cell)
         return info;
     }
 
@@ -626,12 +822,36 @@ gtk_combo_box_menu_hide (GtkWidget *menu,
                                 FALSE);
 }
 
+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);
+
+  g_signal_handlers_disconnect_by_func (menu,
+                                       gtk_combo_box_menu_show,
+                                       combo_box);
+  g_signal_handlers_disconnect_by_func (menu,
+                                       gtk_combo_box_menu_hide,
+                                       combo_box);
+  
+  combo_box->priv->popup_widget = NULL;
+}
+
 static void
 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
                                 GtkWidget   *popup)
 {
   if (GTK_IS_MENU (combo_box->priv->popup_widget))
-    combo_box->priv->popup_widget = NULL;
+    {
+      gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
+      combo_box->priv->popup_widget = NULL;
+    }
   else if (combo_box->priv->popup_widget)
     {
       gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
@@ -645,7 +865,8 @@ gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
       if (combo_box->priv->popup_window)
         {
           gtk_widget_destroy (combo_box->priv->popup_window);
-          combo_box->priv->popup_window = combo_box->priv->popup_frame = NULL;
+          combo_box->priv->popup_window = NULL;
+         combo_box->priv->popup_frame = NULL;
         }
 
       combo_box->priv->popup_widget = popup;
@@ -655,19 +876,25 @@ gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
       g_signal_connect (popup, "hide",
                         G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
 
-      /* FIXME: need to attach to widget? */
+      gtk_menu_attach_to_widget (GTK_MENU (popup),
+                                GTK_WIDGET (combo_box),
+                                gtk_combo_box_detacher);
     }
   else
     {
       if (!combo_box->priv->popup_window)
         {
           combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
+         gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
+          gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
+                                 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
 
           combo_box->priv->popup_frame = gtk_frame_new (NULL);
           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
-                                     GTK_SHADOW_NONE);
+                                     GTK_SHADOW_ETCHED_IN);
           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
                              combo_box->priv->popup_frame);
+
           gtk_widget_show (combo_box->priv->popup_frame);
         }
 
@@ -680,98 +907,229 @@ gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
 }
 
 static void
-gtk_combo_box_menu_position (GtkMenu  *menu,
-                             gint     *x,
-                             gint     *y,
-                             gint     *push_in,
-                             gpointer  user_data)
+gtk_combo_box_menu_position_below (GtkMenu  *menu,
+                                  gint     *x,
+                                  gint     *y,
+                                  gint     *push_in,
+                                  gpointer  user_data)
 {
   gint sx, sy;
   GtkWidget *child;
   GtkRequisition req;
   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
-
+  
   /* FIXME: is using the size request here broken? */
-  child = GTK_BIN (combo_box)->child;
+   child = GTK_BIN (combo_box)->child;
+   
+   gdk_window_get_origin (child->window, &sx, &sy);
+   
+   gtk_widget_size_request (GTK_WIDGET (menu), &req);
+   
+   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
+     *x = sx;
+   else
+     *x = sx + child->allocation.width - req.width;
+   *y = sy + child->allocation.height;
+   
+   if (GTK_WIDGET_NO_WINDOW (child))
+      {
+       *x += child->allocation.x;
+       *y += child->allocation.y;
+      }
+   
+   *push_in = TRUE;
+}
+
+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);
 
-  gdk_window_get_origin (child->window, &sx, &sy);
+  gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
+  menu_width = requisition.width;
 
-  gtk_widget_size_request (GTK_WIDGET (menu), &req);
+  active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
+  gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
 
-  if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
-    *x = sx;
-  else
-    *x = sx + child->allocation.width - req.width;
-  *y = sy + child->allocation.height;
+  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;
+    }
 
-  if (GTK_WIDGET_NO_WINDOW (child))
+  children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
+  while (children)
     {
-      *x += child->allocation.x;
-      *y += child->allocation.y;
+      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;
     }
 
+  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;
+
   *push_in = TRUE;
 }
 
 static void
-gtk_combo_box_popup (GtkComboBox *combo_box)
+gtk_combo_box_menu_position (GtkMenu  *menu,
+                            gint     *x,
+                            gint     *y,
+                            gint     *push_in,
+                            gpointer  user_data)
 {
-  gint x, y, width, height;
-  GtkWidget *sample;
+  GtkComboBox *combo_box;
+  GtkWidget *menu_item;
 
-  if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
-    return;
+  combo_box = GTK_COMBO_BOX (user_data);
 
-  if (GTK_IS_MENU (combo_box->priv->popup_widget))
+  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
     {
-      if (combo_box->priv->active_item != -1)
-        {
-          GList *childs;
-
-          childs = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
-          gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
-                                      g_list_nth_data (childs, combo_box->priv->active_item));
-          g_list_free (childs);
-        }
+      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_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
-                      NULL, NULL,
-                      gtk_combo_box_menu_position, combo_box,
-                      0, 0);
-      return;
+      gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
     }
 
-  /* size it */
+}
+
+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;
+  GtkRequisition popup_req;
+  
   sample = GTK_BIN (combo_box)->child;
 
-  width = sample->allocation.width;
-  height = sample->allocation.height;
+  *width = sample->allocation.width;
+  gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
+  *height = popup_req.height;
 
-  gdk_window_get_origin (sample->window,
-                         &x, &y);
+  gdk_window_get_origin (sample->window, x, y);
 
   if (combo_box->priv->cell_view_frame)
     {
-       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 +
+       *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);
     }
 
-  gtk_widget_set_size_request (combo_box->priv->popup_window,
-                               width, -1);
-
   if (GTK_WIDGET_NO_WINDOW (sample))
     {
-      x += sample->allocation.x;
-      y += sample->allocation.y;
+      *x += sample->allocation.x;
+      *y += sample->allocation.y;
+    }
+  
+  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 + *width > monitor.x + monitor.width)
+    *x = monitor.x + monitor.width - *width;
+  
+  if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
+    *y += sample->allocation.height;
+  else
+    *y -= *height;
+} 
+
+/**
+ * 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
+gtk_combo_box_popup (GtkComboBox *combo_box)
+{
+  gint x, y, width, height;
+  
+  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+
+  if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
+    return;
+
+  if (GTK_IS_MENU (combo_box->priv->popup_widget))
+    {
+      gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
+                          combo_box->priv->active_item);
+
+      gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
+                     NULL, NULL,
+                     gtk_combo_box_menu_position, combo_box,
+                     0, 0);
+      return;
     }
 
-  gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window),
-                   x, y + height);
+  gtk_widget_show_all (combo_box->priv->popup_frame);
+  gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
+
+  gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);  
+  gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
 
   /* popup */
-  gtk_widget_show_all (combo_box->priv->popup_window);
+  gtk_widget_show (combo_box->priv->popup_window);
 
   gtk_widget_grab_focus (combo_box->priv->popup_window);
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
@@ -783,17 +1141,40 @@ gtk_combo_box_popup (GtkComboBox *combo_box)
                          FALSE, GDK_CURRENT_TIME);
       gtk_widget_grab_focus (combo_box->priv->tree_view);
     }
+
+  gtk_grab_add (combo_box->priv->popup_window);
+  gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
+                    GDK_BUTTON_PRESS_MASK |
+                    GDK_BUTTON_RELEASE_MASK |
+                    GDK_POINTER_MOTION_MASK,
+                    NULL, NULL, GDK_CURRENT_TIME);
+
+  gtk_grab_add (combo_box->priv->tree_view);
 }
 
-static void
+/**
+ * gtk_combo_box_popdown:
+ * @combo_box: a #GtkComboBox
+ * 
+ * Hides the menu or dropdown list of @combo_box.
+ *
+ * This function is mostly intended for use by accessibility technologies;
+ * applications should have little use for it.
+ *
+ * Since: 2.4
+ **/
+void
 gtk_combo_box_popdown (GtkComboBox *combo_box)
 {
+  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+
   if (GTK_IS_MENU (combo_box->priv->popup_widget))
     {
       gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
       return;
     }
 
+  gtk_combo_box_list_remove_grabs (combo_box);
   gtk_widget_hide_all (combo_box->priv->popup_window);
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                 FALSE);
@@ -854,7 +1235,7 @@ gtk_combo_box_remeasure (GtkComboBox *combo_box)
       GtkRequisition req;
 
       if (combo_box->priv->cell_view)
-        gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
+       gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
                                        path, &req);
       else
         req.width = 0;
@@ -889,9 +1270,10 @@ gtk_combo_box_size_request (GtkWidget      *widget,
 
       if (combo_box->priv->cell_view)
         {
-          GtkRequisition sep_req, arrow_req;
+          GtkRequisition button_req, sep_req, arrow_req;
           gint border_width, xthickness, ythickness;
 
+          gtk_widget_size_request (combo_box->priv->button, &button_req);
           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
           xthickness = combo_box->priv->button->style->xthickness;
           ythickness = combo_box->priv->button->style->ythickness;
@@ -906,8 +1288,8 @@ gtk_combo_box_size_request (GtkWidget      *widget,
 
           width = bin_req.width + sep_req.width + arrow_req.width;
 
-          height += border_width + 1 + xthickness * 2 + 4;
-          width += border_width + 1 + ythickness * 2 + 4;
+          height += border_width + 1 + ythickness * 2 + 4;
+          width += border_width + 1 + xthickness * 2 + 4;
 
           requisition->width = width;
           requisition->height = height;
@@ -925,13 +1307,14 @@ gtk_combo_box_size_request (GtkWidget      *widget,
   else
     {
       /* list mode */
-      GtkRequisition button_req;
+      GtkRequisition button_req, frame_req;
 
       /* sample + frame */
       *requisition = bin_req;
 
       if (combo_box->priv->cell_view_frame)
         {
+         gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
           requisition->width += 2 *
             (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
@@ -979,7 +1362,7 @@ gtk_combo_box_size_allocate (GtkWidget     *widget,
 
           width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
 
-          /* handle the childs */
+          /* handle the children */
           gtk_widget_size_request (combo_box->priv->arrow, &req);
           child.width = req.width;
           child.height = allocation->height - 2 * (child.y - allocation->y);
@@ -1039,7 +1422,7 @@ gtk_combo_box_size_allocate (GtkWidget     *widget,
       if (is_rtl)
         child.x = allocation->x;
       else
-        child.x += allocation->x + allocation->width - req.width;
+        child.x = allocation->x + allocation->width - req.width;
       child.y = allocation->y;
       child.width = req.width;
       child.height = allocation->height;
@@ -1080,62 +1463,30 @@ gtk_combo_box_size_allocate (GtkWidget     *widget,
 static void
 gtk_combo_box_unset_model (GtkComboBox *combo_box)
 {
-  if (!combo_box->priv->tree_view)
+  if (combo_box->priv->model)
     {
-      /* menu mode */
       g_signal_handler_disconnect (combo_box->priv->model,
-                                   combo_box->priv->inserted_id);
+                                  combo_box->priv->inserted_id);
       g_signal_handler_disconnect (combo_box->priv->model,
-                                   combo_box->priv->deleted_id);
+                                  combo_box->priv->deleted_id);
       g_signal_handler_disconnect (combo_box->priv->model,
-                                   combo_box->priv->changed_id);
-
-      combo_box->priv->inserted_id =
-      combo_box->priv->deleted_id =
-      combo_box->priv->changed_id = -1;
-
-      if (combo_box->priv->popup_widget)
-        gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
-                               (GtkCallback)gtk_widget_destroy, NULL);
-    }
-  else
-    {
-      /* list mode */
+                                  combo_box->priv->reordered_id);
       g_signal_handler_disconnect (combo_box->priv->model,
-                                   combo_box->priv->changed_id);
-      combo_box->priv->changed_id = -1;
+                                  combo_box->priv->changed_id);
     }
-}
 
-static void
-gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
-{
+  /* menu mode */
   if (!combo_box->priv->tree_view)
     {
-      /* menu mode */
-      combo_box->priv->inserted_id =
-        g_signal_connect (combo_box->priv->model, "row_inserted",
-                          G_CALLBACK (gtk_combo_box_menu_row_inserted),
-                          combo_box);
-      combo_box->priv->deleted_id =
-        g_signal_connect (combo_box->priv->model, "row_deleted",
-                          G_CALLBACK (gtk_combo_box_menu_row_deleted),
-                          combo_box);
-      combo_box->priv->changed_id =
-        g_signal_connect (combo_box->priv->model, "row_changed",
-                          G_CALLBACK (gtk_combo_box_menu_row_changed),
-                          combo_box);
+      if (combo_box->priv->popup_widget)
+        gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
+                               (GtkCallback)gtk_widget_destroy, NULL);
     }
-  else
-    {
-      /* list mode */
-      gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
-                               combo_box->priv->model);
 
-      combo_box->priv->changed_id =
-        g_signal_connect (combo_box->priv->model, "row_changed",
-                          G_CALLBACK (gtk_combo_box_list_row_changed),
-                          combo_box);
+  if (combo_box->priv->model)
+    {
+      g_object_unref (G_OBJECT (combo_box->priv->model));
+      combo_box->priv->model = NULL;
     }
 }
 
@@ -1149,26 +1500,14 @@ gtk_combo_box_forall (GtkContainer *container,
 
   if (include_internals)
     {
-      if (!combo_box->priv->tree_view)
-        {
-          if (combo_box->priv->cell_view && combo_box->priv->button)
-            {
-              (* callback) (combo_box->priv->button, callback_data);
-              (* callback) (combo_box->priv->separator, callback_data);
-              (* callback) (combo_box->priv->arrow, callback_data);
-            }
-          else if (combo_box->priv->arrow)
-            {
-              (* callback) (combo_box->priv->button, callback_data);
-              (* callback) (combo_box->priv->arrow, callback_data);
-            }
-        }
-      else
-        {
-          (* callback) (combo_box->priv->button, callback_data);
-          if (combo_box->priv->cell_view_frame)
-            (* callback) (combo_box->priv->cell_view_frame, callback_data);
-        }
+      if (combo_box->priv->button)
+       (* callback) (combo_box->priv->button, callback_data);
+      if (combo_box->priv->separator)
+       (* callback) (combo_box->priv->separator, callback_data);
+      if (combo_box->priv->arrow)
+       (* callback) (combo_box->priv->arrow, callback_data);
+      if (combo_box->priv->cell_view_frame)
+       (* callback) (combo_box->priv->cell_view_frame, callback_data);
     }
 
   if (GTK_BIN (container)->child)
@@ -1275,7 +1614,7 @@ cell_view_sync_cells (GtkComboBox *combo_box,
 
 static void
 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
-                          gboolean     add_childs)
+                          gboolean     add_children)
 {
   GtkWidget *box;
 
@@ -1284,6 +1623,8 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box,
       combo_box->priv->button = gtk_toggle_button_new ();
       g_signal_connect (combo_box->priv->button, "toggled",
                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
+      g_signal_connect_after (combo_box->priv->button, "key_press_event",
+                             G_CALLBACK (gtk_combo_box_key_press), combo_box);
       gtk_widget_set_parent (combo_box->priv->button,
                              GTK_BIN (combo_box)->child->parent);
 
@@ -1310,6 +1651,8 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box,
       combo_box->priv->button = gtk_toggle_button_new ();
       g_signal_connect (combo_box->priv->button, "toggled",
                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
+      g_signal_connect_after (combo_box, "key_press_event",
+                             G_CALLBACK (gtk_combo_box_key_press), combo_box);
       gtk_widget_set_parent (combo_box->priv->button,
                              GTK_BIN (combo_box)->child->parent);
 
@@ -1325,11 +1668,14 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box,
 
   /* create our funky menu */
   box = gtk_menu_new ();
+  g_signal_connect (box, "key_press_event",
+                   G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
   gtk_combo_box_set_popup_widget (combo_box, box);
 
   /* add items */
-  if (add_childs)
+  if (add_children)
     gtk_combo_box_menu_fill (combo_box);
+
 }
 
 static void
@@ -1360,6 +1706,10 @@ gtk_combo_box_menu_fill (GtkComboBox *combo_box)
                             GTK_CELL_VIEW (GTK_BIN (tmp)->child));
 
       gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
+
+      if (combo_box->priv->wrap_width)
+        gtk_combo_box_relayout_item (combo_box, i);
+
       gtk_widget_show (tmp);
 
       gtk_tree_path_free (path);
@@ -1369,9 +1719,6 @@ gtk_combo_box_menu_fill (GtkComboBox *combo_box)
 static void
 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
 {
-  /* disconnect signal handlers */
-  gtk_combo_box_unset_model (combo_box);
-
   g_signal_handlers_disconnect_matched (combo_box->priv->button,
                                         G_SIGNAL_MATCH_DATA,
                                         0, 0, NULL,
@@ -1381,16 +1728,24 @@ gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
   if (combo_box->priv->cell_view)
     {
       gtk_widget_unparent (combo_box->priv->arrow);
+      combo_box->priv->arrow = NULL;
+
       gtk_widget_unparent (combo_box->priv->separator);
+      combo_box->priv->separator = NULL;
+
       gtk_widget_unparent (combo_box->priv->button);
+      combo_box->priv->button = NULL;
     }
   else
     {
       /* will destroy the arrow too */
       gtk_widget_unparent (combo_box->priv->button);
+
+      combo_box->priv->button = NULL;
+      combo_box->priv->arrow = NULL;
     }
 
-  /* changing the popup window will unref the menu and the childs */
+  /* changing the popup window will unref the menu and the children */
 }
 
 /*
@@ -1484,6 +1839,7 @@ gtk_combo_box_relayout_item (GtkComboBox *combo_box,
 
   list = gtk_container_get_children (GTK_CONTAINER (menu));
   item = g_list_nth_data (list, index);
+  g_list_free (list);
 
   gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
 
@@ -1513,22 +1869,15 @@ gtk_combo_box_relayout_item (GtkComboBox *combo_box,
 static void
 gtk_combo_box_relayout (GtkComboBox *combo_box)
 {
-  gint i, items;
   GList *list, *j;
   GtkWidget *menu;
 
-  /* ensure we are in menu style */
+  /* do nothing unless we are in menu style */
   if (combo_box->priv->tree_view)
-    gtk_combo_box_list_destroy (combo_box);
+    return;
 
   menu = combo_box->priv->popup_widget;
 
-  if (!GTK_IS_MENU_SHELL (menu))
-    {
-      gtk_combo_box_menu_setup (combo_box, FALSE);
-      menu = combo_box->priv->popup_widget;
-    }
-
   /* get rid of all children */
   g_return_if_fail (GTK_IS_MENU_SHELL (menu));
 
@@ -1537,35 +1886,10 @@ gtk_combo_box_relayout (GtkComboBox *combo_box)
   for (j = g_list_last (list); j; j = j->prev)
     gtk_container_remove (GTK_CONTAINER (menu), j->data);
 
-  g_list_free (j);
+  g_list_free (list);
 
   /* and relayout */
-  items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
-
-  for (i = 0; i < items; i++)
-    {
-      GtkWidget *tmp;
-      GtkTreePath *path;
-
-      path = gtk_tree_path_new_from_indices (i, -1);
-      tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
-                                                    path);
-
-      g_signal_connect (tmp, "activate",
-                        G_CALLBACK (gtk_combo_box_menu_item_activate),
-                        combo_box);
-
-      cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
-
-      gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
-
-      if (combo_box->priv->wrap_width)
-        gtk_combo_box_relayout_item (combo_box, i);
-
-      gtk_widget_show (tmp);
-
-      gtk_tree_path_free (path);
-    }
+  gtk_combo_box_menu_fill (combo_box);
 }
 
 /* callbacks */
@@ -1582,6 +1906,10 @@ gtk_combo_box_menu_button_press (GtkWidget      *widget,
   if (event->type == GDK_BUTTON_PRESS && event->button == 1)
     {
       combo_box->priv->popup_in_progress = TRUE;
+      
+      gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
+                          combo_box->priv->active_item);
+
       gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
                       NULL, NULL,
                       gtk_combo_box_menu_position, combo_box,
@@ -1609,6 +1937,90 @@ gtk_combo_box_menu_item_activate (GtkWidget *item,
   gtk_combo_box_set_active (combo_box, index);
 }
 
+static void
+gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
+                                 GtkTreePath      *path,
+                                 GtkTreeIter      *iter,
+                                 gpointer          user_data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+  gint index = gtk_tree_path_get_indices (path)[0];
+
+  if (combo_box->priv->active_item >= index)
+    combo_box->priv->active_item++;
+
+  if (!combo_box->priv->tree_view)
+    gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
+}
+
+static void
+gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
+                                GtkTreePath      *path,
+                                gpointer          user_data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+  gint index = gtk_tree_path_get_indices (path)[0];
+
+  if (!combo_box->priv->tree_view)
+    gtk_combo_box_menu_row_deleted (model, path, user_data);
+  
+  if (index == combo_box->priv->active_item)
+    {
+      gint items = gtk_tree_model_iter_n_children (model, NULL);
+
+      if (items == 0)
+       gtk_combo_box_set_active_internal (combo_box, -1);
+      else if (index == items)
+       gtk_combo_box_set_active_internal (combo_box, index - 1);
+      else
+       gtk_combo_box_set_active_internal (combo_box, index);
+    }
+  else if (combo_box->priv->active_item > index)
+    combo_box->priv->active_item--;
+}
+
+static void
+gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
+                                   GtkTreePath     *path,
+                                   GtkTreeIter     *iter,
+                                   gint            *new_order,
+                                   gpointer         user_data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+  gint items = gtk_tree_model_iter_n_children (model, NULL);
+  gint i;
+
+  for (i = 0; i < items; i++)
+    if (new_order[i] == combo_box->priv->active_item)
+      {
+       combo_box->priv->active_item = i;
+       break;
+      }
+
+  if (!combo_box->priv->tree_view)
+    gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
+}
+                                                   
+static void
+gtk_combo_box_model_row_changed (GtkTreeModel     *model,
+                                GtkTreePath      *path,
+                                GtkTreeIter      *iter,
+                                gpointer          user_data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+  gint index = gtk_tree_path_get_indices (path)[0];
+
+  if (index == combo_box->priv->active_item &&
+      combo_box->priv->cell_view)
+    gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
+  
+  if (combo_box->priv->tree_view)
+    gtk_combo_box_list_row_changed (model, path, iter, user_data);
+  else
+    gtk_combo_box_menu_row_changed (model, path, iter, user_data);
+}
+
+
 static void
 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
                                  GtkTreePath  *path,
@@ -1642,7 +2054,7 @@ gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
                                 GtkTreePath  *path,
                                 gpointer      user_data)
 {
-  gint index, items;
+  gint index;
   GtkWidget *menu;
   GtkWidget *item;
   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
@@ -1651,10 +2063,6 @@ gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
     return;
 
   index = gtk_tree_path_get_indices (path)[0];
-  items = gtk_tree_model_iter_n_children (model, NULL);
-
-  if (gtk_combo_box_get_active (combo_box) == index)
-    gtk_combo_box_set_active (combo_box, index + 1 % items);
 
   menu = combo_box->priv->popup_widget;
   g_return_if_fail (GTK_IS_MENU (menu));
@@ -1665,6 +2073,18 @@ gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
   gtk_container_remove (GTK_CONTAINER (menu), item);
 }
 
+static void
+gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
+                                   GtkTreePath      *path,
+                                   GtkTreeIter      *iter,
+                                   gint             *new_order,
+                                   gpointer          user_data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+
+  gtk_combo_box_relayout (combo_box);
+}
+                                   
 static void
 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
                                 GtkTreePath  *path,
@@ -1685,8 +2105,11 @@ gtk_combo_box_menu_row_changed (GtkTreeModel *model,
 
   if (width > combo_box->priv->width)
     {
-      gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
-      gtk_widget_queue_resize (combo_box->priv->cell_view);
+      if (combo_box->priv->cell_view)
+       {
+         gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
+         gtk_widget_queue_resize (combo_box->priv->cell_view);
+       }
       combo_box->priv->width = width;
     }
 }
@@ -1708,10 +2131,13 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box)
                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
   g_signal_connect (combo_box->priv->button, "toggled",
                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
+  g_signal_connect_after (combo_box, "key_press_event",
+                         G_CALLBACK (gtk_combo_box_key_press), combo_box);
 
   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
                      combo_box->priv->arrow);
+  combo_box->priv->separator = NULL;
   gtk_widget_show_all (combo_box->priv->button);
 
   if (combo_box->priv->cell_view)
@@ -1722,10 +2148,8 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box)
       gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
                                  GTK_SHADOW_IN);
 
-      g_object_set (G_OBJECT (combo_box->priv->cell_view),
-                    "background", "white",
-                    "background_set", TRUE,
-                    NULL);
+      gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
+                                         &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
 
       gtk_widget_show (combo_box->priv->cell_view_frame);
     }
@@ -1735,7 +2159,10 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box)
   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
                                      FALSE);
-
+  if (combo_box->priv->model)
+    gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
+                            combo_box->priv->model);
+    
   g_signal_connect (combo_box->priv->tree_view, "button_press_event",
                     G_CALLBACK (gtk_combo_box_list_button_pressed),
                     combo_box);
@@ -1750,9 +2177,6 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box)
   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
                                combo_box->priv->column);
 
-  /* set the models */
-  gtk_combo_box_set_model_internal (combo_box);
-
   /* sync up */
   for (i = combo_box->priv->cells; i; i = i->next)
     {
@@ -1789,8 +2213,7 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box)
     }
 
   /* set sample/popup widgets */
-  gtk_combo_box_set_popup_widget (GTK_COMBO_BOX (combo_box),
-                                  combo_box->priv->tree_view);
+  gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
 
   gtk_widget_show (combo_box->priv->tree_view);
 }
@@ -1799,8 +2222,6 @@ static void
 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
 {
   /* disconnect signals */
-  gtk_combo_box_unset_model (combo_box);
-
   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
                                         G_SIGNAL_MATCH_DATA,
                                         0, 0, NULL, NULL, combo_box);
@@ -1814,6 +2235,8 @@ gtk_combo_box_list_destroy (GtkComboBox *combo_box)
    * last unref on button will destroy the arrow
    */
   gtk_widget_unparent (combo_box->priv->button);
+  combo_box->priv->button = NULL;
+  combo_box->priv->arrow = NULL;
 
   if (combo_box->priv->cell_view)
     {
@@ -1822,9 +2245,11 @@ gtk_combo_box_list_destroy (GtkComboBox *combo_box)
                     NULL);
 
       gtk_widget_unparent (combo_box->priv->cell_view_frame);
+      combo_box->priv->cell_view_frame = NULL;
     }
 
   gtk_widget_destroy (combo_box->priv->tree_view);
+
   combo_box->priv->tree_view = NULL;
   combo_box->priv->popup_widget = NULL;
 }
@@ -1839,6 +2264,7 @@ gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
     {
       gtk_grab_remove (combo_box->priv->popup_window);
+      gdk_keyboard_ungrab (GDK_CURRENT_TIME);
       gdk_pointer_ungrab (GDK_CURRENT_TIME);
     }
 }
@@ -1861,15 +2287,6 @@ gtk_combo_box_list_button_pressed (GtkWidget      *widget,
 
   gtk_combo_box_popup (combo_box);
 
-  gtk_grab_add (combo_box->priv->popup_window);
-  gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
-                    GDK_BUTTON_PRESS_MASK |
-                    GDK_BUTTON_RELEASE_MASK |
-                    GDK_POINTER_MOTION_MASK,
-                    NULL, NULL, GDK_CURRENT_TIME);
-
-  gtk_grab_add (combo_box->priv->tree_view);
-
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
                                 TRUE);
 
@@ -1904,7 +2321,6 @@ gtk_combo_box_list_button_released (GtkWidget      *widget,
           !popup_in_progress &&
           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
         {
-          gtk_combo_box_list_remove_grabs (combo_box);
           gtk_combo_box_popdown (combo_box);
           return TRUE;
         }
@@ -1912,7 +2328,6 @@ gtk_combo_box_list_button_released (GtkWidget      *widget,
       /* released outside treeview */
       if (ewidget != combo_box->priv->button)
         {
-          gtk_combo_box_list_remove_grabs (combo_box);
           gtk_combo_box_popdown (combo_box);
 
           return TRUE;
@@ -1921,9 +2336,6 @@ gtk_combo_box_list_button_released (GtkWidget      *widget,
       return FALSE;
     }
 
-  /* drop grabs */
-  gtk_combo_box_list_remove_grabs (combo_box);
-
   /* select something cool */
   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
                                        event->x, event->y,
@@ -1942,46 +2354,120 @@ gtk_combo_box_list_button_released (GtkWidget      *widget,
 }
 
 static gboolean
-gtk_combo_box_list_key_press (GtkWidget   *widget,
-                              GdkEventKey *event,
-                              gpointer     data)
+gtk_combo_box_key_press (GtkWidget   *widget,
+                        GdkEventKey *event,
+                        gpointer     data)
 {
   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+  guint state = event->state & gtk_accelerator_get_default_mod_mask ();
+  gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
+  gint index = gtk_combo_box_get_active (combo_box);
+  gint new_index;
 
-  if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
-       event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
-      event->keyval == GDK_Escape)
+  if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) && 
+      state == GDK_MOD1_MASK)
     {
-      if (event->keyval != GDK_Escape)
-        {
-          gboolean ret;
-          GtkTreeIter iter;
-          GtkTreeModel *model = NULL;
-          GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
+      gtk_combo_box_popup (combo_box);
 
-          ret = gtk_tree_selection_get_selected (sel, &model, &iter);
-          if (ret)
-            {
-              GtkTreePath *path;
-
-              path = gtk_tree_model_get_path (model, &iter);
-              if (path)
-                {
-                  gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
-                  gtk_tree_path_free (path);
-                }
-            }
-        }
-      else
-        /* reset active item -- this is incredibly lame and ugly */
-        gtk_combo_box_set_active (combo_box,
-                                  gtk_combo_box_get_active (combo_box));
+      return TRUE;
+    }
+
+  switch (event->keyval) 
+    {
+    case GDK_Down:
+    case GDK_KP_Down:
+      new_index = index + 1;
+      break;
+    case GDK_Up:
+    case GDK_KP_Up:
+      new_index = index - 1;
+      break;
+    case GDK_Page_Up:
+    case GDK_KP_Page_Up:
+    case GDK_Home: 
+    case GDK_KP_Home:
+      new_index = 0;
+      break;
+    case GDK_Page_Down:
+    case GDK_KP_Page_Down:
+    case GDK_End: 
+    case GDK_KP_End:
+      new_index = items - 1;
+      break;
+    default:
+      return FALSE;
+    }
+  
+  gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
+
+  return TRUE;
+}
+
+static gboolean
+gtk_combo_box_menu_key_press (GtkWidget   *widget,
+                             GdkEventKey *event,
+                             gpointer     data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+  guint state = event->state & gtk_accelerator_get_default_mod_mask ();
 
-      gtk_combo_box_list_remove_grabs (combo_box);
+  if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
+      state == GDK_MOD1_MASK)
+    {
       gtk_combo_box_popdown (combo_box);
 
       return TRUE;
     }
+  
+  return FALSE;
+}
+
+static gboolean
+gtk_combo_box_list_key_press (GtkWidget   *widget,
+                              GdkEventKey *event,
+                              gpointer     data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+  guint state = event->state & gtk_accelerator_get_default_mod_mask ();
+
+  if (event->keyval == GDK_Escape ||
+      ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
+       state == GDK_MOD1_MASK))
+    {
+      /* reset active item -- this is incredibly lame and ugly */
+      gtk_combo_box_set_active (combo_box,
+                               gtk_combo_box_get_active (combo_box));
+      
+      gtk_combo_box_popdown (combo_box);
+      
+      return TRUE;
+    }
+    
+  if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
+      event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
+  {
+    gboolean ret;
+    GtkTreeIter iter;
+    GtkTreeModel *model = NULL;
+    GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
+    
+    ret = gtk_tree_selection_get_selected (sel, &model, &iter);
+    if (ret)
+      {
+       GtkTreePath *path;
+       
+       path = gtk_tree_model_get_path (model, &iter);
+       if (path)
+         {
+           gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
+           gtk_tree_path_free (path);
+         }
+      }
+
+    gtk_combo_box_popdown (combo_box);
+    
+    return TRUE;
+  }
 
   return FALSE;
 }
@@ -1999,8 +2485,11 @@ gtk_combo_box_list_row_changed (GtkTreeModel *model,
 
   if (width > combo_box->priv->width)
     {
-      gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
-      gtk_widget_queue_resize (combo_box->priv->cell_view);
+      if (combo_box->priv->cell_view) 
+       {
+         gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
+         gtk_widget_queue_resize (combo_box->priv->cell_view);
+       }
       combo_box->priv->width = width;
     }
 }
@@ -2014,12 +2503,17 @@ gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
                                       gboolean         expand)
 {
   ComboCellInfo *info;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
+  GtkComboBox *combo_box;
   GtkWidget *menu;
 
-  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+  g_return_if_fail (GTK_IS_COMBO_BOX (layout));
   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
 
+  combo_box = GTK_COMBO_BOX (layout);
+
+  g_object_ref (G_OBJECT (cell));
+  gtk_object_sink (GTK_OBJECT (cell));
+
   info = g_new0 (ComboCellInfo, 1);
   info->cell = cell;
   info->expand = expand;
@@ -2061,12 +2555,17 @@ gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
                                     gboolean         expand)
 {
   ComboCellInfo *info;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
+  GtkComboBox *combo_box;
   GtkWidget *menu;
 
-  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+  g_return_if_fail (GTK_IS_COMBO_BOX (layout));
   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
 
+  combo_box = GTK_COMBO_BOX (layout);
+
+  g_object_ref (G_OBJECT (cell));
+  gtk_object_sink (GTK_OBJECT (cell));
+
   info = g_new0 (ComboCellInfo, 1);
   info->cell = cell;
   info->expand = expand;
@@ -2104,16 +2603,31 @@ static void
 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
 {
   GtkWidget *menu;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
-
-  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+  GtkComboBox *combo_box;
+  GSList *i;
+  
+  g_return_if_fail (GTK_IS_COMBO_BOX (layout));
 
+  combo_box = GTK_COMBO_BOX (layout);
   if (combo_box->priv->cell_view)
     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
 
   if (combo_box->priv->column)
     gtk_tree_view_column_clear (combo_box->priv->column);
 
+  for (i = combo_box->priv->cells; i; i = i->next)
+    {
+     ComboCellInfo *info = (ComboCellInfo *)i->data;
+
+      gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
+      g_object_unref (G_OBJECT (info->cell));
+      g_free (info);
+      i->data = NULL;
+    }
+  g_slist_free (combo_box->priv->cells);
+  combo_box->priv->cells = NULL;
+
   menu = combo_box->priv->popup_widget;
   if (GTK_IS_MENU (menu))
     {
@@ -2131,6 +2645,7 @@ gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
 
           gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
         }
+      g_list_free (list);
     }
 }
 
@@ -2141,12 +2656,14 @@ gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
                                          gint             column)
 {
   ComboCellInfo *info;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
+  GtkComboBox *combo_box;
   GtkWidget *menu;
 
-  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+  g_return_if_fail (GTK_IS_COMBO_BOX (layout));
   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
 
+  combo_box = GTK_COMBO_BOX (layout);
+
   info = gtk_combo_box_get_cell_info (combo_box, cell);
 
   info->attributes = g_slist_prepend (info->attributes,
@@ -2194,10 +2711,12 @@ gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
                                               GDestroyNotify         destroy)
 {
   ComboCellInfo *info;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
+  GtkComboBox *combo_box;
   GtkWidget *menu;
 
-  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+  g_return_if_fail (GTK_IS_COMBO_BOX (layout));
+
+  combo_box = GTK_COMBO_BOX (layout);
 
   info = gtk_combo_box_get_cell_info (combo_box, cell);
   g_return_if_fail (info != NULL);
@@ -2249,13 +2768,15 @@ gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
                                             GtkCellRenderer *cell)
 {
   ComboCellInfo *info;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
+  GtkComboBox *combo_box;
   GtkWidget *menu;
   GSList *list;
 
   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
 
+  combo_box = GTK_COMBO_BOX (layout);
+
   info = gtk_combo_box_get_cell_info (combo_box, cell);
   g_return_if_fail (info != NULL);
 
@@ -2265,8 +2786,7 @@ gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
       g_free (list->data);
       list = list->next->next;
     }
-  g_slist_free (list);
-
+  g_slist_free (info->attributes);
   info->attributes = NULL;
 
   if (combo_box->priv->cell_view)
@@ -2304,13 +2824,15 @@ gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
                                    gint             position)
 {
   ComboCellInfo *info;
-  GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
+  GtkComboBox *combo_box;
   GtkWidget *menu;
   GSList *link;
 
   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
 
+  combo_box = GTK_COMBO_BOX (layout);
+
   info = gtk_combo_box_get_cell_info (combo_box, cell);
 
   g_return_if_fail (info != NULL);
@@ -2371,7 +2893,7 @@ gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
 GtkWidget *
 gtk_combo_box_new (void)
 {
-  return GTK_WIDGET (g_object_new (gtk_combo_box_get_type (), NULL));
+  return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
 }
 
 /**
@@ -2391,7 +2913,7 @@ gtk_combo_box_new_with_model (GtkTreeModel *model)
 
   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
 
-  combo_box = GTK_COMBO_BOX (g_object_new (gtk_combo_box_get_type (),
+  combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
                                            "model", model,
                                            NULL));
 
@@ -2414,11 +2936,17 @@ gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
                               gint         width)
 {
   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
-  g_return_if_fail (width > 0);
+  g_return_if_fail (width >= 0);
 
-  combo_box->priv->wrap_width = width;
+  if (width != combo_box->priv->wrap_width)
+    {
+      combo_box->priv->wrap_width = width;
 
-  gtk_combo_box_relayout (combo_box);
+      gtk_combo_box_check_appearance (combo_box);
+      gtk_combo_box_relayout (combo_box);
+      
+      g_object_notify (G_OBJECT (combo_box), "wrap_width");
+    }
 }
 
 /**
@@ -2443,9 +2971,14 @@ gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
   g_return_if_fail (row_span >= 0 && row_span < col);
 
-  combo_box->priv->row_column = row_span;
-
-  gtk_combo_box_relayout (combo_box);
+  if (row_span != combo_box->priv->row_column)
+    {
+      combo_box->priv->row_column = row_span;
+      
+      gtk_combo_box_relayout (combo_box);
+      g_object_notify (G_OBJECT (combo_box), "row_span_column");
+    }
 }
 
 /**
@@ -2470,9 +3003,14 @@ gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
   g_return_if_fail (column_span >= 0 && column_span < col);
 
-  combo_box->priv->col_column = column_span;
+  if (column_span != combo_box->priv->col_column)
+    {
+      combo_box->priv->col_column = column_span;
+      
+      gtk_combo_box_relayout (combo_box);
 
-  gtk_combo_box_relayout (combo_box);
+      g_object_notify (G_OBJECT (combo_box), "column_span_column");
+    }
 }
 
 /**
@@ -2509,14 +3047,21 @@ void
 gtk_combo_box_set_active (GtkComboBox *combo_box,
                           gint         index)
 {
-  GtkTreePath *path;
-
   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
   /* -1 means "no item selected" */
   g_return_if_fail (index >= -1);
 
   if (combo_box->priv->active_item == index)
     return;
+  
+  gtk_combo_box_set_active_internal (combo_box, index);
+}
+
+static void
+gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
+                                  gint         index)
+{
+  GtkTreePath *path;
 
   combo_box->priv->active_item = index;
 
@@ -2540,7 +3085,7 @@ gtk_combo_box_set_active (GtkComboBox *combo_box,
       path = gtk_tree_path_new_from_indices (index, -1);
 
       if (combo_box->priv->tree_view)
-        gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
+       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
       else
         {
           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
@@ -2550,7 +3095,7 @@ gtk_combo_box_set_active (GtkComboBox *combo_box,
         }
 
       if (combo_box->priv->cell_view)
-        gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
+       gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
 
       gtk_tree_path_free (path);
     }
@@ -2564,7 +3109,7 @@ gtk_combo_box_set_active (GtkComboBox *combo_box,
  * @combo_box: A #GtkComboBox
  * @iter: The uninitialized #GtkTreeIter.
  * 
- * Set @iter to point to the current active item, if it exists.
+ * Sets @iter to point to the current active item, if it exists.
  * 
  * Return value: %TRUE, if @iter was set
  *
@@ -2597,7 +3142,8 @@ gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
  * @combo_box: A #GtkComboBox
  * @iter: The #GtkTreeIter.
  * 
- * Sets the current active item to be the one referenced by @iter.
+ * Sets the current active item to be the one referenced by @iter. 
+ * @iter must correspond to a path of depth one.
  * 
  * Since: 2.4
  **/
@@ -2611,7 +3157,7 @@ gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
 
   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
   g_return_if_fail (path != NULL);
-  g_return_if_fail (gtk_tree_path_get_depth (path) != 1);
+  g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
   
   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
   gtk_tree_path_free (path);
@@ -2634,18 +3180,45 @@ gtk_combo_box_set_model (GtkComboBox  *combo_box,
   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
   g_return_if_fail (GTK_IS_TREE_MODEL (model));
 
+  if (model == combo_box->priv->model)
+    return;
+  
   if (combo_box->priv->model)
-    {
-      gtk_combo_box_unset_model (combo_box);
-      g_object_unref (G_OBJECT (combo_box->priv->model));
-    }
+    gtk_combo_box_unset_model (combo_box);
 
   combo_box->priv->model = model;
   g_object_ref (G_OBJECT (combo_box->priv->model));
 
-  gtk_combo_box_set_model_internal (combo_box);
-  if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
-    gtk_combo_box_menu_fill (combo_box);
+  combo_box->priv->inserted_id =
+    g_signal_connect (combo_box->priv->model, "row_inserted",
+                     G_CALLBACK (gtk_combo_box_model_row_inserted),
+                     combo_box);
+  combo_box->priv->deleted_id =
+    g_signal_connect (combo_box->priv->model, "row_deleted",
+                     G_CALLBACK (gtk_combo_box_model_row_deleted),
+                     combo_box);
+  combo_box->priv->reordered_id =
+    g_signal_connect (combo_box->priv->model, "rows_reordered",
+                     G_CALLBACK (gtk_combo_box_model_rows_reordered),
+                     combo_box);
+  combo_box->priv->changed_id =
+    g_signal_connect (combo_box->priv->model, "row_changed",
+                     G_CALLBACK (gtk_combo_box_model_row_changed),
+                     combo_box);
+      
+  if (combo_box->priv->tree_view)
+    {
+      /* list mode */
+      gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
+                               combo_box->priv->model);
+    }
+  else
+    {
+      /* menu mode */
+      if (combo_box->priv->popup_widget)
+       gtk_combo_box_menu_fill (combo_box);
+
+    }
 
   if (combo_box->priv->cell_view)
     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
@@ -2678,9 +3251,10 @@ gtk_combo_box_get_model (GtkComboBox *combo_box)
  *
  * Convenience function which constructs a new text combo box, which is a
  * #GtkComboBox just displaying strings. If you use this function to create
- * a text combo box, you only want to manipulate it's data source with the
+ * a text combo box, you should only manipulate its data source with the
  * following convenience functions: gtk_combo_box_append_text(),
- * gtk_combo_box_insert_text() and gtk_combo_box_prepend_text().
+ * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
+ * gtk_combo_box_remove_text().
  *
  * Return value: A new text combo box.
  *
@@ -2820,3 +3394,72 @@ gtk_combo_box_remove_text (GtkComboBox *combo_box,
                                      NULL, position))
     gtk_list_store_remove (store, &iter);
 }
+
+
+static gboolean
+gtk_combo_box_mnemonic_activate (GtkWidget *widget,
+                                gboolean   group_cycling)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
+
+  gtk_widget_grab_focus (combo_box->priv->button);
+
+  return TRUE;
+}
+
+static void
+gtk_combo_box_destroy (GtkObject *object)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (object);
+
+  combo_box->priv->destroying = 1;
+
+  GTK_OBJECT_CLASS (parent_class)->destroy (object);
+
+  combo_box->priv->cell_view = NULL;
+
+  combo_box->priv->destroying = 0;
+}
+
+static void
+gtk_combo_box_finalize (GObject *object)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (object);
+  GSList *i;
+  
+  if (GTK_IS_MENU (combo_box->priv->popup_widget))
+    gtk_combo_box_menu_destroy (combo_box);
+  
+  if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
+    gtk_combo_box_list_destroy (combo_box);
+
+  if (combo_box->priv->popup_window)
+    gtk_widget_destroy (combo_box->priv->popup_window);
+
+  gtk_combo_box_unset_model (combo_box);
+
+  if (combo_box->priv->model)
+    g_object_unref (combo_box->priv->model);
+
+  for (i = combo_box->priv->cells; i; i = i->next)
+    {
+      ComboCellInfo *info = (ComboCellInfo *)i->data;
+      GSList *list = info->attributes;
+
+      if (info->destroy)
+       info->destroy (info->func_data);
+
+      while (list && list->next)
+       {
+         g_free (list->data);
+         list = list->next->next;
+       }
+      g_slist_free (info->attributes);
+
+      g_object_unref (G_OBJECT (info->cell));
+      g_free (info);
+    }
+   g_slist_free (combo_box->priv->cells);
+
+   G_OBJECT_CLASS (parent_class)->finalize (object);
+}