Fix bug #3574: Template addressing
[claws.git] / src / prefs_display_header.c
blob9613bdb0524475204577debfee0675c44f48ab8d
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>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
36 #include "prefs_gtk.h"
37 #include "prefs_display_header.h"
38 #include "prefs_common.h"
39 #include "manage_window.h"
40 #include "alertpanel.h"
41 #include "displayheader.h"
42 #include "utils.h"
43 #include "gtkutils.h"
45 enum {
46 PREFS_HDR_HEADER,
47 PREFS_HDR_DATA,
48 N_PREFS_HDR_COLUMNS
51 static struct DisplayHeader {
52 GtkWidget *window;
54 GtkWidget *ok_btn;
55 GtkWidget *cancel_btn;
57 GtkWidget *hdr_combo;
58 GtkWidget *key_check;
59 GtkWidget *headers_list_view;
60 GtkWidget *hidden_headers_list_view;
62 GtkWidget *other_headers;
63 } dispheader;
65 /* widget creating functions */
66 static void prefs_display_header_create (void);
68 static void prefs_display_header_set_dialog (void);
69 static void prefs_display_header_set_list (void);
70 static void prefs_display_header_list_view_set_row (gboolean hidden);
72 /* callback functions */
73 static void prefs_display_header_register_cb (GtkButton *btn,
74 gpointer hidden_data);
75 static void prefs_display_header_delete_cb (GtkButton *btn,
76 gpointer list_view_data);
77 static void prefs_display_header_up (void);
78 static void prefs_display_header_down (void);
80 static gboolean prefs_display_header_key_pressed (GtkWidget *widget,
81 GdkEventKey *event,
82 gpointer data);
83 static void prefs_display_header_ok (void);
84 static void prefs_display_header_cancel (void);
85 static gint prefs_display_header_deleted (GtkWidget *widget,
86 GdkEventAny *event,
87 gpointer data);
90 static GtkListStore *prefs_display_header_create_store (void);
91 static void prefs_display_header_insert_header (GtkListStore *store,
92 gchar *name,
93 DisplayHeaderProp *dp);
94 static GtkWidget *prefs_display_header_list_view_create (const gchar *name);
95 static void prefs_filtering_create_list_view_columns (GtkWidget *list_view,
96 const gchar *name);
97 static void headers_list_model_rows_reordered (GtkTreeModel *model,
98 GtkTreePath *path,
99 GtkTreeIter *iter,
100 gpointer arg,
101 GtkTreeView *list_view);
103 static void drag_end (GtkTreeView *list_view,
104 GdkDragContext *context,
105 gpointer data);
107 #ifndef GENERIC_UMPC
108 static gchar *defaults[] =
110 "From",
111 "To",
112 "Cc",
113 "Subject",
114 "Date",
115 "Reply-To",
116 "Sender",
117 "User-Agent",
118 "X-Mailer",
119 "Newsgroups",
120 "Followup-To",
121 "Organization",
122 "X-Newsreader",
123 "-Received",
124 "-Message-ID",
125 "-In-Reply-To",
126 "-References",
127 "-MIME-Version",
128 "-Content-Type",
129 "-Content-Transfer-Encoding",
130 "-X-UIDL",
131 "-Precedence",
132 "-Status",
133 "-Priority",
134 "-X-Face"
136 #else
137 static gchar *defaults[] =
139 "From",
140 "To",
141 "Cc",
142 "Subject",
143 "Date",
144 "Newsgroups",
145 "Followup-To",
146 "-Reply-To",
147 "-Sender",
148 "-User-Agent",
149 "-X-Mailer",
150 "-Organization",
151 "-X-Newsreader",
152 "-Received",
153 "-Message-ID",
154 "-In-Reply-To",
155 "-References",
156 "-MIME-Version",
157 "-Content-Type",
158 "-Content-Transfer-Encoding",
159 "-X-UIDL",
160 "-Precedence",
161 "-Status",
162 "-Priority",
163 "-X-Face"
165 #endif
166 static void prefs_display_header_set_default(void)
168 gint i;
169 DisplayHeaderProp *dp;
171 for(i = 0; i < sizeof(defaults) / sizeof(defaults[0]); i++) {
172 dp = display_header_prop_read_str(defaults[i]);
173 prefs_common.disphdr_list =
174 g_slist_append(prefs_common.disphdr_list, dp);
178 void prefs_display_header_open(void)
180 if (prefs_rc_is_readonly(DISPLAY_HEADER_RC))
181 return;
183 if (!dispheader.window) {
184 prefs_display_header_create();
187 manage_window_set_transient(GTK_WINDOW(dispheader.window));
188 gtk_widget_grab_focus(dispheader.ok_btn);
190 prefs_display_header_set_dialog();
192 gtk_widget_show(dispheader.window);
193 gtk_window_set_modal(GTK_WINDOW(dispheader.window), TRUE);
196 static void prefs_display_header_create(void)
198 GtkWidget *window;
199 GtkWidget *vbox;
200 GtkWidget *btn_hbox;
201 GtkWidget *ok_btn;
202 GtkWidget *cancel_btn;
203 GtkWidget *confirm_area;
205 GtkWidget *vbox1;
207 GtkWidget *hbox1;
208 GtkWidget *hdr_label;
209 GtkWidget *hdr_combo;
211 GtkWidget *btn_vbox;
212 GtkWidget *reg_btn;
213 GtkWidget *del_btn;
214 GtkWidget *up_btn;
215 GtkWidget *down_btn;
217 GtkWidget *list_view_hbox;
218 GtkWidget *list_view_hbox1;
219 GtkWidget *list_view_hbox2;
220 GtkWidget *list_view_scrolledwin;
221 GtkWidget *headers_list_view;
222 GtkWidget *hidden_headers_list_view;
224 GtkWidget *checkbtn_other_headers;
225 gint i;
227 debug_print("Creating display header setting window...\n");
229 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_display_header");
230 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
231 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
232 gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
234 vbox = gtk_vbox_new (FALSE, 6);
235 gtk_widget_show (vbox);
236 gtk_container_add (GTK_CONTAINER (window), vbox);
238 btn_hbox = gtk_hbox_new (FALSE, 8);
239 gtk_widget_show (btn_hbox);
240 gtk_box_pack_end (GTK_BOX (vbox), btn_hbox, FALSE, FALSE, 0);
242 gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
243 &ok_btn, GTK_STOCK_OK,
244 NULL, NULL);
245 gtk_widget_show (confirm_area);
246 gtk_box_pack_end (GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
247 gtk_widget_grab_default (ok_btn);
249 gtk_window_set_title (GTK_WINDOW(window),
250 _("Displayed header configuration"));
251 MANAGE_WINDOW_SIGNALS_CONNECT(window);
252 g_signal_connect (G_OBJECT(window), "delete_event",
253 G_CALLBACK(prefs_display_header_deleted),
254 NULL);
255 g_signal_connect (G_OBJECT(window), "key_press_event",
256 G_CALLBACK(prefs_display_header_key_pressed),
257 NULL);
258 g_signal_connect (G_OBJECT(ok_btn), "clicked",
259 G_CALLBACK(prefs_display_header_ok),
260 NULL);
261 g_signal_connect (G_OBJECT(cancel_btn), "clicked",
262 G_CALLBACK(prefs_display_header_cancel),
263 NULL);
265 vbox1 = gtk_vbox_new (FALSE, VSPACING);
266 gtk_widget_show (vbox1);
267 gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
268 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
270 hbox1 = gtk_hbox_new (FALSE, 8);
271 gtk_widget_show (hbox1);
272 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, TRUE, 0);
274 hdr_label = gtk_label_new (_("Header name"));
275 gtk_widget_show (hdr_label);
276 gtk_box_pack_start (GTK_BOX (hbox1), hdr_label, FALSE, FALSE, 0);
278 #if !GTK_CHECK_VERSION(2, 24, 0)
279 hdr_combo = gtk_combo_box_entry_new_text();
280 #else
281 hdr_combo = gtk_combo_box_text_new_with_entry();
282 #endif
283 for(i=0; i < 9 ; i++)
284 #if !GTK_CHECK_VERSION(2, 24, 0)
285 gtk_combo_box_append_text(GTK_COMBO_BOX (hdr_combo),
286 #else
287 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (hdr_combo),
288 #endif
289 (*defaults[i] == '-') ? defaults[i]+1 : defaults[i]);
290 gtk_combo_box_set_active(GTK_COMBO_BOX(hdr_combo), 0);
291 gtk_widget_show (hdr_combo);
292 gtk_box_pack_start (GTK_BOX (hbox1), hdr_combo, TRUE, TRUE, 0);
293 gtk_widget_set_size_request (hdr_combo, 150, -1);
295 list_view_hbox = gtk_hbox_new (FALSE, 10);
296 gtk_widget_show (list_view_hbox);
297 gtk_box_pack_start (GTK_BOX (vbox1), list_view_hbox, TRUE, TRUE, 0);
299 /* display headers list */
301 list_view_hbox1 = gtk_hbox_new (FALSE, 8);
302 gtk_widget_show (list_view_hbox1);
303 gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox1, TRUE, TRUE, 0);
305 list_view_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
306 gtk_widget_set_size_request (list_view_scrolledwin, 200, 210);
307 gtk_widget_show (list_view_scrolledwin);
308 gtk_box_pack_start (GTK_BOX (list_view_hbox1), list_view_scrolledwin,
309 TRUE, TRUE, 0);
310 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (list_view_scrolledwin),
311 GTK_POLICY_AUTOMATIC,
312 GTK_POLICY_AUTOMATIC);
313 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_view_scrolledwin),
314 GTK_SHADOW_IN);
316 headers_list_view = prefs_display_header_list_view_create
317 (_("Displayed Headers"));
318 gtk_widget_show (headers_list_view);
319 gtk_container_add(GTK_CONTAINER(list_view_scrolledwin), headers_list_view);
320 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(headers_list_view), TRUE);
322 g_signal_connect(G_OBJECT(headers_list_view), "drag_end",
323 G_CALLBACK(drag_end),
324 headers_list_view);
326 /* connect rows change for this list view's model */
327 g_signal_connect(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(headers_list_view))),
328 "rows-reordered",
329 G_CALLBACK(headers_list_model_rows_reordered),
330 headers_list_view);
332 btn_vbox = gtk_vbox_new (FALSE, 8);
333 gtk_widget_show (btn_vbox);
334 gtk_box_pack_start (GTK_BOX (list_view_hbox1), btn_vbox, FALSE, FALSE, 0);
336 reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
337 gtk_widget_show (reg_btn);
338 gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
339 g_signal_connect (G_OBJECT (reg_btn), "clicked",
340 G_CALLBACK (prefs_display_header_register_cb),
341 GINT_TO_POINTER(FALSE));
342 del_btn = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
343 gtk_widget_show (del_btn);
344 gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
345 g_signal_connect (G_OBJECT (del_btn), "clicked",
346 G_CALLBACK (prefs_display_header_delete_cb),
347 headers_list_view);
349 up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
350 gtk_widget_show (up_btn);
351 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
352 g_signal_connect (G_OBJECT (up_btn), "clicked",
353 G_CALLBACK (prefs_display_header_up), NULL);
355 down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
356 gtk_widget_show (down_btn);
357 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
358 g_signal_connect (G_OBJECT (down_btn), "clicked",
359 G_CALLBACK (prefs_display_header_down), NULL);
361 /* hidden headers list */
363 list_view_hbox2 = gtk_hbox_new (FALSE, 8);
364 gtk_widget_show (list_view_hbox2);
365 gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox2, TRUE, TRUE, 0);
367 list_view_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
368 gtk_widget_set_size_request (list_view_scrolledwin, 200, 210);
369 gtk_widget_show (list_view_scrolledwin);
370 gtk_box_pack_start (GTK_BOX (list_view_hbox2), list_view_scrolledwin,
371 TRUE, TRUE, 0);
372 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (list_view_scrolledwin),
373 GTK_POLICY_AUTOMATIC,
374 GTK_POLICY_AUTOMATIC);
375 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_view_scrolledwin),
376 GTK_SHADOW_IN);
378 hidden_headers_list_view = prefs_display_header_list_view_create
379 (_("Hidden headers"));
380 gtk_widget_show (hidden_headers_list_view);
381 gtk_container_add (GTK_CONTAINER (list_view_scrolledwin),
382 hidden_headers_list_view);
384 btn_vbox = gtk_vbox_new (FALSE, 8);
385 gtk_widget_show (btn_vbox);
386 gtk_box_pack_start (GTK_BOX (list_view_hbox2), btn_vbox, FALSE, FALSE, 0);
388 reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
389 gtk_widget_show (reg_btn);
390 gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
391 g_signal_connect (G_OBJECT (reg_btn), "clicked",
392 G_CALLBACK
393 (prefs_display_header_register_cb),
394 GINT_TO_POINTER(TRUE));
395 del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
396 gtk_widget_show (del_btn);
397 gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
398 g_signal_connect (G_OBJECT (del_btn), "clicked",
399 G_CALLBACK (prefs_display_header_delete_cb),
400 hidden_headers_list_view);
404 PACK_CHECK_BUTTON (vbox, checkbtn_other_headers,
405 _("Show all unspecified headers"));
406 SET_TOGGLE_SENSITIVITY (checkbtn_other_headers, list_view_hbox2);
408 gtk_widget_show_all(window);
410 dispheader.window = window;
411 dispheader.ok_btn = ok_btn;
412 dispheader.cancel_btn = cancel_btn;
414 dispheader.hdr_combo = hdr_combo;
416 dispheader.headers_list_view = headers_list_view;
417 dispheader.hidden_headers_list_view = hidden_headers_list_view;
419 dispheader.other_headers = checkbtn_other_headers;
422 void prefs_display_header_read_config(void)
424 gchar *rcpath;
425 FILE *fp;
426 gchar buf[PREFSBUFSIZE];
427 DisplayHeaderProp *dp;
429 debug_print("Reading configuration for displaying headers...\n");
431 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
432 DISPLAY_HEADER_RC, NULL);
433 if ((fp = g_fopen(rcpath, "rb")) == NULL) {
434 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
435 g_free(rcpath);
436 prefs_common.disphdr_list = NULL;
437 prefs_display_header_set_default();
438 return;
440 g_free(rcpath);
442 /* remove all previous headers list */
443 while (prefs_common.disphdr_list != NULL) {
444 dp = (DisplayHeaderProp *)prefs_common.disphdr_list->data;
445 display_header_prop_free(dp);
446 prefs_common.disphdr_list =
447 g_slist_remove(prefs_common.disphdr_list, dp);
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 g_strdelimit(buf, "\r\n", '\0');
452 dp = display_header_prop_read_str(buf);
453 if (dp)
454 prefs_common.disphdr_list =
455 g_slist_append(prefs_common.disphdr_list, dp);
458 fclose(fp);
461 static void prefs_display_header_write_config(void)
463 gchar *rcpath;
464 PrefFile *pfile;
465 GSList *cur;
467 debug_print("Writing configuration for displaying headers...\n");
469 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
470 DISPLAY_HEADER_RC, NULL);
472 if ((pfile = prefs_write_open(rcpath)) == NULL) {
473 g_warning("failed to write configuration to file");
474 g_free(rcpath);
475 return;
478 for (cur = prefs_common.disphdr_list; cur != NULL;
479 cur = cur->next) {
480 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
481 gchar *dpstr;
483 dpstr = display_header_prop_get_str(dp);
484 if (fputs(dpstr, pfile->fp) == EOF ||
485 fputc('\n', pfile->fp) == EOF) {
486 FILE_OP_ERROR(rcpath, "fputs || fputc");
487 prefs_file_close_revert(pfile);
488 g_free(rcpath);
489 g_free(dpstr);
490 return;
492 g_free(dpstr);
495 g_free(rcpath);
497 if (prefs_file_close(pfile) < 0) {
498 g_warning("failed to write configuration to file");
499 return;
503 static void prefs_display_header_set_dialog(void)
505 GtkTreeView *list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
506 GtkTreeView *hidden_list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
507 GSList *cur;
508 GtkTreeModel *model_list, *model_hidden;
510 model_list = gtk_tree_view_get_model(list_view);
511 model_hidden = gtk_tree_view_get_model(hidden_list_view);
513 gtk_list_store_clear(GTK_LIST_STORE(model_list));
514 gtk_list_store_clear(GTK_LIST_STORE(model_hidden));
516 for (cur = prefs_common.disphdr_list; cur != NULL;
517 cur = cur->next) {
518 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
520 if (dp->hidden)
521 prefs_display_header_insert_header(GTK_LIST_STORE
522 (model_hidden), dp->name, dp);
523 else
524 prefs_display_header_insert_header(GTK_LIST_STORE
525 (model_list), dp->name, dp);
528 gtk_toggle_button_set_active
529 (GTK_TOGGLE_BUTTON(dispheader.other_headers),
530 prefs_common.show_other_header);
533 static void prefs_display_header_set_list(void)
535 gint row = 0;
536 DisplayHeaderProp *dp;
537 GtkTreeModel *model;
538 GtkTreeIter iter;
540 g_slist_free(prefs_common.disphdr_list);
541 prefs_common.disphdr_list = NULL;
543 model = gtk_tree_view_get_model(GTK_TREE_VIEW(dispheader.headers_list_view));
544 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
545 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
546 if (dp)
547 prefs_common.disphdr_list =
548 g_slist_append(prefs_common.disphdr_list, dp);
549 row++;
552 model = gtk_tree_view_get_model
553 (GTK_TREE_VIEW(dispheader.hidden_headers_list_view));
554 row = 0;
555 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
556 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
557 if (dp)
558 prefs_common.disphdr_list =
559 g_slist_append(prefs_common.disphdr_list, dp);
560 row++;
564 static gint prefs_display_header_find_header(GtkTreeView *list_view,
565 const gchar *header)
567 gint row = 0;
568 DisplayHeaderProp *dp;
569 GtkTreeModel *model;
570 GtkTreeIter iter;
572 model = gtk_tree_view_get_model(list_view);
573 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
574 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
575 if (dp && g_ascii_strcasecmp(dp->name, header) == 0)
576 return row;
577 row++;
580 return -1;
583 static void prefs_display_header_list_view_set_row(gboolean hidden)
585 GtkTreeView *list_view;
586 DisplayHeaderProp *dp;
587 gchar *entry_text;
588 GtkTreeModel *model;
590 #if !GTK_CHECK_VERSION(2, 24, 0)
591 entry_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(dispheader.hdr_combo));
592 #else
593 entry_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(dispheader.hdr_combo));
594 #endif
595 if (!entry_text)
596 entry_text = gtk_editable_get_chars(
597 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(dispheader.hdr_combo))),0,-1);
598 if (!entry_text || entry_text[0] == '\0') {
599 alertpanel_error(_("Header name is not set."));
600 return;
603 if (hidden)
604 list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
605 else
606 list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
608 if (prefs_display_header_find_header(list_view, entry_text) != -1) {
609 alertpanel_error(_("This header is already in the list."));
610 return;
613 dp = g_new0(DisplayHeaderProp, 1);
615 dp->name = g_strdup(entry_text);
616 dp->hidden = hidden;
618 model = gtk_tree_view_get_model(list_view);
619 prefs_display_header_insert_header(GTK_LIST_STORE(model),
620 dp->name, dp);
622 prefs_display_header_set_list();
624 g_free(entry_text);
627 static void prefs_display_header_register_cb(GtkButton *btn,
628 gpointer hidden_data)
630 prefs_display_header_list_view_set_row(GPOINTER_TO_INT(hidden_data));
633 static void prefs_display_header_delete_cb(GtkButton *btn, gpointer list_view_data)
635 GtkTreeView *list_view = GTK_TREE_VIEW(list_view_data);
636 DisplayHeaderProp *dp;
637 GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
638 GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
639 GtkTreeIter iter;
641 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
642 return;
644 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, PREFS_HDR_DATA, &dp, -1);
645 if (!dp)
646 return;
648 prefs_common.disphdr_list =
649 g_slist_remove(prefs_common.disphdr_list, dp);
650 display_header_prop_free(dp);
651 gtk_list_store_remove(store, &iter);
654 static void prefs_display_header_up(void)
656 GtkTreePath *prev, *sel, *try;
657 GtkTreeIter isel;
658 GtkListStore *store = NULL;
659 GtkTreeModel *model = NULL;
660 GtkTreeIter iprev;
662 if (!gtk_tree_selection_get_selected
663 (gtk_tree_view_get_selection
664 (GTK_TREE_VIEW(dispheader.headers_list_view)),
665 &model,
666 &isel))
667 return;
668 store = (GtkListStore *)model;
669 sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
670 if (!sel)
671 return;
673 /* no move if we're at row 0... */
674 try = gtk_tree_path_copy(sel);
675 if (!gtk_tree_path_prev(try)) {
676 gtk_tree_path_free(try);
677 gtk_tree_path_free(sel);
678 return;
681 prev = try;
682 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
683 &iprev, prev);
684 gtk_list_store_swap(store, &iprev, &isel);
686 gtk_tree_path_free(sel);
687 gtk_tree_path_free(prev);
690 static void prefs_display_header_down(void)
692 GtkListStore *store = NULL;
693 GtkTreeModel *model = NULL;
694 GtkTreeIter next, sel;
695 GtkTreePath *try;
697 if (!gtk_tree_selection_get_selected
698 (gtk_tree_view_get_selection
699 (GTK_TREE_VIEW(dispheader.headers_list_view)),
700 &model,
701 &sel))
702 return;
703 store = (GtkListStore *)model;
704 try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
705 if (!try)
706 return;
708 next = sel;
709 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
710 gtk_list_store_swap(store, &next, &sel);
712 gtk_tree_path_free(try);
715 static gboolean prefs_display_header_key_pressed(GtkWidget *widget,
716 GdkEventKey *event,
717 gpointer data)
719 if (event && event->keyval == GDK_KEY_Escape)
720 prefs_display_header_cancel();
721 return FALSE;
724 static void prefs_display_header_ok(void)
726 prefs_common.show_other_header =
727 gtk_toggle_button_get_active
728 (GTK_TOGGLE_BUTTON(dispheader.other_headers));
729 prefs_display_header_write_config();
730 gtk_widget_hide(dispheader.window);
731 gtk_window_set_modal(GTK_WINDOW(dispheader.window), FALSE);
734 static void prefs_display_header_cancel(void)
736 prefs_display_header_read_config();
737 gtk_widget_hide(dispheader.window);
738 gtk_window_set_modal(GTK_WINDOW(dispheader.window), FALSE);
741 static gint prefs_display_header_deleted(GtkWidget *widget, GdkEventAny *event,
742 gpointer data)
744 prefs_display_header_cancel();
745 return TRUE;
748 static GtkListStore *prefs_display_header_create_store(void)
750 return gtk_list_store_new(N_PREFS_HDR_COLUMNS,
751 G_TYPE_STRING,
752 G_TYPE_POINTER,
753 -1);
756 static void prefs_display_header_insert_header(GtkListStore *store,
757 gchar *name,
758 DisplayHeaderProp *dp)
760 GtkTreeIter iter;
762 /* add new */
763 gtk_list_store_append(store, &iter);
764 gtk_list_store_set(store, &iter,
765 PREFS_HDR_HEADER,
766 name,
767 PREFS_HDR_DATA, dp,
768 -1);
771 static GtkWidget *prefs_display_header_list_view_create(const gchar *name)
773 GtkWidget *list_view;
774 GtkTreeSelection *selector;
775 GtkTreeModel *model;
777 model = GTK_TREE_MODEL(prefs_display_header_create_store());
778 list_view = gtk_tree_view_new_with_model(model);
779 g_object_unref(G_OBJECT(model));
781 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view),
782 prefs_common.use_stripes_everywhere);
784 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
785 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
787 prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
789 return list_view;
792 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view,
793 const gchar *name)
795 GtkTreeViewColumn *column;
796 GtkCellRenderer *renderer;
798 renderer = gtk_cell_renderer_text_new();
799 column = gtk_tree_view_column_new_with_attributes
800 (name, renderer, "text", PREFS_HDR_HEADER, NULL);
801 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
805 *\brief Called as a result of a gtk_list_store_swap()
807 static void headers_list_model_rows_reordered(GtkTreeModel *model,
808 GtkTreePath *path,
809 GtkTreeIter *iter,
810 gpointer arg,
811 GtkTreeView *list_view)
813 prefs_display_header_set_list();
817 *\brief Called as a result of a drag & drop
819 static void drag_end(GtkTreeView *list_view,
820 GdkDragContext *context,
821 gpointer data)
823 prefs_display_header_set_list();