Statistics
| Revision:

root / src / prefs_summary_column.c @ 1

History | View | Annotate | Download (15.2 KB)

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.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
};
76

    
77
static SummaryColumnState default_state[N_SUMMARY_COLS] = {
78
        { S_COL_MARK   , TRUE  },
79
        { S_COL_UNREAD , TRUE  },
80
        { S_COL_MIME   , TRUE  },
81
        { S_COL_SUBJECT, TRUE  },
82
        { S_COL_FROM   , TRUE  },
83
        { S_COL_DATE   , TRUE  },
84
        { S_COL_SIZE   , TRUE  },
85
        { S_COL_NUMBER , FALSE }
86
};
87

    
88
static void prefs_summary_column_create        (void);
89

    
90
static void prefs_summary_column_set_dialog        (SummaryColumnState *state);
91
static void prefs_summary_column_set_view        (void);
92

    
93
/* callback functions */
94
static void prefs_summary_column_add        (void);
95
static void prefs_summary_column_remove        (void);
96

    
97
static void prefs_summary_column_up        (void);
98
static void prefs_summary_column_down        (void);
99

    
100
static void prefs_summary_column_set_to_default        (void);
101

    
102
static void prefs_summary_column_ok        (void);
103
static void prefs_summary_column_cancel        (void);
104

    
105
static gint prefs_summary_column_delete_event        (GtkWidget        *widget,
106
                                                 GdkEventAny        *event,
107
                                                 gpointer         data);
108
static gboolean prefs_summary_column_key_pressed(GtkWidget        *widget,
109
                                                 GdkEventKey        *event,
110
                                                 gpointer         data);
111

    
112
void prefs_summary_column_open(void)
113
{
114
        inc_lock();
115

    
116
        if (!summary_col.window)
117
                prefs_summary_column_create();
118

    
119
        manage_window_set_transient(GTK_WINDOW(summary_col.window));
120
        gtk_widget_grab_focus(summary_col.ok_btn);
121

    
122
        prefs_summary_column_set_dialog(NULL);
123

    
124
        gtk_widget_show(summary_col.window);
125

    
126
        summary_col.finished = FALSE;
127
        while (summary_col.finished == FALSE)
128
                gtk_main_iteration();
129

    
130
        gtk_widget_hide(summary_col.window);
131

    
132
        inc_unlock();
133
}
134

    
135
static void prefs_summary_column_create(void)
136
{
137
        GtkWidget *window;
138
        GtkWidget *vbox;
139

    
140
        GtkWidget *label_hbox;
141
        GtkWidget *label;
142

    
143
        GtkWidget *vbox1;
144

    
145
        GtkWidget *hbox1;
146
        GtkWidget *clist_hbox;
147
        GtkWidget *scrolledwin;
148
        GtkWidget *stock_clist;
149
        GtkWidget *shown_clist;
150

    
151
        GtkWidget *btn_vbox;
152
        GtkWidget *btn_vbox1;
153
        GtkWidget *add_btn;
154
        GtkWidget *remove_btn;
155
        GtkWidget *up_btn;
156
        GtkWidget *down_btn;
157

    
158
        GtkWidget *btn_hbox;
159
        GtkWidget *default_btn;
160
        GtkWidget *confirm_area;
161
        GtkWidget *ok_btn;
162
        GtkWidget *cancel_btn;
163

    
164
        gchar *title[1];
165

    
166
        debug_print(_("Creating summary column setting window...\n"));
167

    
168
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
169
        gtk_container_set_border_width(GTK_CONTAINER(window), 8);
170
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
171
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
172
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
173
        gtk_window_set_title(GTK_WINDOW(window),
174
                             _("Summary display item setting"));
175
        g_signal_connect(G_OBJECT(window), "delete_event",
176
                         G_CALLBACK(prefs_summary_column_delete_event), NULL);
177
        g_signal_connect(G_OBJECT(window), "key_press_event",
178
                         G_CALLBACK(prefs_summary_column_key_pressed), NULL);
179

    
180
        vbox = gtk_vbox_new(FALSE, 6);
181
        gtk_widget_show(vbox);
182
        gtk_container_add(GTK_CONTAINER(window), vbox);
183

    
184
        label_hbox = gtk_hbox_new(FALSE, 0);
185
        gtk_widget_show(label_hbox);
186
        gtk_box_pack_start(GTK_BOX(vbox), label_hbox, FALSE, FALSE, 4);
187

    
188
        label = gtk_label_new
189
                (_("Select items to be displayed on the summary view. You can modify\n"
190
                   "the order by using the Up / Down button, or dragging the items."));
191
        gtk_widget_show(label);
192
        gtk_box_pack_start(GTK_BOX(label_hbox), label, FALSE, FALSE, 4);
193
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
194

    
195
        vbox1 = gtk_vbox_new(FALSE, VSPACING);
196
        gtk_widget_show(vbox1);
197
        gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
198
        gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
199

    
200
        hbox1 = gtk_hbox_new(FALSE, 8);
201
        gtk_widget_show(hbox1);
202
        gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
203

    
204
        clist_hbox = gtk_hbox_new(FALSE, 8);
205
        gtk_widget_show(clist_hbox);
206
        gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
207

    
208
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
209
        gtk_widget_set_size_request(scrolledwin, 180, 210);
210
        gtk_widget_show(scrolledwin);
211
        gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
212
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
213
                                       GTK_POLICY_AUTOMATIC,
214
                                       GTK_POLICY_AUTOMATIC);
