r217: Merged the MIME-Type and Special fields in the info window into a single
[rox-filer.git] / ROX-Filer / src / filer.c
blob9fc23a72e78c8587f5f8a5f7ebb2b2d6d6e90dba
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 void set_autoselect(FilerWindow *filer_window, guchar *leaf);
188 static FilerWindow *find_filer_window(char *path, FilerWindow *diff);
189 static void filer_set_title(FilerWindow *filer_window);
190 static gboolean exists(FilerWindow *filer_window);
192 static GdkAtom xa_string;
193 enum
195 TARGET_STRING,
196 TARGET_URI_LIST,
199 static GdkCursor *busy_cursor = NULL;
200 static GtkTooltips *tooltips = NULL;
202 void filer_init()
204 xa_string = gdk_atom_intern("STRING", FALSE);
206 options_sections = g_slist_prepend(options_sections, &options);
207 option_register("filer_new_window_on_1", filer_new_window_on_1);
208 option_register("filer_menu_on_2", filer_menu_on_2);
209 option_register("filer_single_click", filer_single_click);
210 option_register("filer_unique_windows", filer_unique_windows);
211 option_register("filer_toolbar", filer_toolbar);
212 option_register("filer_display_style", filer_display_style);
213 option_register("filer_sort_by", filer_sort_by);
215 busy_cursor = gdk_cursor_new(GDK_WATCH);
217 tooltips = gtk_tooltips_new();
220 static gboolean if_deleted(gpointer item, gpointer removed)
222 int i = ((GPtrArray *) removed)->len;
223 DirItem **r = (DirItem **) ((GPtrArray *) removed)->pdata;
224 char *leafname = ((DirItem *) item)->leafname;
226 while (i--)
228 if (strcmp(leafname, r[i]->leafname) == 0)
229 return TRUE;
232 return FALSE;
235 static void update_item(FilerWindow *filer_window, DirItem *item)
237 int i;
238 char *leafname = item->leafname;
240 if (leafname[0] == '.')
242 if (filer_window->show_hidden == FALSE || leafname[1] == '\0'
243 || (leafname[1] == '.' && leafname[2] == '\0'))
244 return;
247 i = collection_find_item(filer_window->collection, item, dir_item_cmp);
249 if (i >= 0)
250 collection_draw_item(filer_window->collection, i, TRUE);
251 else
252 g_warning("Failed to find '%s'\n", item->leafname);
255 static void update_display(Directory *dir,
256 DirAction action,
257 GPtrArray *items,
258 FilerWindow *filer_window)
260 int i;
261 int cursor = filer_window->collection->cursor_item;
262 char *as;
263 Collection *collection = filer_window->collection;
265 switch (action)
267 case DIR_ADD:
268 as = filer_window->auto_select;
270 for (i = 0; i < items->len; i++)
272 DirItem *item = (DirItem *) items->pdata[i];
274 add_item(filer_window, item);
276 if (cursor != -1 || !as)
277 continue;
279 if (strcmp(as, item->leafname) != 0)
280 continue;
282 cursor = collection->number_of_items - 1;
283 if (filer_window->had_cursor)
285 collection_set_cursor_item(collection,
286 cursor);
287 filer_window->mini_cursor_base = cursor;
289 else
290 collection_wink_item(collection,
291 cursor);
294 collection_qsort(filer_window->collection,
295 filer_window->sort_fn);
296 break;
297 case DIR_REMOVE:
298 collection_delete_if(filer_window->collection,
299 if_deleted,
300 items);
301 break;
302 case DIR_START_SCAN:
303 set_scanning_display(filer_window, TRUE);
304 break;
305 case DIR_END_SCAN:
306 if (filer_window->window->window)
307 gdk_window_set_cursor(
308 filer_window->window->window,
309 NULL);
310 shrink_width(filer_window);
311 if (filer_window->had_cursor &&
312 collection->cursor_item == -1)
314 collection_set_cursor_item(collection, 0);
315 filer_window->had_cursor = FALSE;
317 set_scanning_display(filer_window, FALSE);
318 break;
319 case DIR_UPDATE:
320 for (i = 0; i < items->len; i++)
322 DirItem *item = (DirItem *) items->pdata[i];
324 update_item(filer_window, item);
326 collection_qsort(filer_window->collection,
327 filer_window->sort_fn);
328 break;
332 static void attach(FilerWindow *filer_window)
334 gdk_window_set_cursor(filer_window->window->window, busy_cursor);
335 collection_clear(filer_window->collection);
336 filer_window->scanning = TRUE;
337 dir_attach(filer_window->directory, (DirCallback) update_display,
338 filer_window);
339 filer_set_title(filer_window);
342 static void detach(FilerWindow *filer_window)
344 g_return_if_fail(filer_window->directory != NULL);
346 dir_detach(filer_window->directory,
347 (DirCallback) update_display, filer_window);
348 g_fscache_data_unref(dir_cache, filer_window->directory);
349 filer_window->directory = NULL;
352 static void filer_window_destroyed(GtkWidget *widget,
353 FilerWindow *filer_window)
355 all_filer_windows = g_list_remove(all_filer_windows, filer_window);
357 if (window_with_selection == filer_window)
358 window_with_selection = NULL;
359 if (window_with_focus == filer_window)
360 window_with_focus = NULL;
362 if (filer_window->directory)
363 detach(filer_window);
365 g_free(filer_window->auto_select);
366 g_free(filer_window->path);
367 g_free(filer_window);
369 if (--number_of_windows < 1)
370 gtk_main_quit();
373 static int calc_width(FilerWindow *filer_window, DirItem *item)
375 int pix_width = item->image->width;
377 switch (filer_window->display_style)
379 case FULL_INFO:
380 return MAX_ICON_WIDTH + 12 +
381 MAX(item->details_width, item->name_width);
382 case SMALL_ICONS:
383 return SMALL_ICON_WIDTH + 12 + item->name_width;
384 default:
385 return MAX(pix_width, item->name_width) + 4;
389 /* Add a single object to a directory display */
390 static void add_item(FilerWindow *filer_window, DirItem *item)
392 char *leafname = item->leafname;
393 int item_width;
395 if (leafname[0] == '.')
397 if (filer_window->show_hidden == FALSE || leafname[1] == '\0'
398 || (leafname[1] == '.' && leafname[2] == '\0'))
399 return;
402 item_width = calc_width(filer_window, item);
403 if (item_width > filer_window->collection->item_width)
404 collection_set_item_size(filer_window->collection,
405 item_width,
406 filer_window->collection->item_height);
407 collection_insert(filer_window->collection, item);
410 /* Is a point inside an item? */
411 static gboolean test_point_large(Collection *collection,
412 int point_x, int point_y,
413 CollectionItem *colitem,
414 int width, int height)
416 DirItem *item = (DirItem *) colitem->data;
417 int text_height = item_font->ascent + item_font->descent;
418 MaskedPixmap *image = item->image;
419 int image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
420 int image_width = (image->width >> 1) + 2;
421 int text_width = (item->name_width >> 1) + 2;
422 int x_limit;
424 if (point_y < image_y)
425 return FALSE; /* Too high up (don't worry about too low) */
427 if (point_y <= image_y + image->height + 2)
428 x_limit = image_width;
429 else if (point_y > height - text_height - 2)
430 x_limit = text_width;
431 else
432 x_limit = MIN(image_width, text_width);
434 return ABS(point_x - (width >> 1)) < x_limit;
437 static gboolean test_point_full_info(Collection *collection,
438 int point_x, int point_y,
439 CollectionItem *colitem,
440 int width, int height)
442 DirItem *item = (DirItem *) colitem->data;
443 MaskedPixmap *image = item->image;
444 int image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
445 int low_top = height
446 - fixed_font->descent - 2 - fixed_font->ascent;
448 if (point_x < image->width + 2)
449 return point_x > 2 && point_y > image_y;
451 point_x -= MAX_ICON_WIDTH + 8;
453 if (point_y >= low_top)
454 return point_x < item->details_width;
455 if (point_y >= low_top - item_font->ascent - item_font->descent)
456 return point_x < item->name_width;
457 return FALSE;
460 static gboolean test_point_small(Collection *collection,
461 int point_x, int point_y,
462 CollectionItem *colitem,
463 int width, int height)
465 DirItem *item = (DirItem *) colitem->data;
466 MaskedPixmap *image = item->image;
467 int image_y = MAX(0, SMALL_ICON_HEIGHT - image->height);
468 int low_top = height
469 - fixed_font->descent - 2 - item_font->ascent;
470 int iwidth = MIN(SMALL_ICON_WIDTH, image->width);
472 if (point_x < iwidth + 2)
473 return point_x > 2 && point_y > image_y;
475 point_x -= SMALL_ICON_WIDTH + 4;
477 if (point_y >= low_top)
478 return point_x < item->name_width;
479 return FALSE;
482 static void draw_small_icon(GtkWidget *widget,
483 GdkRectangle *area,
484 DirItem *item,
485 gboolean selected)
487 MaskedPixmap *image = item->image;
488 int width = MIN(image->width, SMALL_ICON_WIDTH);
489 int height = MIN(image->height, SMALL_ICON_HEIGHT);
490 int image_x = area->x + ((area->width - width) >> 1);
491 int image_y;
492 GdkGC *gc = selected ? widget->style->white_gc
493 : widget->style->black_gc;
494 if (!item->image)
495 return;
497 gdk_gc_set_clip_mask(gc, item->image->mask);
499 image_y = MAX(0, SMALL_ICON_HEIGHT - image->height);
500 gdk_gc_set_clip_origin(gc, image_x, area->y + image_y);
501 gdk_draw_pixmap(widget->window, gc,
502 item->image->pixmap,
503 0, 0, /* Source x,y */
504 image_x, area->y + image_y, /* Dest x,y */
505 width, height);
507 if (selected)
509 gdk_gc_set_function(gc, GDK_INVERT);
510 gdk_draw_rectangle(widget->window,
512 TRUE, image_x, area->y + image_y,
513 width, height);
514 gdk_gc_set_function(gc, GDK_COPY);
517 if (item->flags & ITEM_FLAG_SYMLINK)
519 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
520 gdk_gc_set_clip_mask(gc,
521 default_pixmap[TYPE_SYMLINK].mask);
522 gdk_draw_pixmap(widget->window, gc,
523 default_pixmap[TYPE_SYMLINK].pixmap,
524 0, 0, /* Source x,y */
525 image_x, area->y + 8, /* Dest x,y */
526 -1, -1);
528 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
530 int type = item->flags & ITEM_FLAG_MOUNTED
531 ? TYPE_MOUNTED
532 : TYPE_UNMOUNTED;
533 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
534 gdk_gc_set_clip_mask(gc,
535 default_pixmap[type].mask);
536 gdk_draw_pixmap(widget->window, gc,
537 default_pixmap[type].pixmap,
538 0, 0, /* Source x,y */
539 image_x, area->y + 8, /* Dest x,y */
540 -1, -1);
543 gdk_gc_set_clip_mask(gc, NULL);
544 gdk_gc_set_clip_origin(gc, 0, 0);
547 static void draw_large_icon(GtkWidget *widget,
548 GdkRectangle *area,
549 DirItem *item,
550 gboolean selected)
552 MaskedPixmap *image = item->image;
553 int width = MIN(image->width, MAX_ICON_WIDTH);
554 int height = MIN(image->height, MAX_ICON_WIDTH);
555 int image_x = area->x + ((area->width - width) >> 1);
556 int image_y;
557 GdkGC *gc = selected ? widget->style->white_gc
558 : widget->style->black_gc;
560 gdk_gc_set_clip_mask(gc, item->image->mask);
562 image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
563 gdk_gc_set_clip_origin(gc, image_x, area->y + image_y);
564 gdk_draw_pixmap(widget->window, gc,
565 item->image->pixmap,
566 0, 0, /* Source x,y */
567 image_x, area->y + image_y, /* Dest x,y */
568 width, height);
570 if (selected)
572 gdk_gc_set_function(gc, GDK_INVERT);
573 gdk_draw_rectangle(widget->window,
575 TRUE, image_x, area->y + image_y,
576 width, height);
577 gdk_gc_set_function(gc, GDK_COPY);
580 if (item->flags & ITEM_FLAG_SYMLINK)
582 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
583 gdk_gc_set_clip_mask(gc,
584 default_pixmap[TYPE_SYMLINK].mask);
585 gdk_draw_pixmap(widget->window, gc,
586 default_pixmap[TYPE_SYMLINK].pixmap,
587 0, 0, /* Source x,y */
588 image_x, area->y + 8, /* Dest x,y */
589 -1, -1);
591 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
593 int type = item->flags & ITEM_FLAG_MOUNTED
594 ? TYPE_MOUNTED
595 : TYPE_UNMOUNTED;
596 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
597 gdk_gc_set_clip_mask(gc,
598 default_pixmap[type].mask);
599 gdk_draw_pixmap(widget->window, gc,
600 default_pixmap[type].pixmap,
601 0, 0, /* Source x,y */
602 image_x, area->y + 8, /* Dest x,y */
603 -1, -1);
606 gdk_gc_set_clip_mask(gc, NULL);
607 gdk_gc_set_clip_origin(gc, 0, 0);
610 static void draw_string(GtkWidget *widget,
611 GdkFont *font,
612 char *string,
613 int x,
614 int y,
615 int width,
616 gboolean selected)
618 int text_height = font->ascent + font->descent;
620 if (selected)
621 gtk_paint_flat_box(widget->style, widget->window,
622 GTK_STATE_SELECTED, GTK_SHADOW_NONE,
623 NULL, widget, "text",
624 x, y - font->ascent,
625 width,
626 text_height);
628 gdk_draw_text(widget->window,
629 font,
630 selected ? widget->style->white_gc
631 : widget->style->black_gc,
632 x, y,
633 string, strlen(string));
636 /* Return a string (valid until next call) giving details
637 * of this item.
639 char *details(DirItem *item)
641 mode_t m = item->mode;
642 static guchar *buf = NULL;
644 if (buf)
645 g_free(buf);
647 if (item->lstat_errno)
648 buf = g_strdup_printf("lstat(2) failed: %s",
649 g_strerror(item->lstat_errno));
650 else
651 buf = g_strdup_printf("%s %s %-8.8s %-8.8s %s %s",
652 item->flags & ITEM_FLAG_APPDIR? "App " :
653 S_ISDIR(m) ? "Dir " :
654 S_ISCHR(m) ? "Char" :
655 S_ISBLK(m) ? "Blck" :
656 S_ISLNK(m) ? "Link" :
657 S_ISSOCK(m) ? "Sock" :
658 S_ISFIFO(m) ? "Pipe" : "File",
659 pretty_permissions(m),
660 user_name(item->uid),
661 group_name(item->gid),
662 format_size_aligned(item->size),
663 pretty_time(&item->mtime));
664 return buf;
667 static void draw_item_full_info(GtkWidget *widget,
668 CollectionItem *colitem,
669 GdkRectangle *area)
671 DirItem *item = (DirItem *) colitem->data;
672 MaskedPixmap *image = item->image;
673 int text_x = area->x + MAX_ICON_WIDTH + 8;
674 int low_text_y = area->y + area->height - fixed_font->descent - 2;
675 gboolean selected = colitem->selected;
676 GdkRectangle pic_area;
678 pic_area.x = area->x;
679 pic_area.y = area->y;
680 pic_area.width = image->width + 8;
681 pic_area.height = area->height;
683 draw_large_icon(widget, &pic_area, item, selected);
685 draw_string(widget,
686 item_font,
687 item->leafname,
688 text_x,
689 low_text_y - item_font->descent - fixed_font->ascent,
690 item->name_width,
691 selected);
692 draw_string(widget,
693 fixed_font,
694 details(item),
695 text_x, low_text_y,
696 item->details_width,
697 selected);
699 if (item->lstat_errno)
700 return;
702 /* Underline the effective permissions */
703 gdk_draw_rectangle(widget->window,
704 selected ? widget->style->white_gc
705 : widget->style->black_gc,
706 TRUE,
707 text_x - 1 + fixed_width *
708 (5 + 4 * applicable(item->uid, item->gid)),
709 low_text_y + fixed_font->descent - 1,
710 fixed_width * 3 + 1, 1);
713 static void draw_item_small(GtkWidget *widget,
714 CollectionItem *colitem,
715 GdkRectangle *area)
717 DirItem *item = (DirItem *) colitem->data;
718 int text_x = area->x + SMALL_ICON_WIDTH + 4;
719 int low_text_y = area->y + area->height - item_font->descent - 2;
720 gboolean selected = colitem->selected;
721 GdkRectangle pic_area;
723 pic_area.x = area->x;
724 pic_area.y = area->y;
725 pic_area.width = SMALL_ICON_WIDTH;
726 pic_area.height = SMALL_ICON_HEIGHT;
728 draw_small_icon(widget, &pic_area, item, selected);
730 draw_string(widget,
731 item_font,
732 item->leafname,
733 text_x,
734 low_text_y,
735 item->name_width,
736 selected);
739 static void draw_item_large(GtkWidget *widget,
740 CollectionItem *colitem,
741 GdkRectangle *area)
743 DirItem *item = (DirItem *) colitem->data;
744 int text_x = area->x + ((area->width - item->name_width) >> 1);
745 int text_y = area->y + area->height - item_font->descent - 2;
746 gboolean selected = colitem->selected;
748 draw_large_icon(widget, area, item, selected);
750 draw_string(widget,
751 item_font,
752 item->leafname,
753 text_x, text_y, item->name_width,
754 selected);
757 static void show_menu(Collection *collection, GdkEventButton *event,
758 int item, gpointer user_data)
760 show_filer_menu((FilerWindow *) user_data, event, item);
763 /* Returns TRUE iff the directory still exists. */
764 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning)
766 Directory *dir;
768 g_return_val_if_fail(filer_window != NULL, FALSE);
770 /* We do a fresh lookup (rather than update) because the inode may
771 * have changed.
773 dir = g_fscache_lookup(dir_cache, filer_window->path);
774 if (!dir)
776 if (warning)
777 delayed_error("ROX-Filer", "Directory missing/deleted");
778 gtk_widget_destroy(filer_window->window);
779 return FALSE;
781 if (dir == filer_window->directory)
782 g_fscache_data_unref(dir_cache, dir);
783 else
785 detach(filer_window);
786 filer_window->directory = dir;
787 attach(filer_window);
790 return TRUE;
793 /* Another app has grabbed the selection */
794 static gint collection_lose_selection(GtkWidget *widget,
795 GdkEventSelection *event)
797 if (window_with_selection &&
798 window_with_selection->collection == COLLECTION(widget))
800 FilerWindow *filer_window = window_with_selection;
801 window_with_selection = NULL;
802 collection_clear_selection(filer_window->collection);
805 return TRUE;
808 /* Someone wants us to send them the selection */
809 static void selection_get(GtkWidget *widget,
810 GtkSelectionData *selection_data,
811 guint info,
812 guint time,
813 gpointer data)
815 GString *reply, *header;
816 FilerWindow *filer_window;
817 int i;
818 Collection *collection;
820 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
822 reply = g_string_new(NULL);
823 header = g_string_new(NULL);
825 switch (info)
827 case TARGET_STRING:
828 g_string_sprintf(header, " %s",
829 make_path(filer_window->path, "")->str);
830 break;
831 case TARGET_URI_LIST:
832 g_string_sprintf(header, " file://%s%s",
833 our_host_name(),
834 make_path(filer_window->path, "")->str);
835 break;
838 collection = filer_window->collection;
839 for (i = 0; i < collection->number_of_items; i++)
841 if (collection->items[i].selected)
843 DirItem *item =
844 (DirItem *) collection->items[i].data;
846 g_string_append(reply, header->str);
847 g_string_append(reply, item->leafname);
850 /* This works, but I don't think I like it... */
851 /* g_string_append_c(reply, ' '); */
853 gtk_selection_data_set(selection_data, xa_string,
854 8, reply->str + 1, reply->len - 1);
855 g_string_free(reply, TRUE);
856 g_string_free(header, TRUE);
859 /* No items are now selected. This might be because another app claimed
860 * the selection or because the user unselected all the items.
862 static void lose_selection(Collection *collection,
863 guint time,
864 gpointer user_data)
866 FilerWindow *filer_window = (FilerWindow *) user_data;
868 if (window_with_selection == filer_window)
870 window_with_selection = NULL;
871 gtk_selection_owner_set(NULL,
872 GDK_SELECTION_PRIMARY,
873 time);
877 static void gain_selection(Collection *collection,
878 guint time,
879 gpointer user_data)
881 FilerWindow *filer_window = (FilerWindow *) user_data;
883 if (gtk_selection_owner_set(GTK_WIDGET(collection),
884 GDK_SELECTION_PRIMARY,
885 time))
887 window_with_selection = filer_window;
889 else
890 collection_clear_selection(filer_window->collection);
893 int sort_by_name(const void *item1, const void *item2)
895 return strcmp((*((DirItem **)item1))->leafname,
896 (*((DirItem **)item2))->leafname);
899 int sort_by_type(const void *item1, const void *item2)
901 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
902 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
903 MIME_type *m1, *m2;
905 int diff = i1->base_type - i2->base_type;
907 if (!diff)
908 diff = (i1->flags & ITEM_FLAG_APPDIR)
909 - (i2->flags & ITEM_FLAG_APPDIR);
910 if (diff)
911 return diff > 0 ? 1 : -1;
913 m1 = i1->mime_type;
914 m2 = i2->mime_type;
916 if (m1 && m2)
918 diff = strcmp(m1->media_type, m2->media_type);
919 if (!diff)
920 diff = strcmp(m1->subtype, m2->subtype);
922 else if (m1 || m2)
923 diff = m1 ? 1 : -1;
924 else
925 diff = 0;
927 if (diff)
928 return diff > 0 ? 1 : -1;
930 return sort_by_name(item1, item2);
933 int sort_by_date(const void *item1, const void *item2)
935 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
936 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
938 return i1->mtime > i2->mtime ? -1 :
939 i1->mtime < i2->mtime ? 1 :
940 sort_by_name(item1, item2);
943 int sort_by_size(const void *item1, const void *item2)
945 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
946 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
948 return i1->size > i2->size ? -1 :
949 i1->size < i2->size ? 1 :
950 sort_by_name(item1, item2);
953 static void open_item(Collection *collection,
954 gpointer item_data, int item_number,
955 gpointer user_data)
957 FilerWindow *filer_window = (FilerWindow *) user_data;
958 GdkEvent *event;
959 GdkEventButton *bevent;
960 GdkEventKey *kevent;
961 OpenFlags flags = 0;
963 event = (GdkEvent *) gtk_get_current_event();
965 bevent = (GdkEventButton *) event;
966 kevent = (GdkEventKey *) event;
968 switch (event->type)
970 case GDK_2BUTTON_PRESS:
971 case GDK_BUTTON_PRESS:
972 case GDK_BUTTON_RELEASE:
973 if (bevent->state & GDK_SHIFT_MASK)
974 flags |= OPEN_SHIFT;
976 if (o_new_window_on_1 ^ (bevent->button == 1))
977 flags |= OPEN_SAME_WINDOW;
979 if (bevent->button != 1)
980 flags |= OPEN_CLOSE_WINDOW;
982 if (o_single_click == FALSE &&
983 (bevent->state & GDK_CONTROL_MASK) != 0)
984 flags ^= OPEN_SAME_WINDOW | OPEN_CLOSE_WINDOW;
985 break;
986 case GDK_KEY_PRESS:
987 flags |= OPEN_SAME_WINDOW;
988 if (kevent->state & GDK_SHIFT_MASK)
989 flags |= OPEN_SHIFT;
990 break;
991 default:
992 break;
995 filer_openitem(filer_window, item_number, flags);
998 /* Return the full path to the directory containing object 'path'.
999 * Relative paths are resolved from the filerwindow's path.
1001 static void follow_symlink(FilerWindow *filer_window, char *path,
1002 gboolean same_window)
1004 char *real, *slash;
1005 char *new_dir;
1007 if (path[0] != '/')
1008 path = make_path(filer_window->path, path)->str;
1010 real = pathdup(path);
1011 slash = strrchr(real, '/');
1012 if (!slash)
1014 g_free(real);
1015 delayed_error("ROX-Filer",
1016 "Broken symlink (or you don't have permission "
1017 "to follow it).");
1018 return;
1021 *slash = '\0';
1023 if (*real)
1024 new_dir = real;
1025 else
1026 new_dir = "/";
1028 if (filer_window->panel_type || !same_window)
1030 FilerWindow *new;
1032 new = filer_opendir(new_dir, PANEL_NO);
1033 set_autoselect(new, slash + 1);
1035 else
1036 filer_change_to(filer_window, new_dir, slash + 1);
1038 g_free(real);
1041 void filer_openitem(FilerWindow *filer_window, int item_number, OpenFlags flags)
1043 gboolean shift = (flags & OPEN_SHIFT) != 0;
1044 gboolean close_mini = flags & OPEN_FROM_MINI;
1045 gboolean same_window = (flags & OPEN_SAME_WINDOW) != 0
1046 && !filer_window->panel_type;
1047 gboolean close_window = (flags & OPEN_CLOSE_WINDOW) != 0
1048 && !filer_window->panel_type;
1049 GtkWidget *widget;
1050 char *full_path;
1051 DirItem *item = (DirItem *)
1052 filer_window->collection->items[item_number].data;
1053 gboolean wink = TRUE, destroy = FALSE;
1055 widget = filer_window->window;
1056 full_path = make_path(filer_window->path,
1057 item->leafname)->str;
1059 if (item->flags & ITEM_FLAG_SYMLINK && shift)
1061 char path[MAXPATHLEN + 1];
1062 int got;
1064 got = readlink(make_path(filer_window->path,
1065 item->leafname)->str,
1066 path, MAXPATHLEN);
1067 if (got < 0)
1068 delayed_error("ROX-Filer", g_strerror(errno));
1069 else
1071 g_return_if_fail(got <= MAXPATHLEN);
1072 path[got] = '\0';
1074 follow_symlink(filer_window, path,
1075 flags & OPEN_SAME_WINDOW);
1077 return;
1080 switch (item->base_type)
1082 case TYPE_DIRECTORY:
1083 if (item->flags & ITEM_FLAG_APPDIR && !shift)
1085 run_app(make_path(filer_window->path,
1086 item->leafname)->str);
1087 if (close_window)
1088 destroy = TRUE;
1089 break;
1092 if (item->flags & ITEM_FLAG_MOUNT_POINT && shift)
1094 action_mount(filer_window, item);
1095 if (item->flags & ITEM_FLAG_MOUNTED)
1096 break;
1099 if (same_window)
1101 wink = FALSE;
1102 filer_change_to(filer_window, full_path, NULL);
1103 close_mini = FALSE;
1105 else
1106 filer_opendir(full_path, PANEL_NO);
1107 break;
1108 case TYPE_FILE:
1109 if ((item->flags & ITEM_FLAG_EXEC_FILE) && !shift)
1111 char *argv[] = {NULL, NULL};
1113 argv[0] = full_path;
1115 if (spawn_full(argv, filer_window->path))
1117 if (close_window)
1118 destroy = TRUE;
1120 else
1121 report_error("ROX-Filer",
1122 "Failed to fork() child");
1124 else
1126 GString *message;
1127 MIME_type *type = shift ? &text_plain
1128 : item->mime_type;
1130 g_return_if_fail(type != NULL);
1132 if (type_open(full_path, type))
1134 if (close_window)
1135 destroy = TRUE;
1137 else
1139 message = g_string_new(NULL);
1140 g_string_sprintf(message, "No open "
1141 "action specified for files of "
1142 "this type (%s/%s)",
1143 type->media_type,
1144 type->subtype);
1145 report_error("ROX-Filer", message->str);
1146 g_string_free(message, TRUE);
1149 break;
1150 default:
1151 report_error("open_item",
1152 "I don't know how to open that");
1153 break;
1156 if (destroy)
1157 gtk_widget_destroy(filer_window->window);
1158 else
1160 if (wink)
1161 collection_wink_item(filer_window->collection,
1162 item_number);
1163 if (close_mini)
1164 minibuffer_hide(filer_window);
1168 static gint pointer_in(GtkWidget *widget,
1169 GdkEventCrossing *event,
1170 FilerWindow *filer_window)
1172 may_rescan(filer_window, TRUE);
1173 return FALSE;
1176 static gint focus_in(GtkWidget *widget,
1177 GdkEventFocus *event,
1178 FilerWindow *filer_window)
1180 window_with_focus = filer_window;
1182 return FALSE;
1185 static gint focus_out(GtkWidget *widget,
1186 GdkEventFocus *event,
1187 FilerWindow *filer_window)
1189 /* TODO: Shade the cursor */
1191 return FALSE;
1194 /* Handle keys that can't be bound with the menu */
1195 static gint key_press_event(GtkWidget *widget,
1196 GdkEventKey *event,
1197 FilerWindow *filer_window)
1199 switch (event->keyval)
1201 case GDK_BackSpace:
1202 change_to_parent(filer_window);
1203 break;
1204 default:
1205 return FALSE;
1208 return TRUE;
1211 static void toolbar_refresh_clicked(GtkWidget *widget,
1212 FilerWindow *filer_window)
1214 full_refresh();
1215 update_dir(filer_window, TRUE);
1218 static void toolbar_home_clicked(GtkWidget *widget, FilerWindow *filer_window)
1220 filer_change_to(filer_window, home_dir, NULL);
1223 static void toolbar_up_clicked(GtkWidget *widget, FilerWindow *filer_window)
1225 change_to_parent(filer_window);
1228 void change_to_parent(FilerWindow *filer_window)
1230 char *copy;
1231 char *slash;
1233 if (filer_window->path[0] == '/' && filer_window->path[1] == '\0')
1234 return; /* Already in the root */
1236 copy = g_strdup(filer_window->path);
1237 slash = strrchr(copy, '/');
1239 if (slash)
1241 *slash = '\0';
1242 filer_change_to(filer_window,
1243 *copy ? copy : "/",
1244 slash + 1);
1246 else
1247 g_warning("No / in directory path!\n");
1249 g_free(copy);
1253 /* Make filer_window display path. When finished, highlight item 'from', or
1254 * the first item if from is NULL. If there is currently no cursor then
1255 * simply wink 'from' (if not NULL).
1257 void filer_change_to(FilerWindow *filer_window, char *path, char *from)
1259 char *from_dup;
1260 char *real_path = pathdup(path);
1262 if (o_unique_filer_windows)
1264 FilerWindow *fw;
1266 fw = find_filer_window(real_path, filer_window);
1267 if (fw)
1268 gtk_widget_destroy(fw->window);
1271 from_dup = from && *from ? g_strdup(from) : NULL;
1273 detach(filer_window);
1274 g_free(filer_window->path);
1275 filer_window->path = real_path;
1277 filer_window->directory = g_fscache_lookup(dir_cache,
1278 filer_window->path);
1279 if (filer_window->directory)
1281 g_free(filer_window->auto_select);
1282 filer_window->had_cursor =
1283 filer_window->collection->cursor_item != -1
1284 || filer_window->had_cursor;
1285 filer_window->auto_select = from_dup;
1287 filer_set_title(filer_window);
1288 collection_set_cursor_item(filer_window->collection, -1);
1289 attach(filer_window);
1291 if (GTK_WIDGET_VISIBLE(filer_window->minibuffer))
1292 gtk_idle_add((GtkFunction) minibuffer_show_cb,
1293 filer_window);
1295 else
1297 char *error;
1299 g_free(from_dup);
1300 error = g_strdup_printf("Directory '%s' is not accessible.",
1301 path);
1302 delayed_error("ROX-Filer", error);
1303 g_free(error);
1304 gtk_widget_destroy(filer_window->window);
1308 int selected_item_number(Collection *collection)
1310 int i;
1312 g_return_val_if_fail(collection != NULL, -1);
1313 g_return_val_if_fail(IS_COLLECTION(collection), -1);
1314 g_return_val_if_fail(collection->number_selected == 1, -1);
1316 for (i = 0; i < collection->number_of_items; i++)
1317 if (collection->items[i].selected)
1318 return i;
1320 g_warning("selected_item: number_selected is wrong\n");
1322 return -1;
1325 DirItem *selected_item(Collection *collection)
1327 int item;
1329 item = selected_item_number(collection);
1331 if (item > -1)
1332 return (DirItem *) collection->items[item].data;
1333 return NULL;
1336 static int filer_confirm_close(GtkWidget *widget, GdkEvent *event,
1337 FilerWindow *window)
1339 /* TODO: We can open lots of these - very irritating! */
1340 return get_choice("Close panel?",
1341 "You have tried to close a panel via the window "
1342 "manager - I usually find that this is accidental... "
1343 "really close?",
1344 2, "Remove", "Cancel") != 0;
1347 /* Make the items as narrow as possible */
1348 static void shrink_width(FilerWindow *filer_window)
1350 int i;
1351 Collection *col = filer_window->collection;
1352 int width = MIN_ITEM_WIDTH;
1353 int this_width;
1354 DisplayStyle style = filer_window->display_style;
1355 int text_height;
1357 text_height = item_font->ascent + item_font->descent;
1359 for (i = 0; i < col->number_of_items; i++)
1361 this_width = calc_width(filer_window,
1362 (DirItem *) col->items[i].data);
1363 if (this_width > width)
1364 width = this_width;
1367 collection_set_item_size(filer_window->collection,
1368 width,
1369 style == FULL_INFO ? MAX_ICON_HEIGHT + 4 :
1370 style == SMALL_ICONS ? MAX(text_height, SMALL_ICON_HEIGHT) + 4
1371 : text_height + MAX_ICON_HEIGHT + 8);
1374 void filer_set_sort_fn(FilerWindow *filer_window,
1375 int (*fn)(const void *a, const void *b))
1377 if (filer_window->sort_fn == fn)
1378 return;
1380 filer_window->sort_fn = fn;
1381 last_sort_fn = fn;
1383 collection_qsort(filer_window->collection,
1384 filer_window->sort_fn);
1386 update_options_label();
1389 void filer_style_set(FilerWindow *filer_window, DisplayStyle style)
1391 if (filer_window->display_style == style)
1392 return;
1394 if (filer_window->panel_type)
1395 style = LARGE_ICONS;
1396 else
1397 last_display_style = style;
1399 filer_window->display_style = style;
1401 switch (style)
1403 case SMALL_ICONS:
1404 collection_set_functions(filer_window->collection,
1405 draw_item_small, test_point_small);
1406 break;
1407 case FULL_INFO:
1408 collection_set_functions(filer_window->collection,
1409 draw_item_full_info, test_point_full_info);
1410 break;
1411 default:
1412 collection_set_functions(filer_window->collection,
1413 draw_item_large, test_point_large);
1414 break;
1417 shrink_width(filer_window);
1419 update_options_label();
1422 FilerWindow *filer_opendir(char *path, PanelType panel_type)
1424 GtkWidget *hbox, *scrollbar, *collection;
1425 FilerWindow *filer_window;
1426 GtkTargetEntry target_table[] =
1428 {"text/uri-list", 0, TARGET_URI_LIST},
1429 {"STRING", 0, TARGET_STRING},
1431 char *real_path;
1433 real_path = pathdup(path);
1435 if (o_unique_filer_windows && panel_type == PANEL_NO)
1437 FilerWindow *fw;
1439 fw = find_filer_window(real_path, NULL);
1441 if (fw)
1443 /* TODO: this should bring the window to the front
1444 * at the same coordinates.
1446 gtk_widget_hide(fw->window);
1447 gtk_widget_show(fw->window);
1448 g_free(real_path);
1449 return fw;
1453 filer_window = g_new(FilerWindow, 1);
1454 filer_window->minibuffer = NULL;
1455 filer_window->path = real_path;
1456 filer_window->scanning = FALSE;
1457 filer_window->had_cursor = FALSE;
1458 filer_window->auto_select = NULL;
1460 filer_window->directory = g_fscache_lookup(dir_cache,
1461 filer_window->path);
1462 if (!filer_window->directory)
1464 char *error;
1466 error = g_strdup_printf("Directory '%s' not found.", path);
1467 delayed_error("ROX-Filer", error);
1468 g_free(error);
1469 g_free(filer_window->path);
1470 g_free(filer_window);
1471 return NULL;
1474 filer_window->show_hidden = last_show_hidden;
1475 filer_window->panel_type = panel_type;
1476 filer_window->temp_item_selected = FALSE;
1477 filer_window->sort_fn = last_sort_fn;
1478 filer_window->flags = (FilerFlags) 0;
1479 filer_window->display_style = UNKNOWN_STYLE;
1481 filer_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1482 filer_set_title(filer_window);
1484 collection = collection_new(NULL);
1485 gtk_object_set_data(GTK_OBJECT(collection),
1486 "filer_window", filer_window);
1487 filer_window->collection = COLLECTION(collection);
1489 gtk_widget_add_events(filer_window->window, GDK_ENTER_NOTIFY);
1490 gtk_signal_connect(GTK_OBJECT(filer_window->window),
1491 "enter-notify-event",
1492 GTK_SIGNAL_FUNC(pointer_in), filer_window);
1493 gtk_signal_connect(GTK_OBJECT(filer_window->window), "focus_in_event",
1494 GTK_SIGNAL_FUNC(focus_in), filer_window);
1495 gtk_signal_connect(GTK_OBJECT(filer_window->window), "focus_out_event",
1496 GTK_SIGNAL_FUNC(focus_out), filer_window);
1497 gtk_signal_connect(GTK_OBJECT(filer_window->window), "destroy",
1498 filer_window_destroyed, filer_window);
1500 gtk_signal_connect(GTK_OBJECT(filer_window->collection), "open_item",
1501 open_item, filer_window);
1502 gtk_signal_connect(GTK_OBJECT(collection), "show_menu",
1503 show_menu, filer_window);
1504 gtk_signal_connect(GTK_OBJECT(collection), "gain_selection",
1505 gain_selection, filer_window);
1506 gtk_signal_connect(GTK_OBJECT(collection), "lose_selection",
1507 lose_selection, filer_window);
1508 gtk_signal_connect(GTK_OBJECT(collection), "drag_selection",
1509 drag_selection, filer_window);
1510 gtk_signal_connect(GTK_OBJECT(collection), "drag_data_get",
1511 drag_data_get, filer_window);
1512 gtk_signal_connect(GTK_OBJECT(collection), "selection_clear_event",
1513 GTK_SIGNAL_FUNC(collection_lose_selection), NULL);
1514 gtk_signal_connect (GTK_OBJECT(collection), "selection_get",
1515 GTK_SIGNAL_FUNC(selection_get), NULL);
1516 gtk_selection_add_targets(collection, GDK_SELECTION_PRIMARY,
1517 target_table,
1518 sizeof(target_table) / sizeof(*target_table));
1520 filer_style_set(filer_window, last_display_style);
1521 drag_set_dest(filer_window);
1523 if (panel_type)
1525 int swidth, sheight, iwidth, iheight;
1526 GtkWidget *frame, *win = filer_window->window;
1528 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Panel",
1529 "ROX-Filer");
1530 collection_set_panel(filer_window->collection, TRUE);
1531 gtk_signal_connect(GTK_OBJECT(filer_window->window),
1532 "delete_event",
1533 GTK_SIGNAL_FUNC(filer_confirm_close),
1534 filer_window);
1536 gdk_window_get_size(GDK_ROOT_PARENT(), &swidth, &sheight);
1537 iwidth = filer_window->collection->item_width;
1538 iheight = filer_window->collection->item_height;
1541 int height = iheight + PANEL_BORDER;
1542 int y = panel_type == PANEL_TOP
1543 ? -PANEL_BORDER
1544 : sheight - height - PANEL_BORDER;
1546 gtk_widget_set_usize(collection, swidth, height);
1547 gtk_widget_set_uposition(win, 0, y);
1550 frame = gtk_frame_new(NULL);
1551 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1552 gtk_container_add(GTK_CONTAINER(frame), collection);
1553 gtk_container_add(GTK_CONTAINER(win), frame);
1555 gtk_widget_show_all(frame);
1556 gtk_widget_realize(win);
1557 if (override_redirect)
1558 gdk_window_set_override_redirect(win->window, TRUE);
1559 make_panel_window(win->window);
1561 else
1563 GtkWidget *vbox;
1564 int col_height = ROW_HEIGHT_LARGE * 3;
1566 gtk_signal_connect(GTK_OBJECT(collection),
1567 "key_press_event",
1568 GTK_SIGNAL_FUNC(key_press_event), filer_window);
1569 gtk_window_set_default_size(GTK_WINDOW(filer_window->window),
1570 filer_window->display_style == LARGE_ICONS ? 400 : 512,
1571 o_toolbar == TOOLBAR_NONE ? col_height:
1572 o_toolbar == TOOLBAR_NORMAL ? col_height + 24 :
1573 col_height + 38);
1575 hbox = gtk_hbox_new(FALSE, 0);
1576 gtk_container_add(GTK_CONTAINER(filer_window->window),
1577 hbox);
1579 vbox = gtk_vbox_new(FALSE, 0);
1580 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1582 if (o_toolbar != TOOLBAR_NONE)
1584 GtkWidget *toolbar;
1586 toolbar = create_toolbar(filer_window);
1587 gtk_box_pack_start(GTK_BOX(vbox), toolbar,
1588 FALSE, TRUE, 0);
1589 gtk_widget_show_all(toolbar);
1592 gtk_box_pack_start(GTK_BOX(vbox), collection, TRUE, TRUE, 0);
1594 filer_window->minibuffer = create_minibuffer(filer_window);
1595 gtk_box_pack_start(GTK_BOX(vbox), filer_window->minibuffer,
1596 FALSE, TRUE, 0);
1598 scrollbar = gtk_vscrollbar_new(COLLECTION(collection)->vadj);
1599 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
1600 gtk_accel_group_attach(filer_keys,
1601 GTK_OBJECT(filer_window->window));
1602 gtk_window_set_focus(GTK_WINDOW(filer_window->window),
1603 collection);
1605 gtk_widget_show(hbox);
1606 gtk_widget_show(vbox);
1607 gtk_widget_show(scrollbar);
1608 gtk_widget_show(collection);
1611 number_of_windows++;
1612 gtk_widget_show(filer_window->window);
1613 attach(filer_window);
1615 all_filer_windows = g_list_prepend(all_filer_windows, filer_window);
1617 return filer_window;
1620 static gint clear_scanning_display(FilerWindow *filer_window)
1622 if (exists(filer_window))
1623 filer_set_title(filer_window);
1624 return FALSE;
1627 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning)
1629 if (scanning == filer_window->scanning)
1630 return;
1631 filer_window->scanning = scanning;
1633 if (scanning)
1634 filer_set_title(filer_window);
1635 else
1636 gtk_timeout_add(300, (GtkFunction) clear_scanning_display,
1637 filer_window);
1640 static GtkWidget *create_toolbar(FilerWindow *filer_window)
1642 GtkWidget *frame, *box;
1644 if (o_toolbar == TOOLBAR_GNOME)
1646 frame = gtk_handle_box_new();
1647 box = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1648 GTK_TOOLBAR_BOTH);
1649 gtk_container_set_border_width(GTK_CONTAINER(box), 2);
1650 gtk_toolbar_set_space_style(GTK_TOOLBAR(box),
1651 GTK_TOOLBAR_SPACE_LINE);
1652 gtk_toolbar_set_button_relief(GTK_TOOLBAR(box),
1653 GTK_RELIEF_NONE);
1655 else
1657 frame = gtk_frame_new(NULL);
1658 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1660 box = gtk_hbutton_box_new();
1661 gtk_button_box_set_child_size_default(16, 16);
1662 gtk_hbutton_box_set_spacing_default(2);
1663 gtk_button_box_set_layout(GTK_BUTTON_BOX(box),
1664 GTK_BUTTONBOX_START);
1667 gtk_container_add(GTK_CONTAINER(frame), box);
1669 add_button(box, TOOLBAR_UP_ICON,
1670 GTK_SIGNAL_FUNC(toolbar_up_clicked),
1671 filer_window,
1672 "Up", "Change to parent directory");
1673 add_button(box, TOOLBAR_HOME_ICON,
1674 GTK_SIGNAL_FUNC(toolbar_home_clicked),
1675 filer_window,
1676 "Home", "Change to home directory");
1677 add_button(box, TOOLBAR_REFRESH_ICON,
1678 GTK_SIGNAL_FUNC(toolbar_refresh_clicked),
1679 filer_window,
1680 "Rescan", "Rescan directory contents");
1682 return frame;
1685 static void add_button(GtkWidget *box, int pixmap,
1686 GtkSignalFunc cb, FilerWindow *filer_window,
1687 char *label, char *tip)
1689 GtkWidget *button, *icon;
1691 icon = gtk_pixmap_new(default_pixmap[pixmap].pixmap,
1692 default_pixmap[pixmap].mask);
1694 if (o_toolbar == TOOLBAR_GNOME)
1696 gtk_toolbar_append_element(GTK_TOOLBAR(box),
1697 GTK_TOOLBAR_CHILD_BUTTON,
1698 NULL,
1699 label,
1700 tip, NULL,
1701 icon,
1702 cb, filer_window);
1704 else
1706 button = gtk_button_new();
1707 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
1709 gtk_container_add(GTK_CONTAINER(button), icon);
1710 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1711 cb, filer_window);
1713 gtk_tooltips_set_tip(tooltips, button, tip, NULL);
1715 gtk_container_add(GTK_CONTAINER(box), button);
1719 /* Build up some option widgets to go in the options dialog, but don't
1720 * fill them in yet.
1722 static GtkWidget *create_options()
1724 GtkWidget *vbox, *menu, *hbox;
1726 vbox = gtk_vbox_new(FALSE, 0);
1727 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
1729 display_label = gtk_label_new("<>");
1730 gtk_label_set_line_wrap(GTK_LABEL(display_label), TRUE);
1731 gtk_box_pack_start(GTK_BOX(vbox), display_label, FALSE, TRUE, 0);
1733 toggle_new_window_on_1 =
1734 gtk_check_button_new_with_label("New window on button 1 "
1735 "(RISC OS style)");
1736 gtk_box_pack_start(GTK_BOX(vbox), toggle_new_window_on_1,
1737 FALSE, TRUE, 0);
1739 toggle_menu_on_2 =
1740 gtk_check_button_new_with_label("Menu on button 2 "
1741 "(RISC OS style)");
1742 gtk_box_pack_start(GTK_BOX(vbox), toggle_menu_on_2, FALSE, TRUE, 0);
1744 toggle_single_click =
1745 gtk_check_button_new_with_label("Single-click nagivation");
1746 gtk_box_pack_start(GTK_BOX(vbox), toggle_single_click, FALSE, TRUE, 0);
1748 toggle_unique_filer_windows =
1749 gtk_check_button_new_with_label("Unique windows");
1750 gtk_box_pack_start(GTK_BOX(vbox), toggle_unique_filer_windows, FALSE, TRUE, 0);
1752 hbox = gtk_hbox_new(FALSE, 4);
1753 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
1755 gtk_box_pack_start(GTK_BOX(hbox),
1756 gtk_label_new("Toolbar type for new windows"),
1757 FALSE, TRUE, 0);
1758 menu_toolbar = gtk_option_menu_new();
1759 menu = gtk_menu_new();
1760 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("None"));
1761 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("Normal"));
1762 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("GNOME"));
1763 gtk_option_menu_set_menu(GTK_OPTION_MENU(menu_toolbar), menu);
1764 gtk_box_pack_start(GTK_BOX(hbox), menu_toolbar, TRUE, TRUE, 0);
1766 return vbox;
1769 static void update_options_label(void)
1771 guchar *str;
1773 str = g_strdup_printf("The last used display style (%s) and sort "
1774 "function (Sort By %s) will be saved if you click on "
1775 "Save.", style_to_name(), sort_fn_to_name());
1776 gtk_label_set_text(GTK_LABEL(display_label), str);
1777 g_free(str);
1780 /* Reflect current state by changing the widgets in the options box */
1781 static void update_options()
1783 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_new_window_on_1),
1784 o_new_window_on_1);
1785 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_menu_on_2),
1786 collection_menu_button == 2 ? 1 : 0);
1787 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_single_click),
1788 o_single_click);
1789 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_unique_filer_windows),
1790 o_unique_filer_windows);
1791 gtk_option_menu_set_history(GTK_OPTION_MENU(menu_toolbar), o_toolbar);
1793 update_options_label();
1796 /* Set current values by reading the states of the widgets in the options box */
1797 static void set_options()
1799 GtkWidget *item, *menu;
1800 GList *list;
1802 o_new_window_on_1 = gtk_toggle_button_get_active(
1803 GTK_TOGGLE_BUTTON(toggle_new_window_on_1));
1805 collection_menu_button = gtk_toggle_button_get_active(
1806 GTK_TOGGLE_BUTTON(toggle_menu_on_2)) ? 2 : 3;
1808 o_single_click = gtk_toggle_button_get_active(
1809 GTK_TOGGLE_BUTTON(toggle_single_click));
1811 o_unique_filer_windows = gtk_toggle_button_get_active(
1812 GTK_TOGGLE_BUTTON(toggle_unique_filer_windows));
1814 collection_single_click = o_single_click ? TRUE : FALSE;
1816 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(menu_toolbar));
1817 item = gtk_menu_get_active(GTK_MENU(menu));
1818 list = gtk_container_children(GTK_CONTAINER(menu));
1819 o_toolbar = (ToolbarType) g_list_index(list, item);
1820 g_list_free(list);
1824 static guchar *style_to_name(void)
1826 return last_display_style == LARGE_ICONS ? "Large Icons" :
1827 last_display_style == SMALL_ICONS ? "Small Icons" :
1828 "Full Info";
1831 static guchar *sort_fn_to_name(void)
1833 return last_sort_fn == sort_by_name ? "Name" :
1834 last_sort_fn == sort_by_type ? "Type" :
1835 last_sort_fn == sort_by_date ? "Date" :
1836 "Size";
1839 static void save_options()
1841 option_write("filer_new_window_on_1", o_new_window_on_1 ? "1" : "0");
1842 option_write("filer_menu_on_2",
1843 collection_menu_button == 2 ? "1" : "0");
1844 option_write("filer_single_click", o_single_click ? "1" : "0");
1845 option_write("filer_unique_windows", o_unique_filer_windows ? "1" : "0");
1846 option_write("filer_display_style", style_to_name());
1847 option_write("filer_sort_by", sort_fn_to_name());
1848 option_write("filer_toolbar", o_toolbar == TOOLBAR_NONE ? "None" :
1849 o_toolbar == TOOLBAR_NORMAL ? "Normal" :
1850 o_toolbar == TOOLBAR_GNOME ? "GNOME" :
1851 "Unknown");
1854 static char *filer_new_window_on_1(char *data)
1856 o_new_window_on_1 = atoi(data) != 0;
1857 return NULL;
1860 static char *filer_menu_on_2(char *data)
1862 collection_menu_button = atoi(data) != 0 ? 2 : 3;
1863 return NULL;
1866 static char *filer_single_click(char *data)
1868 o_single_click = atoi(data) != 0;
1869 collection_single_click = o_single_click ? TRUE : FALSE;
1870 return NULL;
1873 static char *filer_unique_windows(char *data)
1875 o_unique_filer_windows = atoi(data) != 0;
1876 return NULL;
1879 static char *filer_display_style(char *data)
1881 if (g_strcasecmp(data, "Large Icons") == 0)
1882 last_display_style = LARGE_ICONS;
1883 else if (g_strcasecmp(data, "Small Icons") == 0)
1884 last_display_style = SMALL_ICONS;
1885 else if (g_strcasecmp(data, "Full Info") == 0)
1886 last_display_style = FULL_INFO;
1887 else
1888 return "Unknown display style";
1890 return NULL;
1893 static char *filer_sort_by(char *data)
1895 if (g_strcasecmp(data, "Name") == 0)
1896 last_sort_fn = sort_by_name;
1897 else if (g_strcasecmp(data, "Type") == 0)
1898 last_sort_fn = sort_by_type;
1899 else if (g_strcasecmp(data, "Date") == 0)
1900 last_sort_fn = sort_by_date;
1901 else if (g_strcasecmp(data, "Size") == 0)
1902 last_sort_fn = sort_by_size;
1903 else
1904 return "Unknown sort type";
1906 return NULL;
1909 static char *filer_toolbar(char *data)
1911 if (g_strcasecmp(data, "None") == 0)
1912 o_toolbar = TOOLBAR_NONE;
1913 else if (g_strcasecmp(data, "Normal") == 0)
1914 o_toolbar = TOOLBAR_NORMAL;
1915 else if (g_strcasecmp(data, "GNOME") == 0)
1916 o_toolbar = TOOLBAR_GNOME;
1917 else
1918 return "Unknown toolbar type";
1920 return NULL;
1923 /* Note that filer_window may not exist after this call. */
1924 void update_dir(FilerWindow *filer_window, gboolean warning)
1926 if (may_rescan(filer_window, warning))
1927 dir_update(filer_window->directory, filer_window->path);
1930 void filer_set_hidden(FilerWindow *filer_window, gboolean hidden)
1932 Directory *dir = filer_window->directory;
1934 if (filer_window->show_hidden == hidden)
1935 return;
1937 filer_window->show_hidden = hidden;
1938 last_show_hidden = hidden;
1940 g_fscache_data_ref(dir_cache, dir);
1941 detach(filer_window);
1942 filer_window->directory = dir;
1943 attach(filer_window);
1946 /* Refresh the various caches even if we don't think we need to */
1947 void full_refresh(void)
1949 mount_update(TRUE);
1952 /* See whether a filer window with a given path already exists
1953 * and is different from diff.
1955 static FilerWindow *find_filer_window(char *path, FilerWindow *diff)
1957 GList *next = all_filer_windows;
1959 while (next)
1961 FilerWindow *filer_window = (FilerWindow *) next->data;
1963 if (filer_window->panel_type == PANEL_NO &&
1964 filer_window != diff &&
1965 strcmp(path, filer_window->path) == 0)
1967 return filer_window;
1970 next = next->next;
1973 return NULL;
1976 /* This path has been mounted/umounted/deleted some files - update all dirs */
1977 void filer_check_mounted(char *path)
1979 GList *next = all_filer_windows;
1980 int len;
1982 len = strlen(path);
1984 while (next)
1986 FilerWindow *filer_window = (FilerWindow *) next->data;
1988 next = next->next;
1990 if (strncmp(path, filer_window->path, len) == 0)
1992 char s = filer_window->path[len];
1994 if (s == '/' || s == '\0')
1995 update_dir(filer_window, FALSE);
2000 /* Like minibuffer_show(), except that:
2001 * - It returns FALSE (to be used from an idle callback)
2002 * - It checks that the filer window still exists.
2004 static gboolean minibuffer_show_cb(FilerWindow *filer_window)
2006 if (exists(filer_window))
2007 minibuffer_show(filer_window);
2008 return FALSE;
2011 static gboolean exists(FilerWindow *filer_window)
2013 GList *next;
2015 for (next = all_filer_windows; next; next = next->next)
2017 FilerWindow *fw = (FilerWindow *) next->data;
2019 if (fw == filer_window)
2020 return TRUE;
2023 return FALSE;
2026 /* Highlight (wink or cursor) this item in the filer window. If the item
2027 * isn't already there but we're scanning then highlight it if it
2028 * appears later.
2030 static void set_autoselect(FilerWindow *filer_window, guchar *leaf)
2032 Collection *col = filer_window->collection;
2033 int i;
2035 g_free(filer_window->auto_select);
2036 filer_window->auto_select = NULL;
2038 for (i = 0; i < col->number_of_items; i++)
2040 DirItem *item = (DirItem *) col->items[i].data;
2042 if (strcmp(item->leafname, leaf) == 0)
2044 if (col->cursor_item != -1)
2045 collection_set_cursor_item(col, i);
2046 else
2047 collection_wink_item(col, i);
2048 return;
2052 filer_window->auto_select = g_strdup(leaf);
2055 static void filer_set_title(FilerWindow *filer_window)
2057 if (filer_window->scanning)
2059 guchar *title;
2061 title = g_strdup_printf("%s (Scanning)", filer_window->path);
2062 gtk_window_set_title(GTK_WINDOW(filer_window->window),
2063 title);
2064 g_free(title);
2066 else
2067 gtk_window_set_title(GTK_WINDOW(filer_window->window),
2068 filer_window->path);