2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2002 Hiroyuki Yamamoto
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <gdk/gdkkeysyms.h>
36 #include "alertpanel.h"
37 #include "manage_window.h"
39 #include "addr_compl.h"
40 #include "quote_fmt.h"
42 static struct Templates {
45 GtkWidget *clist_tmpls;
46 GtkWidget *entry_name;
47 GtkWidget *entry_subject;
49 GtkWidget *text_value;
52 /* widget creating functions */
53 static void prefs_template_window_create (void);
54 static void prefs_template_window_setup (void);
55 static void prefs_template_clear (void);
57 static GSList *prefs_template_get_list (void);
60 static gint prefs_template_deleted_cb (GtkWidget *widget,
63 static void prefs_template_key_pressed_cb (GtkWidget *widget,
66 static void prefs_template_cancel_cb (void);
67 static void prefs_template_ok_cb (void);
68 static void prefs_template_select_cb (GtkCList *clist,
72 static void prefs_template_register_cb (void);
73 static void prefs_template_substitute_cb (void);
74 static void prefs_template_delete_cb (void);
76 /* Called from mainwindow.c */
77 void prefs_template_open(void)
81 if (!templates.window)
82 prefs_template_window_create();
84 prefs_template_window_setup();
85 gtk_widget_show(templates.window);
88 #define ADD_ENTRY(entry, str, row) \
90 label1 = gtk_label_new(str); \
91 gtk_widget_show(label1); \
92 gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
94 gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
96 entry = gtk_entry_new(); \
97 gtk_widget_show(entry); \
98 gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
99 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
102 static void prefs_template_window_create(void)
104 /* window structure ;) */
110 GtkWidget *entry_name;
113 GtkWidget *entry_subject;
115 GtkWidget *text_value;
121 GtkWidget *subst_btn;
125 GtkWidget *clist_tmpls;
126 GtkWidget *confirm_area;
128 GtkWidget *cancel_btn;
133 window = gtk_window_new(GTK_WINDOW_DIALOG);
134 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
135 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
136 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
137 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
139 /* vpaned to separate template settings from templates list */
140 vpaned = gtk_vpaned_new();
141 gtk_widget_show(vpaned);
142 gtk_container_add(GTK_CONTAINER(window), vpaned);
144 /* vbox to handle template name and content */
145 vbox1 = gtk_vbox_new(FALSE, 6);
146 gtk_widget_show(vbox1);
147 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
148 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
150 hbox1 = gtk_hbox_new(FALSE, 8);
151 gtk_widget_show(hbox1);
152 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
154 label1 = gtk_label_new(_("Template name"));
155 gtk_widget_show(label1);
156 gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
158 entry_name = gtk_entry_new();
159 gtk_widget_show(entry_name);
160 gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
162 /* table for headers */
163 table = gtk_table_new(2, 2, FALSE);
164 gtk_widget_show(table);
165 gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
166 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
167 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
169 ADD_ENTRY(entry_to, _("To:"), 0);
170 address_completion_register_entry(GTK_ENTRY(entry_to));
171 ADD_ENTRY(entry_subject, _("Subject:"), 1);
175 /* template content */
176 scroll2 = gtk_scrolled_window_new(NULL, NULL);
177 gtk_widget_show(scroll2);
178 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
181 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
183 text_value = gtk_text_new(NULL, NULL);
184 gtk_widget_show(text_value);
185 gtk_widget_set_usize(text_value, -1, 120);
186 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
187 gtk_text_set_editable(GTK_TEXT(text_value), TRUE);
188 gtk_text_set_word_wrap(GTK_TEXT(text_value), TRUE);
190 /* vbox for buttons and templates list */
191 vbox2 = gtk_vbox_new(FALSE, 6);
192 gtk_widget_show(vbox2);
193 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
194 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
196 /* register | substitute | delete */
197 hbox2 = gtk_hbox_new(FALSE, 4);
198 gtk_widget_show(hbox2);
199 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
201 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
202 gtk_widget_show(arrow1);
203 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
204 gtk_widget_set_usize(arrow1, -1, 16);
206 hbox3 = gtk_hbox_new(TRUE, 4);
207 gtk_widget_show(hbox3);
208 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
210 reg_btn = gtk_button_new_with_label(_("Register"));
211 gtk_widget_show(reg_btn);
212 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
213 gtk_signal_connect(GTK_OBJECT (reg_btn), "clicked",
214 GTK_SIGNAL_FUNC (prefs_template_register_cb), NULL);
216 subst_btn = gtk_button_new_with_label(_(" Substitute "));
217 gtk_widget_show(subst_btn);
218 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
219 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
220 GTK_SIGNAL_FUNC(prefs_template_substitute_cb),
223 del_btn = gtk_button_new_with_label(_("Delete"));
224 gtk_widget_show(del_btn);
225 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
226 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
227 GTK_SIGNAL_FUNC(prefs_template_delete_cb), NULL);
229 desc_btn = gtk_button_new_with_label(_(" Symbols "));
230 gtk_widget_show(desc_btn);
231 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
232 gtk_signal_connect(GTK_OBJECT(desc_btn), "clicked",
233 GTK_SIGNAL_FUNC(quote_fmt_quote_description), NULL);
236 scroll1 = gtk_scrolled_window_new(NULL, NULL);
237 gtk_widget_show(scroll1);
238 gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
239 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
240 GTK_POLICY_AUTOMATIC,
241 GTK_POLICY_AUTOMATIC);
243 title[0] = _("Registered templates");
244 clist_tmpls = gtk_clist_new_with_titles(1, title);
245 gtk_widget_show(clist_tmpls);
246 gtk_widget_set_usize(scroll1, -1, 140);
247 gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
248 gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
249 gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls),
250 GTK_SELECTION_BROWSE);
251 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
253 gtk_signal_connect(GTK_OBJECT (clist_tmpls), "select_row",
254 GTK_SIGNAL_FUNC (prefs_template_select_cb), NULL);
257 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
258 &cancel_btn, _("Cancel"), NULL, NULL);
259 gtk_widget_show(confirm_area);
260 gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
261 gtk_widget_grab_default(ok_btn);
263 gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
265 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
266 GTK_SIGNAL_FUNC(prefs_template_deleted_cb), NULL);
267 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
268 GTK_SIGNAL_FUNC(prefs_template_key_pressed_cb), NULL);
269 MANAGE_WINDOW_SIGNALS_CONNECT(window);
270 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
271 GTK_SIGNAL_FUNC(prefs_template_ok_cb), NULL);
272 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
273 GTK_SIGNAL_FUNC(prefs_template_cancel_cb), NULL);
275 address_completion_start(window);
277 templates.window = window;
278 templates.ok_btn = ok_btn;
279 templates.clist_tmpls = clist_tmpls;
280 templates.entry_name = entry_name;
281 templates.entry_subject = entry_subject;
282 templates.entry_to = entry_to;
283 templates.text_value = text_value;
286 static void prefs_template_window_setup(void)
288 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
295 manage_window_set_transient(GTK_WINDOW(templates.window));
296 gtk_widget_grab_focus(templates.ok_btn);
298 gtk_clist_freeze(clist);
299 gtk_clist_clear(clist);
301 title[0] = _("(New)");
302 row = gtk_clist_append(clist, title);
303 gtk_clist_set_row_data(clist, row, NULL);
305 tmpl_list = template_read_config();
307 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
308 tmpl = (Template *)cur->data;
309 title[0] = tmpl->name;
310 row = gtk_clist_append(clist, title);
311 gtk_clist_set_row_data(clist, row, tmpl);
314 g_slist_free(tmpl_list);
316 gtk_clist_thaw(clist);
319 static void prefs_template_clear(void)
324 while ((tmpl = gtk_clist_get_row_data
325 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
330 gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
333 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
336 prefs_template_cancel_cb();
340 static void prefs_template_key_pressed_cb(GtkWidget *widget,
341 GdkEventKey *event, gpointer data)
343 if (event && event->keyval == GDK_Escape)
344 prefs_template_cancel_cb();
347 static void prefs_template_ok_cb(void)
351 tmpl_list = prefs_template_get_list();
352 template_set_config(tmpl_list);
353 compose_reflect_prefs_all();
354 gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
355 gtk_widget_hide(templates.window);
359 static void prefs_template_cancel_cb(void)
361 prefs_template_clear();
362 gtk_widget_hide(templates.window);
366 static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
372 tmpl_def.name = _("Template");
373 tmpl_def.subject = "";
377 if (!(tmpl = gtk_clist_get_row_data(clist, row)))
380 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
381 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
382 tmpl->to ? tmpl->to : "");
383 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
384 tmpl->subject ? tmpl->subject : "");
386 gtk_text_freeze(GTK_TEXT(templates.text_value));
387 gtk_text_set_point(GTK_TEXT(templates.text_value), 0);
388 gtk_text_forward_delete
389 (GTK_TEXT(templates.text_value),
390 gtk_text_get_length(GTK_TEXT(templates.text_value)));
391 gtk_text_insert(GTK_TEXT(templates.text_value), NULL, NULL, NULL,
393 gtk_text_thaw(GTK_TEXT(templates.text_value));
396 static GSList *prefs_template_get_list(void)
399 GSList *tmpl_list = NULL;
402 while ((tmpl = gtk_clist_get_row_data
403 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
404 tmpl_list = g_slist_append(tmpl_list, tmpl);
411 static gint prefs_template_clist_set_row(gint row)
413 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
422 g_return_val_if_fail(row != 0, -1);
424 value = gtk_editable_get_chars(GTK_EDITABLE(templates.text_value),
427 if (value && *value != '\0') {
431 memset(&dummyinfo, 0, sizeof(MsgInfo));
432 quote_fmt_init(&dummyinfo, NULL, NULL);
433 quote_fmt_scan_string(value);
435 parsed_buf = quote_fmt_get_buffer();
437 alertpanel_error(_("Template format error."));
443 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
445 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
447 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
450 if (subject && *subject == '\0') {
454 if (to && *to == '\0') {
459 tmpl = g_new(Template, 1);
461 tmpl->subject = subject;
468 row = gtk_clist_append(clist, title);
470 gtk_clist_set_text(clist, row, 0, name);
471 tmp_tmpl = gtk_clist_get_row_data(clist, row);
473 template_free(tmp_tmpl);
476 gtk_clist_set_row_data(clist, row, tmpl);
480 static void prefs_template_register_cb(void)
482 prefs_template_clist_set_row(-1);
485 static void prefs_template_substitute_cb(void)
487 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
491 if (!clist->selection) return;
493 row = GPOINTER_TO_INT(clist->selection->data);
494 if (row == 0) return;
496 tmpl = gtk_clist_get_row_data(clist, row);
499 prefs_template_clist_set_row(row);
502 static void prefs_template_delete_cb(void)
504 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
508 if (!clist->selection) return;
509 row = GPOINTER_TO_INT(clist->selection->data);
510 if (row == 0) return;
512 if (alertpanel(_("Delete template"),
513 _("Do you really want to delete this template?"),
514 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
517 tmpl = gtk_clist_get_row_data(clist, row);
519 gtk_clist_remove(clist, row);