6901a0c12755288d644f80d966085ceee32bfe91
[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 *btn_vbox1;
201         GtkWidget *add_btn;
202         GtkWidget *remove_btn;
203         GtkWidget *up_btn;
204         GtkWidget *down_btn;
205
206         GtkWidget *btn_hbox;
207         GtkWidget *default_btn;
208         GtkWidget *confirm_area;
209         GtkWidget *ok_btn;
210         GtkWidget *cancel_btn;
211
212         debug_print("Creating summary column setting window...\n");
213
214         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
215         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
216         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
217         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
218         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
219         gtk_window_set_title(GTK_WINDOW(window),
220                              _("Displayed items configuration"));
221         g_signal_connect(G_OBJECT(window), "delete_event",
222                          G_CALLBACK(prefs_summary_column_delete_event),
223                          NULL);
224         g_signal_connect(G_OBJECT(window), "key_press_event",
225                          G_CALLBACK(prefs_summary_column_key_pressed),
226                          NULL);
227
228         vbox = gtk_vbox_new(FALSE, 6);
229         gtk_widget_show(vbox);
230         gtk_container_add(GTK_CONTAINER(window), vbox);
231
232         label_hbox = gtk_hbox_new(FALSE, 0);
233         gtk_widget_show(label_hbox);
234         gtk_box_pack_start(GTK_BOX(vbox), label_hbox, FALSE, FALSE, 4);
235
236         label = gtk_label_new
237                 (_("Select items to be displayed in the summary view. You can modify\n"
238                    "the order by using the Up / Down buttons or by dragging the items."));
239         gtk_widget_show(label);
240         gtk_box_pack_start(GTK_BOX(label_hbox), label, FALSE, FALSE, 4);
241         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
242
243         vbox1 = gtk_vbox_new(FALSE, VSPACING);
244         gtk_widget_show(vbox1);
245         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
246         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
247
248         hbox1 = gtk_hbox_new(FALSE, 8);
249         gtk_widget_show(hbox1);
250         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
251
252         clist_hbox = gtk_hbox_new(FALSE, 8);
253         gtk_widget_show(clist_hbox);
254         gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
255
256         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
257         gtk_widget_set_size_request(scrolledwin, 180, 210);
258         gtk_widget_show(scrolledwin);
259         gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
260         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
261                                        GTK_POLICY_AUTOMATIC,
262                                        GTK_POLICY_AUTOMATIC);
263
264                                        
265         stock_list_view = prefs_summary_column_list_view_create
266                                 (_("Available items"));
267         gtk_widget_show(stock_list_view);
268         gtk_container_add(GTK_CONTAINER(scrolledwin), stock_list_view);
269
270         /* add/remove 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         add_btn = gtk_button_new_with_label(_("  ->  "));
280         gtk_widget_show(add_btn);
281         gtk_box_pack_start(GTK_BOX(btn_vbox1), add_btn, FALSE, FALSE, 0);
282
283         remove_btn = gtk_button_new_with_label(_("  <-  "));
284         gtk_widget_show(remove_btn);
285         gtk_box_pack_start(GTK_BOX(btn_vbox1), remove_btn, FALSE, FALSE, 0);
286
287         g_signal_connect(G_OBJECT(add_btn), "clicked",
288                          G_CALLBACK(prefs_summary_column_add), NULL);
289         g_signal_connect(G_OBJECT(remove_btn), "clicked",
290                          G_CALLBACK(prefs_summary_column_remove), NULL);
291
292         clist_hbox = gtk_hbox_new(FALSE, 8);
293         gtk_widget_show(clist_hbox);
294         gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
295
296         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
297         gtk_widget_set_size_request(scrolledwin, 180, 210);
298         gtk_widget_show(scrolledwin);
299         gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
300         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
301                                        GTK_POLICY_AUTOMATIC,
302                                        GTK_POLICY_AUTOMATIC);
303
304         shown_list_view = prefs_summary_column_list_view_create
305                                 (_("Displayed items"));
306         gtk_widget_show(shown_list_view);
307         gtk_container_add(GTK_CONTAINER(scrolledwin), shown_list_view);
308
309         /* up/down button */
310         btn_vbox = gtk_vbox_new(FALSE, 0);
311         gtk_widget_show(btn_vbox);
312         gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
313
314         btn_vbox1 = gtk_vbox_new(FALSE, 8);
315         gtk_widget_show(btn_vbox1);
316         gtk_box_pack_start(GTK_BOX(btn_vbox), btn_vbox1, TRUE, FALSE, 0);
317
318         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
319         gtk_widget_show(up_btn);
320         gtk_box_pack_start(GTK_BOX(btn_vbox1), up_btn, FALSE, FALSE, 0);
321
322         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
323         gtk_widget_show(down_btn);
324         gtk_box_pack_start(GTK_BOX(btn_vbox1), down_btn, FALSE, FALSE, 0);
325
326         g_signal_connect(G_OBJECT(up_btn), "clicked",
327                          G_CALLBACK(prefs_summary_column_up), NULL);
328         g_signal_connect(G_OBJECT(down_btn), "clicked",
329                          G_CALLBACK(prefs_summary_column_down), NULL);
330
331         btn_hbox = gtk_hbox_new(FALSE, 8);
332         gtk_widget_show(btn_hbox);
333         gtk_box_pack_end(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 0);
334
335         btn_vbox = gtk_vbox_new(FALSE, 0);
336         gtk_widget_show(btn_vbox);
337         gtk_box_pack_start(GTK_BOX(btn_hbox), btn_vbox, FALSE, FALSE, 0);
338
339         default_btn = gtk_button_new_with_label(_(" Use default "));
340         gtk_widget_show(default_btn);
341         gtk_box_pack_start(GTK_BOX(btn_vbox), default_btn, TRUE, FALSE, 0);
342         g_signal_connect(G_OBJECT(default_btn), "clicked",
343                          G_CALLBACK(prefs_summary_column_set_to_default),
344                          NULL);
345
346         gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
347                                       &cancel_btn, GTK_STOCK_CANCEL,
348                                       NULL, NULL);
349         gtk_widget_show(confirm_area);
350         gtk_box_pack_end(GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
351         gtk_widget_grab_default(ok_btn);
352
353         g_signal_connect(G_OBJECT(ok_btn), "clicked",
354                          G_CALLBACK(prefs_summary_column_ok), NULL);
355         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
356                          G_CALLBACK(prefs_summary_column_cancel), NULL);
357
358
359         summary_col.window      = window;
360         summary_col.add_btn     = add_btn;
361         summary_col.remove_btn  = remove_btn;
362         summary_col.up_btn      = up_btn;
363         summary_col.down_btn    = down_btn;
364         summary_col.ok_btn      = ok_btn;
365         summary_col.cancel_btn  = cancel_btn;
366         summary_col.stock_list_view = stock_list_view;
367         summary_col.shown_list_view = shown_list_view;
368 }
369
370 SummaryColumnState *prefs_summary_column_get_config(void)
371 {
372         static SummaryColumnState state[N_SUMMARY_COLS];
373         SummaryColumnType type;
374         gint pos;
375
376         for (pos = 0; pos < N_SUMMARY_COLS; pos++)
377                 state[pos].type = -1;
378
379         for (type = 0; type < N_SUMMARY_COLS; type++) {
380                 pos = prefs_common.summary_col_pos[type];
381                 if (pos < 0 || pos >= N_SUMMARY_COLS ||
382                     state[pos].type != -1) {
383                         g_warning("Wrong column position\n");
384                         prefs_summary_column_set_config(default_state);
385                         return default_state;
386                 }
387
388                 state[pos].type = type;
389                 state[pos].visible = prefs_common.summary_col_visible[type];
390         }
391
392         return state;
393 }
394
395 void prefs_summary_column_set_config(SummaryColumnState *state)
396 {
397         SummaryColumnType type;
398         gint pos;
399
400         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
401                 type = state[pos].type;
402                 prefs_common.summary_col_visible[type] = state[pos].visible;
403                 prefs_common.summary_col_pos[type] = pos;
404         }
405 }
406
407 static void prefs_summary_column_set_dialog(SummaryColumnState *state)
408 {
409         GtkListStore *stock_store, *shown_store;
410         gint pos;
411         SummaryColumnType type;
412         gchar *name;
413
414         stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
415                         (GTK_TREE_VIEW(summary_col.stock_list_view)));
416         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
417                         (GTK_TREE_VIEW(summary_col.shown_list_view)));
418
419         gtk_list_store_clear(stock_store);
420         gtk_list_store_clear(shown_store);
421
422         if (!state)
423                 state = prefs_summary_column_get_config();
424
425         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
426                 type = state[pos].type;
427                 name = gettext(col_name[type]);
428
429                 if (state[pos].visible)
430                         prefs_summary_column_insert_column(shown_store,
431                                                            -1, name,
432                                                            type);
433                 else
434                         prefs_summary_column_insert_column(stock_store,
435                                                             -1, name,
436                                                             type);
437         }
438 }
439
440 static void prefs_summary_column_set_view(void)
441 {
442         gint stock_n_rows, shown_n_rows;
443         SummaryColumnState state[N_SUMMARY_COLS];
444         SummaryColumnType type;
445         gint row, pos = 0;
446
447         stock_n_rows = gtk_tree_model_iter_n_children
448                 (gtk_tree_view_get_model(GTK_TREE_VIEW
449                         (summary_col.stock_list_view)), NULL);
450         shown_n_rows = gtk_tree_model_iter_n_children
451                 (gtk_tree_view_get_model(GTK_TREE_VIEW
452                         (summary_col.shown_list_view)), NULL);
453
454         g_return_if_fail
455                 (stock_n_rows + shown_n_rows == N_SUMMARY_COLS);
456
457         for (row = 0; row < stock_n_rows; row++) {
458                 type = prefs_summary_column_get_column
459                         (summary_col.stock_list_view, row);
460                 state[row].type = type;
461                 state[row].visible = FALSE;
462         }
463
464         pos = row;
465         for (row = 0; row < shown_n_rows; row++) {
466                 type = prefs_summary_column_get_column
467                         (summary_col.shown_list_view, row);
468                 state[pos + row].type = type;
469                 state[pos + row].visible = TRUE;
470         }
471
472         prefs_summary_column_set_config(state);
473         main_window_set_summary_column();
474 }
475
476 static void prefs_summary_column_add(void)
477 {
478         GtkListStore *stock_store, *shown_store;
479         GtkTreeIter stock_sel, shown_sel, shown_add;
480         gboolean shown_sel_valid;
481         gchar *name;
482         SummaryColumnType type;
483         
484         stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
485                 (GTK_TREE_VIEW(summary_col.stock_list_view)));
486         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
487                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
488         
489         if (!gtk_tree_selection_get_selected
490                 (gtk_tree_view_get_selection
491                         (GTK_TREE_VIEW(summary_col.stock_list_view)),
492                  NULL,
493                  &stock_sel))
494                 return;
495
496         shown_sel_valid = gtk_tree_selection_get_selected
497                 (gtk_tree_view_get_selection
498                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
499                  NULL,
500                  &shown_sel);
501                          
502         gtk_tree_model_get(GTK_TREE_MODEL(stock_store), &stock_sel,
503                            SUMCOL_TYPE, &type,
504                            -1);
505                         
506         gtk_list_store_remove(stock_store, &stock_sel);
507
508         gtk_list_store_insert_after(shown_store, &shown_add, 
509                                     shown_sel_valid ? &shown_sel : NULL);
510
511         name = gettext(col_name[type]);                             
512                                     
513         gtk_list_store_set(shown_store, &shown_add,
514                            SUMCOL_NAME, name,
515                            SUMCOL_TYPE, type,
516                            -1);
517         
518         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
519                 (GTK_TREE_VIEW(summary_col.shown_list_view)),
520                  &shown_add);
521 }
522
523 static void prefs_summary_column_remove(void)
524 {
525         GtkListStore *stock_store, *shown_store;
526         GtkTreeIter shown_sel, stock_sel, stock_add;
527         gboolean stock_sel_valid;
528         gchar *name;
529         SummaryColumnType type;
530         
531         stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
532                 (GTK_TREE_VIEW(summary_col.stock_list_view)));
533         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
534                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
535                 
536         if (!gtk_tree_selection_get_selected
537                 (gtk_tree_view_get_selection
538                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
539                  NULL,
540                  &shown_sel))
541                 return;
542
543         stock_sel_valid = gtk_tree_selection_get_selected
544                 (gtk_tree_view_get_selection
545                         (GTK_TREE_VIEW(summary_col.stock_list_view)),
546                  NULL,
547                  &stock_sel);
548         
549         gtk_tree_model_get(GTK_TREE_MODEL(shown_store), &shown_sel,
550                            SUMCOL_TYPE, &type,
551                            -1);
552                         
553         gtk_list_store_remove(shown_store, &shown_sel);
554
555         gtk_list_store_insert_after(stock_store, &stock_add, 
556                                     stock_sel_valid ? &stock_sel : NULL);
557
558         name = gettext(col_name[type]);                             
559                                     
560         gtk_list_store_set(stock_store, &stock_add,
561                            SUMCOL_NAME, name,
562                            SUMCOL_TYPE, type,
563                            -1);
564         
565         gtk_tree_selection_select_iter(gtk_tree_view_get_selection
566                 (GTK_TREE_VIEW(summary_col.stock_list_view)),
567                 &stock_add);
568 }
569
570 static void prefs_summary_column_up(void)
571 {
572         GtkTreePath *prev, *sel;
573         GtkTreeIter isel;
574         GtkListStore *shown_store;
575         GtkTreeIter iprev;
576         
577         if (!gtk_tree_selection_get_selected
578                 (gtk_tree_view_get_selection
579                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
580                  NULL,
581                  &isel))
582                 return;
583
584         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
585                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
586
587         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(shown_store), 
588                                       &isel);
589         if (!sel)
590                 return;
591
592         prev = gtk_tree_path_copy(sel);         
593         if (!gtk_tree_path_prev(prev)) {
594                 gtk_tree_path_free(prev);
595                 gtk_tree_path_free(sel);
596                 return;
597         }
598
599         gtk_tree_model_get_iter(GTK_TREE_MODEL(shown_store),
600                                 &iprev, prev);
601         gtk_tree_path_free(sel);
602         gtk_tree_path_free(prev);
603
604         gtk_list_store_swap(shown_store, &iprev, &isel);
605 }
606
607 static void prefs_summary_column_down(void)
608 {
609         GtkListStore *shown_store;
610         GtkTreeIter next, sel;
611         
612         if (!gtk_tree_selection_get_selected
613                 (gtk_tree_view_get_selection
614                         (GTK_TREE_VIEW(summary_col.shown_list_view)),
615                  NULL,
616                  &sel))
617                 return;
618
619         shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
620                 (GTK_TREE_VIEW(summary_col.shown_list_view)));
621
622         next = sel;
623         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(shown_store), &next)) 
624                 return;
625
626         gtk_list_store_swap(shown_store, &next, &sel);
627 }
628
629 static void prefs_summary_column_set_to_default(void)
630 {
631         prefs_summary_column_set_dialog(default_state);
632 }
633
634 static void prefs_summary_column_ok(void)
635 {
636         if (!summary_col.finished) {
637                 summary_col.finished = TRUE;
638                 prefs_summary_column_set_view();
639         }
640 }
641
642 static void prefs_summary_column_cancel(void)
643 {
644         summary_col.finished = TRUE;
645 }
646
647 static gint prefs_summary_column_delete_event(GtkWidget *widget,
648                                               GdkEventAny *event,
649                                               gpointer data)
650 {
651         summary_col.finished = TRUE;
652         return TRUE;
653 }
654
655 static gboolean prefs_summary_column_key_pressed(GtkWidget *widget,
656                                                  GdkEventKey *event,
657                                                  gpointer data)
658 {
659         if (event && event->keyval == GDK_Escape)
660                 summary_col.finished = TRUE;
661         return FALSE;
662 }
663
664 static GtkListStore *prefs_summary_column_create_store(void)
665 {
666         return gtk_list_store_new(N_SUMCOL_COLUMNS,
667                                   G_TYPE_STRING,
668                                   G_TYPE_INT,
669                                   -1);
670 }
671
672 static void prefs_summary_column_insert_column(GtkListStore *store,
673                                                gint row,
674                                                const gchar *name,
675                                                SummaryColumnType type)
676 {
677         GtkTreeIter iter;
678
679         if (row >= 0) {
680                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
681                                                    &iter, NULL, row))
682                         row = -1;                                                  
683         }
684         if (row < 0) {
685                 /* add new */
686                 gtk_list_store_append(store, &iter);
687                 gtk_list_store_set(store, &iter,
688                                    SUMCOL_NAME, name,
689                                    SUMCOL_TYPE, type,
690                                    -1);
691                 return;
692         } else {
693                 /* change existing */
694                 gtk_list_store_set(store, &iter, 
695                                    SUMCOL_NAME, name,
696                                    SUMCOL_TYPE, type,
697                                    -1);
698         }
699 }
700
701 /*!
702  *\brief        Return the columnn type for a row
703  */
704 static SummaryColumnType prefs_summary_column_get_column(GtkWidget *list, gint row)
705 {       
706         GtkTreeView *list_view = GTK_TREE_VIEW(list);
707         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
708         GtkTreeIter iter;
709         SummaryColumnType result;
710
711         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
712                 return -1;
713         
714         gtk_tree_model_get(model, &iter, 
715                            SUMCOL_TYPE, &result,
716                            -1);
717         
718         return result;
719 }
720
721 static GtkWidget *prefs_summary_column_list_view_create(const gchar *name)
722 {
723         GtkWidget *list_view;
724         GtkTreeSelection *selector;
725         GtkTreeModel *model;
726
727         model = GTK_TREE_MODEL(prefs_summary_column_create_store());
728         list_view = gtk_tree_view_new_with_model(model);
729         g_object_unref(G_OBJECT(model));
730         
731         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view),
732                                      prefs_common.enable_rules_hint);
733         
734         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
735         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
736
737         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
738
739         gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(list_view),
740                                                GDK_BUTTON1_MASK,
741                                                row_targets,
742                                                G_N_ELEMENTS(row_targets), 
743                                                GDK_ACTION_MOVE);
744                                             
745         gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(list_view), 
746                                              row_targets, 
747                                              G_N_ELEMENTS(row_targets), 
748                                              GDK_ACTION_MOVE);
749                 
750         g_signal_connect(G_OBJECT(list_view), "drag_data_get",
751                          G_CALLBACK(drag_data_get),
752                          model);
753
754         g_signal_connect(G_OBJECT(list_view), "drag_data_received",
755                          G_CALLBACK(drag_data_received),
756                          model);
757
758         return list_view;
759 }
760
761 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view, 
762                                                      const gchar *name)
763 {
764         GtkTreeViewColumn *column;
765         GtkCellRenderer *renderer;
766
767         renderer = gtk_cell_renderer_text_new();
768         column = gtk_tree_view_column_new_with_attributes
769                 (name, renderer, "text", SUMCOL_NAME, NULL);
770         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
771 }
772
773 static void drag_data_get(GtkTreeView *tree_view, GdkDragContext *context, 
774                           GtkSelectionData *data, guint info, 
775                           guint time, GtkTreeModel *model)
776 {
777         GtkTreeIter iter;
778         SummaryColumnType type;
779         GtkTreeModel *source_model;
780
781         if (info != TARGET_INFO_SUMCOL) 
782                 return;
783
784         if (!gtk_tree_selection_get_selected
785                         (gtk_tree_view_get_selection(tree_view),
786                          &source_model, &iter)) 
787                 return;                  
788         
789         gtk_tree_model_get(source_model, &iter, 
790                            SUMCOL_TYPE, &type,
791                            -1);
792
793         /* send the type */
794         gtk_selection_data_set(data, data->target, 8, (gchar *) &type, sizeof type);
795 }
796
797 static void drag_data_received(GtkTreeView *tree_view, GdkDragContext *context,
798                                gint x, gint y, GtkSelectionData *data,
799                                guint info, guint time, GtkTreeModel *model)
800 {
801         GtkWidget *source;
802         GtkTreePath *dst = NULL, *sel = NULL;
803         GtkTreeIter isel, idst;
804         GtkTreeViewDropPosition pos;
805         SummaryColumnType type;
806         GtkTreeModel *sel_model;
807         gchar *name;
808         
809         source = gtk_drag_get_source_widget(context);
810         
811         if (source == GTK_WIDGET(tree_view)) {
812         
813                 /*
814                  * Same widget: re-order
815                  */
816                  
817                 gtk_tree_selection_get_selected(gtk_tree_view_get_selection(tree_view),
818                                            NULL, &isel);
819                 sel = gtk_tree_model_get_path(model, &isel);
820                 gtk_tree_view_get_dest_row_at_pos(tree_view, x, y,
821                                                   &dst, &pos);
822
823                 /* NOTE: dst is invalid if selection beyond last row, in that
824                  * case move beyond last one (XXX_move_before(..., NULL)) */                                              
825
826                 if (dst)                                                  
827                         gtk_tree_model_get_iter(model, &idst, dst);
828                 else 
829                         gtk_list_store_move_before(GTK_LIST_STORE(model),
830                                                    &isel,
831                                                    NULL);
832
833                 /* we do not drag if no valid dst and sel, and when
834                  * dst and sel are the same (moving after or before
835                  * itself doesn't change order...) */
836                 if ((dst && sel) && gtk_tree_path_compare(sel, dst) != 0) {
837                         if (pos == GTK_TREE_VIEW_DROP_BEFORE
838                         ||  pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
839                                 gtk_list_store_move_before(GTK_LIST_STORE(model),
840                                                            &isel,
841                                                            &idst);
842                         else
843                                 gtk_list_store_move_after(GTK_LIST_STORE(model),
844                                                           &isel,
845                                                           &idst);
846                         
847                 } 
848                 gtk_tree_path_free(dst);                                          
849                 gtk_tree_path_free(sel);
850                 gtk_drag_finish(context, TRUE, FALSE, time);
851                 
852         } else if (source == summary_col.stock_list_view 
853         ||         source == summary_col.shown_list_view) {
854         
855                 /*
856                  * Other widget: change and update
857                  */
858
859                 
860                 /* get source information and remove */
861                 gtk_tree_selection_get_selected(gtk_tree_view_get_selection(
862                                                 GTK_TREE_VIEW(source)),
863                                                 &sel_model, &isel);
864                 type = *((gint *) data->data);
865                 name = gettext(col_name[type]);
866                 gtk_list_store_remove(GTK_LIST_STORE(sel_model), &isel);
867
868                 /* get insertion position */
869                 gtk_tree_view_get_dest_row_at_pos(tree_view, x, y, &dst, &pos);
870
871                 /* NOTE: dst is invalid if insertion point beyond last row, 
872                  * just append to list in that case (XXX_store_append()) */
873
874                 if (dst) {
875                         gtk_tree_model_get_iter(model, &idst, dst);
876
877                         if (pos == GTK_TREE_VIEW_DROP_BEFORE
878                         ||  pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
879                                 gtk_list_store_insert_before(GTK_LIST_STORE(model),
880                                                              &isel,
881                                                              &idst);
882                         else
883                                 gtk_list_store_insert_after(GTK_LIST_STORE(model),
884                                                             &isel,
885                                                             &idst);
886                 } else
887                         gtk_list_store_append(GTK_LIST_STORE(model),
888                                               &isel);
889                 
890                 gtk_list_store_set(GTK_LIST_STORE(model), &isel,
891                                    SUMCOL_NAME, name,
892                                    SUMCOL_TYPE, type, -1);
893                 gtk_tree_path_free(dst);
894                 gtk_drag_finish(context, TRUE, FALSE, time);
895         }
896
897         /* XXXX: should we call gtk_drag_finish() for other code paths? */
898 }
899