headerbar: Drop the Private struct

This commit is contained in:
Matthias Clasen 2020-07-25 07:40:24 -04:00
parent 0e70ddcab3
commit 54deed9a5a

View File

@ -108,21 +108,10 @@
#define MIN_TITLE_CHARS 5 #define MIN_TITLE_CHARS 5
typedef struct _GtkHeaderBarPrivate GtkHeaderBarPrivate;
typedef struct _GtkHeaderBarClass GtkHeaderBarClass;
struct _GtkHeaderBar struct _GtkHeaderBar
{ {
GtkWidget container; GtkWidget container;
};
struct _GtkHeaderBarClass
{
GtkWidgetClass parent_class;
};
struct _GtkHeaderBarPrivate
{
GtkWidget *handle; GtkWidget *handle;
GtkWidget *center_box; GtkWidget *center_box;
GtkWidget *start_box; GtkWidget *start_box;
@ -131,16 +120,24 @@ struct _GtkHeaderBarPrivate
GtkWidget *title_label; GtkWidget *title_label;
GtkWidget *title_widget; GtkWidget *title_widget;
gboolean show_title_buttons;
char *decoration_layout;
gboolean track_default_decoration;
GtkWidget *start_window_controls; GtkWidget *start_window_controls;
GtkWidget *end_window_controls; GtkWidget *end_window_controls;
char *decoration_layout;
guint show_title_buttons : 1;
guint track_default_decoration : 1;
GdkSurfaceState state; GdkSurfaceState state;
}; };
typedef struct _GtkHeaderBarClass GtkHeaderBarClass;
struct _GtkHeaderBarClass
{
GtkWidgetClass parent_class;
};
enum { enum {
PROP_0, PROP_0,
PROP_TITLE_WIDGET, PROP_TITLE_WIDGET,
@ -154,14 +151,12 @@ static GParamSpec *header_bar_props[LAST_PROP] = { NULL, };
static void gtk_header_bar_buildable_init (GtkBuildableIface *iface); static void gtk_header_bar_buildable_init (GtkBuildableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkHeaderBar, gtk_header_bar, GTK_TYPE_WIDGET, G_DEFINE_TYPE_WITH_CODE (GtkHeaderBar, gtk_header_bar, GTK_TYPE_WIDGET,
G_ADD_PRIVATE (GtkHeaderBar)
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_header_bar_buildable_init)); gtk_header_bar_buildable_init));
static void static void
create_window_controls (GtkHeaderBar *bar) create_window_controls (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
GtkWidget *controls; GtkWidget *controls;
controls = gtk_window_controls_new (GTK_PACK_START); controls = gtk_window_controls_new (GTK_PACK_START);
@ -171,8 +166,8 @@ create_window_controls (GtkHeaderBar *bar)
g_object_bind_property (controls, "empty", g_object_bind_property (controls, "empty",
controls, "visible", controls, "visible",
G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN); G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
gtk_box_append (GTK_BOX (priv->start_box), controls); gtk_box_append (GTK_BOX (bar->start_box), controls);
priv->start_window_controls = controls; bar->start_window_controls = controls;
controls = gtk_window_controls_new (GTK_PACK_END); controls = gtk_window_controls_new (GTK_PACK_END);
g_object_bind_property (bar, "decoration-layout", g_object_bind_property (bar, "decoration-layout",
@ -181,18 +176,17 @@ create_window_controls (GtkHeaderBar *bar)
g_object_bind_property (controls, "empty", g_object_bind_property (controls, "empty",
controls, "visible", controls, "visible",
G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN); G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
gtk_box_append (GTK_BOX (priv->end_box), controls); gtk_box_append (GTK_BOX (bar->end_box), controls);
priv->end_window_controls = controls; bar->end_window_controls = controls;
} }
static void static void
update_default_decoration (GtkHeaderBar *bar) update_default_decoration (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
gboolean have_children = FALSE; gboolean have_children = FALSE;
/* Check whether we have any child widgets that we didn't add ourselves */ /* Check whether we have any child widgets that we didn't add ourselves */
if (gtk_center_box_get_center_widget (GTK_CENTER_BOX (priv->center_box)) != NULL) if (gtk_center_box_get_center_widget (GTK_CENTER_BOX (bar->center_box)) != NULL)
{ {
have_children = TRUE; have_children = TRUE;
} }
@ -200,11 +194,11 @@ update_default_decoration (GtkHeaderBar *bar)
{ {
GtkWidget *w; GtkWidget *w;
for (w = _gtk_widget_get_first_child (priv->start_box); for (w = _gtk_widget_get_first_child (bar->start_box);
w != NULL; w != NULL;
w = _gtk_widget_get_next_sibling (w)) w = _gtk_widget_get_next_sibling (w))
{ {
if (w != priv->start_window_controls) if (w != bar->start_window_controls)
{ {
have_children = TRUE; have_children = TRUE;
break; break;
@ -212,11 +206,11 @@ update_default_decoration (GtkHeaderBar *bar)
} }
if (!have_children) if (!have_children)
for (w = _gtk_widget_get_first_child (priv->end_box); for (w = _gtk_widget_get_first_child (bar->end_box);
w != NULL; w != NULL;
w = _gtk_widget_get_next_sibling (w)) w = _gtk_widget_get_next_sibling (w))
{ {
if (w != priv->end_window_controls) if (w != bar->end_window_controls)
{ {
have_children = TRUE; have_children = TRUE;
break; break;
@ -224,7 +218,7 @@ update_default_decoration (GtkHeaderBar *bar)
} }
} }
if (have_children || priv->title_widget != NULL) if (have_children || bar->title_widget != NULL)
gtk_widget_remove_css_class (GTK_WIDGET (bar), "default-decoration"); gtk_widget_remove_css_class (GTK_WIDGET (bar), "default-decoration");
else else
gtk_widget_add_css_class (GTK_WIDGET (bar), "default-decoration"); gtk_widget_add_css_class (GTK_WIDGET (bar), "default-decoration");
@ -233,9 +227,7 @@ update_default_decoration (GtkHeaderBar *bar)
void void
_gtk_header_bar_track_default_decoration (GtkHeaderBar *bar) _gtk_header_bar_track_default_decoration (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar); bar->track_default_decoration = TRUE;
priv->track_default_decoration = TRUE;
update_default_decoration (bar); update_default_decoration (bar);
} }
@ -243,11 +235,10 @@ _gtk_header_bar_track_default_decoration (GtkHeaderBar *bar)
static void static void
update_title (GtkHeaderBar *bar) update_title (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
GtkRoot *root; GtkRoot *root;
const char *title = NULL; const char *title = NULL;
if (!priv->title_label) if (!bar->title_label)
return; return;
root = gtk_widget_get_root (GTK_WIDGET (bar)); root = gtk_widget_get_root (GTK_WIDGET (bar));
@ -261,16 +252,15 @@ update_title (GtkHeaderBar *bar)
if (!title) if (!title)
title = g_get_prgname (); title = g_get_prgname ();
gtk_label_set_text (GTK_LABEL (priv->title_label), title); gtk_label_set_text (GTK_LABEL (bar->title_label), title);
} }
static void static void
construct_title_label (GtkHeaderBar *bar) construct_title_label (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
GtkWidget *label; GtkWidget *label;
g_assert (priv->title_label == NULL); g_assert (bar->title_label == NULL);
label = gtk_label_new (NULL); label = gtk_label_new (NULL);
gtk_widget_add_css_class (label, GTK_STYLE_CLASS_TITLE); gtk_widget_add_css_class (label, GTK_STYLE_CLASS_TITLE);
@ -279,9 +269,9 @@ construct_title_label (GtkHeaderBar *bar)
gtk_label_set_single_line_mode (GTK_LABEL (label), TRUE); gtk_label_set_single_line_mode (GTK_LABEL (label), TRUE);
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END); gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
gtk_label_set_width_chars (GTK_LABEL (label), MIN_TITLE_CHARS); gtk_label_set_width_chars (GTK_LABEL (label), MIN_TITLE_CHARS);
gtk_center_box_set_center_widget (GTK_CENTER_BOX (priv->center_box), label); gtk_center_box_set_center_widget (GTK_CENTER_BOX (bar->center_box), label);
priv->title_label = label; bar->title_label = label;
update_title (bar); update_title (bar);
} }
@ -306,30 +296,28 @@ void
gtk_header_bar_set_title_widget (GtkHeaderBar *bar, gtk_header_bar_set_title_widget (GtkHeaderBar *bar,
GtkWidget *title_widget) GtkWidget *title_widget)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
g_return_if_fail (GTK_IS_HEADER_BAR (bar)); g_return_if_fail (GTK_IS_HEADER_BAR (bar));
if (title_widget) if (title_widget)
g_return_if_fail (GTK_IS_WIDGET (title_widget)); g_return_if_fail (GTK_IS_WIDGET (title_widget));
/* No need to do anything if the title widget stays the same */ /* No need to do anything if the title widget stays the same */
if (priv->title_widget == title_widget) if (bar->title_widget == title_widget)
return; return;
gtk_center_box_set_center_widget (GTK_CENTER_BOX (priv->center_box), NULL); gtk_center_box_set_center_widget (GTK_CENTER_BOX (bar->center_box), NULL);
priv->title_widget = NULL; bar->title_widget = NULL;
if (title_widget != NULL) if (title_widget != NULL)
{ {
priv->title_widget = title_widget; bar->title_widget = title_widget;
gtk_center_box_set_center_widget (GTK_CENTER_BOX (priv->center_box), title_widget); gtk_center_box_set_center_widget (GTK_CENTER_BOX (bar->center_box), title_widget);
priv->title_label = NULL; bar->title_label = NULL;
} }
else else
{ {
if (priv->title_label == NULL) if (bar->title_label == NULL)
construct_title_label (bar); construct_title_label (bar);
} }
@ -349,11 +337,9 @@ gtk_header_bar_set_title_widget (GtkHeaderBar *bar,
GtkWidget * GtkWidget *
gtk_header_bar_get_title_widget (GtkHeaderBar *bar) gtk_header_bar_get_title_widget (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
g_return_val_if_fail (GTK_IS_HEADER_BAR (bar), NULL); g_return_val_if_fail (GTK_IS_HEADER_BAR (bar), NULL);
return priv->title_widget; return bar->title_widget;
} }
static void static void
@ -384,14 +370,14 @@ gtk_header_bar_unroot (GtkWidget *widget)
static void static void
gtk_header_bar_dispose (GObject *object) gtk_header_bar_dispose (GObject *object)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (object)); GtkHeaderBar *bar = GTK_HEADER_BAR (object);
priv->title_widget = NULL; bar->title_widget = NULL;
priv->title_label = NULL; bar->title_label = NULL;
priv->start_box = NULL; bar->start_box = NULL;
priv->end_box = NULL; bar->end_box = NULL;
g_clear_pointer (&priv->handle, gtk_widget_unparent); g_clear_pointer (&bar->handle, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_header_bar_parent_class)->dispose (object); G_OBJECT_CLASS (gtk_header_bar_parent_class)->dispose (object);
} }
@ -399,9 +385,9 @@ gtk_header_bar_dispose (GObject *object)
static void static void
gtk_header_bar_finalize (GObject *object) gtk_header_bar_finalize (GObject *object)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (GTK_HEADER_BAR (object)); GtkHeaderBar *bar = GTK_HEADER_BAR (object);
g_free (priv->decoration_layout); g_free (bar->decoration_layout);
G_OBJECT_CLASS (gtk_header_bar_parent_class)->finalize (object); G_OBJECT_CLASS (gtk_header_bar_parent_class)->finalize (object);
} }
@ -413,12 +399,11 @@ gtk_header_bar_get_property (GObject *object,
GParamSpec *pspec) GParamSpec *pspec)
{ {
GtkHeaderBar *bar = GTK_HEADER_BAR (object); GtkHeaderBar *bar = GTK_HEADER_BAR (object);
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
switch (prop_id) switch (prop_id)
{ {
case PROP_TITLE_WIDGET: case PROP_TITLE_WIDGET:
g_value_set_object (value, priv->title_widget); g_value_set_object (value, bar->title_widget);
break; break;
case PROP_SHOW_TITLE_BUTTONS: case PROP_SHOW_TITLE_BUTTONS:
@ -468,21 +453,19 @@ gtk_header_bar_pack (GtkHeaderBar *bar,
GtkWidget *widget, GtkWidget *widget,
GtkPackType pack_type) GtkPackType pack_type)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
g_return_if_fail (gtk_widget_get_parent (widget) == NULL); g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
if (pack_type == GTK_PACK_START) if (pack_type == GTK_PACK_START)
{ {
gtk_box_append (GTK_BOX (priv->start_box), widget); gtk_box_append (GTK_BOX (bar->start_box), widget);
} }
else if (pack_type == GTK_PACK_END) else if (pack_type == GTK_PACK_END)
{ {
gtk_box_append (GTK_BOX (priv->end_box), widget); gtk_box_append (GTK_BOX (bar->end_box), widget);
gtk_box_reorder_child_after (GTK_BOX (priv->end_box), widget, NULL); gtk_box_reorder_child_after (GTK_BOX (bar->end_box), widget, NULL);
} }
if (priv->track_default_decoration) if (bar->track_default_decoration)
update_default_decoration (bar); update_default_decoration (bar);
} }
@ -499,29 +482,28 @@ void
gtk_header_bar_remove (GtkHeaderBar *bar, gtk_header_bar_remove (GtkHeaderBar *bar,
GtkWidget *child) GtkWidget *child)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
GtkWidget *parent; GtkWidget *parent;
gboolean removed = FALSE; gboolean removed = FALSE;
parent = gtk_widget_get_parent (child); parent = gtk_widget_get_parent (child);
if (parent == priv->start_box) if (parent == bar->start_box)
{ {
gtk_box_remove (GTK_BOX (priv->start_box), child); gtk_box_remove (GTK_BOX (bar->start_box), child);
removed = TRUE; removed = TRUE;
} }
else if (parent == priv->end_box) else if (parent == bar->end_box)
{ {
gtk_box_remove (GTK_BOX (priv->end_box), child); gtk_box_remove (GTK_BOX (bar->end_box), child);
removed = TRUE; removed = TRUE;
} }
else if (parent == priv->center_box) else if (parent == bar->center_box)
{ {
gtk_center_box_set_center_widget (GTK_CENTER_BOX (priv->center_box), NULL); gtk_center_box_set_center_widget (GTK_CENTER_BOX (bar->center_box), NULL);
removed = TRUE; removed = TRUE;
} }
if (removed && priv->track_default_decoration) if (removed && bar->track_default_decoration)
update_default_decoration (bar); update_default_decoration (bar);
} }
@ -625,26 +607,24 @@ gtk_header_bar_class_init (GtkHeaderBarClass *class)
static void static void
gtk_header_bar_init (GtkHeaderBar *bar) gtk_header_bar_init (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar); bar->title_widget = NULL;
bar->decoration_layout = NULL;
bar->show_title_buttons = TRUE;
bar->state = GDK_SURFACE_STATE_WITHDRAWN;
priv->title_widget = NULL; bar->handle = gtk_window_handle_new ();
priv->decoration_layout = NULL; gtk_widget_set_parent (bar->handle, GTK_WIDGET (bar));
priv->show_title_buttons = TRUE;
priv->state = GDK_SURFACE_STATE_WITHDRAWN;
priv->handle = gtk_window_handle_new (); bar->center_box = gtk_center_box_new ();
gtk_widget_set_parent (priv->handle, GTK_WIDGET (bar)); gtk_window_handle_set_child (GTK_WINDOW_HANDLE (bar->handle), bar->center_box);
priv->center_box = gtk_center_box_new (); bar->start_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_window_handle_set_child (GTK_WINDOW_HANDLE (priv->handle), priv->center_box); gtk_widget_add_css_class (bar->start_box, "start");
gtk_center_box_set_start_widget (GTK_CENTER_BOX (bar->center_box), bar->start_box);
priv->start_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); bar->end_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_add_css_class (priv->start_box, "start"); gtk_widget_add_css_class (bar->end_box, "end");
gtk_center_box_set_start_widget (GTK_CENTER_BOX (priv->center_box), priv->start_box); gtk_center_box_set_end_widget (GTK_CENTER_BOX (bar->center_box), bar->end_box);
priv->end_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_add_css_class (priv->end_box, "end");
gtk_center_box_set_end_widget (GTK_CENTER_BOX (priv->center_box), priv->end_box);
construct_title_label (bar); construct_title_label (bar);
create_window_controls (bar); create_window_controls (bar);
@ -733,13 +713,9 @@ gtk_header_bar_new (void)
gboolean gboolean
gtk_header_bar_get_show_title_buttons (GtkHeaderBar *bar) gtk_header_bar_get_show_title_buttons (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv;
g_return_val_if_fail (GTK_IS_HEADER_BAR (bar), FALSE); g_return_val_if_fail (GTK_IS_HEADER_BAR (bar), FALSE);
priv = gtk_header_bar_get_instance_private (bar); return bar->show_title_buttons;
return priv->show_title_buttons;
} }
/** /**
@ -754,25 +730,21 @@ void
gtk_header_bar_set_show_title_buttons (GtkHeaderBar *bar, gtk_header_bar_set_show_title_buttons (GtkHeaderBar *bar,
gboolean setting) gboolean setting)
{ {
GtkHeaderBarPrivate *priv;
g_return_if_fail (GTK_IS_HEADER_BAR (bar)); g_return_if_fail (GTK_IS_HEADER_BAR (bar));
priv = gtk_header_bar_get_instance_private (bar);
setting = setting != FALSE; setting = setting != FALSE;
if (priv->show_title_buttons == setting) if (bar->show_title_buttons == setting)
return; return;
priv->show_title_buttons = setting; bar->show_title_buttons = setting;
if (setting) if (setting)
create_window_controls (bar); create_window_controls (bar);
else else
{ {
g_clear_pointer (&priv->start_window_controls, gtk_widget_unparent); g_clear_pointer (&bar->start_window_controls, gtk_widget_unparent);
g_clear_pointer (&priv->end_window_controls, gtk_widget_unparent); g_clear_pointer (&bar->end_window_controls, gtk_widget_unparent);
} }
g_object_notify_by_pspec (G_OBJECT (bar), header_bar_props[PROP_SHOW_TITLE_BUTTONS]); g_object_notify_by_pspec (G_OBJECT (bar), header_bar_props[PROP_SHOW_TITLE_BUTTONS]);
@ -805,14 +777,10 @@ void
gtk_header_bar_set_decoration_layout (GtkHeaderBar *bar, gtk_header_bar_set_decoration_layout (GtkHeaderBar *bar,
const char *layout) const char *layout)
{ {
GtkHeaderBarPrivate *priv;
g_return_if_fail (GTK_IS_HEADER_BAR (bar)); g_return_if_fail (GTK_IS_HEADER_BAR (bar));
priv = gtk_header_bar_get_instance_private (bar); g_free (bar->decoration_layout);
bar->decoration_layout = g_strdup (layout);
g_free (priv->decoration_layout);
priv->decoration_layout = g_strdup (layout);
g_object_notify_by_pspec (G_OBJECT (bar), header_bar_props[PROP_DECORATION_LAYOUT]); g_object_notify_by_pspec (G_OBJECT (bar), header_bar_props[PROP_DECORATION_LAYOUT]);
} }
@ -829,11 +797,7 @@ gtk_header_bar_set_decoration_layout (GtkHeaderBar *bar,
const char * const char *
gtk_header_bar_get_decoration_layout (GtkHeaderBar *bar) gtk_header_bar_get_decoration_layout (GtkHeaderBar *bar)
{ {
GtkHeaderBarPrivate *priv;
g_return_val_if_fail (GTK_IS_HEADER_BAR (bar), NULL); g_return_val_if_fail (GTK_IS_HEADER_BAR (bar), NULL);
priv = gtk_header_bar_get_instance_private (bar); return bar->decoration_layout;
return priv->decoration_layout;
} }