summaryrefslogtreecommitdiff
path: root/gtk/gtkmenuitem.c
diff options
context:
space:
mode:
authorCosimo Cecchi <cosimoc@gnome.org>2011-12-15 18:42:27 +0100
committerCosimo Cecchi <cosimoc@gnome.org>2011-12-15 18:52:30 +0100
commit05e62e02510685ecf223c3ee384083448bd8268d (patch)
tree3d2e22cb48732276a50b454c2fb10e14eb836c6d /gtk/gtkmenuitem.c
parent4d76b10ce5ff782fedc3e77e4a43a91415f00562 (diff)
downloadgtk+-05e62e02510685ecf223c3ee384083448bd8268d.tar.gz
menuitem: share code between HFW and non-HFW height requests
The code is very similar; having two slightly different code paths is bad and can lead to bugs. Refactor the code to use the same height request function.
Diffstat (limited to 'gtk/gtkmenuitem.c')
-rw-r--r--gtk/gtkmenuitem.c190
1 files changed, 52 insertions, 138 deletions
diff --git a/gtk/gtkmenuitem.c b/gtk/gtkmenuitem.c
index 9c9dea3a3c..3aeb1aa48c 100644
--- a/gtk/gtkmenuitem.c
+++ b/gtk/gtkmenuitem.c
@@ -856,9 +856,10 @@ gtk_menu_item_get_preferred_width (GtkWidget *widget,
}
static void
-gtk_menu_item_get_preferred_height (GtkWidget *widget,
- gint *minimum_size,
- gint *natural_size)
+gtk_menu_item_real_get_height (GtkWidget *widget,
+ gint for_size,
+ gint *minimum_size,
+ gint *natural_size)
{
GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
GtkMenuItemPrivate *priv = menu_item->priv;
@@ -873,7 +874,8 @@ gtk_menu_item_get_preferred_height (GtkWidget *widget,
guint border_width;
GtkPackDirection pack_dir;
GtkPackDirection child_pack_dir;
- gint min_height, nat_height;
+ gint min_height, nat_height;
+ gint avail_size;
min_height = nat_height = 0;
@@ -900,135 +902,22 @@ gtk_menu_item_get_preferred_height (GtkWidget *widget,
}
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
- min_height = (border_width * 2) + padding.top + padding.bottom;
+ min_height = (border_width * 2) + padding.top + padding.bottom;
if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
(child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
min_height += 2 * horizontal_padding;
- nat_height = min_height;
-
- child = gtk_bin_get_child (bin);
-
- if (child != NULL && gtk_widget_get_visible (child))
- {
- GtkMenuItemPrivate *priv = menu_item->priv;
- gint child_min, child_nat;
-
- gtk_widget_get_preferred_height (child, &child_min, &child_nat);
-
- min_height += child_min;
- nat_height += child_nat;
-
- if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
- {
- gint arrow_size;
-
- get_arrow_size (widget, child, &arrow_size);
-
- min_height = MAX (min_height, arrow_size);
- nat_height = MAX (nat_height, arrow_size);
- }
- }
- else /* separator item */
+ if (for_size != -1)
{
- gboolean wide_separators;
- gint separator_height;
-
- gtk_widget_style_get (widget,
- "wide-separators", &wide_separators,
- "separator-height", &separator_height,
- NULL);
-
- if (wide_separators)
- {
- min_height += separator_height;
- nat_height += separator_height;
- }
- else
- {
- /* force odd, so that we can have the same space above and
- * below the line.
- */
- if (min_height % 2 == 0)
- min_height += 1;
- if (nat_height % 2 == 0)
- nat_height += 1;
- }
- }
-
- accel_width = 0;
- gtk_container_foreach (GTK_CONTAINER (menu_item),
- gtk_menu_item_accel_width_foreach,
- &accel_width);
- priv->accelerator_width = accel_width;
-
- if (minimum_size)
- *minimum_size = min_height;
-
- if (natural_size)
- *natural_size = nat_height;
-}
-
-static void
-gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
- gint for_size,
- gint *minimum_size,
- gint *natural_size)
-{
- GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
- GtkMenuItemPrivate *priv = menu_item->priv;
- GtkBin *bin;
- GtkStyleContext *context;
- GtkStateFlags state;
- GtkBorder padding;
- GtkWidget *child;
- GtkWidget *parent;
- guint horizontal_padding;
- guint border_width;
- GtkPackDirection pack_dir;
- GtkPackDirection child_pack_dir;
- gint min_height, nat_height;
- gint avail_size;
-
- min_height = nat_height = 0;
-
- context = gtk_widget_get_style_context (widget);
- state = gtk_widget_get_state_flags (widget);
- gtk_style_context_get_padding (context, state, &padding);
-
- gtk_widget_style_get (widget,
- "horizontal-padding", &horizontal_padding,
- NULL);
-
- bin = GTK_BIN (widget);
- parent = gtk_widget_get_parent (widget);
+ avail_size = for_size;
+ avail_size -= (border_width * 2) + padding.left + padding.right;
- if (GTK_IS_MENU_BAR (parent))
- {
- pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
- child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
- }
- else
- {
- pack_dir = GTK_PACK_DIRECTION_LTR;
- child_pack_dir = GTK_PACK_DIRECTION_LTR;
+ if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
+ (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
+ avail_size -= 2 * horizontal_padding;
}
- border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
- min_height = (border_width * 2) + padding.top + padding.bottom;
-
- avail_size = for_size;
- avail_size -= (border_width * 2) + padding.left + padding.right;
-
- if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
- (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
- min_height += 2 * horizontal_padding;
-
- if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
- (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
- avail_size -= 2 * horizontal_padding;
-
nat_height = min_height;
child = gtk_bin_get_child (bin);
@@ -1037,34 +926,36 @@ gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
{
gint child_min, child_nat;
gint arrow_size = 0;
-
+ guint arrow_spacing = 0;
+
if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
{
- guint arrow_spacing;
-
gtk_widget_style_get (widget,
"arrow-spacing", &arrow_spacing,
NULL);
-
get_arrow_size (widget, child, &arrow_size);
+ }
+ if (for_size != -1)
+ {
avail_size -= arrow_size;
avail_size -= arrow_spacing;
- }
- gtk_widget_get_preferred_height_for_width (child,
- avail_size,
- &child_min,
- &child_nat);
+ gtk_widget_get_preferred_height_for_width (child,
+ avail_size,
+ &child_min,
+ &child_nat);
+ }
+ else
+ {
+ gtk_widget_get_preferred_height (child, &child_min, &child_nat);
+ }
min_height += child_min;
nat_height += child_nat;
- if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
- {
- min_height = MAX (min_height, arrow_size);
- nat_height = MAX (nat_height, arrow_size);
- }
+ min_height = MAX (min_height, arrow_size);
+ nat_height = MAX (nat_height, arrow_size);
}
else /* separator item */
{
@@ -1093,6 +984,12 @@ gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
}
}
+ accel_width = 0;
+ gtk_container_foreach (GTK_CONTAINER (menu_item),
+ gtk_menu_item_accel_width_foreach,
+ &accel_width);
+ priv->accelerator_width = accel_width;
+
if (minimum_size)
*minimum_size = min_height;
@@ -1101,6 +998,23 @@ gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
}
static void
+gtk_menu_item_get_preferred_height (GtkWidget *widget,
+ gint *minimum_size,
+ gint *natural_size)
+{
+ gtk_menu_item_real_get_height (widget, -1, minimum_size, natural_size);
+}
+
+static void
+gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
+ gint for_size,
+ gint *minimum_size,
+ gint *natural_size)
+{
+ gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size);
+}
+
+static void
gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
{
parent_buildable_iface = g_type_interface_peek_parent (iface);