2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <gdk/gdkkeysyms.h>
28 #include "manage_window.h"
31 #include "alertpanel.h"
34 static struct Templates
{
37 GtkWidget
*clist_tmpls
;
38 GtkWidget
*entry_name
;
39 GtkWidget
*text_value
;
44 /* widget creating functions */
45 static void prefs_templates_create_window(void);
46 static void prefs_templates_setup_window(void);
49 static gint
prefs_templates_deleted_cb(GtkWidget
*widget
,
52 static void prefs_templates_key_pressed_cb(GtkWidget
*widget
,
55 static void prefs_templates_cancel_cb(void);
56 static void prefs_templates_ok_cb(void);
57 static void prefs_templates_select_cb(GtkCList
*clist
,
61 static void prefs_templates_register_cb (void);
62 static void prefs_templates_substitute_cb (void);
63 static void prefs_templates_delete_cb (void);
65 /* Called from mainwindow.c */
66 void prefs_templates_open(void)
68 templates
.tmpl_list
= template_read_config();
70 inc_autocheck_timer_remove();
72 if(!templates
.window
){
73 prefs_templates_create_window();
75 prefs_templates_setup_window();
76 gtk_widget_show(templates
.window
);
79 void prefs_templates_create_window(void)
81 /* window structure ;) */
87 GtkWidget
*entry_name
;
89 GtkWidget
*text_value
;
98 GtkWidget
*clist_tmpls
;
99 GtkWidget
*confirm_area
;
101 GtkWidget
*cancel_btn
;
103 gchar
*title
[] = {_("Registered templates")};
106 window
= gtk_window_new(GTK_WINDOW_DIALOG
);
107 gtk_container_set_border_width(GTK_CONTAINER(window
), 8);
108 gtk_window_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
109 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
110 gtk_window_set_policy(GTK_WINDOW(window
), FALSE
, TRUE
, FALSE
);
111 gtk_window_set_default_size(GTK_WINDOW(window
), 400, 300);
113 /* vpaned to separate template settings from templates list */
114 vpaned
= gtk_vpaned_new();
115 gtk_widget_show(vpaned
);
116 gtk_container_add(GTK_CONTAINER(window
), vpaned
);
118 /* vbox to handle template name and content */
119 vbox1
= gtk_vbox_new(FALSE
, 6);
120 gtk_widget_show(vbox1
);
121 gtk_paned_pack1(GTK_PANED(vpaned
), vbox1
, FALSE
, FALSE
);
123 /* hbox for a label and template name entry */
124 hbox1
= gtk_hbox_new(FALSE
, 12);
125 gtk_widget_show(hbox1
);
126 gtk_box_pack_start(GTK_BOX(vbox1
), hbox1
, FALSE
, FALSE
, 0);
127 gtk_container_set_border_width(GTK_CONTAINER(hbox1
), 2);
129 /* self-documenting */
130 label1
= gtk_label_new(_("Template"));
131 gtk_widget_show(label1
);
132 gtk_box_pack_start(GTK_BOX(hbox1
), label1
, FALSE
, FALSE
, 0);
134 /* holds template name */
135 entry_name
= gtk_entry_new();
136 gtk_widget_show(entry_name
);
137 gtk_box_pack_start(GTK_BOX(hbox1
), entry_name
, TRUE
, TRUE
, 0);
139 /* template content */
140 scroll2
= gtk_scrolled_window_new(NULL
, NULL
);
141 gtk_widget_show(scroll2
);
142 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2
),
145 gtk_box_pack_start(GTK_BOX(vbox1
), scroll2
, TRUE
, TRUE
, 0);
147 text_value
= gtk_text_new(NULL
, NULL
);
148 gtk_widget_show(text_value
);
149 gtk_container_add(GTK_CONTAINER(scroll2
), text_value
);
150 gtk_text_set_editable(GTK_TEXT(text_value
), TRUE
);
151 gtk_text_set_word_wrap(GTK_TEXT(text_value
), TRUE
);
153 /* vbox for buttons and templates list */
154 vbox2
= gtk_vbox_new(FALSE
, 6);
155 gtk_widget_show(vbox2
);
156 gtk_paned_pack2(GTK_PANED(vpaned
), vbox2
, TRUE
, FALSE
);
158 /* register | substitute | delete */
159 hbox2
= gtk_hbox_new(FALSE
, 4);
160 gtk_widget_show(hbox2
);
161 gtk_box_pack_start(GTK_BOX(vbox2
), hbox2
, FALSE
, FALSE
, 0);
163 arrow1
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_OUT
);
164 gtk_widget_show(arrow1
);
165 gtk_box_pack_start(GTK_BOX(hbox2
), arrow1
, FALSE
, FALSE
, 0);
166 gtk_widget_set_usize(arrow1
, -1, 16);
168 hbox3
= gtk_hbox_new(TRUE
, 4);
169 gtk_widget_show(hbox3
);
170 gtk_box_pack_start(GTK_BOX(hbox2
), hbox3
, FALSE
, FALSE
, 0);
172 reg_btn
= gtk_button_new_with_label(_("Register"));
173 gtk_widget_show(reg_btn
);
174 gtk_box_pack_start(GTK_BOX(hbox3
), reg_btn
, FALSE
, TRUE
, 0);
175 gtk_signal_connect (GTK_OBJECT (reg_btn
), "clicked",
176 GTK_SIGNAL_FUNC (prefs_templates_register_cb
), NULL
);
178 subst_btn
= gtk_button_new_with_label(_(" Substitute "));
179 gtk_widget_show(subst_btn
);
180 gtk_box_pack_start(GTK_BOX(hbox3
), subst_btn
, FALSE
, TRUE
, 0);
181 gtk_signal_connect (GTK_OBJECT(subst_btn
), "clicked",
182 GTK_SIGNAL_FUNC(prefs_templates_substitute_cb
),
185 del_btn
= gtk_button_new_with_label(_("Delete"));
186 gtk_widget_show(del_btn
);
187 gtk_box_pack_start(GTK_BOX(hbox3
), del_btn
, FALSE
, TRUE
, 0);
188 gtk_signal_connect (GTK_OBJECT(del_btn
), "clicked",
189 GTK_SIGNAL_FUNC(prefs_templates_delete_cb
), NULL
);
192 scroll1
= gtk_scrolled_window_new(NULL
, NULL
);
193 gtk_widget_show(scroll1
);
194 gtk_widget_set_usize(scroll1
, -1, 150);
195 gtk_box_pack_start(GTK_BOX(vbox2
), scroll1
, TRUE
, TRUE
, 0);
196 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1
),
197 GTK_POLICY_AUTOMATIC
,
198 GTK_POLICY_AUTOMATIC
);
200 clist_tmpls
= gtk_clist_new_with_titles(1, title
);
201 gtk_widget_show(clist_tmpls
);
202 gtk_container_add(GTK_CONTAINER(scroll1
), clist_tmpls
);
203 gtk_clist_set_column_width(GTK_CLIST(clist_tmpls
), 0, 80);
204 gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls
), GTK_SELECTION_BROWSE
);
205 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls
)->column
[0].button
,
207 gtk_signal_connect (GTK_OBJECT (clist_tmpls
), "select_row",
208 GTK_SIGNAL_FUNC (prefs_templates_select_cb
), NULL
);
211 gtkut_button_set_create(&confirm_area
, &ok_btn
, _("OK"),
212 &cancel_btn
, _("Cancel"), NULL
, NULL
);
213 gtk_widget_show(confirm_area
);
214 gtk_box_pack_end(GTK_BOX(vbox2
), confirm_area
, FALSE
, FALSE
, 0);
215 gtk_widget_grab_default(ok_btn
);
217 gtk_window_set_title(GTK_WINDOW(window
), _("Templates"));
219 gtk_signal_connect(GTK_OBJECT(window
), "delete_event",
220 GTK_SIGNAL_FUNC(prefs_templates_deleted_cb
), NULL
);
221 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
222 GTK_SIGNAL_FUNC(prefs_templates_key_pressed_cb
), NULL
);
223 gtk_signal_connect(GTK_OBJECT(window
), "focus_in_event",
224 GTK_SIGNAL_FUNC(manage_window_focus_in
), NULL
);
225 gtk_signal_connect(GTK_OBJECT(window
), "focus_out_event",
226 GTK_SIGNAL_FUNC(manage_window_focus_out
), NULL
);
227 gtk_signal_connect(GTK_OBJECT(ok_btn
), "clicked",
228 GTK_SIGNAL_FUNC(prefs_templates_ok_cb
), NULL
);
229 gtk_signal_connect(GTK_OBJECT(cancel_btn
), "clicked",
230 GTK_SIGNAL_FUNC(prefs_templates_cancel_cb
), NULL
);
232 templates
.window
= window
;
233 templates
.ok_btn
= ok_btn
;
234 templates
.clist_tmpls
= clist_tmpls
;
235 templates
.entry_name
= entry_name
;
236 templates
.text_value
= text_value
;
239 void prefs_templates_setup_window(void)
241 GtkCList
*clist
= GTK_CLIST(templates
.clist_tmpls
);
247 manage_window_set_transient(GTK_WINDOW(templates
.window
));
248 gtk_widget_grab_focus(templates
.ok_btn
);
250 gtk_clist_freeze(clist
);
251 gtk_clist_clear(clist
);
253 cond_str
[0] = _("(New)");
254 row
= gtk_clist_append(clist
, cond_str
);
255 gtk_clist_set_row_data(clist
, row
, NULL
);
257 for (cur
= templates
.tmpl_list
; cur
!= NULL
; cur
= cur
->next
) {
259 cond_str
[0] = tmpl
->name
;
260 row
= gtk_clist_append(clist
, cond_str
);
261 gtk_clist_set_row_data(clist
, row
, tmpl
);
264 gtk_clist_thaw(clist
);
267 static gint
prefs_templates_deleted_cb(GtkWidget
*widget
, GdkEventAny
*event
,
270 prefs_templates_cancel_cb();
274 static void prefs_templates_key_pressed_cb(GtkWidget
*widget
, GdkEventKey
*event
,
277 if (event
&& event
->keyval
== GDK_Escape
)
278 prefs_templates_cancel_cb();
281 static void prefs_templates_ok_cb(void)
283 template_write_config(templates
.tmpl_list
);
284 template_clear_config(templates
.tmpl_list
);
285 gtk_widget_hide(templates
.window
);
286 inc_autocheck_timer_set();
289 static void prefs_templates_cancel_cb(void)
291 template_clear_config(templates
.tmpl_list
);
292 gtk_widget_hide(templates
.window
);
293 inc_autocheck_timer_set();
296 static void prefs_templates_select_cb(GtkCList
*clist
, gint row
, gint column
,
300 Template tmpl_def
= {_("Template name"), _("(empty)")};
302 if (!(tmpl
= gtk_clist_get_row_data(clist
, row
)))
305 gtk_entry_set_text(GTK_ENTRY(templates
.entry_name
), tmpl
->name
);
307 gtk_text_freeze(GTK_TEXT(templates
.text_value
));
308 gtk_text_set_point(GTK_TEXT(templates
.text_value
), 0);
309 gtk_text_forward_delete(GTK_TEXT(templates
.text_value
),
310 gtk_text_get_length(GTK_TEXT(templates
.text_value
)));
311 gtk_text_insert(GTK_TEXT(templates
.text_value
), NULL
, NULL
, NULL
,
313 gtk_text_thaw(GTK_TEXT(templates
.text_value
));
316 static void prefs_templates_create_list(void)
321 g_slist_free(templates
.tmpl_list
);
322 templates
.tmpl_list
= NULL
;
324 while ((tmpl
= gtk_clist_get_row_data(GTK_CLIST(templates
.clist_tmpls
),row
)) != NULL
) {
325 templates
.tmpl_list
= g_slist_append(templates
.tmpl_list
, tmpl
);
330 static gint
prefs_templates_clist_set_row(gint row
)
332 GtkCList
*clist
= GTK_CLIST(templates
.clist_tmpls
);
339 g_return_val_if_fail(row
!= 0, -1);
341 name
= gtk_editable_get_chars(GTK_EDITABLE(templates
.entry_name
),
343 value
= gtk_editable_get_chars(GTK_EDITABLE(templates
.text_value
),
347 tmpl
= g_new(Template
, 1);
354 row
= gtk_clist_append(clist
, tmpl_str
);
356 gtk_clist_set_text(clist
, row
, 0, name
);
357 tmp_tmpl
= gtk_clist_get_row_data(clist
, row
);
359 template_free(tmp_tmpl
);
362 gtk_clist_set_row_data(clist
, row
, tmpl
);
363 prefs_templates_create_list();
367 static void prefs_templates_register_cb(void)
369 prefs_templates_clist_set_row(-1);
372 static void prefs_templates_substitute_cb(void)
374 GtkCList
*clist
= GTK_CLIST(templates
.clist_tmpls
);
378 if (!clist
->selection
) return;
380 row
= GPOINTER_TO_INT(clist
->selection
->data
);
381 if (row
== 0) return;
383 tmpl
= gtk_clist_get_row_data(clist
, row
);
386 prefs_templates_clist_set_row(row
);
389 static void prefs_templates_delete_cb(void)
391 GtkCList
*clist
= GTK_CLIST(templates
.clist_tmpls
);
395 if (!clist
->selection
) return;
396 row
= GPOINTER_TO_INT(clist
->selection
->data
);
397 if (row
== 0) return;
399 if (alertpanel(_("Delete template"),
400 _("Do you really want to delete this template?"),
401 _("Yes"), _("No"), NULL
) == G_ALERTALTERNATE
)
404 tmpl
= gtk_clist_get_row_data(clist
, row
);
406 gtk_clist_remove(clist
, row
);
407 templates
.tmpl_list
= g_slist_remove(templates
.tmpl_list
, tmpl
);