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