2012-02-08 [pawel] 3.8.0cvs27
[claws.git] / src / prefs_toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2011 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 <gdk/gdkkeysyms.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "stock_pixmap.h"
38 #include "manage_window.h"
39 #include "combobox.h"
40 #include "gtkutils.h"
41 #include "mainwindow.h"
42 #include "alertpanel.h"
43 #include "prefs_common.h"
44 #include "prefs_actions.h"
45
46 #include "utils.h"
47
48 #include "toolbar.h"
49 #include "prefs_toolbar.h"
50 #include "prefswindow.h"
51 #include "prefs_gtk.h"
52 #include "plugin.h"
53
54 enum
55 {
56         SET_ICON          = 0,
57         SET_FILENAME      = 1,
58         SET_TEXT          = 2,
59         SET_EVENT         = 3,
60         SET_ICON_TEXT     = 4,          /*!< "icon" text (separator) */ 
61         SET_ICON_IS_TEXT  = 5,          /*!< icon is text representation */
62         N_SET_COLUMNS
63 };
64
65 enum
66 {
67         ITEM_FUNCTION     = 0,
68         ITEM_USER_ACTION  = 1,
69         ITEM_PLUGIN       = 2,
70         ITEM_SEPARATOR    = 3,
71 };
72
73 static const gint ToolbarIcons[] =
74 {
75         STOCK_PIXMAP_ADDRESS_BOOK,
76         STOCK_PIXMAP_ADDRESS_SEARCH,
77         STOCK_PIXMAP_BOOK,
78         STOCK_PIXMAP_CATEGORY,
79         STOCK_PIXMAP_CHECK_SPELLING,
80         STOCK_PIXMAP_CLOSE,
81         STOCK_PIXMAP_DOWN_ARROW,
82         STOCK_PIXMAP_UP_ARROW,
83         STOCK_PIXMAP_EDIT_EXTERN,
84         STOCK_PIXMAP_ERROR,
85         STOCK_PIXMAP_EXEC,
86         STOCK_PIXMAP_GROUP,
87         STOCK_PIXMAP_INSERT_FILE,
88         STOCK_PIXMAP_INTERFACE,
89         STOCK_PIXMAP_JPILOT,
90         STOCK_PIXMAP_LDAP,
91         STOCK_PIXMAP_LINEWRAP_CURRENT,
92         STOCK_PIXMAP_LINEWRAP_ALL,
93         STOCK_PIXMAP_MAIL,
94         STOCK_PIXMAP_MAIL_ATTACH,
95         STOCK_PIXMAP_MAIL_COMPOSE,
96         STOCK_PIXMAP_MAIL_FORWARD,
97         STOCK_PIXMAP_MAIL_RECEIVE,
98         STOCK_PIXMAP_MAIL_RECEIVE_ALL,
99         STOCK_PIXMAP_MAIL_REPLY,
100         STOCK_PIXMAP_MAIL_REPLY_TO_ALL,
101         STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR,
102         STOCK_PIXMAP_MAIL_REPLY_TO_LIST,
103         STOCK_PIXMAP_MAIL_SEND,
104         STOCK_PIXMAP_MAIL_SEND_QUEUE,
105         STOCK_PIXMAP_MAIL_SIGN,
106         STOCK_PIXMAP_OPEN_MAIL,
107         STOCK_PIXMAP_NEWS_COMPOSE,
108         STOCK_PIXMAP_PASTE,
109         STOCK_PIXMAP_PREFERENCES,
110         STOCK_PIXMAP_PROPERTIES,
111         STOCK_PIXMAP_VCARD,
112         STOCK_PIXMAP_ONLINE,
113         STOCK_PIXMAP_OFFLINE,
114         STOCK_PIXMAP_NOTICE_WARN,               /* small warning */
115         STOCK_PIXMAP_NOTICE_ERROR,              /* small error   */
116         STOCK_PIXMAP_NOTICE_NOTE,               /* small message */
117         STOCK_PIXMAP_GO_FOLDERS,
118         STOCK_PIXMAP_MIME_TEXT_PLAIN,
119         STOCK_PIXMAP_MIME_TEXT_HTML,
120         STOCK_PIXMAP_MIME_TEXT_PATCH,
121         STOCK_PIXMAP_MIME_APPLICATION,
122         STOCK_PIXMAP_MIME_IMAGE,
123         STOCK_PIXMAP_MIME_AUDIO,
124         STOCK_PIXMAP_MIME_TEXT_ENRICHED,
125         STOCK_PIXMAP_MIME_UNKNOWN,
126         STOCK_PIXMAP_MIME_PDF,
127         STOCK_PIXMAP_MIME_PS,
128         STOCK_PIXMAP_MIME_TEXT_CALENDAR,
129         STOCK_PIXMAP_MIME_PGP_SIG,
130         STOCK_PIXMAP_PRINTER,
131         STOCK_PIXMAP_PRIVACY_SIGNED,
132         STOCK_PIXMAP_PRIVACY_PASSED,
133         STOCK_PIXMAP_PRIVACY_FAILED,
134         STOCK_PIXMAP_PRIVACY_UNKNOWN,
135         STOCK_PIXMAP_PRIVACY_EXPIRED,
136         STOCK_PIXMAP_PRIVACY_WARN,
137         STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED,
138         STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED,
139         STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED,
140         STOCK_PIXMAP_PRIVACY_EMBLEM_WARN,
141         STOCK_PIXMAP_MIME_MESSAGE,
142         STOCK_PIXMAP_SPAM_BTN,
143         STOCK_PIXMAP_HAM_BTN,
144         STOCK_PIXMAP_TRASH,
145         STOCK_PIXMAP_DELETE,
146         STOCK_PIXMAP_CANCEL,
147         STOCK_PIXMAP_EMPTY,              /* last entry */
148 };
149
150 typedef struct _ToolbarPage
151 {
152         PrefsPage  page;
153
154         GtkWidget *window;              /* do not modify */
155
156         ToolbarType source;
157         GList     *combo_action_list;   /* list of internal functions    */
158
159         GtkWidget *list_view_set;       /* toolbar items treeview        */
160         GtkWidget *item_text_entry;     /* item name                     */
161         GtkWidget *item_type_combo;     /* item type selection widget    */
162         GtkWidget *item_func_combo;     /* item internal function widget */
163         GtkWidget *item_action_combo;   /* item user action widget       */
164         GtkWidget *item_plugin_combo;   /* items registered by plugins */
165         GtkWidget *icon_button;         /* item icon chooser widget      */
166         
167         GtkWidget *icon_chooser_win;
168         GtkWidget *icon_chooser_view;
169         
170         gchar *item_icon_file;          /* item icon file                */
171
172 } ToolbarPage;
173
174 #define ERROR_MSG _("Selected Action already set.\nPlease choose another Action from List")
175 #define ERROR_MSG_NO_ICON _("Item has no icon defined.")
176 #define ERROR_MSG_NO_TEXT _("Item has no text defined.")
177
178 typedef struct _ToolbarPluginItem ToolbarPluginItem;
179 struct _ToolbarPluginItem {
180         gchar *plugin;
181         gchar *item_name;
182         ToolbarPluginCallback cb;
183         gpointer cb_data;
184 };
185
186 /* items registered by plugins */
187 static GHashTable *plugin_items_mainwin = NULL;
188 static GHashTable *plugin_items_compose = NULL;
189 static GHashTable *plugin_items_msgview = NULL;
190
191 static void prefs_toolbar_populate               (ToolbarPage *prefs_toolbar);
192
193 static void get_action_name                      (const gchar *entry, 
194                                                   gchar **menu);
195                                                   
196 static gboolean is_duplicate                     (ToolbarPage *prefs_toolbar,
197                                                   gchar            *chosen_action);
198 static void prefs_toolbar_save                   (PrefsPage        *_page);
199
200 static void prefs_toolbar_register               (GtkButton        *button,
201                                                   ToolbarPage *prefs_toolbar);
202 static void prefs_toolbar_substitute             (GtkButton        *button,
203                                                   ToolbarPage *prefs_toolbar);
204 static void prefs_toolbar_delete                 (GtkButton        *button,
205                                                   ToolbarPage *prefs_toolbar);
206
207 static void prefs_toolbar_up                     (GtkButton        *button,
208                                                   ToolbarPage *prefs_toolbar);
209
210 static void prefs_toolbar_down                   (GtkButton        *button,
211                                                   ToolbarPage *prefs_toolbar);
212
213 static void action_selection_changed             (GtkComboBox *action_combo,
214                                                   ToolbarPage *prefs_toolbar);
215 static void plugin_selection_changed             (GtkComboBox *action_combo,
216                                                   ToolbarPage *prefs_toolbar);
217
218 static void func_selection_changed               (GtkComboBox *action_combo,
219                                                   ToolbarPage *prefs_toolbar);
220
221 static void prefs_toolbar_create                 (ToolbarPage *prefs_toolbar);
222
223 static GtkWidget *create_set_list_view           (ToolbarPage *prefs_toolbar);
224
225 static gboolean set_list_selected                (GtkTreeSelection *selector,
226                                                   GtkTreeModel *model, 
227                                                   GtkTreePath *path,
228                                                   gboolean currently_selected,
229                                                   ToolbarPage *prefs_toolbar);
230
231 static void icon_chooser_create                  (GtkButton *button,
232                                                   ToolbarPage *prefs_toolbar);
233
234
235 static GHashTable** get_plugin_hash_from_toolbar_type(ToolbarType toolbar_type)
236 {
237         if (toolbar_type == TOOLBAR_MAIN)
238                 return &plugin_items_mainwin;
239         else if (toolbar_type == TOOLBAR_COMPOSE)
240                 return &plugin_items_compose;
241         else if (toolbar_type == TOOLBAR_MSGVIEW)
242                 return &plugin_items_msgview;
243         else
244                 return NULL;
245 }
246
247 static void prefs_toolbar_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
248 {
249         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
250         gchar *win_titles[3];
251         win_titles[TOOLBAR_MAIN]    = _("Main toolbar configuration");
252         win_titles[TOOLBAR_COMPOSE] = _("Compose toolbar configuration");  
253         win_titles[TOOLBAR_MSGVIEW] = _("Message view toolbar configuration");  
254
255         prefs_toolbar->window = GTK_WIDGET(window);
256
257         toolbar_read_config_file(prefs_toolbar->source);
258
259         prefs_toolbar_create(prefs_toolbar);
260         prefs_toolbar_populate(prefs_toolbar);
261 }
262
263 static void prefs_toolbar_save(PrefsPage *_page)
264 {
265         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
266         GtkTreeView *list_view = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
267         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
268         GtkTreeIter iter;
269         
270         toolbar_clear_list(prefs_toolbar->source);
271
272         if (!gtk_tree_model_iter_n_children(model, NULL)
273         ||  !gtk_tree_model_get_iter_first(model, &iter))
274                 toolbar_set_default(prefs_toolbar->source);
275         else {
276                 do {
277                         ToolbarItem *item;
278                         gchar *fname, *text, *event; 
279                         
280                         item = g_new0(ToolbarItem, 1);
281
282                         gtk_tree_model_get(model, &iter,
283                                            SET_FILENAME, &fname,
284                                            SET_TEXT, &text,
285                                            SET_EVENT, &event,
286                                            -1);
287
288                         /* XXX: remember that G_TYPE_STRING returned by model
289                          * is owned by caller of gtk_tree_model_get() */
290                         item->file  = fname;
291                         item->text  = text;
292                         item->index = toolbar_ret_val_from_descr(event);
293                         g_free(event);
294
295                         toolbar_set_list_item(item, prefs_toolbar->source);
296
297                         g_free(item->file);
298                         g_free(item->text);
299                         g_free(item);
300                 } while (gtk_tree_model_iter_next(model, &iter));
301         }
302
303         toolbar_save_config_file(prefs_toolbar->source);
304
305         if (prefs_toolbar->source == TOOLBAR_MAIN) 
306                 toolbar_update(TOOLBAR_MAIN, mainwindow_get_mainwindow());
307         else if (prefs_toolbar->source == TOOLBAR_COMPOSE)
308                 compose_reflect_prefs_pixmap_theme();
309         else if (prefs_toolbar->source == TOOLBAR_MSGVIEW)
310                 messageview_reflect_prefs_pixmap_theme();
311 }
312
313 static void prefs_toolbar_destroy_widget(PrefsPage *_page)
314 {
315         ToolbarPage *prefs_toolbar = (ToolbarPage *) _page;
316
317         g_list_free(prefs_toolbar->combo_action_list);
318         prefs_toolbar->combo_action_list = NULL;
319 }
320
321 static void prefs_toolbar_set_displayed(ToolbarPage *prefs_toolbar)
322 {
323         GSList *cur;
324         GtkTreeView *list_view_set = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
325         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
326                                                 (list_view_set));
327         GSList *toolbar_list = toolbar_get_list(prefs_toolbar->source);
328         GtkTreeIter iter;
329
330         gtk_list_store_clear(store);
331
332         /* set currently active toolbar entries */
333         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
334                 ToolbarItem *item = (ToolbarItem*) cur->data;
335
336                 gtk_list_store_append(store, &iter);
337         
338                 if (item->index != A_SEPARATOR) {
339                         GdkPixbuf *pix;
340                         StockPixmap icon = stock_pixmap_get_icon(item->file);
341                         
342                         stock_pixbuf_gdk(prefs_toolbar->window, icon, &pix);
343
344                         gtk_list_store_set(store, &iter, 
345                                            SET_ICON, pix,
346                                            SET_FILENAME, item->file,
347                                            SET_TEXT, item->text,
348                                            SET_EVENT, toolbar_ret_descr_from_val(item->index),
349                                            SET_ICON_TEXT, NULL, 
350                                            SET_ICON_IS_TEXT, FALSE,
351                                            -1);
352                 } else {
353                         gtk_list_store_set(store, &iter,
354                                            SET_ICON, NULL,
355                                            SET_FILENAME, toolbar_ret_descr_from_val(A_SEPARATOR),
356                                            SET_TEXT, (const gchar *) "", 
357                                            SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
358                                            SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
359                                            SET_ICON_IS_TEXT, TRUE,
360                                            -1);
361                 }
362         }
363
364         /* select first */
365         gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
366         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
367                                                 (list_view_set),
368                                        &iter);  
369 }
370
371 static void add_item_to_plugin_combo(gpointer key, gpointer data, gpointer combo_box)
372 {
373         gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), (const gchar*)key);
374 }
375
376 static void prefs_toolbar_populate(ToolbarPage *prefs_toolbar)
377 {
378         GSList *cur;
379         gchar *act, *act_name;
380         GHashTable **hash;
381
382         prefs_toolbar->combo_action_list = toolbar_get_action_items(prefs_toolbar->source);
383         combobox_set_popdown_strings(GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
384                                      prefs_toolbar->combo_action_list);
385         
386         /* get currently defined sylpheed actions */
387         if (prefs_common.actions_list != NULL) {
388                 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
389                         act = (gchar *)cur->data;
390                         get_action_name(act, &act_name);
391                         
392                         gtk_combo_box_append_text(
393                                 GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
394                                 act_name);
395
396                         g_free(act_name);
397                 } 
398
399         } else
400                 combobox_set_sensitive(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
401                                         ITEM_USER_ACTION, FALSE);
402         
403         /* items registered by plugins */
404         hash = get_plugin_hash_from_toolbar_type(prefs_toolbar->source);
405         if (hash && *hash)
406                 g_hash_table_foreach(*hash, add_item_to_plugin_combo, 
407                                 prefs_toolbar->item_plugin_combo);
408         else
409                 combobox_set_sensitive(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
410                                         ITEM_PLUGIN, FALSE);
411
412         gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_func_combo), 0);
413         gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_action_combo), 0);
414         gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_plugin_combo), 0);
415         
416         prefs_toolbar_set_displayed(prefs_toolbar);
417
418         toolbar_clear_list(prefs_toolbar->source);
419 }
420
421 static gboolean is_duplicate(ToolbarPage *prefs_toolbar, gchar *chosen_action)
422 {
423         GtkTreeView *list_view_set = GTK_TREE_VIEW
424                                         (prefs_toolbar->list_view_set);
425         GtkTreeModel *model_set = gtk_tree_view_get_model(list_view_set);                                       
426         gchar *entry;
427         GtkTreeIter iter;
428         gboolean result;
429
430         cm_return_val_if_fail(chosen_action != NULL, TRUE);
431
432         if (!gtk_tree_model_iter_n_children(model_set, NULL))
433                 return FALSE;
434         
435         if (!gtk_tree_model_get_iter_first(model_set, &iter))
436                 return FALSE;
437
438         result = FALSE;
439         do {
440                 gtk_tree_model_get(model_set, &iter,
441                                    SET_EVENT, &entry, 
442                                    -1);
443                 if (g_utf8_collate(chosen_action, entry) == 0) 
444                         result = TRUE;
445                 g_free(entry);                  
446         } while (!result && gtk_tree_model_iter_next(model_set, &iter));
447
448         return result;
449 }
450
451 static void prefs_toolbar_default(GtkButton *button, ToolbarPage *prefs_toolbar)
452 {
453         toolbar_clear_list(prefs_toolbar->source);
454         toolbar_set_default(prefs_toolbar->source);
455         prefs_toolbar_set_displayed(prefs_toolbar);
456 }
457
458 /*!
459  *\return       String that should be freed by caller.
460  */
461 static void get_action_name(const gchar *entry, gchar **menu)
462 {
463         gchar *act, *act_p;
464         
465         *menu = NULL;
466
467         if (prefs_common.actions_list != NULL) {
468                 
469                 act = g_strdup(entry);
470                 act_p = strstr(act, ": ");
471                 if (act_p != NULL)
472                         act_p[0] = 0x00;
473                 /* freed by calling func */
474                 *menu = act;
475         }
476 }
477
478 static void prefs_toolbar_register(GtkButton *button, ToolbarPage *prefs_toolbar)
479 {
480         GtkTreeView *list_view_set   = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
481         gint item_type = gtk_combo_box_get_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo));
482         GtkListStore *store_set;
483         GtkTreeIter iter;
484
485         store_set = GTK_LIST_STORE(gtk_tree_view_get_model(list_view_set));
486
487         /* SEPARATOR or other ? */
488         if (item_type == ITEM_SEPARATOR) {
489                 gtk_list_store_append(store_set, &iter);
490                 gtk_list_store_set(store_set, &iter,
491                                    SET_ICON, NULL,
492                                    SET_FILENAME, prefs_toolbar->item_icon_file,
493                                    SET_TEXT, NULL,
494                                    SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
495                                    SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
496                                    SET_ICON_IS_TEXT, TRUE,
497                                    -1);
498         } else {
499                 GdkPixbuf *pixbuf;
500                 gchar *event, *text;
501
502                 if (prefs_toolbar->item_icon_file == NULL) {
503                         alertpanel_error(ERROR_MSG_NO_ICON);
504                         return;
505                 }
506                 stock_pixbuf_gdk(prefs_toolbar->window, 
507                                  stock_pixmap_get_icon(prefs_toolbar->item_icon_file),
508                                  &pixbuf);
509                 if(pixbuf == NULL) {
510                         alertpanel_error(ERROR_MSG_NO_ICON);
511                         return;
512                 }
513                                 
514                 if (item_type == ITEM_FUNCTION) {
515                         event = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
516                                                 prefs_toolbar->item_func_combo));
517                                                 
518                         if (is_duplicate(prefs_toolbar, event)) {
519                                 alertpanel_error(ERROR_MSG);
520                                 g_free(event);
521                                 return;
522                         }
523                 } else if(item_type == ITEM_PLUGIN)
524                   event = toolbar_ret_descr_from_val(A_CLAWS_PLUGINS);
525                 else
526                         event = toolbar_ret_descr_from_val(A_CLAWS_ACTIONS);
527                 
528                 text = gtk_editable_get_chars(
529                         GTK_EDITABLE(prefs_toolbar->item_text_entry), 0 , -1);
530
531                 if (text != NULL) {
532                         gtk_list_store_append(store_set, &iter);
533                         gtk_list_store_set(store_set, &iter,
534                                            SET_ICON, pixbuf,
535                                            SET_FILENAME, prefs_toolbar->item_icon_file,
536                                            SET_TEXT, text,
537                                            SET_EVENT, event,
538                                            SET_ICON_TEXT, NULL,
539                                            SET_ICON_IS_TEXT, FALSE,
540                                            -1);
541                 } else {
542                         alertpanel_error(ERROR_MSG_NO_TEXT);
543                         return;
544                 }
545                 
546                 g_free(text);
547                 if((item_type != ITEM_USER_ACTION) && (item_type != ITEM_PLUGIN))
548                         g_free(event);
549         }
550         
551         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
552                                                 (list_view_set),
553                                        &iter);
554 }
555
556 static void prefs_toolbar_substitute(GtkButton *button, ToolbarPage *prefs_toolbar)
557 {
558         GtkTreeView *list_view_set   = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
559         GtkListStore *store_set   = GTK_LIST_STORE(gtk_tree_view_get_model(list_view_set));
560         gint item_type = gtk_combo_box_get_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo));
561         GtkTreeSelection *sel_set;
562         GtkTreeIter iter_set;
563
564         if (!gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_set), NULL))
565                 return;
566                 
567         sel_set = gtk_tree_view_get_selection(list_view_set);           
568         if (!gtk_tree_selection_get_selected(sel_set, NULL, &iter_set))
569                 return;
570
571         if (item_type == ITEM_SEPARATOR) {
572                 gtk_list_store_set(store_set, &iter_set, 
573                                    SET_ICON, NULL,
574                                    SET_TEXT, NULL,
575                                    SET_EVENT, toolbar_ret_descr_from_val(A_SEPARATOR),
576                                    SET_FILENAME, prefs_toolbar->item_icon_file,
577                                    SET_ICON_TEXT, (const gchar *) SEPARATOR_PIXMAP,
578                                    SET_ICON_IS_TEXT, TRUE,
579                                    -1);
580         } else {
581                 GdkPixbuf *pixbuf;
582                 gchar *icon_event, *set_event, *text;
583
584                 if (prefs_toolbar->item_icon_file == NULL) {
585                         alertpanel_error(ERROR_MSG_NO_ICON);
586                         return;
587                 }
588                 stock_pixbuf_gdk(prefs_toolbar->window, 
589                                  stock_pixmap_get_icon(prefs_toolbar->item_icon_file),
590                                  &pixbuf);
591                 if(pixbuf == NULL) {
592                         alertpanel_error(ERROR_MSG_NO_ICON);
593                         return;
594                 }
595
596                 gtk_tree_model_get(GTK_TREE_MODEL(store_set), &iter_set, 
597                                                   SET_EVENT, &set_event,
598                                                   -1);
599                 
600                 if (item_type == ITEM_FUNCTION) {
601                         icon_event = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
602                                                 prefs_toolbar->item_func_combo));
603                                                 
604                         if (is_duplicate(prefs_toolbar, icon_event)
605                         && g_utf8_collate(icon_event, set_event) != 0){
606                                 alertpanel_error(ERROR_MSG);
607                                 g_free(icon_event);
608                                 g_free(set_event);
609                                 return;
610                         }
611                 } else if(item_type == ITEM_PLUGIN)
612                         icon_event = toolbar_ret_descr_from_val(A_CLAWS_PLUGINS);
613                 else
614                         icon_event = toolbar_ret_descr_from_val(A_CLAWS_ACTIONS);
615                 
616                 text = gtk_editable_get_chars(
617                         GTK_EDITABLE(prefs_toolbar->item_text_entry), 0 , -1);
618
619                 /* change the row */
620                 if (text != NULL) {
621                         gtk_list_store_set(store_set, &iter_set,
622                                            SET_ICON, pixbuf,
623                                            SET_FILENAME, prefs_toolbar->item_icon_file,
624                                            SET_TEXT, text,
625                                            SET_EVENT, icon_event,
626                                            SET_ICON_TEXT, NULL,
627                                            SET_ICON_IS_TEXT, FALSE,
628                                            -1);
629                 } else 
630                         alertpanel_error(ERROR_MSG_NO_TEXT);
631                                 
632                 g_free(text);
633                 g_free(set_event);
634                 if((item_type != ITEM_USER_ACTION) && (item_type != ITEM_PLUGIN))
635                         g_free(icon_event);
636         }
637 }
638
639 static void prefs_toolbar_delete(GtkButton *button, ToolbarPage *prefs_toolbar)
640 {
641         GtkTreeView *list_view_set = GTK_TREE_VIEW(prefs_toolbar->list_view_set);
642         GtkTreeModel *store_set = gtk_tree_view_get_model(list_view_set);
643         GtkTreeIter iter_set;
644         GtkTreePath *path;                                                      
645
646         if (!gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store_set), NULL))
647                 return;
648         
649         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
650                                                         (list_view_set),
651                                              NULL,
652                                              &iter_set))
653                 return;                                      
654
655         /* select prev list item, if deleted was first select next */
656         path = gtk_tree_model_get_path(store_set, &iter_set);
657         gtk_tree_path_prev(path);
658         
659         gtk_list_store_remove(GTK_LIST_STORE(store_set), &iter_set);
660
661         gtk_tree_selection_select_path(
662                         gtk_tree_view_get_selection(list_view_set),
663                         path);
664
665         gtk_tree_path_free(path);
666 }
667
668 static void prefs_toolbar_up(GtkButton *button, ToolbarPage *prefs_toolbar)
669 {
670         GtkTreePath *prev, *sel;
671         GtkTreeIter isel;
672         GtkListStore *store = NULL;
673         GtkTreeModel *model = NULL;
674         GtkTreeIter iprev;
675         
676         if (!gtk_tree_selection_get_selected
677                 (gtk_tree_view_get_selection
678                         (GTK_TREE_VIEW(prefs_toolbar->list_view_set)),
679                  &model,        
680                  &isel))
681                 return;
682         store = (GtkListStore *)model;
683
684         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
685         if (!sel)
686                 return;
687         
688         /* no move if we're at row 0... */
689         prev = gtk_tree_path_copy(sel);
690         if (!gtk_tree_path_prev(prev)) {
691                 gtk_tree_path_free(prev);
692                 gtk_tree_path_free(sel);
693                 return;
694         }
695
696         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
697                                 &iprev, prev);
698         gtk_tree_path_free(sel);
699         gtk_tree_path_free(prev);
700
701         gtk_list_store_swap(store, &iprev, &isel);
702 }
703
704 static void prefs_toolbar_down(GtkButton *button, ToolbarPage *prefs_toolbar)
705 {
706         GtkListStore *store = NULL;
707         GtkTreeModel *model = NULL;
708         GtkTreeIter next, sel;
709         
710         if (!gtk_tree_selection_get_selected
711                 (gtk_tree_view_get_selection
712                         (GTK_TREE_VIEW(prefs_toolbar->list_view_set)),
713                  &model,
714                  &sel))
715                 return;
716
717         store = (GtkListStore *)model;
718         next = sel;
719         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
720                 return;
721
722         gtk_list_store_swap(store, &next, &sel);
723 }
724
725 static void item_type_changed(GtkComboBox *item_type_combo,
726                                 ToolbarPage *prefs_toolbar)
727 {
728         gint active = gtk_combo_box_get_active(item_type_combo);
729
730         switch(active) {
731         case ITEM_FUNCTION:
732                 gtk_widget_show(prefs_toolbar->item_func_combo);
733                 gtk_widget_hide(prefs_toolbar->item_action_combo);
734                 gtk_widget_hide(prefs_toolbar->item_plugin_combo);
735                 gtk_combo_box_set_active(
736                         GTK_COMBO_BOX(prefs_toolbar->item_func_combo), 0);
737                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
738                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, TRUE);
739                 gtk_widget_set_sensitive(prefs_toolbar->item_func_combo, TRUE);
740                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
741
742                 func_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
743                                         prefs_toolbar);
744                 break;
745         case ITEM_USER_ACTION:
746                 gtk_widget_show(prefs_toolbar->item_action_combo);
747                 gtk_widget_hide(prefs_toolbar->item_func_combo);
748                 gtk_widget_hide(prefs_toolbar->item_plugin_combo);
749                 gtk_combo_box_set_active(
750                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo), 0);
751                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
752                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
753                 gtk_widget_set_sensitive(prefs_toolbar->item_action_combo, TRUE);
754                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
755                 
756                 action_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
757                                         prefs_toolbar);         
758                 break;
759         case ITEM_SEPARATOR:
760                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), _("None"));
761                 gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button), NULL);
762                 g_free(prefs_toolbar->item_icon_file);
763                 prefs_toolbar->item_icon_file = NULL;
764                 gtk_combo_box_set_active(
765                         GTK_COMBO_BOX(prefs_toolbar->item_func_combo), -1);
766                 gtk_combo_box_set_active(
767                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo), -1);
768                 gtk_combo_box_set_active(
769                         GTK_COMBO_BOX(prefs_toolbar->item_plugin_combo), -1);
770                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), "");
771                 gtk_widget_set_sensitive(prefs_toolbar->item_action_combo, FALSE);
772                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
773                 gtk_widget_set_sensitive(prefs_toolbar->item_plugin_combo, FALSE);
774                 gtk_widget_set_sensitive(prefs_toolbar->item_func_combo, FALSE);
775                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, FALSE);
776                 break;
777           case ITEM_PLUGIN:
778                 gtk_widget_show(prefs_toolbar->item_plugin_combo);
779                 gtk_widget_hide(prefs_toolbar->item_func_combo);
780                 gtk_widget_hide(prefs_toolbar->item_action_combo);
781                 gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_plugin_combo), 0);
782                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
783                 gtk_widget_set_sensitive(prefs_toolbar->item_text_entry, FALSE);
784                 gtk_widget_set_sensitive(prefs_toolbar->item_plugin_combo, TRUE);
785                 gtk_widget_set_sensitive(prefs_toolbar->icon_button, TRUE);
786                 plugin_selection_changed(GTK_COMBO_BOX(prefs_toolbar->item_plugin_combo), prefs_toolbar);
787                 break;
788         }
789
790 }
791
792 static void action_selection_changed(GtkComboBox *action_combo,
793                                 ToolbarPage *prefs_toolbar)
794 {
795         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
796                            prefs_toolbar->item_action_combo));
797
798         if(text != NULL) { /* action */
799                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), text);
800                 g_free(text);
801         } 
802 }
803
804 static void plugin_selection_changed(GtkComboBox *action_combo,
805                 ToolbarPage *prefs_toolbar)
806 {
807         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(prefs_toolbar->item_plugin_combo));
808
809         if (text != NULL) { /* action */
810                 gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), text);
811                 g_free(text);
812         }
813 }
814
815 static void func_selection_changed(GtkComboBox *action_combo,
816                                 ToolbarPage *prefs_toolbar)
817 {
818         gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
819                            prefs_toolbar->item_func_combo));
820
821         if(text != NULL) { /* action */
822                 int action = -1;
823                 action = toolbar_ret_val_from_descr(text);
824                 if (action >= 0)
825                         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), 
826                                         toolbar_get_short_text(action));
827                 g_free(text);
828                 if (action >= 0) {
829                         StockPixmap stockp = toolbar_get_icon(action);
830                         if (stockp >= 0)  {
831                                 g_free(prefs_toolbar->item_icon_file);
832                                 prefs_toolbar->item_icon_file = g_strdup(stock_pixmap_get_name(stockp));
833
834                                 gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
835                                      stock_pixmap_widget(prefs_toolbar->window, stockp));
836                         }
837                 }
838         } 
839 }
840
841 static void prefs_toolbar_create(ToolbarPage *prefs_toolbar)
842 {
843         GtkWidget *main_vbox;
844         GtkWidget *toolbar_item_hbox;
845         GtkWidget *icon_vbox;
846         GtkWidget *icon_label;
847         GtkWidget *icon_button;
848         GtkWidget *icon_hbox;
849         GtkWidget *item_type_combo;
850         GtkListStore *item_type_model;
851         GtkTreeIter iter;
852         GtkWidget *item_action_combo;
853         GtkWidget *item_plugin_combo;
854         GtkWidget *item_func_combo;
855         GtkWidget *reg_hbox;
856         GtkWidget *arrow;
857         GtkWidget *btn_hbox;
858 #ifdef GENERIC_UMPC
859         GtkWidget *hbox;
860 #endif
861         GtkWidget *reg_btn;
862         GtkWidget *subst_btn;
863         GtkWidget *del_btn;
864         GtkWidget *default_btn;
865         GtkWidget *vbox_frame;
866         GtkWidget *table;
867         GtkWidget *label_icon_text;
868         GtkWidget *item_text_entry;
869         GtkWidget *vbox_toolbar_items;
870         GtkWidget *hbox_bottom;
871         GtkWidget *scrolledwindow_list_view_set;
872         GtkWidget *list_view_set;
873         GtkWidget *label;
874
875         GtkWidget *btn_vbox;
876         GtkWidget *up_btn;
877         GtkWidget *down_btn;
878
879         debug_print("Creating custom toolbar window...\n");
880
881         main_vbox = gtk_vbox_new(FALSE, 0);
882         gtk_widget_show(main_vbox);
883
884         vbox_frame = gtk_frame_new(_("Toolbar item"));
885         gtk_widget_show(vbox_frame);
886         gtk_box_pack_start(GTK_BOX(main_vbox), vbox_frame, FALSE, TRUE, 0);
887
888         toolbar_item_hbox = gtk_hbox_new (FALSE, 4);
889         gtk_widget_show(toolbar_item_hbox);
890         gtk_container_add(GTK_CONTAINER (vbox_frame), toolbar_item_hbox);
891         
892         table = gtk_table_new (3, 3, FALSE);
893         gtk_box_pack_start(GTK_BOX(toolbar_item_hbox), table,
894                            TRUE, TRUE, 0);
895         gtk_container_set_border_width (GTK_CONTAINER (table), 8);
896         gtk_table_set_row_spacings (GTK_TABLE (table), 8);
897         gtk_table_set_col_spacings (GTK_TABLE (table), 8);
898
899         /* toolbar item type */
900         label = gtk_label_new(_("Item type"));
901         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
902         gtk_widget_show(label);
903         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
904                          (GtkAttachOptions) (GTK_FILL),
905                          (GtkAttachOptions) (0), 0, 0); 
906         
907         item_type_combo = gtkut_sc_combobox_create(NULL, TRUE);
908         item_type_model = GTK_LIST_STORE(gtk_combo_box_get_model(
909                                          GTK_COMBO_BOX(item_type_combo)));
910         COMBOBOX_ADD(item_type_model, _("Internal Function"), ITEM_FUNCTION);
911         COMBOBOX_ADD(item_type_model, _("User Action"), ITEM_USER_ACTION);
912         COMBOBOX_ADD(item_type_model, _("Plugins"), ITEM_PLUGIN);
913         COMBOBOX_ADD(item_type_model, _("Separator"), ITEM_SEPARATOR);  
914         gtk_widget_set_size_request(item_type_combo, 200, -1);
915         gtk_table_attach(GTK_TABLE(table), item_type_combo, 1, 3, 0, 1,
916                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
917                          (GtkAttachOptions) (0), 0, 0);
918
919         /* available actions */
920         label = gtk_label_new(_("Event executed on click"));
921         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
922         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
923                           (GtkAttachOptions) (GTK_FILL),
924                           (GtkAttachOptions) (0), 0, 0);
925
926         item_action_combo = gtk_combo_box_new_text();
927         gtk_widget_set_size_request(item_action_combo, 200, -1);
928         gtk_table_attach (GTK_TABLE (table), item_action_combo, 1, 3, 1, 2,
929                           (GtkAttachOptions) (GTK_FILL),
930                           (GtkAttachOptions) (0), 0, 0);
931                           
932         /* available internal functions */
933         item_func_combo = gtk_combo_box_new_text();
934         gtk_widget_set_size_request(item_func_combo, 200, -1);
935         gtk_table_attach (GTK_TABLE (table), item_func_combo, 1, 3, 1, 2,
936                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
937                           (GtkAttachOptions) (0), 0, 0);
938         
939         /* plugin-registered items */
940         item_plugin_combo = gtk_combo_box_new_text();
941         gtk_widget_set_size_request(item_plugin_combo, 200, -1);
942         gtk_table_attach(GTK_TABLE(table), item_plugin_combo, 1, 3, 1, 2,
943                          (GtkAttachOptions) (GTK_FILL),
944                          (GtkAttachOptions) (0), 0, 0);
945
946         /* toolbar item description */
947         label_icon_text = gtk_label_new(_("Toolbar text"));
948         gtk_misc_set_alignment(GTK_MISC(label_icon_text), 0, 0.5);
949         gtk_widget_show (label_icon_text);
950         gtk_table_attach (GTK_TABLE (table), label_icon_text, 0, 1, 2, 3,
951                           (GtkAttachOptions) (GTK_FILL),
952                           (GtkAttachOptions) (0), 0, 0);
953
954         item_text_entry = gtk_entry_new();
955         gtk_table_attach (GTK_TABLE (table), item_text_entry, 1, 3, 2, 3,
956                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
957                           (GtkAttachOptions) (0), 0, 0);
958
959         icon_vbox = gtk_vbox_new(FALSE, VBOX_BORDER);
960         gtk_widget_show(icon_vbox);
961         
962         icon_label = gtk_label_new(_("Icon"));
963         gtk_widget_set_size_request(icon_label, 100, -1);
964         gtk_box_pack_start(GTK_BOX(icon_vbox), icon_label, FALSE, FALSE, 0);
965         
966         icon_hbox = gtk_hbox_new(FALSE, 0);
967         gtk_widget_show(icon_hbox);
968         
969         label = gtk_label_new("");
970         gtk_box_pack_start(GTK_BOX(icon_hbox), label, TRUE, TRUE, 0);
971         
972         icon_button = gtk_button_new();
973         gtk_widget_show(icon_button);
974         gtk_widget_set_size_request(icon_button, 50, 50);
975         g_signal_connect(G_OBJECT(icon_button), "clicked",
976                          G_CALLBACK(icon_chooser_create), prefs_toolbar);
977         gtk_box_pack_start(GTK_BOX(icon_hbox), icon_button, FALSE, FALSE, 8);
978
979         label = gtk_label_new("");
980         gtk_box_pack_start(GTK_BOX(icon_hbox), label, TRUE, TRUE, 0);   
981         
982         gtk_box_pack_start(GTK_BOX(icon_vbox), icon_hbox, FALSE, FALSE, 0);
983         
984         gtk_box_pack_start(GTK_BOX(toolbar_item_hbox), icon_vbox, FALSE, FALSE, 0);
985                 
986         /* register / substitute / delete */
987         reg_hbox = gtk_hbox_new(FALSE, 4);
988         gtk_box_pack_start(GTK_BOX(main_vbox), reg_hbox, FALSE, FALSE, 0);
989         gtk_container_set_border_width(GTK_CONTAINER(reg_hbox), 10);
990
991         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
992         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
993         gtk_widget_set_size_request(arrow, -1, 16);
994
995         btn_hbox = gtk_hbox_new(TRUE, 4);
996         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
997
998         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
999         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
1000         g_signal_connect(G_OBJECT(reg_btn), "clicked",
1001                          G_CALLBACK(prefs_toolbar_register), 
1002                          prefs_toolbar);
1003
1004         subst_btn = gtkut_get_replace_btn(_("Replace"));
1005         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
1006         g_signal_connect(G_OBJECT(subst_btn), "clicked",
1007                          G_CALLBACK(prefs_toolbar_substitute),
1008                          prefs_toolbar);
1009
1010         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1011         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
1012         g_signal_connect(G_OBJECT(del_btn), "clicked",
1013                          G_CALLBACK(prefs_toolbar_delete), 
1014                           prefs_toolbar);
1015
1016         default_btn = gtk_button_new_with_label(_(" Use default "));
1017 #ifndef GENERIC_UMPC
1018         gtk_box_pack_end(GTK_BOX(reg_hbox), default_btn, FALSE, TRUE, 0);
1019 #else
1020         hbox = gtk_hbox_new(FALSE, 0);
1021         gtk_box_pack_start(GTK_BOX(main_vbox), hbox, FALSE, FALSE, 0);
1022         gtk_box_pack_start(GTK_BOX(hbox), default_btn, FALSE, FALSE, 0);
1023 #endif
1024         g_signal_connect(G_OBJECT(default_btn), "clicked",
1025                          G_CALLBACK(prefs_toolbar_default), 
1026                          prefs_toolbar);
1027
1028         /* currently active toolbar items */
1029         vbox_toolbar_items = gtk_vbox_new(FALSE, VBOX_BORDER);
1030         gtk_box_pack_start(GTK_BOX(main_vbox), vbox_toolbar_items, TRUE, TRUE, 0);
1031         
1032         hbox_bottom = gtk_hbox_new(FALSE, 0);
1033         gtk_container_add(GTK_CONTAINER(vbox_toolbar_items), hbox_bottom);
1034         
1035         scrolledwindow_list_view_set = gtk_scrolled_window_new(NULL, NULL);
1036         gtk_box_pack_start(GTK_BOX(hbox_bottom), scrolledwindow_list_view_set, TRUE, TRUE, 0);
1037         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_list_view_set), 1);
1038         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_list_view_set), 
1039                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1040         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow_list_view_set),
1041                                             GTK_SHADOW_IN);
1042
1043         list_view_set = create_set_list_view(prefs_toolbar); 
1044         gtk_widget_show(list_view_set);
1045         gtk_container_add(GTK_CONTAINER(scrolledwindow_list_view_set), list_view_set);
1046         gtk_widget_set_size_request(list_view_set, 225, 120);
1047
1048         btn_vbox = gtk_vbox_new(FALSE, 8);
1049         gtk_widget_show(btn_vbox);
1050         gtk_box_pack_start(GTK_BOX(hbox_bottom), btn_vbox, FALSE, FALSE, 5);
1051
1052         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
1053         gtk_widget_show(up_btn);
1054         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 2);
1055
1056         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
1057         gtk_widget_show(down_btn);
1058         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
1059
1060         g_signal_connect(G_OBJECT(item_type_combo), "changed",
1061                          G_CALLBACK(item_type_changed), prefs_toolbar);
1062         g_signal_connect(G_OBJECT(item_action_combo), "changed",
1063                          G_CALLBACK(action_selection_changed), prefs_toolbar);
1064         g_signal_connect(G_OBJECT(item_plugin_combo), "changed",
1065                          G_CALLBACK(plugin_selection_changed), prefs_toolbar);
1066         g_signal_connect(G_OBJECT(item_func_combo), "changed",
1067                          G_CALLBACK(func_selection_changed), prefs_toolbar);
1068         g_signal_connect(G_OBJECT(up_btn), "clicked",
1069                          G_CALLBACK(prefs_toolbar_up), prefs_toolbar);
1070         g_signal_connect(G_OBJECT(down_btn), "clicked",
1071                          G_CALLBACK(prefs_toolbar_down), prefs_toolbar);
1072         
1073         gtk_widget_show_all(main_vbox);
1074
1075         prefs_toolbar->list_view_set    = list_view_set;
1076         prefs_toolbar->item_text_entry  = item_text_entry;
1077         prefs_toolbar->item_type_combo  = item_type_combo;
1078         prefs_toolbar->item_func_combo  = item_func_combo;
1079         prefs_toolbar->item_action_combo= item_action_combo;
1080         prefs_toolbar->item_plugin_combo= item_plugin_combo;
1081         prefs_toolbar->icon_button      = icon_button;
1082         prefs_toolbar->item_icon_file   = NULL;
1083         
1084         prefs_toolbar->page.widget = main_vbox;
1085 }
1086
1087 ToolbarPage *prefs_toolbar_mainwindow;
1088 ToolbarPage *prefs_toolbar_composewindow;
1089 ToolbarPage *prefs_toolbar_messageview;
1090
1091 static void toolbar_unregister_plugin_item_real(GHashTable *hash, 
1092                                         const gchar *plugin_name, 
1093                                         const gchar *item_name)
1094 {
1095         gchar *key;
1096
1097         if (!hash)
1098                 return;
1099
1100         key = g_strdup_printf(plugin_name, "/", item_name, NULL);
1101         g_hash_table_remove(hash, key);
1102         g_free(key);
1103 }
1104
1105 void prefs_toolbar_unregister_plugin_item(ToolbarType toolbar_type, 
1106                                         const gchar *plugin_name, 
1107                                         const gchar *item_name)
1108 {
1109         GHashTable **hash;
1110         hash = get_plugin_hash_from_toolbar_type(toolbar_type);
1111         if (hash)
1112                 toolbar_unregister_plugin_item_real(*hash, plugin_name, item_name);
1113 }
1114
1115 static void prefs_toolbar_execute_plugin_item_real(gpointer parent, 
1116                                 GHashTable *hash, const gchar *id)
1117 {
1118         ToolbarPluginItem *value;
1119         GSList *walk;
1120         gboolean found;
1121
1122         if (!hash) {
1123                 debug_print("No plugin registered toolbar items yet\n");
1124                 return;
1125         }
1126
1127         value = g_hash_table_lookup(hash, id);
1128         if (!value) {
1129                 debug_print("Could not find plugin toolbar item with id %s\n", id);
1130                 return;
1131         }
1132
1133         /* check if corresponding plugin is currently loaded */
1134         found = FALSE;
1135         for (walk = plugin_get_list(); walk; walk = walk->next) {
1136                 const gchar *plugin_name;
1137                 Plugin *plugin = walk->data;
1138                 plugin_name = plugin_get_name(plugin);
1139                 if (!strcmp(plugin_name, value->plugin)) {
1140                         found = TRUE;
1141                         break;
1142                 }
1143         }
1144         if (!found) {
1145                 debug_print("Plugin '%s' is currently not loaded, cannot execute toolbar action\n", value->plugin);
1146                 return;
1147         }
1148
1149         value->cb(parent, value->item_name, value->cb_data);
1150 }
1151
1152 void prefs_toolbar_execute_plugin_item(gpointer parent, 
1153                         ToolbarType toolbar_type, const gchar *id)
1154 {
1155         GHashTable **hash;
1156         hash = get_plugin_hash_from_toolbar_type(toolbar_type);
1157         if (hash)
1158                 prefs_toolbar_execute_plugin_item_real(parent, *hash, id);
1159 }
1160
1161 static void destroy_plugin_item_hash_value(ToolbarPluginItem *item)
1162 {
1163         g_free(item->plugin);
1164         g_free(item->item_name);
1165         g_free(item);
1166 }
1167
1168 static void prefs_toolbar_register_plugin_item_real(GHashTable **hash, 
1169                                         const gchar *plugin_name, 
1170                                         const gchar *item_name, 
1171                                         ToolbarPluginCallback cb, 
1172                                         gpointer cb_data)
1173 {
1174         gchar *key;
1175         ToolbarPluginItem *value;
1176
1177         cm_return_if_fail(plugin_name && item_name);
1178
1179         if (!*hash) {
1180                 *hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, 
1181                                 (GDestroyNotify) destroy_plugin_item_hash_value);
1182                 if (!*hash)
1183                         return;
1184         }
1185
1186         key = g_strconcat(plugin_name, "/", item_name, NULL);
1187         value = g_new0(ToolbarPluginItem, 1);
1188         value->plugin = g_strdup(plugin_name);
1189         value->item_name = g_strdup(item_name);
1190         value->cb = cb;
1191         value->cb_data = cb_data;
1192         g_hash_table_insert(*hash, key, value);
1193 }
1194
1195 void prefs_toolbar_register_plugin_item(ToolbarType toolbar_type, 
1196                                         const gchar *plugin_name, 
1197                                         const gchar *item_name, 
1198                                         ToolbarPluginCallback cb, 
1199                                         gpointer cb_data)
1200 {
1201         GHashTable **hash;
1202         hash = get_plugin_hash_from_toolbar_type(toolbar_type);
1203         if(hash)
1204                 prefs_toolbar_register_plugin_item_real(hash, plugin_name, 
1205                                                 item_name, cb, cb_data);
1206 }
1207
1208 void prefs_toolbar_init(void)
1209 {
1210         ToolbarPage *page;
1211         static gchar *mainpath[3], *messagepath[3], *composepath[3];
1212
1213         mainpath[0] = _("Toolbars");
1214         mainpath[1] = _("Main Window");
1215         mainpath[2] = NULL;
1216
1217         page = g_new0(ToolbarPage, 1);
1218         page->page.path = mainpath;
1219         page->page.create_widget = prefs_toolbar_create_widget;
1220         page->page.destroy_widget = prefs_toolbar_destroy_widget;
1221         page->page.save_page = prefs_toolbar_save;
1222         page->source = TOOLBAR_MAIN;
1223         page->page.weight = 50.0;
1224         prefs_gtk_register_page((PrefsPage *) page);
1225         prefs_toolbar_mainwindow = page;
1226
1227         messagepath[0] = _("Toolbars");
1228         messagepath[1] = _("Message Window");
1229         messagepath[2] = NULL;
1230
1231         page = g_new0(ToolbarPage, 1);
1232         page->page.path = messagepath;
1233         page->page.create_widget = prefs_toolbar_create_widget;
1234         page->page.destroy_widget = prefs_toolbar_destroy_widget;
1235         page->page.save_page = prefs_toolbar_save;
1236         page->source = TOOLBAR_MSGVIEW;
1237         page->page.weight = 45.0;
1238         prefs_gtk_register_page((PrefsPage *) page);
1239         prefs_toolbar_messageview = page;
1240
1241         composepath[0] = _("Toolbars");
1242         composepath[1] = _("Compose Window");
1243         composepath[2] = NULL;
1244
1245         page = g_new0(ToolbarPage, 1);
1246         page->page.path = composepath;
1247         page->page.create_widget = prefs_toolbar_create_widget;
1248         page->page.destroy_widget = prefs_toolbar_destroy_widget;
1249         page->page.save_page = prefs_toolbar_save;
1250         page->source = TOOLBAR_COMPOSE;
1251         page->page.weight = 40.0;
1252         prefs_gtk_register_page((PrefsPage *) page);
1253         prefs_toolbar_composewindow = page;
1254 }
1255
1256 void prefs_toolbar_done(void)
1257 {
1258         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_mainwindow);
1259         g_free(prefs_toolbar_mainwindow->item_icon_file);
1260         g_free(prefs_toolbar_mainwindow);
1261         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_composewindow);
1262         g_free(prefs_toolbar_composewindow->item_icon_file);
1263         g_free(prefs_toolbar_composewindow);
1264         prefs_gtk_unregister_page((PrefsPage *) prefs_toolbar_messageview);
1265         g_free(prefs_toolbar_messageview->item_icon_file);
1266         g_free(prefs_toolbar_messageview);
1267 }
1268
1269 void prefs_toolbar_update_action_btns(void) 
1270 {
1271         if (toolbar_check_action_btns(TOOLBAR_MAIN)) {
1272                 toolbar_save_config_file(TOOLBAR_MAIN);
1273                 toolbar_update(TOOLBAR_MAIN, mainwindow_get_mainwindow());
1274         }
1275
1276         if (toolbar_check_action_btns(TOOLBAR_COMPOSE)) {
1277                 toolbar_save_config_file(TOOLBAR_COMPOSE);
1278                 compose_reflect_prefs_pixmap_theme();
1279         }
1280
1281         if (toolbar_check_action_btns(TOOLBAR_MSGVIEW)) {
1282                 toolbar_save_config_file(TOOLBAR_MSGVIEW);
1283                 messageview_reflect_prefs_pixmap_theme();
1284         }
1285 }
1286
1287 static void set_visible_if_not_text(GtkTreeViewColumn *col,
1288                                     GtkCellRenderer   *renderer,
1289                                     GtkTreeModel      *model,
1290                                     GtkTreeIter       *iter,
1291                                     gpointer           user_data)
1292 {
1293         gboolean is_text;
1294         GdkPixbuf *pixbuf;
1295
1296         gtk_tree_model_get(model, iter, SET_ICON_IS_TEXT, &is_text, -1);
1297         if (is_text) {
1298                 g_object_set(renderer, "visible", FALSE, NULL); 
1299         } else {
1300                 pixbuf = NULL;
1301                 gtk_tree_model_get(model, iter, 
1302                                    SET_ICON, &pixbuf,
1303                                    -1);
1304                 /* note getting a pixbuf from a tree model increases
1305                  * its refcount ... */
1306                 g_object_unref(pixbuf);
1307                 
1308                 g_object_set(renderer, "visible", TRUE, NULL);
1309                 g_object_set(renderer, "pixbuf",  pixbuf, NULL);
1310         }
1311 }
1312
1313 static GtkWidget *create_set_list_view(ToolbarPage *prefs_toolbar)
1314 {
1315         GtkTreeView *list_view;
1316         GtkListStore *store;
1317         GtkCellRenderer *renderer;
1318         GtkTreeViewColumn *column;
1319         GtkTreeSelection *selector;
1320
1321         store = gtk_list_store_new(N_SET_COLUMNS, 
1322                                    GDK_TYPE_PIXBUF,
1323                                    G_TYPE_STRING,
1324                                    G_TYPE_STRING,
1325                                    G_TYPE_STRING,
1326                                    G_TYPE_STRING,
1327                                    G_TYPE_BOOLEAN,
1328                                    -1);
1329         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1330         g_object_unref(G_OBJECT(store));
1331
1332         column = gtk_tree_view_column_new();
1333         gtk_tree_view_column_set_title(column, _("Icon"));
1334         renderer = gtk_cell_renderer_pixbuf_new();
1335         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1336         gtk_tree_view_set_reorderable(list_view, TRUE);
1337         /* tell pixbuf renderer it is only visible if 
1338          * the icon is not represented by text */
1339         gtk_tree_view_column_set_cell_data_func(column, renderer,
1340                                                 set_visible_if_not_text,
1341                                                 NULL, NULL);
1342         
1343         renderer = gtk_cell_renderer_text_new();
1344         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1345         
1346         /* tell the text renderer it is only visible if the icon
1347          * is represented by an image */
1348         gtk_tree_view_column_set_attributes(column, renderer,
1349                                             "visible", SET_ICON_IS_TEXT,
1350                                             "text", SET_ICON_TEXT,
1351                                             NULL);
1352
1353         gtk_tree_view_append_column(list_view, column);
1354
1355         column = gtk_tree_view_column_new();
1356         gtk_tree_view_column_set_title(column, _("Icon text"));
1357         renderer = gtk_cell_renderer_text_new();
1358         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1359         gtk_tree_view_column_set_attributes(column, renderer,
1360                                             "text", SET_TEXT,
1361                                             NULL);
1362         gtk_tree_view_append_column(list_view, column);
1363
1364         column = gtk_tree_view_column_new();
1365         gtk_tree_view_column_set_title(column, _("Mapped event"));
1366         renderer = gtk_cell_renderer_text_new();
1367         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1368         gtk_tree_view_column_set_attributes(column, renderer,
1369                                             "text", SET_EVENT,
1370                                             NULL);
1371         gtk_tree_view_append_column(list_view, column);
1372
1373         /* various other tree view attributes */
1374         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1375         
1376         selector = gtk_tree_view_get_selection(list_view);
1377         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1378         gtk_tree_selection_set_select_function
1379                 (selector, (GtkTreeSelectionFunc) set_list_selected,
1380                  prefs_toolbar, NULL);
1381
1382         return GTK_WIDGET(list_view);   
1383
1384 }
1385
1386 static gboolean set_list_selected(GtkTreeSelection *selector,
1387                                   GtkTreeModel *model, 
1388                                   GtkTreePath *path,
1389                                   gboolean currently_selected,
1390                                   ToolbarPage *prefs_toolbar)
1391 {
1392
1393         GtkTreeIter iter;
1394         gchar *icon_text, *icon_file, *descr;
1395         GList *cur;
1396         GSList *cur2;
1397         gint item_num;
1398         GdkPixbuf *pix;
1399         
1400         if (currently_selected || !gtk_tree_model_get_iter(model, &iter, path))
1401                 return TRUE;
1402         
1403         gtk_tree_model_get(model, &iter,
1404                            SET_ICON, &pix,
1405                            SET_TEXT, &icon_text,
1406                            SET_EVENT, &descr,
1407                            SET_FILENAME, &icon_file,
1408                            -1);
1409         
1410         g_free(prefs_toolbar->item_icon_file);
1411         prefs_toolbar->item_icon_file = icon_file;
1412         gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
1413                              gtk_image_new_from_pixbuf(pix));
1414         
1415         if (g_utf8_collate(toolbar_ret_descr_from_val(A_SEPARATOR), descr) == 0) {
1416                 gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button),
1417                                     _("None"));
1418                 g_free(prefs_toolbar->item_icon_file);
1419                 prefs_toolbar->item_icon_file = NULL;
1420                 gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo),
1421                                         ITEM_SEPARATOR);
1422                 g_free(icon_text);
1423                 g_free(descr);
1424
1425                 return TRUE;
1426         }
1427         
1428         gtk_button_set_label(GTK_BUTTON(prefs_toolbar->icon_button), "");
1429         gtk_entry_set_text(GTK_ENTRY(prefs_toolbar->item_text_entry), 
1430                            icon_text);
1431
1432         if (g_utf8_collate(toolbar_ret_descr_from_val(A_CLAWS_ACTIONS), descr) == 0) {
1433                 gtk_combo_box_set_active(GTK_COMBO_BOX(
1434                         prefs_toolbar->item_type_combo), ITEM_USER_ACTION);
1435
1436                 for(cur2 = prefs_common.actions_list, item_num = 0; cur2 != NULL;
1437                     cur2 = cur2->next) {
1438                         gchar *item_string;
1439                         get_action_name((gchar *)cur2->data, &item_string);
1440                         
1441                         if(g_utf8_collate(item_string, icon_text) == 0) {
1442                                 gtk_combo_box_set_active(
1443                                         GTK_COMBO_BOX(prefs_toolbar->item_action_combo),
1444                                         item_num);
1445                                 g_free(item_string);
1446                                 break;
1447                         }
1448                         else {
1449                                 item_num++;
1450                                 g_free(item_string);
1451                         }
1452                 }
1453
1454                 gtk_widget_show(prefs_toolbar->item_action_combo);
1455                 gtk_widget_hide(prefs_toolbar->item_func_combo);
1456                 gtk_widget_hide(prefs_toolbar->item_plugin_combo);
1457                 
1458                 g_free(icon_text);
1459                 g_free(descr);
1460
1461                 return TRUE;
1462         }
1463
1464         if (g_utf8_collate(toolbar_ret_descr_from_val(A_CLAWS_PLUGINS), descr) == 0) {
1465                 gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_type_combo), ITEM_PLUGIN);
1466
1467                 gtk_combo_box_set_active(GTK_COMBO_BOX(prefs_toolbar->item_plugin_combo), 0);
1468
1469                 gtk_widget_show(prefs_toolbar->item_plugin_combo);
1470                 gtk_widget_hide(prefs_toolbar->item_func_combo);
1471                 gtk_widget_hide(prefs_toolbar->item_action_combo);
1472
1473                 g_free(descr);
1474                 g_free(icon_text);
1475                 return TRUE;
1476         }
1477         
1478         /* scan combo list for selected description an set combo item accordingly */
1479         for (cur = prefs_toolbar->combo_action_list, item_num = 0; cur != NULL; 
1480              cur = cur->next) {
1481                 gchar *item_str = (gchar*)cur->data;
1482                 if (g_utf8_collate(item_str, descr) == 0) {
1483                         gtk_combo_box_set_active(
1484                                 GTK_COMBO_BOX(prefs_toolbar->item_func_combo),
1485                                 item_num);
1486                         
1487                         break;
1488                 }
1489                 else
1490                         item_num++;
1491         }
1492
1493         gtk_combo_box_set_active(GTK_COMBO_BOX(
1494                         prefs_toolbar->item_type_combo),ITEM_FUNCTION);
1495         gtk_widget_hide(prefs_toolbar->item_action_combo);
1496         gtk_widget_show(prefs_toolbar->item_func_combo);
1497
1498         g_free(icon_text);
1499         g_free(descr);
1500
1501         return TRUE;
1502 }
1503
1504 static void icon_chooser_ok_clicked(GtkButton *button,
1505                                         ToolbarPage *prefs_toolbar)
1506 {
1507         GtkTreeModel *model;
1508         GtkTreeIter iter;
1509         GList *list;
1510         GdkPixbuf *pix;
1511         gchar *icon_file;
1512         
1513         cm_return_if_fail(prefs_toolbar != NULL);
1514
1515         model = gtk_icon_view_get_model(GTK_ICON_VIEW(prefs_toolbar->icon_chooser_view));
1516         list = gtk_icon_view_get_selected_items(GTK_ICON_VIEW(prefs_toolbar->icon_chooser_view));
1517         if(list == NULL)
1518                 return;
1519         
1520         if(!gtk_tree_model_get_iter(model, &iter, (GtkTreePath *)list->data)) {
1521                 gtk_tree_path_free(list->data);
1522                 g_list_free(list);
1523                 return;
1524         }
1525         
1526         gtk_tree_model_get(model, &iter,
1527                            SET_ICON, &pix,
1528                            SET_FILENAME, &icon_file,
1529                            -1);
1530
1531         g_free(prefs_toolbar->item_icon_file);
1532         prefs_toolbar->item_icon_file = icon_file;
1533         gtk_button_set_image(GTK_BUTTON(prefs_toolbar->icon_button),
1534                              gtk_image_new_from_pixbuf(pix));
1535         
1536         gtk_widget_destroy(prefs_toolbar->icon_chooser_win);
1537         prefs_toolbar->icon_chooser_win = NULL;
1538         prefs_toolbar->icon_chooser_view = NULL;
1539         
1540         gtk_tree_path_free(list->data);
1541         g_list_free(list);
1542 }
1543
1544 static void icon_chooser_cancel_clicked(GtkButton *button,
1545                                         ToolbarPage *prefs_toolbar)
1546 {
1547         cm_return_if_fail(prefs_toolbar != NULL);
1548
1549         gtk_widget_destroy(prefs_toolbar->icon_chooser_win);
1550         prefs_toolbar->icon_chooser_win = NULL;
1551         prefs_toolbar->icon_chooser_view = NULL;
1552 }
1553
1554 static gboolean icon_chooser_key_pressed(GtkWidget *widget, GdkEventKey *event,
1555                         ToolbarPage *prefs_toolbar)
1556 {
1557         if (event && event->keyval == GDK_KEY_Escape) {
1558                 icon_chooser_cancel_clicked(NULL, prefs_toolbar);
1559                 return TRUE;
1560         }
1561
1562         return FALSE;
1563 }
1564
1565 static gboolean icon_list_key_pressed(GtkWidget *widget, GdkEventKey *event,
1566                         ToolbarPage *prefs_toolbar)
1567 {
1568         if (event) {
1569                 if (event->keyval == GDK_KEY_KP_Enter ||
1570                     event->keyval == GDK_KEY_Return ||
1571                     event->keyval == GDK_KEY_space) {
1572                         icon_chooser_ok_clicked(NULL, prefs_toolbar);
1573                         return TRUE;
1574                 }
1575         }
1576         return FALSE;
1577 }
1578
1579 static gboolean ok_cb(gpointer data)
1580 {
1581         ToolbarPage *prefs_toolbar = (ToolbarPage *)data;
1582         icon_chooser_ok_clicked(NULL, prefs_toolbar);
1583         return FALSE;
1584 }
1585
1586 static gboolean icon_list_button_release(GtkWidget *widget,
1587                                                GdkEventButton *event,
1588                                                ToolbarPage *prefs_toolbar )
1589 {
1590         static guint id = -1;
1591         if (id >= 0) {
1592                 g_source_remove(id);
1593                 id = -1;
1594         }
1595         id = g_timeout_add(100, ok_cb, prefs_toolbar);
1596         return FALSE;
1597 }
1598
1599 static gboolean icon_window_button_press(GtkWidget *widget,
1600                                                GdkEventButton *event,
1601                                                ToolbarPage *prefs_toolbar )
1602 {
1603         GtkWidget *event_widget, *button;
1604         gboolean restore = TRUE;
1605
1606         button = prefs_toolbar->icon_button;
1607
1608         /* Test where mouse was clicked */
1609         event_widget = gtk_get_event_widget((GdkEvent *)event);
1610         if (event_widget != widget) {
1611                 while (event_widget) {
1612                         if (event_widget == widget)
1613                                 return FALSE;
1614                         else if (event_widget == prefs_toolbar->icon_chooser_win) {
1615                                 restore = FALSE;
1616                                 break;
1617                         }
1618                         event_widget = gtk_widget_get_parent(event_widget);
1619                 }
1620         }
1621
1622         if (restore) {
1623                 icon_chooser_cancel_clicked(NULL, prefs_toolbar);
1624                 return TRUE;
1625         }
1626         return FALSE;
1627 }
1628
1629 static void icon_chooser_activated(GtkTreeView *treeview, GtkTreePath *path,
1630                                 ToolbarPage *prefs_toolbar)
1631 {
1632         icon_chooser_ok_clicked(NULL, prefs_toolbar);
1633 }
1634
1635 static void icon_chooser_create(GtkButton *button, ToolbarPage *prefs_toolbar)
1636 {
1637         GtkAllocation allocation;
1638         GtkWidget *icon_chooser_win;
1639         GtkWidget *scrollwin;
1640         GtkWidget *icon_view;
1641         GtkListStore *store;
1642         GtkTreeIter iter;
1643         gint i, x, y;
1644         
1645         store = gtk_list_store_new(2, 
1646                                    GDK_TYPE_PIXBUF,
1647                                    G_TYPE_STRING,
1648                                    -1);
1649                                    
1650         gtk_list_store_clear(store);
1651
1652         for (i = 0; ToolbarIcons[i] != STOCK_PIXMAP_EMPTY; i++) {
1653                 GdkPixbuf *pixbuf;
1654                 stock_pixbuf_gdk(prefs_toolbar->window, ToolbarIcons[i], &pixbuf);
1655                 
1656                 gtk_list_store_append(store, &iter);
1657                 gtk_list_store_set(store, &iter,
1658                                    SET_ICON, pixbuf,
1659                                    SET_FILENAME, stock_pixmap_get_name((StockPixmap) ToolbarIcons[i]),
1660                                    -1);
1661         }
1662         
1663         icon_chooser_win = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_toolbar");
1664         gtk_window_set_title(GTK_WINDOW(icon_chooser_win), _("Toolbar item icon"));
1665 #ifndef MAEMO
1666         gtk_window_set_decorated(GTK_WINDOW(icon_chooser_win), FALSE);
1667 #endif
1668         gdk_window_get_origin(gtk_widget_get_window(
1669                         GTK_WIDGET(prefs_toolbar->icon_button)), 
1670                         &x, &y);
1671         gtk_widget_get_allocation(GTK_WIDGET(prefs_toolbar->icon_button), &allocation);
1672         x += allocation.x;
1673         y += allocation.y;
1674         y += 50;
1675         x -= 300-50;
1676         gtk_window_move(GTK_WINDOW(icon_chooser_win), x, y);
1677         gtk_window_set_resizable(GTK_WINDOW(icon_chooser_win), FALSE);
1678         gtk_widget_set_size_request(icon_chooser_win, 300, 320);
1679         
1680         scrollwin = gtk_scrolled_window_new(NULL, NULL);
1681         gtk_container_add(GTK_CONTAINER(icon_chooser_win), scrollwin);
1682         gtk_widget_show(scrollwin);
1683         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
1684                                 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1685         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwin),
1686                                 GTK_SHADOW_OUT);
1687
1688         icon_view = gtk_icon_view_new_with_model(GTK_TREE_MODEL(store));
1689         gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(icon_view), GTK_SELECTION_SINGLE);
1690         gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(icon_view), SET_ICON);
1691         gtk_container_add(GTK_CONTAINER(scrollwin), GTK_WIDGET(icon_view));
1692
1693         g_signal_connect(G_OBJECT(icon_chooser_win), "key_press_event",
1694                          G_CALLBACK(icon_chooser_key_pressed), prefs_toolbar);
1695         g_signal_connect(G_OBJECT(icon_view), "item-activated",
1696                          G_CALLBACK(icon_chooser_activated), prefs_toolbar);
1697         g_signal_connect(G_OBJECT(icon_chooser_win),
1698                          "button-press-event",
1699                          G_CALLBACK(icon_window_button_press),
1700                          prefs_toolbar );
1701         g_signal_connect(G_OBJECT(icon_view),
1702                          "button-release-event",
1703                          G_CALLBACK(icon_list_button_release),
1704                          prefs_toolbar );
1705         g_signal_connect(G_OBJECT(icon_view), "key_press_event",
1706                          G_CALLBACK(icon_list_key_pressed), prefs_toolbar);
1707
1708         gtk_widget_show_all(icon_chooser_win);
1709         gtk_widget_grab_focus(GTK_WIDGET(icon_view));
1710         gtk_window_set_modal(GTK_WINDOW(icon_chooser_win), TRUE);
1711         
1712         prefs_toolbar->icon_chooser_win         = icon_chooser_win;
1713         prefs_toolbar->icon_chooser_view        = icon_view;
1714 }