2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001 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 gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
270 GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
271 gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
272 GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
273 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
274 GTK_SIGNAL_FUNC(prefs_template_ok_cb), NULL);
275 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
276 GTK_SIGNAL_FUNC(prefs_template_cancel_cb), NULL);
278 address_completion_start(window);
280 templates.window = window;
281 templates.ok_btn = ok_btn;
282 templates.clist_tmpls = clist_tmpls;
283 templates.entry_name = entry_name;
284 templates.entry_subject = entry_subject;
285 templates.entry_to = entry_to;
286 templates.text_value = text_value;
289 static void prefs_template_window_setup(void)
291 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
298 manage_window_set_transient(GTK_WINDOW(templates.window));
299 gtk_widget_grab_focus(templates.ok_btn);
301 gtk_clist_freeze(clist);
302 gtk_clist_clear(clist);
304 title[0] = _("(New)");
305 row = gtk_clist_append(clist, title);
306 gtk_clist_set_row_data(clist, row, NULL);
308 tmpl_list = template_read_config();
310 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
311 tmpl = (Template *)cur->data;
312 title[0] = tmpl->name;
313 row = gtk_clist_append(clist, title);
314 gtk_clist_set_row_data(clist, row, tmpl);
317 g_slist_free(tmpl_list);
319 gtk_clist_thaw(clist);
322 static void prefs_template_clear(void)
327 while ((tmpl = gtk_clist_get_row_data
328 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
333 gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
336 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
339 prefs_template_cancel_cb();
343 static void prefs_template_key_pressed_cb(GtkWidget *widget,
344 GdkEventKey *event, gpointer data)
346 if (event && event->keyval == GDK_Escape)
347 prefs_template_cancel_cb();
350 static void prefs_template_ok_cb(void)
354 tmpl_list = prefs_template_get_list();
355 template_set_config(tmpl_list);
356 compose_reflect_prefs_all();
357 gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
358 gtk_widget_hide(templates.window);
362 static void prefs_template_cancel_cb(void)
364 prefs_template_clear();
365 gtk_widget_hide(templates.window);
369 static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
375 tmpl_def.name = _("Template");
376 tmpl_def.subject = "";
380 if (!(tmpl = gtk_clist_get_row_data(clist, row)))
383 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
384 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
385 tmpl->to ? tmpl->to : "");
386 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
387 tmpl->subject ? tmpl->subject : "");
389 gtk_text_freeze(GTK_TEXT(templates.text_value));
390 gtk_text_set_point(GTK_TEXT(templates.text_value), 0);
391 gtk_text_forward_delete
392 (GTK_TEXT(templates.text_value),
393 gtk_text_get_length(GTK_TEXT(templates.text_value)));
394 gtk_text_insert(GTK_TEXT(templates.text_value), NULL, NULL, NULL,
396 gtk_text_thaw(GTK_TEXT(templates.text_value));
399 static GSList *prefs_template_get_list(void)
402 GSList *tmpl_list = NULL;
405 while ((tmpl = gtk_clist_get_row_data
406 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
407 tmpl_list = g_slist_append(tmpl_list, tmpl);
414 static gint prefs_template_clist_set_row(gint row)
416 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
425 g_return_val_if_fail(row != 0, -1);
427 value = gtk_editable_get_chars(GTK_EDITABLE(templates.text_value),
430 if (value && *value != '\0') {
434 memset(&dummyinfo, 0, sizeof(MsgInfo));
435 quote_fmt_init(&dummyinfo, NULL, NULL);
436 quote_fmt_scan_string(value);
438 parsed_buf = quote_fmt_get_buffer();
440 alertpanel_error(_("Template format error."));
446 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
448 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
450 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
453 if (subject && *subject == '\0') {
457 if (to && *to == '\0') {
462 tmpl = g_new(Template, 1);
464 tmpl->subject = subject;
471 row = gtk_clist_append(clist, title);
473 gtk_clist_set_text(clist, row, 0, name);
474 tmp_tmpl = gtk_clist_get_row_data(clist, row);
476 template_free(tmp_tmpl);
479 gtk_clist_set_row_data(clist, row, tmpl);
483 static void prefs_template_register_cb(void)
485 prefs_template_clist_set_row(-1);
488 static void prefs_template_substitute_cb(void)
490 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
494 if (!clist->selection) return;
496 row = GPOINTER_TO_INT(clist->selection->data);
497 if (row == 0) return;
499 tmpl = gtk_clist_get_row_data(clist, row);
502 prefs_template_clist_set_row(row);
505 static void prefs_template_delete_cb(void)
507 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
511 if (!clist->selection) return;
512 row = GPOINTER_TO_INT(clist->selection->data);
513 if (row == 0) return;
515 if (alertpanel(_("Delete template"),
516 _("Do you really want to delete this template?"),
517 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
520 tmpl = gtk_clist_get_row_data(clist, row);
522 gtk_clist_remove(clist, row);