Custom Toolbar: fix crash on CANCEL
[claws.git] / src / prefs_toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 Hiroyuki Yamamoto & the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <gtk/gtk.h>
32 #include <gtk/gtkoptionmenu.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "intl.h"
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
50 typedef enum
51 {
52         COL_ICON        = 0,
53         COL_FILENAME    = 1,
54         COL_TEXT        = 2,
55         COL_EVENT       = 3
56 } DisplayedItemsColumnPos;
57
58 # define N_DISPLAYED_ITEMS_COLS 4
59
60 static struct _Toolbar {
61         GtkWidget *window;
62
63         GtkWidget *clist_icons;
64         GtkWidget *clist_set;
65         GtkWidget *combo_action;
66         GtkWidget *combo_entry;
67         GtkWidget *combo_list;
68         GtkWidget *label_icon_text;
69         GtkWidget *entry_icon_text;
70         GtkWidget *combo_syl_action;
71         GtkWidget *combo_syl_list;
72         GtkWidget *combo_syl_entry;
73
74 }mtoolbar;
75
76 #define CELL_SPACING 24
77 #define ERROR_MSG _("Selected Action already set.\nPlease choose another Action from List")
78
79 static void prefs_toolbar_open                   (void);
80 static void prefs_toolbar_populate               (void);
81 static gboolean is_duplicate                     (gchar *chosen_action);
82 static void prefs_toolbar_save                   (void);
83 static void prefs_toolbar_ok                     (void);
84 static void prefs_toolbar_cancel                 (void);
85
86 static gint prefs_toolbar_register               (void);
87 static gint prefs_toolbar_substitute             (void);
88 static gint prefs_toolbar_delete                 (void);
89
90 static void prefs_toolbar_up                     (void);
91
92 static void prefs_toolbar_down                   (void);
93
94 static void prefs_toolbar_select_row_set         (GtkCList *clist, 
95                                                   gint row, 
96                                                   gint column,
97                                                   GdkEvent *event, 
98                                                   gpointer user_data);
99
100 static void prefs_toolbar_select_row_icons       (GtkCList *clist, 
101                                                   gint row, 
102                                                   gint column,
103                                                   GdkEvent *event, 
104                                                   gpointer user_data);
105
106 static void prefs_toolbar_create                 (void);
107
108 static void prefs_toolbar_selection_changed      (GtkList *list, 
109                                                   gpointer user_data);
110
111 static gint prefs_toolbar_key_pressed            (GtkWidget *widget,
112                                                   GdkEventKey *event,
113                                                   gpointer data);
114
115 void prefs_toolbar(void)
116 {
117         toolbar_read_config_file();
118         prefs_toolbar_open();
119 }
120
121 static void prefs_toolbar_open(void)
122 {
123         if (!mtoolbar.window)
124                 prefs_toolbar_create();
125
126         manage_window_set_transient(GTK_WINDOW(mtoolbar.window));
127         prefs_toolbar_populate();
128         gtk_widget_show(mtoolbar.window);
129 }
130
131 void prefs_toolbar_close(void)
132 {
133         gtk_widget_hide(mtoolbar.window);
134 }
135
136 static void prefs_toolbar_populate(void)
137 {
138         gint i;
139         GSList *cur;
140         GList *combo_action_items;
141         GList *syl_actions = NULL;
142         GtkCList *clist_icons = GTK_CLIST(mtoolbar.clist_icons);
143         GtkCList *clist_set   = GTK_CLIST(mtoolbar.clist_set);
144         GdkPixmap *xpm;
145         GdkBitmap *xpmmask;
146         gchar *avail[2];
147         gchar *activ[5] = {0};
148         gchar *act;
149         
150         gtk_clist_clear(clist_icons);
151         gtk_clist_clear(clist_set);
152
153         gtk_clist_freeze(clist_icons);
154         gtk_clist_freeze(clist_set);
155
156         /* set available icons */
157         avail[0] = g_strdup(SEPARATOR_PIXMAP);
158         avail[1] = g_strdup(SEPARATOR);
159         gtk_clist_append(clist_icons, avail);
160         g_free(avail[0]);
161         g_free(avail[1]);
162
163         combo_action_items = toolbar_get_action_items();
164         gtk_combo_set_popdown_strings(GTK_COMBO(mtoolbar.combo_action), combo_action_items);
165         gtk_combo_set_value_in_list(GTK_COMBO(mtoolbar.combo_action), 0, FALSE);
166         gtk_entry_set_text(GTK_ENTRY(mtoolbar.combo_entry), combo_action_items->data);
167         g_list_free(combo_action_items);
168
169         /* get currently defined sylpheed actions */
170         if (prefs_common.actions_list != NULL) {
171
172                 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
173                         act = (gchar *)cur->data;
174                         syl_actions = g_list_append(syl_actions, act);
175                 } 
176
177                 gtk_combo_set_popdown_strings(GTK_COMBO(mtoolbar.combo_syl_action), syl_actions);
178                 gtk_combo_set_value_in_list(GTK_COMBO(mtoolbar.combo_syl_action), 0, FALSE);
179                 gtk_entry_set_text(GTK_ENTRY(mtoolbar.combo_syl_entry), syl_actions->data);
180                 prefs_toolbar_selection_changed(GTK_LIST(mtoolbar.combo_syl_list), NULL);
181                 g_list_free(syl_actions);
182         }
183
184         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
185                 avail[0] = g_strdup("");
186                 avail[1] = g_strdup(stock_pixmap_get_name((StockPixmap)i));
187
188                 stock_pixmap_gdk(mtoolbar.clist_icons, i,
189                                   &xpm, &xpmmask);
190                 gtk_clist_append(clist_icons, avail);
191                 gtk_clist_set_pixmap(clist_icons, 
192                                        i + 1, 0, xpm, xpmmask);
193                 
194                 g_free(avail[0]);
195                 g_free(avail[1]);
196         }
197
198         /* set currently active toolbar entries */
199         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
200                 ToolbarItem *item = (ToolbarItem*) cur->data;
201         
202                 if (g_strcasecmp(item->file, SEPARATOR) != 0) {
203                         gint row_num;
204                         StockPixmap icon = stock_pixmap_get_icon(item->file);
205                         
206                         stock_pixmap_gdk(mtoolbar.clist_set, icon,
207                                           &xpm, &xpmmask);
208                         activ[0] = g_strdup("");
209                         activ[1] = g_strdup(item->file);
210                         activ[2] = g_strdup(item->text);
211                         activ[3] = g_strdup(toolbar_ret_descr_from_val(item->action));
212                         row_num  = gtk_clist_append(clist_set, activ);
213                         gtk_clist_set_pixmap(clist_set, 
214                                               row_num, 0, xpm, xpmmask);
215
216                 } else {
217                         activ[0] = g_strdup(SEPARATOR_PIXMAP);
218                         activ[1] = g_strdup(item->file);
219                         activ[2] = g_strdup("");
220                         activ[3] = g_strdup("");
221                         gtk_clist_append(clist_set, activ);
222                 }
223
224                 g_free(activ[0]);
225                 g_free(activ[1]);
226                 g_free(activ[2]);
227                 g_free(activ[3]);
228         }
229
230         gtk_clist_thaw(clist_icons);
231         gtk_clist_thaw(clist_set);
232
233         gtk_clist_columns_autosize(clist_icons);
234         gtk_clist_columns_autosize(clist_set);
235
236         gtk_clist_set_row_height(clist_icons, CELL_SPACING);
237         gtk_clist_set_row_height(clist_set, CELL_SPACING);
238
239         gtk_clist_select_row(clist_icons, 0, 0);
240         gtk_clist_select_row(clist_set, 0, 0);
241
242         toolbar_clear_list();
243 }
244
245 static gboolean is_duplicate(gchar *chosen_action)
246 {
247         GtkCList *clist = GTK_CLIST(mtoolbar.clist_set);
248         gchar *entry;
249         gint row = 0;
250         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
251
252         g_return_val_if_fail(chosen_action != NULL, TRUE);
253         if (clist->rows == 0) 
254                 return FALSE;
255         
256         /* allow duplicate entries (A_SYL_ACTIONS) */
257         if (g_strcasecmp(syl_act, chosen_action) == 0)
258                 return FALSE;
259
260         do {
261                 gtk_clist_get_text(clist, row, 3, &entry);
262                 if ( g_strcasecmp(chosen_action, entry) == 0)
263                         return TRUE;
264                 row++;
265         } while ((gtk_clist_get_text(clist, row, 3, &entry)) && (row <= clist->rows));
266         
267         return FALSE;
268 }
269
270 static void prefs_toolbar_save(void)
271 {
272         gint row = 0;
273         GtkCList *clist = GTK_CLIST(mtoolbar.clist_set);
274         gchar *entry = NULL;
275         
276         toolbar_clear_list();
277
278         if (clist->rows == 0) {
279                 toolbar_set_default_toolbar();
280         }
281         else {
282                 do {
283                         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
284                         
285                         gtk_clist_get_text(clist, row, 1, &entry);
286                         toolbar_item->file = g_strdup(entry);
287                         
288                         gtk_clist_get_text(clist, row, 2, &entry);
289                         toolbar_item->text = g_strdup(entry);
290                         
291                         gtk_clist_get_text(clist, row, 3, &entry);      
292                         toolbar_item->action = toolbar_ret_val_from_descr(entry);
293                         
294                         /* TODO: save A_SYL_ACTIONS only if they are still active */
295
296                         toolbar_list = g_slist_append(toolbar_list, toolbar_item);
297                         row++;
298                         
299                 } while(gtk_clist_get_text(clist, row, 3, &entry));
300         }
301         toolbar_save_config_file();
302         toolbar_update();
303         main_window_reflect_prefs_all_real(TRUE);
304 }
305
306 static void prefs_toolbar_ok(void)
307 {
308         prefs_toolbar_save();
309         prefs_toolbar_close();
310 }
311
312 static void prefs_toolbar_cancel(void)
313 {
314         prefs_toolbar_close();
315         toolbar_update();
316 }
317
318 static void get_action_name(gchar *entry, gchar **menu)
319 {
320         gchar *act, *act_p;
321
322         if (prefs_common.actions_list != NULL) {
323                 
324                 act = g_strdup(entry);
325                 act_p = strstr(act, ": ");
326                 if (act_p != NULL)
327                         act_p[0] = 0x00;
328                 /* freed by calling func */
329                 *menu = act;
330         }
331 }
332
333 static gint prefs_toolbar_register(void)
334 {
335         GtkCList *clist_set   = GTK_CLIST(mtoolbar.clist_set);
336         GtkCList *clist_icons = GTK_CLIST(mtoolbar.clist_icons);
337         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
338         gint row_icons = 0;
339         gint row_set = 0;
340         GdkPixmap *xpm;
341         GdkBitmap *xpmmask;
342         gchar *item[4];
343
344         if (clist_icons->rows == 0) return -1; 
345
346         if (clist_icons->selection)
347                 if (clist_icons->selection->data) 
348                         row_icons = GPOINTER_TO_INT(clist_icons->selection->data);
349         
350         gtk_clist_get_text(clist_icons, row_icons, 1, &item[1]);
351         item[3] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mtoolbar.combo_entry)));
352         
353         /* SEPARATOR or other ? */
354         if (g_strcasecmp(item[1], SEPARATOR) == 0) {
355                 item[0] = g_strdup(SEPARATOR_PIXMAP);
356                 item[2] = g_strdup("");
357                 item[3] = g_strdup("");
358                 
359                 row_set = gtk_clist_append(GTK_CLIST(mtoolbar.clist_set), item);
360
361                 g_free(item[0]);
362         } else {
363
364                 if (is_duplicate(item[3])) {
365                         alertpanel_error(ERROR_MSG);
366                         g_free(item[3]);
367                         return -1;
368                 }
369
370                 stock_pixmap_gdk(mtoolbar.clist_set, stock_pixmap_get_icon(item[1]),
371                                   &xpm, &xpmmask);
372
373                 if (g_strcasecmp(item[3], syl_act) == 0) {
374
375                         gchar *entry = gtk_entry_get_text(GTK_ENTRY(mtoolbar.combo_syl_entry));
376                         get_action_name(entry, &item[2]);
377                 }
378                 else {
379                         item[2] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mtoolbar.entry_icon_text)));
380                 }
381
382                 row_set = gtk_clist_append(GTK_CLIST(mtoolbar.clist_set), item);
383                 gtk_clist_set_pixmap(clist_set, row_set, 0, xpm, xpmmask);
384         }
385
386         gtk_clist_moveto(clist_set, row_set, 0, row_set/clist_set->rows, 0);
387         gtk_clist_select_row(clist_set, row_set, 0);
388
389         g_free(item[2]);
390         g_free(item[3]);
391
392         return 0;
393 }
394
395 static gint prefs_toolbar_substitute(void)
396 {
397         GtkCList *clist_set   = GTK_CLIST(mtoolbar.clist_set);
398         GtkCList *clist_icons = GTK_CLIST(mtoolbar.clist_icons);
399         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
400         gint row_icons = 0;
401         gint row_set = 0;
402         GdkPixmap *xpm;
403         GdkBitmap *xpmmask;
404         gchar *item[4];
405         gchar *ac_set;
406
407         /* no rows or nothing selected */
408         if ((clist_set->rows == 0) || (clist_set->selection == 0)) return -1; 
409
410         if (clist_icons->selection)
411                 if (clist_icons->selection->data) 
412                         row_icons = GPOINTER_TO_INT(clist_icons->selection->data);
413
414         if (clist_set->selection)
415                 if (clist_set->selection->data) 
416                         row_set = GPOINTER_TO_INT(clist_set->selection->data);
417
418         gtk_clist_get_text(clist_icons, row_icons, 1, &item[1]);
419         gtk_clist_get_text(clist_set, row_set, 3, &ac_set);
420         item[3] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mtoolbar.combo_entry)));
421
422         if (g_strcasecmp(item[1], SEPARATOR) == 0) {
423                 item[0] = g_strdup(SEPARATOR_PIXMAP);
424                 item[2] = g_strdup("");
425                 item[3] = g_strdup("");
426
427                 gtk_clist_remove(clist_set, row_set);
428                 row_set = gtk_clist_insert(clist_set, row_set, item);
429
430                 g_free(item[0]);
431         } else {
432
433                 if ((is_duplicate(item[3])) && (g_strcasecmp(item[3], ac_set) != 0)){
434                         alertpanel_error(ERROR_MSG);
435                         g_free(item[3]);
436                         return -1;
437                 }
438
439                 stock_pixmap_gdk(mtoolbar.clist_set, stock_pixmap_get_icon(item[1]),
440                                   &xpm, &xpmmask);
441
442                 if (g_strcasecmp(item[3], syl_act) == 0) {
443
444                         gchar *entry = gtk_entry_get_text(GTK_ENTRY(mtoolbar.combo_syl_entry));
445                         get_action_name(entry, &item[2]);
446                 } else {
447                         item[2] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mtoolbar.entry_icon_text)));
448                 }
449
450                 gtk_clist_remove(clist_set, row_set);
451                 row_set = gtk_clist_insert(clist_set, row_set, item);
452                 gtk_clist_set_pixmap(clist_set, row_set, 0, xpm, xpmmask);
453         }
454         
455         gtk_clist_moveto(clist_set, row_set, 0, row_set/clist_set->rows, 0);
456         gtk_clist_select_row(clist_set, row_set, 0);
457
458         g_free(item[2]);
459         g_free(item[3]);
460         
461         return 0;
462 }
463
464 static gint prefs_toolbar_delete(void)
465 {
466         GtkCList *clist_set = GTK_CLIST(mtoolbar.clist_set);
467         gint row_set = 0;
468
469         if (clist_set->rows == 0) return -1; 
470         if (clist_set->selection)
471                 if (clist_set->selection->data) 
472                         row_set = GPOINTER_TO_INT(clist_set->selection->data);
473
474         if (clist_set->row_list != NULL) {
475                         
476                 row_set = GPOINTER_TO_INT(clist_set->selection->data);
477                 gtk_clist_remove(clist_set, row_set);
478                 gtk_clist_columns_autosize(clist_set);
479                 
480                 if (clist_set->rows > 0)
481                         gtk_clist_select_row(clist_set,(row_set == 0) ? 0:row_set - 1, 0);
482         }
483
484         return 0;
485 }
486
487 static void prefs_toolbar_up(void)
488 {
489         GtkCList *clist = GTK_CLIST(mtoolbar.clist_set);
490         gint row = 0;
491
492         if (!clist->selection) return;
493         if (clist->selection->data)
494                 row = GPOINTER_TO_INT(clist->selection->data);
495
496         if (row >= 0) {
497                 gtk_clist_row_move(clist, row, row - 1);
498                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
499                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
500                 } 
501         }
502 }
503
504 static void prefs_toolbar_down(void)
505 {
506         GtkCList *clist = GTK_CLIST(mtoolbar.clist_set);
507         gint row = 0;
508
509         if (!clist->selection) return;
510         if (clist->selection->data)
511                 row = GPOINTER_TO_INT(clist->selection->data);
512
513         if (row >= 0 && row < clist->rows - 1) {
514                 gtk_clist_row_move(clist, row, row + 1);
515                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
516                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
517                 } 
518         }
519 }
520
521 static void prefs_toolbar_select_row_set(GtkCList *clist, gint row, gint column,
522                                          GdkEvent *event, gpointer user_data)
523 {
524         GtkCList *clist_ico = GTK_CLIST(mtoolbar.clist_icons);
525         gchar *syl_act = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
526         gint row_set = 0;
527         gint row_ico = 0;
528         gchar *file, *icon_text, *descr, *entry;
529
530         if (clist->selection->data) 
531                 row_set = GPOINTER_TO_INT(clist->selection->data);      
532
533         gtk_clist_get_text(clist, row_set, 1, &file);
534         gtk_clist_get_text(clist, row_set, 2, &icon_text);
535         gtk_clist_get_text(clist, row_set, 3, &descr);
536
537         if (g_strcasecmp(descr, syl_act) != 0)
538                 gtk_entry_set_text(GTK_ENTRY(mtoolbar.entry_icon_text), icon_text);
539
540         gtk_list_select_item(GTK_LIST(mtoolbar.combo_list), 
541                               toolbar_ret_val_from_descr(descr));
542         do {
543                 gtk_clist_get_text(clist_ico, row_ico, 1, &entry);
544                 row_ico++;
545         } while(g_strcasecmp(entry, file) != 0);
546         
547         gtk_clist_select_row(clist_ico, row_ico - 1, 0);
548         gtk_clist_moveto(clist_ico, row_ico - 1, 0, row_ico/clist_ico->rows, 0);
549 }
550
551 static void prefs_toolbar_select_row_icons(GtkCList *clist, gint row, gint column,
552                                            GdkEvent *event, gpointer user_data)
553 {
554         GtkCList *clist_icons = GTK_CLIST(mtoolbar.clist_icons);
555         gchar *text;
556         
557         gtk_clist_get_text(clist_icons, row, 1, &text);
558
559         if (!text) 
560                 return;
561
562         if (g_strcasecmp(SEPARATOR, text) == 0) {
563                 gtk_widget_set_sensitive(mtoolbar.combo_action,     FALSE);
564                 gtk_widget_set_sensitive(mtoolbar.entry_icon_text,  FALSE);
565                 gtk_widget_set_sensitive(mtoolbar.combo_syl_action, FALSE);
566         } else {
567                 gtk_widget_set_sensitive(mtoolbar.combo_action,     TRUE);
568                 gtk_widget_set_sensitive(mtoolbar.entry_icon_text,  TRUE);
569                 gtk_widget_set_sensitive(mtoolbar.combo_syl_action, TRUE);
570         }
571 }
572
573 static void prefs_toolbar_selection_changed(GtkList *list,
574                                             gpointer user_data)
575 {
576
577         gchar *cur_entry = g_strdup(gtk_entry_get_text(GTK_ENTRY(mtoolbar.combo_entry)));
578         gchar *actions_entry = toolbar_ret_descr_from_val(A_SYL_ACTIONS);
579
580         gtk_widget_set_sensitive(mtoolbar.combo_syl_action, TRUE);
581
582         if (g_strcasecmp(cur_entry, actions_entry) == 0) {
583                 gtk_widget_hide(mtoolbar.entry_icon_text);
584                 gtk_widget_show(mtoolbar.combo_syl_action);
585                 gtk_label_set_text(GTK_LABEL(mtoolbar.label_icon_text), _("Sylpheed Action"));
586
587                 if (prefs_common.actions_list == NULL) {
588                     gtk_widget_set_sensitive(mtoolbar.combo_syl_action, FALSE);
589                 }
590                     
591         } else {
592                 gtk_widget_hide(mtoolbar.combo_syl_action);
593                 gtk_widget_show(mtoolbar.entry_icon_text);
594                 gtk_label_set_text(GTK_LABEL(mtoolbar.label_icon_text), _("Toolbar text"));
595         }
596 }
597
598 static gint prefs_toolbar_key_pressed(GtkWidget *widget,
599                            GdkEventKey *event,
600                            gpointer data)
601 {
602         if (event && event->keyval == GDK_Escape) {
603                 prefs_toolbar_cancel();
604                 return TRUE;
605         }
606         return FALSE;
607 }
608
609 static void prefs_toolbar_create(void)
610 {
611         GtkWidget *window;
612         GtkWidget *main_vbox;
613         GtkWidget *top_hbox;
614         GtkWidget *compose_frame;
615         GtkWidget *reg_hbox;
616         GtkWidget *arrow;
617         GtkWidget *btn_hbox;
618         GtkWidget *reg_btn;
619         GtkWidget *subst_btn;
620         GtkWidget *del_btn;
621         GtkWidget *vbox_frame;
622         GtkWidget *scrolledwindow_clist_icon;
623         GtkWidget *clist_icons;
624         GtkWidget *hbox_icon_text;
625         GtkWidget *label_icon_text;
626         GtkWidget *entry_icon_text;
627         GtkWidget *hbox_action;
628         GtkWidget *label_action_sel;
629         GtkWidget *combo_action;
630         GtkWidget *combo_entry;
631         GtkWidget *combo_list;
632         GtkWidget *combo_syl_action;
633         GtkWidget *combo_syl_entry;
634         GtkWidget *combo_syl_list;
635         GtkWidget *frame_toolbar_items;
636         GtkWidget *hbox_bottom;
637         GtkWidget *scrolledwindow_clist_set;
638         GtkWidget *clist_set;
639
640         GtkWidget *btn_vbox;
641         GtkWidget *up_btn;
642         GtkWidget *down_btn;
643
644         GtkWidget *confirm_area;
645         GtkWidget *ok_btn;
646         GtkWidget *cancel_btn;
647
648         gchar *titles[N_DISPLAYED_ITEMS_COLS];
649
650         debug_print("Creating custom toolbar window...\n");
651
652         window = gtk_window_new(GTK_WINDOW_DIALOG);
653         gtk_widget_set_usize(window, 450, -1); 
654         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
655         gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
656         gtk_window_set_title(GTK_WINDOW(window), _("Customize toolbar"));
657         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
658         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
659         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
660                            GTK_SIGNAL_FUNC(prefs_toolbar_cancel),
661                            NULL);
662         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
663                            GTK_SIGNAL_FUNC(prefs_toolbar_key_pressed),
664                            NULL);
665         MANAGE_WINDOW_SIGNALS_CONNECT(window);  
666         gtk_widget_realize(window); 
667
668         main_vbox = gtk_vbox_new(FALSE, 0);
669         gtk_widget_show(main_vbox);
670         gtk_container_add(GTK_CONTAINER(window), main_vbox);
671         
672         top_hbox = gtk_hbox_new(FALSE, 0);
673         gtk_box_pack_start(GTK_BOX(main_vbox), top_hbox, TRUE, TRUE, 0);
674   
675         compose_frame = gtk_frame_new(_("Available toolbar items"));
676         gtk_box_pack_start(GTK_BOX(top_hbox), compose_frame, TRUE, TRUE, 0);
677         gtk_container_set_border_width(GTK_CONTAINER(compose_frame), 5);
678
679         vbox_frame = gtk_vbox_new(FALSE, 0);
680         gtk_container_add(GTK_CONTAINER(compose_frame), vbox_frame);
681         
682         /* available icons */
683         scrolledwindow_clist_icon = gtk_scrolled_window_new(NULL, NULL);
684         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_clist_icon), 5);
685         gtk_container_add(GTK_CONTAINER(vbox_frame), scrolledwindow_clist_icon);
686         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_clist_icon), 
687                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
688         
689         clist_icons = gtk_clist_new(2);
690         gtk_container_add(GTK_CONTAINER(scrolledwindow_clist_icon), clist_icons);
691         gtk_container_set_border_width(GTK_CONTAINER(clist_icons), 1);
692         gtk_clist_set_column_width(GTK_CLIST(clist_icons), 0, 35);
693         gtk_clist_set_column_width(GTK_CLIST(clist_icons), 1, 200);
694         gtk_clist_column_titles_hide(GTK_CLIST(clist_icons));
695         gtk_widget_set_usize(clist_icons, 225, 108); 
696         
697         /* icon description */
698         hbox_icon_text = gtk_hbox_new(FALSE, 5);
699         gtk_container_add(GTK_CONTAINER(vbox_frame), hbox_icon_text);
700         
701         label_icon_text = gtk_label_new("");
702         gtk_box_pack_start(GTK_BOX(hbox_icon_text), label_icon_text, FALSE, FALSE, 5);
703         
704         entry_icon_text = gtk_entry_new();
705         gtk_box_pack_start(GTK_BOX(hbox_icon_text), entry_icon_text, FALSE, FALSE, 0);
706
707         /* Sylpheed Action Combo Box */ 
708         combo_syl_action = gtk_combo_new();
709         gtk_box_pack_start(GTK_BOX(hbox_icon_text), combo_syl_action, FALSE, FALSE, 0);
710
711         combo_syl_list = GTK_COMBO(combo_syl_action)->list;
712         combo_syl_entry = GTK_COMBO(combo_syl_action)->entry;
713         gtk_entry_set_editable(GTK_ENTRY(combo_syl_entry), FALSE);
714
715         /* available actions */
716         hbox_action = gtk_hbox_new(FALSE, 5);
717         gtk_container_add(GTK_CONTAINER(vbox_frame), hbox_action);
718         gtk_container_set_border_width(GTK_CONTAINER(hbox_action), 5);
719         
720         label_action_sel = gtk_label_new(_("Event executed on click"));
721         gtk_box_pack_start(GTK_BOX(hbox_action), label_action_sel, FALSE, FALSE, 0);
722         
723         combo_action = gtk_combo_new();
724         gtk_box_pack_start(GTK_BOX(hbox_action), combo_action, FALSE, FALSE, 0);
725         
726         combo_list = GTK_COMBO(combo_action)->list;
727         combo_entry = GTK_COMBO(combo_action)->entry;
728         gtk_entry_set_editable(GTK_ENTRY(combo_entry), FALSE);
729         
730         /* register / substitute / delete */
731         reg_hbox = gtk_hbox_new(FALSE, 4);
732         gtk_box_pack_start(GTK_BOX(main_vbox), reg_hbox, FALSE, FALSE, 0);
733         gtk_container_set_border_width(GTK_CONTAINER(reg_hbox), 10);
734
735         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
736         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
737         gtk_widget_set_usize(arrow, -1, 16);
738
739         btn_hbox = gtk_hbox_new(TRUE, 4);
740         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
741
742         reg_btn = gtk_button_new_with_label(_("Register"));
743         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
744         gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
745                             GTK_SIGNAL_FUNC(prefs_toolbar_register), 
746                             NULL);
747
748         subst_btn = gtk_button_new_with_label(_(" Substitute "));
749         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
750         gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
751                             GTK_SIGNAL_FUNC(prefs_toolbar_substitute),
752                             NULL);
753
754         del_btn = gtk_button_new_with_label(_("Delete"));
755         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
756         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
757                             GTK_SIGNAL_FUNC(prefs_toolbar_delete), 
758                             NULL);
759
760         /* currently active toolbar items */
761         frame_toolbar_items = gtk_frame_new(_("Displayed toolbar items"));
762         gtk_box_pack_start(GTK_BOX(main_vbox), frame_toolbar_items, TRUE, TRUE, 0);
763         gtk_container_set_border_width(GTK_CONTAINER(frame_toolbar_items), 5);
764         
765         hbox_bottom = gtk_hbox_new(FALSE, 0);
766         gtk_container_add(GTK_CONTAINER(frame_toolbar_items), hbox_bottom);
767         
768         scrolledwindow_clist_set = gtk_scrolled_window_new(NULL, NULL);
769         gtk_box_pack_start(GTK_BOX(hbox_bottom), scrolledwindow_clist_set, TRUE, TRUE, 0);
770         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow_clist_set), 1);
771         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_clist_icon), 
772                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
773
774         titles[COL_ICON]     = _("Icon");
775         titles[COL_FILENAME] = _("File name");
776         titles[COL_TEXT]     = _("Icon text");
777         titles[COL_EVENT]    = _("Mapped event");
778         
779         clist_set = gtk_clist_new_with_titles(N_DISPLAYED_ITEMS_COLS, titles);
780         gtk_widget_show(clist_set);
781         gtk_container_add(GTK_CONTAINER(scrolledwindow_clist_set), clist_set);
782         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_ICON    , 80);
783         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_FILENAME, 80);
784         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_TEXT    , 80);
785         gtk_clist_set_column_width(GTK_CLIST(clist_set), COL_EVENT   , 80);
786         gtk_clist_column_titles_show(GTK_CLIST(clist_set));
787         gtk_widget_set_usize(clist_set, 225, 120);
788
789         btn_vbox = gtk_vbox_new(FALSE, 8);
790         gtk_widget_show(btn_vbox);
791         gtk_box_pack_start(GTK_BOX(hbox_bottom), btn_vbox, FALSE, FALSE, 5);
792
793         up_btn = gtk_button_new_with_label(_("Up"));
794         gtk_widget_show(up_btn);
795         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 2);
796
797         down_btn = gtk_button_new_with_label(_("Down"));
798         gtk_widget_show(down_btn);
799         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
800
801         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
802                                 &cancel_btn, _("Cancel"), NULL, NULL);
803         gtk_box_pack_end(GTK_BOX(main_vbox), confirm_area, FALSE, FALSE, 0);
804         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
805         gtk_widget_grab_default(ok_btn);
806
807         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
808                            GTK_SIGNAL_FUNC(prefs_toolbar_ok), NULL);
809         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
810                            GTK_SIGNAL_FUNC(prefs_toolbar_cancel), NULL);
811         gtk_signal_connect(GTK_OBJECT(clist_set), "select_row",
812                            GTK_SIGNAL_FUNC(prefs_toolbar_select_row_set),
813                            NULL);
814         gtk_signal_connect(GTK_OBJECT(clist_icons), "select_row",
815                            GTK_SIGNAL_FUNC(prefs_toolbar_select_row_icons),
816                            NULL);
817         gtk_signal_connect(GTK_OBJECT(combo_list), "selection-changed",
818                            GTK_SIGNAL_FUNC(prefs_toolbar_selection_changed),
819                            NULL);
820         gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
821                            GTK_SIGNAL_FUNC(prefs_toolbar_up), NULL);
822         gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
823                            GTK_SIGNAL_FUNC(prefs_toolbar_down), NULL);
824         
825         mtoolbar.window           = window;
826         mtoolbar.clist_icons      = clist_icons;
827         mtoolbar.clist_set        = clist_set;
828         mtoolbar.combo_action     = combo_action;
829         mtoolbar.combo_entry      = combo_entry;
830         mtoolbar.combo_list       = combo_list;
831         mtoolbar.entry_icon_text  = entry_icon_text;
832         mtoolbar.combo_syl_action = combo_syl_action;
833         mtoolbar.combo_syl_list   = combo_syl_list;
834         mtoolbar.combo_syl_entry  = combo_syl_entry;
835
836         mtoolbar.label_icon_text  = label_icon_text;
837
838         gtk_widget_show_all(window);
839 }