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