Use explicit names for mouse buttons instead of numbers.
[geeqie.git] / src / view_dir_list.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  *
5  * Author: John Ellis
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "main.h"
13 #include "view_dir_list.h"
14
15 #include "dnd.h"
16 #include "dupe.h"
17 #include "filelist.h"
18 #include "layout.h"
19 #include "layout_image.h"
20 #include "layout_util.h"
21 #include "utilops.h"
22 #include "ui_bookmark.h"
23 #include "ui_fileops.h"
24 #include "ui_menu.h"
25 #include "ui_tree_edit.h"
26 #include "view_dir.h"
27
28 #include <gdk/gdkkeysyms.h> /* for keyboard values */
29
30
31 #define VDLIST_INFO(_vd_, _part_) (((ViewDirInfoList *)(_vd_->info))->_part_)
32
33
34 /*
35  *-----------------------------------------------------------------------------
36  * misc
37  *-----------------------------------------------------------------------------
38  */
39
40 gint vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
41 {
42         GtkTreeModel *store;
43         gint valid;
44         gint row = 0;
45
46         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
47         valid = gtk_tree_model_get_iter_first(store, iter);
48         while (valid)
49                 {
50                 FileData *fd_n;
51                 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1);
52                 if (fd_n == fd) return row;
53
54                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
55                 row++;
56                 }
57
58         return -1;
59 }
60
61
62 FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row)
63 {
64         GList *work;
65         gint n;
66
67         if (!path)
68                 {
69                 if (row) *row = -1;
70                 return NULL;
71                 }
72
73         n = 0;
74         work = VDLIST_INFO(vd, list);
75         while (work)
76                 {
77                 FileData *fd = work->data;
78                 if (strcmp(fd->path, path) == 0)
79                         {
80                         if (row) *row = n;
81                         return fd;
82                         }
83                 work = work->next;
84                 n++;
85                 }
86
87         if (row) *row = -1;
88         return NULL;
89 }
90
91 /*
92  *-----------------------------------------------------------------------------
93  * dnd
94  *-----------------------------------------------------------------------------
95  */
96
97 static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align)
98 {
99         GtkTreeIter iter;
100
101         if (GTK_WIDGET_REALIZED(vd->view) && vd_find_row(vd, fd, &iter) >= 0)
102                 {
103                 GtkTreeModel *store;
104                 GtkTreePath *tpath;
105
106                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
107                 tpath = gtk_tree_model_get_path(store, &iter);
108                 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vd->view), tpath, NULL, TRUE, y_align, 0.0);
109                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, NULL, FALSE);
110                 gtk_tree_path_free(tpath);
111
112                 if (!GTK_WIDGET_HAS_FOCUS(vd->view)) gtk_widget_grab_focus(vd->view);
113                 }
114 }
115
116 /*
117  *-----------------------------------------------------------------------------
118  * main
119  *-----------------------------------------------------------------------------
120  */
121
122 void vdlist_select_row(ViewDir *vd, FileData *fd)
123 {
124         if (fd && vd->select_func)
125                 {
126                 gchar *path;
127
128                 path = g_strdup(fd->path);
129                 vd->select_func(vd, path, vd->select_data);
130                 g_free(path);
131                 }
132 }
133
134 const gchar *vdlist_row_get_path(ViewDir *vd, gint row)
135 {
136         FileData *fd;
137
138         fd = g_list_nth_data(VDLIST_INFO(vd, list), row);
139
140         if (fd) return fd->path;
141
142         return NULL;
143 }
144
145 static void vdlist_populate(ViewDir *vd)
146 {
147         GtkListStore *store;
148         GList *work;
149
150         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)));
151         gtk_list_store_clear(store);
152
153         work = VDLIST_INFO(vd, list);
154         while (work)
155                 {
156                 FileData *fd;
157                 GtkTreeIter iter;
158                 GdkPixbuf *pixbuf;
159
160                 fd = work->data;
161
162                 if (access_file(fd->path, R_OK | X_OK) && fd->name)
163                         {
164                         if (fd->name[0] == '.' && fd->name[1] == '\0')
165                                 {
166                                 pixbuf = vd->pf->open;
167                                 }
168                         else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0')
169                                 {
170                                 pixbuf = vd->pf->parent;
171                                 }
172                         else
173                                 {
174                                 pixbuf = vd->pf->close;
175                                 }
176                         }
177                 else
178                         {
179                         pixbuf = vd->pf->deny;
180                         }
181
182                 gtk_list_store_append(store, &iter);
183                 gtk_list_store_set(store, &iter,
184                                    DIR_COLUMN_POINTER, fd,
185                                    DIR_COLUMN_ICON, pixbuf,
186                                    DIR_COLUMN_NAME, fd->name, -1);
187
188                 work = work->next;
189                 }
190
191         vd->click_fd = NULL;
192         vd->drop_fd = NULL;
193 }
194
195 gint vdlist_set_path(ViewDir *vd, const gchar *path)
196 {
197         gint ret;
198         FileData *fd;
199         gchar *old_path = NULL;
200         gchar *filepath;
201
202         if (!path) return FALSE;
203         if (vd->path && strcmp(path, vd->path) == 0) return TRUE;
204
205         if (vd->path)
206                 {
207                 gchar *base;
208
209                 base = remove_level_from_path(vd->path);
210                 if (strcmp(base, path) == 0)
211                         {
212                         old_path = g_strdup(filename_from_path(vd->path));
213                         }
214                 g_free(base);
215                 }
216
217         g_free(vd->path);
218         vd->path = g_strdup(path);
219
220         filelist_free(VDLIST_INFO(vd, list));
221         VDLIST_INFO(vd, list) = NULL;
222
223         ret = filelist_read(vd->path, NULL, &VDLIST_INFO(vd, list));
224
225         VDLIST_INFO(vd, list) = filelist_sort(VDLIST_INFO(vd, list), SORT_NAME, TRUE);
226
227         /* add . and .. */
228
229         if (strcmp(vd->path, "/") != 0)
230                 {
231                 filepath = g_strconcat(vd->path, "/", "..", NULL);
232                 fd = file_data_new_simple(filepath);
233                 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd);
234                 g_free(filepath);
235                 }
236
237         if (options->file_filter.show_dot_directory)
238                 {
239                 filepath = g_strconcat(vd->path, "/", ".", NULL);
240                 fd = file_data_new_simple(filepath);
241                 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd);
242                 g_free(filepath);
243         }
244
245         vdlist_populate(vd);
246
247         if (old_path)
248                 {
249                 /* scroll to make last path visible */
250                 FileData *found = NULL;
251                 GList *work;
252
253                 work = VDLIST_INFO(vd, list);
254                 while (work && !found)
255                         {
256                         FileData *fd = work->data;
257                         if (strcmp(old_path, fd->name) == 0) found = fd;
258                         work = work->next;
259                         }
260
261                 if (found) vdlist_scroll_to_row(vd, found, 0.5);
262
263                 g_free(old_path);
264                 return ret;
265                 }
266
267         if (GTK_WIDGET_REALIZED(vd->view))
268                 {
269                 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(vd->view), 0, 0);
270                 }
271
272         return ret;
273 }
274
275 void vdlist_refresh(ViewDir *vd)
276 {
277         gchar *path;
278
279         path = g_strdup(vd->path);
280         vd->path = NULL;
281         vdlist_set_path(vd, path);
282         g_free(path);
283 }
284
285 gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
286 {
287         ViewDir *vd = data;
288         GtkTreePath *tpath;
289
290         if (event->keyval != GDK_Menu) return FALSE;
291
292         gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, NULL);
293         if (tpath)
294                 {
295                 GtkTreeModel *store;
296                 GtkTreeIter iter;
297
298                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
299                 gtk_tree_model_get_iter(store, &iter, tpath);
300                 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vd->click_fd, -1);
301
302                 gtk_tree_path_free(tpath);
303                 }
304         else
305                 {
306                 vd->click_fd = NULL;
307                 }
308
309         vd_color_set(vd, vd->click_fd, TRUE);
310
311         vd->popup = vd_pop_menu(vd, vd->click_fd);
312
313         gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, vd_menu_position_cb, vd, 0, GDK_CURRENT_TIME);
314
315         return TRUE;
316 }
317
318 gint vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
319 {
320         ViewDir *vd = data;
321         GtkTreePath *tpath;
322         GtkTreeIter iter;
323         FileData *fd = NULL;
324
325         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
326                                           &tpath, NULL, NULL, NULL))
327                 {
328                 GtkTreeModel *store;
329
330                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
331                 gtk_tree_model_get_iter(store, &iter, tpath);
332                 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
333                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
334                 gtk_tree_path_free(tpath);
335                 }
336
337         vd->click_fd = fd;
338         vd_color_set(vd, vd->click_fd, TRUE);
339
340         if (bevent->button == MOUSE_BUTTON_RIGHT)
341                 {
342                 vd->popup = vd_pop_menu(vd, vd->click_fd);
343                 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, NULL, NULL,
344                                bevent->button, bevent->time);
345                 }
346
347         return TRUE;
348 }
349
350 void vdlist_destroy_cb(GtkWidget *widget, gpointer data)
351 {
352         ViewDir *vd = data;
353
354         vd_dnd_drop_scroll_cancel(vd);
355         widget_auto_scroll_stop(vd->view);
356
357         filelist_free(VDLIST_INFO(vd, list));
358 }
359
360 ViewDir *vdlist_new(ViewDir *vd, const gchar *path)
361 {
362         GtkListStore *store;
363         GtkTreeSelection *selection;
364         GtkTreeViewColumn *column;
365         GtkCellRenderer *renderer;
366
367         vd->info = g_new0(ViewDirInfoList, 1);
368         vd->type = DIRVIEW_LIST;
369
370         VDLIST_INFO(vd, list) = NULL;
371
372         store = gtk_list_store_new(4, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN);
373         vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
374         g_object_unref(store);
375
376         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE);
377         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE);
378
379         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view));
380         gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
381
382         column = gtk_tree_view_column_new();
383         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
384
385         renderer = gtk_cell_renderer_pixbuf_new();
386         gtk_tree_view_column_pack_start(column, renderer, FALSE);
387         gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON);
388         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL);
389
390         renderer = gtk_cell_renderer_text_new();
391         gtk_tree_view_column_pack_start(column, renderer, TRUE);
392         gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME);
393         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL);
394
395         gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column);
396
397         return vd;
398 }