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