From 05e62e02510685ecf223c3ee384083448bd8268d Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Thu, 15 Dec 2011 18:42:27 +0100 Subject: [PATCH] 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. --- gtk/gtkmenuitem.c | 190 +++++++++++++--------------------------------- 1 file 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,35 +902,60 @@ 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; + if (for_size != -1) + { + avail_size = for_size; + avail_size -= (border_width * 2) + padding.left + padding.right; + + 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); if (child != NULL && gtk_widget_get_visible (child)) { - GtkMenuItemPrivate *priv = menu_item->priv; gint child_min, child_nat; + gint arrow_size = 0; + guint arrow_spacing = 0; - gtk_widget_get_preferred_height (child, &child_min, &child_nat); + if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator) + { + 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); + } + else + { + 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); - } + min_height = MAX (min_height, arrow_size); + nat_height = MAX (nat_height, arrow_size); } else /* separator item */ { @@ -970,134 +997,21 @@ gtk_menu_item_get_preferred_height (GtkWidget *widget, *natural_size = nat_height; } +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) { - 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); - - 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; - } - - 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); - - if (child != NULL && gtk_widget_get_visible (child)) - { - gint child_min, child_nat; - gint arrow_size = 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); - - avail_size -= arrow_size; - avail_size -= arrow_spacing; - } - - gtk_widget_get_preferred_height_for_width (child, - avail_size, - &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); - } - } - else /* separator item */ - { - 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; - } - } - - if (minimum_size) - *minimum_size = min_height; - - if (natural_size) - *natural_size = nat_height; + gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size); } static void