2007-10-06 [colin] 3.0.2cvs23
[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                 stock_pixbuf_gdk(prefs_toolbar->window, 
389                                  stock_pixmap_get_icon(prefs_toolbar->item_icon_file),
390                                  &pixbuf);
391                 if(pixbuf == NULL) {
392                         alertpanel_error(ERROR_MSG_NO_ICON);
393                         return;
394                 }
395                                 
396                 if (item_type == ITEM_FUNCTION) {
397                         event = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
398                                                 prefs_toolbar->item_func_combo));
399                                                 
400                         if (is_duplicate(prefs_toolbar, event)) {
401                                 alertpanel_error(ERROR_MSG);
402                                 g_free(event);
403                                 return;
404                         }
405                 } else
406                         event = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
407                 
408                 text = gtk_editable_get_chars(
409                         GTK_EDITABLE(prefs_toolbar->item_text_entry), 0 , -1);
410
411                 if (text != NULL) {
412                         gtk_list_store_append(store_set, &iter);
413                         gtk_list_store_set(store_set, &iter,
414                                            SET_ICON, pixbuf,
415                                            SET_FILENAME, prefs_toolbar->item_icon_file,
416                                            SET_TEXT, text,
417                                            SET_EVENT, event,
418                                            SET_ICON_TEXT, NULL,
419                                            SET_ICON_IS_TEXT, FALSE,
420                                            -1);
421                 } else {
422                         alertpanel_error(ERROR_MSG_NO_TEXT);
423                         return;
424                 }
425                 
426                 g_free(text);
427                 if(item_type != ITEM_USER_ACTION)
428                         g_free(event);
429         }
430         
431         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
432                                                 (list_view_set),
433                                        &iter);
434 }
435
436 static void prefs_toolbar_substitute(GtkButton *button, ToolbarPage *prefs_toolbar)
437 {
438         GtkTreeView *list_view_set   = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
439         GtkListStore *store_set   = GTK_LIST_STORE(gtk_tree_view_get_model(list_view_set));
440         gint item_type = gtk_combo_box_get_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo));
441         GtkTreeSelection *sel_set;
442         GtkTreeIter iter_set;
443
444         if (!gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_set), NULL))
445                 return;
446                 
447         sel_set = gtk_tree_view_get_selection(list_view_set);           
448         if (!gtk_tree_selection_get_selected(sel_set, NULL, &iter_set))
449                 return;
450
451         if (item_type == ITEM_SEPARATOR) {
452                 gtk_list_store_set(store_set, &iter_set, 
453                                    SET_ICON, NULL,
454                                    SET_TEXT, NULL,
455                                    SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
456                                    SET_FILENAME, prefs_toolbar->item_icon_file,
457                                    SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
458                                    SET_ICON_IS_TEXT, TRUE,
459                                    -1);
460         } else {
461                 GdkPixbuf *pixbuf;
462                 gchar *icon_event, *set_event, *text;
463
464                 stock_pixbuf_gdk(prefs_toolbar->window, 
465                                  stock_pixmap_get_icon(prefs_toolbar->item_icon_file),
466                                  &pixbuf);
467                 if(pixbuf == NULL) {
468                         alertpanel_error(ERROR_MSG_NO_ICON);
469                         return;
470                 }
471
472                 gtk_tree_model_get(GTK_TREE_MODEL(store_set), &iter_set, 
473                                                   SET_EVENT, &set_event,
474                                                   -1);
475                 
476                 if (item_type == ITEM_FUNCTION) {
477                         icon_event = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
478                                                 prefs_toolbar->item_func_combo));
479                                                 
480                         if (is_duplicate(prefs_toolbar, icon_event)
481                         && g_utf8_collate(icon_event, set_event) != 0){
482                                 alertpanel_error(ERROR_MSG);
483                                 g_free(icon_event);
484                                 g_free(set_event);
485                                 return;
486                         }
487                 } else
488                         icon_event = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
489                 
490                 text = gtk_editable_get_chars(
491                         GTK_EDITABLE(prefs_toolbar->item_text_entry), 0 , -1);
492
493                 /* change the row */
494                 if (text != NULL) {
495                         gtk_list_store_set(store_set, &iter_set,
496                                            SET_ICON, pixbuf,
497                                            SET_FILENAME, prefs_toolbar->item_icon_file,
498                                            SET_TEXT, text,
499                                            SET_EVENT, icon_event,
500                                            SET_ICON_TEXT, NULL,
501                                            SET_ICON_IS_TEXT, FALSE,
502                                            -1);
503                 } else 
504                         alertpanel_error(ERROR_MSG_NO_TEXT);
505                                 
506                 g_free(text);
507                 g_free(set_event);
508                 if(item_type != ITEM_USER_ACTION)
509                         g_free(icon_event);             
510         }
511 }
512
513 static void prefs_toolbar_delete(GtkButton *button, ToolbarPage *prefs_toolbar)
514 {
515         GtkTreeView *list_view_set = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
516         GtkTreeModel *store_set = gtk_tree_view_get_model(list_view_set);
517         GtkTreeIter iter_set;
518         GtkTreePath *path;                                                      
519
520         if (!gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_set), NULL))
521                 return;
522         
523         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
524                                                         (list_view_set),
525                                              NULL,
526                                              &iter_set))
527                 return;                                      
528
529         /* select prev list item, if deleted was first select next */
530         path = gtk_tree_model_get_path(store_set, &iter_set);
531         gtk_tree_path_prev(path);
532         
533         gtk_list_store_remove(GTK_LIST_STORE(store_set), &iter_set);
534
535         gtk_tree_selection_select_path(
536                         gtk_tree_view_get_selection(list_view_set),
537                         path);
538
539         gtk_tree_path_free(path);
540 }
541
542 static void prefs_toolbar_up(GtkButton *button, ToolbarPage *prefs_toolbar)
543 {
544         GtkTreePath *prev, *sel;
545         GtkTreeIter isel;
546         GtkListStore *store = NULL;
547         GtkTreeModel *model = NULL;
548         GtkTreeIter iprev;
549         
550         if (!gtk_tree_selection_get_selected
551                 (gtk_tree_view_get_selection
552                         (GTK_TREE_VIEW(prefs_toolbar->list_view_set)),
553                  &model,        
554                  &isel))
555                 return;
556         store = (GtkListStore *)model;
557
558         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
559         if (!sel)
560                 return;
561         
562         /* no move if we're at row 0... */
563         prev = gtk_tree_path_copy(sel);
564         if (!gtk_tree_path_prev(prev)) {
565                 gtk_tree_path_free(prev);
566                 gtk_tree_path_free(sel);
567                 return;
568         }
569
570         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
571                                 &iprev, prev);
572         gtk_tree_path_free(sel);
573         gtk_tree_path_free(prev);
574
575         gtk_list_store_swap(store, &iprev, &isel);
576 }
577
578 static void prefs_toolbar_down(GtkButton *button, ToolbarPage *prefs_toolbar)
579 {
580         GtkListStore *store = NULL;
581         GtkTreeModel *model = NULL;
582         GtkTreeIter next, sel;
583         
584         if (!gtk_tree_selection_get_selected
585                 (gtk_tree_view_get_selection
586                         (GTK_TREE_VIEW(prefs_toolbar->list_view_set)),
587                  &model,
588                  &sel))
589                 return;
590
591         store = (GtkListStore *)model;
592         next = sel;
593         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
594                 return;
595
596         gtk_list_store_swap(store, &next, &sel);
597 }
598
599 static void item_type_changed(GtkComboBox *item_type_combo,
600                                 ToolbarPage *prefs_toolbar)
601 {
602         gint active = gtk_combo_box_get_active(item_type_combo);
603
604         switch(active) {
605         case ITEM_FUNCTION:
606                 gtk_widget_show(prefs_toolbar->item_func_combo);
607                 gtk_widget_hide(prefs_toolbar->item_action_combo);
608                 gtk_combo_box_set_active(
609                         GTK_COMBO_BOX(prefs_toolbar->item_func_combo), 0);
610                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
611                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, TRUE);
612                 gtk_widget_set_sensitive(prefs_toolbar->item_func_combo, TRUE);
613                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
614
615                 func_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
616                                         prefs_toolbar);
617                 break;
618         case ITEM_USER_ACTION:
619                 gtk_widget_show(prefs_toolbar->item_action_combo);
620                 gtk_widget_hide(prefs_toolbar->item_func_combo);
621                 gtk_combo_box_set_active(
622                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo), 0);
623                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
624                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
625                 gtk_widget_set_sensitive(prefs_toolbar->item_action_combo, TRUE);
626                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
627                 
628                 action_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
629                                         prefs_toolbar);         
630                 break;
631         case ITEM_SEPARATOR:
632                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), _("None"));
633                 gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button), NULL);
634                 gtk_combo_box_set_active(
635                         GTK_COMBO_BOX(prefs_toolbar->item_func_combo), -1);
636                 gtk_combo_box_set_active(
637                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo), -1);
638                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), "");
639                 gtk_widget_set_sensitive(prefs_toolbar->item_action_combo, FALSE);
640                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
641                 gtk_widget_set_sensitive(prefs_toolbar->item_func_combo, FALSE);
642                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, FALSE);
643                 break;
644         }
645
646 }
647
648 static void action_selection_changed(GtkComboBox *action_combo,
649                                 ToolbarPage *prefs_toolbar)
650 {
651         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
652                            prefs_toolbar->item_action_combo));
653
654         if(text != NULL) { /* action */
655                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), text);
656                 g_free(text);
657         } 
658 }
659
660 static void func_selection_changed(GtkComboBox *action_combo,
661                                 ToolbarPage *prefs_toolbar)
662 {
663         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
664                            prefs_toolbar->item_func_combo));
665
666         if(text != NULL) { /* action */
667                 int action = -1;
668                 action = toolbar_ret_val_from_descr(text);
669                 if (action >= 0)
670                         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), 
671                                         toolbar_get_short_text(action));
672                 g_free(text);
673         } 
674 }
675
676 static void prefs_toolbar_create(ToolbarPage *prefs_toolbar)
677 {
678         GtkWidget *main_vbox;
679         GtkWidget *toolbar_item_hbox;
680         GtkWidget *icon_vbox;
681         GtkWidget *icon_label;
682         GtkWidget *icon_button;
683         GtkWidget *icon_hbox;
684         GtkWidget *item_type_combo;
685         GtkWidget *item_action_combo;
686         GtkWidget *item_func_combo;
687         GtkWidget *reg_hbox;
688         GtkWidget *arrow;
689         GtkWidget *btn_hbox;
690 #ifdef MAEMO
691         GtkWidget *hbox;
692 #endif
693         GtkWidget *reg_btn;
694         GtkWidget *subst_btn;
695         GtkWidget *del_btn;
696         GtkWidget *default_btn;
697         GtkWidget *vbox_frame;
698         GtkWidget *table;
699         GtkWidget *label_icon_text;
700         GtkWidget *item_text_entry;
701         GtkWidget *vbox_toolbar_items;
702         GtkWidget *hbox_bottom;
703         GtkWidget *scrolledwindow_list_view_set;
704         GtkWidget *list_view_set;
705         GtkWidget *label;
706
707         GtkWidget *btn_vbox;
708         GtkWidget *up_btn;
709         GtkWidget *down_btn;
710
711         debug_print("Creating custom toolbar window...\n");
712
713         main_vbox = gtk_vbox_new(FALSE, 0);
714         gtk_widget_show(main_vbox);
715
716         vbox_frame = gtk_frame_new(_("Toolbar item"));
717         gtk_widget_show(vbox_frame);
718         gtk_box_pack_start(GTK_BOX(main_vbox), vbox_frame, FALSE, TRUE, 0);
719
720         toolbar_item_hbox = gtk_hbox_new (FALSE, 4);
721         gtk_widget_show(toolbar_item_hbox);
722         gtk_container_add(GTK_CONTAINER (vbox_frame), toolbar_item_hbox);
723         
724         table = gtk_table_new (3, 3, FALSE);
725         gtk_box_pack_start(GTK_BOX(toolbar_item_hbox), table,
726                            TRUE, TRUE, 0);
727         gtk_container_set_border_width (GTK_CONTAINER (table), 8);
728         gtk_table_set_row_spacings (GTK_TABLE (table), 8);
729         gtk_table_set_col_spacings (GTK_TABLE (table), 8);
730
731         /* toolbar item type */
732         label = gtk_label_new(_("Item type"));
733         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
734         gtk_widget_show(label);
735         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
736                          (GtkAttachOptions) (GTK_FILL),
737                          (GtkAttachOptions) (0), 0, 0); 
738         
739         item_type_combo = gtk_combo_box_new_text();
740         gtk_widget_set_size_request(item_type_combo, 200, -1);
741         gtk_table_attach(GTK_TABLE(table), item_type_combo, 1, 3, 0, 1,
742                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
743                          (GtkAttachOptions) (0), 0, 0);
744
745         /* available actions */
746         label = gtk_label_new(_("Event executed on click"));
747         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
748         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
749                           (GtkAttachOptions) (GTK_FILL),
750                           (GtkAttachOptions) (0), 0, 0);
751
752         item_action_combo = gtk_combo_box_new_text();
753         gtk_widget_set_size_request(item_action_combo, 200, -1);
754         gtk_table_attach (GTK_TABLE (table), item_action_combo, 1, 3, 1, 2,
755                           (GtkAttachOptions) (GTK_FILL),
756                           (GtkAttachOptions) (0), 0, 0);
757                           
758         /* available internal functions */
759         item_func_combo = gtk_combo_box_new_text();
760         gtk_widget_set_size_request(item_func_combo, 200, -1);
761         gtk_table_attach (GTK_TABLE (table), item_func_combo, 1, 3, 1, 2,
762                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
763                           (GtkAttachOptions) (0), 0, 0);
764         
765         /* toolbar item description */
766         label_icon_text = gtk_label_new(_("Toolbar text"));
767         gtk_misc_set_alignment(GTK_MISC(label_icon_text), 0, 0.5);
768         gtk_widget_show (label_icon_text);
769         gtk_table_attach (GTK_TABLE (table), label_icon_text, 0, 1, 2, 3,
770                           (GtkAttachOptions) (GTK_FILL),
771                           (GtkAttachOptions) (0), 0, 0);
772
773         item_text_entry = gtk_entry_new();
774         gtk_table_attach (GTK_TABLE (table), item_text_entry, 1, 3, 2, 3,
775                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
776                           (GtkAttachOptions) (0), 0, 0);
777
778         icon_vbox = gtk_vbox_new(FALSE, VBOX_BORDER);
779         gtk_widget_show(icon_vbox);
780         
781         icon_label = gtk_label_new(_("Icon"));
782         gtk_widget_set_size_request(icon_label, 100, -1);
783         gtk_box_pack_start(GTK_BOX(icon_vbox), icon_label, FALSE, FALSE, 0);
784         
785         icon_hbox = gtk_hbox_new(FALSE, 0);
786         gtk_widget_show(icon_hbox);
787         
788         label = gtk_label_new("");
789         gtk_box_pack_start(GTK_BOX(icon_hbox), label, TRUE, TRUE, 0);
790         
791         icon_button = gtk_button_new();
792         gtk_widget_show(icon_button);
793         gtk_widget_set_size_request(icon_button, 50, 50);
794         g_signal_connect(G_OBJECT(icon_button), "clicked",
795                          G_CALLBACK(icon_chooser_create), prefs_toolbar);
796         gtk_box_pack_start(GTK_BOX(icon_hbox), icon_button, FALSE, FALSE, 8);
797
798         label = gtk_label_new("");
799         gtk_box_pack_start(GTK_BOX(icon_hbox), label, TRUE, TRUE, 0);   
800         
801         gtk_box_pack_start(GTK_BOX(icon_vbox), icon_hbox, FALSE, FALSE, 0);
802         
803         gtk_box_pack_start(GTK_BOX(toolbar_item_hbox), icon_vbox, FALSE, FALSE, 0);
804                 
805         /* register / substitute / delete */
806         reg_hbox = gtk_hbox_new(FALSE, 4);
807         gtk_box_pack_start(GTK_BOX(main_vbox), reg_hbox, FALSE, FALSE, 0);
808         gtk_container_set_border_width(GTK_CONTAINER(reg_hbox), 10);
809
810         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
811         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
812         gtk_widget_set_size_request(arrow, -1, 16);
813
814         btn_hbox = gtk_hbox_new(TRUE, 4);
815         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
816
817         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
818         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
819         g_signal_connect(G_OBJECT(reg_btn), "clicked",
820                          G_CALLBACK(prefs_toolbar_register), 
821                          prefs_toolbar);
822
823         subst_btn = gtkut_get_replace_btn(_("Replace"));
824         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
825         g_signal_connect(G_OBJECT(subst_btn), "clicked",
826                          G_CALLBACK(prefs_toolbar_substitute),
827                          prefs_toolbar);
828
829         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
830         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
831         g_signal_connect(G_OBJECT(del_btn), "clicked",
832                          G_CALLBACK(prefs_toolbar_delete), 
833                           prefs_toolbar);
834
835         default_btn = gtk_button_new_with_label(_(" Use default "));
836 #ifndef MAEMO
837         gtk_box_pack_end(GTK_BOX(reg_hbox), default_btn, FALSE, TRUE, 0);
838 #else
839         hbox = gtk_hbox_new(FALSE, 0);
840         gtk_box_pack_start(GTK_BOX(main_vbox), hbox, FALSE, FALSE, 0);
841         gtk_box_pack_start(GTK_BOX(hbox), default_btn, FALSE, FALSE, 0);
842 #endif
843         g_signal_connect(G_OBJECT(default_btn), "clicked",
844                          G_CALLBACK(prefs_toolbar_default), 
845                          prefs_toolbar);
846
847         /* currently active toolbar items */
848         vbox_toolbar_items = gtk_vbox_new(FALSE, VBOX_BORDER);
849         gtk_box_pack_start(GTK_BOX(main_vbox), vbox_toolbar_items, TRUE, TRUE, 0);
850         
851         hbox_bottom = gtk_hbox_new(FALSE, 0);
852         gtk_container_add(GTK_CONTAINER(vbox_toolbar_items), hbox_bottom);
853         
854         scrolledwindow_list_view_set = gtk_scrolled_window_new(NULL, NULL);
855         gtk_box_pack_start(GTK_BOX(hbox_bottom), scrolledwindow_list_view_set, TRUE, TRUE, 0);
856         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_list_view_set), 1);
857         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_list_view_set), 
858                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
859         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow_list_view_set),
860                                             GTK_SHADOW_IN);
861
862         list_view_set = create_set_list_view(prefs_toolbar); 
863         gtk_widget_show(list_view_set);
864         gtk_container_add(GTK_CONTAINER(scrolledwindow_list_view_set), list_view_set);
865         gtk_widget_set_size_request(list_view_set, 225, 120);
866
867         btn_vbox = gtk_vbox_new(FALSE, 8);
868         gtk_widget_show(btn_vbox);
869         gtk_box_pack_start(GTK_BOX(hbox_bottom), btn_vbox, FALSE, FALSE, 5);
870
871         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
872         gtk_widget_show(up_btn);
873         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 2);
874
875         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
876         gtk_widget_show(down_btn);
877         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
878
879         g_signal_connect(G_OBJECT(item_type_combo), "changed",
880                          G_CALLBACK(item_type_changed), prefs_toolbar);
881         g_signal_connect(G_OBJECT(item_action_combo), "changed",
882                          G_CALLBACK(action_selection_changed), prefs_toolbar);
883         g_signal_connect(G_OBJECT(item_func_combo), "changed",
884                          G_CALLBACK(func_selection_changed), prefs_toolbar);
885         g_signal_connect(G_OBJECT(up_btn), "clicked",
886                          G_CALLBACK(prefs_toolbar_up), prefs_toolbar);
887         g_signal_connect(G_OBJECT(down_btn), "clicked",
888                          G_CALLBACK(prefs_toolbar_down), prefs_toolbar);
889         
890         gtk_widget_show_all(main_vbox);
891
892         prefs_toolbar->list_view_set    = list_view_set;
893         prefs_toolbar->item_text_entry  = item_text_entry;
894         prefs_toolbar->item_type_combo  = item_type_combo;
895         prefs_toolbar->item_func_combo  = item_func_combo;
896         prefs_toolbar->item_action_combo= item_action_combo;
897         prefs_toolbar->icon_button      = icon_button;
898         prefs_toolbar->item_icon_file   = NULL;
899         
900         prefs_toolbar->page.widget = main_vbox;
901 }
902
903 ToolbarPage *prefs_toolbar_mainwindow;
904 ToolbarPage *prefs_toolbar_composewindow;
905 ToolbarPage *prefs_toolbar_messageview;
906
907 void prefs_toolbar_init(void)
908 {
909         ToolbarPage *page;
910         static gchar *mainpath[3], *messagepath[3], *composepath[3];
911
912         mainpath[0] = _("Customize Toolbars");
913         mainpath[1] = _("Main Window");
914         mainpath[2] = NULL;
915
916         page = g_new0(ToolbarPage, 1);
917         page->page.path = mainpath;
918         page->page.create_widget = prefs_toolbar_create_widget;
919         page->page.destroy_widget = prefs_toolbar_destroy_widget;
920         page->page.save_page = prefs_toolbar_save;
921         page->source = TOOLBAR_MAIN;
922         page->page.weight = 50.0;
923         prefs_gtk_register_page((PrefsPage *) page);
924         prefs_toolbar_mainwindow = page;
925
926         messagepath[0] = _("Customize Toolbars");
927         messagepath[1] = _("Message Window");
928         messagepath[2] = NULL;
929
930         page = g_new0(ToolbarPage, 1);
931         page->page.path = messagepath;
932         page->page.create_widget = prefs_toolbar_create_widget;
933         page->page.destroy_widget = prefs_toolbar_destroy_widget;
934         page->page.save_page = prefs_toolbar_save;
935         page->source = TOOLBAR_MSGVIEW;
936         page->page.weight = 45.0;
937         prefs_gtk_register_page((PrefsPage *) page);
938         prefs_toolbar_messageview = page;
939
940         composepath[0] = _("Customize Toolbars");
941         composepath[1] = _("Compose Window");
942         composepath[2] = NULL;
943
944         page = g_new0(ToolbarPage, 1);
945         page->page.path = composepath;
946         page->page.create_widget = prefs_toolbar_create_widget;
947         page->page.destroy_widget = prefs_toolbar_destroy_widget;
948         page->page.save_page = prefs_toolbar_save;
949         page->source = TOOLBAR_COMPOSE;
950         page->page.weight = 40.0;
951         prefs_gtk_register_page((PrefsPage *) page);
952         prefs_toolbar_composewindow = page;
953 }
954
955 void prefs_toolbar_done(void)
956 {
957         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_mainwindow);
958         g_free(prefs_toolbar_mainwindow->item_icon_file);
959         g_free(prefs_toolbar_mainwindow);
960         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_composewindow);
961         g_free(prefs_toolbar_composewindow->item_icon_file);
962         g_free(prefs_toolbar_composewindow);
963         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_messageview);
964         g_free(prefs_toolbar_messageview->item_icon_file);
965         g_free(prefs_toolbar_messageview);
966 }
967
968 static void set_visible_if_not_text(GtkTreeViewColumn *col,
969                                     GtkCellRenderer   *renderer,
970                                     GtkTreeModel      *model,
971                                     GtkTreeIter       *iter,
972                                     gpointer           user_data)
973 {
974         gboolean is_text;
975         GdkPixbuf *pixbuf;
976
977         gtk_tree_model_get(model, iter, SET_ICON_IS_TEXT, &is_text, -1);
978         if (is_text) {
979                 g_object_set(renderer, "visible", FALSE, NULL); 
980         } else {
981                 pixbuf = NULL;
982                 gtk_tree_model_get(model, iter, 
983                                    SET_ICON, &pixbuf,
984                                    -1);
985                 /* note getting a pixbuf from a tree model increases
986                  * its refcount ... */
987                 g_object_unref(pixbuf);
988                 
989                 g_object_set(renderer, "visible", TRUE, NULL);
990                 g_object_set(renderer, "pixbuf",  pixbuf, NULL);
991         }
992 }
993
994 static GtkWidget *create_set_list_view(ToolbarPage *prefs_toolbar)
995 {
996         GtkTreeView *list_view;
997         GtkListStore *store;
998         GtkCellRenderer *renderer;
999         GtkTreeViewColumn *column;
1000         GtkTreeSelection *selector;
1001
1002         store = gtk_list_store_new(N_SET_COLUMNS, 
1003                                    GDK_TYPE_PIXBUF,
1004                                    G_TYPE_STRING,
1005                                    G_TYPE_STRING,
1006                                    G_TYPE_STRING,
1007                                    G_TYPE_STRING,
1008                                    G_TYPE_BOOLEAN,
1009                                    -1);
1010         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1011         g_object_unref(G_OBJECT(store));
1012
1013         column = gtk_tree_view_column_new();
1014         gtk_tree_view_column_set_title(column, _("Icon"));
1015         renderer = gtk_cell_renderer_pixbuf_new();
1016         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1017         
1018         /* tell pixbuf renderer it is only visible if 
1019          * the icon is not represented by text */
1020         gtk_tree_view_column_set_cell_data_func(column, renderer,
1021                                                 set_visible_if_not_text,
1022                                                 NULL, NULL);
1023         
1024         renderer = gtk_cell_renderer_text_new();
1025         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1026         
1027         /* tell the text renderer it is only visible if the icon
1028          * is represented by an image */
1029         gtk_tree_view_column_set_attributes(column, renderer,
1030                                             "visible", SET_ICON_IS_TEXT,
1031                                             "text", SET_ICON_TEXT,
1032                                             NULL);
1033
1034         gtk_tree_view_append_column(list_view, column);
1035
1036         column = gtk_tree_view_column_new();
1037         gtk_tree_view_column_set_title(column, _("Icon text"));
1038         renderer = gtk_cell_renderer_text_new();
1039         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1040         gtk_tree_view_column_set_attributes(column, renderer,
1041                                             "text", SET_TEXT,
1042                                             NULL);
1043         gtk_tree_view_append_column(list_view, column);
1044
1045         column = gtk_tree_view_column_new();
1046         gtk_tree_view_column_set_title(column, _("Mapped event"));
1047         renderer = gtk_cell_renderer_text_new();
1048         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1049         gtk_tree_view_column_set_attributes(column, renderer,
1050                                             "text", SET_EVENT,
1051                                             NULL);
1052         gtk_tree_view_append_column(list_view, column);
1053
1054         /* various other tree view attributes */
1055         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1056         
1057         selector = gtk_tree_view_get_selection(list_view);
1058         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1059         gtk_tree_selection_set_select_function
1060                 (selector, (GtkTreeSelectionFunc) set_list_selected,
1061                  prefs_toolbar, NULL);
1062
1063         return GTK_WIDGET(list_view);   
1064
1065 }
1066
1067 static gboolean set_list_selected(GtkTreeSelection *selector,
1068                                   GtkTreeModel *model, 
1069                                   GtkTreePath *path,
1070                                   gboolean currently_selected,
1071                                   ToolbarPage *prefs_toolbar)
1072 {
1073
1074         GtkTreeIter iter;
1075         gchar *icon_text, *icon_file, *descr;
1076         GList *cur;
1077         GSList *cur2;
1078         gint item_num;
1079         GdkPixbuf *pix;
1080         
1081         if (currently_selected || !gtk_tree_model_get_iter(model, &iter, path))
1082                 return TRUE;
1083         
1084         gtk_tree_model_get(model, &iter,
1085                            SET_ICON, &pix,
1086                            SET_TEXT, &icon_text,
1087                            SET_EVENT, &descr,
1088                            SET_FILENAME, &icon_file,
1089                            -1);
1090         
1091         g_free(prefs_toolbar->item_icon_file);
1092         prefs_toolbar->item_icon_file = icon_file;
1093         gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
1094                              gtk_image_new_from_pixbuf(pix));
1095         
1096         if (g_utf8_collate(toolbar_ret_descr_from_val(A_SEPARATOR), descr) == 0) {
1097                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button),
1098                                     _("None"));
1099                 gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
1100                                         ITEM_SEPARATOR);
1101                 g_free(icon_text);
1102                 g_free(descr);
1103
1104                 return TRUE;
1105         }
1106         
1107         gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
1108         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), 
1109                            icon_text);
1110
1111         if (g_utf8_collate(toolbar_ret_descr_from_val(A_SYL_ACTIONS), descr) == 0) {
1112                 gtk_combo_box_set_active(GTK_COMBO_BOX(
1113                         prefs_toolbar->item_type_combo), ITEM_USER_ACTION);
1114
1115                 for(cur2 = prefs_common.actions_list, item_num = 0; cur2 != NULL;
1116                     cur2 = cur2->next) {
1117                         gchar *item_string;
1118                         get_action_name((gchar *)cur2->data, &item_string);
1119                         
1120                         if(g_utf8_collate(item_string, icon_text) == 0) {
1121                                 gtk_combo_box_set_active(
1122                                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
1123                                         item_num);
1124                                 g_free(item_string);
1125                                 break;
1126                         }
1127                         else {
1128                                 item_num++;
1129                                 g_free(item_string);
1130                         }
1131                 }
1132                 
1133                 g_free(icon_text);
1134                 g_free(descr);
1135
1136                 return TRUE;
1137         }
1138         
1139         /* scan combo list for selected description an set combo item accordingly */
1140         for (cur = prefs_toolbar->combo_action_list, item_num = 0; cur != NULL; 
1141              cur = cur->next) {
1142                 gchar *item_str = (gchar*)cur->data;
1143                 if (g_utf8_collate(item_str, descr) == 0) {
1144                         gtk_combo_box_set_active(
1145                                 GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
1146                                 item_num);
1147                         
1148                         break;
1149                 }
1150                 else
1151                         item_num++;
1152         }
1153
1154         gtk_combo_box_set_active(GTK_COMBO_BOX(
1155                         prefs_toolbar->item_type_combo),ITEM_FUNCTION);
1156
1157         g_free(icon_text);
1158         g_free(descr);
1159
1160         return TRUE;
1161 }
1162
1163 static void icon_chooser_ok_clicked(GtkButton *button,
1164                                         ToolbarPage *prefs_toolbar)
1165 {
1166         GtkTreeModel *model;
1167         GtkTreeIter iter;
1168         GdkPixbuf *pix;
1169         gchar *icon_file;
1170         
1171         g_return_if_fail(prefs_toolbar != NULL);
1172         
1173         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
1174                         (GTK_TREE_VIEW(prefs_toolbar->icon_chooser_list)),
1175                         &model, &iter))
1176                 return;
1177         
1178         gtk_tree_model_get(model, &iter,
1179                            SET_ICON, &pix,
1180                            SET_FILENAME, &icon_file,
1181                            -1);
1182
1183         g_free(prefs_toolbar->item_icon_file);
1184         prefs_toolbar->item_icon_file = icon_file;
1185
1186         gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
1187                              gtk_image_new_from_pixbuf(pix));
1188         
1189         gtk_widget_destroy(prefs_toolbar->icon_chooser_win);
1190         prefs_toolbar->icon_chooser_win = NULL;
1191         prefs_toolbar->icon_chooser_list = NULL;
1192 }
1193
1194 static void icon_chooser_cancel_clicked(GtkButton *button,
1195                                         ToolbarPage *prefs_toolbar)
1196 {
1197         g_return_if_fail(prefs_toolbar != NULL);
1198
1199         gtk_widget_destroy(prefs_toolbar->icon_chooser_win);
1200         prefs_toolbar->icon_chooser_win = NULL;
1201         prefs_toolbar->icon_chooser_list = NULL;
1202 }
1203
1204 static void icon_chooser_create(GtkButton *button, ToolbarPage *prefs_toolbar)
1205 {
1206         static GtkWidget *icon_chooser_win;
1207         GtkWidget *label;
1208         GtkWidget *scrollwin;
1209         GtkWidget *vbox;
1210         GtkWidget *confirm_area;
1211         GtkWidget *ok_btn;
1212         GtkWidget *cancel_btn;
1213         GtkTreeView *list_view;
1214         GtkListStore *store;
1215         GtkTreeSelection *selector;
1216         GtkTreeViewColumn *column;
1217         GtkCellRenderer *renderer;
1218         GtkTreeIter iter;
1219         gint i;
1220         
1221         store = gtk_list_store_new(2, 
1222                                    GDK_TYPE_PIXBUF,
1223                                    G_TYPE_STRING,
1224                                    -1);
1225                                    
1226         gtk_list_store_clear(store);
1227
1228         for (i = 0; i < STOCK_PIXMAP_EMPTY; i++) {
1229                 GdkPixbuf *pixbuf;
1230                 if (i == STOCK_PIXMAP_CLAWS_MAIL_LOGO) /* that's too big */
1231                         continue;
1232
1233                 stock_pixbuf_gdk(prefs_toolbar->window, i, &pixbuf);
1234                 
1235                 gtk_list_store_append(store, &iter);
1236                 gtk_list_store_set(store, &iter,
1237                                    SET_ICON, pixbuf,
1238                                    SET_FILENAME, stock_pixmap_get_name((StockPixmap) i),
1239                                    -1);
1240         }
1241         
1242         icon_chooser_win = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_toolbar");
1243         gtk_window_set_title(GTK_WINDOW(icon_chooser_win), _("Toolbar item icon"));
1244         gtk_window_set_position(GTK_WINDOW(icon_chooser_win), GTK_WIN_POS_CENTER);
1245         gtk_window_set_resizable(GTK_WINDOW(icon_chooser_win), FALSE);
1246         gtk_widget_set_size_request(icon_chooser_win, 300, 250);
1247         
1248         vbox = gtk_vbox_new(FALSE, 5);
1249         gtk_widget_show(vbox);
1250         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
1251         gtk_container_add(GTK_CONTAINER(icon_chooser_win), vbox);
1252         
1253         label = gtk_label_new(_("Choose icon:"));
1254         gtk_widget_show(label);
1255         gtk_misc_set_alignment(GTK_MISC(label), 0, -1);
1256         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1257         
1258         scrollwin = gtk_scrolled_window_new(NULL, NULL);
1259         gtk_widget_show(scrollwin);
1260         gtk_box_pack_start(GTK_BOX(vbox), scrollwin, TRUE, TRUE, 0);
1261         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
1262                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1263         
1264         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1265         gtk_widget_show(GTK_WIDGET(list_view));
1266         
1267         column = gtk_tree_view_column_new();
1268
1269         renderer = gtk_cell_renderer_pixbuf_new();
1270         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1271         gtk_tree_view_column_set_attributes(column, renderer,
1272                                             "pixbuf", SET_ICON, 
1273                                             NULL);
1274
1275         gtk_tree_view_append_column(list_view, column);
1276
1277         column = gtk_tree_view_column_new();
1278         renderer = gtk_cell_renderer_text_new();
1279         gtk_tree_view_column_pack_start(column, renderer, TRUE);
1280         gtk_tree_view_column_set_attributes(column, renderer,
1281                                             "text", SET_FILENAME,
1282                                             NULL);
1283         
1284         gtk_tree_view_append_column(list_view, column);
1285
1286         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1287         gtk_tree_view_set_headers_visible(list_view, FALSE);
1288         
1289         selector = gtk_tree_view_get_selection(list_view);
1290         gtk_tree_selection_set_mode(selector, GTK_SELECTION_SINGLE);
1291
1292         gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
1293         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
1294                                                 (list_view), &iter);
1295         gtk_container_add(GTK_CONTAINER(scrollwin), GTK_WIDGET(list_view));
1296
1297         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
1298                                       &ok_btn, GTK_STOCK_OK, NULL, NULL);
1299
1300         gtk_box_pack_start(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
1301         gtk_widget_show(confirm_area);
1302         gtk_widget_grab_default(ok_btn);
1303
1304         g_signal_connect(G_OBJECT(ok_btn), "clicked",
1305                          G_CALLBACK(icon_chooser_ok_clicked), prefs_toolbar);
1306         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
1307                          G_CALLBACK(icon_chooser_cancel_clicked), prefs_toolbar);
1308         
1309         gtk_widget_show_all(icon_chooser_win);
1310         gtk_window_set_modal(GTK_WINDOW(icon_chooser_win), TRUE);
1311         
1312         prefs_toolbar->icon_chooser_win         = icon_chooser_win;
1313         prefs_toolbar->icon_chooser_list        = GTK_WIDGET(list_view);
1314 }