inital gtk2 patch
[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         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
189
190         g_list_free(prefs_toolbar->combo_action_list);
191         prefs_toolbar->combo_action_list = NULL;
192 }
193
194 static void prefs_toolbar_set_displayed(ToolbarPage *prefs_toolbar)
195 {
196         GdkPixmap *xpm;
197         GdkBitmap *xpmmask;
198         gchar *activ[4];
199         GSList *cur;
200         GtkCList *clist_set = GTK_CLIST(prefs_toolbar->clist_set);
201         GSList *toolbar_list = toolbar_get_list(prefs_toolbar->source);
202
203         gtk_clist_clear(clist_set);
204         gtk_clist_freeze(clist_set);
205
206         /* set currently active toolbar entries */
207         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
208                 ToolbarItem *item = (ToolbarItem*) cur->data;
209         
210                 if (item->index != A_SEPARATOR) {
211                         gint row_num;
212                         StockPixmap icon = stock_pixmap_get_icon(item->file);
213                         
214                         stock_pixmap_gdk(prefs_toolbar->window, icon,
215                                           &xpm, &xpmmask);
216                         activ[0] = g_strdup("");
217                         activ[1] = g_strdup(item->file);
218                         activ[2] = g_strdup(item->text);
219                         activ[3] = g_strdup(toolbar_ret_descr_from_val(item->index));
220                         row_num  = gtk_clist_append(clist_set, activ);
221                         gtk_clist_set_pixmap(clist_set, 
222                                               row_num, 0, xpm, xpmmask);
223
224                 } else {
225                         activ[0] = g_strdup(SEPARATOR_PIXMAP);
226                         activ[1] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
227                         activ[2] = g_strdup("");
228                         activ[3] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
229                         gtk_clist_append(clist_set, activ);
230                 }
231
232                 g_free(activ[0]);
233                 g_free(activ[1]);
234                 g_free(activ[2]);
235                 g_free(activ[3]);
236         }
237
238         gtk_clist_thaw(clist_set);
239         gtk_clist_columns_autosize(clist_set);
240         gtk_clist_set_row_height(clist_set, CELL_SPACING);
241         gtk_clist_select_row(clist_set, 0, 0);
242 }
243
244 static void prefs_toolbar_populate(ToolbarPage *prefs_toolbar)
245 {
246         gint i;
247         GSList *cur;
248         GList *syl_actions = NULL;
249         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
250         GdkPixmap *xpm;
251         GdkBitmap *xpmmask;
252         gchar *avail[2];
253         gchar *act;
254         
255         gtk_clist_clear(clist_icons);
256         gtk_clist_freeze(clist_icons);
257
258         /* set available icons */
259         avail[0] = g_strdup(SEPARATOR_PIXMAP);
260         avail[1] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
261         gtk_clist_append(clist_icons, avail);
262         g_free(avail[0]);
263         g_free(avail[1]);
264
265         prefs_toolbar->combo_action_list = toolbar_get_action_items(prefs_toolbar->source);
266         gtk_combo_set_popdown_strings(GTK_COMBO(prefs_toolbar->combo_action), prefs_toolbar->combo_action_list);
267         gtk_combo_set_value_in_list(GTK_COMBO(prefs_toolbar->combo_action), 0, FALSE);
268         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->combo_entry), prefs_toolbar->combo_action_list->data);
269
270         /* get currently defined sylpheed actions */
271         if (prefs_common.actions_list != NULL) {
272
273                 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
274                         act = (gchar *)cur->data;
275                         syl_actions = g_list_append(syl_actions, act);
276                 } 
277
278                 gtk_combo_set_popdown_strings(GTK_COMBO(prefs_toolbar->combo_syl_action), syl_actions);
279                 gtk_combo_set_value_in_list(GTK_COMBO(prefs_toolbar->combo_syl_action), 0, FALSE);
280                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->combo_syl_entry), syl_actions->data);
281                 prefs_toolbar_selection_changed(GTK_LIST(prefs_toolbar->combo_syl_list), prefs_toolbar);
282                 g_list_free(syl_actions);
283         }
284
285         for (i = 0; i < STOCK_PIXMAP_SYLPHEED_LOGO; i++) {
286
287                 avail[0] = g_strdup("");
288                 avail[1] = g_strdup(stock_pixmap_get_name((StockPixmap)i));
289
290                 stock_pixmap_gdk(prefs_toolbar->window, i,
291                                   &xpm, &xpmmask);
292                 gtk_clist_append(clist_icons, avail);
293                 gtk_clist_set_pixmap(clist_icons, 
294                                        i + 1, 0, xpm, xpmmask);
295                 
296                 g_free(avail[0]);
297                 g_free(avail[1]);
298         }
299
300         gtk_clist_thaw(clist_icons);
301         gtk_clist_columns_autosize(clist_icons);
302         gtk_clist_set_row_height(clist_icons, CELL_SPACING);
303         gtk_clist_select_row(clist_icons, 0, 0);
304
305         prefs_toolbar_set_displayed(prefs_toolbar);
306
307         toolbar_clear_list(prefs_toolbar->source);
308 }
309
310 static gboolean is_duplicate(ToolbarPage *prefs_toolbar, gchar *chosen_action)
311 {
312         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
313         gchar *entry;
314         gint row = 0;
315         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
316
317         g_return_val_if_fail(chosen_action != NULL, TRUE);
318         if (clist->rows == 0) 
319                 return FALSE;
320         
321         /* allow duplicate entries (A_SYL_ACTIONS) */
322         if (g_strcasecmp(syl_act, chosen_action) == 0)
323                 return FALSE;
324
325         do {
326                 gtk_clist_get_text(clist, row, 3, &entry);
327                 if ( g_strcasecmp(chosen_action, entry) == 0)
328                         return TRUE;
329                 row++;
330         } while ((gtk_clist_get_text(clist, row, 3, &entry)) && (row <= clist->rows));
331         
332         return FALSE;
333 }
334
335 static void prefs_toolbar_default(GtkButton *button, ToolbarPage *prefs_toolbar)
336 {
337         toolbar_clear_list(prefs_toolbar->source);
338         toolbar_set_default(prefs_toolbar->source);
339         prefs_toolbar_set_displayed(prefs_toolbar);
340 }
341
342 static void get_action_name(gchar *entry, gchar **menu)
343 {
344         gchar *act, *act_p;
345
346         if (prefs_common.actions_list != NULL) {
347                 
348                 act = g_strdup(entry);
349                 act_p = strstr(act, ": ");
350                 if (act_p != NULL)
351                         act_p[0] = 0x00;
352                 /* freed by calling func */
353                 *menu = act;
354         }
355 }
356
357 static void prefs_toolbar_register(GtkButton *button, ToolbarPage *prefs_toolbar)
358 {
359         GtkCList *clist_set   = GTK_CLIST(prefs_toolbar->clist_set);
360         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
361         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
362         gint row_icons = 0;
363         gint row_set = 0;
364         GdkPixmap *xpm;
365         GdkBitmap *xpmmask;
366         gchar *item[4] = {NULL, NULL, NULL, NULL};
367
368         if (clist_icons->rows == 0) return; 
369
370         if (clist_icons->selection) {
371                 if (clist_icons->selection->data) 
372                         row_icons = GPOINTER_TO_INT(clist_icons->selection->data);
373         } else
374                 return;
375
376         gtk_clist_get_text(clist_icons, row_icons, 1, &item[1]);
377         item[3] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_entry)));
378         
379         /* SEPARATOR or other ? */
380         if (g_strcasecmp(item[1], toolbar_ret_descr_from_val(A_SEPARATOR)) == 0) {
381                 item[0] = g_strdup(SEPARATOR_PIXMAP);
382                 item[2] = g_strdup("");
383                 item[3] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
384                 
385                 row_set = gtk_clist_append(GTK_CLIST(prefs_toolbar->clist_set), item);
386
387                 g_free(item[0]);
388         } else {
389
390                 if (is_duplicate(prefs_toolbar, item[3])) {
391                         alertpanel_error(ERROR_MSG);
392                         g_free(item[3]);
393                         return;
394                 }
395
396                 stock_pixmap_gdk(prefs_toolbar->window, stock_pixmap_get_icon(item[1]),
397                                   &xpm, &xpmmask);
398
399                 if (g_strcasecmp(item[3], syl_act) == 0) {
400
401                         const gchar *entry = gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_syl_entry));
402                         get_action_name(entry, &item[2]);
403                 }
404                 else {
405                         item[2] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->entry_icon_text)));
406                 }
407
408                 row_set = gtk_clist_append(GTK_CLIST(prefs_toolbar->clist_set), item);
409                 gtk_clist_set_pixmap(clist_set, row_set, 0, xpm, xpmmask);
410         }
411
412         gtk_clist_moveto(clist_set, row_set, 0, row_set/clist_set->rows, 0);
413         gtk_clist_select_row(clist_set, row_set, 0);
414
415         g_free(item[2]);
416         g_free(item[3]);
417 }
418
419 static void prefs_toolbar_substitute(GtkButton *button, ToolbarPage *prefs_toolbar)
420 {
421         GtkCList *clist_set   = GTK_CLIST(prefs_toolbar->clist_set);
422         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
423         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
424         gint row_icons = 0;
425         gint row_set = 0;
426         GdkPixmap *xpm;
427         GdkBitmap *xpmmask;
428         gchar *item[4] = {NULL, NULL, NULL, NULL};
429         gchar *ac_set;
430
431         /* no rows or nothing selected */
432         if ((clist_set->rows == 0) || (clist_set->selection == 0)) return; 
433
434         if (clist_icons->selection) {
435                 if (clist_icons->selection->data) 
436                         row_icons = GPOINTER_TO_INT(clist_icons->selection->data);
437         } else
438                 return;
439         
440         if (clist_set->selection) {
441                 if (clist_set->selection->data) 
442                         row_set = GPOINTER_TO_INT(clist_set->selection->data);
443         } else
444                 return;
445         
446         gtk_clist_get_text(clist_icons, row_icons, 1, &item[1]);
447         gtk_clist_get_text(clist_set, row_set, 3, &ac_set);
448         item[3] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_entry)));
449
450         if (g_strcasecmp(item[1], toolbar_ret_descr_from_val(A_SEPARATOR)) == 0) {
451                 item[0] = g_strdup(SEPARATOR_PIXMAP);
452                 item[2] = g_strdup("");
453                 item[3] = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
454
455                 gtk_clist_remove(clist_set, row_set);
456                 row_set = gtk_clist_insert(clist_set, row_set, item);
457
458                 g_free(item[0]);
459         } else {
460
461                 if ((is_duplicate(prefs_toolbar, item[3])) && (g_strcasecmp(item[3], ac_set) != 0)){
462                         alertpanel_error(ERROR_MSG);
463                         g_free(item[3]);
464                         return;
465                 }
466
467                 stock_pixmap_gdk(prefs_toolbar->window, stock_pixmap_get_icon(item[1]),
468                                   &xpm, &xpmmask);
469
470                 if (g_strcasecmp(item[3], syl_act) == 0) {
471
472                         const gchar *entry = gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_syl_entry));
473                         get_action_name(entry, &item[2]);
474                 } else {
475                         item[2] = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->entry_icon_text)));
476                 }
477
478                 gtk_clist_remove(clist_set, row_set);
479                 row_set = gtk_clist_insert(clist_set, row_set, item);
480                 gtk_clist_set_pixmap(clist_set, row_set, 0, xpm, xpmmask);
481         }
482         
483         gtk_clist_moveto(clist_set, row_set, 0, row_set/clist_set->rows, 0);
484         gtk_clist_select_row(clist_set, row_set, 0);
485
486         g_free(item[2]);
487         g_free(item[3]);
488 }
489
490 static void prefs_toolbar_delete(GtkButton *button, ToolbarPage *prefs_toolbar)
491 {
492         GtkCList *clist_set = GTK_CLIST(prefs_toolbar->clist_set);
493         gint row_set = 0;
494
495         if (clist_set->rows == 0) return; 
496         if (clist_set->selection) {
497                 if (clist_set->selection->data) 
498                         row_set = GPOINTER_TO_INT(clist_set->selection->data);
499         } else
500                 return;
501         
502         if (clist_set->row_list != NULL) {
503                         
504                 row_set = GPOINTER_TO_INT(clist_set->selection->data);
505                 gtk_clist_remove(clist_set, row_set);
506                 gtk_clist_columns_autosize(clist_set);
507                 
508                 if (clist_set->rows > 0)
509                         gtk_clist_select_row(clist_set,(row_set == 0) ? 0:row_set - 1, 0);
510         }
511 }
512
513 static void prefs_toolbar_up(GtkButton *button, ToolbarPage *prefs_toolbar)
514 {
515         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
516         gint row = 0;
517
518         if (!clist->selection) return;
519         if (clist->selection->data)
520                 row = GPOINTER_TO_INT(clist->selection->data);
521
522         if (row >= 0) {
523                 gtk_clist_row_move(clist, row, row - 1);
524                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
525                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
526                 } 
527         }
528 }
529
530 static void prefs_toolbar_down(GtkButton *button, ToolbarPage *prefs_toolbar)
531 {
532         GtkCList *clist = GTK_CLIST(prefs_toolbar->clist_set);
533         gint row = 0;
534
535         if (!clist->selection) return;
536         if (clist->selection->data)
537                 row = GPOINTER_TO_INT(clist->selection->data);
538
539         if (row >= 0 && row < clist->rows - 1) {
540                 gtk_clist_row_move(clist, row, row + 1);
541                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
542                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
543                 } 
544         }
545 }
546
547 static void prefs_toolbar_select_row_set(GtkCList *clist, gint row, gint column,
548                                          GdkEvent *event, ToolbarPage *prefs_toolbar)
549 {
550         GtkCList *clist_ico = GTK_CLIST(prefs_toolbar->clist_icons);
551         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
552         gint row_set = 0;
553         gint row_ico = 0;
554         gint item_num = 0;
555         gchar *file, *icon_text, *descr, *entry;
556         GList *cur;
557
558         if (clist->selection->data) 
559                 row_set = GPOINTER_TO_INT(clist->selection->data);      
560
561         gtk_clist_get_text(clist, row_set, 1, &file);
562         gtk_clist_get_text(clist, row_set, 2, &icon_text);
563         gtk_clist_get_text(clist, row_set, 3, &descr);
564
565         if (g_strcasecmp(descr, syl_act) != 0)
566                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->entry_icon_text), icon_text);
567         
568         /* scan combo list for selected description an set combo item accordingly */
569         for (cur = prefs_toolbar->combo_action_list; cur != NULL; cur = cur->next) {
570                 gchar *item_str = (gchar*)cur->data;
571                 if (g_strcasecmp(item_str, descr) == 0) {
572                         gtk_list_select_item(GTK_LIST(prefs_toolbar->combo_list), item_num);
573                         break;
574                 }
575                 else
576                         item_num++;
577         }
578
579         do {
580                 gtk_clist_get_text(clist_ico, row_ico, 1, &entry);
581                 row_ico++;
582         } while(g_strcasecmp(entry, file) != 0);
583         
584         gtk_clist_select_row(clist_ico, row_ico - 1, 0);
585         gtk_clist_moveto(clist_ico, row_ico - 1, 0, row_ico/clist_ico->rows, 0);
586 }
587
588 static void prefs_toolbar_select_row_icons(GtkCList *clist, gint row, gint column,
589                                            GdkEvent *event, ToolbarPage *prefs_toolbar)
590 {
591         GtkCList *clist_icons = GTK_CLIST(prefs_toolbar->clist_icons);
592         gchar *text;
593         
594         gtk_clist_get_text(clist_icons, row, 1, &text);
595
596         if (!text) return;
597
598         if (g_strcasecmp(toolbar_ret_descr_from_val(A_SEPARATOR), text) == 0) {
599                 gtk_widget_set_sensitive(prefs_toolbar->combo_action,     FALSE);
600                 gtk_widget_set_sensitive(prefs_toolbar->entry_icon_text,  FALSE);
601                 gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, FALSE);
602         } else {
603                 gtk_widget_set_sensitive(prefs_toolbar->combo_action,     TRUE);
604                 gtk_widget_set_sensitive(prefs_toolbar->entry_icon_text,  TRUE);
605                 gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, TRUE);
606         }
607 }
608
609 static void prefs_toolbar_selection_changed(GtkList *list,
610                                             ToolbarPage *prefs_toolbar)
611 {
612         gchar *cur_entry = g_strdup(gtk_entry_get_text(GTK_ENTRY(prefs_toolbar->combo_entry)));
613         gchar *actions_entry = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
614
615         gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, TRUE);
616
617         if (g_strcasecmp(cur_entry, actions_entry) == 0) {
618                 gtk_widget_hide(prefs_toolbar->entry_icon_text);
619                 gtk_widget_show(prefs_toolbar->combo_syl_action);
620                 gtk_label_set_text(GTK_LABEL(prefs_toolbar->label_icon_text), _("Sylpheed Action"));
621
622                 if (prefs_common.actions_list == NULL) {
623                     gtk_widget_set_sensitive(prefs_toolbar->combo_syl_action, FALSE);
624                 }
625
626         } else {
627                 gtk_widget_hide(prefs_toolbar->combo_syl_action);
628                 gtk_widget_show(prefs_toolbar->entry_icon_text);
629                 gtk_label_set_text(GTK_LABEL(prefs_toolbar->label_icon_text), _("Toolbar text"));
630         }
631
632         gtk_misc_set_alignment(GTK_MISC(prefs_toolbar->label_icon_text), 1, 0.5);
633         gtk_widget_show(prefs_toolbar->label_icon_text);
634         g_free(cur_entry);
635 }
636
637 static void prefs_toolbar_create(ToolbarPage *prefs_toolbar)
638 {
639         GtkWidget *main_vbox;
640         GtkWidget *top_hbox;
641         GtkWidget *compose_frame;
642         GtkWidget *reg_hbox;
643         GtkWidget *arrow;
644         GtkWidget *btn_hbox;
645         GtkWidget *reg_btn;
646         GtkWidget *subst_btn;
647         GtkWidget *del_btn;
648         GtkWidget *default_btn;
649         GtkWidget *vbox_frame;
650         GtkWidget *table;
651         GtkWidget *scrolledwindow_clist_icon;
652         GtkWidget *clist_icons;
653         GtkWidget *label_icon_text;
654         GtkWidget *entry_icon_text;
655         GtkWidget *label_action_sel;
656         GtkWidget *empty_label;
657         GtkWidget *combo_action;
658         GtkWidget *combo_entry;
659         GtkWidget *combo_list;
660         GtkWidget *combo_syl_action;
661         GtkWidget *combo_syl_entry;
662         GtkWidget *combo_syl_list;
663         GtkWidget *frame_toolbar_items;
664         GtkWidget *hbox_bottom;
665         GtkWidget *scrolledwindow_clist_set;
666         GtkWidget *clist_set;
667
668         GtkWidget *btn_vbox;
669         GtkWidget *up_btn;
670         GtkWidget *down_btn;
671         
672         gchar *titles[N_DISPLAYED_ITEMS_COLS];
673  
674         debug_print("Creating custom toolbar window...\n");
675
676         main_vbox = gtk_vbox_new(FALSE, 0);
677         gtk_widget_show(main_vbox);
678         
679         top_hbox = gtk_hbox_new(FALSE, 0);
680         gtk_box_pack_start(GTK_BOX(main_vbox), top_hbox, TRUE, TRUE, 0);
681   
682         compose_frame = gtk_frame_new(_("Available toolbar icons"));
683         gtk_box_pack_start(GTK_BOX(top_hbox), compose_frame, TRUE, TRUE, 0);
684         gtk_container_set_border_width(GTK_CONTAINER(compose_frame), 5);
685
686         vbox_frame = gtk_vbox_new(FALSE, 0);
687         gtk_container_add(GTK_CONTAINER(compose_frame), vbox_frame);
688         
689         /* available icons */
690         scrolledwindow_clist_icon = gtk_scrolled_window_new(NULL, NULL);
691         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_clist_icon), 5);
692         gtk_container_add(GTK_CONTAINER(vbox_frame), scrolledwindow_clist_icon);
693         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_clist_icon), 
694                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
695         
696         clist_icons = gtk_clist_new(2);
697         gtk_container_add(GTK_CONTAINER(scrolledwindow_clist_icon), clist_icons);
698         gtk_container_set_border_width(GTK_CONTAINER(clist_icons), 1);
699         gtk_clist_set_column_width(GTK_CLIST(clist_icons), 0, 35);
700         gtk_clist_set_column_width(GTK_CLIST(clist_icons), 1, 200);
701         gtk_clist_column_titles_hide(GTK_CLIST(clist_icons));
702         gtk_widget_set_usize(clist_icons, 225, 108); 
703
704         table = gtk_table_new (2, 3, FALSE);
705         gtk_container_add (GTK_CONTAINER (vbox_frame), table);
706         gtk_container_set_border_width (GTK_CONTAINER (table), 8);
707         gtk_table_set_row_spacings (GTK_TABLE (table), 8);
708         gtk_table_set_col_spacings (GTK_TABLE (table), 8);
709         
710         /* icon description */
711         label_icon_text = gtk_label_new("");
712         gtk_misc_set_alignment(GTK_MISC(label_icon_text), 0, 0.5);
713         gtk_widget_show (label_icon_text);
714         gtk_table_attach (GTK_TABLE (table), label_icon_text, 0, 1, 0, 1,
715                           (GtkAttachOptions) (GTK_FILL),
716                           (GtkAttachOptions) (0), 0, 0);
717
718         entry_icon_text = gtk_entry_new();
719         gtk_table_attach (GTK_TABLE (table), entry_icon_text, 1, 2, 0, 1,
720                           (GtkAttachOptions) (/*GTK_EXPAND | */GTK_FILL),
721                           (GtkAttachOptions) (0), 0, 0);
722
723         /* Sylpheed Action Combo Box */
724         combo_syl_action = gtk_combo_new();
725         combo_syl_list = GTK_COMBO(combo_syl_action)->list;
726         combo_syl_entry = GTK_COMBO(combo_syl_action)->entry;
727         gtk_entry_set_editable(GTK_ENTRY(combo_syl_entry), FALSE);
728         gtk_table_attach (GTK_TABLE (table), combo_syl_action, 1, 2, 0, 1,
729                           (GtkAttachOptions) (/*GTK_EXPAND | */GTK_FILL),
730                           (GtkAttachOptions) (0), 0, 0);
731
732         empty_label = gtk_label_new("");
733         gtk_table_attach (GTK_TABLE (table), empty_label, 2, 3, 0, 1,
734                           (GtkAttachOptions) (GTK_EXPAND),
735                           (GtkAttachOptions) (0), 0, 0);
736         /* available actions */
737         label_action_sel = gtk_label_new(_("Event executed on click"));
738         gtk_misc_set_alignment(GTK_MISC(label_action_sel), 0, 0.5);
739         gtk_table_attach (GTK_TABLE (table), label_action_sel, 0, 1, 1, 2,
740                           (GtkAttachOptions) (GTK_FILL),
741                           (GtkAttachOptions) (0), 0, 0);
742
743         combo_action = gtk_combo_new();
744         gtk_table_attach (GTK_TABLE (table), combo_action, 1, 2, 1, 2,
745                           (GtkAttachOptions) (GTK_FILL),
746                           (GtkAttachOptions) (0), 0, 0);
747         
748         combo_list = GTK_COMBO(combo_action)->list;
749         combo_entry = GTK_COMBO(combo_action)->entry;
750         gtk_entry_set_editable(GTK_ENTRY(combo_entry), FALSE);
751         
752         empty_label = gtk_label_new("");
753         gtk_table_attach (GTK_TABLE (table), empty_label, 2, 3, 1, 2,
754                           (GtkAttachOptions) (GTK_EXPAND),
755                           (GtkAttachOptions) (0), 0, 0);
756
757         /* register / substitute / delete */
758         reg_hbox = gtk_hbox_new(FALSE, 4);
759         gtk_box_pack_start(GTK_BOX(main_vbox), reg_hbox, FALSE, FALSE, 0);
760         gtk_container_set_border_width(GTK_CONTAINER(reg_hbox), 10);
761
762         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
763         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
764         gtk_widget_set_usize(arrow, -1, 16);
765
766         btn_hbox = gtk_hbox_new(TRUE, 4);
767         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
768
769         reg_btn = gtk_button_new_with_label(_("Add"));
770         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
771         gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
772                             GTK_SIGNAL_FUNC(prefs_toolbar_register), 
773                             prefs_toolbar);
774
775         subst_btn = gtk_button_new_with_label(_("  Replace  "));
776         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
777         gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
778                             GTK_SIGNAL_FUNC(prefs_toolbar_substitute),
779                             prefs_toolbar);
780
781         del_btn = gtk_button_new_with_label(_("Delete"));
782         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
783         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
784                             GTK_SIGNAL_FUNC(prefs_toolbar_delete), 
785                             prefs_toolbar);
786
787         default_btn = gtk_button_new_with_label(_(" Default "));
788         gtk_box_pack_end(GTK_BOX(reg_hbox), default_btn, FALSE, TRUE, 0);
789         gtk_signal_connect(GTK_OBJECT(default_btn), "clicked",
790                             GTK_SIGNAL_FUNC(prefs_toolbar_default), 
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(void)
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->source = TOOLBAR_MAIN;
878         prefs_gtk_register_page((PrefsPage *) page);
879         prefs_toolbar_mainwindow = page;
880
881         page = g_new0(ToolbarPage, 1);
882         page->page.path = _("Customize Toolbars/Message Window");
883         page->page.create_widget = prefs_toolbar_create_widget;
884         page->page.destroy_widget = prefs_toolbar_destroy_widget;
885         page->page.save_page = prefs_toolbar_save;
886         page->source = TOOLBAR_MSGVIEW;
887         prefs_gtk_register_page((PrefsPage *) page);
888         prefs_toolbar_messageview = page;
889
890         page = g_new0(ToolbarPage, 1);
891         page->page.path = _("Customize Toolbars/Compose Window");
892         page->page.create_widget = prefs_toolbar_create_widget;
893         page->page.destroy_widget = prefs_toolbar_destroy_widget;
894         page->page.save_page = prefs_toolbar_save;
895         page->source = TOOLBAR_COMPOSE;
896         prefs_gtk_register_page((PrefsPage *) page);
897         prefs_toolbar_composewindow = page;
898 }
899
900 void prefs_toolbar_done(void)
901 {
902         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_mainwindow);
903         g_free(prefs_toolbar_mainwindow);
904         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_composewindow);
905         g_free(prefs_toolbar_composewindow);
906         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_messageview);
907         g_free(prefs_toolbar_messageview);
908 }