From 1e112ca75e13aa68b795e04f64c23758a05b4c50 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 25 Oct 2015 02:13:13 -0400 Subject: [PATCH] inspector: Allow editing strv-valued properties These do occur in some places, and we can easily allow editing them. --- gtk/inspector/Makefile.inc | 2 + gtk/inspector/prop-editor.c | 68 +++++++++++++++ gtk/inspector/strv-editor.c | 167 ++++++++++++++++++++++++++++++++++++ gtk/inspector/strv-editor.h | 67 +++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 gtk/inspector/strv-editor.c create mode 100644 gtk/inspector/strv-editor.h diff --git a/gtk/inspector/Makefile.inc b/gtk/inspector/Makefile.inc index 92fb1b9743..ce4efefaab 100644 --- a/gtk/inspector/Makefile.inc +++ b/gtk/inspector/Makefile.inc @@ -25,6 +25,7 @@ inspector_c_sources = \ inspector/size-groups.c \ inspector/statistics.c \ inspector/style-prop-list.c \ + inspector/strv-editor.c \ inspector/treewalk.c \ inspector/visual.c \ inspector/window.c @@ -55,6 +56,7 @@ inspector_h_sources = \ inspector/size-groups.h \ inspector/statistics.h \ inspector/style-prop-list.h \ + inspector/strv-editor.h \ inspector/treewalk.h \ inspector/visual.h \ inspector/window.h diff --git a/gtk/inspector/prop-editor.c b/gtk/inspector/prop-editor.c index 0efbfa326a..c6a70a988c 100644 --- a/gtk/inspector/prop-editor.c +++ b/gtk/inspector/prop-editor.c @@ -19,6 +19,7 @@ #include #include "prop-editor.h" +#include "strv-editor.h" #include "object-tree.h" #include "gtkactionable.h" @@ -151,6 +152,24 @@ g_object_connect_property (GObject *object, g_free (with_detail); } +static void +block_notify (GObject *editor) +{ + DisconnectData *dd = (DisconnectData *)g_object_get_data (editor, "alive-object-data"); + + if (dd) + g_signal_handler_block (dd->instance, dd->id); +} + +static void +unblock_notify (GObject *editor) +{ + DisconnectData *dd = (DisconnectData *)g_object_get_data (editor, "alive-object-data"); + + if (dd) + g_signal_handler_unblock (dd->instance, dd->id); +} + typedef struct { GObject *obj; @@ -399,6 +418,38 @@ string_changed (GObject *object, GParamSpec *pspec, gpointer data) g_value_unset (&val); } +static void +strv_modified (GtkInspectorStrvEditor *editor, ObjectProperty *p) +{ + GValue val = G_VALUE_INIT; + gchar **strv; + + g_value_init (&val, G_TYPE_STRV); + strv = gtk_inspector_strv_editor_get_strv (editor); + g_value_take_boxed (&val, strv); + block_notify (G_OBJECT (editor)); + set_property_value (p->obj, p->spec, &val); + unblock_notify (G_OBJECT (editor)); + g_value_unset (&val); +} + +static void +strv_changed (GObject *object, GParamSpec *pspec, gpointer data) +{ + GtkInspectorStrvEditor *editor = data; + GValue val = G_VALUE_INIT; + gchar **strv; + + g_value_init (&val, G_TYPE_STRV); + get_property_value (object, pspec, &val); + + strv = g_value_get_boxed (&val); + block_controller (G_OBJECT (editor)); + gtk_inspector_strv_editor_set_strv (editor, strv); + unblock_controller (G_OBJECT (editor)); + + g_value_unset (&val); +} static void bool_modified (GtkToggleButton *tb, ObjectProperty *p) { @@ -1051,6 +1102,23 @@ property_editor (GObject *object, connect_controller (G_OBJECT (prop_edit), "notify::font-desc", object, spec, G_CALLBACK (font_modified)); } + else if (type == G_TYPE_PARAM_BOXED && + G_PARAM_SPEC_VALUE_TYPE (spec) == G_TYPE_STRV) + { + prop_edit = g_object_new (gtk_inspector_strv_editor_get_type (), + "visible", TRUE, + NULL); + + g_object_connect_property (object, spec, + G_CALLBACK (strv_changed), + prop_edit, G_OBJECT (prop_edit)); + + connect_controller (G_OBJECT (prop_edit), "changed", + object, spec, G_CALLBACK (strv_modified)); + + gtk_widget_set_halign (prop_edit, GTK_ALIGN_START); + gtk_widget_set_valign (prop_edit, GTK_ALIGN_CENTER); + } else { msg = g_strdup_printf (_("Uneditable property type: %s"), diff --git a/gtk/inspector/strv-editor.c b/gtk/inspector/strv-editor.c new file mode 100644 index 0000000000..c35ffac24c --- /dev/null +++ b/gtk/inspector/strv-editor.c @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "config.h" +#include + +#include "strv-editor.h" +#include "gtkbutton.h" +#include "gtkentry.h" +#include "gtkorientable.h" +#include "gtkmarshalers.h" + +enum +{ + CHANGED, + N_SIGNALS +}; + +static guint signals[N_SIGNALS] = { 0 }; + +G_DEFINE_TYPE (GtkInspectorStrvEditor, gtk_inspector_strv_editor, GTK_TYPE_BOX); + +static void +emit_changed (GtkInspectorStrvEditor *editor) +{ + if (editor->blocked) + return; + + g_signal_emit (editor, signals[CHANGED], 0); +} + +static void +remove_string (GtkButton *button, + GtkInspectorStrvEditor *editor) +{ + gtk_widget_destroy (gtk_widget_get_parent (GTK_WIDGET (button))); + emit_changed (editor); +} + +static void +add_string (GtkInspectorStrvEditor *editor, + const gchar *str) +{ + GtkWidget *box; + GtkWidget *entry; + GtkWidget *button; + + box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); + gtk_widget_show (box); + + entry = gtk_entry_new (); + gtk_entry_set_text (GTK_ENTRY (entry), str); + gtk_widget_show (entry); + gtk_box_pack_start (GTK_BOX (box), entry, FALSE, TRUE, 0); + g_object_set_data (G_OBJECT (box), "entry", entry); + g_signal_connect_swapped (entry, "notify::text", G_CALLBACK (emit_changed), editor); + + button = gtk_button_new_from_icon_name ("user-trash-symbolic", GTK_ICON_SIZE_MENU); + gtk_widget_show (button); + gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); + g_signal_connect (button, "clicked", G_CALLBACK (remove_string), editor); + + gtk_box_pack_start (GTK_BOX (editor->box), box, FALSE, FALSE, 0); + + gtk_widget_grab_focus (entry); + + emit_changed (editor); +} + +static void +add_cb (GtkButton *button, + GtkInspectorStrvEditor *editor) +{ + add_string (editor, ""); +} + +static void +gtk_inspector_strv_editor_init (GtkInspectorStrvEditor *editor) +{ + gtk_orientable_set_orientation (GTK_ORIENTABLE (editor), GTK_ORIENTATION_VERTICAL); + editor->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); + gtk_widget_show (editor->box); + + editor->button = gtk_button_new_from_icon_name ("list-add-symbolic", GTK_ICON_SIZE_MENU); + gtk_button_set_focus_on_click (GTK_BUTTON (editor->button), FALSE); + gtk_widget_set_halign (editor->button, GTK_ALIGN_END); + gtk_widget_show (editor->button); + g_signal_connect (editor->button, "clicked", G_CALLBACK (add_cb), editor); + + gtk_box_pack_start (GTK_BOX (editor), editor->box, FALSE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (editor), editor->button, FALSE, FALSE, 0); +} + +static void +gtk_inspector_strv_editor_class_init (GtkInspectorStrvEditorClass *class) +{ + signals[CHANGED] = + g_signal_new ("changed", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (GtkInspectorStrvEditorClass, changed), + NULL, NULL, + _gtk_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +void +gtk_inspector_strv_editor_set_strv (GtkInspectorStrvEditor *editor, + gchar **strv) +{ + GList *children, *l; + gint i; + + editor->blocked = TRUE; + + children = gtk_container_get_children (GTK_CONTAINER (editor->box)); + for (l = children; l; l = l->next) + gtk_widget_destroy (GTK_WIDGET (l->data)); + g_list_free (children); + + if (strv) + { + for (i = 0; strv[i]; i++) + add_string (editor, strv[i]); + } + + editor->blocked = FALSE; + + emit_changed (editor); +} + +gchar ** +gtk_inspector_strv_editor_get_strv (GtkInspectorStrvEditor *editor) +{ + GList *children, *l; + GPtrArray *p; + + p = g_ptr_array_new (); + + children = gtk_container_get_children (GTK_CONTAINER (editor->box)); + for (l = children; l; l = l->next) + { + GtkEntry *entry; + + entry = GTK_ENTRY (g_object_get_data (G_OBJECT (l->data), "entry")); + g_ptr_array_add (p, g_strdup (gtk_entry_get_text (entry))); + } + g_list_free (children); + + g_ptr_array_add (p, NULL); + + return (gchar **)g_ptr_array_free (p, FALSE); +} diff --git a/gtk/inspector/strv-editor.h b/gtk/inspector/strv-editor.h new file mode 100644 index 0000000000..10efc368a5 --- /dev/null +++ b/gtk/inspector/strv-editor.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#ifndef _GTK_INSPECTOR_STRV_EDITOR_H_ +#define _GTK_INSPECTOR_STRV_EDITOR_H_ + + +#include + + +#define GTK_TYPE_INSPECTOR_STRV_EDITOR (gtk_inspector_strv_editor_get_type()) +#define GTK_INSPECTOR_STRV_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GTK_TYPE_INSPECTOR_STRV_EDITOR, GtkInspectorStrvEditor)) +#define GTK_INSPECTOR_STRV_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GTK_TYPE_INSPECTOR_STRV_EDITOR, GtkInspectorStrvEditorClass)) +#define GTK_INSPECTOR_IS_STRV_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GTK_TYPE_INSPECTOR_STRV_EDITOR)) +#define GTK_INSPECTOR_IS_STRV_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GTK_TYPE_INSPECTOR_STRV_EDITOR)) +#define GTK_INSPECTOR_STRV_EDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_INSPECTOR_STRV_EDITOR, GtkInspectorStrvEditorClass)) + + +typedef struct +{ + GtkBox parent; + + GtkWidget *box; + GtkWidget *button; + + gboolean blocked; +} GtkInspectorStrvEditor; + +typedef struct +{ + GtkBoxClass parent; + + void (* changed) (GtkInspectorStrvEditor *editor); + +} GtkInspectorStrvEditorClass; + + +G_BEGIN_DECLS + + +GType gtk_inspector_strv_editor_get_type (void); + +void gtk_inspector_strv_editor_set_strv (GtkInspectorStrvEditor *editor, + gchar **strv); + +gchar **gtk_inspector_strv_editor_get_strv (GtkInspectorStrvEditor *editor); + +G_END_DECLS + + +#endif // _GTK_INSPECTOR_STRV_EDITOR_H_ + +// vim: set et: