4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
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)
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
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 /* display.c - code for arranging and displaying file items */
31 #include <sys/param.h>
38 #include <gdk/gdkkeysyms.h>
46 #include "gui_support.h"
55 #include "minibuffer.h"
59 #include "view_iface.h"
61 #define HUGE_WRAP (1.5 * o_large_width.int_value)
64 static Option o_display_caps_first
;
65 static Option o_display_dirs_first
;
66 Option o_display_size
;
67 Option o_display_details
;
68 Option o_display_sort_by
;
69 static Option o_large_width
;
71 Option o_display_show_hidden
;
72 Option o_display_show_thumbs
;
73 Option o_display_show_headers
;
74 Option o_display_show_full_type
;
75 Option o_display_inherit_options
;
76 static Option o_filer_change_size_num
;
77 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
);
109 option_add_notify(options_changed
);
112 static void draw_emblem_on_icon(GdkWindow
*window
, MaskedPixmap
*image
,
115 gdk_pixbuf_render_to_drawable_alpha(image
->pixbuf
,
120 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
121 GDK_RGB_DITHER_NORMAL
, 0, 0);
126 /* Draw this icon (including any symlink or mount symbol) inside the
129 void draw_huge_icon(GdkWindow
*window
, GdkRectangle
*area
,
130 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
,
141 width
= image
->huge_width
;
142 height
= image
->huge_height
;
143 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
144 image_y
= MAX(0, area
->height
- height
- 6);
147 ? create_spotlight_pixbuf(image
->huge_pixbuf
, color
)
148 : image
->huge_pixbuf
;
150 gdk_pixbuf_render_to_drawable_alpha(
154 image_x
, area
->y
+ image_y
, /* dest */
156 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
157 GDK_RGB_DITHER_NORMAL
, 0, 0);
160 g_object_unref(pixbuf
);
162 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
164 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
167 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
169 if (item
->flags
& ITEM_FLAG_SYMLINK
)
171 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
173 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
175 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
179 /* Draw this icon (including any symlink or mount symbol) inside the
182 void draw_large_icon(GdkWindow
*window
,
198 width
= MIN(image
->width
, ICON_WIDTH
);
199 height
= MIN(image
->height
, ICON_HEIGHT
);
200 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
201 image_y
= MAX(0, area
->height
- height
- 6);
204 ? create_spotlight_pixbuf(image
->pixbuf
, color
)
207 gdk_pixbuf_render_to_drawable_alpha(
211 image_x
, area
->y
+ image_y
, /* dest */
213 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
214 GDK_RGB_DITHER_NORMAL
, 0, 0);
217 g_object_unref(pixbuf
);
219 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
221 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
224 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
226 if (item
->flags
& ITEM_FLAG_SYMLINK
)
228 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
230 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
232 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
236 void draw_small_icon(GdkWindow
*window
, GdkRectangle
*area
,
237 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
,
240 int width
, height
, image_x
, image_y
;
246 if (!image
->sm_pixbuf
)
247 pixmap_make_small(image
);
249 width
= MIN(image
->sm_width
, SMALL_WIDTH
);
250 height
= MIN(image
->sm_height
, SMALL_HEIGHT
);
251 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
252 image_y
= MAX(0, SMALL_HEIGHT
- image
->sm_height
);
255 ? create_spotlight_pixbuf(image
->sm_pixbuf
, color
)
258 gdk_pixbuf_render_to_drawable_alpha(
262 image_x
, area
->y
+ image_y
, /* dest */
264 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
265 GDK_RGB_DITHER_NORMAL
, 0, 0);
268 g_object_unref(pixbuf
);
270 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
272 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
275 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
277 if (item
->flags
& ITEM_FLAG_SYMLINK
)
279 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 8);
281 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
283 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 8);
287 /* The sort functions aren't called from outside, but they are
288 * passed as arguments to display_set_sort_fn().
291 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
292 !(item->flags & ITEM_FLAG_APPDIR))
295 if (o_display_dirs_first.int_value) { \
296 gboolean id1 = IS_A_DIR(i1); \
297 gboolean id2 = IS_A_DIR(i2); \
298 if (id1 && !id2) return -1; \
299 if (id2 && !id1) return 1; \
302 int sort_by_name(const void *item1
, const void *item2
)
304 const DirItem
*i1
= (DirItem
*) item1
;
305 const DirItem
*i2
= (DirItem
*) item2
;
306 CollateKey
*n1
= i1
->leafname_collate
;
307 CollateKey
*n2
= i2
->leafname_collate
;
312 retval
= collate_key_cmp(n1
, n2
, o_display_caps_first
.int_value
);
314 return retval
? retval
: strcmp(i1
->leafname
, i2
->leafname
);
317 int sort_by_type(const void *item1
, const void *item2
)
319 const DirItem
*i1
= (DirItem
*) item1
;
320 const DirItem
*i2
= (DirItem
*) item2
;
323 int diff
= i1
->base_type
- i2
->base_type
;
326 diff
= (i1
->flags
& ITEM_FLAG_APPDIR
)
327 - (i2
->flags
& ITEM_FLAG_APPDIR
);
329 return diff
> 0 ? 1 : -1;
336 diff
= strcmp(m1
->media_type
, m2
->media_type
);
338 diff
= strcmp(m1
->subtype
, m2
->subtype
);
346 return diff
> 0 ? 1 : -1;
348 return sort_by_name(item1
, item2
);
351 int sort_by_owner(const void *item1
, const void *item2
)
353 const DirItem
*i1
= (DirItem
*) item1
;
354 const DirItem
*i2
= (DirItem
*) item2
;
359 return sort_by_name(item1
, item2
);
361 name1
=user_name(i1
->uid
);
362 name2
=user_name(i2
->uid
);
364 return strcmp(name1
, name2
);
367 int sort_by_group(const void *item1
, const void *item2
)
369 const DirItem
*i1
= (DirItem
*) item1
;
370 const DirItem
*i2
= (DirItem
*) item2
;
375 return sort_by_name(item1
, item2
);
377 name1
=group_name(i1
->gid
);
378 name2
=group_name(i2
->gid
);
380 return strcmp(name1
, name2
);
383 int sort_by_date(const void *item1
, const void *item2
)
385 const DirItem
*i1
= (DirItem
*) item1
;
386 const DirItem
*i2
= (DirItem
*) item2
;
388 /* SORT_DIRS; -- too confusing! */
390 return i1
->mtime
< i2
->mtime
? -1 :
391 i1
->mtime
> i2
->mtime
? 1 :
392 sort_by_name(item1
, item2
);
395 int sort_by_size(const void *item1
, const void *item2
)
397 const DirItem
*i1
= (DirItem
*) item1
;
398 const DirItem
*i2
= (DirItem
*) item2
;
402 return i1
->size
< i2
->size
? -1 :
403 i1
->size
> i2
->size
? 1 :
404 sort_by_name(item1
, item2
);
407 void display_set_sort_type(FilerWindow
*filer_window
, SortType sort_type
,
410 if (filer_window
->sort_type
== sort_type
&&
411 filer_window
->sort_order
== order
)
414 filer_window
->sort_type
= sort_type
;
415 filer_window
->sort_order
= order
;
417 view_sort(filer_window
->view
);
420 /* Change the icon size and style.
421 * force_resize should only be TRUE for new windows.
423 void display_set_layout(FilerWindow
*filer_window
,
426 gboolean force_resize
)
428 gboolean style_changed
= FALSE
;
430 g_return_if_fail(filer_window
!= NULL
);
432 if (filer_window
->display_style_wanted
!= style
433 || filer_window
->details_type
!= details
)
435 style_changed
= TRUE
;
438 display_style_set(filer_window
, style
);
439 display_details_set(filer_window
, details
);
441 /* Recreate layouts because wrapping may have changed */
442 view_style_changed(filer_window
->view
, VIEW_UPDATE_NAME
);
444 if (force_resize
|| o_filer_auto_resize
.int_value
== RESIZE_ALWAYS
445 || (o_filer_auto_resize
.int_value
== RESIZE_STYLE
&& style_changed
))
447 view_autosize(filer_window
->view
);
451 /* Set the 'Show Thumbnails' flag for this window */
452 void display_set_thumbs(FilerWindow
*filer_window
, gboolean thumbs
)
454 if (filer_window
->show_thumbs
== thumbs
)
457 filer_window
->show_thumbs
= thumbs
;
459 view_style_changed(filer_window
->view
, VIEW_UPDATE_VIEWDATA
);
462 filer_cancel_thumbnails(filer_window
);
464 filer_set_title(filer_window
);
466 filer_create_thumbs(filer_window
);
469 void display_update_hidden(FilerWindow
*filer_window
)
471 filer_detach_rescan(filer_window
); /* (updates titlebar) */
473 display_set_actual_size(filer_window
, FALSE
);
476 /* Set the 'Show Hidden' flag for this window */
477 void display_set_hidden(FilerWindow
*filer_window
, gboolean hidden
)
479 if (filer_window
->show_hidden
== hidden
)
483 filer_window->show_hidden = hidden;
485 filer_set_hidden(filer_window
, hidden
);
487 display_update_hidden(filer_window
);
490 void display_set_filter(FilerWindow
*filer_window
, FilterType type
,
491 const gchar
*filter_string
)
493 if (filer_set_filter(filer_window
, type
, filter_string
))
494 display_update_hidden(filer_window
);
498 /* Highlight (wink or cursor) this item in the filer window. If the item
499 * isn't already there but we're scanning then highlight it if it
502 void display_set_autoselect(FilerWindow
*filer_window
, const gchar
*leaf
)
506 g_return_if_fail(filer_window
!= NULL
);
507 g_return_if_fail(leaf
!= NULL
);
509 new = g_strdup(leaf
); /* leaf == old value sometimes */
511 null_g_free(&filer_window
->auto_select
);
513 if (view_autoselect(filer_window
->view
, new))
516 filer_window
->auto_select
= new;
519 /* Change the icon size (wraps) */
520 void display_change_size(FilerWindow
*filer_window
, gboolean bigger
)
524 g_return_if_fail(filer_window
!= NULL
);
526 switch (filer_window
->display_style
)
529 new = bigger
? HUGE_ICONS
: SMALL_ICONS
;
543 display_set_layout(filer_window
, new, filer_window
->details_type
,
547 ViewData
*display_create_viewdata(FilerWindow
*filer_window
, DirItem
*item
)
551 view
= g_new(ViewData
, 1);
554 view
->details
= NULL
;
557 display_update_view(filer_window
, item
, view
, TRUE
);
562 /* Set the display style to the desired style. If the desired style
563 * is AUTO_SIZE_ICONS, choose an appropriate size. Also resizes filer
564 * window, if requested.
566 void display_set_actual_size(FilerWindow
*filer_window
, gboolean force_resize
)
568 display_set_layout(filer_window
, filer_window
->display_style_wanted
,
569 filer_window
->details_type
, force_resize
);
573 /****************************************************************
574 * INTERNAL FUNCTIONS *
575 ****************************************************************/
577 static void options_changed(void)
581 for (next
= all_filer_windows
; next
; next
= next
->next
)
583 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
586 if (o_display_dirs_first
.has_changed
||
587 o_display_caps_first
.has_changed
)
588 view_sort(VIEW(filer_window
->view
));
590 if (o_display_show_headers
.has_changed
)
591 flags
|= VIEW_UPDATE_HEADERS
;
593 if (o_large_width
.has_changed
|| o_small_width
.has_changed
)
594 flags
|= VIEW_UPDATE_NAME
; /* Recreate PangoLayout */
596 view_style_changed(filer_window
->view
, flags
);
600 /* Return a new string giving details of this item, or NULL if details
601 * are not being displayed. If details are not yet available, return
602 * a string of the right length.
604 static char *details(FilerWindow
*filer_window
, DirItem
*item
)
606 mode_t m
= item
->mode
;
608 gboolean scanned
= item
->base_type
!= TYPE_UNKNOWN
;
610 if (filer_window
->details_type
== DETAILS_NONE
)
613 if (scanned
&& item
->lstat_errno
)
614 buf
= g_strdup_printf(_("lstat(2) failed: %s"),
615 g_strerror(item
->lstat_errno
));
616 else if (filer_window
->details_type
== DETAILS_TYPE
)
618 MIME_type
*type
= item
->mime_type
;
621 return g_strdup("application/octet-stream");
623 buf
= g_strdup_printf("%s/%s",
624 type
->media_type
, type
->subtype
);
626 else if (filer_window
->details_type
== DETAILS_TIMES
)
628 guchar
*ctime
, *mtime
, *atime
;
630 ctime
= pretty_time(&item
->ctime
);
631 mtime
= pretty_time(&item
->mtime
);
632 atime
= pretty_time(&item
->atime
);
634 buf
= g_strdup_printf("a[%s] c[%s] m[%s]", atime
, ctime
, mtime
);
639 else if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
642 return g_strdup("---,---,---/--"
646 " 12345678 12345678");
648 buf
= g_strdup_printf("%s %-8.8s %-8.8s",
649 pretty_permissions(m
),
650 user_name(item
->uid
),
651 group_name(item
->gid
));
657 if (filer_window
->display_style
== SMALL_ICONS
)
658 return g_strdup("1234M");
660 return g_strdup("1234 bytes");
663 if (item
->base_type
!= TYPE_DIRECTORY
)
665 if (filer_window
->display_style
== SMALL_ICONS
)
666 buf
= g_strdup(format_size_aligned(item
->size
));
668 buf
= g_strdup(format_size(item
->size
));
677 /* Note: Call style_changed after this */
678 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
)
680 filer_window
->details_type
= details
;
683 /* Note: Call style_changed after this */
684 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
)
686 filer_window
->display_style_wanted
= style
;
687 display_set_actual_size_real(filer_window
);
690 /* Each displayed item has a ViewData structure with some cached information
691 * to help quickly draw the item (eg, the PangoLayout). This function updates
694 void display_update_view(FilerWindow
*filer_window
,
697 gboolean update_name_layout
)
699 DisplayStyle style
= filer_window
->display_style
;
703 static PangoFontDescription
*monospace
= NULL
;
704 PangoAttrList
*list
= NULL
;
707 monospace
= pango_font_description_from_string("monospace");
711 g_object_unref(G_OBJECT(view
->details
));
712 view
->details
= NULL
;
715 str
= details(filer_window
, item
);
718 PangoAttrList
*details_list
;
719 int perm_offset
= -1;
721 view
->details
= gtk_widget_create_pango_layout(
722 filer_window
->window
, str
);
725 pango_layout_set_font_description(view
->details
, monospace
);
726 pango_layout_get_size(view
->details
, &w
, &h
);
727 view
->details_width
= w
/ PANGO_SCALE
;
728 view
->details_height
= h
/ PANGO_SCALE
;
730 if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
732 if (perm_offset
> -1)
734 PangoAttribute
*attr
;
736 attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
738 perm_offset
+= 4 * applicable(item
->uid
, item
->gid
);
739 attr
->start_index
= perm_offset
;
740 attr
->end_index
= perm_offset
+ 3;
742 details_list
= pango_attr_list_new();
743 pango_attr_list_insert(details_list
, attr
);
744 pango_layout_set_attributes(view
->details
,
751 g_object_unref(view
->image
);
755 if (filer_window
->show_thumbs
&& item
->base_type
== TYPE_FILE
/*&&
756 strcmp(item->mime_type->media_type, "image") == 0*/)
760 path
= make_path(filer_window
->real_path
, item
->leafname
);
762 view
->image
= g_fscache_lookup_full(pixmap_cache
, path
,
763 FSCACHE_LOOKUP_ONLY_NEW
, NULL
);
768 view
->image
= di_image(item
);
770 g_object_ref(view
->image
);
773 if (view
->layout
&& update_name_layout
)
775 g_object_unref(G_OBJECT(view
->layout
));
783 else if (g_utf8_validate(item
->leafname
, -1, NULL
))
785 view
->layout
= gtk_widget_create_pango_layout(
786 filer_window
->window
, item
->leafname
);
790 PangoAttribute
*attr
;
793 utf8
= to_utf8(item
->leafname
);
794 view
->layout
= gtk_widget_create_pango_layout(
795 filer_window
->window
, utf8
);
798 attr
= pango_attr_foreground_new(0xffff, 0, 0);
799 attr
->start_index
= 0;
800 attr
->end_index
= -1;
802 list
= pango_attr_list_new();
803 pango_attr_list_insert(list
, attr
);
806 if (item
->flags
& ITEM_FLAG_RECENT
)
808 PangoAttribute
*attr
;
810 attr
= pango_attr_weight_new(PANGO_WEIGHT_BOLD
);
811 attr
->start_index
= 0;
812 attr
->end_index
= -1;
814 list
= pango_attr_list_new();
815 pango_attr_list_insert(list
, attr
);
819 pango_layout_set_attributes(view
->layout
, list
);
821 if (filer_window
->details_type
== DETAILS_NONE
)
823 if (style
== HUGE_ICONS
)
824 wrap_width
= HUGE_WRAP
* PANGO_SCALE
;
825 else if (style
== LARGE_ICONS
)
826 wrap_width
= o_large_width
.int_value
* PANGO_SCALE
;
829 #ifdef USE_PANGO_WRAP_WORD_CHAR
830 pango_layout_set_wrap(view
->layout
, PANGO_WRAP_WORD_CHAR
);
832 if (wrap_width
!= -1)
833 pango_layout_set_width(view
->layout
, wrap_width
);
835 pango_layout_get_size(view
->layout
, &w
, &h
);
836 view
->name_width
= w
/ PANGO_SCALE
;
837 view
->name_height
= h
/ PANGO_SCALE
;
840 /* Sets display_style from display_style_wanted.
841 * See also display_set_actual_size().
843 static void display_set_actual_size_real(FilerWindow
*filer_window
)
845 DisplayStyle size
= filer_window
->display_style_wanted
;
848 g_return_if_fail(filer_window
!= NULL
);
850 if (size
== AUTO_SIZE_ICONS
)
852 n
= view_count_items(filer_window
->view
);
854 if (n
>= o_filer_change_size_num
.int_value
)
860 filer_window
->display_style
= size
;