0.8.7claws8
[claws.git] / src / prefs_summary_column.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtkvbox.h>
30 #include <gtk/gtkhbox.h>
31 #include <gtk/gtkclist.h>
32 #include <gtk/gtkbutton.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include "intl.h"
36 #include "prefs_gtk.h"
37 #include "prefs_common.h"
38 #include "prefs_summary_column.h"
39 #include "manage_window.h"
40 #include "summaryview.h"
41 #include "mainwindow.h"
42 #include "inc.h"
43 #include "gtkutils.h"
44 #include "utils.h"
45
46 static struct _SummaryColumnDialog
47 {
48         GtkWidget *window;
49
50         GtkWidget *stock_clist;
51         GtkWidget *shown_clist;
52
53         GtkWidget *add_btn;
54         GtkWidget *remove_btn;
55         GtkWidget *up_btn;
56         GtkWidget *down_btn;
57
58         GtkWidget *default_btn;
59
60         GtkWidget *ok_btn;
61         GtkWidget *cancel_btn;
62
63         gboolean finished;
64 } summary_col;
65
66 static const gchar *const col_name[N_SUMMARY_COLS] = {
67         N_("Mark"),             /* S_COL_MARK    */
68         N_("Unread"),           /* S_COL_UNREAD  */
69         N_("Attachment"),       /* S_COL_MIME    */
70         N_("Subject"),          /* S_COL_SUBJECT */
71         N_("From"),             /* S_COL_FROM    */
72         N_("Date"),             /* S_COL_DATE    */
73         N_("Size"),             /* S_COL_SIZE    */
74         N_("Number"),           /* S_COL_NUMBER  */
75         N_("Score"),            /* S_COL_SCORE   */
76         N_("Locked"),           /* S_COL_LOCKED  */
77 };
78
79 static SummaryColumnState default_state[N_SUMMARY_COLS] = {
80         { S_COL_MARK   , TRUE  },
81         { S_COL_UNREAD , TRUE  },
82         { S_COL_MIME   , TRUE  },
83         { S_COL_SUBJECT, TRUE  },
84         { S_COL_FROM   , TRUE  },
85         { S_COL_DATE   , TRUE  },
86         { S_COL_SIZE   , TRUE  },
87         { S_COL_NUMBER , FALSE },
88         { S_COL_SCORE  , FALSE },
89         { S_COL_LOCKED , FALSE  },
90 };
91
92 static void prefs_summary_column_create (void);
93
94 static void prefs_summary_column_set_dialog     (SummaryColumnState *state);
95 static void prefs_summary_column_set_view       (void);
96
97 /* callback functions */
98 static void prefs_summary_column_add    (void);
99 static void prefs_summary_column_remove (void);
100
101 static void prefs_summary_column_up     (void);
102 static void prefs_summary_column_down   (void);
103
104 static void prefs_summary_column_set_to_default (void);
105
106 static void prefs_summary_column_ok     (void);
107 static void prefs_summary_column_cancel (void);
108
109 static gint prefs_summary_column_delete_event   (GtkWidget      *widget,
110                                                  GdkEventAny    *event,
111                                                  gpointer        data);
112 static void prefs_summary_column_key_pressed    (GtkWidget      *widget,
113                                                  GdkEventKey    *event,
114                                                  gpointer        data);
115
116 void prefs_summary_column_open(void)
117 {
118         inc_lock();
119
120         if (!summary_col.window)
121                 prefs_summary_column_create();
122
123         manage_window_set_transient(GTK_WINDOW(summary_col.window));
124         gtk_widget_grab_focus(summary_col.ok_btn);
125
126         prefs_summary_column_set_dialog(NULL);
127
128         gtk_widget_show(summary_col.window);
129
130         summary_col.finished = FALSE;
131         while (summary_col.finished == FALSE)
132                 gtk_main_iteration();
133
134         gtk_widget_hide(summary_col.window);
135
136         inc_unlock();
137 }
138
139 static void prefs_summary_column_create(void)
140 {
141         GtkWidget *window;
142         GtkWidget *vbox;
143
144         GtkWidget *label_hbox;
145         GtkWidget *label;
146
147         GtkWidget *vbox1;
148
149         GtkWidget *hbox1;
150         GtkWidget *clist_hbox;
151         GtkWidget *scrolledwin;
152         GtkWidget *stock_clist;
153         GtkWidget *shown_clist;
154
155         GtkWidget *btn_vbox;
156         GtkWidget *btn_vbox1;
157         GtkWidget *add_btn;
158         GtkWidget *remove_btn;
159         GtkWidget *up_btn;
160         GtkWidget *down_btn;
161
162         GtkWidget *btn_hbox;
163         GtkWidget *default_btn;
164         GtkWidget *confirm_area;
165         GtkWidget *ok_btn;
166         GtkWidget *cancel_btn;
167
168         gchar *title[1];
169
170         debug_print("Creating summary column setting window...\n");
171
172         window = gtk_window_new(GTK_WINDOW_DIALOG);
173         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
174         gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
175         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
176         gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
177         gtk_window_set_title(GTK_WINDOW(window),
178                              _("Displayed items configuration"));
179         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
180                            GTK_SIGNAL_FUNC(prefs_summary_column_delete_event),
181                            NULL);
182         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
183                            GTK_SIGNAL_FUNC(prefs_summary_column_key_pressed),
184                            NULL);
185
186         vbox = gtk_vbox_new(FALSE, 6);
187         gtk_widget_show(vbox);
188         gtk_container_add(GTK_CONTAINER(window), vbox);
189
190         label_hbox = gtk_hbox_new(FALSE, 0);
191         gtk_widget_show(label_hbox);
192         gtk_box_pack_start(GTK_BOX(vbox), label_hbox, FALSE, FALSE, 4);
193
194         label = gtk_label_new
195                 (_("Select items to be displayed in the summary view. You can modify\n"
196                    "the order by using the Up / Down buttons or by dragging the items."));
197         gtk_widget_show(label);
198         gtk_box_pack_start(GTK_BOX(label_hbox), label, FALSE, FALSE, 4);
199         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
200
201         vbox1 = gtk_vbox_new(FALSE, VSPACING);
202         gtk_widget_show(vbox1);
203         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
204         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
205
206         hbox1 = gtk_hbox_new(FALSE, 8);
207         gtk_widget_show(hbox1);
208         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
209
210         clist_hbox = gtk_hbox_new(FALSE, 8);
211         gtk_widget_show(clist_hbox);
212         gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
213
214         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
215         gtk_widget_set_usize(scrolledwin, 180, 210);
216         gtk_widget_show(scrolledwin);
217         gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
218         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
219                                        GTK_POLICY_AUTOMATIC,
220                                        GTK_POLICY_AUTOMATIC);
221
222         title[0] = _("Available items");
223         stock_clist = gtk_clist_new_with_titles(1, title);
224         gtk_widget_show(stock_clist);
225         gtk_container_add(GTK_CONTAINER(scrolledwin), stock_clist);
226         gtk_clist_set_selection_mode(GTK_CLIST(stock_clist),
227                                      GTK_SELECTION_BROWSE);
228         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(stock_clist)->column[0].button,
229                                GTK_CAN_FOCUS);
230
231         /* add/remove button */
232         btn_vbox = gtk_vbox_new(FALSE, 0);
233         gtk_widget_show(btn_vbox);
234         gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
235
236         btn_vbox1 = gtk_vbox_new(FALSE, 8);
237         gtk_widget_show(btn_vbox1);
238         gtk_box_pack_start(GTK_BOX(btn_vbox), btn_vbox1, TRUE, FALSE, 0);
239
240         add_btn = gtk_button_new_with_label(_("  ->  "));
241         gtk_widget_show(add_btn);
242         gtk_box_pack_start(GTK_BOX(btn_vbox1), add_btn, FALSE, FALSE, 0);
243
244         remove_btn = gtk_button_new_with_label(_("  <-  "));
245         gtk_widget_show(remove_btn);
246         gtk_box_pack_start(GTK_BOX(btn_vbox1), remove_btn, FALSE, FALSE, 0);
247
248         gtk_signal_connect(GTK_OBJECT(add_btn), "clicked",
249                            GTK_SIGNAL_FUNC(prefs_summary_column_add), NULL);
250         gtk_signal_connect(GTK_OBJECT(remove_btn), "clicked",
251                            GTK_SIGNAL_FUNC(prefs_summary_column_remove), NULL);
252
253         clist_hbox = gtk_hbox_new(FALSE, 8);
254         gtk_widget_show(clist_hbox);
255         gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
256
257         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
258         gtk_widget_set_usize(scrolledwin, 180, 210);
259         gtk_widget_show(scrolledwin);
260         gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
261         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
262                                        GTK_POLICY_AUTOMATIC,
263                                        GTK_POLICY_AUTOMATIC);
264
265         title[0] = _("Displayed items");
266         shown_clist = gtk_clist_new_with_titles(1, title);
267         gtk_widget_show(shown_clist);
268         gtk_container_add(GTK_CONTAINER(scrolledwin), shown_clist);
269         gtk_clist_set_selection_mode(GTK_CLIST(shown_clist),
270                                      GTK_SELECTION_BROWSE);
271         gtk_clist_set_reorderable(GTK_CLIST(shown_clist), TRUE);
272         gtk_clist_set_use_drag_icons(GTK_CLIST(shown_clist), FALSE);
273         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(shown_clist)->column[0].button,
274                                GTK_CAN_FOCUS);
275
276         /* up/down button */
277         btn_vbox = gtk_vbox_new(FALSE, 0);
278         gtk_widget_show(btn_vbox);
279         gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
280
281         btn_vbox1 = gtk_vbox_new(FALSE, 8);
282         gtk_widget_show(btn_vbox1);
283         gtk_box_pack_start(GTK_BOX(btn_vbox), btn_vbox1, TRUE, FALSE, 0);
284
285         up_btn = gtk_button_new_with_label(_("Up"));
286         gtk_widget_show(up_btn);
287         gtk_box_pack_start(GTK_BOX(btn_vbox1), up_btn, FALSE, FALSE, 0);
288
289         down_btn = gtk_button_new_with_label(_("Down"));
290         gtk_widget_show(down_btn);
291         gtk_box_pack_start(GTK_BOX(btn_vbox1), down_btn, FALSE, FALSE, 0);
292
293         gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
294                            GTK_SIGNAL_FUNC(prefs_summary_column_up), NULL);
295         gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
296                            GTK_SIGNAL_FUNC(prefs_summary_column_down), NULL);
297
298         btn_hbox = gtk_hbox_new(FALSE, 8);
299         gtk_widget_show(btn_hbox);
300         gtk_box_pack_end(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 0);
301
302         btn_vbox = gtk_vbox_new(FALSE, 0);
303         gtk_widget_show(btn_vbox);
304         gtk_box_pack_start(GTK_BOX(btn_hbox), btn_vbox, FALSE, FALSE, 0);
305
306         default_btn = gtk_button_new_with_label(_(" Use default "));
307         gtk_widget_show(default_btn);
308         gtk_box_pack_start(GTK_BOX(btn_vbox), default_btn, TRUE, FALSE, 0);
309         gtk_signal_connect(GTK_OBJECT(default_btn), "clicked",
310                            GTK_SIGNAL_FUNC(prefs_summary_column_set_to_default),
311                            NULL);
312
313         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
314                                 &cancel_btn, _("Cancel"), NULL, NULL);
315         gtk_widget_show(confirm_area);
316         gtk_box_pack_end(GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
317         gtk_widget_grab_default(ok_btn);
318
319         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
320                            GTK_SIGNAL_FUNC(prefs_summary_column_ok), NULL);
321         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
322                            GTK_SIGNAL_FUNC(prefs_summary_column_cancel), NULL);
323
324         summary_col.window      = window;
325         summary_col.stock_clist = stock_clist;
326         summary_col.shown_clist = shown_clist;
327         summary_col.add_btn     = add_btn;
328         summary_col.remove_btn  = remove_btn;
329         summary_col.up_btn      = up_btn;
330         summary_col.down_btn    = down_btn;
331         summary_col.ok_btn      = ok_btn;
332         summary_col.cancel_btn  = cancel_btn;
333 }
334
335 SummaryColumnState *prefs_summary_column_get_config(void)
336 {
337         static SummaryColumnState state[N_SUMMARY_COLS];
338         SummaryColumnType type;
339         gint pos;
340
341         for (pos = 0; pos < N_SUMMARY_COLS; pos++)
342                 state[pos].type = -1;
343
344         for (type = 0; type < N_SUMMARY_COLS; type++) {
345                 pos = prefs_common.summary_col_pos[type];
346                 if (pos < 0 || pos >= N_SUMMARY_COLS ||
347                     state[pos].type != -1) {
348                         g_warning("Wrong column position\n");
349                         prefs_summary_column_set_config(default_state);
350                         return default_state;
351                 }
352
353                 state[pos].type = type;
354                 state[pos].visible = prefs_common.summary_col_visible[type];
355         }
356
357         return state;
358 }
359
360 void prefs_summary_column_set_config(SummaryColumnState *state)
361 {
362         SummaryColumnType type;
363         gint pos;
364
365         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
366                 type = state[pos].type;
367                 prefs_common.summary_col_visible[type] = state[pos].visible;
368                 prefs_common.summary_col_pos[type] = pos;
369         }
370 }
371
372 static void prefs_summary_column_set_dialog(SummaryColumnState *state)
373 {
374         GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
375         GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
376         gint pos;
377         SummaryColumnType type;
378         gchar *name;
379
380         gtk_clist_clear(stock_clist);
381         gtk_clist_clear(shown_clist);
382
383         if (!state)
384                 state = prefs_summary_column_get_config();
385
386         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
387                 gint row;
388                 type = state[pos].type;
389                 name = gettext(col_name[type]);
390
391                 if (state[pos].visible) {
392                         row = gtk_clist_append(shown_clist, (gchar **)&name);
393                         gtk_clist_set_row_data(shown_clist, row,
394                                                GINT_TO_POINTER(type));
395                 } else {
396                         row = gtk_clist_append(stock_clist, (gchar **)&name);
397                         gtk_clist_set_row_data(stock_clist, row,
398                                                GINT_TO_POINTER(type));
399                 }
400         }
401 }
402
403 static void prefs_summary_column_set_view(void)
404 {
405         GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
406         GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
407         SummaryColumnState state[N_SUMMARY_COLS];
408         SummaryColumnType type;
409         gint row, pos = 0;
410
411         g_return_if_fail
412                 (stock_clist->rows + shown_clist->rows == N_SUMMARY_COLS);
413
414         for (row = 0; row < stock_clist->rows; row++) {
415                 type = GPOINTER_TO_INT
416                         (gtk_clist_get_row_data(stock_clist, row));
417                 state[row].type = type;
418                 state[row].visible = FALSE;
419         }
420
421         pos = row;
422         for (row = 0; row < shown_clist->rows; row++) {
423                 type = GPOINTER_TO_INT
424                         (gtk_clist_get_row_data(shown_clist, row));
425                 state[pos + row].type = type;
426                 state[pos + row].visible = TRUE;
427         }
428
429         prefs_summary_column_set_config(state);
430         main_window_set_summary_column();
431 }
432
433 static void prefs_summary_column_add(void)
434 {
435         GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
436         GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
437         gint row;
438         SummaryColumnType type;
439         gchar *name;
440
441         if (!stock_clist->selection) return;
442
443         row = GPOINTER_TO_INT(stock_clist->selection->data);
444         type = GPOINTER_TO_INT(gtk_clist_get_row_data(stock_clist, row));
445         gtk_clist_remove(stock_clist, row);
446         if (stock_clist->rows == row)
447                 gtk_clist_select_row(stock_clist, row - 1, -1);
448
449         if (!shown_clist->selection)
450                 row = 0;
451         else
452                 row = GPOINTER_TO_INT(shown_clist->selection->data) + 1;
453
454         name = gettext(col_name[type]);
455         row = gtk_clist_insert(shown_clist, row, (gchar **)&name);
456         gtk_clist_set_row_data(shown_clist, row, GINT_TO_POINTER(type));
457         gtk_clist_select_row(shown_clist, row, -1);
458 }
459
460 static void prefs_summary_column_remove(void)
461 {
462         GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
463         GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
464         gint row;
465         SummaryColumnType type;
466         gchar *name;
467
468         if (!shown_clist->selection) return;
469
470         row = GPOINTER_TO_INT(shown_clist->selection->data);
471         type = GPOINTER_TO_INT(gtk_clist_get_row_data(shown_clist, row));
472         gtk_clist_remove(shown_clist, row);
473         if (shown_clist->rows == row)
474                 gtk_clist_select_row(shown_clist, row - 1, -1);
475
476         if (!stock_clist->selection)
477                 row = 0;
478         else
479                 row = GPOINTER_TO_INT(stock_clist->selection->data) + 1;
480
481         name = gettext(col_name[type]);
482         row = gtk_clist_insert(stock_clist, row, (gchar **)&name);
483         gtk_clist_set_row_data(stock_clist, row, GINT_TO_POINTER(type));
484         gtk_clist_select_row(stock_clist, row, -1);
485 }
486
487 static void prefs_summary_column_up(void)
488 {
489         GtkCList *clist = GTK_CLIST(summary_col.shown_clist);
490         gint row;
491
492         if (!clist->selection) return;
493
494         row = GPOINTER_TO_INT(clist->selection->data);
495         if (row > 0)
496                 gtk_clist_row_move(clist, row, row - 1);
497 }
498
499 static void prefs_summary_column_down(void)
500 {
501         GtkCList *clist = GTK_CLIST(summary_col.shown_clist);
502         gint row;
503
504         if (!clist->selection) return;
505
506         row = GPOINTER_TO_INT(clist->selection->data);
507         if (row >= 0 && row < clist->rows - 1)
508                 gtk_clist_row_move(clist, row, row + 1);
509 }
510
511 static void prefs_summary_column_set_to_default(void)
512 {
513         prefs_summary_column_set_dialog(default_state);
514 }
515
516 static void prefs_summary_column_ok(void)
517 {
518         if (!summary_col.finished) {
519                 summary_col.finished = TRUE;
520                 prefs_summary_column_set_view();
521         }
522 }
523
524 static void prefs_summary_column_cancel(void)
525 {
526         summary_col.finished = TRUE;
527 }
528
529 static gint prefs_summary_column_delete_event(GtkWidget *widget,
530                                               GdkEventAny *event,
531                                               gpointer data)
532 {
533         summary_col.finished = TRUE;
534         return TRUE;
535 }
536
537 static void prefs_summary_column_key_pressed(GtkWidget *widget,
538                                              GdkEventKey *event,
539                                              gpointer data)
540 {
541         if (event && event->keyval == GDK_Escape)
542                 summary_col.finished = TRUE;
543 }