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"
38 #include "prefs_common.h"
40 #include "addr_compl.h"
41 #include "quote_fmt.h"
43 static struct Templates {
46 GtkWidget *clist_tmpls;
47 GtkWidget *entry_name;
48 GtkWidget *entry_subject;
50 GtkWidget *text_value;
53 /* widget creating functions */
54 static void prefs_template_window_create (void);
55 static void prefs_template_window_setup (void);
56 static void prefs_template_clear (void);
58 static GSList *prefs_template_get_list (void);
61 static gint prefs_template_deleted_cb (GtkWidget *widget,
64 static void prefs_template_key_pressed_cb (GtkWidget *widget,
67 static void prefs_template_cancel_cb (void);
68 static void prefs_template_ok_cb (void);
69 static void prefs_template_select_cb (GtkCList *clist,
73 static void prefs_template_register_cb (void);
74 static void prefs_template_substitute_cb (void);
75 static void prefs_template_delete_cb (void);
77 /* Called from mainwindow.c */
78 void prefs_template_open(void)
82 if (!templates.window)
83 prefs_template_window_create();
85 prefs_template_window_setup();
86 gtk_widget_show(templates.window);
89 #define ADD_ENTRY(entry, str, row) \
91 label1 = gtk_label_new(str); \
92 gtk_widget_show(label1); \
93 gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
95 gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
97 entry = gtk_entry_new(); \
98 gtk_widget_show(entry); \
99 gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
100 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
103 static void prefs_template_window_create(void)
105 /* window structure ;) */
111 GtkWidget *entry_name;
114 GtkWidget *entry_subject;
116 GtkWidget *text_value;
122 GtkWidget *subst_btn;
126 GtkWidget *clist_tmpls;
127 GtkWidget *confirm_area;
129 GtkWidget *cancel_btn;
134 window = gtk_window_new(GTK_WINDOW_DIALOG);
135 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
136 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
137 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
138 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
140 /* vpaned to separate template settings from templates list */
141 vpaned = gtk_vpaned_new();
142 gtk_widget_show(vpaned);
143 gtk_container_add(GTK_CONTAINER(window), vpaned);
145 /* vbox to handle template name and content */
146 vbox1 = gtk_vbox_new(FALSE, 6);
147 gtk_widget_show(vbox1);
148 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
149 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
151 hbox1 = gtk_hbox_new(FALSE, 8);
152 gtk_widget_show(hbox1);
153 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
155 label1 = gtk_label_new(_("Template name"));
156 gtk_widget_show(label1);
157 gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
159 entry_name = gtk_entry_new();
160 gtk_widget_show(entry_name);
161 gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
163 /* table for headers */
164 table = gtk_table_new(2, 2, FALSE);
165 gtk_widget_show(table);
166 gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
167 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
168 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
170 ADD_ENTRY(entry_to, _("To:"), 0);
171 address_completion_register_entry(GTK_ENTRY(entry_to));
172 ADD_ENTRY(entry_subject, _("Subject:"), 1);
176 /* template content */
177 scroll2 = gtk_scrolled_window_new(NULL, NULL);
178 gtk_widget_show(scroll2);
179 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
182 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
184 text_value = gtk_text_new(NULL, NULL);
185 gtk_widget_show(text_value);
186 gtk_widget_set_usize(text_value, -1, 120);
187 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
188 gtk_text_set_editable(GTK_TEXT(text_value), TRUE);
189 gtk_text_set_word_wrap(GTK_TEXT(text_value), TRUE);
191 /* vbox for buttons and templates list */
192 vbox2 = gtk_vbox_new(FALSE, 6);
193 gtk_widget_show(vbox2);
194 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
195 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
197 /* register | substitute | delete */
198 hbox2 = gtk_hbox_new(FALSE, 4);
199 gtk_widget_show(hbox2);
200 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
202 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
203 gtk_widget_show(arrow1);
204 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
205 gtk_widget_set_usize(arrow1, -1, 16);
207 hbox3 = gtk_hbox_new(TRUE, 4);
208 gtk_widget_show(hbox3);
209 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
211 reg_btn = gtk_button_new_with_label(_("Register"));
212 gtk_widget_show(reg_btn);
213 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
214 gtk_signal_connect(GTK_OBJECT (reg_btn), "clicked",
215 GTK_SIGNAL_FUNC (prefs_template_register_cb), NULL);
217 subst_btn = gtk_button_new_with_label(_(" Substitute "));
218 gtk_widget_show(subst_btn);
219 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
220 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
221 GTK_SIGNAL_FUNC(prefs_template_substitute_cb),
224 del_btn = gtk_button_new_with_label(_("Delete"));
225 gtk_widget_show(del_btn);
226 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
227 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
228 GTK_SIGNAL_FUNC(prefs_template_delete_cb), NULL);
230 desc_btn = gtk_button_new_with_label(_(" Symbols "));
231 gtk_widget_show(desc_btn);
232 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
233 gtk_signal_connect(GTK_OBJECT(desc_btn), "clicked",
234 GTK_SIGNAL_FUNC(prefs_quote_description), NULL);
237 scroll1 = gtk_scrolled_window_new(NULL, NULL);
238 gtk_widget_show(scroll1);
239 gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
240 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
241 GTK_POLICY_AUTOMATIC,
242 GTK_POLICY_AUTOMATIC);
244 title[0] = _("Registered templates");
245 clist_tmpls = gtk_clist_new_with_titles(1, title);
246 gtk_widget_show(clist_tmpls);
247 gtk_widget_set_usize(scroll1, -1, 140);
248 gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
249 gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
250 gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls),
251 GTK_SELECTION_BROWSE);
252 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
254 gtk_signal_connect(GTK_OBJECT (clist_tmpls), "select_row",
255 GTK_SIGNAL_FUNC (prefs_template_select_cb), NULL);
258 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
259 &cancel_btn, _("Cancel"), NULL, NULL);
260 gtk_widget_show(confirm_area);
261 gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
262 gtk_widget_grab_default(ok_btn);
264 gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
266 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
267 GTK_SIGNAL_FUNC(prefs_template_deleted_cb), NULL);
268 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
269 GTK_SIGNAL_FUNC(prefs_template_key_pressed_cb), NULL);
270 gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
271 GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
272 gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
273 GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
274 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
275 GTK_SIGNAL_FUNC(prefs_template_ok_cb), NULL);
276 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
277 GTK_SIGNAL_FUNC(prefs_template_cancel_cb), NULL);
279 address_completion_start(window);
281 templates.window = window;
282 templates.ok_btn = ok_btn;
283 templates.clist_tmpls = clist_tmpls;
284 templates.entry_name = entry_name;
285 templates.entry_subject = entry_subject;
286 templates.entry_to = entry_to;
287 templates.text_value = text_value;
290 static void prefs_template_window_setup(void)
292 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
299 manage_window_set_transient(GTK_WINDOW(templates.window));
300 gtk_widget_grab_focus(templates.ok_btn);
302 gtk_clist_freeze(clist);
303 gtk_clist_clear(clist);
305 title[0] = _("(New)");
306 row = gtk_clist_append(clist, title);
307 gtk_clist_set_row_data(clist, row, NULL);
309 tmpl_list = template_read_config();
311 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
312 tmpl = (Template *)cur->data;
313 title[0] = tmpl->name;
314 row = gtk_clist_append(clist, title);
315 gtk_clist_set_row_data(clist, row, tmpl);
318 g_slist_free(tmpl_list);
320 gtk_clist_thaw(clist);
323 static void prefs_template_clear(void)
328 while ((tmpl = gtk_clist_get_row_data
329 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
334 gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
337 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
340 prefs_template_cancel_cb();
344 static void prefs_template_key_pressed_cb(GtkWidget *widget,
345 GdkEventKey *event, gpointer data)
347 if (event && event->keyval == GDK_Escape)
348 prefs_template_cancel_cb();
351 static void prefs_template_ok_cb(void)
355 tmpl_list = prefs_template_get_list();
356 template_set_config(tmpl_list);
357 compose_reflect_prefs_all();
358 gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
359 gtk_widget_hide(templates.window);
363 static void prefs_template_cancel_cb(void)
365 prefs_template_clear();
366 gtk_widget_hide(templates.window);
370 static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
376 tmpl_def.name = _("Template");
377 tmpl_def.subject = "";
381 if (!(tmpl = gtk_clist_get_row_data(clist, row)))
384 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
385 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
386 tmpl->to ? tmpl->to : "");
387 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
388 tmpl->subject ? tmpl->subject : "");
390 gtk_text_freeze(GTK_TEXT(templates.text_value));
391 gtk_text_set_point(GTK_TEXT(templates.text_value), 0);
392 gtk_text_forward_delete
393 (GTK_TEXT(templates.text_value),
394 gtk_text_get_length(GTK_TEXT(templates.text_value)));
395 gtk_text_insert(GTK_TEXT(templates.text_value), NULL, NULL, NULL,
397 gtk_text_thaw(GTK_TEXT(templates.text_value));
400 static GSList *prefs_template_get_list(void)
403 GSList *tmpl_list = NULL;
406 while ((tmpl = gtk_clist_get_row_data
407 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
408 tmpl_list = g_slist_append(tmpl_list, tmpl);
415 static gint prefs_template_clist_set_row(gint row)
417 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
426 g_return_val_if_fail(row != 0, -1);
428 value = gtk_editable_get_chars(GTK_EDITABLE(templates.text_value),
431 if (value && *value != '\0') {
435 memset(&dummyinfo, 0, sizeof(MsgInfo));
436 quote_fmt_init(&dummyinfo, NULL);
437 quote_fmt_scan_string(value);
439 parsed_buf = quote_fmt_get_buffer();
441 alertpanel_error(_("Template format error."));
447 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
449 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
451 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
454 if (subject && *subject == '\0') {
458 if (to && *to == '\0') {
463 tmpl = g_new(Template, 1);
465 tmpl->subject = subject;
472 row = gtk_clist_append(clist, title);
474 gtk_clist_set_text(clist, row, 0, name);
475 tmp_tmpl = gtk_clist_get_row_data(clist, row);
477 template_free(tmp_tmpl);
480 gtk_clist_set_row_data(clist, row, tmpl);
484 static void prefs_template_register_cb(void)
486 prefs_template_clist_set_row(-1);
489 static void prefs_template_substitute_cb(void)
491 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
495 if (!clist->selection) return;
497 row = GPOINTER_TO_INT(clist->selection->data);
498 if (row == 0) return;
500 tmpl = gtk_clist_get_row_data(clist, row);
503 prefs_template_clist_set_row(row);
506 static void prefs_template_delete_cb(void)
508 GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
512 if (!clist->selection) return;
513 row = GPOINTER_TO_INT(clist->selection->data);
514 if (row == 0) return;
516 if (alertpanel(_("Delete template"),
517 _("Do you really want to delete this template?"),
518 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
521 tmpl = gtk_clist_get_row_data(clist, row);
523 gtk_clist_remove(clist, row);