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