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