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