1 /* dzl-preferences-entry.c
2 *
3 * Copyright (C) 2015-2017 Christian Hergert <chergert@redhat.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #define G_LOG_DOMAIN "dzl-preferences-entry"
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include "prefs/dzl-preferences-entry.h"
26
27 typedef struct
28 {
29 GtkEntry *entry;
30 GtkLabel *title;
31 } DzlPreferencesEntryPrivate;
32
33 enum {
34 PROP_0,
35 PROP_TITLE,
36 PROP_TEXT,
37 LAST_PROP
38 };
39
40 enum {
41 ACTIVATE,
42 CHANGED,
43 LAST_SIGNAL
44 };
45
G_DEFINE_TYPE_WITH_PRIVATE(DzlPreferencesEntry,dzl_preferences_entry,DZL_TYPE_PREFERENCES_BIN)46 G_DEFINE_TYPE_WITH_PRIVATE (DzlPreferencesEntry, dzl_preferences_entry, DZL_TYPE_PREFERENCES_BIN)
47
48 static GParamSpec *properties [LAST_PROP];
49 static guint signals [LAST_SIGNAL];
50
51 static void
52 dzl_preferences_entry_get_property (GObject *object,
53 guint prop_id,
54 GValue *value,
55 GParamSpec *pspec)
56 {
57 DzlPreferencesEntry *self = DZL_PREFERENCES_ENTRY (object);
58 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
59
60 switch (prop_id)
61 {
62 case PROP_TEXT:
63 g_value_set_string (value, gtk_entry_get_text (priv->entry));
64 break;
65
66 case PROP_TITLE:
67 g_value_set_string (value, gtk_label_get_text (priv->title));
68 break;
69
70 default:
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
72 }
73 }
74
75 static void
dzl_preferences_entry_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)76 dzl_preferences_entry_set_property (GObject *object,
77 guint prop_id,
78 const GValue *value,
79 GParamSpec *pspec)
80 {
81 DzlPreferencesEntry *self = DZL_PREFERENCES_ENTRY (object);
82 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
83
84 switch (prop_id)
85 {
86 case PROP_TEXT:
87 gtk_entry_set_text (priv->entry, g_value_get_string (value));
88 break;
89
90 case PROP_TITLE:
91 gtk_label_set_label (priv->title, g_value_get_string (value));
92 break;
93
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96 }
97 }
98
99 static void
dzl_preferences_entry_activate(DzlPreferencesEntry * self)100 dzl_preferences_entry_activate (DzlPreferencesEntry *self)
101 {
102 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
103
104 g_assert (DZL_IS_PREFERENCES_ENTRY (self));
105
106 gtk_widget_grab_focus (GTK_WIDGET (priv->entry));
107 }
108
109 static void
dzl_preferences_entry_changed(DzlPreferencesEntry * self,GtkEntry * entry)110 dzl_preferences_entry_changed (DzlPreferencesEntry *self,
111 GtkEntry *entry)
112 {
113 const gchar *text;
114
115 g_assert (DZL_IS_PREFERENCES_ENTRY (self));
116 g_assert (GTK_IS_ENTRY (entry));
117
118 text = gtk_entry_get_text (entry);
119 g_signal_emit (self, signals [CHANGED], 0, text);
120 }
121
122 static gboolean
dzl_preferences_entry_matches(DzlPreferencesBin * bin,DzlPatternSpec * spec)123 dzl_preferences_entry_matches (DzlPreferencesBin *bin,
124 DzlPatternSpec *spec)
125 {
126 DzlPreferencesEntry *self = (DzlPreferencesEntry *)bin;
127 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
128 const gchar *tmp;
129
130 g_assert (DZL_IS_PREFERENCES_ENTRY (self));
131 g_assert (spec != NULL);
132
133 tmp = gtk_label_get_label (priv->title);
134 if (tmp && dzl_pattern_spec_match (spec, tmp))
135 return TRUE;
136
137 tmp = gtk_entry_get_text (GTK_ENTRY (priv->entry));
138 if (tmp && dzl_pattern_spec_match (spec, tmp))
139 return TRUE;
140
141 return FALSE;
142 }
143
144 static void
dzl_preferences_entry_class_init(DzlPreferencesEntryClass * klass)145 dzl_preferences_entry_class_init (DzlPreferencesEntryClass *klass)
146 {
147 GObjectClass *object_class = G_OBJECT_CLASS (klass);
148 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
149 DzlPreferencesBinClass *bin_class = DZL_PREFERENCES_BIN_CLASS (klass);
150
151 object_class->get_property = dzl_preferences_entry_get_property;
152 object_class->set_property = dzl_preferences_entry_set_property;
153
154 bin_class->matches = dzl_preferences_entry_matches;
155
156 signals [ACTIVATE] =
157 g_signal_new_class_handler ("activate",
158 G_TYPE_FROM_CLASS (klass),
159 G_SIGNAL_RUN_LAST,
160 G_CALLBACK (dzl_preferences_entry_activate),
161 NULL, NULL, NULL, G_TYPE_NONE, 0);
162
163 signals [CHANGED] =
164 g_signal_new_class_handler ("changed",
165 G_TYPE_FROM_CLASS (klass),
166 G_SIGNAL_RUN_LAST,
167 NULL, NULL, NULL, NULL,
168 G_TYPE_NONE, 1, G_TYPE_STRING);
169
170 widget_class->activate_signal = signals [ACTIVATE];
171
172 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/dazzle/ui/dzl-preferences-entry.ui");
173 gtk_widget_class_bind_template_child_private (widget_class, DzlPreferencesEntry, entry);
174 gtk_widget_class_bind_template_child_private (widget_class, DzlPreferencesEntry, title);
175
176 properties [PROP_TITLE] =
177 g_param_spec_string ("title",
178 "Title",
179 "Title",
180 NULL,
181 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182
183 properties [PROP_TEXT] =
184 g_param_spec_string ("text",
185 "Text",
186 "Text",
187 NULL,
188 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189
190 g_object_class_install_properties (object_class, LAST_PROP, properties);
191 }
192
193 static void
dzl_preferences_entry_init(DzlPreferencesEntry * self)194 dzl_preferences_entry_init (DzlPreferencesEntry *self)
195 {
196 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
197
198 gtk_widget_init_template (GTK_WIDGET (self));
199
200 g_signal_connect_object (priv->entry,
201 "changed",
202 G_CALLBACK (dzl_preferences_entry_changed),
203 self,
204 G_CONNECT_SWAPPED);
205 }
206
207 /**
208 * dzl_preferences_entry_get_title_widget:
209 *
210 * Returns: (transfer none): A #GtkWidget
211 */
212 GtkWidget *
dzl_preferences_entry_get_title_widget(DzlPreferencesEntry * self)213 dzl_preferences_entry_get_title_widget (DzlPreferencesEntry *self)
214 {
215 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
216
217 g_return_val_if_fail (DZL_IS_PREFERENCES_ENTRY (self), NULL);
218
219 return GTK_WIDGET (priv->title);
220 }
221
222 /**
223 * dzl_preferences_entry_get_entry_widget:
224 *
225 * Returns: (transfer none): A #GtkWidget
226 */
227 GtkWidget *
dzl_preferences_entry_get_entry_widget(DzlPreferencesEntry * self)228 dzl_preferences_entry_get_entry_widget (DzlPreferencesEntry *self)
229 {
230 DzlPreferencesEntryPrivate *priv = dzl_preferences_entry_get_instance_private (self);
231
232 g_return_val_if_fail (DZL_IS_PREFERENCES_ENTRY (self), NULL);
233
234 return GTK_WIDGET (priv->entry);
235 }
236