215

    
216
        title[0] = _("Available items");
217
        stock_clist = gtk_clist_new_with_titles(1, title);
218
        gtk_widget_show(stock_clist);
219
        gtk_container_add(GTK_CONTAINER(scrolledwin), stock_clist);
220
        gtk_clist_set_selection_mode(GTK_CLIST(stock_clist),
221
                                     GTK_SELECTION_BROWSE);
222
        GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(stock_clist)->column[0].button,
223
                               GTK_CAN_FOCUS);
224

    
225
        /* add/remove button */
226
        btn_vbox = gtk_vbox_new(FALSE, 0);
227
        gtk_widget_show(btn_vbox);
228
        gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
229

    
230
        btn_vbox1 = gtk_vbox_new(FALSE, 8);
231
        gtk_widget_show(btn_vbox1);
232
        gtk_box_pack_start(GTK_BOX(btn_vbox), btn_vbox1, TRUE, FALSE, 0);
233

    
234
        add_btn = gtk_button_new_with_label(_("  ->  "));
235
        gtk_widget_show(add_btn);
236
        gtk_box_pack_start(GTK_BOX(btn_vbox1), add_btn, FALSE, FALSE, 0);
237

    
238
        remove_btn = gtk_button_new_with_label(_("  <-  "));
239
        gtk_widget_show(remove_btn);
240
        gtk_box_pack_start(GTK_BOX(btn_vbox1), remove_btn, FALSE, FALSE, 0);
241

    
242
        g_signal_connect(G_OBJECT(add_btn), "clicked",
243
                         G_CALLBACK(prefs_summary_column_add), NULL);
244
        g_signal_connect(G_OBJECT(remove_btn), "clicked",
245
                         G_CALLBACK(prefs_summary_column_remove), NULL);
246

    
247
        clist_hbox = gtk_hbox_new(FALSE, 8);
248
        gtk_widget_show(clist_hbox);
249
        gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
250

    
251
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
252
        gtk_widget_set_size_request(scrolledwin, 180, 210);
253
        gtk_widget_show(scrolledwin);
254
        gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
255
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
256
                                       GTK_POLICY_AUTOMATIC,
257
                                       GTK_POLICY_AUTOMATIC);
258

    
259
        title[0] = _("Displayed items");
260
        shown_clist = gtk_clist_new_with_titles(1, title);
261
        gtk_widget_show(shown_clist);
262
        gtk_container_add(GTK_CONTAINER(scrolledwin), shown_clist);
263
        gtk_clist_set_selection_mode(GTK_CLIST(shown_clist),
264
                                     GTK_SELECTION_BROWSE);
265
        gtk_clist_set_reorderable(GTK_CLIST(shown_clist), TRUE);
266
        gtk_clist_set_use_drag_icons(GTK_CLIST(shown_clist), FALSE);
267
        GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(shown_clist)->column[0].button,
268
                               GTK_CAN_FOCUS);
269

    
270
        /* up/down button */
271
        btn_vbox = gtk_vbox_new(FALSE, 0);
272
        gtk_widget_show(btn_vbox);
273
        gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
274

    
275
        btn_vbox1 = gtk_vbox_new(FALSE, 8);
276
        gtk_widget_show(btn_vbox1);
277
        gtk_box_pack_start(GTK_BOX(btn_vbox), btn_vbox1, TRUE, FALSE, 0);
278

    
279
        up_btn = gtk_button_new_with_label(_("Up"));
280
        gtk_widget_show(up_btn);
281
        gtk_box_pack_start(GTK_BOX(btn_vbox1), up_btn, FALSE, FALSE, 0);
282

    
283
        down_btn = gtk_button_new_with_label(_("Down"));
284
        gtk_widget_show(down_btn);
285
        gtk_box_pack_start(GTK_BOX(btn_vbox1), down_btn, FALSE, FALSE, 0);
286

    
287
        g_signal_connect(G_OBJECT(up_btn), "clicked",
288
                         G_CALLBACK(prefs_summary_column_up), NULL);
