r228: Added a nice list displaying the found files. Clicking opens a preview window
[rox-filer.git] / ROX-Filer / src / filer.c
blobce6070e45242da28e6d283a760fdb2b43758e9b7
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* filer.c - code for handling filer windows */
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/param.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <ctype.h>
35 #include <gtk/gtk.h>
36 #include <gdk/gdkx.h>
37 #include <gdk/gdkkeysyms.h>
38 #include "collection.h"
40 #include "main.h"
41 #include "support.h"
42 #include "gui_support.h"
43 #include "filer.h"
44 #include "pixmaps.h"
45 #include "menu.h"
46 #include "dnd.h"
47 #include "run.h"
48 #include "mount.h"
49 #include "type.h"
50 #include "options.h"
51 #include "action.h"
52 #include "minibuffer.h"
54 #define ROW_HEIGHT_LARGE 64
55 #define ROW_HEIGHT_SMALL 32
56 #define ROW_HEIGHT_FULL_INFO 44
57 #define SMALL_ICON_HEIGHT 20
58 #define SMALL_ICON_WIDTH 48
59 #define MAX_ICON_HEIGHT 42
60 #define MAX_ICON_WIDTH 48
61 #define PANEL_BORDER 2
62 #define MIN_ITEM_WIDTH 64
64 extern int collection_menu_button;
65 extern gboolean collection_single_click;
67 FilerWindow *window_with_focus = NULL;
68 GList *all_filer_windows = NULL;
70 static DisplayStyle last_display_style = LARGE_ICONS;
71 static gboolean last_show_hidden = FALSE;
72 static int (*last_sort_fn)(const void *a, const void *b) = sort_by_type;
74 static FilerWindow *window_with_selection = NULL;
76 /* Options bits */
77 static guchar *style_to_name(void);
78 static guchar *sort_fn_to_name(void);
79 static void update_options_label(void);
81 static GtkWidget *create_options();
82 static void update_options();
83 static void set_options();
84 static void save_options();
85 static char *filer_single_click(char *data);
86 static char *filer_unique_windows(char *data);
87 static char *filer_menu_on_2(char *data);
88 static char *filer_new_window_on_1(char *data);
89 static char *filer_toolbar(char *data);
90 static char *filer_display_style(char *data);
91 static char *filer_sort_by(char *data);
93 static OptionsSection options =
95 "Filer window options",
96 create_options,
97 update_options,
98 set_options,
99 save_options
102 /* The values correspond to the menu indexes in the option widget */
103 typedef enum {
104 TOOLBAR_NONE = 0,
105 TOOLBAR_NORMAL = 1,
106 TOOLBAR_GNOME = 2,
107 } ToolbarType;
108 static ToolbarType o_toolbar = TOOLBAR_NORMAL;
109 static GtkWidget *menu_toolbar;
111 static GtkWidget *display_label;
113 static gboolean o_single_click = FALSE;
114 static GtkWidget *toggle_single_click;
115 static gboolean o_new_window_on_1 = FALSE; /* Button 1 => New window */
116 static GtkWidget *toggle_new_window_on_1;
117 static GtkWidget *toggle_menu_on_2;
118 static GtkWidget *toggle_unique_filer_windows;
119 gboolean o_unique_filer_windows = FALSE;
121 /* Static prototypes */
122 static void attach(FilerWindow *filer_window);
123 static void detach(FilerWindow *filer_window);
124 static void filer_window_destroyed(GtkWidget *widget,
125 FilerWindow *filer_window);
126 static void show_menu(Collection *collection, GdkEventButton *event,
127 int number_selected, gpointer user_data);
128 static gint focus_in(GtkWidget *widget,
129 GdkEventFocus *event,
130 FilerWindow *filer_window);
131 static gint focus_out(GtkWidget *widget,
132 GdkEventFocus *event,
133 FilerWindow *filer_window);
134 static void add_item(FilerWindow *filer_window, DirItem *item);
135 static void toolbar_up_clicked(GtkWidget *widget, FilerWindow *filer_window);
136 static void toolbar_home_clicked(GtkWidget *widget, FilerWindow *filer_window);
137 static void add_button(GtkWidget *box, int pixmap,
138 GtkSignalFunc cb, FilerWindow *filer_window,
139 char *label, char *tip);
140 static GtkWidget *create_toolbar(FilerWindow *filer_window);
141 static int filer_confirm_close(GtkWidget *widget, GdkEvent *event,
142 FilerWindow *window);
143 static int calc_width(FilerWindow *filer_window, DirItem *item);
144 static void draw_large_icon(GtkWidget *widget,
145 GdkRectangle *area,
146 DirItem *item,
147 gboolean selected);
148 static void draw_string(GtkWidget *widget,
149 GdkFont *font,
150 char *string,
151 int x,
152 int y,
153 int width,
154 gboolean selected);
155 static void draw_item_large(GtkWidget *widget,
156 CollectionItem *item,
157 GdkRectangle *area);
158 static void draw_item_small(GtkWidget *widget,
159 CollectionItem *item,
160 GdkRectangle *area);
161 static void draw_item_full_info(GtkWidget *widget,
162 CollectionItem *colitem,
163 GdkRectangle *area);
164 static gboolean test_point_large(Collection *collection,
165 int point_x, int point_y,
166 CollectionItem *item,
167 int width, int height);
168 static gboolean test_point_small(Collection *collection,
169 int point_x, int point_y,
170 CollectionItem *item,
171 int width, int height);
172 static gboolean test_point_full_info(Collection *collection,
173 int point_x, int point_y,
174 CollectionItem *item,
175 int width, int height);
176 static void update_display(Directory *dir,
177 DirAction action,
178 GPtrArray *items,
179 FilerWindow *filer_window);
180 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning);
181 static void shrink_width(FilerWindow *filer_window);
182 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning);
183 static void open_item(Collection *collection,
184 gpointer item_data, int item_number,
185 gpointer user_data);
186 static gboolean minibuffer_show_cb(FilerWindow *filer_window);
187 static FilerWindow *find_filer_window(char *path, FilerWindow *diff);
188 static void filer_set_title(FilerWindow *filer_window);
189 static gboolean exists(FilerWindow *filer_window);
191 static GdkAtom xa_string;
192 enum
194 TARGET_STRING,
195 TARGET_URI_LIST,
198 static GdkCursor *busy_cursor = NULL;
199 static GtkTooltips *tooltips = NULL;
201 void filer_init()
203 xa_string = gdk_atom_intern("STRING", FALSE);
205 options_sections = g_slist_prepend(options_sections, &options);
206 option_register("filer_new_window_on_1", filer_new_window_on_1);
207 option_register("filer_menu_on_2", filer_menu_on_2);
208 option_register("filer_single_click", filer_single_click);
209 option_register("filer_unique_windows", filer_unique_windows);
210 option_register("filer_toolbar", filer_toolbar);
211 option_register("filer_display_style", filer_display_style);
212 option_register("filer_sort_by", filer_sort_by);
214 busy_cursor = gdk_cursor_new(GDK_WATCH);
216 tooltips = gtk_tooltips_new();
219 static gboolean if_deleted(gpointer item, gpointer removed)
221 int i = ((GPtrArray *) removed)->len;
222 DirItem **r = (DirItem **) ((GPtrArray *) removed)->pdata;
223 char *leafname = ((DirItem *) item)->leafname;
225 while (i--)
227 if (strcmp(leafname, r[i]->leafname) == 0)
228 return TRUE;
231 return FALSE;
234 static void update_item(FilerWindow *filer_window, DirItem *item)
236 int i;
237 char *leafname = item->leafname;
239 if (leafname[0] == '.')
241 if (filer_window->show_hidden == FALSE || leafname[1] == '\0'
242 || (leafname[1] == '.' && leafname[2] == '\0'))
243 return;
246 i = collection_find_item(filer_window->collection, item, dir_item_cmp);
248 if (i >= 0)
249 collection_draw_item(filer_window->collection, i, TRUE);
250 else
251 g_warning("Failed to find '%s'\n", item->leafname);
254 static void update_display(Directory *dir,
255 DirAction action,
256 GPtrArray *items,
257 FilerWindow *filer_window)
259 int i;
260 int cursor = filer_window->collection->cursor_item;
261 char *as;
262 Collection *collection = filer_window->collection;
264 switch (action)
266 case DIR_ADD:
267 as = filer_window->auto_select;
269 for (i = 0; i < items->len; i++)
271 DirItem *item = (DirItem *) items->pdata[i];
273 add_item(filer_window, item);
275 if (cursor != -1 || !as)
276 continue;
278 if (strcmp(as, item->leafname) != 0)
279 continue;
281 cursor = collection->number_of_items - 1;
282 if (filer_window->had_cursor)
284 collection_set_cursor_item(collection,
285 cursor);
286 filer_window->mini_cursor_base = cursor;
288 else
289 collection_wink_item(collection,
290 cursor);
293 collection_qsort(filer_window->collection,
294 filer_window->sort_fn);
295 break;
296 case DIR_REMOVE:
297 collection_delete_if(filer_window->collection,
298 if_deleted,
299 items);
300 break;
301 case DIR_START_SCAN:
302 set_scanning_display(filer_window, TRUE);
303 break;
304 case DIR_END_SCAN:
305 if (filer_window->window->window)
306 gdk_window_set_cursor(
307 filer_window->window->window,
308 NULL);
309 shrink_width(filer_window);
310 if (filer_window->had_cursor &&
311 collection->cursor_item == -1)
313 collection_set_cursor_item(collection, 0);
314 filer_window->had_cursor = FALSE;
316 set_scanning_display(filer_window, FALSE);
317 break;
318 case DIR_UPDATE:
319 for (i = 0; i < items->len; i++)
321 DirItem *item = (DirItem *) items->pdata[i];
323 update_item(filer_window, item);
325 collection_qsort(filer_window->collection,
326 filer_window->sort_fn);
327 break;
331 static void attach(FilerWindow *filer_window)
333 gdk_window_set_cursor(filer_window->window->window, busy_cursor);
334 collection_clear(filer_window->collection);
335 filer_window->scanning = TRUE;
336 dir_attach(filer_window->directory, (DirCallback) update_display,
337 filer_window);
338 filer_set_title(filer_window);
341 static void detach(FilerWindow *filer_window)
343 g_return_if_fail(filer_window->directory != NULL);
345 dir_detach(filer_window->directory,
346 (DirCallback) update_display, filer_window);
347 g_fscache_data_unref(dir_cache, filer_window->directory);
348 filer_window->directory = NULL;
351 static void filer_window_destroyed(GtkWidget *widget,
352 FilerWindow *filer_window)
354 all_filer_windows = g_list_remove(all_filer_windows, filer_window);
356 if (window_with_selection == filer_window)
357 window_with_selection = NULL;
358 if (window_with_focus == filer_window)
359 window_with_focus = NULL;
361 if (filer_window->directory)
362 detach(filer_window);
364 g_free(filer_window->auto_select);
365 g_free(filer_window->path);
366 g_free(filer_window);
368 if (--number_of_windows < 1)
369 gtk_main_quit();
372 static int calc_width(FilerWindow *filer_window, DirItem *item)
374 int pix_width = item->image->width;
376 switch (filer_window->display_style)
378 case FULL_INFO:
379 return MAX_ICON_WIDTH + 12 +
380 MAX(item->details_width, item->name_width);
381 case SMALL_ICONS:
382 return SMALL_ICON_WIDTH + 12 + item->name_width;
383 default:
384 return MAX(pix_width, item->name_width) + 4;
388 /* Add a single object to a directory display */
389 static void add_item(FilerWindow *filer_window, DirItem *item)
391 char *leafname = item->leafname;
392 int item_width;
394 if (leafname[0] == '.')
396 if (filer_window->show_hidden == FALSE || leafname[1] == '\0'
397 || (leafname[1] == '.' && leafname[2] == '\0'))
398 return;
401 item_width = calc_width(filer_window, item);
402 if (item_width > filer_window->collection->item_width)
403 collection_set_item_size(filer_window->collection,
404 item_width,
405 filer_window->collection->item_height);
406 collection_insert(filer_window->collection, item);
409 /* Is a point inside an item? */
410 static gboolean test_point_large(Collection *collection,
411 int point_x, int point_y,
412 CollectionItem *colitem,
413 int width, int height)
415 DirItem *item = (DirItem *) colitem->data;
416 int text_height = item_font->ascent + item_font->descent;
417 MaskedPixmap *image = item->image;
418 int image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
419 int image_width = (image->width >> 1) + 2;
420 int text_width = (item->name_width >> 1) + 2;
421 int x_limit;
423 if (point_y < image_y)
424 return FALSE; /* Too high up (don't worry about too low) */
426 if (point_y <= image_y + image->height + 2)
427 x_limit = image_width;
428 else if (point_y > height - text_height - 2)
429 x_limit = text_width;
430 else
431 x_limit = MIN(image_width, text_width);
433 return ABS(point_x - (width >> 1)) < x_limit;
436 static gboolean test_point_full_info(Collection *collection,
437 int point_x, int point_y,
438 CollectionItem *colitem,
439 int width, int height)
441 DirItem *item = (DirItem *) colitem->data;
442 MaskedPixmap *image = item->image;
443 int image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
444 int low_top = height
445 - fixed_font->descent - 2 - fixed_font->ascent;
447 if (point_x < image->width + 2)
448 return point_x > 2 && point_y > image_y;
450 point_x -= MAX_ICON_WIDTH + 8;
452 if (point_y >= low_top)
453 return point_x < item->details_width;
454 if (point_y >= low_top - item_font->ascent - item_font->descent)
455 return point_x < item->name_width;
456 return FALSE;
459 static gboolean test_point_small(Collection *collection,
460 int point_x, int point_y,
461 CollectionItem *colitem,
462 int width, int height)
464 DirItem *item = (DirItem *) colitem->data;
465 MaskedPixmap *image = item->image;
466 int image_y = MAX(0, SMALL_ICON_HEIGHT - image->height);
467 int low_top = height
468 - fixed_font->descent - 2 - item_font->ascent;
469 int iwidth = MIN(SMALL_ICON_WIDTH, image->width);
471 if (point_x < iwidth + 2)
472 return point_x > 2 && point_y > image_y;
474 point_x -= SMALL_ICON_WIDTH + 4;
476 if (point_y >= low_top)
477 return point_x < item->name_width;
478 return FALSE;
481 static void draw_small_icon(GtkWidget *widget,
482 GdkRectangle *area,
483 DirItem *item,
484 gboolean selected)
486 MaskedPixmap *image = item->image;
487 int width = MIN(image->width, SMALL_ICON_WIDTH);
488 int height = MIN(image->height, SMALL_ICON_HEIGHT);
489 int image_x = area->x + ((area->width - width) >> 1);
490 int image_y;
491 GdkGC *gc = selected ? widget->style->white_gc
492 : widget->style->black_gc;
493 if (!item->image)
494 return;
496 gdk_gc_set_clip_mask(gc, item->image->mask);
498 image_y = MAX(0, SMALL_ICON_HEIGHT - image->height);
499 gdk_gc_set_clip_origin(gc, image_x, area->y + image_y);
500 gdk_draw_pixmap(widget->window, gc,
501 item->image->pixmap,
502 0, 0, /* Source x,y */
503 image_x, area->y + image_y, /* Dest x,y */
504 width, height);
506 if (selected)
508 gdk_gc_set_function(gc, GDK_INVERT);
509 gdk_draw_rectangle(widget->window,
511 TRUE, image_x, area->y + image_y,
512 width, height);
513 gdk_gc_set_function(gc, GDK_COPY);
516 if (item->flags & ITEM_FLAG_SYMLINK)
518 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
519 gdk_gc_set_clip_mask(gc,
520 default_pixmap[TYPE_SYMLINK].mask);
521 gdk_draw_pixmap(widget->window, gc,
522 default_pixmap[TYPE_SYMLINK].pixmap,
523 0, 0, /* Source x,y */
524 image_x, area->y + 8, /* Dest x,y */
525 -1, -1);
527 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
529 int type = item->flags & ITEM_FLAG_MOUNTED
530 ? TYPE_MOUNTED
531 : TYPE_UNMOUNTED;
532 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
533 gdk_gc_set_clip_mask(gc,
534 default_pixmap[type].mask);
535 gdk_draw_pixmap(widget->window, gc,
536 default_pixmap[type].pixmap,
537 0, 0, /* Source x,y */
538 image_x, area->y + 8, /* Dest x,y */
539 -1, -1);
542 gdk_gc_set_clip_mask(gc, NULL);
543 gdk_gc_set_clip_origin(gc, 0, 0);
546 static void draw_large_icon(GtkWidget *widget,
547 GdkRectangle *area,
548 DirItem *item,
549 gboolean selected)
551 MaskedPixmap *image = item->image;
552 int width = MIN(image->width, MAX_ICON_WIDTH);
553 int height = MIN(image->height, MAX_ICON_WIDTH);
554 int image_x = area->x + ((area->width - width) >> 1);
555 int image_y;
556 GdkGC *gc = selected ? widget->style->white_gc
557 : widget->style->black_gc;
559 gdk_gc_set_clip_mask(gc, item->image->mask);
561 image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
562 gdk_gc_set_clip_origin(gc, image_x, area->y + image_y);
563 gdk_draw_pixmap(widget->window, gc,
564 item->image->pixmap,
565 0, 0, /* Source x,y */
566 image_x, area->y + image_y, /* Dest x,y */
567 width, height);
569 if (selected)
571 gdk_gc_set_function(gc, GDK_INVERT);
572 gdk_draw_rectangle(widget->window,
574 TRUE, image_x, area->y + image_y,
575 width, height);
576 gdk_gc_set_function(gc, GDK_COPY);
579 if (item->flags & ITEM_FLAG_SYMLINK)
581 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
582 gdk_gc_set_clip_mask(gc,
583 default_pixmap[TYPE_SYMLINK].mask);
584 gdk_draw_pixmap(widget->window, gc,
585 default_pixmap[TYPE_SYMLINK].pixmap,
586 0, 0, /* Source x,y */
587 image_x, area->y + 8, /* Dest x,y */
588 -1, -1);
590 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
592 int type = item->flags & ITEM_FLAG_MOUNTED
593 ? TYPE_MOUNTED
594 : TYPE_UNMOUNTED;
595 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
596 gdk_gc_set_clip_mask(gc,
597 default_pixmap[type].mask);
598 gdk_draw_pixmap(widget->window, gc,
599 default_pixmap[type].pixmap,
600 0, 0, /* Source x,y */
601 image_x, area->y + 8, /* Dest x,y */
602 -1, -1);
605 gdk_gc_set_clip_mask(gc, NULL);
606 gdk_gc_set_clip_origin(gc, 0, 0);
609 static void draw_string(GtkWidget *widget,
610 GdkFont *font,
611 char *string,
612 int x,
613 int y,
614 int width,
615 gboolean selected)
617 int text_height = font->ascent + font->descent;
619 if (selected)
620 gtk_paint_flat_box(widget->style, widget->window,
621 GTK_STATE_SELECTED, GTK_SHADOW_NONE,
622 NULL, widget, "text",
623 x, y - font->ascent,
624 width,
625 text_height);
627 gdk_draw_text(widget->window,
628 font,
629 selected ? widget->style->white_gc
630 : widget->style->black_gc,
631 x, y,
632 string, strlen(string));
635 /* Return a string (valid until next call) giving details
636 * of this item.
638 char *details(DirItem *item)
640 mode_t m = item->mode;
641 static guchar *buf = NULL;
643 if (buf)
644 g_free(buf);
646 if (item->lstat_errno)
647 buf = g_strdup_printf("lstat(2) failed: %s",
648 g_strerror(item->lstat_errno));
649 else
650 buf = g_strdup_printf("%s %s %-8.8s %-8.8s %s %s",
651 item->flags & ITEM_FLAG_APPDIR? "App " :
652 S_ISDIR(m) ? "Dir " :
653 S_ISCHR(m) ? "Char" :
654 S_ISBLK(m) ? "Blck" :
655 S_ISLNK(m) ? "Link" :
656 S_ISSOCK(m) ? "Sock" :
657 S_ISFIFO(m) ? "Pipe" : "File",
658 pretty_permissions(m),
659 user_name(item->uid),
660 group_name(item->gid),
661 format_size_aligned(item->size),
662 pretty_time(&item->mtime));
663 return buf;
666 static void draw_item_full_info(GtkWidget *widget,
667 CollectionItem *colitem,
668 GdkRectangle *area)
670 DirItem *item = (DirItem *) colitem->data;
671 MaskedPixmap *image = item->image;
672 int text_x = area->x + MAX_ICON_WIDTH + 8;
673 int low_text_y = area->y + area->height - fixed_font->descent - 2;
674 gboolean selected = colitem->selected;
675 GdkRectangle pic_area;
677 pic_area.x = area->x;
678 pic_area.y = area->y;
679 pic_area.width = image->width + 8;
680 pic_area.height = area->height;
682 draw_large_icon(widget, &pic_area, item, selected);
684 draw_string(widget,
685 item_font,
686 item->leafname,
687 text_x,
688 low_text_y - item_font->descent - fixed_font->ascent,
689 item->name_width,
690 selected);
691 draw_string(widget,
692 fixed_font,
693 details(item),
694 text_x, low_text_y,
695 item->details_width,
696 selected);
698 if (item->lstat_errno)
699 return;
701 /* Underline the effective permissions */
702 gdk_draw_rectangle(widget->window,
703 selected ? widget->style->white_gc
704 : widget->style->black_gc,
705 TRUE,
706 text_x - 1 + fixed_width *
707 (5 + 4 * applicable(item->uid, item->gid)),
708 low_text_y + fixed_font->descent - 1,
709 fixed_width * 3 + 1, 1);
712 static void draw_item_small(GtkWidget *widget,
713 CollectionItem *colitem,
714 GdkRectangle *area)
716 DirItem *item = (DirItem *) colitem->data;
717 int text_x = area->x + SMALL_ICON_WIDTH + 4;
718 int low_text_y = area->y + area->height - item_font->descent - 2;
719 gboolean selected = colitem->selected;
720 GdkRectangle pic_area;
722 pic_area.x = area->x;
723 pic_area.y = area->y;
724 pic_area.width = SMALL_ICON_WIDTH;
725 pic_area.height = SMALL_ICON_HEIGHT;
727 draw_small_icon(widget, &pic_area, item, selected);
729 draw_string(widget,
730 item_font,
731 item->leafname,
732 text_x,
733 low_text_y,
734 item->name_width,
735 selected);
738 static void draw_item_large(GtkWidget *widget,
739 CollectionItem *colitem,
740 GdkRectangle *area)
742 DirItem *item = (DirItem *) colitem->data;
743 int text_x = area->x + ((area->width - item->name_width) >> 1);
744 int text_y = area->y + area->height - item_font->descent - 2;
745 gboolean selected = colitem->selected;
747 draw_large_icon(widget, area, item, selected);
749 draw_string(widget,
750 item_font,
751 item->leafname,
752 text_x, text_y, item->name_width,
753 selected);
756 static void show_menu(Collection *collection, GdkEventButton *event,
757 int item, gpointer user_data)
759 show_filer_menu((FilerWindow *) user_data, event, item);
762 /* Returns TRUE iff the directory still exists. */
763 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning)
765 Directory *dir;
767 g_return_val_if_fail(filer_window != NULL, FALSE);
769 /* We do a fresh lookup (rather than update) because the inode may
770 * have changed.
772 dir = g_fscache_lookup(dir_cache, filer_window->path);
773 if (!dir)
775 if (warning)
776 delayed_error("ROX-Filer", "Directory missing/deleted");
777 gtk_widget_destroy(filer_window->window);
778 return FALSE;
780 if (dir == filer_window->directory)
781 g_fscache_data_unref(dir_cache, dir);
782 else
784 detach(filer_window);
785 filer_window->directory = dir;
786 attach(filer_window);
789 return TRUE;
792 /* Another app has grabbed the selection */
793 static gint collection_lose_selection(GtkWidget *widget,
794 GdkEventSelection *event)
796 if (window_with_selection &&
797 window_with_selection->collection == COLLECTION(widget))
799 FilerWindow *filer_window = window_with_selection;
800 window_with_selection = NULL;
801 collection_clear_selection(filer_window->collection);
804 return TRUE;
807 /* Someone wants us to send them the selection */
808 static void selection_get(GtkWidget *widget,
809 GtkSelectionData *selection_data,
810 guint info,
811 guint time,
812 gpointer data)
814 GString *reply, *header;
815 FilerWindow *filer_window;
816 int i;
817 Collection *collection;
819 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
821 reply = g_string_new(NULL);
822 header = g_string_new(NULL);
824 switch (info)
826 case TARGET_STRING:
827 g_string_sprintf(header, " %s",
828 make_path(filer_window->path, "")->str);
829 break;
830 case TARGET_URI_LIST:
831 g_string_sprintf(header, " file://%s%s",
832 our_host_name(),
833 make_path(filer_window->path, "")->str);
834 break;
837 collection = filer_window->collection;
838 for (i = 0; i < collection->number_of_items; i++)
840 if (collection->items[i].selected)
842 DirItem *item =
843 (DirItem *) collection->items[i].data;
845 g_string_append(reply, header->str);
846 g_string_append(reply, item->leafname);
849 /* This works, but I don't think I like it... */
850 /* g_string_append_c(reply, ' '); */
852 gtk_selection_data_set(selection_data, xa_string,
853 8, reply->str + 1, reply->len - 1);
854 g_string_free(reply, TRUE);
855 g_string_free(header, TRUE);
858 /* No items are now selected. This might be because another app claimed
859 * the selection or because the user unselected all the items.
861 static void lose_selection(Collection *collection,
862 guint time,
863 gpointer user_data)
865 FilerWindow *filer_window = (FilerWindow *) user_data;
867 if (window_with_selection == filer_window)
869 window_with_selection = NULL;
870 gtk_selection_owner_set(NULL,
871 GDK_SELECTION_PRIMARY,
872 time);
876 static void gain_selection(Collection *collection,
877 guint time,
878 gpointer user_data)
880 FilerWindow *filer_window = (FilerWindow *) user_data;
882 if (gtk_selection_owner_set(GTK_WIDGET(collection),
883 GDK_SELECTION_PRIMARY,
884 time))
886 window_with_selection = filer_window;
888 else
889 collection_clear_selection(filer_window->collection);
892 int sort_by_name(const void *item1, const void *item2)
894 return strcmp((*((DirItem **)item1))->leafname,
895 (*((DirItem **)item2))->leafname);
898 int sort_by_type(const void *item1, const void *item2)
900 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
901 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
902 MIME_type *m1, *m2;
904 int diff = i1->base_type - i2->base_type;
906 if (!diff)
907 diff = (i1->flags & ITEM_FLAG_APPDIR)
908 - (i2->flags & ITEM_FLAG_APPDIR);
909 if (diff)
910 return diff > 0 ? 1 : -1;
912 m1 = i1->mime_type;
913 m2 = i2->mime_type;
915 if (m1 && m2)
917 diff = strcmp(m1->media_type, m2->media_type);
918 if (!diff)
919 diff = strcmp(m1->subtype, m2->subtype);
921 else if (m1 || m2)
922 diff = m1 ? 1 : -1;
923 else
924 diff = 0;
926 if (diff)
927 return diff > 0 ? 1 : -1;
929 return sort_by_name(item1, item2);
932 int sort_by_date(const void *item1, const void *item2)
934 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
935 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
937 return i1->mtime > i2->mtime ? -1 :
938 i1->mtime < i2->mtime ? 1 :
939 sort_by_name(item1, item2);
942 int sort_by_size(const void *item1, const void *item2)
944 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
945 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
947 return i1->size > i2->size ? -1 :
948 i1->size < i2->size ? 1 :
949 sort_by_name(item1, item2);
952 static void open_item(Collection *collection,
953 gpointer item_data, int item_number,
954 gpointer user_data)
956 FilerWindow *filer_window = (FilerWindow *) user_data;
957 GdkEvent *event;
958 GdkEventButton *bevent;
959 GdkEventKey *kevent;
960 OpenFlags flags = 0;
962 event = (GdkEvent *) gtk_get_current_event();
964 bevent = (GdkEventButton *) event;
965 kevent = (GdkEventKey *) event;
967 switch (event->type)
969 case GDK_2BUTTON_PRESS:
970 case GDK_BUTTON_PRESS:
971 case GDK_BUTTON_RELEASE:
972 if (bevent->state & GDK_SHIFT_MASK)
973 flags |= OPEN_SHIFT;
975 if (o_new_window_on_1 ^ (bevent->button == 1))
976 flags |= OPEN_SAME_WINDOW;
978 if (bevent->button != 1)
979 flags |= OPEN_CLOSE_WINDOW;
981 if (o_single_click == FALSE &&
982 (bevent->state & GDK_CONTROL_MASK) != 0)
983 flags ^= OPEN_SAME_WINDOW | OPEN_CLOSE_WINDOW;
984 break;
985 case GDK_KEY_PRESS:
986 flags |= OPEN_SAME_WINDOW;
987 if (kevent->state & GDK_SHIFT_MASK)
988 flags |= OPEN_SHIFT;
989 break;
990 default:
991 break;
994 filer_openitem(filer_window, item_number, flags);
997 /* Return the full path to the directory containing object 'path'.
998 * Relative paths are resolved from the filerwindow's path.
1000 static void follow_symlink(FilerWindow *filer_window, char *path,
1001 gboolean same_window)
1003 char *real, *slash;
1004 char *new_dir;
1006 if (path[0] != '/')
1007 path = make_path(filer_window->path, path)->str;
1009 real = pathdup(path);
1010 slash = strrchr(real, '/');
1011 if (!slash)
1013 g_free(real);
1014 delayed_error("ROX-Filer",
1015 "Broken symlink (or you don't have permission "
1016 "to follow it).");
1017 return;
1020 *slash = '\0';
1022 if (*real)
1023 new_dir = real;
1024 else
1025 new_dir = "/";
1027 if (filer_window->panel_type || !same_window)
1029 FilerWindow *new;
1031 new = filer_opendir(new_dir, PANEL_NO);
1032 filer_set_autoselect(new, slash + 1);
1034 else
1035 filer_change_to(filer_window, new_dir, slash + 1);
1037 g_free(real);
1040 void filer_openitem(FilerWindow *filer_window, int item_number, OpenFlags flags)
1042 gboolean shift = (flags & OPEN_SHIFT) != 0;
1043 gboolean close_mini = flags & OPEN_FROM_MINI;
1044 gboolean same_window = (flags & OPEN_SAME_WINDOW) != 0
1045 && !filer_window->panel_type;
1046 gboolean close_window = (flags & OPEN_CLOSE_WINDOW) != 0
1047 && !filer_window->panel_type;
1048 GtkWidget *widget;
1049 char *full_path;
1050 DirItem *item = (DirItem *)
1051 filer_window->collection->items[item_number].data;
1052 gboolean wink = TRUE, destroy = FALSE;
1054 widget = filer_window->window;
1055 full_path = make_path(filer_window->path,
1056 item->leafname)->str;
1058 if (item->flags & ITEM_FLAG_SYMLINK && shift)
1060 char path[MAXPATHLEN + 1];
1061 int got;
1063 got = readlink(make_path(filer_window->path,
1064 item->leafname)->str,
1065 path, MAXPATHLEN);
1066 if (got < 0)
1067 delayed_error("ROX-Filer", g_strerror(errno));
1068 else
1070 g_return_if_fail(got <= MAXPATHLEN);
1071 path[got] = '\0';
1073 follow_symlink(filer_window, path,
1074 flags & OPEN_SAME_WINDOW);
1076 return;
1079 switch (item->base_type)
1081 case TYPE_DIRECTORY:
1082 if (item->flags & ITEM_FLAG_APPDIR && !shift)
1084 run_app(make_path(filer_window->path,
1085 item->leafname)->str);
1086 if (close_window)
1087 destroy = TRUE;
1088 break;
1091 if (item->flags & ITEM_FLAG_MOUNT_POINT && shift)
1093 action_mount(filer_window, item);
1094 if (item->flags & ITEM_FLAG_MOUNTED)
1095 break;
1098 if (same_window)
1100 wink = FALSE;
1101 filer_change_to(filer_window, full_path, NULL);
1102 close_mini = FALSE;
1104 else
1105 filer_opendir(full_path, PANEL_NO);
1106 break;
1107 case TYPE_FILE:
1108 if ((item->flags & ITEM_FLAG_EXEC_FILE) && !shift)
1110 char *argv[] = {NULL, NULL};
1112 argv[0] = full_path;
1114 if (spawn_full(argv, filer_window->path))
1116 if (close_window)
1117 destroy = TRUE;
1119 else
1120 report_error("ROX-Filer",
1121 "Failed to fork() child");
1123 else
1125 GString *message;
1126 MIME_type *type = shift ? &text_plain
1127 : item->mime_type;
1129 g_return_if_fail(type != NULL);
1131 if (type_open(full_path, type))
1133 if (close_window)
1134 destroy = TRUE;
1136 else
1138 message = g_string_new(NULL);
1139 g_string_sprintf(message, "No open "
1140 "action specified for files of "
1141 "this type (%s/%s)",
1142 type->media_type,
1143 type->subtype);
1144 report_error("ROX-Filer", message->str);
1145 g_string_free(message, TRUE);
1148 break;
1149 default:
1150 report_error("open_item",
1151 "I don't know how to open that");
1152 break;
1155 if (destroy)
1156 gtk_widget_destroy(filer_window->window);
1157 else
1159 if (wink)
1160 collection_wink_item(filer_window->collection,
1161 item_number);
1162 if (close_mini)
1163 minibuffer_hide(filer_window);
1167 static gint pointer_in(GtkWidget *widget,
1168 GdkEventCrossing *event,
1169 FilerWindow *filer_window)
1171 may_rescan(filer_window, TRUE);
1172 return FALSE;
1175 static gint focus_in(GtkWidget *widget,
1176 GdkEventFocus *event,
1177 FilerWindow *filer_window)
1179 window_with_focus = filer_window;
1181 return FALSE;
1184 static gint focus_out(GtkWidget *widget,
1185 GdkEventFocus *event,
1186 FilerWindow *filer_window)
1188 /* TODO: Shade the cursor */
1190 return FALSE;
1193 /* Handle keys that can't be bound with the menu */
1194 static gint key_press_event(GtkWidget *widget,
1195 GdkEventKey *event,
1196 FilerWindow *filer_window)
1198 switch (event->keyval)
1200 case GDK_BackSpace:
1201 change_to_parent(filer_window);
1202 break;
1203 default:
1204 return FALSE;
1207 return TRUE;
1210 static void toolbar_refresh_clicked(GtkWidget *widget,
1211 FilerWindow *filer_window)
1213 full_refresh();
1214 update_dir(filer_window, TRUE);
1217 static void toolbar_home_clicked(GtkWidget *widget, FilerWindow *filer_window)
1219 filer_change_to(filer_window, home_dir, NULL);
1222 static void toolbar_up_clicked(GtkWidget *widget, FilerWindow *filer_window)
1224 change_to_parent(filer_window);
1227 void change_to_parent(FilerWindow *filer_window)
1229 char *copy;
1230 char *slash;
1232 if (filer_window->path[0] == '/' && filer_window->path[1] == '\0')
1233 return; /* Already in the root */
1235 copy = g_strdup(filer_window->path);
1236 slash = strrchr(copy, '/');
1238 if (slash)
1240 *slash = '\0';
1241 filer_change_to(filer_window,
1242 *copy ? copy : "/",
1243 slash + 1);
1245 else
1246 g_warning("No / in directory path!\n");
1248 g_free(copy);
1252 /* Make filer_window display path. When finished, highlight item 'from', or
1253 * the first item if from is NULL. If there is currently no cursor then
1254 * simply wink 'from' (if not NULL).
1256 void filer_change_to(FilerWindow *filer_window, char *path, char *from)
1258 char *from_dup;
1259 char *real_path = pathdup(path);
1261 if (o_unique_filer_windows)
1263 FilerWindow *fw;
1265 fw = find_filer_window(real_path, filer_window);
1266 if (fw)
1267 gtk_widget_destroy(fw->window);
1270 from_dup = from && *from ? g_strdup(from) : NULL;
1272 detach(filer_window);
1273 g_free(filer_window->path);
1274 filer_window->path = real_path;
1276 filer_window->directory = g_fscache_lookup(dir_cache,
1277 filer_window->path);
1278 if (filer_window->directory)
1280 g_free(filer_window->auto_select);
1281 filer_window->had_cursor =
1282 filer_window->collection->cursor_item != -1
1283 || filer_window->had_cursor;
1284 filer_window->auto_select = from_dup;
1286 filer_set_title(filer_window);
1287 collection_set_cursor_item(filer_window->collection, -1);
1288 attach(filer_window);
1290 if (GTK_WIDGET_VISIBLE(filer_window->minibuffer))
1291 gtk_idle_add((GtkFunction) minibuffer_show_cb,
1292 filer_window);
1294 else
1296 char *error;
1298 g_free(from_dup);
1299 error = g_strdup_printf("Directory '%s' is not accessible.",
1300 path);
1301 delayed_error("ROX-Filer", error);
1302 g_free(error);
1303 gtk_widget_destroy(filer_window->window);
1307 int selected_item_number(Collection *collection)
1309 int i;
1311 g_return_val_if_fail(collection != NULL, -1);
1312 g_return_val_if_fail(IS_COLLECTION(collection), -1);
1313 g_return_val_if_fail(collection->number_selected == 1, -1);
1315 for (i = 0; i < collection->number_of_items; i++)
1316 if (collection->items[i].selected)
1317 return i;
1319 g_warning("selected_item: number_selected is wrong\n");
1321 return -1;
1324 DirItem *selected_item(Collection *collection)
1326 int item;
1328 item = selected_item_number(collection);
1330 if (item > -1)
1331 return (DirItem *) collection->items[item].data;
1332 return NULL;
1335 static int filer_confirm_close(GtkWidget *widget, GdkEvent *event,
1336 FilerWindow *window)
1338 /* TODO: We can open lots of these - very irritating! */
1339 return get_choice("Close panel?",
1340 "You have tried to close a panel via the window "
1341 "manager - I usually find that this is accidental... "
1342 "really close?",
1343 2, "Remove", "Cancel") != 0;
1346 /* Make the items as narrow as possible */
1347 static void shrink_width(FilerWindow *filer_window)
1349 int i;
1350 Collection *col = filer_window->collection;
1351 int width = MIN_ITEM_WIDTH;
1352 int this_width;
1353 DisplayStyle style = filer_window->display_style;
1354 int text_height;
1356 text_height = item_font->ascent + item_font->descent;
1358 for (i = 0; i < col->number_of_items; i++)
1360 this_width = calc_width(filer_window,
1361 (DirItem *) col->items[i].data);
1362 if (this_width > width)
1363 width = this_width;
1366 collection_set_item_size(filer_window->collection,
1367 width,
1368 style == FULL_INFO ? MAX_ICON_HEIGHT + 4 :
1369 style == SMALL_ICONS ? MAX(text_height, SMALL_ICON_HEIGHT) + 4
1370 : text_height + MAX_ICON_HEIGHT + 8);
1373 void filer_set_sort_fn(FilerWindow *filer_window,
1374 int (*fn)(const void *a, const void *b))
1376 if (filer_window->sort_fn == fn)
1377 return;
1379 filer_window->sort_fn = fn;
1380 last_sort_fn = fn;
1382 collection_qsort(filer_window->collection,
1383 filer_window->sort_fn);
1385 update_options_label();
1388 void filer_style_set(FilerWindow *filer_window, DisplayStyle style)
1390 if (filer_window->display_style == style)
1391 return;
1393 if (filer_window->panel_type)
1394 style = LARGE_ICONS;
1395 else
1396 last_display_style = style;
1398 filer_window->display_style = style;
1400 switch (style)
1402 case SMALL_ICONS:
1403 collection_set_functions(filer_window->collection,
1404 draw_item_small, test_point_small);
1405 break;
1406 case FULL_INFO:
1407 collection_set_functions(filer_window->collection,
1408 draw_item_full_info, test_point_full_info);
1409 break;
1410 default:
1411 collection_set_functions(filer_window->collection,
1412 draw_item_large, test_point_large);
1413 break;
1416 shrink_width(filer_window);
1418 update_options_label();
1421 FilerWindow *filer_opendir(char *path, PanelType panel_type)
1423 GtkWidget *hbox, *scrollbar, *collection;
1424 FilerWindow *filer_window;
1425 GtkTargetEntry target_table[] =
1427 {"text/uri-list", 0, TARGET_URI_LIST},
1428 {"STRING", 0, TARGET_STRING},
1430 char *real_path;
1432 real_path = pathdup(path);
1434 if (o_unique_filer_windows && panel_type == PANEL_NO)
1436 FilerWindow *fw;
1438 fw = find_filer_window(real_path, NULL);
1440 if (fw)
1442 /* TODO: this should bring the window to the front
1443 * at the same coordinates.
1445 gtk_widget_hide(fw->window);
1446 gtk_widget_show(fw->window);
1447 g_free(real_path);
1448 return fw;
1452 filer_window = g_new(FilerWindow, 1);
1453 filer_window->minibuffer = NULL;
1454 filer_window->path = real_path;
1455 filer_window->scanning = FALSE;
1456 filer_window->had_cursor = FALSE;
1457 filer_window->auto_select = NULL;
1459 filer_window->directory = g_fscache_lookup(dir_cache,
1460 filer_window->path);
1461 if (!filer_window->directory)
1463 char *error;
1465 error = g_strdup_printf("Directory '%s' not found.", path);
1466 delayed_error("ROX-Filer", error);
1467 g_free(error);
1468 g_free(filer_window->path);
1469 g_free(filer_window);
1470 return NULL;
1473 filer_window->show_hidden = last_show_hidden;
1474 filer_window->panel_type = panel_type;
1475 filer_window->temp_item_selected = FALSE;
1476 filer_window->sort_fn = last_sort_fn;
1477 filer_window->flags = (FilerFlags) 0;
1478 filer_window->display_style = UNKNOWN_STYLE;
1480 filer_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1481 filer_set_title(filer_window);
1483 collection = collection_new(NULL);
1484 gtk_object_set_data(GTK_OBJECT(collection),
1485 "filer_window", filer_window);
1486 filer_window->collection = COLLECTION(collection);
1488 gtk_widget_add_events(filer_window->window, GDK_ENTER_NOTIFY);
1489 gtk_signal_connect(GTK_OBJECT(filer_window->window),
1490 "enter-notify-event",
1491 GTK_SIGNAL_FUNC(pointer_in), filer_window);
1492 gtk_signal_connect(GTK_OBJECT(filer_window->window), "focus_in_event",
1493 GTK_SIGNAL_FUNC(focus_in), filer_window);
1494 gtk_signal_connect(GTK_OBJECT(filer_window->window), "focus_out_event",
1495 GTK_SIGNAL_FUNC(focus_out), filer_window);
1496 gtk_signal_connect(GTK_OBJECT(filer_window->window), "destroy",
1497 filer_window_destroyed, filer_window);
1499 gtk_signal_connect(GTK_OBJECT(filer_window->collection), "open_item",
1500 open_item, filer_window);
1501 gtk_signal_connect(GTK_OBJECT(collection), "show_menu",
1502 show_menu, filer_window);
1503 gtk_signal_connect(GTK_OBJECT(collection), "gain_selection",
1504 gain_selection, filer_window);
1505 gtk_signal_connect(GTK_OBJECT(collection), "lose_selection",
1506 lose_selection, filer_window);
1507 gtk_signal_connect(GTK_OBJECT(collection), "drag_selection",
1508 drag_selection, filer_window);
1509 gtk_signal_connect(GTK_OBJECT(collection), "drag_data_get",
1510 drag_data_get, filer_window);
1511 gtk_signal_connect(GTK_OBJECT(collection), "selection_clear_event",
1512 GTK_SIGNAL_FUNC(collection_lose_selection), NULL);
1513 gtk_signal_connect (GTK_OBJECT(collection), "selection_get",
1514 GTK_SIGNAL_FUNC(selection_get), NULL);
1515 gtk_selection_add_targets(collection, GDK_SELECTION_PRIMARY,
1516 target_table,
1517 sizeof(target_table) / sizeof(*target_table));
1519 filer_style_set(filer_window, last_display_style);
1520 drag_set_dest(filer_window);
1522 if (panel_type)
1524 int swidth, sheight, iwidth, iheight;
1525 GtkWidget *frame, *win = filer_window->window;
1527 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Panel",
1528 "ROX-Filer");
1529 collection_set_panel(filer_window->collection, TRUE);
1530 gtk_signal_connect(GTK_OBJECT(filer_window->window),
1531 "delete_event",
1532 GTK_SIGNAL_FUNC(filer_confirm_close),
1533 filer_window);
1535 gdk_window_get_size(GDK_ROOT_PARENT(), &swidth, &sheight);
1536 iwidth = filer_window->collection->item_width;
1537 iheight = filer_window->collection->item_height;
1540 int height = iheight + PANEL_BORDER;
1541 int y = panel_type == PANEL_TOP
1542 ? -PANEL_BORDER
1543 : sheight - height - PANEL_BORDER;
1545 gtk_widget_set_usize(collection, swidth, height);
1546 gtk_widget_set_uposition(win, 0, y);
1549 frame = gtk_frame_new(NULL);
1550 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1551 gtk_container_add(GTK_CONTAINER(frame), collection);
1552 gtk_container_add(GTK_CONTAINER(win), frame);
1554 gtk_widget_show_all(frame);
1555 gtk_widget_realize(win);
1556 if (override_redirect)
1557 gdk_window_set_override_redirect(win->window, TRUE);
1558 make_panel_window(win->window);
1560 else
1562 GtkWidget *vbox;
1563 int col_height = ROW_HEIGHT_LARGE * 3;
1565 gtk_signal_connect(GTK_OBJECT(collection),
1566 "key_press_event",
1567 GTK_SIGNAL_FUNC(key_press_event), filer_window);
1568 gtk_window_set_default_size(GTK_WINDOW(filer_window->window),
1569 filer_window->display_style == LARGE_ICONS ? 400 : 512,
1570 o_toolbar == TOOLBAR_NONE ? col_height:
1571 o_toolbar == TOOLBAR_NORMAL ? col_height + 24 :
1572 col_height + 38);
1574 hbox = gtk_hbox_new(FALSE, 0);
1575 gtk_container_add(GTK_CONTAINER(filer_window->window),
1576 hbox);
1578 vbox = gtk_vbox_new(FALSE, 0);
1579 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1581 if (o_toolbar != TOOLBAR_NONE)
1583 GtkWidget *toolbar;
1585 toolbar = create_toolbar(filer_window);
1586 gtk_box_pack_start(GTK_BOX(vbox), toolbar,
1587 FALSE, TRUE, 0);
1588 gtk_widget_show_all(toolbar);
1591 gtk_box_pack_start(GTK_BOX(vbox), collection, TRUE, TRUE, 0);
1593 filer_window->minibuffer = create_minibuffer(filer_window);
1594 gtk_box_pack_start(GTK_BOX(vbox), filer_window->minibuffer,
1595 FALSE, TRUE, 0);
1597 scrollbar = gtk_vscrollbar_new(COLLECTION(collection)->vadj);
1598 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
1599 gtk_accel_group_attach(filer_keys,
1600 GTK_OBJECT(filer_window->window));
1601 gtk_window_set_focus(GTK_WINDOW(filer_window->window),
1602 collection);
1604 gtk_widget_show(hbox);
1605 gtk_widget_show(vbox);
1606 gtk_widget_show(scrollbar);
1607 gtk_widget_show(collection);
1610 number_of_windows++;
1611 gtk_widget_show(filer_window->window);
1612 attach(filer_window);
1614 all_filer_windows = g_list_prepend(all_filer_windows, filer_window);
1616 return filer_window;
1619 static gint clear_scanning_display(FilerWindow *filer_window)
1621 if (exists(filer_window))
1622 filer_set_title(filer_window);
1623 return FALSE;
1626 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning)
1628 if (scanning == filer_window->scanning)
1629 return;
1630 filer_window->scanning = scanning;
1632 if (scanning)
1633 filer_set_title(filer_window);
1634 else
1635 gtk_timeout_add(300, (GtkFunction) clear_scanning_display,
1636 filer_window);
1639 static GtkWidget *create_toolbar(FilerWindow *filer_window)
1641 GtkWidget *frame, *box;
1643 if (o_toolbar == TOOLBAR_GNOME)
1645 frame = gtk_handle_box_new();
1646 box = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1647 GTK_TOOLBAR_BOTH);
1648 gtk_container_set_border_width(GTK_CONTAINER(box), 2);
1649 gtk_toolbar_set_space_style(GTK_TOOLBAR(box),
1650 GTK_TOOLBAR_SPACE_LINE);
1651 gtk_toolbar_set_button_relief(GTK_TOOLBAR(box),
1652 GTK_RELIEF_NONE);
1654 else
1656 frame = gtk_frame_new(NULL);
1657 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1659 box = gtk_hbutton_box_new();
1660 gtk_button_box_set_child_size_default(16, 16);
1661 gtk_hbutton_box_set_spacing_default(2);
1662 gtk_button_box_set_layout(GTK_BUTTON_BOX(box),
1663 GTK_BUTTONBOX_START);
1666 gtk_container_add(GTK_CONTAINER(frame), box);
1668 add_button(box, TOOLBAR_UP_ICON,
1669 GTK_SIGNAL_FUNC(toolbar_up_clicked),
1670 filer_window,
1671 "Up", "Change to parent directory");
1672 add_button(box, TOOLBAR_HOME_ICON,
1673 GTK_SIGNAL_FUNC(toolbar_home_clicked),
1674 filer_window,
1675 "Home", "Change to home directory");
1676 add_button(box, TOOLBAR_REFRESH_ICON,
1677 GTK_SIGNAL_FUNC(toolbar_refresh_clicked),
1678 filer_window,
1679 "Rescan", "Rescan directory contents");
1681 return frame;
1684 static void add_button(GtkWidget *box, int pixmap,
1685 GtkSignalFunc cb, FilerWindow *filer_window,
1686 char *label, char *tip)
1688 GtkWidget *button, *icon;
1690 icon = gtk_pixmap_new(default_pixmap[pixmap].pixmap,
1691 default_pixmap[pixmap].mask);
1693 if (o_toolbar == TOOLBAR_GNOME)
1695 gtk_toolbar_append_element(GTK_TOOLBAR(box),
1696 GTK_TOOLBAR_CHILD_BUTTON,
1697 NULL,
1698 label,
1699 tip, NULL,
1700 icon,
1701 cb, filer_window);
1703 else
1705 button = gtk_button_new();
1706 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
1708 gtk_container_add(GTK_CONTAINER(button), icon);
1709 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1710 cb, filer_window);
1712 gtk_tooltips_set_tip(tooltips, button, tip, NULL);
1714 gtk_container_add(GTK_CONTAINER(box), button);
1718 /* Build up some option widgets to go in the options dialog, but don't
1719 * fill them in yet.
1721 static GtkWidget *create_options()
1723 GtkWidget *vbox, *menu, *hbox;
1725 vbox = gtk_vbox_new(FALSE, 0);
1726 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
1728 display_label = gtk_label_new("<>");
1729 gtk_label_set_line_wrap(GTK_LABEL(display_label), TRUE);
1730 gtk_box_pack_start(GTK_BOX(vbox), display_label, FALSE, TRUE, 0);
1732 toggle_new_window_on_1 =
1733 gtk_check_button_new_with_label("New window on button 1 "
1734 "(RISC OS style)");
1735 gtk_box_pack_start(GTK_BOX(vbox), toggle_new_window_on_1,
1736 FALSE, TRUE, 0);
1738 toggle_menu_on_2 =
1739 gtk_check_button_new_with_label("Menu on button 2 "
1740 "(RISC OS style)");
1741 gtk_box_pack_start(GTK_BOX(vbox), toggle_menu_on_2, FALSE, TRUE, 0);
1743 toggle_single_click =
1744 gtk_check_button_new_with_label("Single-click nagivation");
1745 gtk_box_pack_start(GTK_BOX(vbox), toggle_single_click, FALSE, TRUE, 0);
1747 toggle_unique_filer_windows =
1748 gtk_check_button_new_with_label("Unique windows");
1749 gtk_box_pack_start(GTK_BOX(vbox), toggle_unique_filer_windows, FALSE, TRUE, 0);
1751 hbox = gtk_hbox_new(FALSE, 4);
1752 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
1754 gtk_box_pack_start(GTK_BOX(hbox),
1755 gtk_label_new("Toolbar type for new windows"),
1756 FALSE, TRUE, 0);
1757 menu_toolbar = gtk_option_menu_new();
1758 menu = gtk_menu_new();
1759 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("None"));
1760 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("Normal"));
1761 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("GNOME"));
1762 gtk_option_menu_set_menu(GTK_OPTION_MENU(menu_toolbar), menu);
1763 gtk_box_pack_start(GTK_BOX(hbox), menu_toolbar, TRUE, TRUE, 0);
1765 return vbox;
1768 static void update_options_label(void)
1770 guchar *str;
1772 str = g_strdup_printf("The last used display style (%s) and sort "
1773 "function (Sort By %s) will be saved if you click on "
1774 "Save.", style_to_name(), sort_fn_to_name());
1775 gtk_label_set_text(GTK_LABEL(display_label), str);
1776 g_free(str);
1779 /* Reflect current state by changing the widgets in the options box */
1780 static void update_options()
1782 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_new_window_on_1),
1783 o_new_window_on_1);
1784 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_menu_on_2),
1785 collection_menu_button == 2 ? 1 : 0);
1786 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_single_click),
1787 o_single_click);
1788 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_unique_filer_windows),
1789 o_unique_filer_windows);
1790 gtk_option_menu_set_history(GTK_OPTION_MENU(menu_toolbar), o_toolbar);
1792 update_options_label();
1795 /* Set current values by reading the states of the widgets in the options box */
1796 static void set_options()
1798 GtkWidget *item, *menu;
1799 GList *list;
1801 o_new_window_on_1 = gtk_toggle_button_get_active(
1802 GTK_TOGGLE_BUTTON(toggle_new_window_on_1));
1804 collection_menu_button = gtk_toggle_button_get_active(
1805 GTK_TOGGLE_BUTTON(toggle_menu_on_2)) ? 2 : 3;
1807 o_single_click = gtk_toggle_button_get_active(
1808 GTK_TOGGLE_BUTTON(toggle_single_click));
1810 o_unique_filer_windows = gtk_toggle_button_get_active(
1811 GTK_TOGGLE_BUTTON(toggle_unique_filer_windows));
1813 collection_single_click = o_single_click ? TRUE : FALSE;
1815 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(menu_toolbar));
1816 item = gtk_menu_get_active(GTK_MENU(menu));
1817 list = gtk_container_children(GTK_CONTAINER(menu));
1818 o_toolbar = (ToolbarType) g_list_index(list, item);
1819 g_list_free(list);
1823 static guchar *style_to_name(void)
1825 return last_display_style == LARGE_ICONS ? "Large Icons" :
1826 last_display_style == SMALL_ICONS ? "Small Icons" :
1827 "Full Info";
1830 static guchar *sort_fn_to_name(void)
1832 return last_sort_fn == sort_by_name ? "Name" :
1833 last_sort_fn == sort_by_type ? "Type" :
1834 last_sort_fn == sort_by_date ? "Date" :
1835 "Size";
1838 static void save_options()
1840 option_write("filer_new_window_on_1", o_new_window_on_1 ? "1" : "0");
1841 option_write("filer_menu_on_2",
1842 collection_menu_button == 2 ? "1" : "0");
1843 option_write("filer_single_click", o_single_click ? "1" : "0");
1844 option_write("filer_unique_windows", o_unique_filer_windows ? "1" : "0");
1845 option_write("filer_display_style", style_to_name());
1846 option_write("filer_sort_by", sort_fn_to_name());
1847 option_write("filer_toolbar", o_toolbar == TOOLBAR_NONE ? "None" :
1848 o_toolbar == TOOLBAR_NORMAL ? "Normal" :
1849 o_toolbar == TOOLBAR_GNOME ? "GNOME" :
1850 "Unknown");
1853 static char *filer_new_window_on_1(char *data)
1855 o_new_window_on_1 = atoi(data) != 0;
1856 return NULL;
1859 static char *filer_menu_on_2(char *data)
1861 collection_menu_button = atoi(data) != 0 ? 2 : 3;
1862 return NULL;
1865 static char *filer_single_click(char *data)
1867 o_single_click = atoi(data) != 0;
1868 collection_single_click = o_single_click ? TRUE : FALSE;
1869 return NULL;
1872 static char *filer_unique_windows(char *data)
1874 o_unique_filer_windows = atoi(data) != 0;
1875 return NULL;
1878 static char *filer_display_style(char *data)
1880 if (g_strcasecmp(data, "Large Icons") == 0)
1881 last_display_style = LARGE_ICONS;
1882 else if (g_strcasecmp(data, "Small Icons") == 0)
1883 last_display_style = SMALL_ICONS;
1884 else if (g_strcasecmp(data, "Full Info") == 0)
1885 last_display_style = FULL_INFO;
1886 else
1887 return "Unknown display style";
1889 return NULL;
1892 static char *filer_sort_by(char *data)
1894 if (g_strcasecmp(data, "Name") == 0)
1895 last_sort_fn = sort_by_name;
1896 else if (g_strcasecmp(data, "Type") == 0)
1897 last_sort_fn = sort_by_type;
1898 else if (g_strcasecmp(data, "Date") == 0)
1899 last_sort_fn = sort_by_date;
1900 else if (g_strcasecmp(data, "Size") == 0)
1901 last_sort_fn = sort_by_size;
1902 else
1903 return "Unknown sort type";
1905 return NULL;
1908 static char *filer_toolbar(char *data)
1910 if (g_strcasecmp(data, "None") == 0)
1911 o_toolbar = TOOLBAR_NONE;
1912 else if (g_strcasecmp(data, "Normal") == 0)
1913 o_toolbar = TOOLBAR_NORMAL;
1914 else if (g_strcasecmp(data, "GNOME") == 0)
1915 o_toolbar = TOOLBAR_GNOME;
1916 else
1917 return "Unknown toolbar type";
1919 return NULL;
1922 /* Note that filer_window may not exist after this call. */
1923 void update_dir(FilerWindow *filer_window, gboolean warning)
1925 if (may_rescan(filer_window, warning))
1926 dir_update(filer_window->directory, filer_window->path);
1929 void filer_set_hidden(FilerWindow *filer_window, gboolean hidden)
1931 Directory *dir = filer_window->directory;
1933 if (filer_window->show_hidden == hidden)
1934 return;
1936 filer_window->show_hidden = hidden;
1937 last_show_hidden = hidden;
1939 g_fscache_data_ref(dir_cache, dir);
1940 detach(filer_window);
1941 filer_window->directory = dir;
1942 attach(filer_window);
1945 /* Refresh the various caches even if we don't think we need to */
1946 void full_refresh(void)
1948 mount_update(TRUE);
1951 /* See whether a filer window with a given path already exists
1952 * and is different from diff.
1954 static FilerWindow *find_filer_window(char *path, FilerWindow *diff)
1956 GList *next = all_filer_windows;
1958 while (next)
1960 FilerWindow *filer_window = (FilerWindow *) next->data;
1962 if (filer_window->panel_type == PANEL_NO &&
1963 filer_window != diff &&
1964 strcmp(path, filer_window->path) == 0)
1966 return filer_window;
1969 next = next->next;
1972 return NULL;
1975 /* This path has been mounted/umounted/deleted some files - update all dirs */
1976 void filer_check_mounted(char *path)
1978 GList *next = all_filer_windows;
1979 int len;
1981 len = strlen(path);
1983 while (next)
1985 FilerWindow *filer_window = (FilerWindow *) next->data;
1987 next = next->next;
1989 if (strncmp(path, filer_window->path, len) == 0)
1991 char s = filer_window->path[len];
1993 if (s == '/' || s == '\0')
1994 update_dir(filer_window, FALSE);
1999 /* Like minibuffer_show(), except that:
2000 * - It returns FALSE (to be used from an idle callback)
2001 * - It checks that the filer window still exists.
2003 static gboolean minibuffer_show_cb(FilerWindow *filer_window)
2005 if (exists(filer_window))
2006 minibuffer_show(filer_window);
2007 return FALSE;
2010 static gboolean exists(FilerWindow *filer_window)
2012 GList *next;
2014 for (next = all_filer_windows; next; next = next->next)
2016 FilerWindow *fw = (FilerWindow *) next->data;
2018 if (fw == filer_window)
2019 return TRUE;
2022 return FALSE;
2025 /* Highlight (wink or cursor) this item in the filer window. If the item
2026 * isn't already there but we're scanning then highlight it if it
2027 * appears later.
2029 void filer_set_autoselect(FilerWindow *filer_window, guchar *leaf)
2031 Collection *col = filer_window->collection;
2032 int i;
2034 g_free(filer_window->auto_select);
2035 filer_window->auto_select = NULL;
2037 for (i = 0; i < col->number_of_items; i++)
2039 DirItem *item = (DirItem *) col->items[i].data;
2041 if (strcmp(item->leafname, leaf) == 0)
2043 if (col->cursor_item != -1)
2044 collection_set_cursor_item(col, i);
2045 else
2046 collection_wink_item(col, i);
2047 return;
2051 filer_window->auto_select = g_strdup(leaf);
2054 static void filer_set_title(FilerWindow *filer_window)
2056 if (filer_window->scanning)
2058 guchar *title;
2060 title = g_strdup_printf("%s (Scanning)", filer_window->path);
2061 gtk_window_set_title(GTK_WINDOW(filer_window->window),
2062 title);
2063 g_free(title);
2065 else
2066 gtk_window_set_title(GTK_WINDOW(filer_window->window),
2067 filer_window->path);