2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * prop_text_entry.c: text entry widget module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
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.
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.
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
25 #include "prop_text_entry.h"
27 #include "text_node.h"
31 /*===========================================*/
33 /*===========================================*/
40 typedef void (*glPropTextEntrySignal) (GtkObject * object, gpointer data);
42 /*===========================================*/
44 /*===========================================*/
46 static GtkContainerClass *parent_class;
48 static gint prop_text_entry_signals[LAST_SIGNAL] = { 0 };
50 /*===========================================*/
51 /* Local function prototypes */
52 /*===========================================*/
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);
60 static void changed_cb (glPropTextEntry * text_entry);
61 static void insert_cb (glPropTextEntry * text_entry);
63 /*================================================================*/
64 /* Boilerplate Object stuff. */
65 /*================================================================*/
67 gl_prop_text_entry_get_type (void)
69 static guint prop_text_entry_type = 0;
71 if (!prop_text_entry_type) {
72 GtkTypeInfo prop_text_entry_info = {
74 sizeof (glPropTextEntry),
75 sizeof (glPropTextEntryClass),
76 (GtkClassInitFunc) gl_prop_text_entry_class_init,
77 (GtkObjectInitFunc) gl_prop_text_entry_init,
82 prop_text_entry_type = gtk_type_unique (gtk_vbox_get_type (),
83 &prop_text_entry_info);
86 return prop_text_entry_type;
90 gl_prop_text_entry_class_init (glPropTextEntryClass * class)
92 GtkObjectClass *object_class;
93 GtkWidgetClass *widget_class;
95 object_class = (GtkObjectClass *) class;
96 widget_class = (GtkWidgetClass *) class;
98 parent_class = gtk_type_class (gtk_vbox_get_type ());
100 object_class->destroy = gl_prop_text_entry_destroy;
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);
109 class->changed = NULL;
113 gl_prop_text_entry_init (glPropTextEntry * text_entry)
115 text_entry->text_entry = NULL;
116 text_entry->key_entry = NULL;
117 text_entry->insert_button = NULL;
121 gl_prop_text_entry_destroy (GtkObject * object)
123 glPropTextEntry *text_entry;
124 glPropTextEntryClass *class;
126 g_return_if_fail (object != NULL);
127 g_return_if_fail (GL_IS_PROP_TEXT_ENTRY (object));
129 text_entry = GL_PROP_TEXT_ENTRY (object);
130 class = GL_PROP_TEXT_ENTRY_CLASS (GTK_OBJECT (text_entry)->klass);
132 GTK_OBJECT_CLASS (parent_class)->destroy (object);
136 gl_prop_text_entry_new (gchar * label,
139 glPropTextEntry *text_entry;
141 text_entry = gtk_type_new (gl_prop_text_entry_get_type ());
143 gl_prop_text_entry_construct (text_entry, label, field_defs);
145 return GTK_WIDGET (text_entry);
148 /*============================================================*/
149 /* Construct composite widget. */
150 /*============================================================*/
152 gl_prop_text_entry_construct (glPropTextEntry * text_entry,
156 GtkWidget *wvbox, *wframe, *wtable, *wlabel, *wcombo;
159 wvbox = GTK_WIDGET (text_entry);
161 wframe = gtk_frame_new (label);
162 gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
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);
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,
179 /* Insert merge field label */
180 wlabel = gtk_label_new (_("Key:"));
181 gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 1, 2);
183 /* Key entry widget */
184 wcombo = gtk_combo_new ();
185 keys = gl_merge_get_key_list (field_defs);
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);
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);
205 /*--------------------------------------------------------------------------*/
206 /* PRIVATE. Callback for when any control in the widget has changed. */
207 /*--------------------------------------------------------------------------*/
209 changed_cb (glPropTextEntry * text_entry)
211 /* Emit our "changed" signal */
212 gtk_signal_emit (GTK_OBJECT (text_entry),
213 prop_text_entry_signals[CHANGED]);
216 /*--------------------------------------------------------------------------*/
217 /* PRIVATE. Callback for when any control in the widget has changed. */
218 /*--------------------------------------------------------------------------*/
220 insert_cb (glPropTextEntry * text_entry)
226 gtk_editable_get_chars (GTK_EDITABLE (text_entry->key_entry), 0,
228 field = g_strdup_printf ("FIELD{%s}", key);
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);
239 /*--------------------------------------------------------------------------*/
240 /* Get widget data. */
241 /*--------------------------------------------------------------------------*/
243 gl_prop_text_entry_get_text (glPropTextEntry * text_entry)
249 gtk_editable_get_chars (GTK_EDITABLE (text_entry->text_entry), 0,
252 lines = gl_text_node_lines_new_from_text (text);
258 /*--------------------------------------------------------------------------*/
259 /* Set widget data. */
260 /*--------------------------------------------------------------------------*/
262 gl_prop_text_entry_set_text (glPropTextEntry * text_entry,
269 gtk_widget_set_sensitive (text_entry->key_entry, merge_flag);
270 gtk_widget_set_sensitive (text_entry->insert_button, merge_flag);
272 gtk_signal_handler_block_by_func (GTK_OBJECT (text_entry->text_entry),
273 GTK_SIGNAL_FUNC (changed_cb),
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),
280 text = gl_text_node_lines_expand (lines, NULL);
283 gtk_editable_insert_text (GTK_EDITABLE (text_entry->text_entry),
284 text, strlen (text), &pos);