289
        g_signal_connect(G_OBJECT(down_btn), "clicked",
290
                         G_CALLBACK(prefs_summary_column_down), NULL);
291

    
292
        btn_hbox = gtk_hbox_new(FALSE, 8);
293
        gtk_widget_show(btn_hbox);
294
        gtk_box_pack_end(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 0);
295

    
296
        btn_vbox = gtk_vbox_new(FALSE, 0);
297
        gtk_widget_show(btn_vbox);
298
        gtk_box_pack_start(GTK_BOX(btn_hbox), btn_vbox, FALSE, FALSE, 0);
299

    
300
        default_btn = gtk_button_new_with_label(_(" Revert to default "));
301
        gtk_widget_show(default_btn);
302
        gtk_box_pack_start(GTK_BOX(btn_vbox), default_btn, TRUE, FALSE, 0);
303
        g_signal_connect(G_OBJECT(default_btn), "clicked",
304
                         G_CALLBACK(prefs_summary_column_set_to_default), NULL);
305

    
306
        gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
307
                                &cancel_btn, _("Cancel"), NULL, NULL);
308
        gtk_widget_show(confirm_area);
309
        gtk_box_pack_end(GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
310
        gtk_widget_grab_default(ok_btn);
311

    
312
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
313
                         G_CALLBACK(prefs_summary_column_ok), NULL);
314
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
315
                         G_CALLBACK(prefs_summary_column_cancel), NULL);
316

    
317
        summary_col.window      = window;
318
        summary_col.stock_clist = stock_clist;
319
        summary_col.shown_clist = shown_clist;
320
        summary_col.add_btn     = add_btn;
321
        summary_col.remove_btn  = remove_btn;
322
        summary_col.up_btn      = up_btn;
323
        summary_col.down_btn    = down_btn;
324
        summary_col.ok_btn      = ok_btn;
325
        summary_col.cancel_btn  = cancel_btn;
326
}
327

    
328
SummaryColumnState *prefs_summary_column_get_config(void)
329
{
330
        static SummaryColumnState state[N_SUMMARY_COLS];
331
        SummaryColumnType type;
332
        gint pos;
333

    
334
        for (pos = 0; pos < N_SUMMARY_COLS; pos++)
335
                state[pos].type = -1;
336

    
337
        for (type = 0; type < N_SUMMARY_COLS; type++) {
338
                pos = prefs_common.summary_col_pos[type];
339
                if (pos < 0 || pos >= N_SUMMARY_COLS ||
340
                    state[pos].type != -1) {
341
                        g_warning("Wrong column position\n");
342
                        prefs_summary_column_set_config(default_state);
343
                        return default_state;
344
                }
345

    
346
                state[pos].type = type;
347
                state[pos].visible = prefs_common.summary_col_visible[type];
348
        }
349

    
350
        return state;
351
}
352

    
353
void prefs_summary_column_set_config(SummaryColumnState *state)
354
{
355
        SummaryColumnType type;
356
        gint pos;
357

    
358
        for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
359
                type = state[pos].type;
360
                prefs_common.summary_col_visible[type] = state[pos].visible;
361
                prefs_common.summary_col_pos[type] = pos;
362
        }
363
}
364

    
365
static void prefs_summary_column_set_dialog(SummaryColumnState *state)
366
{
367
        GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
368
        GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
369
        gint pos;
370
        SummaryColumnType type;
371
        gchar *name;
372

    
373
        gtk_clist_clear(stock_clist);
374
        gtk_clist_clear(shown_clist);
375

    
376
        if (!state)
377
                state = prefs_summary_column_get_config();
378

    
379
        for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
380
                gint row;
381
                type = state[pos].type;
382
                name = gettext(col_name[type]);
383

    
384
                if (state[pos].visible) {
385
                        row = gtk_clist_append(shown_clist, (gchar **)&name);
386
                        gtk_clist_set_row_data(shown_clist, row,
387
                                               GINT_TO_POINTER(type));
388
                } else {
389
                        row = gtk_clist_append(stock_clist, (gchar **)&name);
390
                        gtk_clist_set_row_data(stock_clist, row,
391
                                               GINT_TO_POINTER(type));
392
                }
393
        }
