]> git.sur5r.net Git - glabels/blob - glabels1/src/prop_text_entry.c
e82e6d1db22ca38276929d4ca93b0623b2424a68
[glabels] / glabels1 / src / prop_text_entry.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  prop_text_entry.c:  text entry widget module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <config.h>
24
25 #include "prop_text_entry.h"
26 #include "merge.h"
27 #include "text_node.h"
28
29 #include "debug.h"
30
31 /*===========================================*/
32 /* Private types                             */
33 /*===========================================*/
34
35 enum {
36         CHANGED,
37         LAST_SIGNAL
38 };
39
40 typedef void (*glPropTextEntrySignal) (GtkObject * object, gpointer data);
41
42 /*===========================================*/
43 /* Private globals                           */
44 /*===========================================*/
45
46 static GtkContainerClass *parent_class;
47
48 static gint prop_text_entry_signals[LAST_SIGNAL] = { 0 };
49
50 /*===========================================*/
51 /* Local function prototypes                 */
52 /*===========================================*/
53
54 static void gl_prop_text_entry_class_init (glPropTextEntryClass * class);
55 static void gl_prop_text_entry_init (glPropTextEntry * text_entry);
56 static void gl_prop_text_entry_destroy (GtkObject * object);
57 static void gl_prop_text_entry_construct (glPropTextEntry * text_entry,
58                                           gchar * label, GList * field_defs);
59
60 static void changed_cb (glPropTextEntry * text_entry);
61 static void insert_cb (glPropTextEntry * text_entry);
62 \f
63 /*================================================================*/
64 /* Boilerplate Object stuff.                                      */
65 /*================================================================*/
66 guint
67 gl_prop_text_entry_get_type (void)
68 {
69         static guint prop_text_entry_type = 0;
70
71         if (!prop_text_entry_type) {
72                 GtkTypeInfo prop_text_entry_info = {
73                         "glPropTextEntry",
74                         sizeof (glPropTextEntry),
75                         sizeof (glPropTextEntryClass),
76                         (GtkClassInitFunc) gl_prop_text_entry_class_init,
77                         (GtkObjectInitFunc) gl_prop_text_entry_init,
78                         (GtkArgSetFunc) NULL,
79                         (GtkArgGetFunc) NULL,
80                 };
81
82                 prop_text_entry_type = gtk_type_unique (gtk_vbox_get_type (),
83                                                         &prop_text_entry_info);
84         }
85
86         return prop_text_entry_type;
87 }
88
89 static void
90 gl_prop_text_entry_class_init (glPropTextEntryClass * class)
91 {
92         GtkObjectClass *object_class;
93         GtkWidgetClass *widget_class;
94
95         object_class = (GtkObjectClass *) class;
96         widget_class = (GtkWidgetClass *) class;
97
98         parent_class = gtk_type_class (gtk_vbox_get_type ());
99
100         object_class->destroy = gl_prop_text_entry_destroy;
101
102         prop_text_entry_signals[CHANGED] =
103             gtk_signal_new ("changed", GTK_RUN_LAST, object_class->type,
104                             GTK_SIGNAL_OFFSET (glPropTextEntryClass, changed),
105                             gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
106         gtk_object_class_add_signals (object_class,
107                                       prop_text_entry_signals, LAST_SIGNAL);
108
109         class->changed = NULL;
110 }
111
112 static void
113 gl_prop_text_entry_init (glPropTextEntry * text_entry)
114 {
115         text_entry->text_entry = NULL;
116         text_entry->key_entry = NULL;
117         text_entry->insert_button = NULL;
118 }
119
120 static void
121 gl_prop_text_entry_destroy (GtkObject * object)
122 {
123         glPropTextEntry *text_entry;
124         glPropTextEntryClass *class;
125
126         g_return_if_fail (object != NULL);
127         g_return_if_fail (GL_IS_PROP_TEXT_ENTRY (object));
128
129         text_entry = GL_PROP_TEXT_ENTRY (object);
130         class = GL_PROP_TEXT_ENTRY_CLASS (GTK_OBJECT (text_entry)->klass);
131
132         GTK_OBJECT_CLASS (parent_class)->destroy (object);
133 }
134
135 GtkWidget *
136 gl_prop_text_entry_new (gchar * label,
137                         GList * field_defs)
138 {
139         glPropTextEntry *text_entry;
140
141         text_entry = gtk_type_new (gl_prop_text_entry_get_type ());
142
143         gl_prop_text_entry_construct (text_entry, label, field_defs);
144
145         return GTK_WIDGET (text_entry);
146 }
147
148 /*============================================================*/
149 /* Construct composite widget.                                */
150 /*============================================================*/
151 static void
152 gl_prop_text_entry_construct (glPropTextEntry * text_entry,
153                               gchar * label,
154                               GList * field_defs)
155 {
156         GtkWidget *wvbox, *wframe, *wtable, *wlabel, *wcombo;
157         GList *keys;
158
159         wvbox = GTK_WIDGET (text_entry);
160
161         wframe = gtk_frame_new (label);
162         gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
163
164         wtable = gtk_table_new (2, 3, FALSE);
165         gtk_container_set_border_width (GTK_CONTAINER (wtable), 10);
166         gtk_table_set_row_spacings (GTK_TABLE (wtable), 5);
167         gtk_table_set_col_spacings (GTK_TABLE (wtable), 5);
168         gtk_container_add (GTK_CONTAINER (wframe), wtable);
169
170         /* Actual text entry widget */
171         text_entry->text_entry = gtk_text_new (NULL, NULL);
172         gtk_text_set_editable (GTK_TEXT (text_entry->text_entry), TRUE);
173         gtk_signal_connect_object (GTK_OBJECT (text_entry->text_entry),
174                                    "changed", GTK_SIGNAL_FUNC (changed_cb),
175                                    GTK_OBJECT (text_entry));
176         gtk_table_attach_defaults (GTK_TABLE (wtable), text_entry->text_entry,
177                                    0, 3, 0, 1);
178
179         /* Insert merge field label */
180         wlabel = gtk_label_new (_("Key:"));
181         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 1, 2);
182
183         /* Key entry widget */
184         wcombo = gtk_combo_new ();
185         keys = gl_merge_get_key_list (field_defs);
186         if (keys != NULL)
187                 gtk_combo_set_popdown_strings (GTK_COMBO (wcombo), keys);
188         gl_merge_free_key_list (&keys);
189         text_entry->key_entry = GTK_COMBO (wcombo)->entry;
190         gtk_entry_set_editable (GTK_ENTRY (text_entry->key_entry), FALSE);
191         gtk_widget_set_usize (wcombo, 200, 0);
192         gtk_table_attach_defaults (GTK_TABLE (wtable), wcombo, 1, 2, 1, 2);
193
194         /* Insert button */
195         text_entry->insert_button =
196             gtk_button_new_with_label (_("Insert merge field"));
197         gtk_signal_connect_object (GTK_OBJECT (text_entry->insert_button),
198                                    "clicked", GTK_SIGNAL_FUNC (insert_cb),
199                                    GTK_OBJECT (text_entry));
200         gtk_table_attach_defaults (GTK_TABLE (wtable),
201                                    text_entry->insert_button, 2, 3, 1, 2);
202
203 }
204
205 /*--------------------------------------------------------------------------*/
206 /* PRIVATE.  Callback for when any control in the widget has changed.       */
207 /*--------------------------------------------------------------------------*/
208 static void
209 changed_cb (glPropTextEntry * text_entry)
210 {
211         /* Emit our "changed" signal */
212         gtk_signal_emit (GTK_OBJECT (text_entry),
213                          prop_text_entry_signals[CHANGED]);
214 }
215
216 /*--------------------------------------------------------------------------*/
217 /* PRIVATE.  Callback for when any control in the widget has changed.       */
218 /*--------------------------------------------------------------------------*/
219 static void
220 insert_cb (glPropTextEntry * text_entry)
221 {
222         gchar *key, *field;
223         gint pos;
224
225         key =
226             gtk_editable_get_chars (GTK_EDITABLE (text_entry->key_entry), 0,
227                                     -1);
228         field = g_strdup_printf ("FIELD{%s}", key);
229
230         pos = gtk_editable_get_position (GTK_EDITABLE (text_entry->text_entry));
231         gtk_editable_insert_text (GTK_EDITABLE (text_entry->text_entry),
232                                   field, strlen (field), &pos);
233
234         g_free (field);
235         g_free (key);
236
237 }
238
239 /*--------------------------------------------------------------------------*/
240 /* Get widget data.                                                         */
241 /*--------------------------------------------------------------------------*/
242 GList *
243 gl_prop_text_entry_get_text (glPropTextEntry * text_entry)
244 {
245         gchar *text;
246         GList *lines;
247
248         text =
249             gtk_editable_get_chars (GTK_EDITABLE (text_entry->text_entry), 0,
250                                     -1);
251
252         lines = gl_text_node_lines_new_from_text (text);
253
254         g_free (text);
255         return lines;
256 }
257
258 /*--------------------------------------------------------------------------*/
259 /* Set widget data.                                                         */
260 /*--------------------------------------------------------------------------*/
261 void
262 gl_prop_text_entry_set_text (glPropTextEntry * text_entry,
263                              gboolean merge_flag,
264                              GList * lines)
265 {
266         gint pos;
267         gchar *text;
268
269         gtk_widget_set_sensitive (text_entry->key_entry, merge_flag);
270         gtk_widget_set_sensitive (text_entry->insert_button, merge_flag);
271
272         gtk_signal_handler_block_by_func (GTK_OBJECT (text_entry->text_entry),
273                                           GTK_SIGNAL_FUNC (changed_cb),
274                                           text_entry);
275         gtk_editable_delete_text (GTK_EDITABLE (text_entry->text_entry), 0, -1);
276         gtk_signal_handler_unblock_by_func (GTK_OBJECT (text_entry->text_entry),
277                                             GTK_SIGNAL_FUNC (changed_cb),
278                                             text_entry);
279
280         text = gl_text_node_lines_expand (lines, NULL);
281
282         pos = 0;
283         gtk_editable_insert_text (GTK_EDITABLE (text_entry->text_entry),
284                                   text, strlen (text), &pos);
285
286 }