[gtk+/composite-templates] Improve GtkMenuButton menu positioning



commit 0fed8ba4972cb87a9e0f9c24941ea4d48340c9c4
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Aug 6 22:26:13 2012 -0400

    Improve GtkMenuButton menu positioning
    
    Take the menu halign/valign into consideration when positioning
    the menu. Also, document the various combinations.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680988

 docs/reference/gtk/Makefile.am             |   14 ++-
 docs/reference/gtk/images/down-center.png  |  Bin 0 -> 2194 bytes
 docs/reference/gtk/images/down-end.png     |  Bin 0 -> 2170 bytes
 docs/reference/gtk/images/down-start.png   |  Bin 0 -> 2177 bytes
 docs/reference/gtk/images/left-center.png  |  Bin 0 -> 2234 bytes
 docs/reference/gtk/images/left-end.png     |  Bin 0 -> 2196 bytes
 docs/reference/gtk/images/left-start.png   |  Bin 0 -> 2099 bytes
 docs/reference/gtk/images/right-center.png |  Bin 0 -> 2250 bytes
 docs/reference/gtk/images/right-end.png    |  Bin 0 -> 2241 bytes
 docs/reference/gtk/images/right-start.png  |  Bin 0 -> 2110 bytes
 docs/reference/gtk/images/up-center.png    |  Bin 0 -> 2171 bytes
 docs/reference/gtk/images/up-end.png       |  Bin 0 -> 2123 bytes
 docs/reference/gtk/images/up-start.png     |  Bin 0 -> 2148 bytes
 gtk/gtkmenubutton.c                        |  246 ++++++++++++++++++---------
 14 files changed, 177 insertions(+), 83 deletions(-)
---
diff --git a/docs/reference/gtk/Makefile.am b/docs/reference/gtk/Makefile.am
index 0d86b6a..34bcf02 100644
--- a/docs/reference/gtk/Makefile.am
+++ b/docs/reference/gtk/Makefile.am
@@ -382,7 +382,19 @@ HTML_IMAGES = \
 	$(srcdir)/images/numerableicon2.png				\
 	$(srcdir)/images/bloatpad-osx.png				\
 	$(srcdir)/images/bloatpad-gnome.png				\
-	$(srcdir)/images/bloatpad-xfce.png
+	$(srcdir)/images/bloatpad-xfce.png				\
+	$(srcdir)/images/down-center.png				\
+	$(srcdir)/images/down-end.png					\
+	$(srcdir)/images/down-start.png					\
+	$(srcdir)/images/left-center.png				\
+	$(srcdir)/images/left-end.png					\
+	$(srcdir)/images/left-start.png					\
+	$(srcdir)/images/right-center.png				\
+	$(srcdir)/images/right-end.png					\
+	$(srcdir)/images/right-start.png				\
+	$(srcdir)/images/up-center.png					\
+	$(srcdir)/images/up-end.png					\
+	$(srcdir)/images/up-start.png
 
 # Extra options to supply to gtkdoc-fixref
 FIXXREF_OPTIONS=--extra-dir=../gdk/html \
