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_inherit_options
;
75 static Option o_filer_change_size_num
;
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_inherit_options
,
101 "display_inherit_options", FALSE
);
102 option_add_int(&o_filer_change_size_num
, "filer_change_size_num", 30);
104 option_add_notify(options_changed
);
107 static void draw_emblem_on_icon(GdkWindow
*window
, MaskedPixmap
*image
,
110 gdk_pixbuf_render_to_drawable_alpha(image
->pixbuf
,
115 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
116 GDK_RGB_DITHER_NORMAL
, 0, 0);
121 /* Draw this icon (including any symlink or mount symbol) inside the
124 void draw_huge_icon(GdkWindow
*window
, GdkRectangle
*area
,
125 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
)
134 width
= image
->huge_width
;
135 height
= image
->huge_height
;
136 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
137 image_y
= MAX(0, area
->height
- height
- 6);
139 gdk_pixbuf_render_to_drawable_alpha(
140 selected
? image
->huge_pixbuf_lit
141 : image
->huge_pixbuf
,
144 image_x
, area
->y
+ image_y
, /* dest */
146 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
147 GDK_RGB_DITHER_NORMAL
, 0, 0);
149 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
151 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
154 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
156 if (item
->flags
& ITEM_FLAG_SYMLINK
)
158 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
160 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
162 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
166 /* Draw this icon (including any symlink or mount symbol) inside the
169 void draw_large_icon(GdkWindow
*window
,
183 width
= MIN(image
->width
, ICON_WIDTH
);
184 height
= MIN(image
->height
, ICON_HEIGHT
);
185 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
186 image_y
= MAX(0, area
->height
- height
- 6);
188 gdk_pixbuf_render_to_drawable_alpha(
189 selected
? image
->pixbuf_lit
: image
->pixbuf
,
192 image_x
, area
->y
+ image_y
, /* dest */
194 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
195 GDK_RGB_DITHER_NORMAL
, 0, 0);
197 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
199 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
202 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
204 if (item
->flags
& ITEM_FLAG_SYMLINK
)
206 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
208 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
210 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
214 void draw_small_icon(GdkWindow
*window
, GdkRectangle
*area
,
215 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
)
217 int width
, height
, image_x
, image_y
;
222 if (!image
->sm_pixbuf
)
223 pixmap_make_small(image
);
225 width
= MIN(image
->sm_width
, SMALL_WIDTH
);
226 height
= MIN(image
->sm_height
, SMALL_HEIGHT
);
227 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
228 image_y
= MAX(0, SMALL_HEIGHT
- image
->sm_height
);
230 gdk_pixbuf_render_to_drawable_alpha(
231 selected
? image
->sm_pixbuf_lit
: image
->sm_pixbuf
,
234 image_x
, area
->y
+ image_y
, /* dest */
236 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
237 GDK_RGB_DITHER_NORMAL
, 0, 0);
239 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
241 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
244 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
246 if (item
->flags
& ITEM_FLAG_SYMLINK
)
248 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 8);
250 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
252 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 8);
256 /* The sort functions aren't called from outside, but they are
257 * passed as arguments to display_set_sort_fn().
260 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
261 !(item->flags & ITEM_FLAG_APPDIR))
264 if (o_display_dirs_first.int_value) { \
265 gboolean id1 = IS_A_DIR(i1); \
266 gboolean id2 = IS_A_DIR(i2); \
267 if (id1 && !id2) return -1; \
268 if (id2 && !id1) return 1; \
271 int sort_by_name(const void *item1
, const void *item2
)
273 const DirItem
*i1
= (DirItem
*) item1
;
274 const DirItem
*i2
= (DirItem
*) item2
;
275 CollateKey
*n1
= i1
->leafname_collate
;
276 CollateKey
*n2
= i2
->leafname_collate
;
281 retval
= collate_key_cmp(n1
, n2
, o_display_caps_first
.int_value
);
283 return retval
? retval
: strcmp(i1
->leafname
, i2
->leafname
);
286 int sort_by_type(const void *item1
, const void *item2
)
288 const DirItem
*i1
= (DirItem
*) item1
;
289 const DirItem
*i2
= (DirItem
*) item2
;
292 int diff
= i1
->base_type
- i2
->base_type
;
295 diff
= (i1
->flags
& ITEM_FLAG_APPDIR
)
296 - (i2
->flags
& ITEM_FLAG_APPDIR
);
298 return diff
> 0 ? 1 : -1;
305 diff
= strcmp(m1
->media_type
, m2
->media_type
);
307 diff
= strcmp(m1
->subtype
, m2
->subtype
);
315 return diff
> 0 ? 1 : -1;
317 return sort_by_name(item1
, item2
);
320 int sort_by_owner(const void *item1
, const void *item2
)
322 const DirItem
*i1
= (DirItem
*) item1
;
323 const DirItem
*i2
= (DirItem
*) item2
;
328 return sort_by_name(item1
, item2
);
330 name1
=user_name(i1
->uid
);
331 name2
=user_name(i2
->uid
);
333 return strcmp(name1
, name2
);
336 int sort_by_group(const void *item1
, const void *item2
)
338 const DirItem
*i1
= (DirItem
*) item1
;
339 const DirItem
*i2
= (DirItem
*) item2
;
344 return sort_by_name(item1
, item2
);
346 name1
=group_name(i1
->gid
);
347 name2
=group_name(i2
->gid
);
349 return strcmp(name1
, name2
);
352 int sort_by_date(const void *item1
, const void *item2
)
354 const DirItem
*i1
= (DirItem
*) item1
;
355 const DirItem
*i2
= (DirItem
*) item2
;
357 /* SORT_DIRS; -- too confusing! */
359 return i1
->mtime
< i2
->mtime
? -1 :
360 i1
->mtime
> i2
->mtime
? 1 :
361 sort_by_name(item1
, item2
);
364 int sort_by_size(const void *item1
, const void *item2
)
366 const DirItem
*i1
= (DirItem
*) item1
;
367 const DirItem
*i2
= (DirItem
*) item2
;
371 return i1
->size
< i2
->size
? -1 :
372 i1
->size
> i2
->size
? 1 :
373 sort_by_name(item1
, item2
);
376 void display_set_sort_type(FilerWindow
*filer_window
, SortType sort_type
,
379 if (filer_window
->sort_type
== sort_type
&&
380 filer_window
->sort_order
== order
)
383 filer_window
->sort_type
= sort_type
;
384 filer_window
->sort_order
= order
;
386 view_sort(filer_window
->view
);
389 /* Change the icon size and style.
390 * force_resize should only be TRUE for new windows.
392 void display_set_layout(FilerWindow
*filer_window
,
395 gboolean force_resize
)
397 gboolean style_changed
= FALSE
;
399 g_return_if_fail(filer_window
!= NULL
);
401 if (filer_window
->display_style_wanted
!= style
402 || filer_window
->details_type
!= details
)
404 style_changed
= TRUE
;
407 display_style_set(filer_window
, style
);
408 display_details_set(filer_window
, details
);
410 /* Recreate layouts because wrapping may have changed */
411 view_style_changed(filer_window
->view
, VIEW_UPDATE_NAME
);
413 if (force_resize
|| o_filer_auto_resize
.int_value
== RESIZE_ALWAYS
414 || (o_filer_auto_resize
.int_value
== RESIZE_STYLE
&& style_changed
))
416 view_autosize(filer_window
->view
);
420 /* Set the 'Show Thumbnails' flag for this window */
421 void display_set_thumbs(FilerWindow
*filer_window
, gboolean thumbs
)
423 if (filer_window
->show_thumbs
== thumbs
)
426 filer_window
->show_thumbs
= thumbs
;
428 view_style_changed(filer_window
->view
, VIEW_UPDATE_VIEWDATA
);
431 filer_cancel_thumbnails(filer_window
);
433 filer_set_title(filer_window
);
435 filer_create_thumbs(filer_window
);
438 void display_update_hidden(FilerWindow
*filer_window
)
440 filer_detach_rescan(filer_window
); /* (updates titlebar) */
442 display_set_actual_size(filer_window
, FALSE
);
445 /* Set the 'Show Hidden' flag for this window */
446 void display_set_hidden(FilerWindow
*filer_window
, gboolean hidden
)
448 if (filer_window
->show_hidden
== hidden
)
452 filer_window->show_hidden = hidden;
454 filer_set_hidden(filer_window
, hidden
);
456 display_update_hidden(filer_window
);
459 void display_set_filter(FilerWindow
*filer_window
, FilterType type
,
460 const gchar
*filter_string
)
462 if (filer_set_filter(filer_window
, type
, filter_string
))
463 display_update_hidden(filer_window
);
467 /* Highlight (wink or cursor) this item in the filer window. If the item
468 * isn't already there but we're scanning then highlight it if it
471 void display_set_autoselect(FilerWindow
*filer_window
, const gchar
*leaf
)
475 g_return_if_fail(filer_window
!= NULL
);
476 g_return_if_fail(leaf
!= NULL
);
478 new = g_strdup(leaf
); /* leaf == old value sometimes */
480 null_g_free(&filer_window
->auto_select
);
482 if (view_autoselect(filer_window
->view
, new))
485 filer_window
->auto_select
= new;
488 /* Change the icon size (wraps) */
489 void display_change_size(FilerWindow
*filer_window
, gboolean bigger
)
493 g_return_if_fail(filer_window
!= NULL
);
495 switch (filer_window
->display_style
)
498 new = bigger
? HUGE_ICONS
: SMALL_ICONS
;
512 display_set_layout(filer_window
, new, filer_window
->details_type
,
516 ViewData
*display_create_viewdata(FilerWindow
*filer_window
, DirItem
*item
)
520 view
= g_new(ViewData
, 1);
523 view
->details
= NULL
;
526 display_update_view(filer_window
, item
, view
, TRUE
);
531 /* Set the display style to the desired style. If the desired style
532 * is AUTO_SIZE_ICONS, choose an appropriate size. Also resizes filer
533 * window, if requested.
535 void display_set_actual_size(FilerWindow
*filer_window
, gboolean force_resize
)
537 display_set_layout(filer_window
, filer_window
->display_style_wanted
,
538 filer_window
->details_type
, force_resize
);
542 /****************************************************************
543 * INTERNAL FUNCTIONS *
544 ****************************************************************/
546 static void options_changed(void)
550 for (next
= all_filer_windows
; next
; next
= next
->next
)
552 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
555 if (o_display_dirs_first
.has_changed
||
556 o_display_caps_first
.has_changed
)
557 view_sort(VIEW(filer_window
->view
));
559 if (o_display_show_headers
.has_changed
)
560 flags
|= VIEW_UPDATE_HEADERS
;
562 if (o_large_width
.has_changed
|| o_small_width
.has_changed
)
563 flags
|= VIEW_UPDATE_NAME
; /* Recreate PangoLayout */
565 view_style_changed(filer_window
->view
, flags
);
569 /* Return a new string giving details of this item, or NULL if details
570 * are not being displayed. If details are not yet available, return
571 * a string of the right length.
573 static char *details(FilerWindow
*filer_window
, DirItem
*item
)
575 mode_t m
= item
->mode
;
577 gboolean scanned
= item
->base_type
!= TYPE_UNKNOWN
;
579 if (filer_window
->details_type
== DETAILS_NONE
)
582 if (scanned
&& item
->lstat_errno
)
583 buf
= g_strdup_printf(_("lstat(2) failed: %s"),
584 g_strerror(item
->lstat_errno
));
585 else if (filer_window
->details_type
== DETAILS_TYPE
)
587 MIME_type
*type
= item
->mime_type
;
590 return g_strdup("application/octet-stream");
592 buf
= g_strdup_printf("%s/%s",
593 type
->media_type
, type
->subtype
);
595 else if (filer_window
->details_type
== DETAILS_TIMES
)
597 guchar
*ctime
, *mtime
, *atime
;
599 ctime
= pretty_time(&item
->ctime
);
600 mtime
= pretty_time(&item
->mtime
);
601 atime
= pretty_time(&item
->atime
);
603 buf
= g_strdup_printf("a[%s] c[%s] m[%s]", atime
, ctime
, mtime
);
608 else if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
611 return g_strdup("---,---,---/--"
615 " 12345678 12345678");
617 buf
= g_strdup_printf("%s %-8.8s %-8.8s",
618 pretty_permissions(m
),
619 user_name(item
->uid
),
620 group_name(item
->gid
));
626 if (filer_window
->display_style
== SMALL_ICONS
)
627 return g_strdup("1234M");
629 return g_strdup("1234 bytes");
632 if (item
->base_type
!= TYPE_DIRECTORY
)
634 if (filer_window
->display_style
== SMALL_ICONS
)
635 buf
= g_strdup(format_size_aligned(item
->size
));
637 buf
= g_strdup(format_size(item
->size
));
646 /* Note: Call style_changed after this */
647 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
)
649 filer_window
->details_type
= details
;
652 /* Note: Call style_changed after this */
653 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
)
655 filer_window
->display_style_wanted
= style
;
656 display_set_actual_size_real(filer_window
);
659 /* Each displayed item has a ViewData structure with some cached information
660 * to help quickly draw the item (eg, the PangoLayout). This function updates
663 void display_update_view(FilerWindow
*filer_window
,
666 gboolean update_name_layout
)
668 DisplayStyle style
= filer_window
->display_style
;
672 static PangoFontDescription
*monospace
= NULL
;
673 PangoAttrList
*list
= NULL
;
676 monospace
= pango_font_description_from_string("monospace");
680 g_object_unref(G_OBJECT(view
->details
));
681 view
->details
= NULL
;
684 str
= details(filer_window
, item
);
687 PangoAttrList
*details_list
;
688 int perm_offset
= -1;
690 view
->details
= gtk_widget_create_pango_layout(
691 filer_window
->window
, str
);
694 pango_layout_set_font_description(view
->details
, monospace
);
695 pango_layout_get_size(view
->details
, &w
, &h
);
696 view
->details_width
= w
/ PANGO_SCALE
;
697 view
->details_height
= h
/ PANGO_SCALE
;
699 if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
701 if (perm_offset
> -1)
703 PangoAttribute
*attr
;
705 attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
707 perm_offset
+= 4 * applicable(item
->uid
, item
->gid
);
708 attr
->start_index
= perm_offset
;
709 attr
->end_index
= perm_offset
+ 3;
711 details_list
= pango_attr_list_new();
712 pango_attr_list_insert(details_list
, attr
);
713 pango_layout_set_attributes(view
->details
,
720 g_object_unref(view
->image
);
724 if (filer_window
->show_thumbs
&& item
->base_type
== TYPE_FILE
/*&&
725 strcmp(item->mime_type->media_type, "image") == 0*/)
729 path
= make_path(filer_window
->real_path
, item
->leafname
);
731 view
->image
= g_fscache_lookup_full(pixmap_cache
, path
,
732 FSCACHE_LOOKUP_ONLY_NEW
, NULL
);
737 view
->image
= di_image(item
);
739 g_object_ref(view
->image
);
742 if (view
->layout
&& update_name_layout
)
744 g_object_unref(G_OBJECT(view
->layout
));
752 else if (g_utf8_validate(item
->leafname
, -1, NULL
))
754 view
->layout
= gtk_widget_create_pango_layout(
755 filer_window
->window
, item
->leafname
);
759 PangoAttribute
*attr
;
762 utf8
= to_utf8(item
->leafname
);
763 view
->layout
= gtk_widget_create_pango_layout(
764 filer_window
->window
, utf8
);
767 attr
= pango_attr_foreground_new(0xffff, 0, 0);
768 attr
->start_index
= 0;
769 attr
->end_index
= -1;
771 list
= pango_attr_list_new();
772 pango_attr_list_insert(list
, attr
);
775 if (item
->flags
& ITEM_FLAG_RECENT
)
777 PangoAttribute
*attr
;
779 attr
= pango_attr_weight_new(PANGO_WEIGHT_BOLD
);
780 attr
->start_index
= 0;
781 attr
->end_index
= -1;
783 list
= pango_attr_list_new();
784 pango_attr_list_insert(list
, attr
);
788 pango_layout_set_attributes(view
->layout
, list
);
790 if (filer_window
->details_type
== DETAILS_NONE
)
792 if (style
== HUGE_ICONS
)
793 wrap_width
= HUGE_WRAP
* PANGO_SCALE
;
794 else if (style
== LARGE_ICONS
)
795 wrap_width
= o_large_width
.int_value
* PANGO_SCALE
;
798 #ifdef USE_PANGO_WRAP_WORD_CHAR
799 pango_layout_set_wrap(view
->layout
, PANGO_WRAP_WORD_CHAR
);
801 if (wrap_width
!= -1)
802 pango_layout_set_width(view
->layout
, wrap_width
);
804 pango_layout_get_size(view
->layout
, &w
, &h
);
805 view
->name_width
= w
/ PANGO_SCALE
;
806 view
->name_height
= h
/ PANGO_SCALE
;
809 /* Sets display_style from display_style_wanted.
810 * See also display_set_actual_size().
812 static void display_set_actual_size_real(FilerWindow
*filer_window
)
814 DisplayStyle size
= filer_window
->display_style_wanted
;
817 g_return_if_fail(filer_window
!= NULL
);
819 if (size
== AUTO_SIZE_ICONS
)
821 n
= view_count_items(filer_window
->view
);
823 if (n
>= o_filer_change_size_num
.int_value
)
829 filer_window
->display_style
= size
;