container: Reduce boilerplate

We no longer need to hand-roll the type registration,
since we no longer install our own param spec pool.
This commit is contained in:
Matthias Clasen 2019-04-05 04:43:36 +00:00
parent 70bf6d4b97
commit ea7a43beb4
2 changed files with 49 additions and 134 deletions

View File

@ -51,14 +51,14 @@
* @Short_description: Base class for widgets which contain other widgets * @Short_description: Base class for widgets which contain other widgets
* @Title: GtkContainer * @Title: GtkContainer
* *
* A GTK+ user interface is constructed by nesting widgets inside widgets. * A GTK user interface is constructed by nesting widgets inside widgets.
* Container widgets are the inner nodes in the resulting tree of widgets: * Container widgets are the inner nodes in the resulting tree of widgets:
* they contain other widgets. So, for example, you might have a #GtkWindow * they contain other widgets. So, for example, you might have a #GtkWindow
* containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
* of a textual label inside the frame, you might replace the #GtkLabel widget * of a textual label inside the frame, you might replace the #GtkLabel widget
* with a #GtkImage widget. * with a #GtkImage widget.
* *
* There are two major kinds of container widgets in GTK+. Both are subclasses * There are two major kinds of container widgets in GTK. Both are subclasses
* of the abstract GtkContainer base class. * of the abstract GtkContainer base class.
* *
* The first type of container widget has a single child widget and derives * The first type of container widget has a single child widget and derives
@ -81,6 +81,7 @@
* If the GtkContainer implementation has internal children, they should be added * If the GtkContainer implementation has internal children, they should be added
* with gtk_widget_set_parent() on init() and removed with gtk_widget_unparent() * with gtk_widget_set_parent() on init() and removed with gtk_widget_unparent()
* in the #GtkWidgetClass.destroy() implementation. * in the #GtkWidgetClass.destroy() implementation.
*
* See more about implementing custom widgets at https://wiki.gnome.org/HowDoI/CustomWidgets * See more about implementing custom widgets at https://wiki.gnome.org/HowDoI/CustomWidgets
*/ */
@ -98,15 +99,7 @@ enum {
LAST_SIGNAL LAST_SIGNAL
}; };
#define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
#define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
/* --- prototypes --- */ /* --- prototypes --- */
static void gtk_container_base_class_init (GtkContainerClass *klass);
static void gtk_container_base_class_finalize (GtkContainerClass *klass);
static void gtk_container_class_init (GtkContainerClass *klass);
static void gtk_container_init (GtkContainer *container);
static void gtk_container_destroy (GtkWidget *widget); static void gtk_container_destroy (GtkWidget *widget);
static void gtk_container_add_unimplemented (GtkContainer *container, static void gtk_container_add_unimplemented (GtkContainer *container,
GtkWidget *widget); GtkWidget *widget);
@ -117,7 +110,6 @@ static void gtk_container_compute_expand (GtkWidget *widget,
gboolean *vexpand_p); gboolean *vexpand_p);
static void gtk_container_real_set_focus_child (GtkContainer *container, static void gtk_container_real_set_focus_child (GtkContainer *container,
GtkWidget *widget); GtkWidget *widget);
static void gtk_container_children_callback (GtkWidget *widget, static void gtk_container_children_callback (GtkWidget *widget,
gpointer client_data); gpointer client_data);
static GtkSizeRequestMode gtk_container_get_request_mode (GtkWidget *widget); static GtkSizeRequestMode gtk_container_get_request_mode (GtkWidget *widget);
@ -127,80 +119,16 @@ static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *cont
/* GtkBuildable */ /* GtkBuildable */
static void gtk_container_buildable_init (GtkBuildableIface *iface); static void gtk_container_buildable_init (GtkBuildableIface *iface);
static void gtk_container_buildable_add_child (GtkBuildable *buildable, static GtkBuildableIface *parent_buildable_iface;
GtkBuilder *builder,
GObject *child,
const gchar *type);
/* --- variables --- */
static GQuark vadjustment_key_id; static GQuark vadjustment_key_id;
static GQuark hadjustment_key_id; static GQuark hadjustment_key_id;
static guint container_signals[LAST_SIGNAL] = { 0 }; static guint container_signals[LAST_SIGNAL] = { 0 };
static gint GtkContainer_private_offset;
static GtkWidgetClass *gtk_container_parent_class = NULL;
static GtkBuildableIface *parent_buildable_iface;
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkContainer, gtk_container, GTK_TYPE_WIDGET,
/* --- functions --- */ G_ADD_PRIVATE (GtkContainer)
static inline gpointer G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_container_get_instance_private (GtkContainer *self) gtk_container_buildable_init))
{
return G_STRUCT_MEMBER_P (self, GtkContainer_private_offset);
}
GType
gtk_container_get_type (void)
{
static GType container_type = 0;
if (!container_type)
{
const GTypeInfo container_info =
{
sizeof (GtkContainerClass),
(GBaseInitFunc) gtk_container_base_class_init,
(GBaseFinalizeFunc) gtk_container_base_class_finalize,
(GClassInitFunc) gtk_container_class_init,
NULL /* class_finalize */,
NULL /* class_data */,
sizeof (GtkContainer),
0 /* n_preallocs */,
(GInstanceInitFunc) gtk_container_init,
NULL, /* value_table */
};
const GInterfaceInfo buildable_info =
{
(GInterfaceInitFunc) gtk_container_buildable_init,
NULL,
NULL
};
container_type =
g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
&container_info, G_TYPE_FLAG_ABSTRACT);
GtkContainer_private_offset =
g_type_add_instance_private (container_type, sizeof (GtkContainerPrivate));
g_type_add_interface_static (container_type,
GTK_TYPE_BUILDABLE,
&buildable_info);
}
return container_type;
}
static void
gtk_container_base_class_init (GtkContainerClass *class)
{
}
static void
gtk_container_base_class_finalize (GtkContainerClass *class)
{
}
static void static void
gtk_container_class_init (GtkContainerClass *class) gtk_container_class_init (GtkContainerClass *class)
@ -208,8 +136,6 @@ gtk_container_class_init (GtkContainerClass *class)
GObjectClass *gobject_class = G_OBJECT_CLASS (class); GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
gtk_container_parent_class = g_type_class_peek_parent (class);
vadjustment_key_id = g_quark_from_static_string ("gtk-vadjustment"); vadjustment_key_id = g_quark_from_static_string ("gtk-vadjustment");
hadjustment_key_id = g_quark_from_static_string ("gtk-hadjustment"); hadjustment_key_id = g_quark_from_static_string ("gtk-hadjustment");
@ -243,19 +169,9 @@ gtk_container_class_init (GtkContainerClass *class)
G_TYPE_NONE, 1, G_TYPE_NONE, 1,
GTK_TYPE_WIDGET); GTK_TYPE_WIDGET);
if (GtkContainer_private_offset != 0)
g_type_class_adjust_private_offset (class, &GtkContainer_private_offset);
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CONTAINER_ACCESSIBLE); gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CONTAINER_ACCESSIBLE);
} }
static void
gtk_container_buildable_init (GtkBuildableIface *iface)
{
parent_buildable_iface = g_type_interface_peek_parent (iface);
iface->add_child = gtk_container_buildable_add_child;
}
static void static void
gtk_container_buildable_add_child (GtkBuildable *buildable, gtk_container_buildable_add_child (GtkBuildable *buildable,
GtkBuilder *builder, GtkBuilder *builder,
@ -266,20 +182,23 @@ gtk_container_buildable_add_child (GtkBuildable *buildable,
_gtk_widget_get_parent (GTK_WIDGET (child)) == NULL) _gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
{ {
if (type) if (type)
{
GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type); GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
}
else else
{
gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child)); gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
} }
}
else else
{ {
parent_buildable_iface->add_child (buildable, builder, child, type); parent_buildable_iface->add_child (buildable, builder, child, type);
} }
} }
static void
gtk_container_buildable_init (GtkBuildableIface *iface)
{
parent_buildable_iface = g_type_interface_peek_parent (iface);
iface->add_child = gtk_container_buildable_add_child;
}
/** /**
* gtk_container_child_type: * gtk_container_child_type:
* @container: a #GtkContainer * @container: a #GtkContainer
@ -290,37 +209,31 @@ gtk_container_buildable_add_child (GtkBuildable *buildable,
* children can be added, e.g. for a #GtkPaned which already has two * children can be added, e.g. for a #GtkPaned which already has two
* children. * children.
* *
* Returns: a #GType. * Returns: a #GType
**/ **/
GType GType
gtk_container_child_type (GtkContainer *container) gtk_container_child_type (GtkContainer *container)
{ {
GType slot;
GtkContainerClass *class;
g_return_val_if_fail (GTK_IS_CONTAINER (container), 0); g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
class = GTK_CONTAINER_GET_CLASS (container); if (GTK_CONTAINER_GET_CLASS (container)->child_type)
if (class->child_type) return GTK_CONTAINER_GET_CLASS (container)->child_type (container);
slot = class->child_type (container);
else else
slot = G_TYPE_NONE; return G_TYPE_NONE;
return slot;
} }
static void static void
gtk_container_add_unimplemented (GtkContainer *container, gtk_container_add_unimplemented (GtkContainer *container,
GtkWidget *widget) GtkWidget *widget)
{ {
g_warning ("GtkContainerClass::add not implemented for '%s'", g_type_name (G_TYPE_FROM_INSTANCE (container))); g_warning ("GtkContainerClass::add not implemented for '%s'", G_OBJECT_TYPE_NAME (container));
} }
static void static void
gtk_container_remove_unimplemented (GtkContainer *container, gtk_container_remove_unimplemented (GtkContainer *container,
GtkWidget *widget) GtkWidget *widget)
{ {
g_warning ("GtkContainerClass::remove not implemented for '%s'", g_type_name (G_TYPE_FROM_INSTANCE (container))); g_warning ("GtkContainerClass::remove not implemented for '%s'", G_OBJECT_TYPE_NAME (container));
} }
static void static void
@ -376,9 +289,9 @@ gtk_container_add (GtkContainer *container,
g_warning ("Attempting to add a widget with type %s to a container of " g_warning ("Attempting to add a widget with type %s to a container of "
"type %s, but the widget is already inside a container of type %s, " "type %s, but the widget is already inside a container of type %s, "
"please remove the widget from its existing container first." , "please remove the widget from its existing container first." ,
g_type_name (G_OBJECT_TYPE (widget)), G_OBJECT_TYPE_NAME (widget),
g_type_name (G_OBJECT_TYPE (container)), G_OBJECT_TYPE_NAME (container),
g_type_name (G_OBJECT_TYPE (parent))); G_OBJECT_TYPE_NAME (parent));
return; return;
} }
@ -584,9 +497,7 @@ gtk_container_forall (GtkContainer *container,
g_return_if_fail (GTK_IS_CONTAINER (container)); g_return_if_fail (GTK_IS_CONTAINER (container));
g_return_if_fail (callback != NULL); g_return_if_fail (callback != NULL);
gtk_widget_forall (GTK_WIDGET (container), gtk_widget_forall (GTK_WIDGET (container), callback, callback_data);
callback,
callback_data);
} }
/** /**
@ -612,15 +523,11 @@ gtk_container_foreach (GtkContainer *container,
GtkCallback callback, GtkCallback callback,
gpointer callback_data) gpointer callback_data)
{ {
GtkContainerClass *class;
g_return_if_fail (GTK_IS_CONTAINER (container)); g_return_if_fail (GTK_IS_CONTAINER (container));
g_return_if_fail (callback != NULL); g_return_if_fail (callback != NULL);
class = GTK_CONTAINER_GET_CLASS (container); if (GTK_CONTAINER_GET_CLASS (container)->forall)
GTK_CONTAINER_GET_CLASS (container)->forall (container, callback, callback_data);
if (class->forall)
class->forall (container, callback, callback_data);
} }
/** /**
@ -746,10 +653,8 @@ gtk_container_real_get_path_for_child (GtkContainer *container,
GtkWidget *child) GtkWidget *child)
{ {
GtkWidgetPath *path; GtkWidgetPath *path;
GtkWidget *widget = GTK_WIDGET (container);
path = _gtk_widget_create_path (widget);
path = _gtk_widget_create_path (GTK_WIDGET (container));
gtk_widget_path_append_for_widget (path, child); gtk_widget_path_append_for_widget (path, child);
return path; return path;

View File

@ -686,6 +686,11 @@ static gboolean gtk_widget_buildable_custom_tag_start (GtkBuildable
const gchar *tagname, const gchar *tagname,
GMarkupParser *parser, GMarkupParser *parser,
gpointer *data); gpointer *data);
static void gtk_widget_buildable_custom_tag_end (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer data);
static void gtk_widget_buildable_custom_finished (GtkBuildable *buildable, static void gtk_widget_buildable_custom_finished (GtkBuildable *buildable,
GtkBuilder *builder, GtkBuilder *builder,
GObject *child, GObject *child,
@ -2987,7 +2992,6 @@ void
gtk_widget_unparent (GtkWidget *widget) gtk_widget_unparent (GtkWidget *widget)
{ {
GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget); GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
GObjectNotifyQueue *nqueue;
GtkWidget *old_parent; GtkWidget *old_parent;
GtkWidget *old_prev_sibling; GtkWidget *old_prev_sibling;
GtkWidget *toplevel; GtkWidget *toplevel;
@ -3002,7 +3006,6 @@ gtk_widget_unparent (GtkWidget *widget)
gtk_widget_push_verify_invariants (widget); gtk_widget_push_verify_invariants (widget);
g_object_freeze_notify (G_OBJECT (widget)); g_object_freeze_notify (G_OBJECT (widget));
nqueue = g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
toplevel = _gtk_widget_get_toplevel (widget); toplevel = _gtk_widget_get_toplevel (widget);
if (_gtk_widget_is_toplevel (toplevel)) if (_gtk_widget_is_toplevel (toplevel))
@ -3091,9 +3094,6 @@ gtk_widget_unparent (GtkWidget *widget)
g_object_notify_by_pspec (G_OBJECT (widget), widget_props[PROP_PARENT]); g_object_notify_by_pspec (G_OBJECT (widget), widget_props[PROP_PARENT]);
g_object_thaw_notify (G_OBJECT (widget)); g_object_thaw_notify (G_OBJECT (widget));
if (!priv->parent)
g_object_notify_queue_clear (G_OBJECT (widget), nqueue);
g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
gtk_widget_pop_verify_invariants (widget); gtk_widget_pop_verify_invariants (widget);
g_object_unref (widget); g_object_unref (widget);
@ -9469,6 +9469,7 @@ gtk_widget_buildable_interface_init (GtkBuildableIface *iface)
iface->set_buildable_property = gtk_widget_buildable_set_buildable_property; iface->set_buildable_property = gtk_widget_buildable_set_buildable_property;
iface->parser_finished = gtk_widget_buildable_parser_finished; iface->parser_finished = gtk_widget_buildable_parser_finished;
iface->custom_tag_start = gtk_widget_buildable_custom_tag_start; iface->custom_tag_start = gtk_widget_buildable_custom_tag_start;
iface->custom_tag_end = gtk_widget_buildable_custom_tag_end;
iface->custom_finished = gtk_widget_buildable_custom_finished; iface->custom_finished = gtk_widget_buildable_custom_finished;
iface->add_child = gtk_widget_buildable_add_child; iface->add_child = gtk_widget_buildable_add_child;
} }
@ -10137,6 +10138,15 @@ gtk_widget_buildable_custom_tag_start (GtkBuildable *buildable,
return FALSE; return FALSE;
} }
static void
gtk_widget_buildable_custom_tag_end (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer data)
{
}
void void
_gtk_widget_buildable_finish_accelerator (GtkWidget *widget, _gtk_widget_buildable_finish_accelerator (GtkWidget *widget,
GtkWidget *toplevel, GtkWidget *toplevel,