2005-08-06 [paul] 1.9.13cvs18
[claws.git] / src / prefs_summary_column.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkhbox.h>
32 #include <gtk/gtkbutton.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include "prefs_gtk.h"
36 #include "prefs_common.h"
37 #include "prefs_summary_column.h"
38 #include "manage_window.h"
39 #include "summaryview.h"
40 #include "mainwindow.h"
41 #include "inc.h"
42 #include "gtkutils.h"
43 #include "utils.h"
44
45 enum {
46         SUMCOL_NAME,
47         SUMCOL_TYPE,
48         N_SUMCOL_COLUMNS
49 };
50
51 #define TARGET_INFO_SUMCOL  (0xFEEDBABE)
52
53 static const GtkTargetEntry row_targets[] = {
54         { "PREFS_SUM_COL_MODEL_ROW", GTK_TARGET_SAME_APP, TARGET_INFO_SUMCOL }
55 };
56
57
58 static struct _SummaryColumnDialog
59 {
60         GtkWidget *window;
61
62         GtkWidget *stock_list_view;
63         GtkWidget *shown_list_view;
64
65         GtkWidget *add_btn;
66         GtkWidget *remove_btn;
67         GtkWidget *up_btn;
68         GtkWidget *down_btn;
69
70         GtkWidget *default_btn;
71
72         GtkWidget *ok_btn;
73         GtkWidget *cancel_btn;
74
75         gboolean finished;
76 } summary_col;
77
78 static const gchar *const col_name[N_SUMMARY_COLS] = {
79         N_("Mark"),             /* S_COL_MARK    */
80         N_("Status"),           /* S_COL_STATUS  */
81         N_("Attachment"),       /* S_COL_MIME    */
82         N_("Subject"),          /* S_COL_SUBJECT */
83         N_("From"),             /* S_COL_FROM    */
84         N_("To"),               /* S_COL_TO      */
85         N_("Date"),             /* S_COL_DATE    */
86         N_("Size"),             /* S_COL_SIZE    */
87         N_("Number"),           /* S_COL_NUMBER  */
88         N_("Score"),            /* S_COL_SCORE   */
89         N_("Locked"),           /* S_COL_LOCKED  */
90 };
91
92 static SummaryColumnState default_state[N_SUMMARY_COLS] = {
93         { S_COL_MARK   , TRUE  },
94         { S_COL_STATUS , TRUE  },
95         { S_COL_MIME   , TRUE  },
96         { S_COL_SUBJECT, TRUE  },
97         { S_COL_FROM   , TRUE  },
98         { S_COL_TO     , FALSE },
99         { S_COL_DATE   , TRUE  },
100         { S_COL_SIZE   , TRUE  },
101         { S_COL_NUMBER , FALSE },
102         { S_COL_SCORE  , FALSE },
103         { S_COL_LOCKED , FALSE },
104 };
105
106 static void prefs_summary_column_create (void);
107
108 static void prefs_summary_column_set_dialog     (SummaryColumnState *state);
109 static void prefs_summary_column_set_view       (void);
110
111 /* callback functions */
112 static void prefs_summary_column_add    (void);
113 static void prefs_summary_column_remove (void);
114
115 static void prefs_summary_column_up     (void);
116 static void prefs_summary_column_down   (void);
117
118 static void prefs_summary_column_set_to_default (void);
119
120 static void prefs_summary_column_ok     (void);
121 static void prefs_summary_column_cancel (void);
122
123 static gint prefs_summary_column_delete_event   (GtkWidget      *widget,
124                                                  GdkEventAny    *event,
125                                                  gpointer        data);
126 static gboolean prefs_summary_column_key_pressed(GtkWidget      *widget,
127                                                  GdkEventKey    *event,
128                                                  gpointer        data);
129
130 static GtkListStore *prefs_summary_column_create_store  (void);
131
132 static void prefs_summary_column_insert_column  (GtkListStore *store,
133                                                  gint row,
134                                                  const gchar *name,
135                                                  SummaryColumnType type);
136                                                
137 static SummaryColumnType prefs_summary_column_get_column        (GtkWidget *list, 
138                                                                  gint row);
139
140 static GtkWidget *prefs_summary_column_list_view_create (const gchar *name);
141
142 static void prefs_filtering_create_list_view_columns    (GtkWidget *list_view, 
143                                                          const gchar *name);
144
145 static void drag_data_get       (GtkTreeView *tree_view, 
146                                  GdkDragContext *context, 
147                                  GtkSelectionData *data, 
148                                  guint info, 
149                                  guint time, 
150                                  GtkTreeModel *model);
151                           
152 static void drag_data_received  (GtkTreeView *tree_view, 
153                                  GdkDragContext *context,
154                                  gint x, gint y, 
155                                  GtkSelectionData *data,
156                                  guint info, 
157                                  guint time, 
158                                  GtkTreeModel *model);
159
160 void prefs_summary_column_open(void)
161 {
162         inc_lock();
163
164         if (!summary_col.window)
165                 prefs_summary_column_create();
166
167         manage_window_set_transient(GTK_WINDOW(summary_col.window));
168         gtk_widget_grab_focus(summary_col.ok_btn);
169
170         prefs_summary_column_set_dialog(NULL);
171
172         gtk_widget_show(summary_col.window);
173
174         summary_col.finished = FALSE;
175         while (summary_col.finished == FALSE)
176                 gtk_main_iteration();
177
178         gtk_widget_hide(summary_col.window);
179
180         inc_unlock();
181 }
182
183 static void prefs_summary_column_create(void)
184 {
185         GtkWidget *window;
186         GtkWidget *vbox;
187
188         GtkWidget *label_hbox;
189         GtkWidget *label;
190
191         GtkWidget *vbox1;
192
193         GtkWidget *hbox1;
194         GtkWidget *clist_hbox;
195         GtkWidget *scrolledwin;
196         GtkWidget *stock_list_view;
197         GtkWidget *shown_list_view;
198
199         GtkWidget *btn_vbox;
200         GtkWidget *add_btn;
201         GtkWidget *remove_btn;
202         GtkWidget *up_btn;
203         GtkWidget *down_btn;
204
205         GtkWidget *btn_hbox;
206         GtkWidget *default_btn;
207         GtkWidget *confirm_area;
208         GtkWidget *ok_btn;
209         GtkWidget *cancel_btn;
210
211         debug_print("Creating summary column setting window...\n");
212
213         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
214         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
215         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
216         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
217         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
218         gtk_window_set_title(GTK_WINDOW(window),
219                              _("Message list columns configuration"));
220         g_signal_connect(G_OBJECT(window), "delete_event",
221                          G_CALLBACK(prefs_summary_column_delete_event),
222                          NULL);
223         g_signal_connect(G_OBJECT(window), "key_press_event",
224                          G_CALLBACK(prefs_summary_column_key_pressed),
225                          NULL);
226
227         vbox = gtk_vbox_new(FALSE, 6);
228         gtk_widget_show(vbox);
229         gtk_container_add(GTK_CONTAINER(window), vbox);
230
231         label_hbox = gtk_hbox_new(FALSE, 0);
232         gtk_widget_show(label_hbox);
233         gtk_box_pack_start(GTK_BOX(vbox), label_hbox, FALSE, FALSE, 4);
234
235         label = gtk_label_new
236                 (_("Select columns to be displayed in the message list. You can modify\n"
237                    "the order by using the Up / Down buttons or by dragging the items."));
238         gtk_widget_show(label);
239         gtk_box_pack_start(GTK_BOX(label_hbox), label, FALSE, FALSE, 4);
240         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
241
242         vbox1 = gtk_vbox_new(FALSE, VSPACING);
243         gtk_widget_show(vbox1);
244         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
245         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
246
247         hbox1 = gtk_hbox_new(FALSE, 8);
248         gtk_widget_show(hbox1);
249         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
250
251         clist_hbox = gtk_hbox_new(FALSE, 8);
252         gtk_widget_show(clist_hbox);
253         gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
254
255         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
256         gtk_widget_set_size_request(scrolledwin, 180, 210);
257         gtk_widget_show(scrolledwin);
258         gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
259         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
260                                        GTK_POLICY_AUTOMATIC,
261                                        GTK_POLICY_AUTOMATIC);
262
263                                        
264         stock_list_view = prefs_summary_column_list_view_create
265                                 (_("Hidden columns"));
266         gtk_widget_show(stock_list_view);
267         gtk_container_add(GTK_CONTAINER(scrolledwin), stock_list_view);
268
269         /* add/remove button */
270         btn_vbox = gtk_vbox_new(FALSE, 8);
271         gtk_widget_show(btn_vbox);
272         gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
273
274         add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
275         gtk_widget_show(add_btn);
276         gtk_box_pack_start(GTK_BOX(btn_vbox), add_btn, FALSE, TRUE, 0);
277
278         g_signal_connect(G_OBJECT(add_btn), "clicked",
279                          G_CALLBACK(prefs_summary_column_add), NULL);
280
281         clist_hbox = gtk_hbox_new(FALSE, 8);
282         gtk_widget_show(clist_hbox);
283         gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
284
285         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
286         gtk_widget_set_size_request(scrolledwin, 180, 210);
287         gtk_widget_show(scrolledwin);
288         gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
289         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
290                                        GTK_POLICY_AUTOMATIC,
291                                        GTK_POLICY_AUTOMATIC);
292
293         shown_list_view = prefs_summary_column_list_view_create
294                                 (_("Displayed columns"));
295         gtk_widget_show(shown_list_view);
296         gtk_container_add(GTK_CONTAINER(scrolledwin), shown_list_view);
297
298         /* up/down button */
299         btn_vbox = gtk_vbox_new(FALSE, 8);
300         gtk_widget_show(btn_vbox);
301         gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
302
303         remove_btn = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
304         gtk_widget_show(remove_btn);
305         gtk_box_pack_start(GTK_BOX(btn_vbox), remove_btn, FALSE, TRUE, 0);
306
307         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
308         gtk_widget_show(up_btn);
309         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, TRUE, 0);
310
311         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
312         gtk_widget_show(down_btn);
313         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, TRUE, 0);
314
315         g_signal_connect(G_OBJECT(remove_btn), "clicked",
316                          G_CALLBACK(prefs_summary_column_remove), NULL);
317         g_signal_connect(G_OBJECT(up_btn), "clicked",
318                          G_CALLBACK(prefs_summary_column_up), NULL);
319         g_signal_connect(G_OBJECT(down_btn), "clicked",
320                          G_CALLBACK(prefs_summary_column_down), NULL);
321
322         btn_hbox = gtk_hbox_new(FALSE, 8);
323         gtk_widget_show(btn_hbox);
324         gtk_box_pack_end(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 0);
325
326         btn_vbox = gtk_vbox_new(FALSE, 0);
327         gtk_widget_show(btn_vbox);
328         gtk_box_pack_start(GTK_BOX(btn_hbox), btn_vbox, FALSE, FALSE, 0);
329
330         default_btn = gtk_button_new_with_label(_(" Use default "));
331         gtk_widget_show(default_btn);
332         gtk_box_pack_start(GTK_BOX(btn_vbox), default_btn, TRUE, FALSE, 0);
333         g_signal_connect(G_OBJECT(default_btn), "clicked",
334                          G_CALLBACK(prefs_summary_column_set_to_default),
335                          NULL);
336
337         gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
338                                       &cancel_btn, GTK_STOCK_CANCEL,
339                                       NULL, NULL);
340         gtk_widget_show(confirm_area);
341         gtk_box_pack_end(GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
342         gtk_widget_grab_default(ok_btn);
343
344         g_signal_connect(G_OBJECT(ok_btn), "clicked",
345                          G_CALLBACK(prefs_summary_column_ok), NULL);
346         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
347                          G_CALLBACK(prefs_summary_column_cancel), NULL);
348
349
350         summary_col.window      = window;
351         summary_col.add_btn     = add_btn;
352         summary_col.remove_btn  = remove_btn;
353         summary_col.up_btn      = up_btn;
354         summary_col.down_btn    = down_btn;
355         summary_col.ok_btn      = ok_btn;
356         summary_col.cancel_btn  = cancel_btn;
357         summary_col.stock_list_view = stock_list_view;
358         summary_col.shown_list_view = shown_list_view;
359 }
360
361 SummaryColumnState *prefs_summary_column_get_config(void)
362 {
363         static SummaryColumnState state[N_SUMMARY_COLS];
364         SummaryColumnType type;
365         gint pos;
366
367         for (pos = 0; pos < N_SUMMARY_COLS; pos++)
368                 state[pos].type = -1;
369
370         for (type = 0; type < N_SUMMARY_COLS; type++) {
371                 pos = prefs_common.summary_col_pos[type];
372                 if (pos < 0 || pos >= N_SUMMARY_COLS ||
373                     state[pos].type != -1) {
374                         g_warning("Wrong column position\n");
375                         prefs_summary_column_set_config(default_state);
376                         return default_state;
377                 }
378
379                 state[pos].type = type;
380                 state[pos].visible = prefs_common.summary_col_visible[type];
381         }
382
383         return state;
384 }
385
386 void prefs_summary_column_set_config(SummaryColumnState *state)
387 {
388         SummaryColumnType type;
389         gint pos;
390
391         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
392                 type = state[pos].type;
393                 prefs_common.summary_col_visible[type] = state[pos].visible;
394                 prefs_common.summary_col_pos[type] = pos;
395         }
396 }
397
398 static void prefs_summary_column_set_dialog(SummaryColumnState *state)
399 {
400         GtkListStore *stock_store, *shown_store;
401         gint pos;
402         SummaryColumnType type;
403         gchar *name;
404
405         stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
406                         (GTK_TREE_VIEW(summary_col.stock_list_view)));
407         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
408                         (GTK_TREE_VIEW(summary_col.shown_list_view)));
409
410         gtk_list_store_clear(stock_store);
411         gtk_list_store_clear(shown_store);
412
413         if (!state)
414                 state = prefs_summary_column_get_config();
415
416         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
417                 type = state[pos].type;
418                 name = gettext(col_name[type]);
419
420                 if (state[pos].visible)
421                         prefs_summary_column_insert_column(shown_store,
422                                                            -1, name,
423                                                            type);
424                 else
425                         prefs_summary_column_insert_column(stock_store,
426                                                             -1, name,
427                                                             type);
428         }
429 }
430
431 static void prefs_summary_column_set_view(void)
432 {
433         gint stock_n_rows, shown_n_rows;
434         SummaryColumnState state[N_SUMMARY_COLS];
435         SummaryColumnType type;
436         gint row, pos = 0;
437
438         stock_n_rows = gtk_tree_model_iter_n_children
439                 (gtk_tree_view_get_model(GTK_TREE_VIEW
440                         (summary_col.stock_list_view)), NULL);
441         shown_n_rows = gtk_tree_model_iter_n_children
442                 (gtk_tree_view_get_model(GTK_TREE_VIEW
443                         (summary_col.shown_list_view)), NULL);
444
445         g_return_if_fail
446                 (stock_n_rows + shown_n_rows == N_SUMMARY_COLS);
447
448         for (row = 0; row < stock_n_rows; row++) {
449                 type = prefs_summary_column_get_column
450                         (summary_col.stock_list_view, row);
451                 state[row].type = type;
452                 state[row].visible = FALSE;
453         }
454
455         pos = row;
456         for (row = 0; row < shown_n_rows; row++) {
457                 type = prefs_summary_column_get_column
458                         (summary_col.shown_list_view, row);
459                 state[pos + row].type = type;
460                 state[pos + row].visible = TRUE;
461         }
462
463         prefs_summary_column_set_config(state);
464         main_window_set_summary_column();
465 }
466
467 static void prefs_summary_column_add(void)
468 {
469         GtkListStore *stock_store, *shown_store;
470         GtkTreeIter stock_sel, shown_sel, shown_add;
471         gboolean shown_sel_valid;
472         gchar *name;
473         SummaryColumnType type;
474         
475         stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
476                 (GTK_TREE_VIEW(summary_col.stock_list_view)));
477         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
478                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
479         
480         if (!gtk_tree_selection_get_selected
481                 (gtk_tree_view_get_selection
482                         (GTK_TREE_VIEW(summary_col.stock_list_view)),
483                  NULL,
484                  &stock_sel))
485                 return;
486
487         shown_sel_valid = gtk_tree_selection_get_selected
488                 (gtk_tree_view_get_selection
489                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
490                  NULL,
491                  &shown_sel);
492                          
493         gtk_tree_model_get(GTK_TREE_MODEL(stock_store), &stock_sel,
494                            SUMCOL_TYPE, &type,
495                            -1);
496                         
497         gtk_list_store_remove(stock_store, &stock_sel);
498
499         gtk_list_store_insert_after(shown_store, &shown_add, 
500                                     shown_sel_valid ? &shown_sel : NULL);
501
502         name = gettext(col_name[type]);                             
503                                     
504         gtk_list_store_set(shown_store, &shown_add,
505                            SUMCOL_NAME, name,
506                            SUMCOL_TYPE, type,
507                            -1);
508         
509         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
510                 (GTK_TREE_VIEW(summary_col.shown_list_view)),
511                  &shown_add);
512 }
513
514 static void prefs_summary_column_remove(void)
515 {
516         GtkListStore *stock_store, *shown_store;
517         GtkTreeIter shown_sel, stock_sel, stock_add;
518         gboolean stock_sel_valid;
519         gchar *name;
520         SummaryColumnType type;
521         
522         stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
523                 (GTK_TREE_VIEW(summary_col.stock_list_view)));
524         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
525                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
526                 
527         if (!gtk_tree_selection_get_selected
528                 (gtk_tree_view_get_selection
529                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
530                  NULL,
531                  &shown_sel))
532                 return;
533
534         stock_sel_valid = gtk_tree_selection_get_selected
535                 (gtk_tree_view_get_selection
536                         (GTK_TREE_VIEW(summary_col.stock_list_view)),
537                  NULL,
538                  &stock_sel);
539         
540         gtk_tree_model_get(GTK_TREE_MODEL(shown_store), &shown_sel,
541                            SUMCOL_TYPE, &type,
542                            -1);
543                         
544         gtk_list_store_remove(shown_store, &shown_sel);
545
546         gtk_list_store_insert_after(stock_store, &stock_add, 
547                                     stock_sel_valid ? &stock_sel : NULL);
548
549         name = gettext(col_name[type]);                             
550                                     
551         gtk_list_store_set(stock_store, &stock_add,
552                            SUMCOL_NAME, name,
553                            SUMCOL_TYPE, type,
554                            -1);
555         
556         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
557                 (GTK_TREE_VIEW(summary_col.stock_list_view)),
558                 &stock_add);
559 }
560
561 static void prefs_summary_column_up(void)
562 {
563         GtkTreePath *prev, *sel;
564         GtkTreeIter isel;
565         GtkListStore *shown_store;
566         GtkTreeIter iprev;
567         
568         if (!gtk_tree_selection_get_selected
569                 (gtk_tree_view_get_selection
570                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
571                  NULL,
572                  &isel))
573                 return;
574
575         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
576                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
577
578         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(shown_store), 
579                                       &isel);
580         if (!sel)
581                 return;
582
583         prev = gtk_tree_path_copy(sel);         
584         if (!gtk_tree_path_prev(prev)) {
585                 gtk_tree_path_free(prev);
586                 gtk_tree_path_free(sel);
587                 return;
588         }
589
590         gtk_tree_model_get_iter(GTK_TREE_MODEL(shown_store),
591                                 &iprev, prev);
592         gtk_tree_path_free(sel);
593         gtk_tree_path_free(prev);
594
595         gtk_list_store_swap(shown_store, &iprev, &isel);
596 }
597
598 static void prefs_summary_column_down(void)
599 {
600         GtkListStore *shown_store;
601         GtkTreeIter next, sel;
602         
603         if (!gtk_tree_selection_get_selected
604                 (gtk_tree_view_get_selection
605                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
606                  NULL,
607                  &sel))
608                 return;
609
610         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
611                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
612
613         next = sel;
614         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(shown_store), &next)) 
615                 return;
616
617         gtk_list_store_swap(shown_store, &next, &sel);
618 }
619
620 static void prefs_summary_column_set_to_default(void)
621 {
622         prefs_summary_column_set_dialog(default_state);
623 }
624
625 static void prefs_summary_column_ok(void)
626 {
627         if (!summary_col.finished) {
628                 summary_col.finished = TRUE;
629                 prefs_summary_column_set_view();
630         }
631 }
632
633 static void prefs_summary_column_cancel(void)
634 {
635         summary_col.finished = TRUE;
636 }
637
638 static gint prefs_summary_column_delete_event(GtkWidget *widget,
639                                               GdkEventAny *event,
640                                               gpointer data)
641 {
642         summary_col.finished = TRUE;
643         return TRUE;
644 }
645
646 static gboolean prefs_summary_column_key_pressed(GtkWidget *widget,
647                                                  GdkEventKey *event,
648                                                  gpointer data)
649 {
650         if (event && event->keyval == GDK_Escape)
651                 summary_col.finished = TRUE;
652         return FALSE;
653 }
654
655 static GtkListStore *prefs_summary_column_create_store(void)
656 {
657         return gtk_list_store_new(N_SUMCOL_COLUMNS,
658                                   G_TYPE_STRING,
659                                   G_TYPE_INT,
660                                   -1);
661 }
662
663 static void prefs_summary_column_insert_column(GtkListStore *store,
664                                                gint row,
665                                                const gchar *name,
666                                                SummaryColumnType type)
667 {
668         GtkTreeIter iter;
669
670         if (row >= 0) {
671                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
672                                                    &iter, NULL, row))
673                         row = -1;                                                  
674         }
675         if (row < 0) {
676                 /* add new */
677                 gtk_list_store_append(store, &iter);
678                 gtk_list_store_set(store, &iter,
679                                    SUMCOL_NAME, name,
680                                    SUMCOL_TYPE, type,
681                                    -1);
682                 return;
683         } else {
684                 /* change existing */
685                 gtk_list_store_set(store, &iter, 
686                                    SUMCOL_NAME, name,
687                                    SUMCOL_TYPE, type,
688                                    -1);
689         }
690 }
691
692 /*!
693  *\brief        Return the columnn type for a row
694  */
695 static SummaryColumnType prefs_summary_column_get_column(GtkWidget *list, gint row)
696 {       
697         GtkTreeView *list_view = GTK_TREE_VIEW(list);
698         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
699         GtkTreeIter iter;
700         SummaryColumnType result;
701
702         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
703                 return -1;
704         
705         gtk_tree_model_get(model, &iter, 
706                            SUMCOL_TYPE, &result,
707                            -1);
708         
709         return result;
710 }
711
712 static GtkWidget *prefs_summary_column_list_view_create(const gchar *name)
713 {
714         GtkWidget *list_view;
715         GtkTreeSelection *selector;
716         GtkTreeModel *model;
717
718         model = GTK_TREE_MODEL(prefs_summary_column_create_store());
719         list_view = gtk_tree_view_new_with_model(model);
720         g_object_unref(G_OBJECT(model));
721         
722         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view),
723                                      prefs_common.enable_rules_hint);
724         
725         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
726         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
727
728         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
729
730         gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(list_view),
731                                                GDK_BUTTON1_MASK,
732                                                row_targets,
733                                                G_N_ELEMENTS(row_targets), 
734                                                GDK_ACTION_MOVE);
735                                             
736         gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(list_view), 
737                                              row_targets, 
738                                              G_N_ELEMENTS(row_targets), 
739                                              GDK_ACTION_MOVE);
740                 
741         g_signal_connect(G_OBJECT(list_view), "drag_data_get",
742                          G_CALLBACK(drag_data_get),
743                          model);
744
745         g_signal_connect(G_OBJECT(list_view), "drag_data_received",
746                          G_CALLBACK(drag_data_received),
747                          model);
748
749         return list_view;
750 }
751
752 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view, 
753                                                      const gchar *name)
754 {
755         GtkTreeViewColumn *column;
756         GtkCellRenderer *renderer;
757
758         renderer = gtk_cell_renderer_text_new();
759         column = gtk_tree_view_column_new_with_attributes
760                 (name, renderer, "text", SUMCOL_NAME, NULL);
761         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
762 }
763
764 static void drag_data_get(GtkTreeView *tree_view, GdkDragContext *context, 
765                           GtkSelectionData *data, guint info, 
766                           guint time, GtkTreeModel *model)
767 {
768         GtkTreeIter iter;
769         SummaryColumnType type;
770         GtkTreeModel *source_model;
771
772         if (info != TARGET_INFO_SUMCOL) 
773                 return;
774
775         if (!gtk_tree_selection_get_selected
776                         (gtk_tree_view_get_selection(tree_view),
777                          &source_model, &iter)) 
778                 return;                  
779         
780         gtk_tree_model_get(source_model, &iter, 
781                            SUMCOL_TYPE, &type,
782                            -1);
783
784         /* send the type */
785         gtk_selection_data_set(data, data->target, 8, (gchar *) &type, sizeof type);
786 }
787
788 static void drag_data_received(GtkTreeView *tree_view, GdkDragContext *context,
789                                gint x, gint y, GtkSelectionData *data,
790                                guint info, guint time, GtkTreeModel *model)
791 {
792         GtkWidget *source;
793         GtkTreePath *dst = NULL, *sel = NULL;
794         GtkTreeIter isel, idst;
795         GtkTreeViewDropPosition pos;
796         SummaryColumnType type;
797         GtkTreeModel *sel_model;
798         gchar *name;
799         
800         source = gtk_drag_get_source_widget(context);
801         
802         if (source == GTK_WIDGET(tree_view)) {
803         
804                 /*
805                  * Same widget: re-order
806                  */
807                  
808                 gtk_tree_selection_get_selected(gtk_tree_view_get_selection(tree_view),
809                                            NULL, &isel);
810                 sel = gtk_tree_model_get_path(model, &isel);
811                 gtk_tree_view_get_dest_row_at_pos(tree_view, x, y,
812                                                   &dst, &pos);
813
814                 /* NOTE: dst is invalid if selection beyond last row, in that
815                  * case move beyond last one (XXX_move_before(..., NULL)) */                                              
816
817                 if (dst)                                                  
818                         gtk_tree_model_get_iter(model, &idst, dst);
819                 else 
820                         gtk_list_store_move_before(GTK_LIST_STORE(model),
821                                                    &isel,
822                                                    NULL);
823
824                 /* we do not drag if no valid dst and sel, and when
825                  * dst and sel are the same (moving after or before
826                  * itself doesn't change order...) */
827                 if ((dst && sel) && gtk_tree_path_compare(sel, dst) != 0) {
828                         if (pos == GTK_TREE_VIEW_DROP_BEFORE
829                         ||  pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
830                                 gtk_list_store_move_before(GTK_LIST_STORE(model),
831                                                            &isel,
832                                                            &idst);
833                         else
834                                 gtk_list_store_move_after(GTK_LIST_STORE(model),
835                                                           &isel,
836                                                           &idst);
837                         
838                 } 
839                 gtk_tree_path_free(dst);                                          
840                 gtk_tree_path_free(sel);
841                 gtk_drag_finish(context, TRUE, FALSE, time);
842                 
843         } else if (source == summary_col.stock_list_view 
844         ||         source == summary_col.shown_list_view) {
845         
846                 /*
847                  * Other widget: change and update
848                  */
849
850                 
851                 /* get source information and remove */
852                 gtk_tree_selection_get_selected(gtk_tree_view_get_selection(
853                                                 GTK_TREE_VIEW(source)),
854                                                 &sel_model, &isel);
855                 type = *((gint *) data->data);
856                 name = gettext(col_name[type]);
857                 gtk_list_store_remove(GTK_LIST_STORE(sel_model), &isel);
858
859                 /* get insertion position */
860                 gtk_tree_view_get_dest_row_at_pos(tree_view, x, y, &dst, &pos);
861
862                 /* NOTE: dst is invalid if insertion point beyond last row, 
863                  * just append to list in that case (XXX_store_append()) */
864
865                 if (dst) {
866                         gtk_tree_model_get_iter(model, &idst, dst);
867
868                         if (pos == GTK_TREE_VIEW_DROP_BEFORE
869                         ||  pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
870                                 gtk_list_store_insert_before(GTK_LIST_STORE(model),
871                                                              &isel,
872                                                              &idst);
873                         else
874                                 gtk_list_store_insert_after(GTK_LIST_STORE(model),
875                                                             &isel,
876                                                             &idst);
877                 } else
878                         gtk_list_store_append(GTK_LIST_STORE(model),
879                                               &isel);
880                 
881                 gtk_list_store_set(GTK_LIST_STORE(model), &isel,
882                                    SUMCOL_NAME, name,
883                                    SUMCOL_TYPE, type, -1);
884                 gtk_tree_path_free(dst);
885                 gtk_drag_finish(context, TRUE, FALSE, time);
886         }
887
888         /* XXXX: should we call gtk_drag_finish() for other code paths? */
889 }
890