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