inspector: Clean up GtkInspectorActionEditor

Drop the Private struct, dispose properly,
and don't derive from GtkBox.
This commit is contained in:
Matthias Clasen 2020-07-17 23:36:05 -04:00
parent 1bff328895
commit 2de31e4cac
2 changed files with 85 additions and 86 deletions

View File

@ -24,9 +24,14 @@
#include "gtktogglebutton.h"
#include "gtkentry.h"
#include "gtklabel.h"
#include "gtkbox.h"
#include "gtkboxlayout.h"
#include "gtkorientable.h"
struct _GtkInspectorActionEditorPrivate
struct _GtkInspectorActionEditor
{
GtkWidget parent;
GActionGroup *group;
gchar *name;
gboolean enabled;
@ -38,6 +43,11 @@ struct _GtkInspectorActionEditorPrivate
GtkSizeGroup *sg;
};
typedef struct
{
GtkWidgetClass parent;
} GtkInspectorActionEditorClass;
enum
{
PROP_0,
@ -46,21 +56,22 @@ enum
PROP_SIZEGROUP
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorActionEditor, gtk_inspector_action_editor, GTK_TYPE_BOX)
G_DEFINE_TYPE (GtkInspectorActionEditor, gtk_inspector_action_editor, GTK_TYPE_WIDGET)
static void
gtk_inspector_action_editor_init (GtkInspectorActionEditor *editor)
{
editor->priv = gtk_inspector_action_editor_get_instance_private (editor);
g_object_set (editor,
"orientation", GTK_ORIENTATION_HORIZONTAL,
"spacing", 10,
NULL);
GtkBoxLayout *layout;
layout = GTK_BOX_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (editor)));
gtk_orientable_set_orientation (GTK_ORIENTABLE (layout), GTK_ORIENTATION_HORIZONTAL);
gtk_box_layout_set_spacing (layout, 10);
}
typedef void (*VariantEditorChanged) (GtkWidget *editor, gpointer data);
typedef struct {
typedef struct
{
GtkWidget *editor;
VariantEditorChanged callback;
gpointer data;
@ -92,7 +103,7 @@ variant_editor_new (const GVariantType *type,
{
editor = gtk_toggle_button_new_with_label ("FALSE");
g_signal_connect (editor, "notify::active", G_CALLBACK (variant_editor_changed_cb), d);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
{
editor = gtk_entry_new ();
@ -195,9 +206,9 @@ activate_action (GtkWidget *button,
{
GVariant *parameter = NULL;
if (r->priv->parameter_entry)
parameter = variant_editor_get_value (r->priv->parameter_entry);
g_action_group_activate_action (r->priv->group, r->priv->name, parameter);
if (r->parameter_entry)
parameter = variant_editor_get_value (r->parameter_entry);
g_action_group_activate_action (r->group, r->name, parameter);
}
static void
@ -208,7 +219,7 @@ parameter_changed (GtkWidget *editor,
GVariant *value;
value = variant_editor_get_value (editor);
gtk_widget_set_sensitive (r->priv->activate_button, r->priv->enabled && value != NULL);
gtk_widget_set_sensitive (r->activate_button, r->enabled && value != NULL);
if (value)
g_variant_unref (value);
}
@ -222,21 +233,21 @@ state_changed (GtkWidget *editor,
value = variant_editor_get_value (editor);
if (value)
g_action_group_change_action_state (r->priv->group, r->priv->name, value);
g_action_group_change_action_state (r->group, r->name, value);
}
static void
update_enabled (GtkInspectorActionEditor *r,
gboolean enabled)
{
r->priv->enabled = enabled;
if (r->priv->parameter_entry)
r->enabled = enabled;
if (r->parameter_entry)
{
gtk_widget_set_sensitive (r->priv->parameter_entry, enabled);
parameter_changed (r->priv->parameter_entry, r);
gtk_widget_set_sensitive (r->parameter_entry, enabled);
parameter_changed (r->parameter_entry, r);
}
if (r->priv->activate_button)
gtk_widget_set_sensitive (r->priv->activate_button, enabled);
if (r->activate_button)
gtk_widget_set_sensitive (r->activate_button, enabled);
}
static void
@ -245,18 +256,16 @@ action_enabled_changed_cb (GActionGroup *group,
gboolean enabled,
GtkInspectorActionEditor *r)
{
if (!g_str_equal (action_name, r->priv->name))
return;
update_enabled (r, enabled);
if (g_str_equal (action_name, r->name))
update_enabled (r, enabled);
}
static void
update_state (GtkInspectorActionEditor *r,
GVariant *state)
{
if (r->priv->state_entry)
variant_editor_set_value (r->priv->state_entry, state);
if (r->state_entry)
variant_editor_set_value (r->state_entry, state);
}
static void
@ -265,10 +274,8 @@ action_state_changed_cb (GActionGroup *group,
GVariant *state,
GtkInspectorActionEditor *r)
{
if (!g_str_equal (action_name, r->priv->name))
return;
update_state (r, state);
if (g_str_equal (action_name, r->name))
update_state (r, state);
}
static void
@ -280,64 +287,68 @@ constructed (GObject *object)
GtkWidget *activate;
GtkWidget *label;
r->priv->enabled = g_action_group_get_action_enabled (r->priv->group, r->priv->name);
state = g_action_group_get_action_state (r->priv->group, r->priv->name);
r->enabled = g_action_group_get_action_enabled (r->group, r->name);
state = g_action_group_get_action_state (r->group, r->name);
row = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
activate = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
gtk_box_append (GTK_BOX (row), activate);
if (r->priv->sg)
gtk_size_group_add_widget (r->priv->sg, activate);
if (r->sg)
gtk_size_group_add_widget (r->sg, activate);
r->priv->activate_button = gtk_button_new_with_label (_("Activate"));
g_signal_connect (r->priv->activate_button, "clicked", G_CALLBACK (activate_action), r);
r->activate_button = gtk_button_new_with_label (_("Activate"));
g_signal_connect (r->activate_button, "clicked", G_CALLBACK (activate_action), r);
gtk_widget_set_sensitive (r->priv->activate_button, r->priv->enabled);
gtk_box_append (GTK_BOX (activate), r->priv->activate_button);
gtk_widget_set_sensitive (r->activate_button, r->enabled);
gtk_box_append (GTK_BOX (activate), r->activate_button);
r->priv->parameter_type = g_action_group_get_action_parameter_type (r->priv->group, r->priv->name);
if (r->priv->parameter_type)
r->parameter_type = g_action_group_get_action_parameter_type (r->group, r->name);
if (r->parameter_type)
{
r->priv->parameter_entry = variant_editor_new (r->priv->parameter_type, parameter_changed, r);
gtk_widget_set_sensitive (r->priv->parameter_entry, r->priv->enabled);
gtk_box_append (GTK_BOX (activate), r->priv->parameter_entry);
r->parameter_entry = variant_editor_new (r->parameter_type, parameter_changed, r);
gtk_widget_set_sensitive (r->parameter_entry, r->enabled);
gtk_box_append (GTK_BOX (activate), r->parameter_entry);
}
gtk_box_append (GTK_BOX (r), row);
gtk_widget_set_parent (row, GTK_WIDGET (r));
if (state)
{
r->priv->state_type = g_variant_type_copy (g_variant_get_type (state));
r->state_type = g_variant_type_copy (g_variant_get_type (state));
row = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
label = gtk_label_new (_("Set State"));
if (r->priv->sg)
gtk_size_group_add_widget (r->priv->sg, label);
if (r->sg)
gtk_size_group_add_widget (r->sg, label);
gtk_box_append (GTK_BOX (row), label);
r->priv->state_entry = variant_editor_new (r->priv->state_type, state_changed, r);
variant_editor_set_value (r->priv->state_entry, state);
gtk_box_append (GTK_BOX (row), r->priv->state_entry);
gtk_box_append (GTK_BOX (r), row);
r->state_entry = variant_editor_new (r->state_type, state_changed, r);
variant_editor_set_value (r->state_entry, state);
gtk_box_append (GTK_BOX (row), r->state_entry);
gtk_widget_set_parent (row, GTK_WIDGET (r));
}
g_signal_connect (r->priv->group, "action-enabled-changed",
g_signal_connect (r->group, "action-enabled-changed",
G_CALLBACK (action_enabled_changed_cb), r);
g_signal_connect (r->priv->group, "action-state-changed",
g_signal_connect (r->group, "action-state-changed",
G_CALLBACK (action_state_changed_cb), r);
}
static void
finalize (GObject *object)
dispose (GObject *object)
{
GtkInspectorActionEditor *r = GTK_INSPECTOR_ACTION_EDITOR (object);
GtkWidget *child;
g_free (r->priv->name);
g_clear_object (&r->priv->sg);
if (r->priv->state_type)
g_variant_type_free (r->priv->state_type);
g_signal_handlers_disconnect_by_func (r->priv->group, action_enabled_changed_cb, r);
g_signal_handlers_disconnect_by_func (r->priv->group, action_state_changed_cb, r);
g_free (r->name);
g_clear_object (&r->sg);
if (r->state_type)
g_variant_type_free (r->state_type);
g_signal_handlers_disconnect_by_func (r->group, action_enabled_changed_cb, r);
g_signal_handlers_disconnect_by_func (r->group, action_state_changed_cb, r);
G_OBJECT_CLASS (gtk_inspector_action_editor_parent_class)->finalize (object);
while ((child = gtk_widget_get_first_child (GTK_WIDGET (r))))
gtk_widget_unparent (child);
G_OBJECT_CLASS (gtk_inspector_action_editor_parent_class)->dispose (object);
}
static void
@ -351,15 +362,15 @@ get_property (GObject *object,
switch (param_id)
{
case PROP_GROUP:
g_value_set_object (value, r->priv->group);
g_value_set_object (value, r->group);
break;
case PROP_NAME:
g_value_set_string (value, r->priv->name);
g_value_set_string (value, r->name);
break;
case PROP_SIZEGROUP:
g_value_set_object (value, r->priv->sg);
g_value_set_object (value, r->sg);
break;
default:
@ -379,16 +390,16 @@ set_property (GObject *object,
switch (param_id)
{
case PROP_GROUP:
r->priv->group = g_value_get_object (value);
r->group = g_value_get_object (value);
break;
case PROP_NAME:
g_free (r->priv->name);
r->priv->name = g_value_dup_string (value);
g_free (r->name);
r->name = g_value_dup_string (value);
break;
case PROP_SIZEGROUP:
r->priv->sg = g_value_dup_object (value);
r->sg = g_value_dup_object (value);
break;
default:
@ -401,9 +412,10 @@ static void
gtk_inspector_action_editor_class_init (GtkInspectorActionEditorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->constructed = constructed;
object_class->finalize = finalize;
object_class->dispose = dispose;
object_class->get_property = get_property;
object_class->set_property = set_property;
@ -417,6 +429,8 @@ gtk_inspector_action_editor_class_init (GtkInspectorActionEditorClass *klass)
g_object_class_install_property (object_class, PROP_SIZEGROUP,
g_param_spec_object ("sizegroup", "Size Group", "The Size Group for activate",
GTK_TYPE_SIZE_GROUP, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
}
GtkWidget *

View File

@ -19,34 +19,19 @@
#define _GTK_INSPECTOR_ACTION_EDITOR_H_
#include <gtk/gtkbox.h>
#include <gtk/gtkwidget.h>
#include <gtk/gtksizegroup.h>
#define GTK_TYPE_INSPECTOR_ACTION_EDITOR (gtk_inspector_action_editor_get_type())
#define GTK_INSPECTOR_ACTION_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GTK_TYPE_INSPECTOR_ACTION_EDITOR, GtkInspectorActionEditor))
#define GTK_INSPECTOR_ACTION_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GTK_TYPE_INSPECTOR_ACTION_EDITOR, GtkInspectorActionEditorClass))
#define GTK_INSPECTOR_IS_ACTION_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GTK_TYPE_INSPECTOR_ACTION_EDITOR))
#define GTK_INSPECTOR_IS_ACTION_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GTK_TYPE_INSPECTOR_ACTION_EDITOR))
#define GTK_INSPECTOR_ACTION_EDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_INSPECTOR_ACTION_EDITOR, GtkInspectorActionEditorClass))
typedef struct _GtkInspectorActionEditorPrivate GtkInspectorActionEditorPrivate;
typedef struct
{
GtkBox parent;
GtkInspectorActionEditorPrivate *priv;
} GtkInspectorActionEditor;
typedef struct
{
GtkBoxClass parent;
} GtkInspectorActionEditorClass;
typedef struct _GtkInspectorActionEditor GtkInspectorActionEditor;
G_BEGIN_DECLS
GType gtk_inspector_action_editor_get_type (void);
GtkWidget *gtk_inspector_action_editor_new (GActionGroup *group,
const gchar *name,