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
This commit is contained in:
Matthias Clasen
2012-08-06 22:26:13 -04:00
parent 4942b6f5e4
commit 5a3442bf9c
14 changed files with 178 additions and 84 deletions

View File

@ -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 \

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -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)
/* treat the default align value like START */
if (align == GTK_ALIGN_FILL)
align = GTK_ALIGN_START;
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;
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);
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)
*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;
}
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;
/* treat the default align value like START */
if (align == GTK_ALIGN_FILL)
align = GTK_ALIGN_START;
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;
}