1 /*
2  * Copyright (c) 2015 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 #include <glib/gi18n-lib.h>
20 
21 #include "strv-editor.h"
22 #include "gtkbutton.h"
23 #include "gtkentry.h"
24 #include "gtkbox.h"
25 #include "gtkstylecontext.h"
26 #include "gtkorientable.h"
27 #include "gtkmarshalers.h"
28 
29 enum
30 {
31   CHANGED,
32   N_SIGNALS
33 };
34 
35 static guint signals[N_SIGNALS] = { 0 };
36 
37 G_DEFINE_TYPE (GtkInspectorStrvEditor, gtk_inspector_strv_editor, GTK_TYPE_BOX);
38 
39 static void
emit_changed(GtkInspectorStrvEditor * editor)40 emit_changed (GtkInspectorStrvEditor *editor)
41 {
42   if (editor->blocked)
43     return;
44 
45   g_signal_emit (editor, signals[CHANGED], 0);
46 }
47 
48 static void
remove_string(GtkButton * button,GtkInspectorStrvEditor * editor)49 remove_string (GtkButton              *button,
50                GtkInspectorStrvEditor *editor)
51 {
52   gtk_widget_destroy (gtk_widget_get_parent (GTK_WIDGET (button)));
53   emit_changed (editor);
54 }
55 
56 static void
add_string(GtkInspectorStrvEditor * editor,const gchar * str)57 add_string (GtkInspectorStrvEditor *editor,
58             const gchar            *str)
59 {
60   GtkWidget *box;
61   GtkWidget *entry;
62   GtkWidget *button;
63 
64   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
65   gtk_style_context_add_class (gtk_widget_get_style_context (box), "linked");
66   gtk_widget_show (box);
67 
68   entry = gtk_entry_new ();
69   gtk_entry_set_text (GTK_ENTRY (entry), str);
70   gtk_widget_show (entry);
71   gtk_box_pack_start (GTK_BOX (box), entry, FALSE, TRUE, 0);
72   g_object_set_data (G_OBJECT (box), "entry", entry);
73   g_signal_connect_swapped (entry, "notify::text", G_CALLBACK (emit_changed), editor);
74 
75   button = gtk_button_new_from_icon_name ("user-trash-symbolic", GTK_ICON_SIZE_MENU);
76   gtk_style_context_add_class (gtk_widget_get_style_context (button), "image-button");
77   gtk_widget_show (button);
78   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
79   g_signal_connect (button, "clicked", G_CALLBACK (remove_string), editor);
80 
81   gtk_box_pack_start (GTK_BOX (editor->box), box, FALSE, FALSE, 0);
82 
83   gtk_widget_grab_focus (entry);
84 
85   emit_changed (editor);
86 }
87 
88 static void
add_cb(GtkButton * button,GtkInspectorStrvEditor * editor)89 add_cb (GtkButton              *button,
90         GtkInspectorStrvEditor *editor)
91 {
92   add_string (editor, "");
93 }
94 
95 static void
gtk_inspector_strv_editor_init(GtkInspectorStrvEditor * editor)96 gtk_inspector_strv_editor_init (GtkInspectorStrvEditor *editor)
97 {
98   gtk_box_set_spacing (GTK_BOX (editor), 6);
99   gtk_orientable_set_orientation (GTK_ORIENTABLE (editor), GTK_ORIENTATION_VERTICAL);
100   editor->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
101   gtk_widget_show (editor->box);
102 
103   editor->button = gtk_button_new_from_icon_name ("list-add-symbolic", GTK_ICON_SIZE_MENU);
104   gtk_style_context_add_class (gtk_widget_get_style_context (editor->button), "image-button");
105   gtk_widget_set_focus_on_click (editor->button, FALSE);
106   gtk_widget_set_halign (editor->button, GTK_ALIGN_END);
107   gtk_widget_show (editor->button);
108   g_signal_connect (editor->button, "clicked", G_CALLBACK (add_cb), editor);
109 
110   gtk_box_pack_start (GTK_BOX (editor), editor->box, FALSE, TRUE, 0);
111   gtk_box_pack_start (GTK_BOX (editor), editor->button, FALSE, FALSE, 0);
112 }
113 
114 static void
gtk_inspector_strv_editor_class_init(GtkInspectorStrvEditorClass * class)115 gtk_inspector_strv_editor_class_init (GtkInspectorStrvEditorClass *class)
116 {
117   signals[CHANGED] =
118     g_signal_new ("changed",
119                   G_TYPE_FROM_CLASS (class),
120                   G_SIGNAL_RUN_FIRST,
121                   G_STRUCT_OFFSET (GtkInspectorStrvEditorClass, changed),
122                   NULL, NULL,
123                   NULL,
124                   G_TYPE_NONE, 0);
125 }
126 
127 void
gtk_inspector_strv_editor_set_strv(GtkInspectorStrvEditor * editor,gchar ** strv)128 gtk_inspector_strv_editor_set_strv (GtkInspectorStrvEditor  *editor,
129                                     gchar                  **strv)
130 {
131   GList *children, *l;
132   gint i;
133 
134   editor->blocked = TRUE;
135 
136   children = gtk_container_get_children (GTK_CONTAINER (editor->box));
137   for (l = children; l; l = l->next)
138     gtk_widget_destroy (GTK_WIDGET (l->data));
139   g_list_free (children);
140 
141   if (strv)
142     {
143       for (i = 0; strv[i]; i++)
144         add_string (editor, strv[i]);
145     }
146 
147   editor->blocked = FALSE;
148 
149   emit_changed (editor);
150 }
151 
152 gchar **
gtk_inspector_strv_editor_get_strv(GtkInspectorStrvEditor * editor)153 gtk_inspector_strv_editor_get_strv (GtkInspectorStrvEditor *editor)
154 {
155   GList *children, *l;
156   GPtrArray *p;
157 
158   p = g_ptr_array_new ();
159 
160   children = gtk_container_get_children (GTK_CONTAINER (editor->box));
161   for (l = children; l; l = l->next)
162     {
163       GtkEntry *entry;
164 
165       entry = GTK_ENTRY (g_object_get_data (G_OBJECT (l->data), "entry"));
166       g_ptr_array_add (p, g_strdup (gtk_entry_get_text (entry)));
167     }
168   g_list_free (children);
169 
170   g_ptr_array_add (p, NULL);
171 
172   return (gchar **)g_ptr_array_free (p, FALSE);
173 }
174