r241: Small icons are now drawn at quarter size (super-sampled :-)
[rox-filer/ma.git] / ROX-Filer / src / filer.c
blobf5b1ac96fa80a58df90acd112a3e9a4b6ade8d19
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 28
56 #define ROW_HEIGHT_FULL_INFO 44
57 #define SMALL_ICON_HEIGHT 20
58 #ifdef HAVE_IMLIB
59 # define SMALL_ICON_WIDTH 32
60 #else
61 # define SMALL_ICON_WIDTH 48
62 #endif
63 #define MAX_ICON_HEIGHT 42
64 #define MAX_ICON_WIDTH 48
65 #define PANEL_BORDER 2
66 #define MIN_ITEM_WIDTH 64
68 extern int collection_menu_button;
69 extern gboolean collection_single_click;
71 FilerWindow *window_with_focus = NULL;
72 GList *all_filer_windows = NULL;
74 static DisplayStyle last_display_style = LARGE_ICONS;
75 static gboolean last_show_hidden = FALSE;
76 static int (*last_sort_fn)(const void *a, const void *b) = sort_by_type;
78 static FilerWindow *window_with_selection = NULL;
80 /* Options bits */
81 static guchar *style_to_name(void);
82 static guchar *sort_fn_to_name(void);
83 static void update_options_label(void);
85 static GtkWidget *create_options();
86 static void update_options();
87 static void set_options();
88 static void save_options();
89 static char *filer_single_click(char *data);
90 static char *filer_unique_windows(char *data);
91 static char *filer_menu_on_2(char *data);
92 static char *filer_new_window_on_1(char *data);
93 static char *filer_toolbar(char *data);
94 static char *filer_display_style(char *data);
95 static char *filer_sort_by(char *data);
97 static OptionsSection options =
99 "Filer window options",
100 create_options,
101 update_options,
102 set_options,
103 save_options
106 /* The values correspond to the menu indexes in the option widget */
107 typedef enum {
108 TOOLBAR_NONE = 0,
109 TOOLBAR_NORMAL = 1,
110 TOOLBAR_GNOME = 2,
111 } ToolbarType;
112 static ToolbarType o_toolbar = TOOLBAR_NORMAL;
113 static GtkWidget *menu_toolbar;
115 static GtkWidget *display_label;
117 static gboolean o_single_click = FALSE;
118 static GtkWidget *toggle_single_click;
119 static gboolean o_new_window_on_1 = FALSE; /* Button 1 => New window */
120 static GtkWidget *toggle_new_window_on_1;
121 static GtkWidget *toggle_menu_on_2;
122 static GtkWidget *toggle_unique_filer_windows;
123 gboolean o_unique_filer_windows = FALSE;
125 /* Static prototypes */
126 static void attach(FilerWindow *filer_window);
127 static void detach(FilerWindow *filer_window);
128 static void filer_window_destroyed(GtkWidget *widget,
129 FilerWindow *filer_window);
130 static void show_menu(Collection *collection, GdkEventButton *event,
131 int number_selected, gpointer user_data);
132 static gint focus_in(GtkWidget *widget,
133 GdkEventFocus *event,
134 FilerWindow *filer_window);
135 static gint focus_out(GtkWidget *widget,
136 GdkEventFocus *event,
137 FilerWindow *filer_window);
138 static void add_item(FilerWindow *filer_window, DirItem *item);
139 static void toolbar_up_clicked(GtkWidget *widget, FilerWindow *filer_window);
140 static void toolbar_home_clicked(GtkWidget *widget, FilerWindow *filer_window);
141 static void add_button(GtkWidget *box, int pixmap,
142 GtkSignalFunc cb, FilerWindow *filer_window,
143 char *label, char *tip);
144 static GtkWidget *create_toolbar(FilerWindow *filer_window);
145 static int filer_confirm_close(GtkWidget *widget, GdkEvent *event,
146 FilerWindow *window);
147 static int calc_width(FilerWindow *filer_window, DirItem *item);
148 static void draw_large_icon(GtkWidget *widget,
149 GdkRectangle *area,
150 DirItem *item,
151 gboolean selected);
152 static void draw_string(GtkWidget *widget,
153 GdkFont *font,
154 char *string,
155 int x,
156 int y,
157 int width,
158 gboolean selected);
159 static void draw_item_large(GtkWidget *widget,
160 CollectionItem *item,
161 GdkRectangle *area);
162 static void draw_item_small(GtkWidget *widget,
163 CollectionItem *item,
164 GdkRectangle *area);
165 static void draw_item_full_info(GtkWidget *widget,
166 CollectionItem *colitem,
167 GdkRectangle *area);
168 static gboolean test_point_large(Collection *collection,
169 int point_x, int point_y,
170 CollectionItem *item,
171 int width, int height);
172 static gboolean test_point_small(Collection *collection,
173 int point_x, int point_y,
174 CollectionItem *item,
175 int width, int height);
176 static gboolean test_point_full_info(Collection *collection,
177 int point_x, int point_y,
178 CollectionItem *item,
179 int width, int height);
180 static void update_display(Directory *dir,
181 DirAction action,
182 GPtrArray *items,
183 FilerWindow *filer_window);
184 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning);
185 static void shrink_width(FilerWindow *filer_window);
186 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning);
187 static void open_item(Collection *collection,
188 gpointer item_data, int item_number,
189 gpointer user_data);
190 static gboolean minibuffer_show_cb(FilerWindow *filer_window);
191 static FilerWindow *find_filer_window(char *path, FilerWindow *diff);
192 static void filer_set_title(FilerWindow *filer_window);
193 static gboolean exists(FilerWindow *filer_window);
195 static GdkAtom xa_string;
196 enum
198 TARGET_STRING,
199 TARGET_URI_LIST,
202 static GdkCursor *busy_cursor = NULL;
203 static GtkTooltips *tooltips = NULL;
205 void filer_init()
207 xa_string = gdk_atom_intern("STRING", FALSE);
209 options_sections = g_slist_prepend(options_sections, &options);
210 option_register("filer_new_window_on_1", filer_new_window_on_1);
211 option_register("filer_menu_on_2", filer_menu_on_2);
212 option_register("filer_single_click", filer_single_click);
213 option_register("filer_unique_windows", filer_unique_windows);
214 option_register("filer_toolbar", filer_toolbar);
215 option_register("filer_display_style", filer_display_style);
216 option_register("filer_sort_by", filer_sort_by);
218 busy_cursor = gdk_cursor_new(GDK_WATCH);
220 tooltips = gtk_tooltips_new();
223 static gboolean if_deleted(gpointer item, gpointer removed)
225 int i = ((GPtrArray *) removed)->len;
226 DirItem **r = (DirItem **) ((GPtrArray *) removed)->pdata;
227 char *leafname = ((DirItem *) item)->leafname;
229 while (i--)
231 if (strcmp(leafname, r[i]->leafname) == 0)
232 return TRUE;
235 return FALSE;
238 static void update_item(FilerWindow *filer_window, DirItem *item)
240 int i;
241 char *leafname = item->leafname;
243 if (leafname[0] == '.')
245 if (filer_window->show_hidden == FALSE || leafname[1] == '\0'
246 || (leafname[1] == '.' && leafname[2] == '\0'))
247 return;
250 i = collection_find_item(filer_window->collection, item, dir_item_cmp);
252 if (i >= 0)
253 collection_draw_item(filer_window->collection, i, TRUE);
254 else
255 g_warning("Failed to find '%s'\n", item->leafname);
258 static void update_display(Directory *dir,
259 DirAction action,
260 GPtrArray *items,
261 FilerWindow *filer_window)
263 int old_num;
264 int i;
265 int cursor = filer_window->collection->cursor_item;
266 char *as;
267 Collection *collection = filer_window->collection;
269 switch (action)
271 case DIR_ADD:
272 as = filer_window->auto_select;
274 old_num = collection->number_of_items;
275 for (i = 0; i < items->len; i++)
277 DirItem *item = (DirItem *) items->pdata[i];
279 add_item(filer_window, item);
281 if (cursor != -1 || !as)
282 continue;
284 if (strcmp(as, item->leafname) != 0)
285 continue;
287 cursor = collection->number_of_items - 1;
288 if (filer_window->had_cursor)
290 collection_set_cursor_item(collection,
291 cursor);
292 filer_window->mini_cursor_base = cursor;
294 else
295 collection_wink_item(collection,
296 cursor);
299 if (old_num != collection->number_of_items)
300 collection_qsort(filer_window->collection,
301 filer_window->sort_fn);
302 break;
303 case DIR_REMOVE:
304 collection_delete_if(filer_window->collection,
305 if_deleted,
306 items);
307 break;
308 case DIR_START_SCAN:
309 set_scanning_display(filer_window, TRUE);
310 break;
311 case DIR_END_SCAN:
312 if (filer_window->window->window)
313 gdk_window_set_cursor(
314 filer_window->window->window,
315 NULL);
316 shrink_width(filer_window);
317 if (filer_window->had_cursor &&
318 collection->cursor_item == -1)
320 collection_set_cursor_item(collection, 0);
321 filer_window->had_cursor = FALSE;
323 set_scanning_display(filer_window, FALSE);
324 break;
325 case DIR_UPDATE:
326 for (i = 0; i < items->len; i++)
328 DirItem *item = (DirItem *) items->pdata[i];
330 update_item(filer_window, item);
332 collection_qsort(filer_window->collection,
333 filer_window->sort_fn);
334 break;
338 static void attach(FilerWindow *filer_window)
340 gdk_window_set_cursor(filer_window->window->window, busy_cursor);
341 collection_clear(filer_window->collection);
342 filer_window->scanning = TRUE;
343 dir_attach(filer_window->directory, (DirCallback) update_display,
344 filer_window);
345 filer_set_title(filer_window);
348 static void detach(FilerWindow *filer_window)
350 g_return_if_fail(filer_window->directory != NULL);
352 dir_detach(filer_window->directory,
353 (DirCallback) update_display, filer_window);
354 g_fscache_data_unref(dir_cache, filer_window->directory);
355 filer_window->directory = NULL;
358 static void filer_window_destroyed(GtkWidget *widget,
359 FilerWindow *filer_window)
361 all_filer_windows = g_list_remove(all_filer_windows, filer_window);
363 if (window_with_selection == filer_window)
364 window_with_selection = NULL;
365 if (window_with_focus == filer_window)
366 window_with_focus = NULL;
368 if (filer_window->directory)
369 detach(filer_window);
371 g_free(filer_window->auto_select);
372 g_free(filer_window->path);
373 g_free(filer_window);
375 if (--number_of_windows < 1)
376 gtk_main_quit();
379 static int calc_width(FilerWindow *filer_window, DirItem *item)
381 int pix_width = item->image->width;
383 switch (filer_window->display_style)
385 case FULL_INFO:
386 return MAX_ICON_WIDTH + 12 +
387 MAX(item->details_width, item->name_width);
388 case SMALL_ICONS:
389 return SMALL_ICON_WIDTH + 12 + item->name_width;
390 default:
391 return MAX(pix_width, item->name_width) + 4;
395 /* Add a single object to a directory display */
396 static void add_item(FilerWindow *filer_window, DirItem *item)
398 char *leafname = item->leafname;
399 int item_width;
401 if (leafname[0] == '.')
403 if (filer_window->show_hidden == FALSE || leafname[1] == '\0'
404 || (leafname[1] == '.' && leafname[2] == '\0'))
405 return;
408 item_width = calc_width(filer_window, item);
409 if (item_width > filer_window->collection->item_width)
410 collection_set_item_size(filer_window->collection,
411 item_width,
412 filer_window->collection->item_height);
413 collection_insert(filer_window->collection, item);
416 /* Is a point inside an item? */
417 static gboolean test_point_large(Collection *collection,
418 int point_x, int point_y,
419 CollectionItem *colitem,
420 int width, int height)
422 DirItem *item = (DirItem *) colitem->data;
423 int text_height = item_font->ascent + item_font->descent;
424 MaskedPixmap *image = item->image;
425 int image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
426 int image_width = (image->width >> 1) + 2;
427 int text_width = (item->name_width >> 1) + 2;
428 int x_limit;
430 if (point_y < image_y)
431 return FALSE; /* Too high up (don't worry about too low) */
433 if (point_y <= image_y + image->height + 2)
434 x_limit = image_width;
435 else if (point_y > height - text_height - 2)
436 x_limit = text_width;
437 else
438 x_limit = MIN(image_width, text_width);
440 return ABS(point_x - (width >> 1)) < x_limit;
443 static gboolean test_point_full_info(Collection *collection,
444 int point_x, int point_y,
445 CollectionItem *colitem,
446 int width, int height)
448 DirItem *item = (DirItem *) colitem->data;
449 MaskedPixmap *image = item->image;
450 int image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
451 int low_top = height
452 - fixed_font->descent - 2 - fixed_font->ascent;
454 if (point_x < image->width + 2)
455 return point_x > 2 && point_y > image_y;
457 point_x -= MAX_ICON_WIDTH + 8;
459 if (point_y >= low_top)
460 return point_x < item->details_width;
461 if (point_y >= low_top - item_font->ascent - item_font->descent)
462 return point_x < item->name_width;
463 return FALSE;
466 static gboolean test_point_small(Collection *collection,
467 int point_x, int point_y,
468 CollectionItem *colitem,
469 int width, int height)
471 DirItem *item = (DirItem *) colitem->data;
472 MaskedPixmap *image = item->image;
473 int image_y = MAX(0, SMALL_ICON_HEIGHT - image->height);
474 int low_top = height
475 - fixed_font->descent - 2 - item_font->ascent;
476 int iwidth = MIN(SMALL_ICON_WIDTH, image->width);
478 if (point_x < iwidth + 2)
479 return point_x > 2 && point_y > image_y;
481 point_x -= SMALL_ICON_WIDTH + 4;
483 if (point_y >= low_top)
484 return point_x < item->name_width;
485 return FALSE;
488 static void draw_small_icon(GtkWidget *widget,
489 GdkRectangle *area,
490 DirItem *item,
491 gboolean selected)
493 GdkGC *gc = selected ? widget->style->white_gc
494 : widget->style->black_gc;
495 MaskedPixmap *image = item->image;
496 int width, height, image_x, image_y;
498 if (!image)
499 return;
501 if (!image->sm_pixmap)
502 pixmap_make_small(image);
504 width = MIN(image->sm_width, SMALL_ICON_WIDTH);
505 height = MIN(image->sm_height, SMALL_ICON_HEIGHT);
506 image_x = area->x + ((area->width - width) >> 1);
508 gdk_gc_set_clip_mask(gc, item->image->sm_mask);
510 image_y = MAX(0, SMALL_ICON_HEIGHT - image->sm_height);
511 gdk_gc_set_clip_origin(gc, image_x, area->y + image_y);
512 gdk_draw_pixmap(widget->window, gc,
513 item->image->sm_pixmap,
514 0, 0, /* Source x,y */
515 image_x, area->y + image_y, /* Dest x,y */
516 width, height);
518 if (selected)
520 gdk_gc_set_function(gc, GDK_INVERT);
521 gdk_draw_rectangle(widget->window,
523 TRUE, image_x, area->y + image_y,
524 width, height);
525 gdk_gc_set_function(gc, GDK_COPY);
528 if (item->flags & ITEM_FLAG_SYMLINK)
530 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
531 gdk_gc_set_clip_mask(gc,
532 default_pixmap[TYPE_SYMLINK]->mask);
533 gdk_draw_pixmap(widget->window, gc,
534 default_pixmap[TYPE_SYMLINK]->pixmap,
535 0, 0, /* Source x,y */
536 image_x, area->y + 8, /* Dest x,y */
537 -1, -1);
539 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
541 int type = item->flags & ITEM_FLAG_MOUNTED
542 ? TYPE_MOUNTED
543 : TYPE_UNMOUNTED;
544 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
545 gdk_gc_set_clip_mask(gc,
546 default_pixmap[type]->mask);
547 gdk_draw_pixmap(widget->window, gc,
548 default_pixmap[type]->pixmap,
549 0, 0, /* Source x,y */
550 image_x, area->y + 8, /* Dest x,y */
551 -1, -1);
554 gdk_gc_set_clip_mask(gc, NULL);
555 gdk_gc_set_clip_origin(gc, 0, 0);
558 static void draw_large_icon(GtkWidget *widget,
559 GdkRectangle *area,
560 DirItem *item,
561 gboolean selected)
563 MaskedPixmap *image = item->image;
564 int width = MIN(image->width, MAX_ICON_WIDTH);
565 int height = MIN(image->height, MAX_ICON_WIDTH);
566 int image_x = area->x + ((area->width - width) >> 1);
567 int image_y;
568 GdkGC *gc = selected ? widget->style->white_gc
569 : widget->style->black_gc;
571 gdk_gc_set_clip_mask(gc, item->image->mask);
573 image_y = MAX(0, MAX_ICON_HEIGHT - image->height);
574 gdk_gc_set_clip_origin(gc, image_x, area->y + image_y);
575 gdk_draw_pixmap(widget->window, gc,
576 item->image->pixmap,
577 0, 0, /* Source x,y */
578 image_x, area->y + image_y, /* Dest x,y */
579 width, height);
581 if (selected)
583 gdk_gc_set_function(gc, GDK_INVERT);
584 gdk_draw_rectangle(widget->window,
586 TRUE, image_x, area->y + image_y,
587 width, height);
588 gdk_gc_set_function(gc, GDK_COPY);
591 if (item->flags & ITEM_FLAG_SYMLINK)
593 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
594 gdk_gc_set_clip_mask(gc,
595 default_pixmap[TYPE_SYMLINK]->mask);
596 gdk_draw_pixmap(widget->window, gc,
597 default_pixmap[TYPE_SYMLINK]->pixmap,
598 0, 0, /* Source x,y */
599 image_x, area->y + 8, /* Dest x,y */
600 -1, -1);
602 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
604 int type = item->flags & ITEM_FLAG_MOUNTED
605 ? TYPE_MOUNTED
606 : TYPE_UNMOUNTED;
607 gdk_gc_set_clip_origin(gc, image_x, area->y + 8);
608 gdk_gc_set_clip_mask(gc,
609 default_pixmap[type]->mask);
610 gdk_draw_pixmap(widget->window, gc,
611 default_pixmap[type]->pixmap,
612 0, 0, /* Source x,y */
613 image_x, area->y + 8, /* Dest x,y */
614 -1, -1);
617 gdk_gc_set_clip_mask(gc, NULL);
618 gdk_gc_set_clip_origin(gc, 0, 0);
621 static void draw_string(GtkWidget *widget,
622 GdkFont *font,
623 char *string,
624 int x,
625 int y,
626 int width,
627 gboolean selected)
629 int text_height = font->ascent + font->descent;
631 if (selected)
632 gtk_paint_flat_box(widget->style, widget->window,
633 GTK_STATE_SELECTED, GTK_SHADOW_NONE,
634 NULL, widget, "text",
635 x, y - font->ascent,
636 width,
637 text_height);
639 gdk_draw_text(widget->window,
640 font,
641 selected ? widget->style->white_gc
642 : widget->style->black_gc,
643 x, y,
644 string, strlen(string));
647 /* Return a string (valid until next call) giving details
648 * of this item.
650 char *details(DirItem *item)
652 mode_t m = item->mode;
653 static guchar *buf = NULL;
655 if (buf)
656 g_free(buf);
658 if (item->lstat_errno)
659 buf = g_strdup_printf("lstat(2) failed: %s",
660 g_strerror(item->lstat_errno));
661 else
662 buf = g_strdup_printf("%s %s %-8.8s %-8.8s %s %s",
663 item->flags & ITEM_FLAG_APPDIR? "App " :
664 S_ISDIR(m) ? "Dir " :
665 S_ISCHR(m) ? "Char" :
666 S_ISBLK(m) ? "Blck" :
667 S_ISLNK(m) ? "Link" :
668 S_ISSOCK(m) ? "Sock" :
669 S_ISFIFO(m) ? "Pipe" : "File",
670 pretty_permissions(m),
671 user_name(item->uid),
672 group_name(item->gid),
673 format_size_aligned(item->size),
674 pretty_time(&item->mtime));
675 return buf;
678 static void draw_item_full_info(GtkWidget *widget,
679 CollectionItem *colitem,
680 GdkRectangle *area)
682 DirItem *item = (DirItem *) colitem->data;
683 MaskedPixmap *image = item->image;
684 int text_x = area->x + MAX_ICON_WIDTH + 8;
685 int low_text_y = area->y + area->height - fixed_font->descent - 2;
686 gboolean selected = colitem->selected;
687 GdkRectangle pic_area;
689 pic_area.x = area->x;
690 pic_area.y = area->y;
691 pic_area.width = image->width + 8;
692 pic_area.height = area->height;
694 draw_large_icon(widget, &pic_area, item, selected);
696 draw_string(widget,
697 item_font,
698 item->leafname,
699 text_x,
700 low_text_y - item_font->descent - fixed_font->ascent,
701 item->name_width,
702 selected);
703 draw_string(widget,
704 fixed_font,
705 details(item),
706 text_x, low_text_y,
707 item->details_width,
708 selected);
710 if (item->lstat_errno)
711 return;
713 /* Underline the effective permissions */
714 gdk_draw_rectangle(widget->window,
715 selected ? widget->style->white_gc
716 : widget->style->black_gc,
717 TRUE,
718 text_x - 1 + fixed_width *
719 (5 + 4 * applicable(item->uid, item->gid)),
720 low_text_y + fixed_font->descent - 1,
721 fixed_width * 3 + 1, 1);
724 static void draw_item_small(GtkWidget *widget,
725 CollectionItem *colitem,
726 GdkRectangle *area)
728 DirItem *item = (DirItem *) colitem->data;
729 int text_x = area->x + SMALL_ICON_WIDTH + 4;
730 int low_text_y = area->y + area->height - item_font->descent - 2;
731 gboolean selected = colitem->selected;
732 GdkRectangle pic_area;
734 pic_area.x = area->x;
735 pic_area.y = area->y;
736 pic_area.width = SMALL_ICON_WIDTH;
737 pic_area.height = SMALL_ICON_HEIGHT;
739 draw_small_icon(widget, &pic_area, item, selected);
741 draw_string(widget,
742 item_font,
743 item->leafname,
744 text_x,
745 low_text_y,
746 item->name_width,
747 selected);
750 static void draw_item_large(GtkWidget *widget,
751 CollectionItem *colitem,
752 GdkRectangle *area)
754 DirItem *item = (DirItem *) colitem->data;
755 int text_x = area->x + ((area->width - item->name_width) >> 1);
756 int text_y = area->y + area->height - item_font->descent - 2;
757 gboolean selected = colitem->selected;
759 draw_large_icon(widget, area, item, selected);
761 draw_string(widget,
762 item_font,
763 item->leafname,
764 text_x, text_y, item->name_width,
765 selected);
768 static void show_menu(Collection *collection, GdkEventButton *event,
769 int item, gpointer user_data)
771 show_filer_menu((FilerWindow *) user_data, event, item);
774 /* Returns TRUE iff the directory still exists. */
775 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning)
777 Directory *dir;
779 g_return_val_if_fail(filer_window != NULL, FALSE);
781 /* We do a fresh lookup (rather than update) because the inode may
782 * have changed.
784 dir = g_fscache_lookup(dir_cache, filer_window->path);
785 if (!dir)
787 if (warning)
788 delayed_error("ROX-Filer", "Directory missing/deleted");
789 gtk_widget_destroy(filer_window->window);
790 return FALSE;
792 if (dir == filer_window->directory)
793 g_fscache_data_unref(dir_cache, dir);
794 else
796 detach(filer_window);
797 filer_window->directory = dir;
798 attach(filer_window);
801 return TRUE;
804 /* Another app has grabbed the selection */
805 static gint collection_lose_selection(GtkWidget *widget,
806 GdkEventSelection *event)
808 if (window_with_selection &&
809 window_with_selection->collection == COLLECTION(widget))
811 FilerWindow *filer_window = window_with_selection;
812 window_with_selection = NULL;
813 collection_clear_selection(filer_window->collection);
816 return TRUE;
819 /* Someone wants us to send them the selection */
820 static void selection_get(GtkWidget *widget,
821 GtkSelectionData *selection_data,
822 guint info,
823 guint time,
824 gpointer data)
826 GString *reply, *header;
827 FilerWindow *filer_window;
828 int i;
829 Collection *collection;
831 filer_window = gtk_object_get_data(GTK_OBJECT(widget), "filer_window");
833 reply = g_string_new(NULL);
834 header = g_string_new(NULL);
836 switch (info)
838 case TARGET_STRING:
839 g_string_sprintf(header, " %s",
840 make_path(filer_window->path, "")->str);
841 break;
842 case TARGET_URI_LIST:
843 g_string_sprintf(header, " file://%s%s",
844 our_host_name(),
845 make_path(filer_window->path, "")->str);
846 break;
849 collection = filer_window->collection;
850 for (i = 0; i < collection->number_of_items; i++)
852 if (collection->items[i].selected)
854 DirItem *item =
855 (DirItem *) collection->items[i].data;
857 g_string_append(reply, header->str);
858 g_string_append(reply, item->leafname);
861 /* This works, but I don't think I like it... */
862 /* g_string_append_c(reply, ' '); */
864 gtk_selection_data_set(selection_data, xa_string,
865 8, reply->str + 1, reply->len - 1);
866 g_string_free(reply, TRUE);
867 g_string_free(header, TRUE);
870 /* No items are now selected. This might be because another app claimed
871 * the selection or because the user unselected all the items.
873 static void lose_selection(Collection *collection,
874 guint time,
875 gpointer user_data)
877 FilerWindow *filer_window = (FilerWindow *) user_data;
879 if (window_with_selection == filer_window)
881 window_with_selection = NULL;
882 gtk_selection_owner_set(NULL,
883 GDK_SELECTION_PRIMARY,
884 time);
888 static void gain_selection(Collection *collection,
889 guint time,
890 gpointer user_data)
892 FilerWindow *filer_window = (FilerWindow *) user_data;
894 if (gtk_selection_owner_set(GTK_WIDGET(collection),
895 GDK_SELECTION_PRIMARY,
896 time))
898 window_with_selection = filer_window;
900 else
901 collection_clear_selection(filer_window->collection);
904 int sort_by_name(const void *item1, const void *item2)
906 return strcmp((*((DirItem **)item1))->leafname,
907 (*((DirItem **)item2))->leafname);
910 int sort_by_type(const void *item1, const void *item2)
912 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
913 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
914 MIME_type *m1, *m2;
916 int diff = i1->base_type - i2->base_type;
918 if (!diff)
919 diff = (i1->flags & ITEM_FLAG_APPDIR)
920 - (i2->flags & ITEM_FLAG_APPDIR);
921 if (diff)
922 return diff > 0 ? 1 : -1;
924 m1 = i1->mime_type;
925 m2 = i2->mime_type;
927 if (m1 && m2)
929 diff = strcmp(m1->media_type, m2->media_type);
930 if (!diff)
931 diff = strcmp(m1->subtype, m2->subtype);
933 else if (m1 || m2)
934 diff = m1 ? 1 : -1;
935 else
936 diff = 0;
938 if (diff)
939 return diff > 0 ? 1 : -1;
941 return sort_by_name(item1, item2);
944 int sort_by_date(const void *item1, const void *item2)
946 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
947 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
949 return i1->mtime > i2->mtime ? -1 :
950 i1->mtime < i2->mtime ? 1 :
951 sort_by_name(item1, item2);
954 int sort_by_size(const void *item1, const void *item2)
956 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
957 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
959 return i1->size > i2->size ? -1 :
960 i1->size < i2->size ? 1 :
961 sort_by_name(item1, item2);
964 static void open_item(Collection *collection,
965 gpointer item_data, int item_number,
966 gpointer user_data)
968 FilerWindow *filer_window = (FilerWindow *) user_data;
969 GdkEvent *event;
970 GdkEventButton *bevent;
971 GdkEventKey *kevent;
972 OpenFlags flags = 0;
974 event = (GdkEvent *) gtk_get_current_event();
976 bevent = (GdkEventButton *) event;
977 kevent = (GdkEventKey *) event;
979 switch (event->type)
981 case GDK_2BUTTON_PRESS:
982 case GDK_BUTTON_PRESS:
983 case GDK_BUTTON_RELEASE:
984 if (bevent->state & GDK_SHIFT_MASK)
985 flags |= OPEN_SHIFT;
987 if (o_new_window_on_1 ^ (bevent->button == 1))
988 flags |= OPEN_SAME_WINDOW;
990 if (bevent->button != 1)
991 flags |= OPEN_CLOSE_WINDOW;
993 if (o_single_click == FALSE &&
994 (bevent->state & GDK_CONTROL_MASK) != 0)
995 flags ^= OPEN_SAME_WINDOW | OPEN_CLOSE_WINDOW;
996 break;
997 case GDK_KEY_PRESS:
998 flags |= OPEN_SAME_WINDOW;
999 if (kevent->state & GDK_SHIFT_MASK)
1000 flags |= OPEN_SHIFT;
1001 break;
1002 default:
1003 break;
1006 filer_openitem(filer_window, item_number, flags);
1009 /* Return the full path to the directory containing object 'path'.
1010 * Relative paths are resolved from the filerwindow's path.
1012 static void follow_symlink(FilerWindow *filer_window, char *path,
1013 gboolean same_window)
1015 char *real, *slash;
1016 char *new_dir;
1018 if (path[0] != '/')
1019 path = make_path(filer_window->path, path)->str;
1021 real = pathdup(path);
1022 slash = strrchr(real, '/');
1023 if (!slash)
1025 g_free(real);
1026 delayed_error("ROX-Filer",
1027 "Broken symlink (or you don't have permission "
1028 "to follow it).");
1029 return;
1032 *slash = '\0';
1034 if (*real)
1035 new_dir = real;
1036 else
1037 new_dir = "/";
1039 if (filer_window->panel_type || !same_window)
1041 FilerWindow *new;
1043 new = filer_opendir(new_dir, PANEL_NO);
1044 filer_set_autoselect(new, slash + 1);
1046 else
1047 filer_change_to(filer_window, new_dir, slash + 1);
1049 g_free(real);
1052 /* Open the item (or add it to the shell command minibuffer) */
1053 void filer_openitem(FilerWindow *filer_window, int item_number, OpenFlags flags)
1055 gboolean shift = (flags & OPEN_SHIFT) != 0;
1056 gboolean close_mini = flags & OPEN_FROM_MINI;
1057 gboolean same_window = (flags & OPEN_SAME_WINDOW) != 0
1058 && !filer_window->panel_type;
1059 gboolean close_window = (flags & OPEN_CLOSE_WINDOW) != 0
1060 && !filer_window->panel_type;
1061 GtkWidget *widget;
1062 char *full_path;
1063 DirItem *item = (DirItem *)
1064 filer_window->collection->items[item_number].data;
1065 gboolean wink = TRUE, destroy = FALSE;
1067 widget = filer_window->window;
1068 if (filer_window->mini_type == MINI_SHELL)
1070 minibuffer_add(filer_window, item->leafname);
1071 return;
1074 full_path = make_path(filer_window->path,
1075 item->leafname)->str;
1077 if (item->flags & ITEM_FLAG_SYMLINK && shift)
1079 char path[MAXPATHLEN + 1];
1080 int got;
1082 got = readlink(make_path(filer_window->path,
1083 item->leafname)->str,
1084 path, MAXPATHLEN);
1085 if (got < 0)
1086 delayed_error("ROX-Filer", g_strerror(errno));
1087 else
1089 g_return_if_fail(got <= MAXPATHLEN);
1090 path[got] = '\0';
1092 follow_symlink(filer_window, path,
1093 flags & OPEN_SAME_WINDOW);
1095 return;
1098 switch (item->base_type)
1100 case TYPE_DIRECTORY:
1101 if (item->flags & ITEM_FLAG_APPDIR && !shift)
1103 run_app(make_path(filer_window->path,
1104 item->leafname)->str);
1105 if (close_window)
1106 destroy = TRUE;
1107 break;
1110 if (item->flags & ITEM_FLAG_MOUNT_POINT && shift)
1112 action_mount(filer_window, item);
1113 if (item->flags & ITEM_FLAG_MOUNTED)
1114 break;
1117 if (same_window)
1119 wink = FALSE;
1120 filer_change_to(filer_window, full_path, NULL);
1121 close_mini = FALSE;
1123 else
1124 filer_opendir(full_path, PANEL_NO);
1125 break;
1126 case TYPE_FILE:
1127 if ((item->flags & ITEM_FLAG_EXEC_FILE) && !shift)
1129 char *argv[] = {NULL, NULL};
1131 argv[0] = full_path;
1133 if (spawn_full(argv, filer_window->path))
1135 if (close_window)
1136 destroy = TRUE;
1138 else
1139 report_error("ROX-Filer",
1140 "Failed to fork() child");
1142 else
1144 GString *message;
1145 MIME_type *type = shift ? &text_plain
1146 : item->mime_type;
1148 g_return_if_fail(type != NULL);
1150 if (type_open(full_path, type))
1152 if (close_window)
1153 destroy = TRUE;
1155 else
1157 message = g_string_new(NULL);
1158 g_string_sprintf(message, "No open "
1159 "action specified for files of "
1160 "this type (%s/%s)",
1161 type->media_type,
1162 type->subtype);
1163 report_error("ROX-Filer", message->str);
1164 g_string_free(message, TRUE);
1167 break;
1168 default:
1169 report_error("open_item",
1170 "I don't know how to open that");
1171 break;
1174 if (destroy)
1175 gtk_widget_destroy(filer_window->window);
1176 else
1178 if (wink)
1179 collection_wink_item(filer_window->collection,
1180 item_number);
1181 if (close_mini)
1182 minibuffer_hide(filer_window);
1186 static gint pointer_in(GtkWidget *widget,
1187 GdkEventCrossing *event,
1188 FilerWindow *filer_window)
1190 may_rescan(filer_window, TRUE);
1191 return FALSE;
1194 static gint focus_in(GtkWidget *widget,
1195 GdkEventFocus *event,
1196 FilerWindow *filer_window)
1198 window_with_focus = filer_window;
1200 return FALSE;
1203 static gint focus_out(GtkWidget *widget,
1204 GdkEventFocus *event,
1205 FilerWindow *filer_window)
1207 /* TODO: Shade the cursor */
1209 return FALSE;
1212 /* Handle keys that can't be bound with the menu */
1213 static gint key_press_event(GtkWidget *widget,
1214 GdkEventKey *event,
1215 FilerWindow *filer_window)
1217 switch (event->keyval)
1219 case GDK_BackSpace:
1220 change_to_parent(filer_window);
1221 break;
1222 default:
1223 return FALSE;
1226 return TRUE;
1229 static void toolbar_refresh_clicked(GtkWidget *widget,
1230 FilerWindow *filer_window)
1232 full_refresh();
1233 update_dir(filer_window, TRUE);
1236 static void toolbar_home_clicked(GtkWidget *widget, FilerWindow *filer_window)
1238 filer_change_to(filer_window, home_dir, NULL);
1241 static void toolbar_up_clicked(GtkWidget *widget, FilerWindow *filer_window)
1243 change_to_parent(filer_window);
1246 void change_to_parent(FilerWindow *filer_window)
1248 char *copy;
1249 char *slash;
1251 if (filer_window->path[0] == '/' && filer_window->path[1] == '\0')
1252 return; /* Already in the root */
1254 copy = g_strdup(filer_window->path);
1255 slash = strrchr(copy, '/');
1257 if (slash)
1259 *slash = '\0';
1260 filer_change_to(filer_window,
1261 *copy ? copy : "/",
1262 slash + 1);
1264 else
1265 g_warning("No / in directory path!\n");
1267 g_free(copy);
1271 /* Make filer_window display path. When finished, highlight item 'from', or
1272 * the first item if from is NULL. If there is currently no cursor then
1273 * simply wink 'from' (if not NULL).
1275 void filer_change_to(FilerWindow *filer_window, char *path, char *from)
1277 char *from_dup;
1278 char *real_path = pathdup(path);
1280 if (o_unique_filer_windows)
1282 FilerWindow *fw;
1284 fw = find_filer_window(real_path, filer_window);
1285 if (fw)
1286 gtk_widget_destroy(fw->window);
1289 from_dup = from && *from ? g_strdup(from) : NULL;
1291 detach(filer_window);
1292 g_free(filer_window->path);
1293 filer_window->path = real_path;
1295 filer_window->directory = g_fscache_lookup(dir_cache,
1296 filer_window->path);
1297 if (filer_window->directory)
1299 g_free(filer_window->auto_select);
1300 filer_window->had_cursor =
1301 filer_window->collection->cursor_item != -1
1302 || filer_window->had_cursor;
1303 filer_window->auto_select = from_dup;
1305 filer_set_title(filer_window);
1306 collection_set_cursor_item(filer_window->collection, -1);
1307 attach(filer_window);
1309 if (filer_window->mini_type == MINI_PATH)
1310 gtk_idle_add((GtkFunction) minibuffer_show_cb,
1311 filer_window);
1313 else
1315 char *error;
1317 g_free(from_dup);
1318 error = g_strdup_printf("Directory '%s' is not accessible.",
1319 path);
1320 delayed_error("ROX-Filer", error);
1321 g_free(error);
1322 gtk_widget_destroy(filer_window->window);
1326 int selected_item_number(Collection *collection)
1328 int i;
1330 g_return_val_if_fail(collection != NULL, -1);
1331 g_return_val_if_fail(IS_COLLECTION(collection), -1);
1332 g_return_val_if_fail(collection->number_selected == 1, -1);
1334 for (i = 0; i < collection->number_of_items; i++)
1335 if (collection->items[i].selected)
1336 return i;
1338 g_warning("selected_item: number_selected is wrong\n");
1340 return -1;
1343 DirItem *selected_item(Collection *collection)
1345 int item;
1347 item = selected_item_number(collection);
1349 if (item > -1)
1350 return (DirItem *) collection->items[item].data;
1351 return NULL;
1354 static int filer_confirm_close(GtkWidget *widget, GdkEvent *event,
1355 FilerWindow *window)
1357 /* TODO: We can open lots of these - very irritating! */
1358 return get_choice("Close panel?",
1359 "You have tried to close a panel via the window "
1360 "manager - I usually find that this is accidental... "
1361 "really close?",
1362 2, "Remove", "Cancel") != 0;
1365 /* Make the items as narrow as possible */
1366 static void shrink_width(FilerWindow *filer_window)
1368 int i;
1369 Collection *col = filer_window->collection;
1370 int width = MIN_ITEM_WIDTH;
1371 int this_width;
1372 DisplayStyle style = filer_window->display_style;
1373 int text_height;
1375 text_height = item_font->ascent + item_font->descent;
1377 for (i = 0; i < col->number_of_items; i++)
1379 this_width = calc_width(filer_window,
1380 (DirItem *) col->items[i].data);
1381 if (this_width > width)
1382 width = this_width;
1385 collection_set_item_size(filer_window->collection,
1386 width,
1387 style == FULL_INFO ? MAX_ICON_HEIGHT + 4 :
1388 style == SMALL_ICONS ? MAX(text_height, SMALL_ICON_HEIGHT) + 4
1389 : text_height + MAX_ICON_HEIGHT + 8);
1392 void filer_set_sort_fn(FilerWindow *filer_window,
1393 int (*fn)(const void *a, const void *b))
1395 if (filer_window->sort_fn == fn)
1396 return;
1398 filer_window->sort_fn = fn;
1399 last_sort_fn = fn;
1401 collection_qsort(filer_window->collection,
1402 filer_window->sort_fn);
1404 update_options_label();
1407 void filer_style_set(FilerWindow *filer_window, DisplayStyle style)
1409 if (filer_window->display_style == style)
1410 return;
1412 if (filer_window->panel_type)
1413 style = LARGE_ICONS;
1414 else
1415 last_display_style = style;
1417 filer_window->display_style = style;
1419 switch (style)
1421 case SMALL_ICONS:
1422 collection_set_functions(filer_window->collection,
1423 draw_item_small, test_point_small);
1424 break;
1425 case FULL_INFO:
1426 collection_set_functions(filer_window->collection,
1427 draw_item_full_info, test_point_full_info);
1428 break;
1429 default:
1430 collection_set_functions(filer_window->collection,
1431 draw_item_large, test_point_large);
1432 break;
1435 shrink_width(filer_window);
1437 update_options_label();
1440 FilerWindow *filer_opendir(char *path, PanelType panel_type)
1442 GtkWidget *hbox, *scrollbar, *collection;
1443 FilerWindow *filer_window;
1444 GtkTargetEntry target_table[] =
1446 {"text/uri-list", 0, TARGET_URI_LIST},
1447 {"STRING", 0, TARGET_STRING},
1449 char *real_path;
1451 real_path = pathdup(path);
1453 if (o_unique_filer_windows && panel_type == PANEL_NO)
1455 FilerWindow *fw;
1457 fw = find_filer_window(real_path, NULL);
1459 if (fw)
1461 /* TODO: this should bring the window to the front
1462 * at the same coordinates.
1464 gtk_widget_hide(fw->window);
1465 gtk_widget_show(fw->window);
1466 g_free(real_path);
1467 return fw;
1471 filer_window = g_new(FilerWindow, 1);
1472 filer_window->minibuffer = NULL;
1473 filer_window->path = real_path;
1474 filer_window->scanning = FALSE;
1475 filer_window->had_cursor = FALSE;
1476 filer_window->auto_select = NULL;
1477 filer_window->mini_type = MINI_NONE;
1479 filer_window->directory = g_fscache_lookup(dir_cache,
1480 filer_window->path);
1481 if (!filer_window->directory)
1483 char *error;
1485 error = g_strdup_printf("Directory '%s' not found.", path);
1486 delayed_error("ROX-Filer", error);
1487 g_free(error);
1488 g_free(filer_window->path);
1489 g_free(filer_window);
1490 return NULL;
1493 filer_window->show_hidden = last_show_hidden;
1494 filer_window->panel_type = panel_type;
1495 filer_window->temp_item_selected = FALSE;
1496 filer_window->sort_fn = last_sort_fn;
1497 filer_window->flags = (FilerFlags) 0;
1498 filer_window->display_style = UNKNOWN_STYLE;
1500 filer_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1501 filer_set_title(filer_window);
1503 collection = collection_new(NULL);
1504 gtk_object_set_data(GTK_OBJECT(collection),
1505 "filer_window", filer_window);
1506 filer_window->collection = COLLECTION(collection);
1508 gtk_widget_add_events(filer_window->window, GDK_ENTER_NOTIFY);
1509 gtk_signal_connect(GTK_OBJECT(filer_window->window),
1510 "enter-notify-event",
1511 GTK_SIGNAL_FUNC(pointer_in), filer_window);
1512 gtk_signal_connect(GTK_OBJECT(filer_window->window), "focus_in_event",
1513 GTK_SIGNAL_FUNC(focus_in), filer_window);
1514 gtk_signal_connect(GTK_OBJECT(filer_window->window), "focus_out_event",
1515 GTK_SIGNAL_FUNC(focus_out), filer_window);
1516 gtk_signal_connect(GTK_OBJECT(filer_window->window), "destroy",
1517 filer_window_destroyed, filer_window);
1519 gtk_signal_connect(GTK_OBJECT(filer_window->collection), "open_item",
1520 open_item, filer_window);
1521 gtk_signal_connect(GTK_OBJECT(collection), "show_menu",
1522 show_menu, filer_window);
1523 gtk_signal_connect(GTK_OBJECT(collection), "gain_selection",
1524 gain_selection, filer_window);
1525 gtk_signal_connect(GTK_OBJECT(collection), "lose_selection",
1526 lose_selection, filer_window);
1527 gtk_signal_connect(GTK_OBJECT(collection), "drag_selection",
1528 drag_selection, filer_window);
1529 gtk_signal_connect(GTK_OBJECT(collection), "drag_data_get",
1530 drag_data_get, filer_window);
1531 gtk_signal_connect(GTK_OBJECT(collection), "selection_clear_event",
1532 GTK_SIGNAL_FUNC(collection_lose_selection), NULL);
1533 gtk_signal_connect (GTK_OBJECT(collection), "selection_get",
1534 GTK_SIGNAL_FUNC(selection_get), NULL);
1535 gtk_selection_add_targets(collection, GDK_SELECTION_PRIMARY,
1536 target_table,
1537 sizeof(target_table) / sizeof(*target_table));
1539 filer_style_set(filer_window, last_display_style);
1540 drag_set_dest(filer_window);
1542 if (panel_type)
1544 int swidth, sheight, iwidth, iheight;
1545 GtkWidget *frame, *win = filer_window->window;
1547 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Panel",
1548 "ROX-Filer");
1549 collection_set_panel(filer_window->collection, TRUE);
1550 gtk_signal_connect(GTK_OBJECT(filer_window->window),
1551 "delete_event",
1552 GTK_SIGNAL_FUNC(filer_confirm_close),
1553 filer_window);
1555 gdk_window_get_size(GDK_ROOT_PARENT(), &swidth, &sheight);
1556 iwidth = filer_window->collection->item_width;
1557 iheight = filer_window->collection->item_height;
1560 int height = iheight + PANEL_BORDER;
1561 int y = panel_type == PANEL_TOP
1562 ? -PANEL_BORDER
1563 : sheight - height - PANEL_BORDER;
1565 gtk_widget_set_usize(collection, swidth, height);
1566 gtk_widget_set_uposition(win, 0, y);
1569 frame = gtk_frame_new(NULL);
1570 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1571 gtk_container_add(GTK_CONTAINER(frame), collection);
1572 gtk_container_add(GTK_CONTAINER(win), frame);
1574 gtk_widget_show_all(frame);
1575 gtk_widget_realize(win);
1576 if (override_redirect)
1577 gdk_window_set_override_redirect(win->window, TRUE);
1578 make_panel_window(win->window);
1580 else
1582 GtkWidget *vbox;
1583 int col_height = ROW_HEIGHT_LARGE * 3;
1585 gtk_signal_connect(GTK_OBJECT(collection),
1586 "key_press_event",
1587 GTK_SIGNAL_FUNC(key_press_event), filer_window);
1588 gtk_window_set_default_size(GTK_WINDOW(filer_window->window),
1589 filer_window->display_style == LARGE_ICONS ? 400 : 512,
1590 o_toolbar == TOOLBAR_NONE ? col_height:
1591 o_toolbar == TOOLBAR_NORMAL ? col_height + 24 :
1592 col_height + 38);
1594 hbox = gtk_hbox_new(FALSE, 0);
1595 gtk_container_add(GTK_CONTAINER(filer_window->window),
1596 hbox);
1598 vbox = gtk_vbox_new(FALSE, 0);
1599 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1601 if (o_toolbar != TOOLBAR_NONE)
1603 GtkWidget *toolbar;
1605 toolbar = create_toolbar(filer_window);
1606 gtk_box_pack_start(GTK_BOX(vbox), toolbar,
1607 FALSE, TRUE, 0);
1608 gtk_widget_show_all(toolbar);
1611 gtk_box_pack_start(GTK_BOX(vbox), collection, TRUE, TRUE, 0);
1613 filer_window->minibuffer = create_minibuffer(filer_window);
1614 gtk_box_pack_start(GTK_BOX(vbox), filer_window->minibuffer,
1615 FALSE, TRUE, 0);
1617 scrollbar = gtk_vscrollbar_new(COLLECTION(collection)->vadj);
1618 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
1619 gtk_accel_group_attach(filer_keys,
1620 GTK_OBJECT(filer_window->window));
1621 gtk_window_set_focus(GTK_WINDOW(filer_window->window),
1622 collection);
1624 gtk_widget_show(hbox);
1625 gtk_widget_show(vbox);
1626 gtk_widget_show(scrollbar);
1627 gtk_widget_show(collection);
1630 number_of_windows++;
1631 gtk_widget_show(filer_window->window);
1632 attach(filer_window);
1634 all_filer_windows = g_list_prepend(all_filer_windows, filer_window);
1636 return filer_window;
1639 static gint clear_scanning_display(FilerWindow *filer_window)
1641 if (exists(filer_window))
1642 filer_set_title(filer_window);
1643 return FALSE;
1646 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning)
1648 if (scanning == filer_window->scanning)
1649 return;
1650 filer_window->scanning = scanning;
1652 if (scanning)
1653 filer_set_title(filer_window);
1654 else
1655 gtk_timeout_add(300, (GtkFunction) clear_scanning_display,
1656 filer_window);
1659 static GtkWidget *create_toolbar(FilerWindow *filer_window)
1661 GtkWidget *frame, *box;
1663 if (o_toolbar == TOOLBAR_GNOME)
1665 frame = gtk_handle_box_new();
1666 box = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1667 GTK_TOOLBAR_BOTH);
1668 gtk_container_set_border_width(GTK_CONTAINER(box), 2);
1669 gtk_toolbar_set_space_style(GTK_TOOLBAR(box),
1670 GTK_TOOLBAR_SPACE_LINE);
1671 gtk_toolbar_set_button_relief(GTK_TOOLBAR(box),
1672 GTK_RELIEF_NONE);
1674 else
1676 frame = gtk_frame_new(NULL);
1677 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1679 box = gtk_hbutton_box_new();
1680 gtk_button_box_set_child_size_default(16, 16);
1681 gtk_hbutton_box_set_spacing_default(2);
1682 gtk_button_box_set_layout(GTK_BUTTON_BOX(box),
1683 GTK_BUTTONBOX_START);
1686 gtk_container_add(GTK_CONTAINER(frame), box);
1688 add_button(box, TOOLBAR_UP_ICON,
1689 GTK_SIGNAL_FUNC(toolbar_up_clicked),
1690 filer_window,
1691 "Up", "Change to parent directory");
1692 add_button(box, TOOLBAR_HOME_ICON,
1693 GTK_SIGNAL_FUNC(toolbar_home_clicked),
1694 filer_window,
1695 "Home", "Change to home directory");
1696 add_button(box, TOOLBAR_REFRESH_ICON,
1697 GTK_SIGNAL_FUNC(toolbar_refresh_clicked),
1698 filer_window,
1699 "Rescan", "Rescan directory contents");
1701 return frame;
1704 static void add_button(GtkWidget *box, int pixmap,
1705 GtkSignalFunc cb, FilerWindow *filer_window,
1706 char *label, char *tip)
1708 GtkWidget *button, *icon;
1710 icon = gtk_pixmap_new(default_pixmap[pixmap]->pixmap,
1711 default_pixmap[pixmap]->mask);
1713 if (o_toolbar == TOOLBAR_GNOME)
1715 gtk_toolbar_append_element(GTK_TOOLBAR(box),
1716 GTK_TOOLBAR_CHILD_BUTTON,
1717 NULL,
1718 label,
1719 tip, NULL,
1720 icon,
1721 cb, filer_window);
1723 else
1725 button = gtk_button_new();
1726 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
1728 gtk_container_add(GTK_CONTAINER(button), icon);
1729 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1730 cb, filer_window);
1732 gtk_tooltips_set_tip(tooltips, button, tip, NULL);
1734 gtk_container_add(GTK_CONTAINER(box), button);
1738 /* Build up some option widgets to go in the options dialog, but don't
1739 * fill them in yet.
1741 static GtkWidget *create_options()
1743 GtkWidget *vbox, *menu, *hbox;
1745 vbox = gtk_vbox_new(FALSE, 0);
1746 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
1748 display_label = gtk_label_new("<>");
1749 gtk_label_set_line_wrap(GTK_LABEL(display_label), TRUE);
1750 gtk_box_pack_start(GTK_BOX(vbox), display_label, FALSE, TRUE, 0);
1752 toggle_new_window_on_1 =
1753 gtk_check_button_new_with_label("New window on button 1 "
1754 "(RISC OS style)");
1755 gtk_box_pack_start(GTK_BOX(vbox), toggle_new_window_on_1,
1756 FALSE, TRUE, 0);
1758 toggle_menu_on_2 =
1759 gtk_check_button_new_with_label("Menu on button 2 "
1760 "(RISC OS style)");
1761 gtk_box_pack_start(GTK_BOX(vbox), toggle_menu_on_2, FALSE, TRUE, 0);
1763 toggle_single_click =
1764 gtk_check_button_new_with_label("Single-click nagivation");
1765 gtk_box_pack_start(GTK_BOX(vbox), toggle_single_click, FALSE, TRUE, 0);
1767 toggle_unique_filer_windows =
1768 gtk_check_button_new_with_label("Unique windows");
1769 gtk_box_pack_start(GTK_BOX(vbox), toggle_unique_filer_windows, FALSE, TRUE, 0);
1771 hbox = gtk_hbox_new(FALSE, 4);
1772 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
1774 gtk_box_pack_start(GTK_BOX(hbox),
1775 gtk_label_new("Toolbar type for new windows"),
1776 FALSE, TRUE, 0);
1777 menu_toolbar = gtk_option_menu_new();
1778 menu = gtk_menu_new();
1779 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("None"));
1780 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("Normal"));
1781 gtk_menu_append(GTK_MENU(menu), gtk_menu_item_new_with_label("GNOME"));
1782 gtk_option_menu_set_menu(GTK_OPTION_MENU(menu_toolbar), menu);
1783 gtk_box_pack_start(GTK_BOX(hbox), menu_toolbar, TRUE, TRUE, 0);
1785 return vbox;
1788 static void update_options_label(void)
1790 guchar *str;
1792 str = g_strdup_printf("The last used display style (%s) and sort "
1793 "function (Sort By %s) will be saved if you click on "
1794 "Save.", style_to_name(), sort_fn_to_name());
1795 gtk_label_set_text(GTK_LABEL(display_label), str);
1796 g_free(str);
1799 /* Reflect current state by changing the widgets in the options box */
1800 static void update_options()
1802 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_new_window_on_1),
1803 o_new_window_on_1);
1804 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_menu_on_2),
1805 collection_menu_button == 2 ? 1 : 0);
1806 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_single_click),
1807 o_single_click);
1808 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_unique_filer_windows),
1809 o_unique_filer_windows);
1810 gtk_option_menu_set_history(GTK_OPTION_MENU(menu_toolbar), o_toolbar);
1812 update_options_label();
1815 /* Set current values by reading the states of the widgets in the options box */
1816 static void set_options()
1818 GtkWidget *item, *menu;
1819 GList *list;
1821 o_new_window_on_1 = gtk_toggle_button_get_active(
1822 GTK_TOGGLE_BUTTON(toggle_new_window_on_1));
1824 collection_menu_button = gtk_toggle_button_get_active(
1825 GTK_TOGGLE_BUTTON(toggle_menu_on_2)) ? 2 : 3;
1827 o_single_click = gtk_toggle_button_get_active(
1828 GTK_TOGGLE_BUTTON(toggle_single_click));
1830 o_unique_filer_windows = gtk_toggle_button_get_active(
1831 GTK_TOGGLE_BUTTON(toggle_unique_filer_windows));
1833 collection_single_click = o_single_click ? TRUE : FALSE;
1835 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(menu_toolbar));
1836 item = gtk_menu_get_active(GTK_MENU(menu));
1837 list = gtk_container_children(GTK_CONTAINER(menu));
1838 o_toolbar = (ToolbarType) g_list_index(list, item);
1839 g_list_free(list);
1843 static guchar *style_to_name(void)
1845 return last_display_style == LARGE_ICONS ? "Large Icons" :
1846 last_display_style == SMALL_ICONS ? "Small Icons" :
1847 "Full Info";
1850 static guchar *sort_fn_to_name(void)
1852 return last_sort_fn == sort_by_name ? "Name" :
1853 last_sort_fn == sort_by_type ? "Type" :
1854 last_sort_fn == sort_by_date ? "Date" :
1855 "Size";
1858 static void save_options()
1860 option_write("filer_new_window_on_1", o_new_window_on_1 ? "1" : "0");
1861 option_write("filer_menu_on_2",
1862 collection_menu_button == 2 ? "1" : "0");
1863 option_write("filer_single_click", o_single_click ? "1" : "0");
1864 option_write("filer_unique_windows", o_unique_filer_windows ? "1" : "0");
1865 option_write("filer_display_style", style_to_name());
1866 option_write("filer_sort_by", sort_fn_to_name());
1867 option_write("filer_toolbar", o_toolbar == TOOLBAR_NONE ? "None" :
1868 o_toolbar == TOOLBAR_NORMAL ? "Normal" :
1869 o_toolbar == TOOLBAR_GNOME ? "GNOME" :
1870 "Unknown");
1873 static char *filer_new_window_on_1(char *data)
1875 o_new_window_on_1 = atoi(data) != 0;
1876 return NULL;
1879 static char *filer_menu_on_2(char *data)
1881 collection_menu_button = atoi(data) != 0 ? 2 : 3;
1882 return NULL;
1885 static char *filer_single_click(char *data)
1887 o_single_click = atoi(data) != 0;
1888 collection_single_click = o_single_click ? TRUE : FALSE;
1889 return NULL;
1892 static char *filer_unique_windows(char *data)
1894 o_unique_filer_windows = atoi(data) != 0;
1895 return NULL;
1898 static char *filer_display_style(char *data)
1900 if (g_strcasecmp(data, "Large Icons") == 0)
1901 last_display_style = LARGE_ICONS;
1902 else if (g_strcasecmp(data, "Small Icons") == 0)
1903 last_display_style = SMALL_ICONS;
1904 else if (g_strcasecmp(data, "Full Info") == 0)
1905 last_display_style = FULL_INFO;
1906 else
1907 return "Unknown display style";
1909 return NULL;
1912 static char *filer_sort_by(char *data)
1914 if (g_strcasecmp(data, "Name") == 0)
1915 last_sort_fn = sort_by_name;
1916 else if (g_strcasecmp(data, "Type") == 0)
1917 last_sort_fn = sort_by_type;
1918 else if (g_strcasecmp(data, "Date") == 0)
1919 last_sort_fn = sort_by_date;
1920 else if (g_strcasecmp(data, "Size") == 0)
1921 last_sort_fn = sort_by_size;
1922 else
1923 return "Unknown sort type";
1925 return NULL;
1928 static char *filer_toolbar(char *data)
1930 if (g_strcasecmp(data, "None") == 0)
1931 o_toolbar = TOOLBAR_NONE;
1932 else if (g_strcasecmp(data, "Normal") == 0)
1933 o_toolbar = TOOLBAR_NORMAL;
1934 else if (g_strcasecmp(data, "GNOME") == 0)
1935 o_toolbar = TOOLBAR_GNOME;
1936 else
1937 return "Unknown toolbar type";
1939 return NULL;
1942 /* Note that filer_window may not exist after this call. */
1943 void update_dir(FilerWindow *filer_window, gboolean warning)
1945 if (may_rescan(filer_window, warning))
1946 dir_update(filer_window->directory, filer_window->path);
1949 void filer_set_hidden(FilerWindow *filer_window, gboolean hidden)
1951 Directory *dir = filer_window->directory;
1953 if (filer_window->show_hidden == hidden)
1954 return;
1956 filer_window->show_hidden = hidden;
1957 last_show_hidden = hidden;
1959 g_fscache_data_ref(dir_cache, dir);
1960 detach(filer_window);
1961 filer_window->directory = dir;
1962 attach(filer_window);
1965 /* Refresh the various caches even if we don't think we need to */
1966 void full_refresh(void)
1968 mount_update(TRUE);
1971 /* See whether a filer window with a given path already exists
1972 * and is different from diff.
1974 static FilerWindow *find_filer_window(char *path, FilerWindow *diff)
1976 GList *next = all_filer_windows;
1978 while (next)
1980 FilerWindow *filer_window = (FilerWindow *) next->data;
1982 if (filer_window->panel_type == PANEL_NO &&
1983 filer_window != diff &&
1984 strcmp(path, filer_window->path) == 0)
1986 return filer_window;
1989 next = next->next;
1992 return NULL;
1995 /* This path has been mounted/umounted/deleted some files - update all dirs */
1996 void filer_check_mounted(char *path)
1998 GList *next = all_filer_windows;
1999 int len;
2001 len = strlen(path);
2003 while (next)
2005 FilerWindow *filer_window = (FilerWindow *) next->data;
2007 next = next->next;
2009 if (strncmp(path, filer_window->path, len) == 0)
2011 char s = filer_window->path[len];
2013 if (s == '/' || s == '\0')
2014 update_dir(filer_window, FALSE);
2019 /* Like minibuffer_show(), except that:
2020 * - It returns FALSE (to be used from an idle callback)
2021 * - It checks that the filer window still exists.
2023 static gboolean minibuffer_show_cb(FilerWindow *filer_window)
2025 if (exists(filer_window))
2026 minibuffer_show(filer_window, MINI_PATH);
2027 return FALSE;
2030 static gboolean exists(FilerWindow *filer_window)
2032 GList *next;
2034 for (next = all_filer_windows; next; next = next->next)
2036 FilerWindow *fw = (FilerWindow *) next->data;
2038 if (fw == filer_window)
2039 return TRUE;
2042 return FALSE;
2045 /* Highlight (wink or cursor) this item in the filer window. If the item
2046 * isn't already there but we're scanning then highlight it if it
2047 * appears later.
2049 void filer_set_autoselect(FilerWindow *filer_window, guchar *leaf)
2051 Collection *col = filer_window->collection;
2052 int i;
2054 g_free(filer_window->auto_select);
2055 filer_window->auto_select = NULL;
2057 for (i = 0; i < col->number_of_items; i++)
2059 DirItem *item = (DirItem *) col->items[i].data;
2061 if (strcmp(item->leafname, leaf) == 0)
2063 if (col->cursor_item != -1)
2064 collection_set_cursor_item(col, i);
2065 else
2066 collection_wink_item(col, i);
2067 return;
2071 filer_window->auto_select = g_strdup(leaf);
2074 static void filer_set_title(FilerWindow *filer_window)
2076 if (filer_window->scanning)
2078 guchar *title;
2080 title = g_strdup_printf("%s (Scanning)", filer_window->path);
2081 gtk_window_set_title(GTK_WINDOW(filer_window->window),
2082 title);
2083 g_free(title);
2085 else
2086 gtk_window_set_title(GTK_WINDOW(filer_window->window),
2087 filer_window->path);