2015-10-25 06:13:13 +00:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <glib/gi18n-lib.h>
|
|
|
|
|
|
|
|
#include "strv-editor.h"
|
|
|
|
#include "gtkbutton.h"
|
|
|
|
#include "gtkentry.h"
|
2016-03-10 02:58:38 +00:00
|
|
|
#include "gtkbox.h"
|
|
|
|
#include "gtkstylecontext.h"
|
2015-10-25 06:13:13 +00:00
|
|
|
#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)
|
|
|
|
{
|
2020-05-09 14:33:02 +00:00
|
|
|
GtkWidget *row;
|
|
|
|
|
|
|
|
row = gtk_widget_get_parent (GTK_WIDGET (button));
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_remove (GTK_BOX (gtk_widget_get_parent (row)), row);
|
2015-10-25 06:13:13 +00:00
|
|
|
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);
|
2020-04-10 16:08:16 +00:00
|
|
|
gtk_widget_add_css_class (box, "linked");
|
2015-10-25 06:13:13 +00:00
|
|
|
gtk_widget_show (box);
|
|
|
|
|
|
|
|
entry = gtk_entry_new ();
|
2019-02-28 19:31:36 +00:00
|
|
|
gtk_editable_set_text (GTK_EDITABLE (entry), str);
|
2015-10-25 06:13:13 +00:00
|
|
|
gtk_widget_show (entry);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), entry);
|
2015-10-25 06:13:13 +00:00
|
|
|
g_object_set_data (G_OBJECT (box), "entry", entry);
|
|
|
|
g_signal_connect_swapped (entry, "notify::text", G_CALLBACK (emit_changed), editor);
|
|
|
|
|
2017-01-20 07:02:48 +00:00
|
|
|
button = gtk_button_new_from_icon_name ("user-trash-symbolic");
|
2020-04-10 16:08:16 +00:00
|
|
|
gtk_widget_add_css_class (button, "image-button");
|
2015-10-25 06:13:13 +00:00
|
|
|
gtk_widget_show (button);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), button);
|
2015-10-25 06:13:13 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (remove_string), editor);
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (editor->box), box);
|
2015-10-25 06:13:13 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-03-10 02:58:38 +00:00
|
|
|
gtk_box_set_spacing (GTK_BOX (editor), 6);
|
2015-10-25 06:13:13 +00:00
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (editor), GTK_ORIENTATION_VERTICAL);
|
2016-03-10 02:58:38 +00:00
|
|
|
editor->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
|
2015-10-25 06:13:13 +00:00
|
|
|
gtk_widget_show (editor->box);
|
|
|
|
|
2017-01-20 07:02:48 +00:00
|
|
|
editor->button = gtk_button_new_from_icon_name ("list-add-symbolic");
|
2020-04-10 16:08:16 +00:00
|
|
|
gtk_widget_add_css_class (editor->button, "image-button");
|
2015-11-03 12:42:05 +00:00
|
|
|
gtk_widget_set_focus_on_click (editor->button, FALSE);
|
2015-10-25 06:13:13 +00:00
|
|
|
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);
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (editor), editor->box);
|
|
|
|
gtk_box_append (GTK_BOX (editor), editor->button);
|
2015-10-25 06:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2016-08-29 14:00:17 +00:00
|
|
|
NULL,
|
2015-10-25 06:13:13 +00:00
|
|
|
G_TYPE_NONE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_inspector_strv_editor_set_strv (GtkInspectorStrvEditor *editor,
|
|
|
|
gchar **strv)
|
|
|
|
{
|
2020-05-09 14:33:02 +00:00
|
|
|
GtkWidget *child;
|
2015-10-25 06:13:13 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
editor->blocked = TRUE;
|
|
|
|
|
2020-05-08 06:19:18 +00:00
|
|
|
while ((child = gtk_widget_get_first_child (GTK_WIDGET (editor->box))))
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_remove (GTK_BOX (editor->box), child);
|
2015-10-25 06:13:13 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-05-08 06:19:18 +00:00
|
|
|
GtkWidget *child;
|
2015-10-25 06:13:13 +00:00
|
|
|
GPtrArray *p;
|
|
|
|
|
|
|
|
p = g_ptr_array_new ();
|
|
|
|
|
2020-05-08 06:19:18 +00:00
|
|
|
for (child = gtk_widget_get_first_child (editor->box);
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
2015-10-25 06:13:13 +00:00
|
|
|
{
|
|
|
|
GtkEntry *entry;
|
|
|
|
|
2020-05-08 06:19:18 +00:00
|
|
|
entry = GTK_ENTRY (g_object_get_data (G_OBJECT (child), "entry"));
|
2019-02-28 19:31:36 +00:00
|
|
|
g_ptr_array_add (p, g_strdup (gtk_editable_get_text (GTK_EDITABLE (entry))));
|
2015-10-25 06:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_ptr_array_add (p, NULL);
|
|
|
|
|
|
|
|
return (gchar **)g_ptr_array_free (p, FALSE);
|
|
|
|
}
|