394
}
395

    
396
static void prefs_summary_column_set_view(void)
397
{
398
        GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
399
        GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
400
        SummaryColumnState state[N_SUMMARY_COLS];
401
        SummaryColumnType type;
402
        gint row, pos = 0;
403

    
404
        g_return_if_fail
405
                (stock_clist->rows + shown_clist->rows == N_SUMMARY_COLS);
406

    
407
        for (row = 0; row < stock_clist->rows; row++) {
408
                type = GPOINTER_TO_INT
409
                        (gtk_clist_get_row_data(stock_clist, row));
410
                state[row].type = type;
411
                state[row].visible = FALSE;
412
        }
413

    
414
        pos = row;
415
        for (row = 0; row < shown_clist->rows; row++) {
416
                type = GPOINTER_TO_INT
417
                        (gtk_clist_get_row_data(shown_clist, row));
418
                state[pos + row].type = type;
419
                state[pos + row].visible = TRUE;
420
        }
421

    
422
        prefs_summary_column_set_config(state);
423
        main_window_set_summary_column();
424
}
425

    
426
static void prefs_summary_column_add(void)
427
{
428
        GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
429
        GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
430
        gint row;
431
        SummaryColumnType type;
432
        gchar *name;
433

    
434
        if (!stock_clist->selection) return;
435

    
436
        row = GPOINTER_TO_INT(stock_clist->selection->data);
437
        type = GPOINTER_TO_INT(gtk_clist_get_row_data(stock_clist, row));
438
        gtk_clist_remove(stock_clist, row);
439
        if (stock_clist->rows == row)
440
                gtk_clist_select_row(stock_clist, row - 1, -1);
441

    
442
        if (!shown_clist->selection)
443
                row = 0;
444
        else
445
                row = GPOINTER_TO_INT(shown_clist->selection->data) + 1;
446

    
447
        name = gettext(col_name[type]);
448
        row = gtk_clist_insert(shown_clist, row, (gchar **)&name);
449
        gtk_clist_set_row_data(shown_clist, row, GINT_TO_POINTER(type));
450
        gtk_clist_select_row(shown_clist, row, -1);
451
}
452

    
453
static void prefs_summary_column_remove(void)
454
{
455
        GtkCList *stock_clist = GTK_CLIST(summary_col.stock_clist);
456
        GtkCList *shown_clist = GTK_CLIST(summary_col.shown_clist);
457
        gint row;
458
        SummaryColumnType type;
459
        gchar *name;
460

    
461
        if (!shown_clist->selection) return;
462

    
463
        row = GPOINTER_TO_INT(shown_clist->selection->data);
464
        type = GPOINTER_TO_INT(gtk_clist_get_row_data(shown_clist, row));
465
        gtk_clist_remove(shown_clist, row);
466
        if (shown_clist->rows == row)
467
                gtk_clist_select_row(shown_clist, row - 1, -1);
468

    
469
        if (!stock_clist->selection)
470
                row = 0;
471
        else
472
                row = GPOINTER_TO_INT(stock_clist->selection->data) + 1;
473

    
474
        name = gettext(col_name[type]);
475
        row = gtk_clist_insert(stock_clist, row, (gchar **)&name);
476
        gtk_clist_set_row_data(stock_clist, row, GINT_TO_POINTER(type));
477
        gtk_clist_select_row(stock_clist, row, -1);
478
}
479

    
480
static void prefs_summary_column_up(void)
481
{
482
        GtkCList *clist = GTK_CLIST(summary_col.shown_clist);
483
        gint row;
484

    
485
        if (!clist->selection) return;
486

    
487
        row = GPOINTER_TO_INT(clist->selection->data);
488
        if (row > 0)
489
                gtk_clist_row_move(clist, row, row - 1);
490
}
491

    
492
static void prefs_summary_column_down(void)
493
{
494
        GtkCList *clist = GTK_CLIST(summary_col.shown_clist);
495
        gint row;
496

    
497
        if (!clist->selection) return;
498

    
499
        row = GPOINTER_TO_INT(clist->selection->data);
500
        if (row >= 0 && row < clist->rows - 1)
501
                gtk_clist_row_move(clist, row, row + 1);
502
}
503

    
504
static void prefs_summary_column_set_to_default(void)
505
{
506
        prefs_summary_column_set_dialog(default_state);
507
}
508

    
509
static void prefs_summary_column_ok(void)
510
{
511
        if (!summary_col.finished) {
512
                summary_col.finished = TRUE;
513
                prefs_summary_column_set_view();
514
        }
515
}
516

    
517
static void prefs_summary_column_cancel(void)
518
{
519
        summary_col.finished = TRUE;
520
}
521

    
522
static gint prefs_summary_column_delete_event(GtkWidget *widget,
523
                                              GdkEventAny *event,
524
                                              gpointer data)
525
{
526
        summary_col.finished = TRUE;
527
        return TRUE;
528
}
529

    
530
static gboolean prefs_summary_column_key_pressed(GtkWidget *widget,
531
                                                 GdkEventKey *event,
532
                                                 gpointer data)
533
{
534
        if (event && event->keyval == GDK_Escape)
535
                summary_col.finished = TRUE;
536
        return FALSE;
537
}