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