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