commit 520a9d14473675d9b7eafbee8c9d723d5c2941ea
Author: Tristan Van Berkom <tristan.van.berkom@gmail.com>
Date:   Wed Oct 13 18:03:38 2010 +0900

    Allow GtkComboBox popup to be wider than the combo itself.
    
    This patch adds a GtkComboBox:popup-fixed-width to decide if the
    popup's width should be a fixed width matching the combo's allocated width.
    
    The patch includes a kindof hack to work around treeviews currently
    not supporting height-for-width geometry (for list-mode only), this
    hack can be safely removed once treeviews start reporting natural
    widths properly.

diff --git a/gtk/gtkcombobox.c b/gtk/gtkcombobox.c
index aeb5e91..a7df7ed 100644
--- a/gtk/gtkcombobox.c
+++ b/gtk/gtkcombobox.c
@@ -152,6 +152,7 @@ struct _GtkComboBoxPrivate
   guint auto_scroll : 1;
   guint focus_on_click : 1;
   guint button_sensitivity : 2;
+  guint popup_fixed_width : 1;
 
   GtkTreeViewRowSeparatorFunc row_separator_func;
   gpointer                    row_separator_data;
@@ -238,7 +239,8 @@ enum {
   PROP_FOCUS_ON_CLICK,
   PROP_POPUP_SHOWN,
   PROP_BUTTON_SENSITIVITY,
-  PROP_EDITING_CANCELED
+  PROP_EDITING_CANCELED,
+  PROP_POPUP_FIXED_WIDTH
 };
 
 static guint combo_box_signals[LAST_SIGNAL] = {0,};
@@ -896,6 +898,24 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
                                                        GTK_SENSITIVITY_AUTO,
                                                        GTK_PARAM_READWRITE));
 
