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
@ -382,7 +382,19 @@ HTML_IMAGES = \
|
|||||||
$(srcdir)/images/numerableicon2.png \
|
$(srcdir)/images/numerableicon2.png \
|
||||||
$(srcdir)/images/bloatpad-osx.png \
|
$(srcdir)/images/bloatpad-osx.png \
|
||||||
$(srcdir)/images/bloatpad-gnome.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
|
# Extra options to supply to gtkdoc-fixref
|
||||||
FIXXREF_OPTIONS=--extra-dir=../gdk/html \
|
FIXXREF_OPTIONS=--extra-dir=../gdk/html \
|
||||||
|
|||||||
BIN
docs/reference/gtk/images/down-center.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/down-end.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/down-start.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/left-center.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
docs/reference/gtk/images/left-end.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/left-start.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
BIN
docs/reference/gtk/images/right-center.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
docs/reference/gtk/images/right-end.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
docs/reference/gtk/images/right-start.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/up-center.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/up-end.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
docs/reference/gtk/images/up-start.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
@ -26,9 +26,118 @@
|
|||||||
* The #GtkMenuButton widget is used to display a menu when clicked on.
|
* 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.
|
* 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
|
* The #GtkMenuButton widget can hold any valid child widget. That is, it
|
||||||
* almost any other standard #GtkWidget. The most commonly used child is the
|
* can hold almost any other standard #GtkWidget. The most commonly used
|
||||||
* provided #GtkArrow.
|
* 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"
|
#include "config.h"
|
||||||
@ -132,7 +241,7 @@ gtk_menu_button_state_flags_changed (GtkWidget *widget,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
menu_position_down_func (GtkMenu *menu,
|
menu_position_up_down_func (GtkMenu *menu,
|
||||||
gint *x,
|
gint *x,
|
||||||
gint *y,
|
gint *y,
|
||||||
gboolean *push_in,
|
gboolean *push_in,
|
||||||
@ -147,14 +256,12 @@ menu_position_down_func (GtkMenu *menu,
|
|||||||
GdkScreen *screen;
|
GdkScreen *screen;
|
||||||
GdkWindow *window;
|
GdkWindow *window;
|
||||||
GtkAllocation allocation, arrow_allocation;
|
GtkAllocation allocation, arrow_allocation;
|
||||||
GtkWidget *toplevel;
|
GtkAlign align;
|
||||||
|
|
||||||
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->menu));
|
|
||||||
gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
|
|
||||||
|
|
||||||
gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
|
gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
|
||||||
&menu_req, NULL);
|
&menu_req, NULL);
|
||||||
|
|
||||||
|
align = gtk_widget_get_halign (GTK_WIDGET (priv->menu));
|
||||||
direction = gtk_widget_get_direction (widget);
|
direction = gtk_widget_get_direction (widget);
|
||||||
window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
|
window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
|
||||||
|
|
||||||
@ -171,11 +278,24 @@ menu_position_down_func (GtkMenu *menu,
|
|||||||
*x += allocation.x;
|
*x += allocation.x;
|
||||||
*y += allocation.y;
|
*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);
|
*x += MAX (allocation.width - menu_req.width, 0);
|
||||||
else if (menu_req.width > allocation.width)
|
else if (menu_req.width > allocation.width)
|
||||||
*x -= menu_req.width - allocation.width;
|
*x -= menu_req.width - allocation.width;
|
||||||
|
|
||||||
|
if (priv->arrow_type == GTK_ARROW_UP && *y - menu_req.height >= monitor.y)
|
||||||
|
{
|
||||||
|
*y -= menu_req.height;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
|
if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
|
||||||
*y += arrow_allocation.height;
|
*y += arrow_allocation.height;
|
||||||
else if ((*y - menu_req.height) >= monitor.y)
|
else if ((*y - menu_req.height) >= monitor.y)
|
||||||
@ -184,58 +304,6 @@ menu_position_down_func (GtkMenu *menu,
|
|||||||
*y += arrow_allocation.height;
|
*y += arrow_allocation.height;
|
||||||
else
|
else
|
||||||
*y -= menu_req.height;
|
*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)
|
|
||||||
{
|
|
||||||
menu_position_down_func (menu, x, y, push_in, menu_button);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*push_in = FALSE;
|
*push_in = FALSE;
|
||||||
@ -249,19 +317,21 @@ menu_position_side_func (GtkMenu *menu,
|
|||||||
GtkMenuButton *menu_button)
|
GtkMenuButton *menu_button)
|
||||||
{
|
{
|
||||||
GtkMenuButtonPrivate *priv = menu_button->priv;
|
GtkMenuButtonPrivate *priv = menu_button->priv;
|
||||||
GtkAllocation toggle_allocation;
|
GtkAllocation allocation;
|
||||||
GtkWidget *widget = GTK_WIDGET (menu_button);
|
GtkWidget *widget = GTK_WIDGET (menu_button);
|
||||||
GtkRequisition menu_req;
|
GtkRequisition menu_req;
|
||||||
GdkRectangle monitor;
|
GdkRectangle monitor;
|
||||||
gint monitor_num;
|
gint monitor_num;
|
||||||
GdkScreen *screen;
|
GdkScreen *screen;
|
||||||
GdkWindow *window;
|
GdkWindow *window;
|
||||||
|
GtkAlign align;
|
||||||
|
|
||||||
gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
|
gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
|
||||||
&menu_req, NULL);
|
&menu_req, NULL);
|
||||||
|
|
||||||
window = gtk_widget_get_window (widget);
|
window = gtk_widget_get_window (widget);
|
||||||
|
|
||||||
|
align = gtk_widget_get_valign (GTK_WIDGET (menu));
|
||||||
screen = gtk_widget_get_screen (GTK_WIDGET (menu));
|
screen = gtk_widget_get_screen (GTK_WIDGET (menu));
|
||||||
monitor_num = gdk_screen_get_monitor_at_window (screen, window);
|
monitor_num = gdk_screen_get_monitor_at_window (screen, window);
|
||||||
if (monitor_num < 0)
|
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);
|
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)
|
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
|
else
|
||||||
*x -= menu_req.width;
|
*x -= menu_req.width;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (*x - menu_req.width >= monitor.x)
|
||||||
|
*x -= menu_req.width;
|
||||||
|
else
|
||||||
|
*x += allocation.width;
|
||||||
|
}
|
||||||
|
|
||||||
if (*y + menu_req.height > monitor.y + monitor.height &&
|
/* treat the default align value like START */
|
||||||
*y + toggle_allocation.height - monitor.y > monitor.y + monitor.height - *y)
|
if (align == GTK_ALIGN_FILL)
|
||||||
*y += toggle_allocation.height - menu_req.height;
|
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;
|
*push_in = FALSE;
|
||||||
}
|
}
|
||||||
@ -299,15 +384,12 @@ popup_menu (GtkMenuButton *menu_button,
|
|||||||
|
|
||||||
switch (priv->arrow_type)
|
switch (priv->arrow_type)
|
||||||
{
|
{
|
||||||
case GTK_ARROW_UP:
|
|
||||||
func = (GtkMenuPositionFunc) menu_position_up_func;
|
|
||||||
break;
|
|
||||||
case GTK_ARROW_LEFT:
|
case GTK_ARROW_LEFT:
|
||||||
case GTK_ARROW_RIGHT:
|
case GTK_ARROW_RIGHT:
|
||||||
func = (GtkMenuPositionFunc) menu_position_side_func;
|
func = (GtkMenuPositionFunc) menu_position_side_func;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
func = (GtkMenuPositionFunc) menu_position_down_func;
|
func = (GtkMenuPositionFunc) menu_position_up_down_func;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||