headerbar: Convert to gadgets

This commit is contained in:
Matthias Clasen 2015-12-05 23:59:38 -05:00
parent 90109cfe4c
commit e237c947c7

View File

@ -26,7 +26,10 @@
#include "gtktypebuiltins.h"
#include "gtkwidgetprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcsscustomgadgetprivate.h"
#include "gtkwindowprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkcontainerprivate.h"
#include "a11y/gtkcontaineraccessible.h"
#include <string.h>
@ -79,6 +82,8 @@ struct _GtkHeaderBarPrivate
GtkWidget *titlebar_end_separator;
GtkWidget *titlebar_icon;
GtkCssGadget *gadget;
};
typedef struct _Child Child;
@ -116,25 +121,6 @@ G_DEFINE_TYPE_WITH_CODE (GtkHeaderBar, gtk_header_bar, GTK_TYPE_CONTAINER,
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_header_bar_buildable_init));
static void
get_css_padding_and_border (GtkWidget *widget,
GtkBorder *border)
{
GtkStyleContext *context;
GtkStateFlags state;
GtkBorder tmp;
context = gtk_widget_get_style_context (widget);
state = gtk_style_context_get_state (context);
gtk_style_context_get_padding (context, state, border);
gtk_style_context_get_border (context, state, &tmp);
border->top += tmp.top;
border->right += tmp.right;
border->bottom += tmp.bottom;
border->left += tmp.left;
}
static void
init_sizing_box (GtkHeaderBar *bar)
{
@ -583,29 +569,6 @@ construct_label_box (GtkHeaderBar *bar)
gtk_widget_set_parent (priv->label_box, GTK_WIDGET (bar));
}
static void
gtk_header_bar_init (GtkHeaderBar *bar)
{
GtkHeaderBarPrivate *priv;
priv = gtk_header_bar_get_instance_private (bar);
gtk_widget_set_has_window (GTK_WIDGET (bar), FALSE);
gtk_widget_set_redraw_on_allocate (GTK_WIDGET (bar), FALSE);
priv->title = NULL;
priv->subtitle = NULL;
priv->custom_title = NULL;
priv->children = NULL;
priv->spacing = DEFAULT_SPACING;
priv->has_subtitle = TRUE;
priv->decoration_layout = NULL;
priv->decoration_layout_set = FALSE;
init_sizing_box (bar);
construct_label_box (bar);
}
static gint
count_visible_children (GtkHeaderBar *bar)
{
@ -666,7 +629,6 @@ gtk_header_bar_get_size (GtkWidget *widget,
GList *l;
gint nvis_children;
gint minimum, natural;
GtkBorder css_borders;
gint center_min, center_nat;
minimum = natural = 0;
@ -705,28 +667,23 @@ gtk_header_bar_get_size (GtkWidget *widget,
nvis_children += 1;
}
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
minimum += center_min;
natural += center_nat;
}
else
{
minimum = MAX (minimum, center_min);
natural = MAX (natural, center_nat);
}
if (nvis_children > 0 && orientation == GTK_ORIENTATION_HORIZONTAL)
{
minimum += nvis_children * priv->spacing;
natural += nvis_children * priv->spacing;
}
get_css_padding_and_border (widget, &css_borders);
if (GTK_ORIENTATION_HORIZONTAL == orientation)
{
minimum += center_min + css_borders.left + css_borders.right;
natural += center_nat + css_borders.left + css_borders.right;
}
else
{
/* We don't enforce css borders on the center widget, to make
* title/subtitle combinations fit without growing the header
*/
minimum = MAX (center_min, minimum + css_borders.top + css_borders.bottom);
natural = MAX (center_nat, natural + css_borders.top + css_borders.bottom);
}
*minimum_size = minimum;
*natural_size = natural;
}
@ -745,7 +702,6 @@ gtk_header_bar_compute_size_for_orientation (GtkWidget *widget,
gint child_size;
gint child_natural;
gint nvis_children;
GtkBorder css_borders;
nvis_children = 0;
@ -806,11 +762,6 @@ gtk_header_bar_compute_size_for_orientation (GtkWidget *widget,
required_natural += nvis_children * priv->spacing;
}
get_css_padding_and_border (widget, &css_borders);
required_size += css_borders.left + css_borders.right;
required_natural += css_borders.left + css_borders.right;
*minimum_size = required_size;
*natural_size = required_natural;
}
@ -835,7 +786,6 @@ gtk_header_bar_compute_size_for_opposing_orientation (GtkWidget *widget,
gint child_size;
gint child_minimum;
gint child_natural;
GtkBorder css_borders;
gint center_min, center_nat;
nvis_children = count_visible_children (bar);
@ -927,56 +877,110 @@ gtk_header_bar_compute_size_for_opposing_orientation (GtkWidget *widget,
computed_natural = MAX (computed_natural, child_natural);
}
get_css_padding_and_border (widget, &css_borders);
/* We don't enforce css borders on the center widget, to make
* title/subtitle combinations fit without growing the header
*/
computed_minimum = MAX (center_min, computed_minimum + css_borders.top + css_borders.bottom);
computed_natural = MAX (center_nat, computed_natural + css_borders.top + css_borders.bottom);
*minimum_size = computed_minimum;
*natural_size = computed_natural;
}
static void
gtk_header_bar_get_preferred_width (GtkWidget *widget,
gint *minimum_size,
gint *natural_size)
gtk_header_bar_get_content_size (GtkCssGadget *gadget,
GtkOrientation orientation,
gint for_size,
gint *minimum,
gint *natural,
gint *minimum_baseline,
gint *natural_baseline,
gpointer unused)
{
gtk_header_bar_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
if (for_size < 0)
gtk_header_bar_get_size (widget, orientation, minimum, natural);
else if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_header_bar_compute_size_for_orientation (widget, for_size, minimum, natural);
else
gtk_header_bar_compute_size_for_opposing_orientation (widget, for_size, minimum, natural);
}
static void
gtk_header_bar_get_preferred_width (GtkWidget *widget,
gint *minimum,
gint *natural)
{
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (widget));
gtk_css_gadget_get_preferred_size (priv->gadget,
GTK_ORIENTATION_HORIZONTAL,
-1,
minimum, natural,
NULL, NULL);
}
static void
gtk_header_bar_get_preferred_height (GtkWidget *widget,
gint *minimum_size,
gint *natural_size)
gint *minimum,
gint *natural)
{
gtk_header_bar_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (widget));
gtk_css_gadget_get_preferred_size (priv->gadget,
GTK_ORIENTATION_VERTICAL,
-1,
minimum, natural,
NULL, NULL);
}
static void
gtk_header_bar_get_preferred_width_for_height (GtkWidget *widget,
gint height,
gint *minimum_width,
gint *natural_width)
gint *minimum,
gint *natural)
{
gtk_header_bar_compute_size_for_orientation (widget, height, minimum_width, natural_width);
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (widget));
gtk_css_gadget_get_preferred_size (priv->gadget,
GTK_ORIENTATION_HORIZONTAL,
height,
minimum, natural,
NULL, NULL);
}
static void
gtk_header_bar_get_preferred_height_for_width (GtkWidget *widget,
gint width,
gint *minimum_height,
gint *natural_height)
gint *minimum,
gint *natural)
{
gtk_header_bar_compute_size_for_opposing_orientation (widget, width, minimum_height, natural_height);
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (widget));
gtk_css_gadget_get_preferred_size (priv->gadget,
GTK_ORIENTATION_VERTICAL,
width,
minimum, natural,
NULL, NULL);
}
static void
gtk_header_bar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (widget));
GtkAllocation clip;
gtk_widget_set_allocation (widget, allocation);
gtk_css_gadget_allocate (priv->gadget, allocation, gtk_widget_get_allocated_baseline (widget), &clip);
gtk_widget_set_clip (widget, &clip);
}
static void
gtk_header_bar_allocate_contents (GtkCssGadget *gadget,
const GtkAllocation *allocation,
int baseline,
GtkAllocation *out_clip,
gpointer unused)
{
GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
GtkHeaderBar *bar = GTK_HEADER_BAR (widget);
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
GtkRequestedSize *sizes;
@ -994,7 +998,6 @@ gtk_header_bar_size_allocate (GtkWidget *widget,
gint x;
gint child_size;
GtkTextDirection direction;
GtkBorder css_borders;
gtk_widget_set_allocation (widget, allocation);
@ -1002,9 +1005,8 @@ gtk_header_bar_size_allocate (GtkWidget *widget,
nvis_children = count_visible_children (bar);
sizes = g_newa (GtkRequestedSize, nvis_children);
get_css_padding_and_border (widget, &css_borders);
width = allocation->width - nvis_children * priv->spacing - css_borders.left - css_borders.right;
height = allocation->height - css_borders.top - css_borders.bottom;
width = allocation->width - nvis_children * priv->spacing;
height = allocation->height;
i = 0;
for (l = priv->children; l; l = l->next)
@ -1069,12 +1071,12 @@ gtk_header_bar_size_allocate (GtkWidget *widget,
side[0] = side[1] = 0;
for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++)
{
child_allocation.y = allocation->y + css_borders.top;
child_allocation.y = allocation->y;
child_allocation.height = height;
if (packing == GTK_PACK_START)
x = allocation->x + css_borders.left + start_width;
x = allocation->x + start_width;
else
x = allocation->x + allocation->width - end_width - css_borders.right;
x = allocation->x + allocation->width - end_width;
i = 0;
for (l = priv->children; l != NULL; l = l->next)
@ -1151,16 +1153,16 @@ gtk_header_bar_size_allocate (GtkWidget *widget,
if (priv->label_box != NULL)
gtk_widget_size_allocate (priv->label_box, &child_allocation);
child_allocation.y = allocation->y + css_borders.top;
child_allocation.y = allocation->y;
child_allocation.height = height;
if (priv->titlebar_start_box)
{
gboolean left = (direction == GTK_TEXT_DIR_LTR);
if (left)
child_allocation.x = allocation->x + css_borders.left;
child_allocation.x = allocation->x;
else
child_allocation.x = allocation->x + allocation->width - css_borders.right - start_width + priv->spacing;
child_allocation.x = allocation->x + allocation->width - start_width + priv->spacing;
child_allocation.width = start_width - priv->spacing;
gtk_widget_size_allocate (priv->titlebar_start_box, &child_allocation);
}
@ -1169,14 +1171,14 @@ gtk_header_bar_size_allocate (GtkWidget *widget,
{
gboolean left = (direction != GTK_TEXT_DIR_LTR);
if (left)
child_allocation.x = allocation->x + css_borders.left;
child_allocation.x = allocation->x;
else
child_allocation.x = allocation->x + allocation->width - css_borders.right - end_width + priv->spacing;
child_allocation.x = allocation->x + allocation->width - end_width + priv->spacing;
child_allocation.width = end_width - priv->spacing;
gtk_widget_size_allocate (priv->titlebar_end_box, &child_allocation);
}
_gtk_widget_set_simple_clip (widget, NULL);
gtk_container_get_children_clip (GTK_CONTAINER (widget), out_clip);
}
/**
@ -1430,6 +1432,8 @@ gtk_header_bar_finalize (GObject *object)
g_free (priv->subtitle);
g_free (priv->decoration_layout);
g_clear_object (&priv->gadget);
G_OBJECT_CLASS (gtk_header_bar_parent_class)->finalize (object);
}
@ -1780,17 +1784,23 @@ static gint
gtk_header_bar_draw (GtkWidget *widget,
cairo_t *cr)
{
GtkStyleContext *context;
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (widget));
context = gtk_widget_get_style_context (widget);
gtk_css_gadget_draw (priv->gadget, cr);
gtk_render_background (context, cr, 0, 0,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
gtk_render_frame (context, cr, 0, 0,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
return FALSE;
}
static gboolean
gtk_header_bar_render_contents (GtkCssGadget *gadget,
cairo_t *cr,
int x,
int y,
int width,
int height,
gpointer unused)
{
GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
GTK_WIDGET_CLASS (gtk_header_bar_parent_class)->draw (widget, cr);
@ -2011,6 +2021,40 @@ gtk_header_bar_class_init (GtkHeaderBarClass *class)
gtk_widget_class_set_css_name (widget_class, "headerbar");
}
static void
gtk_header_bar_init (GtkHeaderBar *bar)
{
GtkHeaderBarPrivate *priv;
GtkCssNode *widget_node;
priv = gtk_header_bar_get_instance_private (bar);
gtk_widget_set_has_window (GTK_WIDGET (bar), FALSE);
gtk_widget_set_redraw_on_allocate (GTK_WIDGET (bar), FALSE);
priv->title = NULL;
priv->subtitle = NULL;
priv->custom_title = NULL;
priv->children = NULL;
priv->spacing = DEFAULT_SPACING;
priv->has_subtitle = TRUE;
priv->decoration_layout = NULL;
priv->decoration_layout_set = FALSE;
init_sizing_box (bar);
construct_label_box (bar);
widget_node = gtk_widget_get_css_node (GTK_WIDGET (bar));
priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
GTK_WIDGET (bar),
gtk_header_bar_get_content_size,
gtk_header_bar_allocate_contents,
gtk_header_bar_render_contents,
NULL,
NULL);
}
static void
gtk_header_bar_buildable_add_child (GtkBuildable *buildable,
GtkBuilder *builder,