change gtk_scrolled_window_set_policy from
[claws.git] / src / foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkwindow.h>
27 #include <gtk/gtkvbox.h>
28 #include <gtk/gtkscrolledwindow.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtkhbbox.h>
31 #include <gtk/gtksignal.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "utils.h"
43 #include "gtkutils.h"
44 #include "gtksctree.h"
45 #include "stock_pixmap.h"
46 #include "foldersel.h"
47 #include "alertpanel.h"
48 #include "manage_window.h"
49 #include "folder.h"
50
51 static GdkPixmap *folderxpm;
52 static GdkBitmap *folderxpmmask;
53 static GdkPixmap *folderopenxpm;
54 static GdkBitmap *folderopenxpmmask;
55
56 static GtkWidget *window;
57 static GtkWidget *ctree;
58 static GtkWidget *entry;
59 static GtkWidget *ok_button;
60 static GtkWidget *cancel_button;
61
62 static FolderItem *folder_item;
63 static FolderItem *selected_item;
64
65 static gboolean cancelled;
66 static gboolean finished;
67
68 static void foldersel_create    (void);
69 static void foldersel_init      (void);
70 static void foldersel_set_tree  (Folder                 *cur_folder,
71                                  FolderSelectionType     type);
72
73 static void foldersel_selected  (GtkCList       *clist,
74                                  gint            row,
75                                  gint            column,
76                                  GdkEvent       *event,
77                                  gpointer        data);
78
79 static void foldersel_ok        (GtkButton      *button,
80                                  gpointer        data);
81 static void foldersel_cancel    (GtkButton      *button,
82                                  gpointer        data);
83 static void foldersel_activated (void);
84 static gint delete_event        (GtkWidget      *widget,
85                                  GdkEventAny    *event,
86                                  gpointer        data);
87 static gboolean key_pressed     (GtkWidget      *widget,
88                                  GdkEventKey    *event,
89                                  gpointer        data);
90
91 static gint foldersel_clist_compare     (GtkCList       *clist,
92                                          gconstpointer   ptr1,
93                                          gconstpointer   ptr2);
94
95 FolderItem *foldersel_folder_sel(Folder *cur_folder,
96                                  FolderSelectionType type,
97                                  const gchar *default_folder)
98 {
99         GtkCTreeNode *node;
100
101         selected_item = NULL;
102
103         if (!window) {
104                 foldersel_create();
105                 foldersel_init();
106         } else
107                 gtk_widget_show(window);
108         manage_window_set_transient(GTK_WINDOW(window));
109
110         foldersel_set_tree(cur_folder, type);
111
112         if (folder_item) {
113                 node = gtk_ctree_find_by_row_data
114                         (GTK_CTREE(ctree), NULL, folder_item);
115                 if (node) {
116                         gint row;
117
118                         row = gtkut_ctree_get_nth_from_node
119                                 (GTK_CTREE(ctree), node);
120                         gtk_clist_select_row(GTK_CLIST(ctree), row, -1);
121                         gtkut_clist_set_focus_row(GTK_CLIST(ctree), row);
122                         gtk_ctree_node_moveto(GTK_CTREE(ctree), node, -1,
123                                               0.5, 0);
124                 }
125         }
126         gtk_widget_grab_focus(ok_button);
127         gtk_widget_grab_focus(ctree);
128
129         cancelled = finished = FALSE;
130
131         while (finished == FALSE)
132                 gtk_main_iteration();
133
134         gtk_widget_hide(window);
135         gtk_entry_set_text(GTK_ENTRY(entry), "");
136         gtk_clist_clear(GTK_CLIST(ctree));
137
138         if (!cancelled &&
139             selected_item && selected_item->path) {
140                 folder_item = selected_item;
141                 return folder_item;
142         } else
143                 return NULL;
144 }
145
146 static void foldersel_create(void)
147 {
148         GtkWidget *vbox;
149         GtkWidget *scrolledwin;
150         GtkWidget *confirm_area;
151
152         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
153         gtk_window_set_title(GTK_WINDOW(window), _("Select folder"));
154         gtk_container_set_border_width(GTK_CONTAINER(window), 4);
155         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
156         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
157         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
158         g_signal_connect(G_OBJECT(window), "delete_event",
159                          G_CALLBACK(delete_event), NULL);
160         g_signal_connect(G_OBJECT(window), "key_press_event",
161                          G_CALLBACK(key_pressed), NULL);
162         MANAGE_WINDOW_SIGNALS_CONNECT(window);
163
164         vbox = gtk_vbox_new(FALSE, 4);
165         gtk_container_add(GTK_CONTAINER(window), vbox);
166
167         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
168         gtk_widget_set_size_request(window, 300, 360);
169         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
170                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
171         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
172
173         ctree = gtk_ctree_new(1, 0);
174         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
175                                             GTK_CLIST(ctree)->vadjustment);
176         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
177         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
178         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
179         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
180                                      GTK_CTREE_EXPANDER_SQUARE);
181         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
182         gtk_clist_set_compare_func(GTK_CLIST(ctree), foldersel_clist_compare);
183         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[0].button,
184                                GTK_CAN_FOCUS);
185         /* g_signal_connect(G_OBJECT(ctree), "tree_select_row",
186                             G_CALLBACK(foldersel_selected), NULL); */
187         g_signal_connect(G_OBJECT(ctree), "select_row",
188                          G_CALLBACK(foldersel_selected), NULL);
189
190         entry = gtk_entry_new();
191         gtk_entry_set_editable(GTK_ENTRY(entry), FALSE);
192         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
193         g_signal_connect(G_OBJECT(entry), "activate",
194                          G_CALLBACK(foldersel_activated), NULL);
195
196         gtkut_button_set_create(&confirm_area,
197                                 &ok_button,     _("OK"),
198                                 &cancel_button, _("Cancel"),
199                                 NULL,           NULL);
200
201         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
202         gtk_widget_grab_default(ok_button);
203
204         g_signal_connect(G_OBJECT(ok_button), "clicked",
205                          G_CALLBACK(foldersel_ok), NULL);
206         g_signal_connect(G_OBJECT(cancel_button), "clicked",
207                          G_CALLBACK(foldersel_cancel), NULL);
208
209         gtk_widget_show_all(window);
210 }
211
212 static void foldersel_init(void)
213 {
214         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE,
215                          &folderxpm, &folderxpmmask);
216         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN,
217                          &folderopenxpm, &folderopenxpmmask);
218 }
219
220 static gboolean foldersel_gnode_func(GtkCTree *ctree, guint depth,
221                                      GNode *gnode, GtkCTreeNode *cnode,
222                                      gpointer data)
223 {
224         FolderItem *item = FOLDER_ITEM(gnode->data);
225         gchar *name;
226
227         name = folder_item_get_name(item);
228
229         gtk_ctree_node_set_row_data(ctree, cnode, item);
230         gtk_ctree_set_node_info(ctree, cnode, name,
231                                 FOLDER_SPACING,
232                                 folderxpm, folderxpmmask,
233                                 folderopenxpm, folderopenxpmmask,
234                                 FALSE, FALSE);
235
236         g_free(name);
237
238         return TRUE;
239 }
240
241 static void foldersel_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
242                                   gpointer data)
243 {
244         if (GTK_CTREE_ROW(node)->children)
245                 gtk_ctree_expand(ctree, node);
246 }
247
248 #define SET_SPECIAL_FOLDER(item) \
249 { \
250         if (item) { \
251                 GtkCTreeNode *node_, *parent, *sibling; \
252  \
253                 node_ = gtk_ctree_find_by_row_data \
254                         (GTK_CTREE(ctree), node, item); \
255                 if (!node_) \
256                         g_warning("%s not found.\n", item->path); \
257                 else { \
258                         parent = GTK_CTREE_ROW(node_)->parent; \
259                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
260                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
261                         else \
262                                 sibling = GTK_CTREE_ROW(parent)->children; \
263                         if (node_ != sibling) \
264                                 gtk_ctree_move(GTK_CTREE(ctree), \
265                                                node_, parent, sibling); \
266                 } \
267  \
268                 prev = node_; \
269         } \
270 }
271
272 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
273 {
274         Folder *folder;
275         GtkCTreeNode *node;
276         GList *list;
277
278         list = folder_get_list();
279
280         gtk_clist_freeze(GTK_CLIST(ctree));
281
282         for (; list != NULL; list = list->next) {
283                 GtkCTreeNode *prev = NULL;
284
285                 folder = FOLDER(list->data);
286                 g_return_if_fail(folder != NULL);
287
288                 if (type != FOLDER_SEL_ALL) {
289                         if (FOLDER_TYPE(folder) == F_NEWS)
290                                 continue;
291                 }
292
293                 node = gtk_ctree_insert_gnode(GTK_CTREE(ctree), NULL, NULL,
294                                               folder->node,
295                                               foldersel_gnode_func,
296                                               NULL);
297                 gtk_sctree_sort_recursive(GTK_CTREE(ctree), node);
298                 SET_SPECIAL_FOLDER(folder->inbox);
299                 SET_SPECIAL_FOLDER(folder->outbox);
300                 SET_SPECIAL_FOLDER(folder->draft);
301                 SET_SPECIAL_FOLDER(folder->queue);
302                 SET_SPECIAL_FOLDER(folder->trash);
303                 gtk_ctree_pre_recursive(GTK_CTREE(ctree), node,
304                                         foldersel_expand_func,
305                                         NULL);
306         }
307
308         gtk_clist_thaw(GTK_CLIST(ctree));
309 }
310
311 static void foldersel_selected(GtkCList *clist, gint row, gint column,
312                                GdkEvent *event, gpointer data)
313 {
314         GdkEventButton *ev = (GdkEventButton *)event;
315
316         selected_item = gtk_clist_get_row_data(clist, row);
317         if (selected_item && selected_item->path) {
318                 gchar *id;
319                 id = folder_item_get_identifier(selected_item);
320                 gtk_entry_set_text(GTK_ENTRY(entry), id);
321                 g_free(id);
322         } else
323                 gtk_entry_set_text(GTK_ENTRY(entry), "");
324
325         if (ev && GDK_2BUTTON_PRESS == ev->type)
326                 gtk_button_clicked(GTK_BUTTON(ok_button));
327 }
328
329 static void foldersel_ok(GtkButton *button, gpointer data)
330 {
331         finished = TRUE;
332 }
333
334 static void foldersel_cancel(GtkButton *button, gpointer data)
335 {
336         cancelled = TRUE;
337         finished = TRUE;
338 }
339
340 static void foldersel_activated(void)
341 {
342         gtk_button_clicked(GTK_BUTTON(ok_button));
343 }
344
345 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
346 {
347         foldersel_cancel(NULL, NULL);
348         return TRUE;
349 }
350
351 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
352 {
353         if (event && event->keyval == GDK_Escape)
354                 foldersel_cancel(NULL, NULL);
355         return FALSE;
356 }
357
358 static gint foldersel_clist_compare(GtkCList *clist,
359                                     gconstpointer ptr1, gconstpointer ptr2)
360 {
361         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
362         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
363
364         if (!item1->name)
365                 return (item2->name != NULL);
366         if (!item2->name)
367                 return -1;
368
369         return g_strcasecmp(item1->name, item2->name);
370 }