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"
60 #define HUGE_WRAP (1.5 * o_large_width.int_value)
63 static Option o_display_caps_first
;
64 static Option o_display_dirs_first
;
65 Option o_display_size
;
66 Option o_display_details
;
67 Option o_display_sort_by
;
68 static Option o_large_width
;
70 Option o_display_show_hidden
;
71 Option o_display_show_thumbs
;
72 Option o_display_show_headers
;
73 Option o_display_show_full_type
;
74 Option o_display_inherit_options
;
75 static Option o_filer_change_size_num
;
76 Option o_vertical_order_small
, o_vertical_order_large
;
79 /* Static prototypes */
80 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
);
81 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
);
82 static void options_changed(void);
83 static char *details(FilerWindow
*filer_window
, DirItem
*item
);
84 static void display_set_actual_size_real(FilerWindow
*filer_window
);
86 /****************************************************************
87 * EXTERNAL INTERFACE *
88 ****************************************************************/
92 option_add_int(&o_display_caps_first
, "display_caps_first", FALSE
);
93 option_add_int(&o_display_dirs_first
, "display_dirs_first", FALSE
);
94 option_add_int(&o_display_size
, "display_icon_size", AUTO_SIZE_ICONS
);
95 option_add_int(&o_display_details
, "display_details", DETAILS_NONE
);
96 option_add_int(&o_display_sort_by
, "display_sort_by", SORT_NAME
);
97 option_add_int(&o_large_width
, "display_large_width", 155);
98 option_add_int(&o_small_width
, "display_small_width", 250);
99 option_add_int(&o_display_show_hidden
, "display_show_hidden", FALSE
);
100 option_add_int(&o_display_show_thumbs
, "display_show_thumbs", FALSE
);
101 option_add_int(&o_display_show_headers
, "display_show_headers", TRUE
);
102 option_add_int(&o_display_show_full_type
, "display_show_full_type", FALSE
);
103 option_add_int(&o_display_inherit_options
,
104 "display_inherit_options", FALSE
);
105 option_add_int(&o_filer_change_size_num
, "filer_change_size_num", 30);
106 option_add_int(&o_vertical_order_small
, "vertical_order_small", FALSE
);
107 option_add_int(&o_vertical_order_large
, "vertical_order_large", FALSE
);
108 option_add_int(&o_xattr_show
, "xattr_show", TRUE
);
110 option_add_notify(options_changed
);
113 static void draw_emblem_on_icon(GdkWindow
*window
, MaskedPixmap
*image
,
116 gdk_pixbuf_render_to_drawable_alpha(image
->pixbuf
,
121 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
122 GDK_RGB_DITHER_NORMAL
, 0, 0);
127 /* Draw this icon (including any symlink or mount symbol) inside the
130 void draw_huge_icon(GdkWindow
*window
, GdkRectangle
*area
,
131 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
,
142 width
= image
->huge_width
;
143 height
= image
->huge_height
;
144 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
145 image_y
= MAX(0, area
->height
- height
- 6);
148 ? create_spotlight_pixbuf(image
->huge_pixbuf
, color
)
149 : image
->huge_pixbuf
;
151 gdk_pixbuf_render_to_drawable_alpha(
155 image_x
, area
->y
+ image_y
, /* dest */
157 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
158 GDK_RGB_DITHER_NORMAL
, 0, 0);
161 g_object_unref(pixbuf
);
163 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
165 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
168 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
170 if (item
->flags
& ITEM_FLAG_SYMLINK
)
172 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
174 if ((item
->flags
& ITEM_FLAG_HAS_XATTR
) && o_xattr_show
.int_value
)
176 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
180 /* Draw this icon (including any symlink or mount symbol) inside the
183 void draw_large_icon(GdkWindow
*window
,
199 width
= MIN(image
->width
, ICON_WIDTH
);
200 height
= MIN(image
->height
, ICON_HEIGHT
);
201 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
202 image_y
= MAX(0, area
->height
- height
- 6);
205 ? create_spotlight_pixbuf(image
->pixbuf
, color
)
208 gdk_pixbuf_render_to_drawable_alpha(
212 image_x
, area
->y
+ image_y
, /* dest */
214 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
215 GDK_RGB_DITHER_NORMAL
, 0, 0);
218 g_object_unref(pixbuf
);
220 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
222 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
225 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
227 if (item
->flags
& ITEM_FLAG_SYMLINK
)
229 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
231 if ((item
->flags
& ITEM_FLAG_HAS_XATTR
) && o_xattr_show
.int_value
)
233 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
237 void draw_small_icon(GdkWindow
*window
, GdkRectangle
*area
,
238 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
,
241 int width
, height
, image_x
, image_y
;
247 if (!image
->sm_pixbuf
)
248 pixmap_make_small(image
);
250 width
= MIN(image
->sm_width
, SMALL_WIDTH
);
251 height
= MIN(image
->sm_height
, SMALL_HEIGHT
);
252 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
253 image_y
= MAX(0, SMALL_HEIGHT
- image
->sm_height
);
256 ? create_spotlight_pixbuf(image
->sm_pixbuf
, color
)
259 gdk_pixbuf_render_to_drawable_alpha(
263 image_x
, area
->y
+ image_y
, /* dest */
265 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
266 GDK_RGB_DITHER_NORMAL
, 0, 0);
269 g_object_unref(pixbuf
);
271 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
273 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
276 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
278 if (item
->flags
& ITEM_FLAG_SYMLINK
)
280 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 8);
282 if ((item
->flags
& ITEM_FLAG_HAS_XATTR
) && o_xattr_show
.int_value
)
284 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 8);
288 /* The sort functions aren't called from outside, but they are
289 * passed as arguments to display_set_sort_fn().
292 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
293 !(item->flags & ITEM_FLAG_APPDIR))
296 if (o_display_dirs_first.int_value) { \
297 gboolean id1 = IS_A_DIR(i1); \
298 gboolean id2 = IS_A_DIR(i2); \
299 if (id1 && !id2) return -1; \
300 if (id2 && !id1) return 1; \
303 int sort_by_name(const void *item1
, const void *item2
)
305 const DirItem
*i1
= (DirItem
*) item1
;
306 const DirItem
*i2
= (DirItem
*) item2
;
307 CollateKey
*n1
= i1
->leafname_collate
;
308 CollateKey
*n2
= i2
->leafname_collate
;
313 retval
= collate_key_cmp(n1
, n2
, o_display_caps_first
.int_value
);
315 return retval
? retval
: strcmp(i1
->leafname
, i2
->leafname
);
318 int sort_by_type(const void *item1
, const void *item2
)
320 const DirItem
*i1
= (DirItem
*) item1
;
321 const DirItem
*i2
= (DirItem
*) item2
;
324 int diff
= i1
->base_type
- i2
->base_type
;
327 diff
= (i1
->flags
& ITEM_FLAG_APPDIR
)
328 - (i2
->flags
& ITEM_FLAG_APPDIR
);
330 return diff
> 0 ? 1 : -1;
337 diff
= strcmp(m1
->media_type
, m2
->media_type
);
339 diff
= strcmp(m1
->subtype
, m2
->subtype
);
347 return diff
> 0 ? 1 : -1;
349 return sort_by_name(item1
, item2
);
352 int sort_by_owner(const void *item1
, const void *item2
)
354 const DirItem
*i1
= (DirItem
*) item1
;
355 const DirItem
*i2
= (DirItem
*) item2
;
360 return sort_by_name(item1
, item2
);
362 name1
=user_name(i1
->uid
);
363 name2
=user_name(i2
->uid
);
365 return strcmp(name1
, name2
);
368 int sort_by_group(const void *item1
, const void *item2
)
370 const DirItem
*i1
= (DirItem
*) item1
;
371 const DirItem
*i2
= (DirItem
*) item2
;
376 return sort_by_name(item1
, item2
);
378 name1
=group_name(i1
->gid
);
379 name2
=group_name(i2
->gid
);
381 return strcmp(name1
, name2
);
384 int sort_by_date(const void *item1
, const void *item2
)
386 const DirItem
*i1
= (DirItem
*) item1
;
387 const DirItem
*i2
= (DirItem
*) item2
;
389 /* SORT_DIRS; -- too confusing! */
391 return i1
->mtime
< i2
->mtime
? -1 :
392 i1
->mtime
> i2
->mtime
? 1 :
393 sort_by_name(item1
, item2
);
396 int sort_by_size(const void *item1
, const void *item2
)
398 const DirItem
*i1
= (DirItem
*) item1
;
399 const DirItem
*i2
= (DirItem
*) item2
;
403 return i1
->size
< i2
->size
? -1 :
404 i1
->size
> i2
->size
? 1 :
405 sort_by_name(item1
, item2
);
408 void display_set_sort_type(FilerWindow
*filer_window
, SortType sort_type
,
411 if (filer_window
->sort_type
== sort_type
&&
412 filer_window
->sort_order
== order
)
415 filer_window
->sort_type
= sort_type
;
416 filer_window
->sort_order
= order
;
418 view_sort(filer_window
->view
);
421 /* Change the icon size and style.
422 * force_resize should only be TRUE for new windows.
424 void display_set_layout(FilerWindow
*filer_window
,
427 gboolean force_resize
)
429 gboolean style_changed
= FALSE
;
431 g_return_if_fail(filer_window
!= NULL
);
433 if (filer_window
->display_style_wanted
!= style
434 || filer_window
->details_type
!= details
)
436 style_changed
= TRUE
;
439 display_style_set(filer_window
, style
);
440 display_details_set(filer_window
, details
);
442 /* Recreate layouts because wrapping may have changed */
443 view_style_changed(filer_window
->view
, VIEW_UPDATE_NAME
);
445 if (force_resize
|| o_filer_auto_resize
.int_value
== RESIZE_ALWAYS
446 || (o_filer_auto_resize
.int_value
== RESIZE_STYLE
&& style_changed
))
448 view_autosize(filer_window
->view
);
452 /* Set the 'Show Thumbnails' flag for this window */
453 void display_set_thumbs(FilerWindow
*filer_window
, gboolean thumbs
)
455 if (filer_window
->show_thumbs
== thumbs
)
458 filer_window
->show_thumbs
= thumbs
;
460 view_style_changed(filer_window
->view
, VIEW_UPDATE_VIEWDATA
);
463 filer_cancel_thumbnails(filer_window
);
465 filer_set_title(filer_window
);
467 filer_create_thumbs(filer_window
);
470 void display_update_hidden(FilerWindow
*filer_window
)
472 filer_detach_rescan(filer_window
); /* (updates titlebar) */
474 display_set_actual_size(filer_window
, FALSE
);
477 /* Set the 'Show Hidden' flag for this window */
478 void display_set_hidden(FilerWindow
*filer_window
, gboolean hidden
)
480 if (filer_window
->show_hidden
== hidden
)
484 filer_window->show_hidden = hidden;
486 filer_set_hidden(filer_window
, hidden
);
488 display_update_hidden(filer_window
);
491 /* Set the 'Filter Directories' flag for this window */
492 void display_set_filter_directories(FilerWindow
*filer_window
, gboolean filter_directories
)
494 if (filer_window
->filter_directories
== filter_directories
)
498 filer_window->show_hidden = hidden;
500 filer_set_filter_directories(filer_window
, filter_directories
);
502 display_update_hidden(filer_window
);
505 void display_set_filter(FilerWindow
*filer_window
, FilterType type
,
506 const gchar
*filter_string
)
508 if (filer_set_filter(filer_window
, type
, filter_string
))
509 display_update_hidden(filer_window
);
513 /* Highlight (wink or cursor) this item in the filer window. If the item
514 * isn't already there but we're scanning then highlight it if it
517 void display_set_autoselect(FilerWindow
*filer_window
, const gchar
*leaf
)
521 g_return_if_fail(filer_window
!= NULL
);
522 g_return_if_fail(leaf
!= NULL
);
524 new = g_strdup(leaf
); /* leaf == old value sometimes */
526 null_g_free(&filer_window
->auto_select
);
528 if (view_autoselect(filer_window
->view
, new))
531 filer_window
->auto_select
= new;
534 /* Change the icon size (wraps) */
535 void display_change_size(FilerWindow
*filer_window
, gboolean bigger
)
539 g_return_if_fail(filer_window
!= NULL
);
541 switch (filer_window
->display_style
)
544 new = bigger
? HUGE_ICONS
: SMALL_ICONS
;
558 display_set_layout(filer_window
, new, filer_window
->details_type
,
562 ViewData
*display_create_viewdata(FilerWindow
*filer_window
, DirItem
*item
)
566 view
= g_new(ViewData
, 1);
569 view
->details
= NULL
;
572 display_update_view(filer_window
, item
, view
, TRUE
);
577 /* Set the display style to the desired style. If the desired style
578 * is AUTO_SIZE_ICONS, choose an appropriate size. Also resizes filer
579 * window, if requested.
581 void display_set_actual_size(FilerWindow
*filer_window
, gboolean force_resize
)
583 display_set_layout(filer_window
, filer_window
->display_style_wanted
,
584 filer_window
->details_type
, force_resize
);
588 /****************************************************************
589 * INTERNAL FUNCTIONS *
590 ****************************************************************/
592 static void options_changed(void)
596 for (next
= all_filer_windows
; next
; next
= next
->next
)
598 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
601 if (o_display_dirs_first
.has_changed
||
602 o_display_caps_first
.has_changed
)
603 view_sort(VIEW(filer_window
->view
));
605 if (o_display_show_headers
.has_changed
)
606 flags
|= VIEW_UPDATE_HEADERS
;
608 if (o_large_width
.has_changed
|| o_small_width
.has_changed
)
609 flags
|= VIEW_UPDATE_NAME
; /* Recreate PangoLayout */
611 view_style_changed(filer_window
->view
, flags
);
615 /* Return a new string giving details of this item, or NULL if details
616 * are not being displayed. If details are not yet available, return
617 * a string of the right length.
619 static char *details(FilerWindow
*filer_window
, DirItem
*item
)
621 mode_t m
= item
->mode
;
623 gboolean scanned
= item
->base_type
!= TYPE_UNKNOWN
;
625 if (filer_window
->details_type
== DETAILS_NONE
)
628 if (scanned
&& item
->lstat_errno
)
629 buf
= g_strdup_printf(_("lstat(2) failed: %s"),
630 g_strerror(item
->lstat_errno
));
631 else if (filer_window
->details_type
== DETAILS_TYPE
)
633 MIME_type
*type
= item
->mime_type
;
636 return g_strdup("application/octet-stream");
638 buf
= g_strdup_printf("%s/%s",
639 type
->media_type
, type
->subtype
);
641 else if (filer_window
->details_type
== DETAILS_TIMES
)
643 guchar
*ctime
, *mtime
, *atime
;
645 ctime
= pretty_time(&item
->ctime
);
646 mtime
= pretty_time(&item
->mtime
);
647 atime
= pretty_time(&item
->atime
);
649 buf
= g_strdup_printf("a[%s] c[%s] m[%s]", atime
, ctime
, mtime
);
654 else if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
657 return g_strdup("---,---,---/--"
661 " 12345678 12345678");
663 buf
= g_strdup_printf("%s %-8.8s %-8.8s",
664 pretty_permissions(m
),
665 user_name(item
->uid
),
666 group_name(item
->gid
));
672 if (filer_window
->display_style
== SMALL_ICONS
)
673 return g_strdup("1234M");
675 return g_strdup("1234 bytes");
678 if (item
->base_type
!= TYPE_DIRECTORY
)
680 if (filer_window
->display_style
== SMALL_ICONS
)
681 buf
= g_strdup(format_size_aligned(item
->size
));
683 buf
= g_strdup(format_size(item
->size
));
692 /* Note: Call style_changed after this */
693 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
)
695 filer_window
->details_type
= details
;
698 /* Note: Call style_changed after this */
699 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
)
701 filer_window
->display_style_wanted
= style
;
702 display_set_actual_size_real(filer_window
);
705 /* Each displayed item has a ViewData structure with some cached information
706 * to help quickly draw the item (eg, the PangoLayout). This function updates
709 void display_update_view(FilerWindow
*filer_window
,
712 gboolean update_name_layout
)
714 DisplayStyle style
= filer_window
->display_style
;
718 static PangoFontDescription
*monospace
= NULL
;
719 PangoAttrList
*list
= NULL
;
722 monospace
= pango_font_description_from_string("monospace");
726 g_object_unref(G_OBJECT(view
->details
));
727 view
->details
= NULL
;
730 str
= details(filer_window
, item
);
733 PangoAttrList
*details_list
;
734 int perm_offset
= -1;
736 view
->details
= gtk_widget_create_pango_layout(
737 filer_window
->window
, str
);
740 pango_layout_set_font_description(view
->details
, monospace
);
741 pango_layout_get_size(view
->details
, &w
, &h
);
742 view
->details_width
= w
/ PANGO_SCALE
;
743 view
->details_height
= h
/ PANGO_SCALE
;
745 if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
747 if (perm_offset
> -1)
749 PangoAttribute
*attr
;
751 attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
753 perm_offset
+= 4 * applicable(item
->uid
, item
->gid
);
754 attr
->start_index
= perm_offset
;
755 attr
->end_index
= perm_offset
+ 3;
757 details_list
= pango_attr_list_new();
758 pango_attr_list_insert(details_list
, attr
);
759 pango_layout_set_attributes(view
->details
,
766 g_object_unref(view
->image
);
770 if (filer_window
->show_thumbs
&& item
->base_type
== TYPE_FILE
/*&&
771 strcmp(item->mime_type->media_type, "image") == 0*/)
775 path
= make_path(filer_window
->real_path
, item
->leafname
);
777 view
->image
= g_fscache_lookup_full(pixmap_cache
, path
,
778 FSCACHE_LOOKUP_ONLY_NEW
, NULL
);
783 view
->image
= di_image(item
);
785 g_object_ref(view
->image
);
788 if (view
->layout
&& update_name_layout
)
790 g_object_unref(G_OBJECT(view
->layout
));
798 else if (g_utf8_validate(item
->leafname
, -1, NULL
))
800 view
->layout
= gtk_widget_create_pango_layout(
801 filer_window
->window
, item
->leafname
);
802 pango_layout_set_auto_dir(view
->layout
, FALSE
);
806 PangoAttribute
*attr
;
809 utf8
= to_utf8(item
->leafname
);
810 view
->layout
= gtk_widget_create_pango_layout(
811 filer_window
->window
, utf8
);
814 attr
= pango_attr_foreground_new(0xffff, 0, 0);
815 attr
->start_index
= 0;
816 attr
->end_index
= -1;
818 list
= pango_attr_list_new();
819 pango_attr_list_insert(list
, attr
);
822 if (item
->flags
& ITEM_FLAG_RECENT
)
824 PangoAttribute
*attr
;
826 attr
= pango_attr_weight_new(PANGO_WEIGHT_BOLD
);
827 attr
->start_index
= 0;
828 attr
->end_index
= -1;
830 list
= pango_attr_list_new();
831 pango_attr_list_insert(list
, attr
);
835 pango_layout_set_attributes(view
->layout
, list
);
837 if (filer_window
->details_type
== DETAILS_NONE
)
839 if (style
== HUGE_ICONS
)
840 wrap_width
= HUGE_WRAP
* PANGO_SCALE
;
841 else if (style
== LARGE_ICONS
)
842 wrap_width
= o_large_width
.int_value
* PANGO_SCALE
;
845 #ifdef USE_PANGO_WRAP_WORD_CHAR
846 pango_layout_set_wrap(view
->layout
, PANGO_WRAP_WORD_CHAR
);
848 if (wrap_width
!= -1)
849 pango_layout_set_width(view
->layout
, wrap_width
);
851 pango_layout_get_size(view
->layout
, &w
, &h
);
852 view
->name_width
= w
/ PANGO_SCALE
;
853 view
->name_height
= h
/ PANGO_SCALE
;
856 /* Sets display_style from display_style_wanted.
857 * See also display_set_actual_size().
859 static void display_set_actual_size_real(FilerWindow
*filer_window
)
861 DisplayStyle size
= filer_window
->display_style_wanted
;
864 g_return_if_fail(filer_window
!= NULL
);
866 if (size
== AUTO_SIZE_ICONS
)
868 n
= view_count_items(filer_window
->view
);
870 if (n
>= o_filer_change_size_num
.int_value
)
876 filer_window
->display_style
= size
;