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