expander: Derive from GtkWidget

We want to remove GtkBin and GtkContainer as they don't
provide much useful functionality anymore. This requires
us to move get_request_mode and compute_expand down.

See #2719
This commit is contained in:
Matthias Clasen 2020-05-07 00:16:08 -04:00
parent 1afc749443
commit 825e98dcca
3 changed files with 60 additions and 128 deletions

View File

@ -23,7 +23,7 @@
static void atk_action_interface_init (AtkActionIface *iface); static void atk_action_interface_init (AtkActionIface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkExpanderAccessible, gtk_expander_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE, G_DEFINE_TYPE_WITH_CODE (GtkExpanderAccessible, gtk_expander_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)) G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
static const gchar * static const gchar *
@ -60,68 +60,33 @@ static gint
gtk_expander_accessible_get_n_children (AtkObject *obj) gtk_expander_accessible_get_n_children (AtkObject *obj)
{ {
GtkWidget *widget; GtkWidget *widget;
GList *children;
gint count = 0;
widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj)); widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
if (widget == NULL) if (widget == NULL)
return 0; return 0;
children = gtk_container_get_children (GTK_CONTAINER(widget)); if (gtk_expander_get_child (GTK_EXPANDER (widget)))
count = g_list_length (children); return 1;
g_list_free (children);
/* See if there is a label - if there is, reduce our count by 1 return 0;
* since we don't want the label included with the children.
*/
if (gtk_expander_get_label_widget (GTK_EXPANDER (widget)))
count -= 1;
return count;
} }
static AtkObject * static AtkObject *
gtk_expander_accessible_ref_child (AtkObject *obj, gtk_expander_accessible_ref_child (AtkObject *obj,
gint i) gint i)
{ {
GList *children, *tmp_list;
AtkObject *accessible; AtkObject *accessible;
GtkWidget *widget; GtkWidget *widget;
GtkWidget *label;
gint index;
widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj)); widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
if (widget == NULL) if (widget == NULL)
return NULL; return NULL;
children = gtk_container_get_children (GTK_CONTAINER (widget)); widget = gtk_expander_get_child (GTK_EXPANDER (widget));
if (widget == NULL)
return NULL;
/* See if there is a label - if there is, we need to skip it accessible = gtk_widget_get_accessible (widget);
* since we don't want the label included with the children.
*/
label = gtk_expander_get_label_widget (GTK_EXPANDER (widget));
if (label)
{
for (index = 0; index <= i; index++)
{
tmp_list = g_list_nth (children, index);
if (label == GTK_WIDGET (tmp_list->data))
{
i += 1;
break;
}
}
}
tmp_list = g_list_nth (children, i);
if (!tmp_list)
{
g_list_free (children);
return NULL;
}
accessible = gtk_widget_get_accessible (GTK_WIDGET (tmp_list->data));
g_list_free (children);
g_object_ref (accessible); g_object_ref (accessible);
return accessible; return accessible;
} }

View File

@ -22,7 +22,7 @@
#error "Only <gtk/gtk-a11y.h> can be included directly." #error "Only <gtk/gtk-a11y.h> can be included directly."
#endif #endif
#include <gtk/a11y/gtkcontaineraccessible.h> #include <gtk/a11y/gtkwidgetaccessible.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -39,14 +39,14 @@ typedef struct _GtkExpanderAccessiblePrivate GtkExpanderAccessiblePrivate;
struct _GtkExpanderAccessible struct _GtkExpanderAccessible
{ {
GtkContainerAccessible parent; GtkWidgetAccessible parent;
GtkExpanderAccessiblePrivate *priv; GtkExpanderAccessiblePrivate *priv;
}; };
struct _GtkExpanderAccessibleClass struct _GtkExpanderAccessibleClass
{ {
GtkContainerAccessibleClass parent_class; GtkWidgetAccessibleClass parent_class;
}; };
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL

View File

