Fix bug #3574: Template addressing
[claws.git] / src / prefs_summary_column.c
blob54ebd7b045bd522357645dca62571e8a5bdae6e9
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
32 #include "prefs_gtk.h"
33 #include "prefs_common.h"
34 #include "prefs_summary_column.h"
35 #include "manage_window.h"
36 #include "summaryview.h"
37 #include "mainwindow.h"
38 #include "inc.h"
39 #include "gtkutils.h"
40 #include "utils.h"
42 static void prefs_summary_column_set_config(SummaryColumnState *state);
44 enum {
45 SUMCOL_NAME,
46 SUMCOL_TYPE,
47 N_SUMCOL_COLUMNS
50 #define TARGET_INFO_SUMCOL (0xFEEDBABE)
52 static const GtkTargetEntry row_targets[] = {
53 { "PREFS_SUM_COL_MODEL_ROW", GTK_TARGET_SAME_APP, TARGET_INFO_SUMCOL }
57 static struct _SummaryColumnDialog
59 GtkWidget *window;
61 GtkWidget *stock_list_view;
62 GtkWidget *shown_list_view;
64 GtkWidget *add_btn;
65 GtkWidget *remove_btn;
66 GtkWidget *up_btn;
67 GtkWidget *down_btn;
69 GtkWidget *default_btn;
71 GtkWidget *ok_btn;
72 GtkWidget *cancel_btn;
74 gboolean finished;
75 } summary_col;
77 static const gchar *const col_name[N_SUMMARY_COLS] = {
78 N_("Mark"), /* S_COL_MARK */
79 N_("Status"), /* S_COL_STATUS */
80 N_("Attachment"), /* S_COL_MIME */
81 N_("Subject"), /* S_COL_SUBJECT */
82 N_("From"), /* S_COL_FROM */
83 N_("To"), /* S_COL_TO */
84 N_("Date"), /* S_COL_DATE */
85 N_("Size"), /* S_COL_SIZE */
86 N_("Number"), /* S_COL_NUMBER */
87 N_("Score"), /* S_COL_SCORE */
88 N_("Locked"), /* S_COL_LOCKED */
89 N_("Tags"), /* S_COL_TAGS */
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 { S_COL_TAGS , FALSE },
107 static void prefs_summary_column_create (void);
109 static void prefs_summary_column_set_dialog (SummaryColumnState *state);
110 static void prefs_summary_column_set_view (void);
112 /* callback functions */
113 static void prefs_summary_column_add (void);
114 static void prefs_summary_column_remove (void);
116 static void prefs_summary_column_up (void);
117 static void prefs_summary_column_down (void);
119 static void prefs_summary_column_set_to_default (void);
121 static void prefs_summary_column_ok (void);
122 static void prefs_summary_column_cancel (void);
124 static gint prefs_summary_column_delete_event (GtkWidget *widget,
125 GdkEventAny *event,
126 gpointer data);
127 static gboolean prefs_summary_column_key_pressed(GtkWidget *widget,
128 GdkEventKey *event,
129 gpointer data);
131 static GtkListStore *prefs_summary_column_create_store (void);
133 static void prefs_summary_column_insert_column (GtkListStore *store,
134 gint row,
135 const gchar *name,
136 SummaryColumnType type);
138 static SummaryColumnType prefs_summary_column_get_column (GtkWidget *list,
139 gint row);
141 static GtkWidget *prefs_summary_column_list_view_create (const gchar *name);
143 static void prefs_filtering_create_list_view_columns (GtkWidget *list_view,
144 const gchar *name);
146 static void drag_data_get (GtkTreeView *tree_view,
147 GdkDragContext *context,
148 GtkSelectionData *data,
149 guint info,
150 guint time,
151 GtkTreeModel *model);
153 static void drag_data_received (GtkTreeView *tree_view,
154 GdkDragContext *context,
155 gint x, gint y,
156 GtkSelectionData *data,
157 guint info,
158 guint time,
159 GtkTreeModel *model);
161 static void prefs_summary_column_shown_set_btn_sensitivity(void);
162 static void prefs_summary_column_shown_set_active(const gboolean active);
163 static void prefs_summary_column_stock_set_active(const gboolean active);
164 static void prefs_summary_column_shown_sel_changed(void);
165 static void prefs_summary_column_stock_sel_changed(void);
167 void prefs_summary_column_open(void)
169 inc_lock();
171 if (!summary_col.window)
172 prefs_summary_column_create();
174 manage_window_set_transient(GTK_WINDOW(summary_col.window));
175 gtk_widget_grab_focus(summary_col.ok_btn);
177 prefs_summary_column_set_dialog(NULL);
179 gtk_widget_show(summary_col.window);
180 gtk_window_set_modal(GTK_WINDOW(summary_col.window), TRUE);
182 summary_col.finished = FALSE;
183 while (summary_col.finished == FALSE)
184 gtk_main_iteration();
186 gtk_widget_hide(summary_col.window);
187 gtk_window_set_modal(GTK_WINDOW(summary_col.window), FALSE);
188 inc_unlock();
191 static void prefs_summary_column_create(void)
193 GtkWidget *window;
194 GtkWidget *vbox;
196 GtkWidget *label_hbox;
197 GtkWidget *label;
199 GtkWidget *vbox1;
201 GtkWidget *hbox1;
202 GtkWidget *clist_hbox;
203 GtkWidget *scrolledwin;
204 GtkWidget *stock_list_view;
205 GtkWidget *shown_list_view;
207 GtkWidget *btn_vbox;
208 GtkWidget *add_btn;
209 GtkWidget *remove_btn;
210 GtkWidget *up_btn;
211 GtkWidget *down_btn;
213 GtkWidget *btn_hbox;
214 GtkWidget *default_btn;
215 GtkWidget *confirm_area;
216 GtkWidget *ok_btn;
217 GtkWidget *cancel_btn;
219 debug_print("Creating summary column setting window...\n");
221 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_summary_column");
222 gtk_container_set_border_width(GTK_CONTAINER(window), 8);
223 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
224 gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
225 gtk_window_set_title(GTK_WINDOW(window),
226 _("Message list columns configuration"));
227 g_signal_connect(G_OBJECT(window), "delete_event",
228 G_CALLBACK(prefs_summary_column_delete_event),
229 NULL);
230 g_signal_connect(G_OBJECT(window), "key_press_event",
231 G_CALLBACK(prefs_summary_column_key_pressed),
232 NULL);
234 vbox = gtk_vbox_new(FALSE, 6);
235 gtk_widget_show(vbox);
236 gtk_container_add(GTK_CONTAINER(window), vbox);
238 label_hbox = gtk_hbox_new(FALSE, 0);
239 gtk_widget_show(label_hbox);
240 gtk_box_pack_start(GTK_BOX(vbox), label_hbox, FALSE, FALSE, 4);
242 label = gtk_label_new
243 (_("Select columns to be displayed in the message list. You can modify\n"
244 "the order by using the Up / Down buttons or by dragging the items."));
245 gtk_widget_show(label);
246 gtk_box_pack_start(GTK_BOX(label_hbox), label, FALSE, FALSE, 4);
247 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
249 vbox1 = gtk_vbox_new(FALSE, VSPACING);
250 gtk_widget_show(vbox1);
251 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
252 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
254 hbox1 = gtk_hbox_new(FALSE, 8);
255 gtk_widget_show(hbox1);
256 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
258 clist_hbox = gtk_hbox_new(FALSE, 8);
259 gtk_widget_show(clist_hbox);
260 gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
262 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
263 gtk_widget_set_size_request(scrolledwin, 180, 210);
264 gtk_widget_show(scrolledwin);
265 gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
266 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
267 GTK_POLICY_AUTOMATIC,
268 GTK_POLICY_AUTOMATIC);
271 stock_list_view = prefs_summary_column_list_view_create
272 (_("Hidden columns"));
273 g_signal_connect(G_OBJECT(stock_list_view), "cursor-changed",
274 G_CALLBACK(prefs_summary_column_stock_sel_changed),
275 NULL);
276 gtk_widget_show(stock_list_view);
277 gtk_container_add(GTK_CONTAINER(scrolledwin), stock_list_view);
279 /* add/remove button */
280 btn_vbox = gtk_vbox_new(FALSE, 8);
281 gtk_widget_show(btn_vbox);
282 gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
284 add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
285 gtk_widget_show(add_btn);
286 gtk_box_pack_start(GTK_BOX(btn_vbox), add_btn, FALSE, TRUE, 0);
288 g_signal_connect(G_OBJECT(add_btn), "clicked",
289 G_CALLBACK(prefs_summary_column_add), NULL);
291 clist_hbox = gtk_hbox_new(FALSE, 8);
292 gtk_widget_show(clist_hbox);
293 gtk_box_pack_start(GTK_BOX(hbox1), clist_hbox, TRUE, TRUE, 0);
295 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
296 gtk_widget_set_size_request(scrolledwin, 180, 210);
297 gtk_widget_show(scrolledwin);
298 gtk_box_pack_start(GTK_BOX(clist_hbox), scrolledwin, TRUE, TRUE, 0);
299 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
300 GTK_POLICY_AUTOMATIC,
301 GTK_POLICY_AUTOMATIC);
303 shown_list_view = prefs_summary_column_list_view_create
304 (_("Displayed columns"));
305 g_signal_connect(G_OBJECT(shown_list_view), "cursor-changed",
306 G_CALLBACK(prefs_summary_column_shown_sel_changed),
307 NULL);
308 gtk_widget_show(shown_list_view);
309 gtk_container_add(GTK_CONTAINER(scrolledwin), shown_list_view);
311 /* up/down button */
312 btn_vbox = gtk_vbox_new(FALSE, 8);
313 gtk_widget_show(btn_vbox);
314 gtk_box_pack_start(GTK_BOX(hbox1), btn_vbox, FALSE, FALSE, 0);
316 remove_btn = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
317 gtk_widget_show(remove_btn);
318 gtk_box_pack_start(GTK_BOX(btn_vbox), remove_btn, FALSE, TRUE, 0);
320 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
321 gtk_widget_show(up_btn);
322 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, TRUE, 0);
324 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
325 gtk_widget_show(down_btn);
326 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, TRUE, 0);
328 g_signal_connect(G_OBJECT(remove_btn), "clicked",
329 G_CALLBACK(prefs_summary_column_remove), NULL);
330 g_signal_connect(G_OBJECT(up_btn), "clicked",
331 G_CALLBACK(prefs_summary_column_up), NULL);
332 g_signal_connect(G_OBJECT(down_btn), "clicked",
333 G_CALLBACK(prefs_summary_column_down), NULL);
335 btn_hbox = gtk_hbox_new(FALSE, 8);
336 gtk_widget_show(btn_hbox);
337 gtk_box_pack_end(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 0);
339 btn_vbox = gtk_vbox_new(FALSE, 0);
340 gtk_widget_show(btn_vbox);
341 gtk_box_pack_start(GTK_BOX(btn_hbox), btn_vbox, FALSE, FALSE, 0);
343 default_btn = gtk_button_new_with_label(_(" Use default "));
344 gtk_widget_show(default_btn);
345 gtk_box_pack_start(GTK_BOX(btn_vbox), default_btn, TRUE, FALSE, 0);
346 g_signal_connect(G_OBJECT(default_btn), "clicked",
347 G_CALLBACK(prefs_summary_column_set_to_default),
348 NULL);
350 gtkut_stock_button_set_create(&confirm_area,
351 &cancel_btn, GTK_STOCK_CANCEL,
352 &ok_btn, GTK_STOCK_OK,
353 NULL, NULL);
354 gtk_widget_show(confirm_area);
355 gtk_box_pack_end(GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
356 gtk_widget_grab_default(ok_btn);
358 g_signal_connect(G_OBJECT(ok_btn), "clicked",
359 G_CALLBACK(prefs_summary_column_ok), NULL);
360 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
361 G_CALLBACK(prefs_summary_column_cancel), NULL);
364 summary_col.window = window;
365 summary_col.add_btn = add_btn;
366 summary_col.remove_btn = remove_btn;
367 summary_col.up_btn = up_btn;
368 summary_col.down_btn = down_btn;
369 summary_col.ok_btn = ok_btn;
370 summary_col.cancel_btn = cancel_btn;
371 summary_col.stock_list_view = stock_list_view;
372 summary_col.shown_list_view = shown_list_view;
374 prefs_summary_column_shown_set_active(FALSE);
375 prefs_summary_column_stock_set_active(FALSE);
378 SummaryColumnState *prefs_summary_column_get_config(void)
380 static SummaryColumnState state[N_SUMMARY_COLS];
381 SummaryColumnType type;
382 gint pos;
384 for (pos = 0; pos < N_SUMMARY_COLS; pos++)
385 state[pos].type = -1;
387 for (type = 0; type < N_SUMMARY_COLS; type++) {
388 pos = prefs_common.summary_col_pos[type];
389 if (pos < 0 || pos >= N_SUMMARY_COLS ||
390 state[pos].type != -1) {
391 g_warning("Wrong column position");
392 prefs_summary_column_set_config(default_state);
393 return default_state;
396 state[pos].type = type;
397 state[pos].visible = prefs_common.summary_col_visible[type];
400 return state;
403 static void prefs_summary_column_set_config(SummaryColumnState *state)
405 SummaryColumnType type;
406 gint pos;
408 for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
409 type = state[pos].type;
410 prefs_common.summary_col_visible[type] = state[pos].visible;
411 prefs_common.summary_col_pos[type] = pos;
415 static void prefs_summary_column_set_dialog(SummaryColumnState *state)
417 GtkListStore *stock_store, *shown_store;
418 gint pos;
419 SummaryColumnType type;
420 gchar *name;
422 stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
423 (GTK_TREE_VIEW(summary_col.stock_list_view)));
424 shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
425 (GTK_TREE_VIEW(summary_col.shown_list_view)));
427 gtk_list_store_clear(stock_store);
428 gtk_list_store_clear(shown_store);
430 if (!state)
431 state = prefs_summary_column_get_config();
433 for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
434 type = state[pos].type;
435 name = gettext(col_name[type]);
437 if (state[pos].visible)
438 prefs_summary_column_insert_column(shown_store,
439 -1, name,
440 type);
441 else
442 prefs_summary_column_insert_column(stock_store,
443 -1, name,
444 type);
448 static void prefs_summary_column_set_view(void)
450 gint stock_n_rows, shown_n_rows;
451 SummaryColumnState state[N_SUMMARY_COLS];
452 SummaryColumnType type;
453 gint row, pos = 0;
455 stock_n_rows = gtk_tree_model_iter_n_children
456 (gtk_tree_view_get_model(GTK_TREE_VIEW
457 (summary_col.stock_list_view)), NULL);
458 shown_n_rows = gtk_tree_model_iter_n_children
459 (gtk_tree_view_get_model(GTK_TREE_VIEW
460 (summary_col.shown_list_view)), NULL);
462 cm_return_if_fail
463 (stock_n_rows + shown_n_rows == N_SUMMARY_COLS);
465 for (row = 0; row < stock_n_rows; row++) {
466 type = prefs_summary_column_get_column
467 (summary_col.stock_list_view, row);
468 state[row].type = type;
469 state[row].visible = FALSE;
472 pos = row;
473 for (row = 0; row < shown_n_rows; row++) {
474 type = prefs_summary_column_get_column
475 (summary_col.shown_list_view, row);
476 state[pos + row].type = type;
477 state[pos + row].visible = TRUE;
480 prefs_summary_column_set_config(state);
481 main_window_set_summary_column();
484 static void prefs_summary_column_add(void)
486 GtkListStore *stock_store, *shown_store;
487 GtkTreeIter stock_sel, shown_sel, shown_add;
488 gboolean shown_sel_valid;
489 gchar *name;
490 SummaryColumnType type;
492 stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
493 (GTK_TREE_VIEW(summary_col.stock_list_view)));
494 shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
495 (GTK_TREE_VIEW(summary_col.shown_list_view)));
497 if (!gtk_tree_selection_get_selected
498 (gtk_tree_view_get_selection
499 (GTK_TREE_VIEW(summary_col.stock_list_view)),
500 NULL,
501 &stock_sel))
502 return;
504 shown_sel_valid = gtk_tree_selection_get_selected
505 (gtk_tree_view_get_selection
506 (GTK_TREE_VIEW(summary_col.shown_list_view)),
507 NULL,
508 &shown_sel);
510 gtk_tree_model_get(GTK_TREE_MODEL(stock_store), &stock_sel,
511 SUMCOL_TYPE, &type,
512 -1);
514 gtk_list_store_remove(stock_store, &stock_sel);
516 gtk_list_store_insert_after(shown_store, &shown_add,
517 shown_sel_valid ? &shown_sel : NULL);
519 name = gettext(col_name[type]);
521 gtk_list_store_set(shown_store, &shown_add,
522 SUMCOL_NAME, name,
523 SUMCOL_TYPE, type,
524 -1);
526 gtk_tree_selection_select_iter(gtk_tree_view_get_selection
527 (GTK_TREE_VIEW(summary_col.shown_list_view)),
528 &shown_add);
529 prefs_summary_column_shown_set_active(TRUE);
530 prefs_summary_column_stock_set_active(FALSE);
533 static void prefs_summary_column_remove(void)
535 GtkListStore *stock_store, *shown_store;
536 GtkTreeIter shown_sel, stock_sel, stock_add;
537 gboolean stock_sel_valid;
538 gchar *name;
539 SummaryColumnType type;
541 stock_store = GTK_LIST_STORE(gtk_tree_view_get_model
542 (GTK_TREE_VIEW(summary_col.stock_list_view)));
543 shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
544 (GTK_TREE_VIEW(summary_col.shown_list_view)));
546 if (!gtk_tree_selection_get_selected
547 (gtk_tree_view_get_selection
548 (GTK_TREE_VIEW(summary_col.shown_list_view)),
549 NULL,
550 &shown_sel))
551 return;
553 stock_sel_valid = gtk_tree_selection_get_selected
554 (gtk_tree_view_get_selection
555 (GTK_TREE_VIEW(summary_col.stock_list_view)),
556 NULL,
557 &stock_sel);
559 gtk_tree_model_get(GTK_TREE_MODEL(shown_store), &shown_sel,
560 SUMCOL_TYPE, &type,
561 -1);
563 gtk_list_store_remove(shown_store, &shown_sel);
565 gtk_list_store_insert_after(stock_store, &stock_add,
566 stock_sel_valid ? &stock_sel : NULL);
568 name = gettext(col_name[type]);
570 gtk_list_store_set(stock_store, &stock_add,
571 SUMCOL_NAME, name,
572 SUMCOL_TYPE, type,
573 -1);
575 gtk_tree_selection_select_iter(gtk_tree_view_get_selection
576 (GTK_TREE_VIEW(summary_col.stock_list_view)),
577 &stock_add);
578 prefs_summary_column_shown_set_active(FALSE);
579 prefs_summary_column_stock_set_active(TRUE);
582 static void prefs_summary_column_up(void)
584 GtkTreePath *prev, *sel;
585 GtkTreeIter isel;
586 GtkListStore *shown_store;
587 GtkTreeIter iprev;
589 if (!gtk_tree_selection_get_selected
590 (gtk_tree_view_get_selection
591 (GTK_TREE_VIEW(summary_col.shown_list_view)),
592 NULL,
593 &isel))
594 return;
596 shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
597 (GTK_TREE_VIEW(summary_col.shown_list_view)));
599 sel = gtk_tree_model_get_path(GTK_TREE_MODEL(shown_store),
600 &isel);
601 if (!sel)
602 return;
604 prev = gtk_tree_path_copy(sel);
605 if (!gtk_tree_path_prev(prev)) {
606 gtk_tree_path_free(prev);
607 gtk_tree_path_free(sel);
608 return;
611 gtk_tree_model_get_iter(GTK_TREE_MODEL(shown_store),
612 &iprev, prev);
613 gtk_tree_path_free(sel);
614 gtk_tree_path_free(prev);
616 gtk_list_store_swap(shown_store, &iprev, &isel);
617 prefs_summary_column_shown_set_btn_sensitivity();
620 static void prefs_summary_column_down(void)
622 GtkListStore *shown_store;
623 GtkTreeIter next, sel;
625 if (!gtk_tree_selection_get_selected
626 (gtk_tree_view_get_selection
627 (GTK_TREE_VIEW(summary_col.shown_list_view)),
628 NULL,
629 &sel))
630 return;
632 shown_store = GTK_LIST_STORE(gtk_tree_view_get_model
633 (GTK_TREE_VIEW(summary_col.shown_list_view)));
635 next = sel;
636 if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(shown_store), &next))
637 return;
639 gtk_list_store_swap(shown_store, &next, &sel);
640 prefs_summary_column_shown_set_btn_sensitivity();
643 static void prefs_summary_column_set_to_default(void)
645 prefs_summary_column_set_dialog(default_state);
646 prefs_summary_column_shown_set_active(FALSE);
647 prefs_summary_column_stock_set_active(FALSE);
650 static void prefs_summary_column_ok(void)
652 if (!summary_col.finished) {
653 summary_col.finished = TRUE;
654 prefs_summary_column_set_view();
658 static void prefs_summary_column_cancel(void)
660 summary_col.finished = TRUE;
663 static gint prefs_summary_column_delete_event(GtkWidget *widget,
664 GdkEventAny *event,
665 gpointer data)
667 summary_col.finished = TRUE;
668 return TRUE;
671 static gboolean prefs_summary_column_key_pressed(GtkWidget *widget,
672 GdkEventKey *event,
673 gpointer data)
675 if (event && event->keyval == GDK_KEY_Escape)
676 summary_col.finished = TRUE;
677 return FALSE;
680 static GtkListStore *prefs_summary_column_create_store(void)
682 return gtk_list_store_new(N_SUMCOL_COLUMNS,
683 G_TYPE_STRING,
684 G_TYPE_INT,
685 -1);
688 static void prefs_summary_column_insert_column(GtkListStore *store,
689 gint row,
690 const gchar *name,
691 SummaryColumnType type)
693 GtkTreeIter iter;
695 if (row >= 0) {
696 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
697 &iter, NULL, row))
698 row = -1;
700 if (row < 0) {
701 /* add new */
702 gtk_list_store_append(store, &iter);
703 gtk_list_store_set(store, &iter,
704 SUMCOL_NAME, name,
705 SUMCOL_TYPE, type,
706 -1);
707 return;
708 } else {
709 /* change existing */
710 gtk_list_store_set(store, &iter,
711 SUMCOL_NAME, name,
712 SUMCOL_TYPE, type,
713 -1);
718 *\brief Return the columnn type for a row
720 static SummaryColumnType prefs_summary_column_get_column(GtkWidget *list, gint row)
722 GtkTreeView *list_view = GTK_TREE_VIEW(list);
723 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
724 GtkTreeIter iter;
725 SummaryColumnType result;
727 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
728 return -1;
730 gtk_tree_model_get(model, &iter,
731 SUMCOL_TYPE, &result,
732 -1);
734 return result;
737 static GtkWidget *prefs_summary_column_list_view_create(const gchar *name)
739 GtkWidget *list_view;
740 GtkTreeSelection *selector;
741 GtkTreeModel *model;
743 model = GTK_TREE_MODEL(prefs_summary_column_create_store());
744 list_view = gtk_tree_view_new_with_model(model);
745 g_object_unref(G_OBJECT(model));
747 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view),
748 prefs_common.use_stripes_everywhere);
750 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
751 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
753 prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
755 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(list_view),
756 GDK_BUTTON1_MASK,
757 row_targets,
758 G_N_ELEMENTS(row_targets),
759 GDK_ACTION_MOVE);
761 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(list_view),
762 row_targets,
763 G_N_ELEMENTS(row_targets),
764 GDK_ACTION_MOVE);
766 g_signal_connect(G_OBJECT(list_view), "drag_data_get",
767 G_CALLBACK(drag_data_get),
768 model);
770 g_signal_connect(G_OBJECT(list_view), "drag_data_received",
771 G_CALLBACK(drag_data_received),
772 model);
774 return list_view;
777 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view,
778 const gchar *name)
780 GtkTreeViewColumn *column;
781 GtkCellRenderer *renderer;
783 renderer = gtk_cell_renderer_text_new();
784 column = gtk_tree_view_column_new_with_attributes
785 (name, renderer, "text", SUMCOL_NAME, NULL);
786 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
789 static void drag_data_get(GtkTreeView *tree_view, GdkDragContext *context,
790 GtkSelectionData *data, guint info,
791 guint time, GtkTreeModel *model)
793 GtkTreeIter iter;
794 SummaryColumnType type;
795 GtkTreeModel *source_model;
797 if (info != TARGET_INFO_SUMCOL)
798 return;
800 if (!gtk_tree_selection_get_selected
801 (gtk_tree_view_get_selection(tree_view),
802 &source_model, &iter))
803 return;
805 gtk_tree_model_get(source_model, &iter,
806 SUMCOL_TYPE, &type,
807 -1);
809 /* send the type */
810 gtk_selection_data_set(data, gtk_selection_data_get_target(data), 8,
811 (gchar *) &type, sizeof type);
814 static void drag_data_received(GtkTreeView *tree_view, GdkDragContext *context,
815 gint x, gint y, GtkSelectionData *data,
816 guint info, guint time, GtkTreeModel *model)
818 GtkWidget *source;
819 GtkTreePath *dst = NULL, *sel = NULL;
820 GtkTreeIter isel, idst;
821 GtkTreeViewDropPosition pos;
822 SummaryColumnType type;
823 GtkTreeModel *sel_model;
824 gchar *name;
826 source = gtk_drag_get_source_widget(context);
828 if (source == GTK_WIDGET(tree_view)) {
831 * Same widget: re-order
834 gtk_tree_selection_get_selected(gtk_tree_view_get_selection(tree_view),
835 NULL, &isel);
836 sel = gtk_tree_model_get_path(model, &isel);
837 gtk_tree_view_get_dest_row_at_pos(tree_view, x, y,
838 &dst, &pos);
840 /* NOTE: dst is invalid if selection beyond last row, in that
841 * case move beyond last one (XXX_move_before(..., NULL)) */
843 if (dst)
844 gtk_tree_model_get_iter(model, &idst, dst);
845 else
846 gtk_list_store_move_before(GTK_LIST_STORE(model),
847 &isel,
848 NULL);
850 /* we do not drag if no valid dst and sel, and when
851 * dst and sel are the same (moving after or before
852 * itself doesn't change order...) */
853 if ((dst && sel) && gtk_tree_path_compare(sel, dst) != 0) {
854 if (pos == GTK_TREE_VIEW_DROP_BEFORE
855 || pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
856 gtk_list_store_move_before(GTK_LIST_STORE(model),
857 &isel,
858 &idst);
859 else
860 gtk_list_store_move_after(GTK_LIST_STORE(model),
861 &isel,
862 &idst);
865 gtk_tree_path_free(dst);
866 gtk_tree_path_free(sel);
867 gtk_drag_finish(context, TRUE, FALSE, time);
869 } else if (source == summary_col.stock_list_view
870 || source == summary_col.shown_list_view) {
873 * Other widget: change and update
877 /* get source information and remove */
878 gtk_tree_selection_get_selected(gtk_tree_view_get_selection(
879 GTK_TREE_VIEW(source)),
880 &sel_model, &isel);
881 type = *((gint *) gtk_selection_data_get_data(data));
882 name = gettext(col_name[type]);
883 gtk_list_store_remove(GTK_LIST_STORE(sel_model), &isel);
885 /* get insertion position */
886 gtk_tree_view_get_dest_row_at_pos(tree_view, x, y, &dst, &pos);
888 /* NOTE: dst is invalid if insertion point beyond last row,
889 * just append to list in that case (XXX_store_append()) */
891 if (dst) {
892 gtk_tree_model_get_iter(model, &idst, dst);
894 if (pos == GTK_TREE_VIEW_DROP_BEFORE
895 || pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
896 gtk_list_store_insert_before(GTK_LIST_STORE(model),
897 &isel,
898 &idst);
899 else
900 gtk_list_store_insert_after(GTK_LIST_STORE(model),
901 &isel,
902 &idst);
903 } else
904 gtk_list_store_append(GTK_LIST_STORE(model),
905 &isel);
907 gtk_list_store_set(GTK_LIST_STORE(model), &isel,
908 SUMCOL_NAME, name,
909 SUMCOL_TYPE, type, -1);
910 gtk_tree_path_free(dst);
911 gtk_drag_finish(context, TRUE, FALSE, time);
914 prefs_summary_column_shown_set_active(FALSE);
915 prefs_summary_column_stock_set_active(FALSE);
917 /* XXXX: should we call gtk_drag_finish() for other code paths? */
920 static void prefs_summary_column_shown_set_btn_sensitivity(void)
922 GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(
923 GTK_TREE_VIEW(summary_col.shown_list_view)));
924 GtkTreeSelection *selection = gtk_tree_view_get_selection(
925 GTK_TREE_VIEW(summary_col.shown_list_view));
926 GtkTreeIter iter;
927 GtkTreePath *path;
929 if(!gtk_tree_selection_get_selected(selection, NULL, &iter)) {
930 gtk_widget_set_sensitive(summary_col.remove_btn, FALSE);
931 gtk_widget_set_sensitive(summary_col.up_btn, FALSE);
932 gtk_widget_set_sensitive(summary_col.down_btn, FALSE);
933 return;
936 path = gtk_tree_model_get_path(model, &iter);
938 gtk_widget_set_sensitive(summary_col.up_btn, gtk_tree_path_prev(path));
939 gtk_widget_set_sensitive(summary_col.down_btn,
940 gtk_tree_model_iter_next(model, &iter));
941 gtk_tree_path_free(path);
944 static void prefs_summary_column_shown_set_active(const gboolean active)
946 GtkTreeSelection *selection = NULL;
948 gtk_widget_set_sensitive(summary_col.remove_btn, active);
950 if(active == FALSE) {
951 selection = gtk_tree_view_get_selection(
952 GTK_TREE_VIEW(summary_col.shown_list_view));
953 gtk_tree_selection_unselect_all(selection);
955 gtk_widget_set_sensitive(summary_col.up_btn, FALSE);
956 gtk_widget_set_sensitive(summary_col.down_btn, FALSE);
957 } else {
958 prefs_summary_column_shown_set_btn_sensitivity();
962 static void prefs_summary_column_stock_set_active(const gboolean active)
964 GtkTreeSelection *selection = NULL;
966 gtk_widget_set_sensitive(summary_col.add_btn, active);
968 if(active == FALSE) {
969 selection = gtk_tree_view_get_selection(
970 GTK_TREE_VIEW(summary_col.stock_list_view));
971 gtk_tree_selection_unselect_all(selection);
975 static void prefs_summary_column_stock_sel_changed(void)
977 GtkTreeSelection *selection = gtk_tree_view_get_selection(
978 GTK_TREE_VIEW(summary_col.stock_list_view));
979 prefs_summary_column_stock_set_active(
980 (selection != NULL) ? TRUE : FALSE);
981 prefs_summary_column_shown_set_active(
982 (selection != NULL) ? FALSE : TRUE);
985 static void prefs_summary_column_shown_sel_changed(void)
987 GtkTreeSelection *selection = gtk_tree_view_get_selection(
988 GTK_TREE_VIEW(summary_col.shown_list_view));
989 prefs_summary_column_shown_set_active(
990 (selection != NULL) ? TRUE : FALSE);
991 prefs_summary_column_stock_set_active(
992 (selection != NULL) ? FALSE : TRUE);