2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* display.c - code for arranging and displaying file items */
29 #include <sys/param.h>
36 #include <gdk/gdkkeysyms.h>
44 #include "gui_support.h"
53 #include "minibuffer.h"
57 #include "view_iface.h"
59 #define HUGE_WRAP (1.5 * o_large_width.int_value)
62 static Option o_display_caps_first
;
63 static Option o_display_dirs_first
;
64 Option o_display_size
;
65 Option o_display_details
;
66 Option o_display_sort_by
;
67 static Option o_large_width
;
69 Option o_display_show_hidden
;
70 Option o_display_show_thumbs
;
71 Option o_display_show_headers
;
72 Option o_display_show_full_type
;
73 Option o_display_inherit_options
;
74 static Option o_filer_change_size_num
;
75 Option o_vertical_order_small
, o_vertical_order_large
;
77 /* Static prototypes */
78 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
);
79 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
);
80 static void options_changed(void);
81 static char *details(FilerWindow
*filer_window
, DirItem
*item
);
82 static void display_set_actual_size_real(FilerWindow
*filer_window
);
84 /****************************************************************
85 * EXTERNAL INTERFACE *
86 ****************************************************************/
90 option_add_int(&o_display_caps_first
, "display_caps_first", FALSE
);
91 option_add_int(&o_display_dirs_first
, "display_dirs_first", FALSE
);
92 option_add_int(&o_display_size
, "display_icon_size", AUTO_SIZE_ICONS
);
93 option_add_int(&o_display_details
, "display_details", DETAILS_NONE
);
94 option_add_int(&o_display_sort_by
, "display_sort_by", SORT_NAME
);
95 option_add_int(&o_large_width
, "display_large_width", 155);
96 option_add_int(&o_small_width
, "display_small_width", 250);
97 option_add_int(&o_display_show_hidden
, "display_show_hidden", FALSE
);
98 option_add_int(&o_display_show_thumbs
, "display_show_thumbs", FALSE
);
99 option_add_int(&o_display_show_headers
, "display_show_headers", TRUE
);
100 option_add_int(&o_display_show_full_type
, "display_show_full_type", FALSE
);
101 option_add_int(&o_display_inherit_options
,
102 "display_inherit_options", FALSE
);
103 option_add_int(&o_filer_change_size_num
, "filer_change_size_num", 30);
104 option_add_int(&o_vertical_order_small
, "vertical_order_small", FALSE
);
105 option_add_int(&o_vertical_order_large
, "vertical_order_large", FALSE
);
107 option_add_notify(options_changed
);
110 static void draw_emblem_on_icon(GdkWindow
*window
, MaskedPixmap
*image
,
113 gdk_pixbuf_render_to_drawable_alpha(image
->pixbuf
,
118 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
119 GDK_RGB_DITHER_NORMAL
, 0, 0);
124 /* Draw this icon (including any symlink or mount symbol) inside the
127 void draw_huge_icon(GdkWindow
*window
, GdkRectangle
*area
,
128 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
,
139 width
= image
->huge_width
;
140 height
= image
->huge_height
;
141 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
142 image_y
= MAX(0, area
->height
- height
- 6);
145 ? create_spotlight_pixbuf(image
->huge_pixbuf
, color
)
146 : image
->huge_pixbuf
;
148 gdk_pixbuf_render_to_drawable_alpha(
152 image_x
, area
->y
+ image_y
, /* dest */
154 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
155 GDK_RGB_DITHER_NORMAL
, 0, 0);
158 g_object_unref(pixbuf
);
160 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
162 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
165 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
167 if (item
->flags
& ITEM_FLAG_SYMLINK
)
169 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
171 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
173 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
177 /* Draw this icon (including any symlink or mount symbol) inside the
180 void draw_large_icon(GdkWindow
*window
,
196 width
= MIN(image
->width
, ICON_WIDTH
);
197 height
= MIN(image
->height
, ICON_HEIGHT
);
198 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
199 image_y
= MAX(0, area
->height
- height
- 6);
202 ? create_spotlight_pixbuf(image
->pixbuf
, color
)
205 gdk_pixbuf_render_to_drawable_alpha(
209 image_x
, area
->y
+ image_y
, /* dest */
211 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
212 GDK_RGB_DITHER_NORMAL
, 0, 0);
215 g_object_unref(pixbuf
);
217 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
219 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
222 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
224 if (item
->flags
& ITEM_FLAG_SYMLINK
)
226 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
228 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
230 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
234 void draw_small_icon(GdkWindow
*window
, GdkRectangle
*area
,
235 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
,
238 int width
, height
, image_x
, image_y
;
244 if (!image
->sm_pixbuf
)
245 pixmap_make_small(image
);
247 width
= MIN(image
->sm_width
, SMALL_WIDTH
);
248 height
= MIN(image
->sm_height
, SMALL_HEIGHT
);
249 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
250 image_y
= MAX(0, SMALL_HEIGHT
- image
->sm_height
);
253 ? create_spotlight_pixbuf(image
->sm_pixbuf
, color
)
256 gdk_pixbuf_render_to_drawable_alpha(
260 image_x
, area
->y
+ image_y
, /* dest */
262 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
263 GDK_RGB_DITHER_NORMAL
, 0, 0);
266 g_object_unref(pixbuf
);
268 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
270 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
273 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
275 if (item
->flags
& ITEM_FLAG_SYMLINK
)
277 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 8);
279 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
281 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 8);
285 /* The sort functions aren't called from outside, but they are
286 * passed as arguments to display_set_sort_fn().
289 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
290 !(item->flags & ITEM_FLAG_APPDIR))
293 if (o_display_dirs_first.int_value) { \
294 gboolean id1 = IS_A_DIR(i1); \
295 gboolean id2 = IS_A_DIR(i2); \
296 if (id1 && !id2) return -1; \
297 if (id2 && !id1) return 1; \
300 int sort_by_name(const void *item1
, const void *item2
)
302 const DirItem
*i1
= (DirItem
*) item1
;
303 const DirItem
*i2
= (DirItem
*) item2
;
304 CollateKey
*n1
= i1
->leafname_collate
;
305 CollateKey
*n2
= i2
->leafname_collate
;
310 retval
= collate_key_cmp(n1
, n2
, o_display_caps_first
.int_value
);
312 return retval
? retval
: strcmp(i1
->leafname
, i2
->leafname
);
315 int sort_by_type(const void *item1
, const void *item2
)
317 const DirItem
*i1
= (DirItem
*) item1
;
318 const DirItem
*i2
= (DirItem
*) item2
;
321 int diff
= i1
->base_type
- i2
->base_type
;
324 diff
= (i1
->flags
& ITEM_FLAG_APPDIR
)
325 - (i2
->flags
& ITEM_FLAG_APPDIR
);
327 return diff
> 0 ? 1 : -1;
334 diff
= strcmp(m1
->media_type
, m2
->media_type
);
336 diff
= strcmp(m1
->subtype
, m2
->subtype
);
344 return diff
> 0 ? 1 : -1;
346 return sort_by_name(item1
, item2
);
349 int sort_by_owner(const void *item1
, const void *item2
)
351 const DirItem
*i1
= (DirItem
*) item1
;
352 const DirItem
*i2
= (DirItem
*) item2
;
357 return sort_by_name(item1
, item2
);
359 name1
=user_name(i1
->uid
);
360 name2
=user_name(i2
->uid
);
362 return strcmp(name1
, name2
);
365 int sort_by_group(const void *item1
, const void *item2
)
367 const DirItem
*i1
= (DirItem
*) item1
;
368 const DirItem
*i2
= (DirItem
*) item2
;
373 return sort_by_name(item1
, item2
);
375 name1
=group_name(i1
->gid
);
376 name2
=group_name(i2
->gid
);
378 return strcmp(name1
, name2
);
381 int sort_by_date(const void *item1
, const void *item2
)
383 const DirItem
*i1
= (DirItem
*) item1
;
384 const DirItem
*i2
= (DirItem
*) item2
;
386 /* SORT_DIRS; -- too confusing! */
388 return i1
->mtime
< i2
->mtime
? -1 :
389 i1
->mtime
> i2
->mtime
? 1 :
390 sort_by_name(item1
, item2
);
393 int sort_by_size(const void *item1
, const void *item2
)
395 const DirItem
*i1
= (DirItem
*) item1
;
396 const DirItem
*i2
= (DirItem
*) item2
;
400 return i1
->size
< i2
->size
? -1 :
401 i1
->size
> i2
->size
? 1 :
402 sort_by_name(item1
, item2
);
405 void display_set_sort_type(FilerWindow
*filer_window
, SortType sort_type
,
408 if (filer_window
->sort_type
== sort_type
&&
409 filer_window
->sort_order
== order
)
412 filer_window
->sort_type
= sort_type
;
413 filer_window
->sort_order
= order
;
415 view_sort(filer_window
->view
);
418 /* Change the icon size and style.
419 * force_resize should only be TRUE for new windows.
421 void display_set_layout(FilerWindow
*filer_window
,
424 gboolean force_resize
)
426 gboolean style_changed
= FALSE
;
428 g_return_if_fail(filer_window
!= NULL
);
430 if (filer_window
->display_style_wanted
!= style
431 || filer_window
->details_type
!= details
)
433 style_changed
= TRUE
;
436 display_style_set(filer_window
, style
);
437 display_details_set(filer_window
, details
);
439 /* Recreate layouts because wrapping may have changed */
440 view_style_changed(filer_window
->view
, VIEW_UPDATE_NAME
);
442 if (force_resize
|| o_filer_auto_resize
.int_value
== RESIZE_ALWAYS
443 || (o_filer_auto_resize
.int_value
== RESIZE_STYLE
&& style_changed
))
445 view_autosize(filer_window
->view
);
449 /* Set the 'Show Thumbnails' flag for this window */
450 void display_set_thumbs(FilerWindow
*filer_window
, gboolean thumbs
)
452 if (filer_window
->show_thumbs
== thumbs
)
455 filer_window
->show_thumbs
= thumbs
;
457 view_style_changed(filer_window
->view
, VIEW_UPDATE_VIEWDATA
);
460 filer_cancel_thumbnails(filer_window
);
462 filer_set_title(filer_window
);
464 filer_create_thumbs(filer_window
);
467 void display_update_hidden(FilerWindow
*filer_window
)
469 filer_detach_rescan(filer_window
); /* (updates titlebar) */
471 display_set_actual_size(filer_window
, FALSE
);
474 /* Set the 'Show Hidden' flag for this window */
475 void display_set_hidden(FilerWindow
*filer_window
, gboolean hidden
)
477 if (filer_window
->show_hidden
== hidden
)
481 filer_window->show_hidden = hidden;
483 filer_set_hidden(filer_window
, hidden
);
485 display_update_hidden(filer_window
);
488 void display_set_filter(FilerWindow
*filer_window
, FilterType type
,
489 const gchar
*filter_string
)
491 if (filer_set_filter(filer_window
, type
, filter_string
))
492 display_update_hidden(filer_window
);
496 /* Highlight (wink or cursor) this item in the filer window. If the item
497 * isn't already there but we're scanning then highlight it if it
500 void display_set_autoselect(FilerWindow
*filer_window
, const gchar
*leaf
)
504 g_return_if_fail(filer_window
!= NULL
);
505 g_return_if_fail(leaf
!= NULL
);
507 new = g_strdup(leaf
); /* leaf == old value sometimes */
509 null_g_free(&filer_window
->auto_select
);
511 if (view_autoselect(filer_window
->view
, new))
514 filer_window
->auto_select
= new;
517 /* Change the icon size (wraps) */
518 void display_change_size(FilerWindow
*filer_window
, gboolean bigger
)
522 g_return_if_fail(filer_window
!= NULL
);
524 switch (filer_window
->display_style
)
527 new = bigger
? HUGE_ICONS
: SMALL_ICONS
;
541 display_set_layout(filer_window
, new, filer_window
->details_type
,
545 ViewData
*display_create_viewdata(FilerWindow
*filer_window
, DirItem
*item
)
549 view
= g_new(ViewData
, 1);
552 view
->details
= NULL
;
555 display_update_view(filer_window
, item
, view
, TRUE
);
560 /* Set the display style to the desired style. If the desired style
561 * is AUTO_SIZE_ICONS, choose an appropriate size. Also resizes filer
562 * window, if requested.
564 void display_set_actual_size(FilerWindow
*filer_window
, gboolean force_resize
)
566 display_set_layout(filer_window
, filer_window
->display_style_wanted
,
567 filer_window
->details_type
, force_resize
);
571 /****************************************************************
572 * INTERNAL FUNCTIONS *
573 ****************************************************************/
575 static void options_changed(void)
579 for (next
= all_filer_windows
; next
; next
= next
->next
)
581 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
584 if (o_display_dirs_first
.has_changed
||
585 o_display_caps_first
.has_changed
)
586 view_sort(VIEW(filer_window
->view
));
588 if (o_display_show_headers
.has_changed
)
589 flags
|= VIEW_UPDATE_HEADERS
;
591 if (o_large_width
.has_changed
|| o_small_width
.has_changed
)
592 flags
|= VIEW_UPDATE_NAME
; /* Recreate PangoLayout */
594 view_style_changed(filer_window
->view
, flags
);
598 /* Return a new string giving details of this item, or NULL if details
599 * are not being displayed. If details are not yet available, return
600 * a string of the right length.
602 static char *details(FilerWindow
*filer_window
, DirItem
*item
)
604 mode_t m
= item
->mode
;
606 gboolean scanned
= item
->base_type
!= TYPE_UNKNOWN
;
608 if (filer_window
->details_type
== DETAILS_NONE
)
611 if (scanned
&& item
->lstat_errno
)
612 buf
= g_strdup_printf(_("lstat(2) failed: %s"),
613 g_strerror(item
->lstat_errno
));
614 else if (filer_window
->details_type
== DETAILS_TYPE
)
616 MIME_type
*type
= item
->mime_type
;
619 return g_strdup("application/octet-stream");
621 buf
= g_strdup_printf("%s/%s",
622 type
->media_type
, type
->subtype
);
624 else if (filer_window
->details_type
== DETAILS_TIMES
)
626 guchar
*ctime
, *mtime
, *atime
;
628 ctime
= pretty_time(&item
->ctime
);
629 mtime
= pretty_time(&item
->mtime
);
630 atime
= pretty_time(&item
->atime
);
632 buf
= g_strdup_printf("a[%s] c[%s] m[%s]", atime
, ctime
, mtime
);
637 else if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
640 return g_strdup("---,---,---/--"
644 " 12345678 12345678");
646 buf
= g_strdup_printf("%s %-8.8s %-8.8s",
647 pretty_permissions(m
),
648 user_name(item
->uid
),
649 group_name(item
->gid
));
655 if (filer_window
->display_style
== SMALL_ICONS
)
656 return g_strdup("1234M");
658 return g_strdup("1234 bytes");
661 if (item
->base_type
!= TYPE_DIRECTORY
)
663 if (filer_window
->display_style
== SMALL_ICONS
)
664 buf
= g_strdup(format_size_aligned(item
->size
));
666 buf
= g_strdup(format_size(item
->size
));
675 /* Note: Call style_changed after this */
676 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
)
678 filer_window
->details_type
= details
;
681 /* Note: Call style_changed after this */
682 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
)
684 filer_window
->display_style_wanted
= style
;
685 display_set_actual_size_real(filer_window
);
688 /* Each displayed item has a ViewData structure with some cached information
689 * to help quickly draw the item (eg, the PangoLayout). This function updates
692 void display_update_view(FilerWindow
*filer_window
,
695 gboolean update_name_layout
)
697 DisplayStyle style
= filer_window
->display_style
;
701 static PangoFontDescription
*monospace
= NULL
;
702 PangoAttrList
*list
= NULL
;
705 monospace
= pango_font_description_from_string("monospace");
709 g_object_unref(G_OBJECT(view
->details
));
710 view
->details
= NULL
;
713 str
= details(filer_window
, item
);
716 PangoAttrList
*details_list
;
717 int perm_offset
= -1;
719 view
->details
= gtk_widget_create_pango_layout(
720 filer_window
->window
, str
);
723 pango_layout_set_font_description(view
->details
, monospace
);
724 pango_layout_get_size(view
->details
, &w
, &h
);
725 view
->details_width
= w
/ PANGO_SCALE
;
726 view
->details_height
= h
/ PANGO_SCALE
;
728 if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
730 if (perm_offset
> -1)
732 PangoAttribute
*attr
;
734 attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
736 perm_offset
+= 4 * applicable(item
->uid
, item
->gid
);
737 attr
->start_index
= perm_offset
;
738 attr
->end_index
= perm_offset
+ 3;
740 details_list
= pango_attr_list_new();
741 pango_attr_list_insert(details_list
, attr
);
742 pango_layout_set_attributes(view
->details
,
749 g_object_unref(view
->image
);
753 if (filer_window
->show_thumbs
&& item
->base_type
== TYPE_FILE
/*&&
754 strcmp(item->mime_type->media_type, "image") == 0*/)
758 path
= make_path(filer_window
->real_path
, item
->leafname
);
760 view
->image
= g_fscache_lookup_full(pixmap_cache
, path
,
761 FSCACHE_LOOKUP_ONLY_NEW
, NULL
);
766 view
->image
= di_image(item
);
768 g_object_ref(view
->image
);
771 if (view
->layout
&& update_name_layout
)
773 g_object_unref(G_OBJECT(view
->layout
));
781 else if (g_utf8_validate(item
->leafname
, -1, NULL
))
783 view
->layout
= gtk_widget_create_pango_layout(
784 filer_window
->window
, item
->leafname
);
788 PangoAttribute
*attr
;
791 utf8
= to_utf8(item
->leafname
);
792 view
->layout
= gtk_widget_create_pango_layout(
793 filer_window
->window
, utf8
);
796 attr
= pango_attr_foreground_new(0xffff, 0, 0);
797 attr
->start_index
= 0;
798 attr
->end_index
= -1;
800 list
= pango_attr_list_new();
801 pango_attr_list_insert(list
, attr
);
804 if (item
->flags
& ITEM_FLAG_RECENT
)
806 PangoAttribute
*attr
;
808 attr
= pango_attr_weight_new(PANGO_WEIGHT_BOLD
);
809 attr
->start_index
= 0;
810 attr
->end_index
= -1;
812 list
= pango_attr_list_new();
813 pango_attr_list_insert(list
, attr
);
817 pango_layout_set_attributes(view
->layout
, list
);
819 if (filer_window
->details_type
== DETAILS_NONE
)
821 if (style
== HUGE_ICONS
)
822 wrap_width
= HUGE_WRAP
* PANGO_SCALE
;
823 else if (style
== LARGE_ICONS
)
824 wrap_width
= o_large_width
.int_value
* PANGO_SCALE
;
827 #ifdef USE_PANGO_WRAP_WORD_CHAR
828 pango_layout_set_wrap(view
->layout
, PANGO_WRAP_WORD_CHAR
);
830 if (wrap_width
!= -1)
831 pango_layout_set_width(view
->layout
, wrap_width
);
833 pango_layout_get_size(view
->layout
, &w
, &h
);
834 view
->name_width
= w
/ PANGO_SCALE
;
835 view
->name_height
= h
/ PANGO_SCALE
;
838 /* Sets display_style from display_style_wanted.
839 * See also display_set_actual_size().
841 static void display_set_actual_size_real(FilerWindow
*filer_window
)
843 DisplayStyle size
= filer_window
->display_style_wanted
;
846 g_return_if_fail(filer_window
!= NULL
);
848 if (size
== AUTO_SIZE_ICONS
)
850 n
= view_count_items(filer_window
->view
);
852 if (n
>= o_filer_change_size_num
.int_value
)
858 filer_window
->display_style
= size
;