+   /**
+    * GtkComboBox:popup-fixed-width:
+    *
+    * Whether the popup's width should be a fixed width matching the
+    * allocated width of the combo box.
+    *
+    * Since: 3.0
+    */
+   g_object_class_install_property (object_class,
+                                    PROP_POPUP_FIXED_WIDTH,
+                                    g_param_spec_boolean ("popup-fixed-width",
+							  P_("Popup Fixed Width"),
+							  P_("Whether the popup's width should be a "
+							     "fixed width matching the allocated width "
+							     "of the combo box"),
+							  TRUE,
+							  GTK_PARAM_READWRITE));
+
   gtk_widget_class_install_style_property (widget_class,
                                            g_param_spec_boolean ("appears-as-list",
                                                                  P_("Appears as list"),
@@ -1001,6 +1021,7 @@ gtk_combo_box_init (GtkComboBox *combo_box)
   priv->auto_scroll = FALSE;
   priv->focus_on_click = TRUE;
   priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
+  priv->popup_fixed_width = TRUE;
 
   gtk_combo_box_check_appearance (combo_box);
 }
@@ -1064,6 +1085,11 @@ gtk_combo_box_set_property (GObject      *object,
                                             g_value_get_enum (value));
       break;
 
+    case PROP_POPUP_FIXED_WIDTH:
+      gtk_combo_box_set_popup_fixed_width (combo_box,
+					   g_value_get_boolean (value));
+      break;
+
     case PROP_EDITING_CANCELED:
       combo_box->priv->editing_canceled = g_value_get_boolean (value);
       break;
@@ -1129,6 +1155,10 @@ gtk_combo_box_get_property (GObject    *object,
         g_value_set_enum (value, combo_box->priv->button_sensitivity);
         break;
 
+      case PROP_POPUP_FIXED_WIDTH:
+        g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
+        break;
+
       case PROP_EDITING_CANCELED:
         g_value_set_boolean (value, priv->editing_canceled);
         break;
@@ -1557,8 +1587,10 @@ gtk_combo_box_menu_position_below (GtkMenu  *menu,
   if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
     sx -= gtk_widget_get_style (GTK_WIDGET (combo_box))->xthickness;
 
-  gtk_widget_get_preferred_size (GTK_WIDGET (menu),
-                                 &req, NULL);
+  if (combo_box->priv->popup_fixed_width)
+    gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
+  else
+    gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
 
   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
     *x = sx;
@@ -1617,7 +1649,10 @@ gtk_combo_box_menu_position_over (GtkMenu  *menu,
   menu_xpos = allocation.x;
   menu_ypos = allocation.y + allocation.height / 2 - 2;
 
-  gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
+  if (combo_box->priv->popup_fixed_width)
+    gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
+  else
+    gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
 
   if (active != NULL)
     {
@@ -1675,7 +1710,6 @@ gtk_combo_box_menu_position (GtkMenu  *menu,
   GtkComboBoxPrivate *priv = combo_box->priv;
   GtkWidget *menu_item;
 
-
   if (priv->wrap_width > 0 || priv->cell_view == NULL)	
     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
   else
@@ -1734,16 +1768,37 @@ gtk_combo_box_list_position (GtkComboBox *combo_box,
   hpolicy = vpolicy = GTK_POLICY_NEVER;
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
 				  hpolicy, vpolicy);
-  gtk_widget_get_preferred_size (priv->scrolled_window,
-                                 &popup_req, NULL);
 
-  if (popup_req.width > *width)
+  if (combo_box->priv->popup_fixed_width)
     {
-      hpolicy = GTK_POLICY_ALWAYS;
-      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
-				      hpolicy, vpolicy);
-      gtk_widget_get_preferred_size (priv->scrolled_window,
-                                     &popup_req, NULL);
+      gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
+
+      if (popup_req.width > *width)
+	{
+	  hpolicy = GTK_POLICY_ALWAYS;
+	  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
+					  hpolicy, vpolicy);
+	}
+    }
+  else
+    {
+      /* XXX The treeview doesnt do height-for-width or natural width yet,
+       * remeasuring the combo box and using priv->natural_width will be obsolete/unneeded 
+       * once treeview height-for-width gets implemented */
+      gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
+
+      gtk_combo_box_remeasure (combo_box);
+
+      if (priv->natural_width > *width)
+	{
+	  hpolicy = GTK_POLICY_NEVER;
+	  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
+					  hpolicy, vpolicy);
+
+	  /* XXX This hack is to guess at the scrolled window border width while treeviews
+	   * dont support height-for-width */
+	  *width = priv->natural_width + 10;
+	}
     }
 
   *height = popup_req.height;
@@ -1894,7 +1949,7 @@ gtk_combo_box_menu_popup (GtkComboBox *combo_box,
   GtkComboBoxPrivate *priv = combo_box->priv;
   GtkTreePath *path;
   gint active_item;
-  gint width, min_width;
+  gint width, min_width, nat_width;
   
   update_menu_sensitivity (combo_box, priv->popup_widget);
 
@@ -1919,10 +1974,14 @@ gtk_combo_box_menu_popup (GtkComboBox *combo_box,
       gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
       width = allocation.width;
       gtk_widget_set_size_request (priv->popup_widget, -1, -1);
-      gtk_widget_get_preferred_width (priv->popup_widget, &min_width, NULL);
-      
-      gtk_widget_set_size_request (priv->popup_widget,
-				   MAX (width, min_width), -1);
+      gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
+
+      if (combo_box->priv->popup_fixed_width)
+	width = MAX (width, min_width);
+      else
+	width = MAX (width, nat_width);
+
+      gtk_widget_set_size_request (priv->popup_widget, width, -1);
     }
   
   gtk_menu_popup (GTK_MENU (priv->popup_widget),
@@ -2320,7 +2379,7 @@ gtk_combo_box_size_allocate (GtkWidget     *widget,
 
           if (gtk_widget_get_visible (priv->popup_widget))
             {
-              gint width, min_width;
+              gint width, menu_width;
 
               if (priv->wrap_width == 0)
                 {
@@ -2329,9 +2388,14 @@ gtk_combo_box_size_allocate (GtkWidget     *widget,
                   gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
                   width = combo_box_allocation.width;
                   gtk_widget_set_size_request (priv->popup_widget, -1, -1);
-                  gtk_widget_get_preferred_width (priv->popup_widget, &min_width, NULL);
+
+		  if (combo_box->priv->popup_fixed_width)
+		    gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
+		  else
+		    gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
+
                   gtk_widget_set_size_request (priv->popup_widget,
-                    MAX (width, min_width), -1);
+					       MAX (width, menu_width), -1);
                }
 
               /* reposition the menu after giving it a new width */
@@ -5892,6 +5956,49 @@ gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
 }
 
 
+
+/**
+ * gtk_combo_box_set_popup_fixed_width:
+ * @combo_box: a #GtkComboBox
+ * @fixed: whether to use a fixed popup width
+ *
+ * Specifies whether the popup's width should be a fixed width matching 
+ * the allocated width of the combo box.
+ *
+ * Since: 3.0
+ **/
+void
+gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
+				     gboolean     fixed)
+{
+  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+
+  if (combo_box->priv->popup_fixed_width != fixed)
+    {
+      combo_box->priv->popup_fixed_width = fixed;
+
+      g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
+    }
+}
+
+/**
+ * gtk_combo_box_get_popup_fixed_width:
+ * @combo_box: a #GtkComboBox
+ *
+ * Gets whether the popup uses a fixed width matching 
+ * the allocated width of the combo box.
+ *
+ * Since: 3.0
+ **/
+gboolean
+gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
+{
+  g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
+
+  return combo_box->priv->popup_fixed_width;
+}
+
+
 /**
  * gtk_combo_box_set_focus_on_click:
  * @combo: a #GtkComboBox
diff --git a/gtk/gtkcombobox.h b/gtk/gtkcombobox.h
index 84f5475..c4cdbc8 100644
--- a/gtk/gtkcombobox.h
+++ b/gtk/gtkcombobox.h
@@ -118,6 +118,10 @@ void               gtk_combo_box_set_button_sensitivity (GtkComboBox        *com
 							 GtkSensitivityType  sensitivity);
 GtkSensitivityType gtk_combo_box_get_button_sensitivity (GtkComboBox        *combo_box);
 
+void               gtk_combo_box_set_popup_fixed_width  (GtkComboBox      *combo_box,
+							 gboolean          fixed);
+gboolean           gtk_combo_box_get_popup_fixed_width  (GtkComboBox      *combo_box);
+
 /* convenience -- text */
 GtkWidget    *gtk_combo_box_new_text         (void);
 void          gtk_combo_box_append_text      (GtkComboBox     *combo_box,
diff --git a/tests/testcombo.c b/tests/testcombo.c
index bf01142..99607f7 100644
--- a/tests/testcombo.c
+++ b/tests/testcombo.c
@@ -372,6 +372,39 @@ create_list_blaat (void)
         return GTK_TREE_MODEL (store);
 }
 
+
+static GtkTreeModel *
+create_list_long (void)
+{
+        GtkTreeIter iter;
+        GtkListStore *store;
+
+        store = gtk_list_store_new (1, G_TYPE_STRING);
+
+        gtk_list_store_append (store, &iter);
+        gtk_list_store_set (store, &iter,
+                            0, "here is some long long text that grows out of the combo's allocation",
+                            -1);
+
+
+        gtk_list_store_append (store, &iter);
+        gtk_list_store_set (store, &iter,
+                            0, "with at least a few of these rows",
+                            -1);
+
+        gtk_list_store_append (store, &iter);
+        gtk_list_store_set (store, &iter,
+                            0, "so that we can get some ellipsized text here",
+                            -1);
+
+        gtk_list_store_append (store, &iter);
+        gtk_list_store_set (store, &iter,
+                            0, "and see the combo box menu being allocated without any constraints",
+                            -1);
+
+        return GTK_TREE_MODEL (store);
+}
+
 /* blaat */
 static GtkTreeModel *
 create_phylogenetic_tree (void)
@@ -1335,6 +1368,27 @@ main (int argc, char **argv)
 	gdk_threads_add_timeout (1000, (GSourceFunc) capital_animation, model);
 #endif
 
+	/* Ellipsizing growing combos */
+        tmp = gtk_frame_new ("Unconstrained Menu");
+        gtk_box_pack_start (GTK_BOX (mainbox), tmp, FALSE, FALSE, 0);
+
+        boom = gtk_vbox_new (FALSE, 0);
+        gtk_container_set_border_width (GTK_CONTAINER (boom), 5);
+        gtk_container_add (GTK_CONTAINER (tmp), boom);
+
+	model = create_list_long ();
+	combobox = gtk_combo_box_new_with_model (model);
+        g_object_unref (model);
+        gtk_container_add (GTK_CONTAINER (boom), combobox);
+        renderer = gtk_cell_renderer_text_new ();
+	g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
+
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+                                        "text", 0, NULL);
+        gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
+	gtk_combo_box_set_popup_fixed_width (GTK_COMBO_BOX (combobox), FALSE);
+
         gtk_widget_show_all (window);
 
         gtk_main ();
