]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcombobox.c
Don't unref model if it is NULL. (#139770)
[~andy/gtk] / gtk / gtkcombobox.c
index 1ae57c59b75d911fc752e18225975b3769ede9da..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>
@@ -91,13 +92,14 @@ 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
@@ -190,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);
@@ -208,6 +213,16 @@ 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,
@@ -219,7 +234,6 @@ static gint     gtk_combo_box_calc_requested_width (GtkComboBox      *combo_box,
 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);
@@ -233,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);
@@ -255,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);
 
@@ -279,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,
@@ -309,6 +361,8 @@ static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout
 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)
@@ -365,6 +419,7 @@ 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;
@@ -372,6 +427,8 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
   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;
@@ -443,9 +500,9 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
                                                      G_PARAM_READWRITE));
 
   gtk_widget_class_install_style_property (widget_class,
-                                           g_param_spec_boolean ("appearance",
-                                                                 P_("ComboBox appareance"),
-                                                                 P_("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));
 
@@ -469,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;
@@ -556,46 +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);
-      else
-       gtk_combo_box_unset_model (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);
-      else
-       gtk_combo_box_unset_model (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)
@@ -618,9 +698,14 @@ gtk_combo_box_add (GtkContainer *container,
   GtkComboBox *combo_box = GTK_COMBO_BOX (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);
+    {
+      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)
@@ -649,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)
@@ -659,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;
     }
 
@@ -730,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;
@@ -749,14 +885,16 @@ gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
       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);
         }
 
@@ -769,39 +907,188 @@ 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 (GTK_WIDGET_NO_WINDOW (child))
+  if (active != NULL)
     {
-      *x += child->allocation.x;
-      *y += child->allocation.y;
+      gtk_widget_get_child_requisition (active, &requisition);
+      menu_ypos -= requisition.height / 2;
     }
 
+  children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
+  while (children)
+    {
+      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_menu_position (GtkMenu  *menu,
+                            gint     *x,
+                            gint     *y,
+                            gint     *push_in,
+                            gpointer  user_data)
+{
+  GtkComboBox *combo_box;
+  GtkWidget *menu_item;
+
+  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
+    {
+      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);
+    }
+
+}
+
+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;
+  gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
+  *height = popup_req.height;
+
+  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 +
+            GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
+    }
+
+  if (GTK_WIDGET_NO_WINDOW (sample))
+    {
+      *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
@@ -817,8 +1104,7 @@ void
 gtk_combo_box_popup (GtkComboBox *combo_box)
 {
   gint x, y, width, height;
-  GtkWidget *sample;
-
+  
   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
 
   if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
@@ -826,54 +1112,24 @@ gtk_combo_box_popup (GtkComboBox *combo_box)
 
   if (GTK_IS_MENU (combo_box->priv->popup_widget))
     {
-      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);
-        }
+      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);
+                     NULL, NULL,
+                     gtk_combo_box_menu_position, combo_box,
+                     0, 0);
       return;
     }
 
-  /* size it */
-  sample = GTK_BIN (combo_box)->child;
-
-  width = sample->allocation.width;
-  height = sample->allocation.height;
-
-  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 +
-            GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
-    }
-
-  gtk_widget_set_size_request (combo_box->priv->popup_window,
-                               width, -1);
+  gtk_widget_show_all (combo_box->priv->popup_frame);
+  gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
 
-  if (GTK_WIDGET_NO_WINDOW (sample))
-    {
-      x += sample->allocation.x;
-      y += sample->allocation.y;
-    }
-
-  gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window),
-                   x, y + 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),
@@ -885,6 +1141,15 @@ 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);
 }
 
 /**
@@ -909,6 +1174,7 @@ gtk_combo_box_popdown (GtkComboBox *combo_box)
       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);
@@ -969,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;
@@ -1004,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;
@@ -1021,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;
@@ -1040,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);
@@ -1094,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);
@@ -1195,23 +1463,16 @@ gtk_combo_box_size_allocate (GtkWidget     *widget,
 static void
 gtk_combo_box_unset_model (GtkComboBox *combo_box)
 {
-  if (combo_box->priv->inserted_id != -1)
+  if (combo_box->priv->model)
     {
       g_signal_handler_disconnect (combo_box->priv->model,
                                   combo_box->priv->inserted_id);
-      combo_box->priv->inserted_id = -1;
-    }
-  if (combo_box->priv->deleted_id != -1)
-    {
       g_signal_handler_disconnect (combo_box->priv->model,
                                   combo_box->priv->deleted_id);
-      combo_box->priv->deleted_id = -1;
-    }
-  if (combo_box->priv->changed_id != -1)
-    {
+      g_signal_handler_disconnect (combo_box->priv->model,
+                                  combo_box->priv->reordered_id);
       g_signal_handler_disconnect (combo_box->priv->model,
                                   combo_box->priv->changed_id);
-      combo_box->priv->changed_id = -1;
     }
 
   /* menu mode */
@@ -1221,37 +1482,11 @@ gtk_combo_box_unset_model (GtkComboBox *combo_box)
         gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
                                (GtkCallback)gtk_widget_destroy, NULL);
     }
-}
 
-static void
-gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
-{
-  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);
-    }
-  else
+  if (combo_box->priv->model)
     {
-      /* 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);
+      g_object_unref (G_OBJECT (combo_box->priv->model));
+      combo_box->priv->model = NULL;
     }
 }
 
@@ -1379,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;
 
@@ -1388,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);
 
@@ -1414,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);
 
@@ -1429,13 +1668,12 @@ 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);
 
-  /* set the models */
-  gtk_combo_box_set_model_internal (combo_box);
-
   /* add items */
-  if (add_childs)
+  if (add_children)
     gtk_combo_box_menu_fill (combo_box);
 
 }
@@ -1468,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);
@@ -1477,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,
@@ -1489,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 */
 }
 
 /*
@@ -1622,24 +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);
-  else
-    gtk_combo_box_unset_model (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));
 
@@ -1651,32 +1889,7 @@ gtk_combo_box_relayout (GtkComboBox *combo_box)
   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 */
@@ -1693,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,
@@ -1720,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,
@@ -1753,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);
@@ -1762,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));
@@ -1776,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,
@@ -1822,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)
@@ -1836,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);
     }
@@ -1849,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);
@@ -1864,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)
     {
@@ -1912,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);
@@ -1927,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)
     {
@@ -1935,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;
 }
@@ -1952,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);
     }
 }
@@ -1974,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);
 
@@ -2017,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;
         }
@@ -2025,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;
@@ -2034,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,
@@ -2055,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;
 }
@@ -2250,6 +2623,7 @@ gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
       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;
@@ -2562,12 +2936,13 @@ 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);
 
   if (width != combo_box->priv->wrap_width)
     {
       combo_box->priv->wrap_width = width;
-      
+
+      gtk_combo_box_check_appearance (combo_box);
       gtk_combo_box_relayout (combo_box);
       
       g_object_notify (G_OBJECT (combo_box), "wrap_width");
@@ -2672,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;
 
@@ -2703,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);
@@ -2713,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);
     }
@@ -2727,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
  *
@@ -2798,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),
@@ -2842,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.
  *
@@ -3002,9 +3412,13 @@ 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
@@ -3013,8 +3427,6 @@ gtk_combo_box_finalize (GObject *object)
   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
   GSList *i;
   
-  gtk_combo_box_unset_model (combo_box);
-
   if (GTK_IS_MENU (combo_box->priv->popup_widget))
     gtk_combo_box_menu_destroy (combo_box);
   
@@ -3024,6 +3436,8 @@ gtk_combo_box_finalize (GObject *object)
   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);