28a356f17258a4f984474c0b238a8c560e36dcaf
[claws.git] / src / prefs_toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * General functions for accessing address book files.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <gtk/gtk.h>
32 #include <gtk/gtkoptionmenu.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "intl.h"
38 #include "stock_pixmap.h"
39 #include "manage_window.h"
40 #include "gtkutils.h"
41 #include "mainwindow.h"
42 #include "alertpanel.h"
43 #include "prefs_common.h"
44
45 #include "utils.h"
46
47 #include "toolbar.h"
48 #include "prefs_toolbar.h"
49 #include "prefswindow.h"
50 #include "prefs_gtk.h"
51
52 typedef enum
53 {
54         COL_ICON        = 0,
55         COL_FILENAME    = 1,
56         COL_TEXT        = 2,
57         COL_EVENT       = 3
58 } DisplayedItemsColumnPos;
59
60 # define N_DISPLAYED_ITEMS_COLS 4
61
62 typedef struct _ToolbarPage
63 {
64         PrefsPage  page;
65
66         GtkWidget *window;              /* do not modify */
67
68         GtkWidget *clist_icons;
69         GtkWidget *clist_set;
70         GtkWidget *combo_action;
71         GtkWidget *combo_entry;
72         GtkWidget *combo_list;
73         GtkWidget *label_icon_text;
74         GtkWidget *label_action_sel;
75         GtkWidget *entry_icon_text;
76         GtkWidget *combo_syl_action;
77         GtkWidget *combo_syl_list;
78         GtkWidget *combo_syl_entry;
79
80         ToolbarType source;
81         GList      *combo_action_list;
82
83 } ToolbarPage;
84
85 #define CELL_SPACING 24
86 #define ERROR_MSG _("Selected Action already set.\nPlease choose another Action from List")
87
88 static void prefs_toolbar_populate               (ToolbarPage *prefs_toolbar);
89 static gboolean is_duplicate                     (ToolbarPage *prefs_toolbar,
90                                                   gchar            *chosen_action);
91 static void prefs_toolbar_save                   (PrefsPage        *_page);
92
93 static void prefs_toolbar_register               (GtkButton        *button,
94                                                   ToolbarPage *prefs_toolbar);
95 static void prefs_toolbar_substitute             (GtkButton        *button,
96                                                   ToolbarPage *prefs_toolbar);
97 static void prefs_toolbar_delete                 (GtkButton        *button,
98                                                   ToolbarPage *prefs_toolbar);
99
100 static void prefs_toolbar_up                     (GtkButton        *button,
101                                                   ToolbarPage *prefs_toolbar);
102
103 static void prefs_toolbar_down                   (GtkButton        *button,
104                                                   ToolbarPage *prefs_toolbar);
105
106 static void prefs_toolbar_select_row_set         (GtkCList *clist, 
107                                                   gint row, 
108                                                   gint column,
109                                                   GdkEvent *event, 
110                                                   ToolbarPage *prefs_toolbar);
111
112 static void prefs_toolbar_select_row_icons       (GtkCList *clist, 
113                                                   gint row, 
114                                                   gint column,
115                                                   GdkEvent *event, 
116                                                   ToolbarPage *prefs_toolbar);
117
118 static void prefs_toolbar_create                 (ToolbarPage *prefs_toolbar);
119
120 static void prefs_toolbar_selection_changed      (GtkList *list, 
121                                                   ToolbarPage *prefs_toolbar);
122
123 void prefs_toolbar_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
124 {
125         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
126         gchar *win_titles[3];
127         win_titles[TOOLBAR_MAIN]    = _("Main toolbar configuration");
128         win_titles[TOOLBAR_COMPOSE] = _("Compose toolbar configuration");  
129         win_titles[TOOLBAR_MSGVIEW] = _("Message view toolbar configuration");  
130
131         prefs_toolbar->window = GTK_WIDGET(window);
132
133         toolbar_read_config_file(prefs_toolbar->source);
134
135         prefs_toolbar_create(prefs_toolbar);
136         prefs_toolbar_populate(prefs_toolbar);
137 }
138
139 void prefs_toolbar_save(PrefsPage *_page)
140 {
141         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
142         gint row = 0;
143         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
144         gchar *entry = NULL;
145         ToolbarItem *item;
146
147         toolbar_clear_list(prefs_toolbar->source);
148
149         if (clist->rows == 0) {
150                 toolbar_set_default(prefs_toolbar->source);
151         }
152         else {
153                 do {
154                         item = g_new0(ToolbarItem, 1);
155                         
156                         gtk_clist_get_text(clist, row, 1, &entry);
157                         item->file = g_strdup(entry);
158                         
159                         gtk_clist_get_text(clist, row, 2, &entry);
160                         item->text = g_strdup(entry);
161                         
162                         gtk_clist_get_text(clist, row, 3, &entry);      
163                         item->index = toolbar_ret_val_from_descr(entry);
164
165                         /* TODO: save A_SYL_ACTIONS only if they are still active */
166                         toolbar_set_list_item(item, prefs_toolbar->source);
167
168                         g_free(item->file);
169                         g_free(item->text);
170                         g_free(item);
171                         row++;
172                         
173                 } while(gtk_clist_get_text(clist, row, 3, &entry));
174         }
175
176         toolbar_save_config_file(prefs_toolbar->source);
177
178         if (prefs_toolbar->source == TOOLBAR_MAIN) 
179                 main_window_reflect_prefs_all_real(TRUE);
180         else if (prefs_toolbar->source == TOOLBAR_COMPOSE)
181                 compose_reflect_prefs_pixmap_theme();
182         else if (prefs_toolbar->source == TOOLBAR_MSGVIEW)
183                 messageview_reflect_prefs_pixmap_theme();
184 }
185
186 static void prefs_toolbar_destroy_widget(PrefsPage *_page)
187 {
188         /* nothing here */
189 }
190
191 static void prefs_toolbar_destroy(PrefsPage *_page)
192 {
193         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
194
195         g_list_free(prefs_toolbar->combo_action_list);
196         g_free(_page);
197 }
198
199 static void prefs_toolbar_set_displayed(ToolbarPage *prefs_toolbar)
200 {
201         GdkPixmap *xpm;
202         GdkBitmap *xpmmask;
203         gchar *activ[4];
204         GSList *cur;
205         GtkCList *clist_set = GTK_CLIST(prefs_toolbar->clist_set);
206         GSList *toolbar_list = toolbar_get_list(prefs_toolbar->source);
207
208         gtk_clist_clear(clist_set);
209         gtk_clist_freeze(clist_set);
210
211         /* set currently active toolbar entries */
212         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
213                 ToolbarItem *item = (ToolbarItem*) cur->data;
214         
215                 if (item->index != A_SEPARATOR) {
216                         gint row_num;
217                         StockPixmap icon = stock_pixmap_get_icon(item->file);
218                         
219                         stock_pixmap_gdk(prefs_toolbar->window, icon,
220                                           &xpm, &xpmmask);
221                         activ[0] = g_strdup("");
222                         activ[1] = g_strdup(item->file);
223                         activ[2] = g_strdup(item->text);
224                         activ[3] = g_strdup(toolbar_ret_descr_from_val(item->index));
225                         row_num  = gtk_clist_append(clist_set, activ);
226                         gtk_clist_set_pixmap(clist_set, 
227                                               row_num, 0, xpm, xpmmask);
228
229                 } else {
230                         activ[0] = g_strdup(SEPARATOR_PIXMAP);
231                         activ[1] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
232                         activ[2] = g_strdup("");
233                         activ[3] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
234                         gtk_clist_append(clist_set, activ);
235                 }
236
237                 g_free(activ[0]);
238                 g_free(activ[1]);
239                 g_free(activ[2]);
240                 g_free(activ[3]);
241         }
242
243         gtk_clist_thaw(clist_set);
244         gtk_clist_columns_autosize(clist_set);
245         gtk_clist_set_row_height(clist_set, CELL_SPACING);
246         gtk_clist_select_row(clist_set, 0, 0);
247 }
248
249 static void prefs_toolbar_populate(ToolbarPage *prefs_toolbar)
250 {
251         gint i;
252         GSList *cur;
253         GList *syl_actions = NULL;
254         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
255         GdkPixmap *xpm;
256         GdkBitmap *xpmmask;
257         gchar *avail[2];
258         gchar *act;
259         
260         gtk_clist_clear(clist_icons);
261         gtk_clist_freeze(clist_icons);
262
263         /* set available icons */
264         avail[0] = g_strdup(SEPARATOR_PIXMAP);
265         avail[1] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
266         gtk_clist_append(clist_icons, avail);
267         g_free(avail[0]);
268         g_free(avail[1]);
269
270         prefs_toolbar->combo_action_list = toolbar_get_action_items(prefs_toolbar->source);
271         gtk_combo_set_popdown_strings(GTK_COMBO(prefs_toolbar->combo_action), prefs_toolbar->combo_action_list);
272         gtk_combo_set_value_in_list(GTK_COMBO(prefs_toolbar->combo_action), 0, FALSE);
273         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->combo_entry), prefs_toolbar->combo_action_list->data);
274
275         /* get currently defined sylpheed actions */
276         if (prefs_common.actions_list != NULL) {
277
278                 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
279                         act = (gchar *)cur->data;
280                         syl_actions = g_list_append(syl_actions, act);
281                 } 
282
283                 gtk_combo_set_popdown_strings(GTK_COMBO(prefs_toolbar->combo_syl_action), syl_actions);
284                 gtk_combo_set_value_in_list(GTK_COMBO(prefs_toolbar->combo_syl_action), 0, FALSE);
285                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->combo_syl_entry), syl_actions->data);
286                 prefs_toolbar_selection_changed(GTK_LIST(prefs_toolbar->combo_syl_list), prefs_toolbar);
287                 g_list_free(syl_actions);
288         }
289
290         for (i = 0; i < STOCK_PIXMAP_SYLPHEED_LOGO; i++) {
291
292                 avail[0] = g_strdup("");
293                 avail[1] = g_strdup(stock_pixmap_get_name((StockPixmap)i));
294
295                 stock_pixmap_gdk(prefs_toolbar->window, i,
296                                   &xpm, &xpmmask);
297                 gtk_clist_append(clist_icons, avail);
298                 gtk_clist_set_pixmap(clist_icons, 
299                                        i + 1, 0, xpm, xpmmask);
300                 
301                 g_free(avail[0]);
302                 g_free(avail[1]);
303         }
304
305         gtk_clist_thaw(clist_icons);
306         gtk_clist_columns_autosize(clist_icons);
307         gtk_clist_set_row_height(clist_icons, CELL_SPACING);
308         gtk_clist_select_row(clist_icons, 0, 0);
309
310         prefs_toolbar_set_displayed(prefs_toolbar);
311
312         toolbar_clear_list(prefs_toolbar->source);
313 }
314
315 static gboolean is_duplicate(ToolbarPage *prefs_toolbar, gchar *chosen_action)
316 {
317         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
318         gchar *entry;
319         gint row = 0;
320         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
321
322         g_return_val_if_fail(chosen_action != NULL, TRUE);
323         if (clist->rows == 0) 
324                 return FALSE;
325         
326         /* allow duplicate entries (A_SYL_ACTIONS) */
327         if (g_strcasecmp(syl_act, chosen_action) == 0)
328                 return FALSE;
329
330         do {
331                 gtk_clist_get_text(clist, row, 3, &entry);
332                 if ( g_strcasecmp(chosen_action, entry) == 0)
333                         return TRUE;
334                 row++;
335         } while ((gtk_clist_get_text(clist, row, 3, &entry)) && (row <= clist->rows));
336         
337         return FALSE;
338 }
339
340 #if 0
341 static void prefs_toolbar_default(GtkButton *button, ToolbarPage *prefs_toolbar)
342 {
343         toolbar_clear_list(prefs_toolbar->source);
344         toolbar_set_default(prefs_toolbar->source);
345         prefs_toolbar_set_displayed(prefs_toolbar);
346 }
347 #endif
348
349 static void get_action_name(gchar *entry, gchar **menu)
350 {
351         gchar *act, *act_p;
352
353         if (prefs_common.actions_list != NULL) {
354                 
355                 act = g_strdup(entry);
356                 act_p = strstr(act, ": ");
357                 if (act_p != NULL)
358                         act_p[0] = 0x00;
359                 /* freed by calling func */
360                 *menu = act;
361         }
362 }
363
364 static void prefs_toolbar_register(GtkButton *button, ToolbarPage *prefs_toolbar)
365 {
366         GtkCList *clist_set   = GTK_CLIST(prefs_toolbar->clist_set);
367         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
368         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
369         gint row_icons = 0;
370         gint row_set = 0;
371         GdkPixmap *xpm;
372         GdkBitmap *xpmmask;
373         gchar *item[4] = {NULL, NULL, NULL, NULL};
374
375         if (clist_icons->rows == 0) return; 
376
377         if (clist_icons->selection) {
378                 if (clist_icons->selection->data) 
379                         row_icons = GPOINTER_TO_INT(clist_icons->selection->data);
380         } else
381                 return;
382
383         gtk_clist_get_text(clist_icons, row_icons, 1, &item[1]);
384         item[3] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_entry)));
385         
386         /* SEPARATOR or other ? */
387         if (g_strcasecmp(item[1], toolbar_ret_descr_from_val(A_SEPARATOR)) == 0) {
388                 item[0] = g_strdup(SEPARATOR_PIXMAP);
389                 item[2] = g_strdup("");
390                 item[3] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
391                 
392                 row_set = gtk_clist_append(GTK_CLIST(prefs_toolbar->clist_set), item);
393
394                 g_free(item[0]);
395         } else {
396
397                 if (is_duplicate(prefs_toolbar, item[3])) {
398                         alertpanel_error(ERROR_MSG);
399                         g_free(item[3]);
400                         return;
401                 }
402
403                 stock_pixmap_gdk(prefs_toolbar->window, stock_pixmap_get_icon(item[1]),
404                                   &xpm, &xpmmask);
405
406                 if (g_strcasecmp(item[3], syl_act) == 0) {
407
408                         gchar *entry = gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_syl_entry));
409                         get_action_name(entry, &item[2]);
410                 }
411                 else {
412                         item[2] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->entry_icon_text)));
413                 }
414
415                 row_set = gtk_clist_append(GTK_CLIST(prefs_toolbar->clist_set), item);
416                 gtk_clist_set_pixmap(clist_set, row_set, 0, xpm, xpmmask);
417         }
418
419         gtk_clist_moveto(clist_set, row_set, 0, row_set/clist_set->rows, 0);
420         gtk_clist_select_row(clist_set, row_set, 0);
421
422         g_free(item[2]);
423         g_free(item[3]);
424 }
425
426 static void prefs_toolbar_substitute(GtkButton *button, ToolbarPage *prefs_toolbar)
427 {
428         GtkCList *clist_set   = GTK_CLIST(prefs_toolbar->clist_set);
429         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
430         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
431         gint row_icons = 0;
432         gint row_set = 0;
433         GdkPixmap *xpm;
434         GdkBitmap *xpmmask;
435         gchar *item[4] = {NULL, NULL, NULL, NULL};
436         gchar *ac_set;
437
438         /* no rows or nothing selected */
439         if ((clist_set->rows == 0) || (clist_set->selection == 0)) return; 
440
441         if (clist_icons->selection) {
442                 if (clist_icons->selection->data) 
443                         row_icons = GPOINTER_TO_INT(clist_icons->selection->data);
444         } else
445                 return;
446         
447         if (clist_set->selection) {
448                 if (clist_set->selection->data) 
449                         row_set = GPOINTER_TO_INT(clist_set->selection->data);
450         } else
451                 return;
452         
453         gtk_clist_get_text(clist_icons, row_icons, 1, &item[1]);
454         gtk_clist_get_text(clist_set, row_set, 3, &ac_set);
455         item[3] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_entry)));
456
457         if (g_strcasecmp(item[1], toolbar_ret_descr_from_val(A_SEPARATOR)) == 0) {
458                 item[0] = g_strdup(SEPARATOR_PIXMAP);
459                 item[2] = g_strdup("");
460                 item[3] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
461
462                 gtk_clist_remove(clist_set, row_set);
463                 row_set = gtk_clist_insert(clist_set, row_set, item);
464
465                 g_free(item[0]);
466         } else {
467
468                 if ((is_duplicate(prefs_toolbar, item[3])) && (g_strcasecmp(item[3], ac_set) != 0)){
469                         alertpanel_error(ERROR_MSG);
470                         g_free(item[3]);
471                         return;
472                 }
473
474                 stock_pixmap_gdk(prefs_toolbar->window, stock_pixmap_get_icon(item[1]),
475                                   &xpm, &xpmmask);
476
477                 if (g_strcasecmp(item[3], syl_act) == 0) {
478
479                         gchar *entry = gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_syl_entry));
480                         get_action_name(entry, &item[2]);
481                 } else {
482                         item[2] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->entry_icon_text)));
483                 }
484
485                 gtk_clist_remove(clist_set, row_set);
486                 row_set = gtk_clist_insert(clist_set, row_set, item);
487                 gtk_clist_set_pixmap(clist_set, row_set, 0, xpm, xpmmask);
488         }
489         
490         gtk_clist_moveto(clist_set, row_set, 0, row_set/clist_set->rows, 0);
491         gtk_clist_select_row(clist_set, row_set, 0);
492
493         g_free(item[2]);
494         g_free(item[3]);
495 }
496
497 static void prefs_toolbar_delete(GtkButton *button, ToolbarPage *prefs_toolbar)
498 {
499         GtkCList *clist_set = GTK_CLIST(prefs_toolbar->clist_set);
500         gint row_set = 0;
501
502         if (clist_set->rows == 0) return; 
503         if (clist_set->selection) {
504                 if (clist_set->selection->data) 
505                         row_set = GPOINTER_TO_INT(clist_set->selection->data);
506         } else
507                 return;
508         
509         if (clist_set->row_list != NULL) {
510                         
511                 row_set = GPOINTER_TO_INT(clist_set->selection->data);
512                 gtk_clist_remove(clist_set, row_set);
513                 gtk_clist_columns_autosize(clist_set);
514                 
515                 if (clist_set->rows > 0)
516                         gtk_clist_select_row(clist_set,(row_set == 0) ? 0:row_set - 1, 0);
517         }
518 }
519
520 static void prefs_toolbar_up(GtkButton *button, ToolbarPage *prefs_toolbar)
521 {
522         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
523         gint row = 0;
524
525         if (!clist->selection) return;
526         if (clist->selection->data)
527                 row = GPOINTER_TO_INT(clist->selection->data);
528
529         if (row >= 0) {
530                 gtk_clist_row_move(clist, row, row - 1);
531                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
532                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
533                 } 
534         }
535 }
536
537 static void prefs_toolbar_down(GtkButton *button, ToolbarPage *prefs_toolbar)
538 {
539         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
540         gint row = 0;
541
542         if (!clist->selection) return;
543         if (clist->selection->data)
544                 row = GPOINTER_TO_INT(clist->selection->data);
545
546         if (row >= 0 && row < clist->rows - 1) {
547                 gtk_clist_row_move(clist, row, row + 1);
548                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
549                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
550                 } 
551         }
552 }
553
554 static void prefs_toolbar_select_row_set(GtkCList *clist, gint row, gint column,
555                                          GdkEvent *event, ToolbarPage *prefs_toolbar)
556 {
557         GtkCList *clist_ico = GTK_CLIST(prefs_toolbar->clist_icons);
558         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
559         gint row_set = 0;
560         gint row_ico = 0;
561         gint item_num = 0;
562         gchar *file, *icon_text, *descr, *entry;
563         GList *cur;
564
565         if (clist->selection->data) 
566                 row_set = GPOINTER_TO_INT(clist->selection->data);      
567
568         gtk_clist_get_text(clist, row_set, 1, &file);
569         gtk_clist_get_text(clist, row_set, 2, &icon_text);
570         gtk_clist_get_text(clist, row_set, 3, &descr);
571
572         if (g_strcasecmp(descr, syl_act) != 0)
573                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->entry_icon_text), icon_text);
574         
575         /* scan combo list for selected description an set combo item accordingly */
576         for (cur = prefs_toolbar->combo_action_list; cur != NULL; cur = cur->next) {
577                 gchar *item_str = (gchar*)cur->data;
578                 if (g_strcasecmp(item_str, descr) == 0) {
579                         gtk_list_select_item(GTK_LIST(prefs_toolbar->combo_list), item_num);
580                         break;
581                 }
582                 else
583                         item_num++;
584         }
585
586         do {
587                 gtk_clist_get_text(clist_ico, row_ico, 1, &entry);
588                 row_ico++;
589         } while(g_strcasecmp(entry, file) != 0);
590         
591         gtk_clist_select_row(clist_ico, row_ico - 1, 0);
592         gtk_clist_moveto(clist_ico, row_ico - 1, 0, row_ico/clist_ico->rows, 0);
593 }
594
595 static void prefs_toolbar_select_row_icons(GtkCList *clist, gint row, gint column,
596                                            GdkEvent *event, ToolbarPage *prefs_toolbar)
597 {
598         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
599         gchar *text;
600         
601         gtk_clist_get_text(clist_icons, row, 1, &text);
602
603         if (!text) return;
604
605         if (g_strcasecmp(toolbar_ret_descr_from_val(A_SEPARATOR), text) == 0) {
606                 gtk_widget_set_sensitive(prefs_toolbar->combo_action,     FALSE);
607                 gtk_widget_set_sensitive(prefs_toolbar->entry_icon_text,  FALSE);
608                 gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, FALSE);
609         } else {
610                 gtk_widget_set_sensitive(prefs_toolbar->combo_action,     TRUE);
611                 gtk_widget_set_sensitive(prefs_toolbar->entry_icon_text,  TRUE);
612                 gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, TRUE);
613         }
614 }
615
616 static void prefs_toolbar_selection_changed(GtkList *list,
617                                             ToolbarPage *prefs_toolbar)
618 {
619         gchar *cur_entry = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_entry)));
620         gchar *actions_entry = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
621
622         gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, TRUE);
623
624         if (g_strcasecmp(cur_entry, actions_entry) == 0) {
625                 gtk_widget_hide(prefs_toolbar->entry_icon_text);
626                 gtk_widget_show(prefs_toolbar->combo_syl_action);
627                 gtk_label_set_text(GTK_LABEL(prefs_toolbar->label_icon_text), _("Sylpheed Action"));
628
629                 if (prefs_common.actions_list == NULL) {
630                     gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, FALSE);
631                 }
632
633         } else {
634                 gtk_widget_hide(prefs_toolbar->combo_syl_action);
635                 gtk_widget_show(prefs_toolbar->entry_icon_text);
636                 gtk_label_set_text(GTK_LABEL(prefs_toolbar->label_icon_text), _("Toolbar text"));
637         }
638
639         gtk_misc_set_alignment(GTK_MISC(prefs_toolbar->label_icon_text), 1, 0.5);
640         gtk_widget_show(prefs_toolbar->label_icon_text);
641         g_free(cur_entry);
642 }
643
644 static void prefs_toolbar_create(ToolbarPage *prefs_toolbar)
645 {
646         GtkWidget *main_vbox;
647         GtkWidget *top_hbox;
648         GtkWidget *compose_frame;
649         GtkWidget *reg_hbox;
650         GtkWidget *arrow;
651         GtkWidget *btn_hbox;
652         GtkWidget *reg_btn;
653         GtkWidget *subst_btn;
654         GtkWidget *del_btn;
655         GtkWidget *vbox_frame;
656         GtkWidget *table;
657         GtkWidget *scrolledwindow_clist_icon;
658         GtkWidget *clist_icons;
659         GtkWidget *label_icon_text;
660         GtkWidget *entry_icon_text;
661         GtkWidget *label_action_sel;
662         GtkWidget *empty_label;
663         GtkWidget *combo_action;
664         GtkWidget *combo_entry;
665         GtkWidget *combo_list;
666         GtkWidget *combo_syl_action;
667         GtkWidget *combo_syl_entry;
668         GtkWidget *combo_syl_list;
669         GtkWidget *frame_toolbar_items;
670         GtkWidget *hbox_bottom;
671         GtkWidget *scrolledwindow_clist_set;
672         GtkWidget *clist_set;
673
674         GtkWidget *btn_vbox;
675         GtkWidget *up_btn;
676         GtkWidget *down_btn;
677         
678         gchar *titles[N_DISPLAYED_ITEMS_COLS];
679  
680         debug_print("Creating custom toolbar window...\n");
681
682         main_vbox = gtk_vbox_new(FALSE, 0);
683         gtk_widget_show(main_vbox);
684         
685         top_hbox = gtk_hbox_new(FALSE, 0);
686         gtk_box_pack_start(GTK_BOX(main_vbox), top_hbox, TRUE, TRUE, 0);
687   
688         compose_frame = gtk_frame_new(_("Available toolbar items"));
689         gtk_box_pack_start(GTK_BOX(top_hbox), compose_frame, TRUE, TRUE, 0);
690         gtk_container_set_border_width(GTK_CONTAINER(compose_frame), 5);
691
692         vbox_frame = gtk_vbox_new(FALSE, 0);
693         gtk_container_add(GTK_CONTAINER(compose_frame), vbox_frame);
694         
695         /* available icons */
696         scrolledwindow_clist_icon = gtk_scrolled_window_new(NULL, NULL);
697         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_clist_icon), 5);
698         gtk_container_add(GTK_CONTAINER(vbox_frame), scrolledwindow_clist_icon);
699         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_clist_icon), 
700                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
701         
702         clist_icons = gtk_clist_new(2);
703         gtk_container_add(GTK_CONTAINER(scrolledwindow_clist_icon), clist_icons);
704         gtk_container_set_border_width(GTK_CONTAINER(clist_icons), 1);
705         gtk_clist_set_column_width(GTK_CLIST(clist_icons), 0, 35);
706         gtk_clist_set_column_width(GTK_CLIST(clist_icons), 1, 200);
707         gtk_clist_column_titles_hide(GTK_CLIST(clist_icons));
708         gtk_widget_set_usize(clist_icons, 225, 108); 
709
710         table = gtk_table_new (2, 3, FALSE);
711         gtk_container_add (GTK_CONTAINER (vbox_frame), table);
712         gtk_container_set_border_width (GTK_CONTAINER (table), 8);
713         gtk_table_set_row_spacings (GTK_TABLE (table), 8);
714         gtk_table_set_col_spacings (GTK_TABLE (table), 8);
715         
716         /* icon description */
717         label_icon_text = gtk_label_new("");
718         gtk_misc_set_alignment(GTK_MISC(label_icon_text), 0, 0.5);
719         gtk_widget_show (label_icon_text);
720         gtk_table_attach (GTK_TABLE (table), label_icon_text, 0, 1, 0, 1,
721                           (GtkAttachOptions) (GTK_FILL),
722                           (GtkAttachOptions) (0), 0, 0);
723
724         entry_icon_text = gtk_entry_new();
725         gtk_table_attach (GTK_TABLE (table), entry_icon_text, 1, 2, 0, 1,
726                           (GtkAttachOptions) (/*GTK_EXPAND | */GTK_FILL),
727                           (GtkAttachOptions) (0), 0, 0);
728
729         /* Sylpheed Action Combo Box */
730         combo_syl_action = gtk_combo_new();
731         combo_syl_list = GTK_COMBO(combo_syl_action)->list;
732         combo_syl_entry = GTK_COMBO(combo_syl_action)->entry;
733         gtk_entry_set_editable(GTK_ENTRY(combo_syl_entry), FALSE);
734         gtk_table_attach (GTK_TABLE (table), combo_syl_action, 1, 2, 0, 1,
735                           (GtkAttachOptions) (/*GTK_EXPAND | */GTK_FILL),
736                           (GtkAttachOptions) (0), 0, 0);
737
738         empty_label = gtk_label_new("");
739         gtk_table_attach (GTK_TABLE (table), empty_label, 2, 3, 0, 1,
740                           (GtkAttachOptions) (GTK_EXPAND),
741                           (GtkAttachOptions) (0), 0, 0);
742         /* available actions */
743         label_action_sel = gtk_label_new(_("Event executed on click"));
744         gtk_misc_set_alignment(GTK_MISC(label_action_sel), 0, 0.5);
745         gtk_table_attach (GTK_TABLE (table), label_action_sel, 0, 1, 1, 2,
746                           (GtkAttachOptions) (GTK_FILL),
747                           (GtkAttachOptions) (0), 0, 0);
748
749         combo_action = gtk_combo_new();
750         gtk_table_attach (GTK_TABLE (table), combo_action, 1, 2, 1, 2,
751                           (GtkAttachOptions) (GTK_FILL),
752                           (GtkAttachOptions) (0), 0, 0);
753         
754         combo_list = GTK_COMBO(combo_action)->list;
755         combo_entry = GTK_COMBO(combo_action)->entry;
756         gtk_entry_set_editable(GTK_ENTRY(combo_entry), FALSE);
757         
758         empty_label = gtk_label_new("");
759         gtk_table_attach (GTK_TABLE (table), empty_label, 2, 3, 1, 2,
760                           (GtkAttachOptions) (GTK_EXPAND),
761                           (GtkAttachOptions) (0), 0, 0);
762
763         /* register / substitute / delete */
764         reg_hbox = gtk_hbox_new(FALSE, 4);
765         gtk_box_pack_start(GTK_BOX(main_vbox), reg_hbox, FALSE, FALSE, 0);
766         gtk_container_set_border_width(GTK_CONTAINER(reg_hbox), 10);
767
768         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
769         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
770         gtk_widget_set_usize(arrow, -1, 16);
771
772         btn_hbox = gtk_hbox_new(TRUE, 4);
773         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
774
775         reg_btn = gtk_button_new_with_label(_("Add"));
776         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
777         gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
778                             GTK_SIGNAL_FUNC(prefs_toolbar_register), 
779                             prefs_toolbar);
780
781         subst_btn = gtk_button_new_with_label(_("  Replace  "));
782         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
783         gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
784                             GTK_SIGNAL_FUNC(prefs_toolbar_substitute),
785                             prefs_toolbar);
786
787         del_btn = gtk_button_new_with_label(_("Delete"));
788         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
789         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
790                             GTK_SIGNAL_FUNC(prefs_toolbar_delete), 
791                             prefs_toolbar);
792
793         /* currently active toolbar items */
794         frame_toolbar_items = gtk_frame_new(_("Displayed toolbar items"));
795         gtk_box_pack_start(GTK_BOX(main_vbox), frame_toolbar_items, TRUE, TRUE, 0);
796         gtk_container_set_border_width(GTK_CONTAINER(frame_toolbar_items), 5);
797         
798         hbox_bottom = gtk_hbox_new(FALSE, 0);
799         gtk_container_add(GTK_CONTAINER(frame_toolbar_items), hbox_bottom);
800         
801         scrolledwindow_clist_set = gtk_scrolled_window_new(NULL, NULL);
802         gtk_box_pack_start(GTK_BOX(hbox_bottom), scrolledwindow_clist_set, TRUE, TRUE, 0);
803         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_clist_set), 1);
804         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_clist_icon), 
805                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
806
807         titles[COL_ICON]     = _("Icon");
808         titles[COL_FILENAME] = _("File name");
809         titles[COL_TEXT]     = _("Icon text");
810         titles[COL_EVENT]    = _("Mapped event");
811         
812         clist_set = gtk_clist_new_with_titles(N_DISPLAYED_ITEMS_COLS, titles);
813         gtk_widget_show(clist_set);
814         gtk_container_add(GTK_CONTAINER(scrolledwindow_clist_set), clist_set);
815         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_ICON    , 80);
816         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_FILENAME, 80);
817         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_TEXT    , 80);
818         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_EVENT   , 80);
819         gtk_clist_column_titles_show(GTK_CLIST(clist_set));
820         gtk_widget_set_usize(clist_set, 225, 120);
821
822         btn_vbox = gtk_vbox_new(FALSE, 8);
823         gtk_widget_show(btn_vbox);
824         gtk_box_pack_start(GTK_BOX(hbox_bottom), btn_vbox, FALSE, FALSE, 5);
825
826         up_btn = gtk_button_new_with_label(_("Up"));
827         gtk_widget_show(up_btn);
828         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 2);
829
830         down_btn = gtk_button_new_with_label(_("Down"));
831         gtk_widget_show(down_btn);
832         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
833
834         gtk_signal_connect(GTK_OBJECT(clist_set), "select_row",
835                            GTK_SIGNAL_FUNC(prefs_toolbar_select_row_set),
836                            prefs_toolbar);
837         gtk_signal_connect(GTK_OBJECT(clist_icons), "select_row",
838                            GTK_SIGNAL_FUNC(prefs_toolbar_select_row_icons),
839                            prefs_toolbar);
840         gtk_signal_connect(GTK_OBJECT(combo_list), "selection-changed",
841                            GTK_SIGNAL_FUNC(prefs_toolbar_selection_changed),
842                            prefs_toolbar);
843         gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
844                            GTK_SIGNAL_FUNC(prefs_toolbar_up), prefs_toolbar);
845         gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
846                            GTK_SIGNAL_FUNC(prefs_toolbar_down), prefs_toolbar);
847         
848         prefs_toolbar->clist_icons      = clist_icons;
849         prefs_toolbar->clist_set        = clist_set;
850         prefs_toolbar->combo_action     = combo_action;
851         prefs_toolbar->combo_entry      = combo_entry;
852         prefs_toolbar->combo_list       = combo_list;
853         prefs_toolbar->entry_icon_text  = entry_icon_text;
854         prefs_toolbar->combo_syl_action = combo_syl_action;
855         prefs_toolbar->combo_syl_list   = combo_syl_list;
856         prefs_toolbar->combo_syl_entry  = combo_syl_entry;
857
858         prefs_toolbar->label_icon_text  = label_icon_text;
859         prefs_toolbar->label_action_sel = label_action_sel;
860
861         prefs_toolbar->page.widget = main_vbox;
862 }
863
864 ToolbarPage *prefs_toolbar_mainwindow;
865 ToolbarPage *prefs_toolbar_composewindow;
866 ToolbarPage *prefs_toolbar_messageview;
867
868 void prefs_toolbar_init()
869 {
870         ToolbarPage *page;
871
872         page = g_new0(ToolbarPage, 1);
873         page->page.path = _("Customize Toolbars/Main Window");
874         page->page.create_widget = prefs_toolbar_create_widget;
875         page->page.destroy_widget = prefs_toolbar_destroy_widget;
876         page->page.save_page = prefs_toolbar_save;
877         page->page.destroy_page = prefs_toolbar_destroy;
878         page->source = TOOLBAR_MAIN;
879         prefs_gtk_register_page((PrefsPage *) page);
880         prefs_toolbar_mainwindow = page;
881
882         page = g_new0(ToolbarPage, 1);
883         page->page.path = _("Customize Toolbars/Compose Window");
884         page->page.create_widget = prefs_toolbar_create_widget;
885         page->page.destroy_widget = prefs_toolbar_destroy_widget;
886         page->page.save_page = prefs_toolbar_save;
887         page->page.destroy_page = prefs_toolbar_destroy;
888         page->source = TOOLBAR_COMPOSE;
889         prefs_gtk_register_page((PrefsPage *) page);
890         prefs_toolbar_composewindow = page;
891
892         page = g_new0(ToolbarPage, 1);
893         page->page.path = _("Customize Toolbars/Message Window");
894         page->page.create_widget = prefs_toolbar_create_widget;
895         page->page.destroy_widget = prefs_toolbar_destroy_widget;
896         page->page.save_page = prefs_toolbar_save;
897         page->page.destroy_page = prefs_toolbar_destroy;
898         page->source = TOOLBAR_MSGVIEW;
899         prefs_gtk_register_page((PrefsPage *) page);
900         prefs_toolbar_messageview = page;
901 }
902
903 void prefs_toolbar_done()
904 {
905         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_mainwindow);
906         g_free(prefs_toolbar_mainwindow);
907         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_composewindow);
908         g_free(prefs_toolbar_composewindow);
909         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_messageview);
910         g_free(prefs_toolbar_messageview);
911 }