diff --git a/docs/reference/gtk/images/down-center.png b/docs/reference/gtk/images/down-center.png
new file mode 100644
index 0000000..c42ccbe
Binary files /dev/null and b/docs/reference/gtk/images/down-center.png differ
diff --git a/docs/reference/gtk/images/down-end.png b/docs/reference/gtk/images/down-end.png
new file mode 100644
index 0000000..f2a2f6f
Binary files /dev/null and b/docs/reference/gtk/images/down-end.png differ
diff --git a/docs/reference/gtk/images/down-start.png b/docs/reference/gtk/images/down-start.png
new file mode 100644
index 0000000..9d68db0
Binary files /dev/null and b/docs/reference/gtk/images/down-start.png differ
diff --git a/docs/reference/gtk/images/left-center.png b/docs/reference/gtk/images/left-center.png
new file mode 100644
index 0000000..0473d65
Binary files /dev/null and b/docs/reference/gtk/images/left-center.png differ
diff --git a/docs/reference/gtk/images/left-end.png b/docs/reference/gtk/images/left-end.png
new file mode 100644
index 0000000..d5101be
Binary files /dev/null and b/docs/reference/gtk/images/left-end.png differ
diff --git a/docs/reference/gtk/images/left-start.png b/docs/reference/gtk/images/left-start.png
new file mode 100644
index 0000000..618f3dc
Binary files /dev/null and b/docs/reference/gtk/images/left-start.png differ
diff --git a/docs/reference/gtk/images/right-center.png b/docs/reference/gtk/images/right-center.png
new file mode 100644
index 0000000..401b4d3
Binary files /dev/null and b/docs/reference/gtk/images/right-center.png differ
diff --git a/docs/reference/gtk/images/right-end.png b/docs/reference/gtk/images/right-end.png
new file mode 100644
index 0000000..8af0ac1
Binary files /dev/null and b/docs/reference/gtk/images/right-end.png differ
diff --git a/docs/reference/gtk/images/right-start.png b/docs/reference/gtk/images/right-start.png
new file mode 100644
index 0000000..e08f314
Binary files /dev/null and b/docs/reference/gtk/images/right-start.png differ
diff --git a/docs/reference/gtk/images/up-center.png b/docs/reference/gtk/images/up-center.png
new file mode 100644
index 0000000..1d12224
Binary files /dev/null and b/docs/reference/gtk/images/up-center.png differ
diff --git a/docs/reference/gtk/images/up-end.png b/docs/reference/gtk/images/up-end.png
new file mode 100644
index 0000000..e3882af
Binary files /dev/null and b/docs/reference/gtk/images/up-end.png differ
diff --git a/docs/reference/gtk/images/up-start.png b/docs/reference/gtk/images/up-start.png
new file mode 100644
index 0000000..7315780
Binary files /dev/null and b/docs/reference/gtk/images/up-start.png differ
diff --git a/gtk/gtkmenubutton.c b/gtk/gtkmenubutton.c
index 8a76361..a1aaa8c 100644
--- a/gtk/gtkmenubutton.c
+++ b/gtk/gtkmenubutton.c
@@ -26,9 +26,118 @@
  * The #GtkMenuButton widget is used to display a menu when clicked on.
  * This menu can be provided either as a #GtkMenu, or an abstract #GMenuModel.
  *
- * The #GtkMenuButton widget can hold any valid child widget.  That is, it can hold
- * almost any other standard #GtkWidget. The most commonly used child is the
- * provided #GtkArrow.
+ * The #GtkMenuButton widget can hold any valid child widget. That is, it
+ * can hold almost any other standard #GtkWidget. The most commonly used
+ * child is the provided #GtkArrow.
+ *
+ * The positioning of the menu is determined by the #GtkMenuButton:direction
+ * property of the menu button and the #GtkWidget:halign or #GtkWidget:valign
+ * properties of the menu. For example, when the direction is %GTK_ARROW_DOWN
+ * and the horizontal alignment is %GTK_ALIGN_START, the menu will be
+ * positioned below the button, with the starting edge (depending on the
+ * text direction) of the menu aligned with the starting edge of the button.
+ * If there is not enough space below the button, the menu is popped up above
+ * the button instead. If the alignment would move part of the menu offscreen,
+ * it is 'pushed in'.
+ *
+ * <informaltable>
+ *   <tgroup cols="4">
+ *     <tbody>
+ *       <row>
+ *         <entry></entry>
+ *         <entry>halign = start</entry>
+ *         <entry>halign = center</entry>
+ *         <entry>halign = end</entry>
+ *       </row>
+ *     <row>
+ *       <entry>direction = down</entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="down-start.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="down-center.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="down-end.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *     </row>
+ *     <row>
+ *       <entry>direction = up</entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="up-start.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="up-center.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="up-end.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *      </row>
+ *     </tbody>
+ *   </tgroup>
+ * </informaltable>
+ * <informaltable>
+ *   <tgroup cols="3">
+ *     <tbody>
+ *       <row>
+ *         <entry></entry>
+ *         <entry>direction = left</entry>
+ *         <entry>direction = right</entry>
+ *       </row>
+ *     <row>
+ *       <entry>valign = start</entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="left-start.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="right-start.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *     </row>
+ *     <row>
+ *       <entry>valign = center</entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="left-center.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="right-center.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *     </row>
+ *     <row>
+ *       <entry>valign = end</entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="left-end.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *       <entry>
+ *         <inlinemediaobject>
+ *           <imageobject><imagedata fileref="right-end.png" format="PNG"/></imageobject>
+ *         </inlinemediaobject>
+ *       </entry>
+ *      </row>
+ *     </tbody>
+ *   </tgroup>
+ * </informaltable>
  */
 
 #include "config.h"
