Commit 5a3c3d82 authored by Matthias Clasen's avatar Matthias Clasen

Merge branch 'inspector-controllers' into 'master'

Inspector controllers

See merge request !3101
parents 40446201 ccd9827b
Pipeline #249111 failed with stages
in 26 minutes and 41 seconds
......@@ -19,8 +19,8 @@
#include <glib/gi18n-lib.h>
#include "action-editor.h"
#include "variant-editor.h"
#include "gtksizegroup.h"
#include "gtktogglebutton.h"
#include "gtkentry.h"
#include "gtklabel.h"
......@@ -41,7 +41,7 @@ struct _GtkInspectorActionEditor
GtkWidget *activate_button;
GtkWidget *parameter_entry;
GtkWidget *state_entry;
GtkSizeGroup *sg;
GtkWidget *state_editor;
};
typedef struct
......@@ -59,147 +59,7 @@ enum
G_DEFINE_TYPE (GtkInspectorActionEditor, gtk_inspector_action_editor, GTK_TYPE_WIDGET)
static void
gtk_inspector_action_editor_init (GtkInspectorActionEditor *editor)
{
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
{
GtkWidget *editor;
VariantEditorChanged callback;
gpointer data;
} VariantEditorData;
static void
variant_editor_changed_cb (GObject *obj,
GParamSpec *pspec,
VariantEditorData *data)
{
data->callback (data->editor, data->data);
}
static GtkWidget *
variant_editor_new (const GVariantType *type,
VariantEditorChanged callback,
gpointer data)
{
GtkWidget *editor;
GtkWidget *label;
GtkWidget *entry;
VariantEditorData *d;
d = g_new (VariantEditorData, 1);
d->callback = callback;
d->data = data;
if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
{
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 ();
gtk_editable_set_width_chars (GTK_EDITABLE (editor), 10);
g_signal_connect (editor, "notify::text", G_CALLBACK (variant_editor_changed_cb), d);
}
else
{
editor = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
entry = gtk_entry_new ();
gtk_editable_set_width_chars (GTK_EDITABLE (entry), 10);
gtk_box_append (GTK_BOX (editor), entry);
label = gtk_label_new (g_variant_type_peek_string (type));
gtk_box_append (GTK_BOX (editor), label);
g_signal_connect (entry, "notify::text", G_CALLBACK (variant_editor_changed_cb), d);
}
g_object_set_data (G_OBJECT (editor), "type", (gpointer)type);
d->editor = editor;
g_object_set_data_full (G_OBJECT (editor), "callback", d, g_free);
return editor;
}
static void
variant_editor_set_value (GtkWidget *editor,
GVariant *value)
{
const GVariantType *type;
gpointer data;
data = g_object_get_data (G_OBJECT (editor), "callback");
g_signal_handlers_block_by_func (editor, variant_editor_changed_cb, data);
type = g_variant_get_type (value);
if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
{
GtkToggleButton *tb = GTK_TOGGLE_BUTTON (editor);
GtkWidget *child;
gtk_toggle_button_set_active (tb, g_variant_get_boolean (value));
child = gtk_button_get_child (GTK_BUTTON (tb));
gtk_label_set_text (GTK_LABEL (child),
g_variant_get_boolean (value) ? "TRUE" : "FALSE");
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
{
GtkEntry *entry = GTK_ENTRY (editor);
gtk_editable_set_text (GTK_EDITABLE (entry), g_variant_get_string (value, NULL));
}
else
{
GtkWidget *entry;
char *text;
entry = gtk_widget_get_first_child (editor);
text = g_variant_print (value, FALSE);
gtk_editable_set_text (GTK_EDITABLE (entry), text);
g_free (text);
}
g_signal_handlers_unblock_by_func (editor, variant_editor_changed_cb, data);
}
static GVariant *
variant_editor_get_value (GtkWidget *editor)
{
const GVariantType *type;
GVariant *value;
type = (const GVariantType *) g_object_get_data (G_OBJECT (editor), "type");
if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
{
GtkToggleButton *tb = GTK_TOGGLE_BUTTON (editor);
value = g_variant_new_boolean (gtk_toggle_button_get_active (tb));
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
{
GtkEntry *entry = GTK_ENTRY (editor);
value = g_variant_new_string (gtk_editable_get_text (GTK_EDITABLE (entry)));
}
else
{
GtkWidget *entry;
const char *text;
entry = gtk_widget_get_first_child (editor);
text = gtk_editable_get_text (GTK_EDITABLE (entry));
value = g_variant_parse (type, text, NULL, NULL, NULL);
}
return value;
}
static void update_widgets (GtkInspectorActionEditor *r);
static void
activate_action (GtkWidget *button,
......@@ -208,11 +68,12 @@ activate_action (GtkWidget *button,
GVariant *parameter = NULL;
if (r->parameter_entry)
parameter = variant_editor_get_value (r->parameter_entry);
parameter = gtk_inspector_variant_editor_get_value (r->parameter_entry);
if (G_IS_ACTION_GROUP (r->owner))
g_action_group_activate_action (G_ACTION_GROUP (r->owner), r->name, parameter);
else if (GTK_IS_ACTION_MUXER (r->owner))
gtk_action_muxer_activate_action (GTK_ACTION_MUXER (r->owner), r->name, parameter);
update_widgets (r);
}
static void
......@@ -222,7 +83,7 @@ parameter_changed (GtkWidget *editor,
GtkInspectorActionEditor *r = data;
GVariant *value;
value = variant_editor_get_value (editor);
value = gtk_inspector_variant_editor_get_value (editor);
gtk_widget_set_sensitive (r->activate_button, r->enabled && value != NULL);
if (value)
g_variant_unref (value);
......@@ -235,7 +96,7 @@ state_changed (GtkWidget *editor,
GtkInspectorActionEditor *r = data;
GVariant *value;
value = variant_editor_get_value (editor);
value = gtk_inspector_variant_editor_get_value (editor);
if (value)
{
if (G_IS_ACTION_GROUP (r->owner))
......@@ -245,6 +106,40 @@ state_changed (GtkWidget *editor,
}
}
static void
gtk_inspector_action_editor_init (GtkInspectorActionEditor *r)
{
GtkBoxLayout *layout;
GtkWidget *row, *activate, *label;
layout = GTK_BOX_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (r)));
gtk_orientable_set_orientation (GTK_ORIENTABLE (layout), GTK_ORIENTATION_HORIZONTAL);
gtk_box_layout_set_spacing (layout, 10);
row = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
activate = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
gtk_box_append (GTK_BOX (row), activate);
r->activate_button = gtk_button_new_with_label (_("Activate"));
g_signal_connect (r->activate_button, "clicked", G_CALLBACK (activate_action), r);
gtk_box_append (GTK_BOX (activate), r->activate_button);
r->parameter_entry = gtk_inspector_variant_editor_new (NULL, parameter_changed, r);
gtk_widget_hide (r->parameter_entry);
gtk_box_append (GTK_BOX (activate), r->parameter_entry);
gtk_widget_set_parent (row, GTK_WIDGET (r));
r->state_editor = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
label = gtk_label_new (_("Set State"));
gtk_box_append (GTK_BOX (r->state_editor), label);
r->state_entry = gtk_inspector_variant_editor_new (NULL, state_changed, r);
gtk_box_append (GTK_BOX (r->state_editor), r->state_entry);
gtk_widget_set_parent (r->state_editor, GTK_WIDGET (r));
gtk_widget_hide (r->state_editor);
}
static void
update_enabled (GtkInspectorActionEditor *r,
gboolean enabled)
......@@ -274,7 +169,7 @@ update_state (GtkInspectorActionEditor *r,
GVariant *state)
{
if (r->state_entry)
variant_editor_set_value (r->state_entry, state);
gtk_inspector_variant_editor_set_value (r->state_entry, state);
}
static void
......@@ -288,13 +183,9 @@ action_state_changed_cb (GActionGroup *group,
}
static void
constructed (GObject *object)
update_widgets (GtkInspectorActionEditor *r)
{
GtkInspectorActionEditor *r = GTK_INSPECTOR_ACTION_EDITOR (object);
GVariant *state;
GtkWidget *row;
GtkWidget *activate;
GtkWidget *label;
if (G_IS_ACTION_GROUP (r->owner))
g_action_group_query_action (G_ACTION_GROUP (r->owner), r->name,
......@@ -307,40 +198,27 @@ constructed (GObject *object)
else
state = NULL;
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->sg)
gtk_size_group_add_widget (r->sg, activate);
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->activate_button, r->enabled);
gtk_box_append (GTK_BOX (activate), r->activate_button);
if (r->parameter_type)
{
r->parameter_entry = variant_editor_new (r->parameter_type, parameter_changed, r);
gtk_inspector_variant_editor_set_type (r->parameter_entry, r->parameter_type);
gtk_widget_show (r->parameter_entry);
gtk_widget_set_sensitive (r->parameter_entry, r->enabled);
gtk_box_append (GTK_BOX (activate), r->parameter_entry);
}
gtk_widget_set_parent (row, GTK_WIDGET (r));
else
gtk_widget_hide (r->parameter_entry);
if (state)
{
if (r->state_type)
g_variant_type_free (r->state_type);
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->sg)
gtk_size_group_add_widget (r->sg, label);
gtk_box_append (GTK_BOX (row), label);
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));
gtk_inspector_variant_editor_set_value (r->state_entry, state);
gtk_widget_show (r->state_editor);
}
else
gtk_widget_hide (r->state_editor);
if (G_IS_ACTION_GROUP (r->owner))
{
......@@ -358,11 +236,13 @@ dispose (GObject *object)
GtkWidget *child;
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->owner, action_enabled_changed_cb, r);
g_signal_handlers_disconnect_by_func (r->owner, action_state_changed_cb, r);
if (r->owner)
{
g_signal_handlers_disconnect_by_func (r->owner, action_enabled_changed_cb, r);
g_signal_handlers_disconnect_by_func (r->owner, action_state_changed_cb, r);
}
while ((child = gtk_widget_get_first_child (GTK_WIDGET (r))))
gtk_widget_unparent (child);
......@@ -388,10 +268,6 @@ get_property (GObject *object,
g_value_set_string (value, r->name);
break;
case PROP_SIZEGROUP:
g_value_set_object (value, r->sg);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
......@@ -409,6 +285,11 @@ set_property (GObject *object,
switch (param_id)
{
case PROP_OWNER:
if (r->owner)
{
g_signal_handlers_disconnect_by_func (r->owner, action_enabled_changed_cb, r);
g_signal_handlers_disconnect_by_func (r->owner, action_state_changed_cb, r);
}
r->owner = g_value_get_object (value);
break;
......@@ -417,10 +298,6 @@ set_property (GObject *object,
r->name = g_value_dup_string (value);
break;
case PROP_SIZEGROUP:
r->sg = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
break;
......@@ -433,35 +310,34 @@ 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->dispose = dispose;
object_class->get_property = get_property;
object_class->set_property = set_property;
g_object_class_install_property (object_class, PROP_OWNER,
g_param_spec_object ("owner", "Owner", "The owner of the action",
G_TYPE_OBJECT, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
G_TYPE_OBJECT, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_NAME,
g_param_spec_string ("name", "Name", "The action name",
NULL, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
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));
NULL, G_PARAM_READWRITE));
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
}
GtkWidget *
gtk_inspector_action_editor_new (GObject *owner,
const char *name,
GtkSizeGroup *activate)
gtk_inspector_action_editor_new (void)
{
return g_object_new (GTK_TYPE_INSPECTOR_ACTION_EDITOR, NULL);
}
void
gtk_inspector_action_editor_set (GtkInspectorActionEditor *self,
GObject *owner,
const char *name)
{
return g_object_new (GTK_TYPE_INSPECTOR_ACTION_EDITOR,
"owner", owner,
"name", name,
"sizegroup", activate,
NULL);
g_object_set (self, "owner", owner, "name", name, NULL);
update_widgets (self);
}
void
......
......@@ -33,10 +33,11 @@ typedef struct _GtkInspectorActionEditor GtkInspectorActionEditor;
G_BEGIN_DECLS
GType gtk_inspector_action_editor_get_type (void);
GtkWidget *gtk_inspector_action_editor_new (GObject *owner,
const char *name,
GtkSizeGroup *activate);
void gtk_inspector_action_editor_update (GtkInspectorActionEditor *r,
GtkWidget *gtk_inspector_action_editor_new (void);
void gtk_inspector_action_editor_set (GtkInspectorActionEditor *self,
GObject *owner,
const char *name);
void gtk_inspector_action_editor_update (GtkInspectorActionEditor *self,
gboolean enabled,
GVariant *state);
......
......@@ -46,6 +46,7 @@ struct _GtkInspectorActions
GObject *object;
GListStore *actions;
GtkSortListModel *sorted;
GtkColumnViewColumn *name;
};
......@@ -90,6 +91,7 @@ setup_name_cb (GtkSignalListItemFactory *factory,
label = gtk_label_new (NULL);
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_widget_add_css_class (label, "cell");
gtk_list_item_set_child (list_item, label);
}
......@@ -114,6 +116,7 @@ setup_enabled_cb (GtkSignalListItemFactory *factory,
label = gtk_label_new (NULL);
gtk_label_set_xalign (GTK_LABEL (label), 0.5);
gtk_widget_add_css_class (label, "cell");
gtk_list_item_set_child (list_item, label);
}
......@@ -149,8 +152,8 @@ setup_parameter_cb (GtkSignalListItemFactory *factory,
label = gtk_label_new (NULL);
gtk_label_set_xalign (GTK_LABEL (label), 0.5);
gtk_list_item_set_child (list_item, label);
gtk_widget_add_css_class (label, "cell");
gtk_list_item_set_child (list_item, label);
}
static void
......@@ -189,8 +192,8 @@ setup_state_cb (GtkSignalListItemFactory *factory,
gtk_widget_set_margin_start (label, 5);
gtk_widget_set_margin_end (label, 5);
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_list_item_set_child (list_item, label);
gtk_widget_add_css_class (label, "cell");
gtk_list_item_set_child (list_item, label);
}
static void
......@@ -202,7 +205,6 @@ bind_state_cb (GtkSignalListItemFactory *factory,
GObject *owner;
const char *name;
GVariant *state;
char *state_string;
item = gtk_list_item_get_item (list_item);
label = gtk_list_item_get_child (list_item);
......@@ -218,15 +220,27 @@ bind_state_cb (GtkSignalListItemFactory *factory,
state = NULL;
if (state)
state_string = g_variant_print (state, FALSE);
{
char *state_string;
state_string = g_variant_print (state, FALSE);
gtk_label_set_label (GTK_LABEL (label), state_string);
g_free (state_string);
g_variant_unref (state);
}
else
state_string = g_strdup ("");
gtk_label_set_label (GTK_LABEL (label), "");
}
gtk_label_set_label (GTK_LABEL (label), state_string);
static void
setup_changes_cb (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *editor;
g_free (state_string);
if (state)
g_variant_unref (state);
editor = gtk_inspector_action_editor_new ();
gtk_widget_add_css_class (editor, "cell");
gtk_list_item_set_child (list_item, editor);
}
static void
......@@ -239,20 +253,14 @@ bind_changes_cb (GtkSignalListItemFactory *factory,
GtkWidget *editor;
item = gtk_list_item_get_item (list_item);
editor = gtk_list_item_get_child (list_item);
owner = action_holder_get_owner (ACTION_HOLDER (item));
name = action_holder_get_name (ACTION_HOLDER (item));
editor = gtk_inspector_action_editor_new (owner, name, NULL);
gtk_widget_add_css_class (editor, "cell");
gtk_list_item_set_child (list_item, editor);
}
static void
unbind_changes_cb (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
gtk_list_item_set_child (list_item, NULL);
gtk_inspector_action_editor_set (GTK_INSPECTOR_ACTION_EDITOR (editor),
owner,
name);
}
static void
......@@ -284,12 +292,15 @@ add_muxer (GtkInspectorActions *sl,
static gboolean
reload (GtkInspectorActions *sl)
{
g_list_store_remove_all (sl->actions);
gboolean loaded = FALSE;
g_object_unref (sl->actions);
sl->actions = g_list_store_new (ACTION_TYPE_HOLDER);
if (GTK_IS_APPLICATION (sl->object))
{
add_group (sl, G_ACTION_GROUP (sl->object));
return TRUE;
loaded = TRUE;
}
else if (GTK_IS_WIDGET (sl->object))
{
......@@ -299,11 +310,13 @@ reload (GtkInspectorActions *sl)
if (muxer)
{
add_muxer (sl, muxer);
return TRUE;
loaded = TRUE;
}
}
return FALSE;
gtk_sort_list_model_set_model (sl->sorted, G_LIST_MODEL (sl->actions));
return loaded;
}
static void
......@@ -325,10 +338,10 @@ gtk_inspector_actions_set_object (GtkInspectorActions *sl,
gtk_stack_page_set_visible (page, FALSE);
g_set_object (&sl->object, object);
loaded = reload (sl);
gtk_stack_page_set_visible (page, loaded);
gtk_column_view_sort_by_column (GTK_COLUMN_VIEW (sl->list), sl->name, GTK_SORT_ASCENDING);
loaded = reload (sl);
gtk_stack_page_set_visible (page, loaded);
}
static void
......@@ -382,7 +395,6 @@ constructed (GObject *object)
{
GtkInspectorActions *sl = GTK_INSPECTOR_ACTIONS (object);
GtkSorter *sorter;
GListModel *sorted;
GListModel *model;
g_signal_connect_swapped (sl->button, "clicked",
......@@ -397,9 +409,9 @@ constructed (GObject *object)
g_object_unref (sorter);
sl->actions = g_list_store_new (ACTION_TYPE_HOLDER);
sorted = G_LIST_MODEL (gtk_sort_list_model_new (g_object_ref (G_LIST_MODEL (sl->actions)),
g_object_ref (gtk_column_view_get_sorter (GTK_COLUMN_VIEW (sl->list)))));
model = G_LIST_MODEL (gtk_no_selection_new (sorted));
sl->sorted = gtk_sort_list_model_new (g_object_ref (G_LIST_MODEL (sl->actions)),
g_object_ref (gtk_column_view_get_sorter (GTK_COLUMN_VIEW (sl->list))));
model = G_LIST_MODEL (gtk_no_selection_new (g_object_ref (G_LIST_MODEL (sl->sorted))));
gtk_column_view_set_model (GTK_COLUMN_VIEW (sl->list), GTK_SELECTION_MODEL (model));
g_object_unref (model);
}
......@@ -410,6 +422,7 @@ dispose (GObject *object)
GtkInspectorActions *sl = GTK_INSPECTOR_ACTIONS (object);
GtkWidget *child;
g_clear_object (&sl->sorted);
g_clear_object (&sl->actions);
g_clear_object (&sl->object);
......@@ -445,8 +458,8 @@ gtk_inspector_actions_class_init (GtkInspectorActionsClass *klass)
gtk_widget_class_bind_template_callback (widget_class, bind_parameter_cb);
gtk_widget_class_bind_template_callback (widget_class, setup_state_cb);
gtk_widget_class_bind_template_callback (widget_class, bind_state_cb);
gtk_widget_class_bind_template_callback (widget_class, setup_changes_cb);
gtk_widget_class_bind_template_callback (widget_class, bind_changes_cb);
gtk_widget_class_bind_template_callback (widget_class, unbind_changes_cb);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
}
......
......@@ -64,8 +64,8 @@
<property name="expand">1</property>