commit 7ef0faddd09cfdfa1453f41a4dba370befe83d13
Author: Tristan Van Berkom <tristan.van.berkom@gmail.com>
Date:   Wed Oct 6 01:26:22 2010 +0900

    Added GtkComboMenuGrowPolicy enum and property
    
    Added a property to specify how the combo box's menu can
    grow to it's natural size; either not grow at all, or grow
    to the left/right or from the center of the combo box's allocation.

diff --git a/gtk/gtkcombobox.c b/gtk/gtkcombobox.c
index aeb5e91..44dd58f 100644
--- a/gtk/gtkcombobox.c
+++ b/gtk/gtkcombobox.c
@@ -141,6 +141,8 @@ struct _GtkComboBoxPrivate
   gint  minimum_width;
   gint  natural_width;
 
+  GtkComboMenuGrowPolicy menu_grow_policy;
+
   GSList *cells;
 
   guint popup_in_progress : 1;
@@ -238,7 +240,8 @@ enum {
   PROP_FOCUS_ON_CLICK,
   PROP_POPUP_SHOWN,
   PROP_BUTTON_SENSITIVITY,
-  PROP_EDITING_CANCELED
+  PROP_EDITING_CANCELED,
+  PROP_GROW_POLICY
 };
 
 static guint combo_box_signals[LAST_SIGNAL] = {0,};
@@ -896,6 +899,29 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass)
                                                        GTK_SENSITIVITY_AUTO,
                                                        GTK_PARAM_READWRITE));
 
