53866ffe80ac9b1c266a9b88ba12a7c17b926b97
[claws.git] / src / gtk / foldersort.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24
25 #include "foldersort.h"
26 #include "inc.h"
27 #include "utils.h"
28
29 typedef struct _FolderSortDialog FolderSortDialog;
30
31 struct _FolderSortDialog
32 {
33         GtkWidget *window;
34         GtkWidget *moveup_btn;
35         GtkWidget *movedown_btn;
36         GtkWidget *folderlist;
37
38         gint rows, selected;
39 };
40
41 static void destroy_dialog(FolderSortDialog *dialog)
42 {
43         inc_unlock();
44         gtk_widget_destroy(dialog->window);
45
46         g_free(dialog);
47 }
48
49 static void ok_clicked(GtkWidget *widget, FolderSortDialog *dialog)
50 {
51         Folder *folder;
52         int i;
53
54         for (i = 0; i < dialog->rows; i++) {
55                 folder = gtk_clist_get_row_data(GTK_CLIST(dialog->folderlist), i);
56
57                 folder_set_sort(folder, dialog->rows - i);
58         }
59
60         destroy_dialog(dialog);
61 }
62
63 static void cancel_clicked(GtkWidget *widget, FolderSortDialog *dialog)
64 {
65         destroy_dialog(dialog);
66 }
67
68 static void set_selected(FolderSortDialog *dialog, gint row)
69 {
70         if (row >= 0) {
71                 gtk_widget_set_sensitive(dialog->moveup_btn, row > 0);
72                 gtk_widget_set_sensitive(dialog->movedown_btn, row < (dialog->rows - 1));
73         } else {
74                 gtk_widget_set_sensitive(dialog->moveup_btn, FALSE);
75                 gtk_widget_set_sensitive(dialog->movedown_btn, FALSE);
76         }
77
78         dialog->selected = row;
79 }
80
81 static void moveup_clicked(GtkWidget *widget, FolderSortDialog *dialog)
82 {
83         g_return_if_fail(dialog->selected > 0);
84
85         gtk_clist_swap_rows(GTK_CLIST(dialog->folderlist), dialog->selected, dialog->selected - 1);
86 }
87
88 static void movedown_clicked(GtkWidget *widget, FolderSortDialog *dialog)
89 {
90         g_return_if_fail(dialog->selected < (dialog->rows - 1));
91
92         gtk_clist_swap_rows(GTK_CLIST(dialog->folderlist), dialog->selected, dialog->selected + 1);
93 }
94
95 static void row_selected(GtkCList *clist, gint row, gint column, GdkEventButton *event, FolderSortDialog *dialog)
96 {
97         set_selected(dialog, row);
98 }
99
100 static void row_unselected(GtkCList *clist, gint row, gint column, GdkEventButton *event, FolderSortDialog *dialog)
101 {
102         set_selected(dialog, -1);
103 }
104
105 static void row_moved(GtkCList *clist, gint srcpos, gint destpos, FolderSortDialog *dialog)
106 {
107         if (dialog->selected == -1)
108                 return;
109         else if (srcpos == dialog->selected)
110                 set_selected(dialog, destpos);
111         else if (srcpos < dialog->selected && destpos >= dialog->selected)
112                 set_selected(dialog, dialog->selected - 1);
113         else if (srcpos > dialog->selected && destpos <= dialog->selected)
114                 set_selected(dialog, dialog->selected + 1);
115 }
116
117 void foldersort_open()
118 {
119         FolderSortDialog *dialog = g_new0(FolderSortDialog, 1);
120         GList *flist;
121
122         GtkWidget *window;
123         GtkWidget *vbox;
124         GtkWidget *vbox1;
125         GtkWidget *label1;
126         GtkWidget *hbox;
127         GtkWidget *hbox2;
128         GtkWidget *ok_btn;
129         GtkWidget *cancel_btn;
130         GtkWidget *confirm_area;
131         GtkWidget *moveup_btn;
132         GtkWidget *movedown_btn;
133         GtkWidget *btn_vbox;
134         GtkWidget *scrolledwindow1;
135         GtkWidget *folderlist;
136         GtkWidget *label2;
137
138         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
139         g_object_set_data(G_OBJECT(window), "window", window);
140         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
141         gtk_window_set_title(GTK_WINDOW(window),
142                              _("Set folder order"));
143         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
144         gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
145
146         vbox = gtk_vbox_new(FALSE, 6);
147         gtk_widget_show(vbox);
148         gtk_container_add(GTK_CONTAINER(window), vbox);
149
150         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
151                                       &ok_btn, GTK_STOCK_OK,
152                                       NULL, NULL);
153         gtk_widget_show(confirm_area);
154         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
155         gtk_widget_grab_focus(ok_btn);
156
157         g_signal_connect(G_OBJECT(ok_btn), "clicked",
158                          G_CALLBACK(ok_clicked), dialog);
159         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
160                          G_CALLBACK(cancel_clicked), dialog);
161
162         vbox1 = gtk_vbox_new(FALSE, 8);
163         gtk_widget_show(vbox1);
164         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
165         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
166
167         hbox = gtk_hbox_new(FALSE, 8);
168         gtk_widget_show(hbox);
169         gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
170
171         label1 = gtk_label_new(_
172                 ("Move folders up or down to change the sort order in the folder list."));
173         gtk_widget_show(label1);
174         gtk_label_set_line_wrap(GTK_LABEL(label1), TRUE);
175         gtk_box_pack_start(GTK_BOX(hbox), label1, FALSE, FALSE, 0);
176
177         hbox2 = gtk_hbox_new(FALSE, 8);
178         gtk_widget_show(hbox2);
179         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
180
181         scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
182         gtk_widget_show(scrolledwindow1);
183         gtk_widget_set_size_request(scrolledwindow1, -1, 150);
184         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow1,
185                            TRUE, TRUE, 0);
186         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow1),
187                                        GTK_POLICY_AUTOMATIC,
188                                        GTK_POLICY_AUTOMATIC);
189
190         folderlist = gtk_clist_new(1);
191         gtk_widget_show(folderlist);
192         gtk_container_add(GTK_CONTAINER(scrolledwindow1), folderlist);
193         gtk_clist_set_column_width(GTK_CLIST(folderlist), 0, 80);
194         gtk_clist_column_titles_show(GTK_CLIST(folderlist));
195
196         label2 = gtk_label_new(_("Folders"));
197         gtk_widget_show(label2);
198         gtk_clist_set_column_widget(GTK_CLIST(folderlist), 0, label2);
199         gtk_label_set_justify(GTK_LABEL(label2), GTK_JUSTIFY_LEFT);
200         gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
201
202         btn_vbox = gtk_vbox_new(FALSE, 8);
203         gtk_widget_show(btn_vbox);
204         gtk_box_pack_start(GTK_BOX(hbox2), btn_vbox, FALSE, FALSE, 0);
205
206         moveup_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
207         gtk_widget_show(moveup_btn);
208         gtk_box_pack_start(GTK_BOX(btn_vbox), moveup_btn, FALSE, FALSE, 0);
209
210         movedown_btn =  gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
211         gtk_widget_show(movedown_btn);
212         gtk_box_pack_start(GTK_BOX(btn_vbox), movedown_btn, FALSE, FALSE, 0);
213
214         dialog->window = window;
215         dialog->moveup_btn = moveup_btn;
216         dialog->movedown_btn = movedown_btn;
217         dialog->folderlist = folderlist;
218
219         gtk_widget_show(window);
220         gtk_widget_set_sensitive(moveup_btn, FALSE);
221         gtk_widget_set_sensitive(movedown_btn, FALSE);
222         gtk_clist_set_reorderable(GTK_CLIST(folderlist), TRUE);
223
224         g_signal_connect(G_OBJECT(moveup_btn), "clicked",
225                          G_CALLBACK(moveup_clicked), dialog);
226         g_signal_connect(G_OBJECT(movedown_btn), "clicked",
227                          G_CALLBACK(movedown_clicked), dialog);
228
229         g_signal_connect(G_OBJECT(folderlist), "select-row",
230                          G_CALLBACK(row_selected), dialog);
231         g_signal_connect(G_OBJECT(folderlist), "unselect-row",
232                          G_CALLBACK(row_unselected), dialog);
233         g_signal_connect(G_OBJECT(folderlist), "row-move",
234                          G_CALLBACK(row_moved), dialog);
235
236         dialog->rows = 0;
237         dialog->selected = -1;
238         for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
239                 Folder *folder = flist->data;
240                 int row;
241                 gchar *text[1];
242
243                 text[0] = folder->name;
244                 row = gtk_clist_append(GTK_CLIST(folderlist), text);
245                 gtk_clist_set_row_data(GTK_CLIST(folderlist), row, folder);
246                 dialog->rows++;
247         }
248
249         inc_lock();
250 }