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
;
78 /* Static prototypes */
79 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
);
80 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
);
81 static void options_changed(void);
82 static char *details(FilerWindow
*filer_window
, DirItem
*item
);
83 static void display_set_actual_size_real(FilerWindow
*filer_window
);
85 /****************************************************************
86 * EXTERNAL INTERFACE *
87 ****************************************************************/
91 option_add_int(&o_display_caps_first
, "display_caps_first", FALSE
);
92 option_add_int(&o_display_dirs_first
, "display_dirs_first", FALSE
);
93 option_add_int(&o_display_size
, "display_icon_size", AUTO_SIZE_ICONS
);
94 option_add_int(&o_display_details
, "display_details", DETAILS_NONE
);
95 option_add_int(&o_display_sort_by
, "display_sort_by", SORT_NAME
);
96 option_add_int(&o_large_width
, "display_large_width", 155);
97 option_add_int(&o_small_width
, "display_small_width", 250);
98 option_add_int(&o_display_show_hidden
, "display_show_hidden", FALSE
);
99 option_add_int(&o_display_show_thumbs
, "display_show_thumbs", FALSE
);
100 option_add_int(&o_display_show_headers
, "display_show_headers", TRUE
);
101 option_add_int(&o_display_show_full_type
, "display_show_full_type", FALSE
);
102 option_add_int(&o_display_inherit_options
,
103 "display_inherit_options", FALSE
);
104 option_add_int(&o_filer_change_size_num
, "filer_change_size_num", 30);
106 option_add_notify(options_changed
);
109 static void draw_emblem_on_icon(GdkWindow
*window
, MaskedPixmap
*image
,
112 gdk_pixbuf_render_to_drawable_alpha(image
->pixbuf
,
117 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
118 GDK_RGB_DITHER_NORMAL
, 0, 0);
123 /* Draw this icon (including any symlink or mount symbol) inside the
126 void draw_huge_icon(GdkWindow
*window
, GdkRectangle
*area
,
127 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
)
136 width
= image
->huge_width
;
137 height
= image
->huge_height
;
138 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
139 image_y
= MAX(0, area
->height
- height
- 6);
141 gdk_pixbuf_render_to_drawable_alpha(
142 selected
? image
->huge_pixbuf_lit
143 : image
->huge_pixbuf
,
146 image_x
, area
->y
+ image_y
, /* dest */
148 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
149 GDK_RGB_DITHER_NORMAL
, 0, 0);
151 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
153 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
156 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
158 if (item
->flags
& ITEM_FLAG_SYMLINK
)
160 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
162 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
164 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
168 /* Draw this icon (including any symlink or mount symbol) inside the
171 void draw_large_icon(GdkWindow
*window
,
185 width
= MIN(image
->width
, ICON_WIDTH
);
186 height
= MIN(image
->height
, ICON_HEIGHT
);
187 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
188 image_y
= MAX(0, area
->height
- height
- 6);
190 gdk_pixbuf_render_to_drawable_alpha(
191 selected
? image
->pixbuf_lit
: image
->pixbuf
,
194 image_x
, area
->y
+ image_y
, /* dest */
196 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
197 GDK_RGB_DITHER_NORMAL
, 0, 0);
199 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
201 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
204 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
206 if (item
->flags
& ITEM_FLAG_SYMLINK
)
208 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
210 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
212 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
216 void draw_small_icon(GdkWindow
*window
, GdkRectangle
*area
,
217 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
)
219 int width
, height
, image_x
, image_y
;
224 if (!image
->sm_pixbuf
)
225 pixmap_make_small(image
);
227 width
= MIN(image
->sm_width
, SMALL_WIDTH
);
228 height
= MIN(image
->sm_height
, SMALL_HEIGHT
);
229 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
230 image_y
= MAX(0, SMALL_HEIGHT
- image
->sm_height
);
232 gdk_pixbuf_render_to_drawable_alpha(
233 selected
? image
->sm_pixbuf_lit
: image
->sm_pixbuf
,
236 image_x
, area
->y
+ image_y
, /* dest */
238 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
239 GDK_RGB_DITHER_NORMAL
, 0, 0);
241 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
243 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
246 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
248 if (item
->flags
& ITEM_FLAG_SYMLINK
)
250 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 8);
252 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
254 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 8);
258 /* The sort functions aren't called from outside, but they are
259 * passed as arguments to display_set_sort_fn().
262 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
263 !(item->flags & ITEM_FLAG_APPDIR))
266 if (o_display_dirs_first.int_value) { \
267 gboolean id1 = IS_A_DIR(i1); \
268 gboolean id2 = IS_A_DIR(i2); \
269 if (id1 && !id2) return -1; \
270 if (id2 && !id1) return 1; \
273 int sort_by_name(const void *item1
, const void *item2
)
275 const DirItem
*i1
= (DirItem
*) item1
;
276 const DirItem
*i2
= (DirItem
*) item2
;
277 CollateKey
*n1
= i1
->leafname_collate
;
278 CollateKey
*n2
= i2
->leafname_collate
;
283 retval
= collate_key_cmp(n1
, n2
, o_display_caps_first
.int_value
);
285 return retval
? retval
: strcmp(i1
->leafname
, i2
->leafname
);
288 int sort_by_type(const void *item1
, const void *item2
)
290 const DirItem
*i1
= (DirItem
*) item1
;
291 const DirItem
*i2
= (DirItem
*) item2
;
294 int diff
= i1
->base_type
- i2
->base_type
;
297 diff
= (i1
->flags
& ITEM_FLAG_APPDIR
)
298 - (i2
->flags
& ITEM_FLAG_APPDIR
);
300 return diff
> 0 ? 1 : -1;
307 diff
= strcmp(m1
->media_type
, m2
->media_type
);
309 diff
= strcmp(m1
->subtype
, m2
->subtype
);
317 return diff
> 0 ? 1 : -1;
319 return sort_by_name(item1
, item2
);
322 int sort_by_owner(const void *item1
, const void *item2
)
324 const DirItem
*i1
= (DirItem
*) item1
;
325 const DirItem
*i2
= (DirItem
*) item2
;
330 return sort_by_name(item1
, item2
);
332 name1
=user_name(i1
->uid
);
333 name2
=user_name(i2
->uid
);
335 return strcmp(name1
, name2
);
338 int sort_by_group(const void *item1
, const void *item2
)
340 const DirItem
*i1
= (DirItem
*) item1
;
341 const DirItem
*i2
= (DirItem
*) item2
;
346 return sort_by_name(item1
, item2
);
348 name1
=group_name(i1
->gid
);
349 name2
=group_name(i2
->gid
);
351 return strcmp(name1
, name2
);
354 int sort_by_date(const void *item1
, const void *item2
)
356 const DirItem
*i1
= (DirItem
*) item1
;
357 const DirItem
*i2
= (DirItem
*) item2
;
359 /* SORT_DIRS; -- too confusing! */
361 return i1
->mtime
< i2
->mtime
? -1 :
362 i1
->mtime
> i2
->mtime
? 1 :
363 sort_by_name(item1
, item2
);
366 int sort_by_size(const void *item1
, const void *item2
)
368 const DirItem
*i1
= (DirItem
*) item1
;
369 const DirItem
*i2
= (DirItem
*) item2
;
373 return i1
->size
< i2
->size
? -1 :
374 i1
->size
> i2
->size
? 1 :
375 sort_by_name(item1
, item2
);
378 void display_set_sort_type(FilerWindow
*filer_window
, SortType sort_type
,
381 if (filer_window
->sort_type
== sort_type
&&
382 filer_window
->sort_order
== order
)
385 filer_window
->sort_type
= sort_type
;
386 filer_window
->sort_order
= order
;
388 view_sort(filer_window
->view
);
391 /* Change the icon size and style.
392 * force_resize should only be TRUE for new windows.
394 void display_set_layout(FilerWindow
*filer_window
,
397 gboolean force_resize
)
399 gboolean style_changed
= FALSE
;
401 g_return_if_fail(filer_window
!= NULL
);
403 if (filer_window
->display_style_wanted
!= style
404 || filer_window
->details_type
!= details
)
406 style_changed
= TRUE
;
409 display_style_set(filer_window
, style
);
410 display_details_set(filer_window
, details
);
412 /* Recreate layouts because wrapping may have changed */
413 view_style_changed(filer_window
->view
, VIEW_UPDATE_NAME
);
415 if (force_resize
|| o_filer_auto_resize
.int_value
== RESIZE_ALWAYS
416 || (o_filer_auto_resize
.int_value
== RESIZE_STYLE
&& style_changed
))
418 view_autosize(filer_window
->view
);
422 /* Set the 'Show Thumbnails' flag for this window */
423 void display_set_thumbs(FilerWindow
*filer_window
, gboolean thumbs
)
425 if (filer_window
->show_thumbs
== thumbs
)
428 filer_window
->show_thumbs
= thumbs
;
430 view_style_changed(filer_window
->view
, VIEW_UPDATE_VIEWDATA
);
433 filer_cancel_thumbnails(filer_window
);
435 filer_set_title(filer_window
);
437 filer_create_thumbs(filer_window
);
440 void display_update_hidden(FilerWindow
*filer_window
)
442 filer_detach_rescan(filer_window
); /* (updates titlebar) */
444 display_set_actual_size(filer_window
, FALSE
);
447 /* Set the 'Show Hidden' flag for this window */
448 void display_set_hidden(FilerWindow
*filer_window
, gboolean hidden
)
450 if (filer_window
->show_hidden
== hidden
)
454 filer_window->show_hidden = hidden;
456 filer_set_hidden(filer_window
, hidden
);
458 display_update_hidden(filer_window
);
461 void display_set_filter(FilerWindow
*filer_window
, FilterType type
,
462 const gchar
*filter_string
)
464 if (filer_set_filter(filer_window
, type
, filter_string
))
465 display_update_hidden(filer_window
);
469 /* Highlight (wink or cursor) this item in the filer window. If the item
470 * isn't already there but we're scanning then highlight it if it
473 void display_set_autoselect(FilerWindow
*filer_window
, const gchar
*leaf
)
477 g_return_if_fail(filer_window
!= NULL
);
478 g_return_if_fail(leaf
!= NULL
);
480 new = g_strdup(leaf
); /* leaf == old value sometimes */
482 null_g_free(&filer_window
->auto_select
);
484 if (view_autoselect(filer_window
->view
, new))
487 filer_window
->auto_select
= new;
490 /* Change the icon size (wraps) */
491 void display_change_size(FilerWindow
*filer_window
, gboolean bigger
)
495 g_return_if_fail(filer_window
!= NULL
);
497 switch (filer_window
->display_style
)
500 new = bigger
? HUGE_ICONS
: SMALL_ICONS
;
514 display_set_layout(filer_window
, new, filer_window
->details_type
,
518 ViewData
*display_create_viewdata(FilerWindow
*filer_window
, DirItem
*item
)
522 view
= g_new(ViewData
, 1);
525 view
->details
= NULL
;
528 display_update_view(filer_window
, item
, view
, TRUE
);
533 /* Set the display style to the desired style. If the desired style
534 * is AUTO_SIZE_ICONS, choose an appropriate size. Also resizes filer
535 * window, if requested.
537 void display_set_actual_size(FilerWindow
*filer_window
, gboolean force_resize
)
539 display_set_layout(filer_window
, filer_window
->display_style_wanted
,
540 filer_window
->details_type
, force_resize
);
544 /****************************************************************
545 * INTERNAL FUNCTIONS *
546 ****************************************************************/
548 static void options_changed(void)
552 for (next
= all_filer_windows
; next
; next
= next
->next
)
554 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
557 if (o_display_dirs_first
.has_changed
||
558 o_display_caps_first
.has_changed
)
559 view_sort(VIEW(filer_window
->view
));
561 if (o_display_show_headers
.has_changed
)
562 flags
|= VIEW_UPDATE_HEADERS
;
564 if (o_large_width
.has_changed
|| o_small_width
.has_changed
)
565 flags
|= VIEW_UPDATE_NAME
; /* Recreate PangoLayout */
567 view_style_changed(filer_window
->view
, flags
);
571 /* Return a new string giving details of this item, or NULL if details
572 * are not being displayed. If details are not yet available, return
573 * a string of the right length.
575 static char *details(FilerWindow
*filer_window
, DirItem
*item
)
577 mode_t m
= item
->mode
;
579 gboolean scanned
= item
->base_type
!= TYPE_UNKNOWN
;
581 if (filer_window
->details_type
== DETAILS_NONE
)
584 if (scanned
&& item
->lstat_errno
)
585 buf
= g_strdup_printf(_("lstat(2) failed: %s"),
586 g_strerror(item
->lstat_errno
));
587 else if (filer_window
->details_type
== DETAILS_TYPE
)
589 MIME_type
*type
= item
->mime_type
;
592 return g_strdup("application/octet-stream");
594 buf
= g_strdup_printf("%s/%s",
595 type
->media_type
, type
->subtype
);
597 else if (filer_window
->details_type
== DETAILS_TIMES
)
599 guchar
*ctime
, *mtime
, *atime
;
601 ctime
= pretty_time(&item
->ctime
);
602 mtime
= pretty_time(&item
->mtime
);
603 atime
= pretty_time(&item
->atime
);
605 buf
= g_strdup_printf("a[%s] c[%s] m[%s]", atime
, ctime
, mtime
);
610 else if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
613 return g_strdup("---,---,---/--"
617 " 12345678 12345678");
619 buf
= g_strdup_printf("%s %-8.8s %-8.8s",
620 pretty_permissions(m
),
621 user_name(item
->uid
),
622 group_name(item
->gid
));
628 if (filer_window
->display_style
== SMALL_ICONS
)
629 return g_strdup("1234M");
631 return g_strdup("1234 bytes");
634 if (item
->base_type
!= TYPE_DIRECTORY
)
636 if (filer_window
->display_style
== SMALL_ICONS
)
637 buf
= g_strdup(format_size_aligned(item
->size
));
639 buf
= g_strdup(format_size(item
->size
));
648 /* Note: Call style_changed after this */
649 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
)
651 filer_window
->details_type
= details
;
654 /* Note: Call style_changed after this */
655 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
)
657 filer_window
->display_style_wanted
= style
;
658 display_set_actual_size_real(filer_window
);
661 /* Each displayed item has a ViewData structure with some cached information
662 * to help quickly draw the item (eg, the PangoLayout). This function updates
665 void display_update_view(FilerWindow
*filer_window
,
668 gboolean update_name_layout
)
670 DisplayStyle style
= filer_window
->display_style
;
674 static PangoFontDescription
*monospace
= NULL
;
675 PangoAttrList
*list
= NULL
;
678 monospace
= pango_font_description_from_string("monospace");
682 g_object_unref(G_OBJECT(view
->details
));
683 view
->details
= NULL
;
686 str
= details(filer_window
, item
);
689 PangoAttrList
*details_list
;
690 int perm_offset
= -1;
692 view
->details
= gtk_widget_create_pango_layout(
693 filer_window
->window
, str
);
696 pango_layout_set_font_description(view
->details
, monospace
);
697 pango_layout_get_size(view
->details
, &w
, &h
);
698 view
->details_width
= w
/ PANGO_SCALE
;
699 view
->details_height
= h
/ PANGO_SCALE
;
701 if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
703 if (perm_offset
> -1)
705 PangoAttribute
*attr
;
707 attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
709 perm_offset
+= 4 * applicable(item
->uid
, item
->gid
);
710 attr
->start_index
= perm_offset
;
711 attr
->end_index
= perm_offset
+ 3;
713 details_list
= pango_attr_list_new();
714 pango_attr_list_insert(details_list
, attr
);
715 pango_layout_set_attributes(view
->details
,
722 g_object_unref(view
->image
);
726 if (filer_window
->show_thumbs
&& item
->base_type
== TYPE_FILE
/*&&
727 strcmp(item->mime_type->media_type, "image") == 0*/)
731 path
= make_path(filer_window
->real_path
, item
->leafname
);
733 view
->image
= g_fscache_lookup_full(pixmap_cache
, path
,
734 FSCACHE_LOOKUP_ONLY_NEW
, NULL
);
739 view
->image
= di_image(item
);
741 g_object_ref(view
->image
);
744 if (view
->layout
&& update_name_layout
)
746 g_object_unref(G_OBJECT(view
->layout
));
754 else if (g_utf8_validate(item
->leafname
, -1, NULL
))
756 view
->layout
= gtk_widget_create_pango_layout(
757 filer_window
->window
, item
->leafname
);
761 PangoAttribute
*attr
;
764 utf8
= to_utf8(item
->leafname
);
765 view
->layout
= gtk_widget_create_pango_layout(
766 filer_window
->window
, utf8
);
769 attr
= pango_attr_foreground_new(0xffff, 0, 0);
770 attr
->start_index
= 0;
771 attr
->end_index
= -1;
773 list
= pango_attr_list_new();
774 pango_attr_list_insert(list
, attr
);
777 if (item
->flags
& ITEM_FLAG_RECENT
)
779 PangoAttribute
*attr
;
781 attr
= pango_attr_weight_new(PANGO_WEIGHT_BOLD
);
782 attr
->start_index
= 0;
783 attr
->end_index
= -1;
785 list
= pango_attr_list_new();
786 pango_attr_list_insert(list
, attr
);
790 pango_layout_set_attributes(view
->layout
, list
);
792 if (filer_window
->details_type
== DETAILS_NONE
)
794 if (style
== HUGE_ICONS
)
795 wrap_width
= HUGE_WRAP
* PANGO_SCALE
;
796 else if (style
== LARGE_ICONS
)
797 wrap_width
= o_large_width
.int_value
* PANGO_SCALE
;
800 #ifdef USE_PANGO_WRAP_WORD_CHAR
801 pango_layout_set_wrap(view
->layout
, PANGO_WRAP_WORD_CHAR
);
803 if (wrap_width
!= -1)
804 pango_layout_set_width(view
->layout
, wrap_width
);
806 pango_layout_get_size(view
->layout
, &w
, &h
);
807 view
->name_width
= w
/ PANGO_SCALE
;
808 view
->name_height
= h
/ PANGO_SCALE
;
811 /* Sets display_style from display_style_wanted.
812 * See also display_set_actual_size().
814 static void display_set_actual_size_real(FilerWindow
*filer_window
)
816 DisplayStyle size
= filer_window
->display_style_wanted
;
819 g_return_if_fail(filer_window
!= NULL
);
821 if (size
== AUTO_SIZE_ICONS
)
823 n
= view_count_items(filer_window
->view
);
825 if (n
>= o_filer_change_size_num
.int_value
)
831 filer_window
->display_style
= size
;