+   /**
+    * GtkComboBox:menu-grow-policy:
+    *
+    * Specifies the #GtkComboMenuGrowPolicy. 
+    *
+    * This is used to decide how a combo's drop-down menu will grow in width on 
+    * either side of the combo box.
+    *
+    * The default behaviour is to always use the same width as the combo box 
+    * for the drop down menu.
+    *
+    * Since: 3.0
+    */
+   g_object_class_install_property (object_class,
+                                    PROP_GROW_POLICY,
+                                    g_param_spec_enum ("menu-grow-policy",
+                                                       P_("Menu Grow Policy"),
+                                                       P_("Specifies the #GtkComboMenuGrowPolicy"),
+                                                       GTK_TYPE_COMBO_MENU_GROW_POLICY,
+                                                       GTK_COMBO_MENU_GROW_NONE,
+                                                       GTK_PARAM_READWRITE));
+
+
   gtk_widget_class_install_style_property (widget_class,
                                            g_param_spec_boolean ("appears-as-list",
                                                                  P_("Appears as list"),
@@ -1064,6 +1090,11 @@ gtk_combo_box_set_property (GObject      *object,
                                             g_value_get_enum (value));
       break;
 
+    case PROP_GROW_POLICY:
+      gtk_combo_box_set_menu_grow_policy (combo_box,
+					  g_value_get_enum (value));
+      break;
+
     case PROP_EDITING_CANCELED:
       combo_box->priv->editing_canceled = g_value_get_boolean (value);
       break;
@@ -1129,6 +1160,10 @@ gtk_combo_box_get_property (GObject    *object,
         g_value_set_enum (value, combo_box->priv->button_sensitivity);
         break;
 
+      case PROP_GROW_POLICY:
+        g_value_set_enum (value, combo_box->priv->menu_grow_policy);
+        break;
+
       case PROP_EDITING_CANCELED:
         g_value_set_boolean (value, priv->editing_canceled);
         break;
@@ -1523,6 +1558,43 @@ gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
 }
 
 static void
+gtk_combo_box_menu_offset (GtkComboBox *combo_box,
+			   GtkMenu     *menu,
+			   gint        *x)
+{
+  GtkAllocation allocation;
+  gint menu_width, menu_offset;
+
+  /* Position the x coordinate of the menu according to the menu-grow-policy */
+  switch (combo_box->priv->menu_grow_policy)
+    {
+    case GTK_COMBO_MENU_GROW_LEFT:
+      gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
+      gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
+
+      menu_offset = menu_width - allocation.width;
+
+      *x -= MAX (menu_offset, 0);
+      break;
+    case GTK_COMBO_MENU_GROW_CENTER:
+      gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
+      gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
+
+      menu_offset = (menu_width - allocation.width) / 2;
+
+      *x -= MAX (menu_offset, 0);
+      break;
+
+      /* No need to reposition the x coordinate if the menu grows to the right */
+    case GTK_COMBO_MENU_GROW_RIGHT:
+    case GTK_COMBO_MENU_GROW_NONE:
+    default:
+      break;
+    }
+}
+
+
+static void
 gtk_combo_box_menu_position_below (GtkMenu  *menu,
 				   gint     *x,
 				   gint     *y,
@@ -1554,11 +1626,17 @@ gtk_combo_box_menu_position_below (GtkMenu  *menu,
   gdk_window_get_root_coords (gtk_widget_get_window (child),
                               sx, sy, &sx, &sy);
 
+  gtk_combo_box_menu_offset (combo_box, menu, &sx);
+
   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->menu_grow_policy != GTK_COMBO_MENU_GROW_NONE)
+    gtk_widget_get_preferred_size (GTK_WIDGET (menu),
+				   NULL, &req);
+  else
+    gtk_widget_get_preferred_size (GTK_WIDGET (menu),
+				   &req, NULL);
 
   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
     *x = sx;
@@ -1617,7 +1695,12 @@ 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);
+  gtk_combo_box_menu_offset (combo_box, menu, &menu_xpos);
+
+  if (combo_box->priv->menu_grow_policy != GTK_COMBO_MENU_GROW_NONE)
+    gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
+  else
+    gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
 
   if (active != NULL)
     {
@@ -1675,7 +1758,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
@@ -1894,7 +1976,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 +2001,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 (priv->menu_grow_policy != GTK_COMBO_MENU_GROW_NONE)
+	width = MAX (width, nat_width);
+      else
+	width = MAX (width, min_width);
+
+      gtk_widget_set_size_request (priv->popup_widget, width, -1);
     }
   
   gtk_menu_popup (GTK_MENU (priv->popup_widget),
@@ -2320,7 +2406,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 +2415,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 (priv->menu_grow_policy != GTK_COMBO_MENU_GROW_NONE)
+		    gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
+		  else
+		    gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
+
                   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 +5983,51 @@ gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
 }
 
 
+
+/**
+ * gtk_combo_box_set_menu_grow_policy:
+ * @combo_box: a #GtkComboBox
+ * @policy: the #GtkComboMenuGrowPolicy to use
+ *
+ * Specify how a combo's drop-down menu will grow in width on 
+ * either side of the combo box.
+ *
+ * With the default policy %GTK_COMBO_MENU_GROW_NONE; the menu
+ * will always be allocated the same width as @combo_box.
+ *
+ * Since: 3.0
+ **/
+void
+gtk_combo_box_set_menu_grow_policy (GtkComboBox            *combo_box,
+				    GtkComboMenuGrowPolicy  policy)
+{
+  g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+
+  if (combo_box->priv->menu_grow_policy != policy)
+    {
+      combo_box->priv->menu_grow_policy = policy;
+
+      g_object_notify (G_OBJECT (combo_box), "menu-grow-policy");
+    }
+}
+
+/**
+ * gtk_combo_box_get_menu_grow_policy:
+ * @combo_box: a #GtkComboBox
+ *
+ * Gets the current #GtkComboMenuGrowPolicy.
+ *
+ * Since: 3.0
+ **/
+GtkComboMenuGrowPolicy
+gtk_combo_box_get_menu_grow_policy (GtkComboBox *combo_box)
+{
+  g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
+
+  return combo_box->priv->button_sensitivity;
+}
+
+
 /**
  * gtk_combo_box_set_focus_on_click:
  * @combo: a #GtkComboBox
diff --git a/gtk/gtkcombobox.h b/gtk/gtkcombobox.h
index 84f5475..56d7b2d 100644
--- a/gtk/gtkcombobox.h
+++ b/gtk/gtkcombobox.h
@@ -118,6 +118,11 @@ 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_menu_grow_policy (GtkComboBox            *combo_box,
+							   GtkComboMenuGrowPolicy  policy);
+GtkComboMenuGrowPolicy gtk_combo_box_get_menu_grow_policy (GtkComboBox            *combo_box);
+
+
 /* convenience -- text */
 GtkWidget    *gtk_combo_box_new_text         (void);
 void          gtk_combo_box_append_text      (GtkComboBox     *combo_box,
diff --git a/gtk/gtkenums.h b/gtk/gtkenums.h
index 98cd0a5..bab913d 100644
--- a/gtk/gtkenums.h
+++ b/gtk/gtkenums.h
@@ -594,6 +594,26 @@ typedef enum
   GTK_WRAP_BOX_V_EXPAND = 1 << 1
 } GtkWrapBoxPacking;
 
+/**
+ * GtkComboMenuGrowPolicy:
+ * @GTK_COMBO_MENU_GROW_NONE:   The menu gets the same width that was allocated to the combo box.
+ * @GTK_COMBO_MENU_GROW_LEFT:   The menu gets as much of it's natural width as possible while growing
+ *                              to the left of the combo box.
+ * @GTK_COMBO_MENU_GROW_RIGHT:  The menu gets as much of it's natural width as possible while growing
+ *                              to the right of the combo box.
+ * @GTK_COMBO_MENU_GROW_CENTER: The menu gets as much of it's natural width as possible while growing
+ *                              out on both sides of the combo box.
+ * 
+ * Specifies the policy #GtkComboBox will use to allocate a width for it's drop-down menu.
+ */
+typedef enum
+{
+  GTK_COMBO_MENU_GROW_NONE,
+  GTK_COMBO_MENU_GROW_LEFT,
+  GTK_COMBO_MENU_GROW_RIGHT,
+  GTK_COMBO_MENU_GROW_CENTER
+} GtkComboMenuGrowPolicy;
+
 
 G_END_DECLS
 
diff --git a/tests/testcombo.c b/tests/testcombo.c
index bf01142..9e3b19f 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 growing out in either direction",
+                            -1);
+
+        return GTK_TREE_MODEL (store);
+}
+
 /* blaat */
 static GtkTreeModel *
 create_phylogenetic_tree (void)
@@ -1335,6 +1368,68 @@ main (int argc, char **argv)
 	gdk_threads_add_timeout (1000, (GSourceFunc) capital_animation, model);
 #endif
 
+	/* Ellipsizing growing combos */
+        tmp = gtk_frame_new ("Growing Left");
+        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_menu_grow_policy (GTK_COMBO_BOX (combobox), GTK_COMBO_MENU_GROW_LEFT);
+
+        tmp = gtk_frame_new ("Growing Right");
+        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_menu_grow_policy (GTK_COMBO_BOX (combobox), GTK_COMBO_MENU_GROW_RIGHT);
+
+        tmp = gtk_frame_new ("Growing Center");
+        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_menu_grow_policy (GTK_COMBO_BOX (combobox), GTK_COMBO_MENU_GROW_CENTER);
+
+
         gtk_widget_show_all (window);
 
         gtk_main ();