@ -148,7 +148,7 @@ typedef struct _GtkExpanderClass GtkExpanderClass;
struct _GtkExpander struct _GtkExpander
{ {
GtkContainer parent_instance; GtkWidget parent_instance;
GtkWidget *label_widget; GtkWidget *label_widget;
@ -167,7 +167,7 @@ struct _GtkExpander
struct _GtkExpanderClass struct _GtkExpanderClass
{ {
GtkContainerClass parent_class; GtkWidgetClass parent_class;
void (* activate) (GtkExpander *expander); void (* activate) (GtkExpander *expander);
}; };
@ -189,11 +189,6 @@ static void gtk_expander_size_allocate (GtkWidget *widget,
static gboolean gtk_expander_focus (GtkWidget *widget, static gboolean gtk_expander_focus (GtkWidget *widget,
GtkDirectionType direction); GtkDirectionType direction);
static void gtk_expander_add (GtkContainer *container,
GtkWidget *widget);
static void gtk_expander_remove (GtkContainer *container,
GtkWidget *widget);
static void gtk_expander_activate (GtkExpander *expander); static void gtk_expander_activate (GtkExpander *expander);
@ -221,7 +216,7 @@ static void gesture_click_released_cb (GtkGestureClick *gesture,
gdouble y, gdouble y,
GtkExpander *expander); GtkExpander *expander);
G_DEFINE_TYPE_WITH_CODE (GtkExpander, gtk_expander, GTK_TYPE_CONTAINER, G_DEFINE_TYPE_WITH_CODE (GtkExpander, gtk_expander, GTK_TYPE_WIDGET,
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_expander_buildable_init)) gtk_expander_buildable_init))
@ -262,30 +257,41 @@ gtk_expander_drag_leave (GtkDropControllerMotion *motion,
} }
} }
static void static GtkSizeRequestMode
gtk_expander_forall (GtkContainer *container, gtk_expander_get_request_mode (GtkWidget *widget)
GtkCallback callback,
gpointer user_data)
{ {
GtkExpander *expander = GTK_EXPANDER (container); GtkExpander *expander = GTK_EXPANDER (widget);
if (expander->child) if (expander->child)
(*callback) (expander->child, user_data); return gtk_widget_get_request_mode (expander->child);
else
return GTK_SIZE_REQUEST_CONSTANT_SIZE;
}
if (expander->label_widget) static void
(*callback) (expander->label_widget, user_data); gtk_expander_compute_expand (GtkWidget *widget,
gboolean *hexpand,
gboolean *vexpand)
{
GtkExpander *expander = GTK_EXPANDER (widget);
if (expander->child)
{
*hexpand = gtk_widget_compute_expand (expander->child, GTK_ORIENTATION_HORIZONTAL);
*vexpand = gtk_widget_compute_expand (expander->child, GTK_ORIENTATION_VERTICAL);
}
else
{
*hexpand = FALSE;
*vexpand = FALSE;
}
} }
static void static void
gtk_expander_class_init (GtkExpanderClass *klass) gtk_expander_class_init (GtkExpanderClass *klass)
{ {
GObjectClass *gobject_class; GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class; GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkContainerClass *container_class;
gobject_class = (GObjectClass *) klass;
widget_class = (GtkWidgetClass *) klass;
container_class = (GtkContainerClass *) klass;
gobject_class->dispose = gtk_expander_dispose; gobject_class->dispose = gtk_expander_dispose;
gobject_class->set_property = gtk_expander_set_property; gobject_class->set_property = gtk_expander_set_property;
@ -295,10 +301,8 @@ gtk_expander_class_init (GtkExpanderClass *klass)
widget_class->focus = gtk_expander_focus; widget_class->focus = gtk_expander_focus;
widget_class->grab_focus = gtk_widget_grab_focus_self; widget_class->grab_focus = gtk_widget_grab_focus_self;
widget_class->measure = gtk_expander_measure; widget_class->measure = gtk_expander_measure;
widget_class->compute_expand = gtk_expander_compute_expand;
container_class->add = gtk_expander_add; widget_class->get_request_mode = gtk_expander_get_request_mode;
container_class->remove = gtk_expander_remove;
container_class->forall = gtk_expander_forall;
klass->activate = gtk_expander_activate; klass->activate = gtk_expander_activate;
@ -433,6 +437,8 @@ gtk_expander_buildable_add_child (GtkBuildable *buildable,
{ {
if (g_strcmp0 (type, "label") == 0) if (g_strcmp0 (type, "label") == 0)
gtk_expander_set_label_widget (GTK_EXPANDER (buildable), GTK_WIDGET (child)); gtk_expander_set_label_widget (GTK_EXPANDER (buildable), GTK_WIDGET (child));
else if (GTK_IS_WIDGET (child))
gtk_expander_set_child (GTK_EXPANDER (buildable), GTK_WIDGET (child));
else else
parent_buildable_iface->add_child (buildable, builder, child, type); parent_buildable_iface->add_child (buildable, builder, child, type);
} }
@ -769,59 +775,6 @@ gtk_expander_focus (GtkWidget *widget,
return TRUE; return TRUE;
} }
static void
gtk_expander_add (GtkContainer *container,
GtkWidget *widget)
{
GtkExpander *expander = GTK_EXPANDER (container);
if (expander->child != NULL)
{
g_warning ("Attempting to add a widget with type %s to a %s, "
"but a %s can only contain one widget at a time; "
"it already contains a widget of type %s",
g_type_name (G_OBJECT_TYPE (widget)),
g_type_name (G_OBJECT_TYPE (container)),
g_type_name (G_OBJECT_TYPE (container)),
g_type_name (G_OBJECT_TYPE (expander->child)));
return;
}
if (expander->expanded)
{
gtk_container_add (GTK_CONTAINER (expander->box), widget);
}
else
{
if (g_object_is_floating (widget))
g_object_ref_sink (widget);
g_object_ref (widget);
}
expander->child = widget;
}
static void
gtk_expander_remove (GtkContainer *container,
GtkWidget *widget)
{
GtkExpander *expander = GTK_EXPANDER (container);
if (expander->label_widget == widget)
gtk_expander_set_label_widget (expander, NULL);
else
{
gtk_container_remove (GTK_CONTAINER (expander->box), widget);
if (!expander->expanded)
{
/* We hold an extra ref */
g_object_unref (widget);
}
GTK_CONTAINER_CLASS (gtk_expander_parent_class)->remove (container, widget);
}
}
static void static void
gtk_expander_activate (GtkExpander *expander) gtk_expander_activate (GtkExpander *expander)
{ {
@ -1221,12 +1174,26 @@ gtk_expander_set_child (GtkExpander *expander,
g_return_if_fail (GTK_IS_EXPANDER (expander)); g_return_if_fail (GTK_IS_EXPANDER (expander));
g_return_if_fail (child == NULL || GTK_IS_WIDGET (child)); g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
g_clear_pointer (&expander->child, gtk_widget_unparent); if (expander->child)
{
gtk_container_remove (GTK_CONTAINER (expander->box), expander->child);
if (!expander->expanded)
g_object_unref (expander->child);
}
expander->child = child; expander->child = child;
if (expander->child) if (expander->child)
gtk_widget_set_parent (expander->child, GTK_WIDGET (expander)); {
if (expander->expanded)
gtk_container_add (GTK_CONTAINER (expander->box), expander->child);
else
{
if (g_object_is_floating (expander->child))
g_object_ref_sink (expander->child);
g_object_ref (expander->child);
}
}
g_object_notify (G_OBJECT (expander), "child"); g_object_notify (G_OBJECT (expander), "child");
} }