409f05c6ef8408c3b519131f7fecfcc531c16c32
[claws.git] / src / prefs_toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2007 Hiroyuki Yamamoto & the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 <glib/gi18n.h>
32 #include <gtk/gtk.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
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 enum
53 {
54         SET_ICON          = 0,
55         SET_FILENAME      = 1,
56         SET_TEXT          = 2,
57         SET_EVENT         = 3,
58         SET_ICON_TEXT     = 4,          /*!< "icon" text (separator) */ 
59         SET_ICON_IS_TEXT  = 5,          /*!< icon is text representation */
60         N_SET_COLUMNS
61 };
62
63 enum
64 {
65         ITEM_FUNCTION     = 0,
66         ITEM_USER_ACTION  = 1,
67         ITEM_SEPARATOR    = 2
68 };
69
70 typedef struct _ToolbarPage
71 {
72         PrefsPage  page;
73
74         GtkWidget *window;              /* do not modify */
75
76         ToolbarType source;
77         GList     *combo_action_list;   /* list of internal functions    */
78
79         GtkWidget *list_view_set;       /* toolbar items treeview        */
80         GtkWidget *item_text_entry;     /* item name                     */
81         GtkWidget *item_type_combo;     /* item type selection widget    */
82         GtkWidget *item_func_combo;     /* item internal function widget */
83         GtkWidget *item_action_combo;   /* item user action widget       */
84         GtkWidget *icon_button;         /* item icon chooser widget      */
85         
86         GtkWidget *icon_chooser_win;
87         GtkWidget *icon_chooser_list;
88         
89         gchar *item_icon_file;          /* item icon file                */
90
91 } ToolbarPage;
92
93 #define ERROR_MSG _("Selected Action already set.\nPlease choose another Action from List")
94 #define ERROR_MSG_NO_ICON _("Item has no icon defined.")
95 #define ERROR_MSG_NO_TEXT _("Item has no text defined.")
96
97 static void prefs_toolbar_populate               (ToolbarPage *prefs_toolbar);
98
99 static void get_action_name                      (const gchar *entry, 
100                                                   gchar **menu);
101                                                   
102 static gboolean is_duplicate                     (ToolbarPage *prefs_toolbar,
103                                                   gchar            *chosen_action);
104 static void prefs_toolbar_save                   (PrefsPage        *_page);
105
106 static void prefs_toolbar_register               (GtkButton        *button,
107                                                   ToolbarPage *prefs_toolbar);
108 static void prefs_toolbar_substitute             (GtkButton        *button,
109                                                   ToolbarPage *prefs_toolbar);
110 static void prefs_toolbar_delete                 (GtkButton        *button,
111                                                   ToolbarPage *prefs_toolbar);
112
113 static void prefs_toolbar_up                     (GtkButton        *button,
114                                                   ToolbarPage *prefs_toolbar);
115
116 static void prefs_toolbar_down                   (GtkButton        *button,
117                                                   ToolbarPage *prefs_toolbar);
118
119 static void action_selection_changed             (GtkComboBox *action_combo,
120                                                   ToolbarPage *prefs_toolbar);
121
122 static void func_selection_changed               (GtkComboBox *action_combo,
123                                                   ToolbarPage *prefs_toolbar);
124
125 static void prefs_toolbar_create                 (ToolbarPage *prefs_toolbar);
126
127 static GtkWidget *create_set_list_view           (ToolbarPage *prefs_toolbar);
128
129 static gboolean set_list_selected                (GtkTreeSelection *selector,
130                                                   GtkTreeModel *model, 
131                                                   GtkTreePath *path,
132                                                   gboolean currently_selected,
133                                                   ToolbarPage *prefs_toolbar);
134
135 static void icon_chooser_create                  (GtkButton *button,
136                                                   ToolbarPage *prefs_toolbar);
137
138 static void prefs_toolbar_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
139 {
140         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
141         gchar *win_titles[3];
142         win_titles[TOOLBAR_MAIN]    = _("Main toolbar configuration");
143         win_titles[TOOLBAR_COMPOSE] = _("Compose toolbar configuration");  
144         win_titles[TOOLBAR_MSGVIEW] = _("Message view toolbar configuration");  
145
146         prefs_toolbar->window = GTK_WIDGET(window);
147
148         toolbar_read_config_file(prefs_toolbar->source);
149
150         prefs_toolbar_create(prefs_toolbar);
151         prefs_toolbar_populate(prefs_toolbar);
152 }
153
154 static void prefs_toolbar_save(PrefsPage *_page)
155 {
156         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
157         GtkTreeView *list_view = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
158         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
159         GtkTreeIter iter;
160         
161         toolbar_clear_list(prefs_toolbar->source);
162
163         if (!gtk_tree_model_iter_n_children(model, NULL)
164         ||  !gtk_tree_model_get_iter_first(model, &iter))
165                 toolbar_set_default(prefs_toolbar->source);
166         else {
167                 do {
168                         ToolbarItem *item;
169                         gchar *fname, *text, *event; 
170                         
171                         item = g_new0(ToolbarItem, 1);
172
173                         gtk_tree_model_get(model, &iter,
174                                            SET_FILENAME, &fname,
175                                            SET_TEXT, &text,
176                                            SET_EVENT, &event,
177                                            -1);
178
179                         /* XXX: remember that G_TYPE_STRING returned by model
180                          * is owned by caller of gtk_tree_model_get() */
181                         item->file  = fname;
182                         item->text  = text;
183                         item->index = toolbar_ret_val_from_descr(event);
184                         g_free(event);
185
186                         /* TODO: save A_SYL_ACTIONS only if they are still active */
187                         toolbar_set_list_item(item, prefs_toolbar->source);
188
189                         g_free(item->file);
190                         g_free(item->text);
191                         g_free(item);
192                 } while (gtk_tree_model_iter_next(model, &iter));
193         }
194
195         toolbar_save_config_file(prefs_toolbar->source);
196
197         if (prefs_toolbar->source == TOOLBAR_MAIN) 
198                 main_window_reflect_prefs_all_real(TRUE);
199         else if (prefs_toolbar->source == TOOLBAR_COMPOSE)
200                 compose_reflect_prefs_pixmap_theme();
201         else if (prefs_toolbar->source == TOOLBAR_MSGVIEW)
202                 messageview_reflect_prefs_pixmap_theme();
203 }
204
205 static void prefs_toolbar_destroy_widget(PrefsPage *_page)
206 {
207         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
208
209         g_list_free(prefs_toolbar->combo_action_list);
210         prefs_toolbar->combo_action_list = NULL;
211 }
212
213 static void prefs_toolbar_set_displayed(ToolbarPage *prefs_toolbar)
214 {
215         GSList *cur;
216         GtkTreeView *list_view_set = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
217         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
218                                                 (list_view_set));
219         GSList *toolbar_list = toolbar_get_list(prefs_toolbar->source);
220         GtkTreeIter iter;
221
222         gtk_list_store_clear(store);
223
224         /* set currently active toolbar entries */
225         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
226                 ToolbarItem *item = (ToolbarItem*) cur->data;
227
228                 gtk_list_store_append(store, &iter);
229         
230                 if (item->index != A_SEPARATOR) {
231                         GdkPixbuf *pix;
232                         StockPixmap icon = stock_pixmap_get_icon(item->file);
233                         
234                         stock_pixbuf_gdk(prefs_toolbar->window, icon, &pix);
235
236                         gtk_list_store_set(store, &iter, 
237                                            SET_ICON, pix,
238                                            SET_FILENAME, item->file,
239                                            SET_TEXT, item->text,
240                                            SET_EVENT, toolbar_ret_descr_from_val(item->index),
241                                            SET_ICON_TEXT, NULL, 
242                                            SET_ICON_IS_TEXT, FALSE,
243                                            -1);
244                 } else {
245                         gtk_list_store_set(store, &iter,
246                                            SET_ICON, NULL,
247                                            SET_FILENAME, toolbar_ret_descr_from_val(A_SEPARATOR),
248                                            SET_TEXT, (const gchar *) "", 
249                                            SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
250                                            SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
251                                            SET_ICON_IS_TEXT, TRUE,
252                                            -1);
253                 }
254         }
255
256         /* select first */
257         gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
258         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
259                                                 (list_view_set),
260                                        &iter);  
261 }
262
263 static void prefs_toolbar_populate(ToolbarPage *prefs_toolbar)
264 {
265         GList *cur;
266         GSList *cur2;
267         gchar *act, *act_name;
268
269         gtk_combo_box_append_text(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
270                                   _("Internal Function"));      
271         gtk_combo_box_append_text(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
272                                   _("User Action"));
273         gtk_combo_box_append_text(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
274                                   _("Separator"));
275         
276         prefs_toolbar->combo_action_list = toolbar_get_action_items(prefs_toolbar->source);
277         for(cur = prefs_toolbar->combo_action_list; cur != NULL; cur = cur->next) {
278                 act = (gchar *)cur->data;
279                 gtk_combo_box_append_text(GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
280                                           act);
281         }
282
283         /* get currently defined sylpheed actions */
284         if (prefs_common.actions_list != NULL) {
285                 for (cur2 = prefs_common.actions_list; cur2 != NULL; cur2 = cur2->next) {
286                         act = (gchar *)cur2->data;
287                         get_action_name(act, &act_name);
288                         
289                         gtk_combo_box_append_text(
290                                 GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
291                                 act_name);
292
293                         g_free(act_name);
294                 } 
295
296         }
297         
298         gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo), 0);
299         gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_func_combo), 0);
300         gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_action_combo), 0);
301         
302         prefs_toolbar_set_displayed(prefs_toolbar);
303
304         toolbar_clear_list(prefs_toolbar->source);
305 }
306
307 static gboolean is_duplicate(ToolbarPage *prefs_toolbar, gchar *chosen_action)
308 {
309         GtkTreeView *list_view_set = GTK_TREE_VIEW
310                                         (prefs_toolbar->list_view_set);
311         GtkTreeModel *model_set = gtk_tree_view_get_model(list_view_set);                                       
312         gchar *entry;
313         GtkTreeIter iter;
314         gboolean result;
315
316         g_return_val_if_fail(chosen_action != NULL, TRUE);
317
318         if (!gtk_tree_model_iter_n_children(model_set, NULL))
319                 return FALSE;
320         
321         if (!gtk_tree_model_get_iter_first(model_set, &iter))
322                 return FALSE;
323
324         result = FALSE;
325         do {
326                 gtk_tree_model_get(model_set, &iter,
327                                    SET_EVENT, &entry, 
328                                    -1);
329                 if (g_utf8_collate(chosen_action, entry) == 0) 
330                         result = TRUE;
331                 g_free(entry);                  
332         } while (!result && gtk_tree_model_iter_next(model_set, &iter));
333
334         return result;
335 }
336
337 static void prefs_toolbar_default(GtkButton *button, ToolbarPage *prefs_toolbar)
338 {
339         toolbar_clear_list(prefs_toolbar->source);
340         toolbar_set_default(prefs_toolbar->source);
341         prefs_toolbar_set_displayed(prefs_toolbar);
342 }
343
344 /*!
345  *\return       String that should be freed by caller.
346  */
347 static void get_action_name(const gchar *entry, gchar **menu)
348 {
349         gchar *act, *act_p;
350         
351         *menu = NULL;
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         GtkTreeView *list_view_set   = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
367         gint item_type = gtk_combo_box_get_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo));
368         GtkListStore *store_set;
369         GtkTreeIter iter;
370
371         store_set = GTK_LIST_STORE(gtk_tree_view_get_model(list_view_set));
372
373         /* SEPARATOR or other ? */
374         if (item_type == ITEM_SEPARATOR) {
375                 gtk_list_store_append(store_set, &iter);
376                 gtk_list_store_set(store_set, &iter,
377                                    SET_ICON, NULL,
378                                    SET_FILENAME, prefs_toolbar->item_icon_file,
379                                    SET_TEXT, NULL,
380                                    SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
381                                    SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
382                                    SET_ICON_IS_TEXT, TRUE,
383                                    -1);
384         } else {
385                 GdkPixbuf *pixbuf;
386                 gchar *event, *text;
387
388                 if (prefs_toolbar->item_icon_file == NULL) {
389                         alertpanel_error(ERROR_MSG_NO_ICON);
390                         return;
391                 }
392                 stock_pixbuf_gdk(prefs_toolbar->window, 
393                                  stock_pixmap_get_icon(prefs_toolbar->item_icon_file),
394                                  &pixbuf);
395                 if(pixbuf == NULL) {
396                         alertpanel_error(ERROR_MSG_NO_ICON);
397                         return;
398                 }
399                                 
400                 if (item_type == ITEM_FUNCTION) {
401                         event = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
402                                                 prefs_toolbar->item_func_combo));
403                                                 
404                         if (is_duplicate(prefs_toolbar, event)) {
405                                 alertpanel_error(ERROR_MSG);
406                                 g_free(event);
407                                 return;
408                         }
409                 } else
410                         event = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
411                 
412                 text = gtk_editable_get_chars(
413                         GTK_EDITABLE(prefs_toolbar->item_text_entry), 0 , -1);
414
415                 if (text != NULL) {
416                         gtk_list_store_append(store_set, &iter);
417                         gtk_list_store_set(store_set, &iter,
418                                            SET_ICON, pixbuf,
419                                            SET_FILENAME, prefs_toolbar->item_icon_file,
420                                            SET_TEXT, text,
421                                            SET_EVENT, event,
422                                            SET_ICON_TEXT, NULL,
423                                            SET_ICON_IS_TEXT, FALSE,
424                                            -1);
425                 } else {
426                         alertpanel_error(ERROR_MSG_NO_TEXT);
427                         return;
428                 }
429                 
430                 g_free(text);
431                 if(item_type != ITEM_USER_ACTION)
432                         g_free(event);
433         }
434         
435         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
436                                                 (list_view_set),
437                                        &iter);
438 }
439
440 static void prefs_toolbar_substitute(GtkButton *button, ToolbarPage *prefs_toolbar)
441 {
442         GtkTreeView *list_view_set   = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
443         GtkListStore *store_set   = GTK_LIST_STORE(gtk_tree_view_get_model(list_view_set));
444         gint item_type = gtk_combo_box_get_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo));
445         GtkTreeSelection *sel_set;
446         GtkTreeIter iter_set;
447
448         if (!gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_set), NULL))
449                 return;
450                 
451         sel_set = gtk_tree_view_get_selection(list_view_set);           
452         if (!gtk_tree_selection_get_selected(sel_set, NULL, &iter_set))
453                 return;
454
455         if (item_type == ITEM_SEPARATOR) {
456                 gtk_list_store_set(store_set, &iter_set, 
457                                    SET_ICON, NULL,
458                                    SET_TEXT, NULL,
459                                    SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
460                                    SET_FILENAME, prefs_toolbar->item_icon_file,
461                                    SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
462                                    SET_ICON_IS_TEXT, TRUE,
463                                    -1);
464         } else {
465                 GdkPixbuf *pixbuf;
466                 gchar *icon_event, *set_event, *text;
467
468                 if (prefs_toolbar->item_icon_file == NULL) {
469                         alertpanel_error(ERROR_MSG_NO_ICON);
470                         return;
471                 }
472                 stock_pixbuf_gdk(prefs_toolbar->window, 
473                                  stock_pixmap_get_icon(prefs_toolbar->item_icon_file),
474                                  &pixbuf);
475                 if(pixbuf == NULL) {
476                         alertpanel_error(ERROR_MSG_NO_ICON);
477                         return;
478                 }
479
480                 gtk_tree_model_get(GTK_TREE_MODEL(store_set), &iter_set, 
481                                                   SET_EVENT, &set_event,
482                                                   -1);
483                 
484                 if (item_type == ITEM_FUNCTION) {
485                         icon_event = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
486                                                 prefs_toolbar->item_func_combo));
487                                                 
488                         if (is_duplicate(prefs_toolbar, icon_event)
489                         && g_utf8_collate(icon_event, set_event) != 0){
490                                 alertpanel_error(ERROR_MSG);
491                                 g_free(icon_event);
492                                 g_free(set_event);
493                                 return;
494                         }
495                 } else
496                         icon_event = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
497                 
498                 text = gtk_editable_get_chars(
499                         GTK_EDITABLE(prefs_toolbar->item_text_entry), 0 , -1);
500
501                 /* change the row */
502                 if (text != NULL) {
503                         gtk_list_store_set(store_set, &iter_set,
504                                            SET_ICON, pixbuf,
505                                            SET_FILENAME, prefs_toolbar->item_icon_file,
506                                            SET_TEXT, text,
507                                            SET_EVENT, icon_event,
508                                            SET_ICON_TEXT, NULL,
509                                            SET_ICON_IS_TEXT, FALSE,
510                                            -1);
511                 } else 
512                         alertpanel_error(ERROR_MSG_NO_TEXT);
513                                 
514                 g_free(text);
515                 g_free(set_event);
516                 if(item_type != ITEM_USER_ACTION)
517                         g_free(icon_event);             
518         }
519 }
520
521 static void prefs_toolbar_delete(GtkButton *button, ToolbarPage *prefs_toolbar)
522 {
523         GtkTreeView *list_view_set = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
524         GtkTreeModel *store_set = gtk_tree_view_get_model(list_view_set);
525         GtkTreeIter iter_set;
526         GtkTreePath *path;                                                      
527
528         if (!gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_set), NULL))
529                 return;
530         
531         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
532                                                         (list_view_set),
533                                              NULL,
534                                              &iter_set))
535                 return;                                      
536
537         /* select prev list item, if deleted was first select next */
538         path = gtk_tree_model_get_path(store_set, &iter_set);
539         gtk_tree_path_prev(path);
540         
541         gtk_list_store_remove(GTK_LIST_STORE(store_set), &iter_set);
542
543         gtk_tree_selection_select_path(
544                         gtk_tree_view_get_selection(list_view_set),
545                         path);
546
547         gtk_tree_path_free(path);
548 }
549
550 static void prefs_toolbar_up(GtkButton *button, ToolbarPage *prefs_toolbar)
551 {
552         GtkTreePath *prev, *sel;
553         GtkTreeIter isel;
554         GtkListStore *store = NULL;
555         GtkTreeModel *model = NULL;
556         GtkTreeIter iprev;
557         
558         if (!gtk_tree_selection_get_selected
559                 (gtk_tree_view_get_selection
560                         (GTK_TREE_VIEW(prefs_toolbar->list_view_set)),
561                  &model,        
562                  &isel))
563                 return;
564         store = (GtkListStore *)model;
565
566         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
567         if (!sel)
568                 return;
569         
570         /* no move if we're at row 0... */
571         prev = gtk_tree_path_copy(sel);
572         if (!gtk_tree_path_prev(prev)) {
573                 gtk_tree_path_free(prev);
574                 gtk_tree_path_free(sel);
575                 return;
576         }
577
578         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
579                                 &iprev, prev);
580         gtk_tree_path_free(sel);
581         gtk_tree_path_free(prev);
582
583         gtk_list_store_swap(store, &iprev, &isel);
584 }
585
586 static void prefs_toolbar_down(GtkButton *button, ToolbarPage *prefs_toolbar)
587 {
588         GtkListStore *store = NULL;
589         GtkTreeModel *model = NULL;
590         GtkTreeIter next, sel;
591         
592         if (!gtk_tree_selection_get_selected
593                 (gtk_tree_view_get_selection
594                         (GTK_TREE_VIEW(prefs_toolbar->list_view_set)),
595                  &model,
596                  &sel))
597                 return;
598
599         store = (GtkListStore *)model;
600         next = sel;
601         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
602                 return;
603
604         gtk_list_store_swap(store, &next, &sel);
605 }
606
607 static void item_type_changed(GtkComboBox *item_type_combo,
608                                 ToolbarPage *prefs_toolbar)
609 {
610         gint active = gtk_combo_box_get_active(item_type_combo);
611
612         switch(active) {
613         case ITEM_FUNCTION:
614                 gtk_widget_show(prefs_toolbar->item_func_combo);
615                 gtk_widget_hide(prefs_toolbar->item_action_combo);
616                 gtk_combo_box_set_active(
617                         GTK_COMBO_BOX(prefs_toolbar->item_func_combo), 0);
618                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
619                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, TRUE);
620                 gtk_widget_set_sensitive(prefs_toolbar->item_func_combo, TRUE);
621                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
622
623                 func_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
624                                         prefs_toolbar);
625                 break;
626         case ITEM_USER_ACTION:
627                 gtk_widget_show(prefs_toolbar->item_action_combo);
628                 gtk_widget_hide(prefs_toolbar->item_func_combo);
629                 gtk_combo_box_set_active(
630                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo), 0);
631                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
632                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
633                 gtk_widget_set_sensitive(prefs_toolbar->item_action_combo, TRUE);
634                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
635                 
636                 action_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
637                                         prefs_toolbar);         
638                 break;
639         case ITEM_SEPARATOR:
640                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), _("None"));
641                 gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button), NULL);
642                 g_free(prefs_toolbar->item_icon_file);
643                 prefs_toolbar->item_icon_file = NULL;
644                 gtk_combo_box_set_active(
645                         GTK_COMBO_BOX(prefs_toolbar->item_func_combo), -1);
646                 gtk_combo_box_set_active(
647                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo), -1);
648                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), "");
649                 gtk_widget_set_sensitive(prefs_toolbar->item_action_combo, FALSE);
650                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
651                 gtk_widget_set_sensitive(prefs_toolbar->item_func_combo, FALSE);
652                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, FALSE);
653                 break;
654         }
655
656 }
657
658 static void action_selection_changed(GtkComboBox *action_combo,
659                                 ToolbarPage *prefs_toolbar)
660 {
661         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
662                            prefs_toolbar->item_action_combo));
663
664         if(text != NULL) { /* action */
665                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), text);
666                 g_free(text);
667         } 
668 }
669
670 static void func_selection_changed(GtkComboBox *action_combo,
671                                 ToolbarPage *prefs_toolbar)
672 {
673         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
674                            prefs_toolbar->item_func_combo));
675
676         if(text != NULL) { /* action */
677                 int action = -1;
678                 action = toolbar_ret_val_from_descr(text);
679                 if (action >= 0)
680                         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), 
681                                         toolbar_get_short_text(action));
682                 g_free(text);
683                 if (action >= 0) {
684                         StockPixmap stockp = toolbar_get_icon(action);
685                         if (stockp >= 0)  {
686                                 g_free(prefs_toolbar->item_icon_file);
687                                 prefs_toolbar->item_icon_file = g_strdup(stock_pixmap_get_name(stockp));
688
689                                 gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
690                                      stock_pixmap_widget(prefs_toolbar->window, stockp));
691                         }
692                 }
693         } 
694 }
695
696 static void prefs_toolbar_create(ToolbarPage *prefs_toolbar)
697 {
698         GtkWidget *main_vbox;
699         GtkWidget *toolbar_item_hbox;
700         GtkWidget *icon_vbox;
701         GtkWidget *icon_label;
702         GtkWidget *icon_button;
703         GtkWidget *icon_hbox;
704         GtkWidget *item_type_combo;
705         GtkWidget *item_action_combo;
706         GtkWidget *item_func_combo;
707         GtkWidget *reg_hbox;
708         GtkWidget *arrow;
709         GtkWidget *btn_hbox;
710 #ifdef MAEMO
711         GtkWidget *hbox;
712 #endif
713         GtkWidget *reg_btn;
714         GtkWidget *subst_btn;
715         GtkWidget *del_btn;
716         GtkWidget *default_btn;
717         GtkWidget *vbox_frame;
718         GtkWidget *table;
719         GtkWidget *label_icon_text;
720         GtkWidget *item_text_entry;
721         GtkWidget *vbox_toolbar_items;
722         GtkWidget *hbox_bottom;
723         GtkWidget *scrolledwindow_list_view_set;
724         GtkWidget *list_view_set;
725         GtkWidget *label;
726
727         GtkWidget *btn_vbox;
728         GtkWidget *up_btn;
729         GtkWidget *down_btn;
730
731         debug_print("Creating custom toolbar window...\n");
732
733         main_vbox = gtk_vbox_new(FALSE, 0);
734         gtk_widget_show(main_vbox);
735
736         vbox_frame = gtk_frame_new(_("Toolbar item"));
737         gtk_widget_show(vbox_frame);
738         gtk_box_pack_start(GTK_BOX(main_vbox), vbox_frame, FALSE, TRUE, 0);
739
740         toolbar_item_hbox = gtk_hbox_new (FALSE, 4);
741         gtk_widget_show(toolbar_item_hbox);
742         gtk_container_add(GTK_CONTAINER (vbox_frame), toolbar_item_hbox);
743         
744         table = gtk_table_new (3, 3, FALSE);
745         gtk_box_pack_start(GTK_BOX(toolbar_item_hbox), table,
746                            TRUE, TRUE, 0);
747         gtk_container_set_border_width (GTK_CONTAINER (table), 8);
748         gtk_table_set_row_spacings (GTK_TABLE (table), 8);
749         gtk_table_set_col_spacings (GTK_TABLE (table), 8);
750
751         /* toolbar item type */
752         label = gtk_label_new(_("Item type"));
753         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
754         gtk_widget_show(label);
755         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
756                          (GtkAttachOptions) (GTK_FILL),
757                          (GtkAttachOptions) (0), 0, 0); 
758         
759         item_type_combo = gtk_combo_box_new_text();
760         gtk_widget_set_size_request(item_type_combo, 200, -1);
761         gtk_table_attach(GTK_TABLE(table), item_type_combo, 1, 3, 0, 1,
762                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
763                          (GtkAttachOptions) (0), 0, 0);
764
765         /* available actions */
766         label = gtk_label_new(_("Event executed on click"));
767         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
768         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
769                           (GtkAttachOptions) (GTK_FILL),
770                           (GtkAttachOptions) (0), 0, 0);
771
772         item_action_combo = gtk_combo_box_new_text();
773         gtk_widget_set_size_request(item_action_combo, 200, -1);
774         gtk_table_attach (GTK_TABLE (table), item_action_combo, 1, 3, 1, 2,
775                           (GtkAttachOptions) (GTK_FILL),
776                           (GtkAttachOptions) (0), 0, 0);
777                           
778         /* available internal functions */
779         item_func_combo = gtk_combo_box_new_text();
780         gtk_widget_set_size_request(item_func_combo, 200, -1);
781         gtk_table_attach (GTK_TABLE (table), item_func_combo, 1, 3, 1, 2,
782                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
783                           (GtkAttachOptions) (0), 0, 0);
784         
785         /* toolbar item description */
786         label_icon_text = gtk_label_new(_("Toolbar text"));
787         gtk_misc_set_alignment(GTK_MISC(label_icon_text), 0, 0.5);
788         gtk_widget_show (label_icon_text);
789         gtk_table_attach (GTK_TABLE (table), label_icon_text, 0, 1, 2, 3,
790                           (GtkAttachOptions) (GTK_FILL),
791                           (GtkAttachOptions) (0), 0, 0);
792
793         item_text_entry = gtk_entry_new();
794         gtk_table_attach (GTK_TABLE (table), item_text_entry, 1, 3, 2, 3,
795                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
796                           (GtkAttachOptions) (0), 0, 0);
797
798         icon_vbox = gtk_vbox_new(FALSE, VBOX_BORDER);
799         gtk_widget_show(icon_vbox);
800         
801         icon_label = gtk_label_new(_("Icon"));
802         gtk_widget_set_size_request(icon_label, 100, -1);
803         gtk_box_pack_start(GTK_BOX(icon_vbox), icon_label, FALSE, FALSE, 0);
804         
805         icon_hbox = gtk_hbox_new(FALSE, 0);
806         gtk_widget_show(icon_hbox);
807         
808         label = gtk_label_new("");
809         gtk_box_pack_start(GTK_BOX(icon_hbox), label, TRUE, TRUE, 0);
810         
811         icon_button = gtk_button_new();
812         gtk_widget_show(icon_button);
813         gtk_widget_set_size_request(icon_button, 50, 50);
814         g_signal_connect(G_OBJECT(icon_button), "clicked",
815                          G_CALLBACK(icon_chooser_create), prefs_toolbar);
816         gtk_box_pack_start(GTK_BOX(icon_hbox), icon_button, FALSE, FALSE, 8);
817
818         label = gtk_label_new("");
819         gtk_box_pack_start(GTK_BOX(icon_hbox), label, TRUE, TRUE, 0);   
820         
821         gtk_box_pack_start(GTK_BOX(icon_vbox), icon_hbox, FALSE, FALSE, 0);
822         
823         gtk_box_pack_start(GTK_BOX(toolbar_item_hbox), icon_vbox, FALSE, FALSE, 0);
824                 
825         /* register / substitute / delete */
826         reg_hbox = gtk_hbox_new(FALSE, 4);
827         gtk_box_pack_start(GTK_BOX(main_vbox), reg_hbox, FALSE, FALSE, 0);
828         gtk_container_set_border_width(GTK_CONTAINER(reg_hbox), 10);
829
830         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
831         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
832         gtk_widget_set_size_request(arrow, -1, 16);
833
834         btn_hbox = gtk_hbox_new(TRUE, 4);
835         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
836
837         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
838         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
839         g_signal_connect(G_OBJECT(reg_btn), "clicked",
840                          G_CALLBACK(prefs_toolbar_register), 
841                          prefs_toolbar);
842
843         subst_btn = gtkut_get_replace_btn(_("Replace"));
844         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
845         g_signal_connect(G_OBJECT(subst_btn), "clicked",
846                          G_CALLBACK(prefs_toolbar_substitute),
847                          prefs_toolbar);
848
849         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
850         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
851         g_signal_connect(G_OBJECT(del_btn), "clicked",
852                          G_CALLBACK(prefs_toolbar_delete), 
853                           prefs_toolbar);
854
855         default_btn = gtk_button_new_with_label(_(" Use default "));
856 #ifndef MAEMO
857         gtk_box_pack_end(GTK_BOX(reg_hbox), default_btn, FALSE, TRUE, 0);
858 #else
859         hbox = gtk_hbox_new(FALSE, 0);
860         gtk_box_pack_start(GTK_BOX(main_vbox), hbox, FALSE, FALSE, 0);
861         gtk_box_pack_start(GTK_BOX(hbox), default_btn, FALSE, FALSE, 0);
862 #endif
863         g_signal_connect(G_OBJECT(default_btn), "clicked",
864                          G_CALLBACK(prefs_toolbar_default), 
865                          prefs_toolbar);
866
867         /* currently active toolbar items */
868         vbox_toolbar_items = gtk_vbox_new(FALSE, VBOX_BORDER);
869         gtk_box_pack_start(GTK_BOX(main_vbox), vbox_toolbar_items, TRUE, TRUE, 0);
870         
871         hbox_bottom = gtk_hbox_new(FALSE, 0);
872         gtk_container_add(GTK_CONTAINER(vbox_toolbar_items), hbox_bottom);
873         
874         scrolledwindow_list_view_set = gtk_scrolled_window_new(NULL, NULL);
875         gtk_box_pack_start(GTK_BOX(hbox_bottom), scrolledwindow_list_view_set, TRUE, TRUE, 0);
876         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_list_view_set), 1);
877         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_list_view_set), 
878                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
879         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow_list_view_set),
880                                             GTK_SHADOW_IN);
881
882         list_view_set = create_set_list_view(prefs_toolbar); 
883         gtk_widget_show(list_view_set);
884         gtk_container_add(GTK_CONTAINER(scrolledwindow_list_view_set), list_view_set);
885         gtk_widget_set_size_request(list_view_set, 225, 120);
886
887         btn_vbox = gtk_vbox_new(FALSE, 8);
888         gtk_widget_show(btn_vbox);
889         gtk_box_pack_start(GTK_BOX(hbox_bottom), btn_vbox, FALSE, FALSE, 5);
890
891         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
892         gtk_widget_show(up_btn);
893         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 2);
894
895         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
896         gtk_widget_show(down_btn);
897         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
898
899         g_signal_connect(G_OBJECT(item_type_combo), "changed",
900                          G_CALLBACK(item_type_changed), prefs_toolbar);
901         g_signal_connect(G_OBJECT(item_action_combo), "changed",
902                          G_CALLBACK(action_selection_changed), prefs_toolbar);
903         g_signal_connect(G_OBJECT(item_func_combo), "changed",
904                          G_CALLBACK(func_selection_changed), prefs_toolbar);
905         g_signal_connect(G_OBJECT(up_btn), "clicked",
906                          G_CALLBACK(prefs_toolbar_up), prefs_toolbar);
907         g_signal_connect(G_OBJECT(down_btn), "clicked",
908                          G_CALLBACK(prefs_toolbar_down), prefs_toolbar);
909         
910         gtk_widget_show_all(main_vbox);
911
912         prefs_toolbar->list_view_set    = list_view_set;
913         prefs_toolbar->item_text_entry  = item_text_entry;
914         prefs_toolbar->item_type_combo  = item_type_combo;
915         prefs_toolbar->item_func_combo  = item_func_combo;
916         prefs_toolbar->item_action_combo= item_action_combo;
917         prefs_toolbar->icon_button      = icon_button;
918         prefs_toolbar->item_icon_file   = NULL;
919         
920         prefs_toolbar->page.widget = main_vbox;
921 }
922
923 ToolbarPage *prefs_toolbar_mainwindow;
924 ToolbarPage *prefs_toolbar_composewindow;
925 ToolbarPage *prefs_toolbar_messageview;
926
927 void prefs_toolbar_init(void)
928 {
929         ToolbarPage *page;
930         static gchar *mainpath[3], *messagepath[3], *composepath[3];
931
932         mainpath[0] = _("Customize Toolbars");
933         mainpath[1] = _("Main Window");
934         mainpath[2] = NULL;
935
936         page = g_new0(ToolbarPage, 1);
937         page->page.path = mainpath;
938         page->page.create_widget = prefs_toolbar_create_widget;
939         page->page.destroy_widget = prefs_toolbar_destroy_widget;
940         page->page.save_page = prefs_toolbar_save;
941         page->source = TOOLBAR_MAIN;
942         page->page.weight = 50.0;
943         prefs_gtk_register_page((PrefsPage *) page);
944         prefs_toolbar_mainwindow = page;
945
946         messagepath[0] = _("Customize Toolbars");
947         messagepath[1] = _("Message Window");
948         messagepath[2] = NULL;
949
950         page = g_new0(ToolbarPage, 1);
951         page->page.path = messagepath;
952         page->page.create_widget = prefs_toolbar_create_widget;
953         page->page.destroy_widget = prefs_toolbar_destroy_widget;
954         page->page.save_page = prefs_toolbar_save;
955         page->source = TOOLBAR_MSGVIEW;
956         page->page.weight = 45.0;
957         prefs_gtk_register_page((PrefsPage *) page);
958         prefs_toolbar_messageview = page;
959
960         composepath[0] = _("Customize Toolbars");
961         composepath[1] = _("Compose Window");
962         composepath[2] = NULL;
963
964         page = g_new0(ToolbarPage, 1);
965         page->page.path = composepath;
966         page->page.create_widget = prefs_toolbar_create_widget;
967         page->page.destroy_widget = prefs_toolbar_destroy_widget;
968         page->page.save_page = prefs_toolbar_save;
969         page->source = TOOLBAR_COMPOSE;
970         page->page.weight = 40.0;
971         prefs_gtk_register_page((PrefsPage *) page);
972         prefs_toolbar_composewindow = page;
973 }
974
975 void prefs_toolbar_done(void)
976 {
977         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_mainwindow);
978         g_free(prefs_toolbar_mainwindow->item_icon_file);
979         g_free(prefs_toolbar_mainwindow);
980         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_composewindow);
981         g_free(prefs_toolbar_composewindow->item_icon_file);
982         g_free(prefs_toolbar_composewindow);
983         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_messageview);
984         g_free(prefs_toolbar_messageview->item_icon_file);
985         g_free(prefs_toolbar_messageview);
986 }
987
988 static void set_visible_if_not_text(GtkTreeViewColumn *col,
989                                     GtkCellRenderer   *renderer,
990                                     GtkTreeModel      *model,
991                                     GtkTreeIter       *iter,
992                                     gpointer           user_data)
993 {
994         gboolean is_text;
995         GdkPixbuf *pixbuf;
996
997         gtk_tree_model_get(model, iter, SET_ICON_IS_TEXT, &is_text, -1);
998         if (is_text) {
999                 g_object_set(renderer, "visible", FALSE, NULL); 
1000         } else {
1001                 pixbuf = NULL;
1002                 gtk_tree_model_get(model, iter, 
1003                                    SET_ICON, &pixbuf,
1004                                    -1);
1005                 /* note getting a pixbuf from a tree model increases
1006                  * its refcount ... */
1007                 g_object_unref(pixbuf);
1008                 
1009                 g_object_set(renderer, "visible", TRUE, NULL);
1010                 g_object_set(renderer, "pixbuf",  pixbuf, NULL);
1011         }
1012 }
1013
1014 static GtkWidget *create_set_list_view(ToolbarPage *prefs_toolbar)
1015 {
1016         GtkTreeView *list_view;
1017         GtkListStore *store;
1018         GtkCellRenderer *renderer;
1019         GtkTreeViewColumn *column;
1020         GtkTreeSelection *selector;
1021
1022         store = gtk_list_store_new(N_SET_COLUMNS, 
1023                                    GDK_TYPE_PIXBUF,
1024                                    G_TYPE_STRING,
1025                                    G_TYPE_STRING,
1026                                    G_TYPE_STRING,
1027                                    G_TYPE_STRING,
1028                                    G_TYPE_BOOLEAN,
1029                                    -1);
1030         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1031         g_object_unref(G_OBJECT(store));
1032
1033         column = gtk_tree_view_column_new();
1034         gtk_tree_view_column_set_title(column, _("Icon"));
1035         renderer = gtk_cell_renderer_pixbuf_new();
1036         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1037         
1038         /* tell pixbuf renderer it is only visible if 
1039          * the icon is not represented by text */
1040         gtk_tree_view_column_set_cell_data_func(column, renderer,
1041                                                 set_visible_if_not_text,
1042                                                 NULL, NULL);
1043         
1044         renderer = gtk_cell_renderer_text_new();
1045         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1046         
1047         /* tell the text renderer it is only visible if the icon
1048          * is represented by an image */
1049         gtk_tree_view_column_set_attributes(column, renderer,
1050                                             "visible", SET_ICON_IS_TEXT,
1051                                             "text", SET_ICON_TEXT,
1052                                             NULL);
1053
1054         gtk_tree_view_append_column(list_view, column);
1055
1056         column = gtk_tree_view_column_new();
1057         gtk_tree_view_column_set_title(column, _("Icon text"));
1058         renderer = gtk_cell_renderer_text_new();
1059         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1060         gtk_tree_view_column_set_attributes(column, renderer,
1061                                             "text", SET_TEXT,
1062                                             NULL);
1063         gtk_tree_view_append_column(list_view, column);
1064
1065         column = gtk_tree_view_column_new();
1066         gtk_tree_view_column_set_title(column, _("Mapped event"));
1067         renderer = gtk_cell_renderer_text_new();
1068         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1069         gtk_tree_view_column_set_attributes(column, renderer,
1070                                             "text", SET_EVENT,
1071                                             NULL);
1072         gtk_tree_view_append_column(list_view, column);
1073
1074         /* various other tree view attributes */
1075         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1076         
1077         selector = gtk_tree_view_get_selection(list_view);
1078         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1079         gtk_tree_selection_set_select_function
1080                 (selector, (GtkTreeSelectionFunc) set_list_selected,
1081                  prefs_toolbar, NULL);
1082
1083         return GTK_WIDGET(list_view);   
1084
1085 }
1086
1087 static gboolean set_list_selected(GtkTreeSelection *selector,
1088                                   GtkTreeModel *model, 
1089                                   GtkTreePath *path,
1090                                   gboolean currently_selected,
1091                                   ToolbarPage *prefs_toolbar)
1092 {
1093
1094         GtkTreeIter iter;
1095         gchar *icon_text, *icon_file, *descr;
1096         GList *cur;
1097         GSList *cur2;
1098         gint item_num;
1099         GdkPixbuf *pix;
1100         
1101         if (currently_selected || !gtk_tree_model_get_iter(model, &iter, path))
1102                 return TRUE;
1103         
1104         gtk_tree_model_get(model, &iter,
1105                            SET_ICON, &pix,
1106                            SET_TEXT, &icon_text,
1107                            SET_EVENT, &descr,
1108                            SET_FILENAME, &icon_file,
1109                            -1);
1110         
1111         g_free(prefs_toolbar->item_icon_file);
1112         prefs_toolbar->item_icon_file = icon_file;
1113         gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
1114                              gtk_image_new_from_pixbuf(pix));
1115         
1116         if (g_utf8_collate(toolbar_ret_descr_from_val(A_SEPARATOR), descr) == 0) {
1117                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button),
1118                                     _("None"));
1119                 g_free(prefs_toolbar->item_icon_file);
1120                 prefs_toolbar->item_icon_file = NULL;
1121                 gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
1122                                         ITEM_SEPARATOR);
1123                 g_free(icon_text);
1124                 g_free(descr);
1125
1126                 return TRUE;
1127         }
1128         
1129         gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
1130         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), 
1131                            icon_text);
1132
1133         if (g_utf8_collate(toolbar_ret_descr_from_val(A_SYL_ACTIONS), descr) == 0) {
1134                 gtk_combo_box_set_active(GTK_COMBO_BOX(
1135                         prefs_toolbar->item_type_combo), ITEM_USER_ACTION);
1136
1137                 for(cur2 = prefs_common.actions_list, item_num = 0; cur2 != NULL;
1138                     cur2 = cur2->next) {
1139                         gchar *item_string;
1140                         get_action_name((gchar *)cur2->data, &item_string);
1141                         
1142                         if(g_utf8_collate(item_string, icon_text) == 0) {
1143                                 gtk_combo_box_set_active(
1144                                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
1145                                         item_num);
1146                                 g_free(item_string);
1147                                 break;
1148                         }
1149                         else {
1150                                 item_num++;
1151                                 g_free(item_string);
1152                         }
1153                 }
1154                 
1155                 g_free(icon_text);
1156                 g_free(descr);
1157
1158                 return TRUE;
1159         }
1160         
1161         /* scan combo list for selected description an set combo item accordingly */
1162         for (cur = prefs_toolbar->combo_action_list, item_num = 0; cur != NULL; 
1163              cur = cur->next) {
1164                 gchar *item_str = (gchar*)cur->data;
1165                 if (g_utf8_collate(item_str, descr) == 0) {
1166                         gtk_combo_box_set_active(
1167                                 GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
1168                                 item_num);
1169                         
1170                         break;
1171                 }
1172                 else
1173                         item_num++;
1174         }
1175
1176         gtk_combo_box_set_active(GTK_COMBO_BOX(
1177                         prefs_toolbar->item_type_combo),ITEM_FUNCTION);
1178
1179         g_free(icon_text);
1180         g_free(descr);
1181
1182         return TRUE;
1183 }
1184
1185 static void icon_chooser_ok_clicked(GtkButton *button,
1186                                         ToolbarPage *prefs_toolbar)
1187 {
1188         GtkTreeModel *model;
1189         GtkTreeIter iter;
1190         GdkPixbuf *pix;
1191         gchar *icon_file;
1192         
1193         g_return_if_fail(prefs_toolbar != NULL);
1194         
1195         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
1196                         (GTK_TREE_VIEW(prefs_toolbar->icon_chooser_list)),
1197                         &model, &iter))
1198                 return;
1199         
1200         gtk_tree_model_get(model, &iter,
1201                            SET_ICON, &pix,
1202                            SET_FILENAME, &icon_file,
1203                            -1);
1204
1205         g_free(prefs_toolbar->item_icon_file);
1206         prefs_toolbar->item_icon_file = icon_file;
1207
1208         gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
1209                              gtk_image_new_from_pixbuf(pix));
1210         
1211         gtk_widget_destroy(prefs_toolbar->icon_chooser_win);
1212         prefs_toolbar->icon_chooser_win = NULL;
1213         prefs_toolbar->icon_chooser_list = NULL;
1214 }
1215
1216 static void icon_chooser_cancel_clicked(GtkButton *button,
1217                                         ToolbarPage *prefs_toolbar)
1218 {
1219         g_return_if_fail(prefs_toolbar != NULL);
1220
1221         gtk_widget_destroy(prefs_toolbar->icon_chooser_win);
1222         prefs_toolbar->icon_chooser_win = NULL;
1223         prefs_toolbar->icon_chooser_list = NULL;
1224 }
1225
1226 static gboolean icon_chooser_key_pressed(GtkWidget *widget, GdkEventKey *event,
1227                         ToolbarPage *prefs_toolbar)
1228 {
1229         if (event && event->keyval == GDK_Escape) {
1230                 icon_chooser_cancel_clicked(NULL, prefs_toolbar);
1231                 return TRUE;
1232         }
1233
1234         return FALSE;
1235 }
1236
1237 static void icon_chooser_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
1238                                      GtkTreeViewColumn *column, ToolbarPage *prefs_toolbar)
1239 {
1240         icon_chooser_ok_clicked(NULL, prefs_toolbar);
1241 }
1242
1243 static void icon_chooser_create(GtkButton *button, ToolbarPage *prefs_toolbar)
1244 {
1245         static GtkWidget *icon_chooser_win;
1246         GtkWidget *label;
1247         GtkWidget *scrollwin;
1248         GtkWidget *vbox;
1249         GtkWidget *confirm_area;
1250         GtkWidget *ok_btn;
1251         GtkWidget *cancel_btn;
1252         GtkTreeView *list_view;
1253         GtkListStore *store;
1254         GtkTreeSelection *selector;
1255         GtkTreeViewColumn *column;
1256         GtkCellRenderer *renderer;
1257         GtkTreeIter iter;
1258         gint i;
1259         
1260         store = gtk_list_store_new(2, 
1261                                    GDK_TYPE_PIXBUF,
1262                                    G_TYPE_STRING,
1263                                    -1);
1264                                    
1265         gtk_list_store_clear(store);
1266
1267         for (i = 0; i < STOCK_PIXMAP_EMPTY; i++) {
1268                 GdkPixbuf *pixbuf;
1269                 if (i == STOCK_PIXMAP_CLAWS_MAIL_LOGO) /* that's too big */
1270                         continue;
1271
1272                 stock_pixbuf_gdk(prefs_toolbar->window, i, &pixbuf);
1273                 
1274                 gtk_list_store_append(store, &iter);
1275                 gtk_list_store_set(store, &iter,
1276                                    SET_ICON, pixbuf,
1277                                    SET_FILENAME, stock_pixmap_get_name((StockPixmap) i),
1278                                    -1);
1279         }
1280         
1281         icon_chooser_win = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_toolbar");
1282         gtk_window_set_title(GTK_WINDOW(icon_chooser_win), _("Toolbar item icon"));
1283         gtk_window_set_position(GTK_WINDOW(icon_chooser_win), GTK_WIN_POS_CENTER);
1284         gtk_window_set_resizable(GTK_WINDOW(icon_chooser_win), FALSE);
1285         gtk_widget_set_size_request(icon_chooser_win, 300, 250);
1286         
1287         vbox = gtk_vbox_new(FALSE, 5);
1288         gtk_widget_show(vbox);
1289         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
1290         gtk_container_add(GTK_CONTAINER(icon_chooser_win), vbox);
1291         
1292         label = gtk_label_new(_("Choose icon:"));
1293         gtk_widget_show(label);
1294         gtk_misc_set_alignment(GTK_MISC(label), 0, -1);
1295         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1296         
1297         scrollwin = gtk_scrolled_window_new(NULL, NULL);
1298         gtk_widget_show(scrollwin);
1299         gtk_box_pack_start(GTK_BOX(vbox), scrollwin, TRUE, TRUE, 0);
1300         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
1301                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1302         
1303         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1304         gtk_widget_show(GTK_WIDGET(list_view));
1305         
1306         column = gtk_tree_view_column_new();
1307
1308         renderer = gtk_cell_renderer_pixbuf_new();
1309         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1310         gtk_tree_view_column_set_attributes(column, renderer,
1311                                             "pixbuf", SET_ICON, 
1312                                             NULL);
1313
1314         gtk_tree_view_append_column(list_view, column);
1315
1316         column = gtk_tree_view_column_new();
1317         renderer = gtk_cell_renderer_text_new();
1318         gtk_tree_view_column_pack_start(column, renderer, TRUE);
1319         gtk_tree_view_column_set_attributes(column, renderer,
1320                                             "text", SET_FILENAME,
1321                                             NULL);
1322         
1323         gtk_tree_view_append_column(list_view, column);
1324
1325         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1326         gtk_tree_view_set_headers_visible(list_view, FALSE);
1327         
1328         selector = gtk_tree_view_get_selection(list_view);
1329         gtk_tree_selection_set_mode(selector, GTK_SELECTION_SINGLE);
1330
1331         gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
1332         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
1333                                                 (list_view), &iter);
1334         gtk_container_add(GTK_CONTAINER(scrollwin), GTK_WIDGET(list_view));
1335
1336         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
1337                                       &ok_btn, GTK_STOCK_OK, NULL, NULL);
1338
1339         gtk_box_pack_start(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
1340         gtk_widget_show(confirm_area);
1341         gtk_widget_grab_default(ok_btn);
1342
1343         g_signal_connect(G_OBJECT(ok_btn), "clicked",
1344                          G_CALLBACK(icon_chooser_ok_clicked), prefs_toolbar);
1345         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
1346                          G_CALLBACK(icon_chooser_cancel_clicked), prefs_toolbar);
1347         g_signal_connect(G_OBJECT(icon_chooser_win), "key_press_event",
1348                          G_CALLBACK(icon_chooser_key_pressed), prefs_toolbar);
1349         g_signal_connect(G_OBJECT(list_view), "row-activated",
1350                          G_CALLBACK(icon_chooser_tree_activated), prefs_toolbar);
1351
1352         gtk_widget_show_all(icon_chooser_win);
1353         gtk_window_set_modal(GTK_WINDOW(icon_chooser_win), TRUE);
1354         
1355         prefs_toolbar->icon_chooser_win         = icon_chooser_win;
1356         prefs_toolbar->icon_chooser_list        = GTK_WIDGET(list_view);
1357 }