@@ -132,11 +241,11 @@ gtk_menu_button_state_flags_changed (GtkWidget    *widget,
 }
 
 static void
-menu_position_down_func (GtkMenu       *menu,
-                         gint          *x,
-                         gint          *y,
-                         gboolean      *push_in,
-                         GtkMenuButton *menu_button)
+menu_position_up_down_func (GtkMenu       *menu,
+                            gint          *x,
+                            gint          *y,
+                            gboolean      *push_in,
+                            GtkMenuButton *menu_button)
 {
   GtkMenuButtonPrivate *priv = menu_button->priv;
   GtkWidget *widget = GTK_WIDGET (menu_button);
@@ -147,14 +256,12 @@ menu_position_down_func (GtkMenu       *menu,
   GdkScreen *screen;
   GdkWindow *window;
   GtkAllocation allocation, arrow_allocation;
-  GtkWidget *toplevel;
-
-  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->menu));
-  gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
+  GtkAlign align;
 
   gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
                                  &menu_req, NULL);
 
+  align = gtk_widget_get_halign (GTK_WIDGET (priv->menu));
   direction = gtk_widget_get_direction (widget);
   window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
 
@@ -171,71 +278,32 @@ menu_position_down_func (GtkMenu       *menu,
   *x += allocation.x;
   *y += allocation.y;
 
-  if (direction == GTK_TEXT_DIR_LTR)
-    *x += MAX (allocation.width - menu_req.width, 0);
-  else if (menu_req.width > allocation.width)
-    *x -= menu_req.width - allocation.width;
-
-  if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
-    *y += arrow_allocation.height;
-  else if ((*y - menu_req.height) >= monitor.y)
-    *y -= menu_req.height;
-  else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
-    *y += arrow_allocation.height;
-  else
-    *y -= menu_req.height;
-
-  *push_in = FALSE;
-}
-
-static void
-menu_position_up_func (GtkMenu       *menu,
-                       gint          *x,
-                       gint          *y,
-                       gboolean      *push_in,
-                       GtkMenuButton *menu_button)
-{
-  GtkMenuButtonPrivate *priv = menu_button->priv;
-  GtkWidget *widget = GTK_WIDGET (menu_button);
-  GtkRequisition menu_req;
-  GtkTextDirection direction;
-  GdkRectangle monitor;
-  gint monitor_num;
-  GdkScreen *screen;
-  GdkWindow *window;
-  GtkAllocation allocation, arrow_allocation;
-
-  gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
-                                 &menu_req, NULL);
+  /* treat the default align value like START */
+  if (align == GTK_ALIGN_FILL)
+    align = GTK_ALIGN_START;
 
