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