-  direction = gtk_widget_get_direction (widget);
-  window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
-
-  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-  monitor_num = gdk_screen_get_monitor_at_window (screen, window);
-  if (monitor_num < 0)
-    monitor_num = 0;
-  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
-
-  gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
-  gtk_widget_get_allocation (widget, &arrow_allocation);
-
-  gdk_window_get_origin (window, x, y);
-  *x += allocation.x;
-  *y += allocation.y;
-
-  if (direction == GTK_TEXT_DIR_LTR)
+  if (align == GTK_ALIGN_CENTER)
+    *x -= (menu_req.width - allocation.width) / 2;
+  else if ((align == GTK_ALIGN_START && direction == GTK_TEXT_DIR_LTR) ||
+           (align == GTK_ALIGN_END && direction == GTK_TEXT_DIR_RTL))
     *x += MAX (allocation.width - menu_req.width, 0);
   else if (menu_req.width > allocation.width)
     *x -= menu_req.width - allocation.width;
 
-  *y -= menu_req.height;
-
-  /* If we're going to clip the top, pop down instead */
-  if (*y < monitor.y)
+  if (priv->arrow_type == GTK_ARROW_UP && *y - menu_req.height >= monitor.y)
     {
-      menu_position_down_func (menu, x, y, push_in, menu_button);
-      return;
+      *y -= menu_req.height;
+    }
+  else
+    {
+      if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
+        *y += arrow_allocation.height;
+      else if ((*y - menu_req.height) >= monitor.y)
+        *y -= menu_req.height;
+      else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
+        *y += arrow_allocation.height;
+      else
+        *y -= menu_req.height;
     }
 
   *push_in = FALSE;
@@ -249,19 +317,21 @@ menu_position_side_func (GtkMenu       *menu,
                          GtkMenuButton *menu_button)
 {
   GtkMenuButtonPrivate *priv = menu_button->priv;
-  GtkAllocation toggle_allocation;
+  GtkAllocation allocation;
   GtkWidget *widget = GTK_WIDGET (menu_button);
   GtkRequisition menu_req;
   GdkRectangle monitor;
   gint monitor_num;
   GdkScreen *screen;
   GdkWindow *window;
+  GtkAlign align;
 
   gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
                                  &menu_req, NULL);
 
   window = gtk_widget_get_window (widget);
 
+  align = gtk_widget_get_valign (GTK_WIDGET (menu));
   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
   if (monitor_num < 0)
@@ -270,16 +340,31 @@ menu_position_side_func (GtkMenu       *menu,
 
   gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);
 
-  gtk_widget_get_allocation (widget, &toggle_allocation);
+  gtk_widget_get_allocation (widget, &allocation);
 
   if (priv->arrow_type == GTK_ARROW_RIGHT)
-    *x += toggle_allocation.width;
+    {
+      if (*x + allocation.width + menu_req.width <= monitor.x + monitor.width)
+        *x += allocation.width;
+      else
+        *x -= menu_req.width;
+    }
   else
-    *x -= menu_req.width;
+    {
+      if (*x - menu_req.width >= monitor.x)
+        *x -= menu_req.width;
+      else
+        *x += allocation.width;
+    }
+
+  /* treat the default align value like START */
+  if (align == GTK_ALIGN_FILL)
+    align = GTK_ALIGN_START;
 
-  if (*y + menu_req.height > monitor.y + monitor.height &&
-      *y + toggle_allocation.height - monitor.y > monitor.y + monitor.height - *y)
-    *y += toggle_allocation.height - menu_req.height;
+  if (align == GTK_ALIGN_CENTER)
+    *y -= (menu_req.height - allocation.height) / 2;
+  else if (align == GTK_ALIGN_END)
+    *y -= menu_req.height - allocation.height;
 
   *push_in = FALSE;
 }
@@ -299,15 +384,12 @@ popup_menu (GtkMenuButton  *menu_button,
 
   switch (priv->arrow_type)
     {
-      case GTK_ARROW_UP:
-        func = (GtkMenuPositionFunc) menu_position_up_func;
-        break;
       case GTK_ARROW_LEFT:
       case GTK_ARROW_RIGHT:
         func = (GtkMenuPositionFunc) menu_position_side_func;
         break;
       default:
-        func = (GtkMenuPositionFunc) menu_position_down_func;
+        func = (GtkMenuPositionFunc) menu_position_up_down_func;
         break;
   }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]