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
)
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);
144 gdk_pixbuf_render_to_drawable_alpha(
145 selected
? image
->huge_pixbuf_lit
146 : image
->huge_pixbuf
,
149 image_x
, area
->y
+ image_y
, /* dest */
151 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
152 GDK_RGB_DITHER_NORMAL
, 0, 0);
154 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
156 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
159 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
161 if (item
->flags
& ITEM_FLAG_SYMLINK
)
163 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
165 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
167 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
171 /* Draw this icon (including any symlink or mount symbol) inside the
174 void draw_large_icon(GdkWindow
*window
,
188 width
= MIN(image
->width
, ICON_WIDTH
);
189 height
= MIN(image
->height
, ICON_HEIGHT
);
190 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
191 image_y
= MAX(0, area
->height
- height
- 6);
193 gdk_pixbuf_render_to_drawable_alpha(
194 selected
? image
->pixbuf_lit
: image
->pixbuf
,
197 image_x
, area
->y
+ image_y
, /* dest */
199 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
200 GDK_RGB_DITHER_NORMAL
, 0, 0);
202 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
204 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
207 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
209 if (item
->flags
& ITEM_FLAG_SYMLINK
)
211 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 2);
213 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
215 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 2);
219 void draw_small_icon(GdkWindow
*window
, GdkRectangle
*area
,
220 DirItem
*item
, MaskedPixmap
*image
, gboolean selected
)
222 int width
, height
, image_x
, image_y
;
227 if (!image
->sm_pixbuf
)
228 pixmap_make_small(image
);
230 width
= MIN(image
->sm_width
, SMALL_WIDTH
);
231 height
= MIN(image
->sm_height
, SMALL_HEIGHT
);
232 image_x
= area
->x
+ ((area
->width
- width
) >> 1);
233 image_y
= MAX(0, SMALL_HEIGHT
- image
->sm_height
);
235 gdk_pixbuf_render_to_drawable_alpha(
236 selected
? image
->sm_pixbuf_lit
: image
->sm_pixbuf
,
239 image_x
, area
->y
+ image_y
, /* dest */
241 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
242 GDK_RGB_DITHER_NORMAL
, 0, 0);
244 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
246 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
249 draw_emblem_on_icon(window
, mp
, &image_x
, area
->y
+ 2);
251 if (item
->flags
& ITEM_FLAG_SYMLINK
)
253 draw_emblem_on_icon(window
, im_symlink
, &image_x
, area
->y
+ 8);
255 if (item
->flags
& ITEM_FLAG_HAS_XATTR
)
257 draw_emblem_on_icon(window
, im_xattr
, &image_x
, area
->y
+ 8);
261 /* The sort functions aren't called from outside, but they are
262 * passed as arguments to display_set_sort_fn().
265 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
266 !(item->flags & ITEM_FLAG_APPDIR))
269 if (o_display_dirs_first.int_value) { \
270 gboolean id1 = IS_A_DIR(i1); \
271 gboolean id2 = IS_A_DIR(i2); \
272 if (id1 && !id2) return -1; \
273 if (id2 && !id1) return 1; \
276 int sort_by_name(const void *item1
, const void *item2
)
278 const DirItem
*i1
= (DirItem
*) item1
;
279 const DirItem
*i2
= (DirItem
*) item2
;
280 CollateKey
*n1
= i1
->leafname_collate
;
281 CollateKey
*n2
= i2
->leafname_collate
;
286 retval
= collate_key_cmp(n1
, n2
, o_display_caps_first
.int_value
);
288 return retval
? retval
: strcmp(i1
->leafname
, i2
->leafname
);
291 int sort_by_type(const void *item1
, const void *item2
)
293 const DirItem
*i1
= (DirItem
*) item1
;
294 const DirItem
*i2
= (DirItem
*) item2
;
297 int diff
= i1
->base_type
- i2
->base_type
;
300 diff
= (i1
->flags
& ITEM_FLAG_APPDIR
)
301 - (i2
->flags
& ITEM_FLAG_APPDIR
);
303 return diff
> 0 ? 1 : -1;
310 diff
= strcmp(m1
->media_type
, m2
->media_type
);
312 diff
= strcmp(m1
->subtype
, m2
->subtype
);
320 return diff
> 0 ? 1 : -1;
322 return sort_by_name(item1
, item2
);
325 int sort_by_owner(const void *item1
, const void *item2
)
327 const DirItem
*i1
= (DirItem
*) item1
;
328 const DirItem
*i2
= (DirItem
*) item2
;
333 return sort_by_name(item1
, item2
);
335 name1
=user_name(i1
->uid
);
336 name2
=user_name(i2
->uid
);
338 return strcmp(name1
, name2
);
341 int sort_by_group(const void *item1
, const void *item2
)
343 const DirItem
*i1
= (DirItem
*) item1
;
344 const DirItem
*i2
= (DirItem
*) item2
;
349 return sort_by_name(item1
, item2
);
351 name1
=group_name(i1
->gid
);
352 name2
=group_name(i2
->gid
);
354 return strcmp(name1
, name2
);
357 int sort_by_date(const void *item1
, const void *item2
)
359 const DirItem
*i1
= (DirItem
*) item1
;
360 const DirItem
*i2
= (DirItem
*) item2
;
362 /* SORT_DIRS; -- too confusing! */
364 return i1
->mtime
< i2
->mtime
? -1 :
365 i1
->mtime
> i2
->mtime
? 1 :
366 sort_by_name(item1
, item2
);
369 int sort_by_size(const void *item1
, const void *item2
)
371 const DirItem
*i1
= (DirItem
*) item1
;
372 const DirItem
*i2
= (DirItem
*) item2
;
376 return i1
->size
< i2
->size
? -1 :
377 i1
->size
> i2
->size
? 1 :
378 sort_by_name(item1
, item2
);
381 void display_set_sort_type(FilerWindow
*filer_window
, SortType sort_type
,
384 if (filer_window
->sort_type
== sort_type
&&
385 filer_window
->sort_order
== order
)
388 filer_window
->sort_type
= sort_type
;
389 filer_window
->sort_order
= order
;
391 view_sort(filer_window
->view
);
394 /* Change the icon size and style.
395 * force_resize should only be TRUE for new windows.
397 void display_set_layout(FilerWindow
*filer_window
,
400 gboolean force_resize
)
402 gboolean style_changed
= FALSE
;
404 g_return_if_fail(filer_window
!= NULL
);
406 if (filer_window
->display_style_wanted
!= style
407 || filer_window
->details_type
!= details
)
409 style_changed
= TRUE
;
412 display_style_set(filer_window
, style
);
413 display_details_set(filer_window
, details
);
415 /* Recreate layouts because wrapping may have changed */
416 view_style_changed(filer_window
->view
, VIEW_UPDATE_NAME
);
418 if (force_resize
|| o_filer_auto_resize
.int_value
== RESIZE_ALWAYS
419 || (o_filer_auto_resize
.int_value
== RESIZE_STYLE
&& style_changed
))
421 view_autosize(filer_window
->view
);
425 /* Set the 'Show Thumbnails' flag for this window */
426 void display_set_thumbs(FilerWindow
*filer_window
, gboolean thumbs
)
428 if (filer_window
->show_thumbs
== thumbs
)
431 filer_window
->show_thumbs
= thumbs
;
433 view_style_changed(filer_window
->view
, VIEW_UPDATE_VIEWDATA
);
436 filer_cancel_thumbnails(filer_window
);
438 filer_set_title(filer_window
);
440 filer_create_thumbs(filer_window
);
443 void display_update_hidden(FilerWindow
*filer_window
)
445 filer_detach_rescan(filer_window
); /* (updates titlebar) */
447 display_set_actual_size(filer_window
, FALSE
);
450 /* Set the 'Show Hidden' flag for this window */
451 void display_set_hidden(FilerWindow
*filer_window
, gboolean hidden
)
453 if (filer_window
->show_hidden
== hidden
)
457 filer_window->show_hidden = hidden;
459 filer_set_hidden(filer_window
, hidden
);
461 display_update_hidden(filer_window
);
464 void display_set_filter(FilerWindow
*filer_window
, FilterType type
,
465 const gchar
*filter_string
)
467 if (filer_set_filter(filer_window
, type
, filter_string
))
468 display_update_hidden(filer_window
);
472 /* Highlight (wink or cursor) this item in the filer window. If the item
473 * isn't already there but we're scanning then highlight it if it
476 void display_set_autoselect(FilerWindow
*filer_window
, const gchar
*leaf
)
480 g_return_if_fail(filer_window
!= NULL
);
481 g_return_if_fail(leaf
!= NULL
);
483 new = g_strdup(leaf
); /* leaf == old value sometimes */
485 null_g_free(&filer_window
->auto_select
);
487 if (view_autoselect(filer_window
->view
, new))
490 filer_window
->auto_select
= new;
493 /* Change the icon size (wraps) */
494 void display_change_size(FilerWindow
*filer_window
, gboolean bigger
)
498 g_return_if_fail(filer_window
!= NULL
);
500 switch (filer_window
->display_style
)
503 new = bigger
? HUGE_ICONS
: SMALL_ICONS
;
517 display_set_layout(filer_window
, new, filer_window
->details_type
,
521 ViewData
*display_create_viewdata(FilerWindow
*filer_window
, DirItem
*item
)
525 view
= g_new(ViewData
, 1);
528 view
->details
= NULL
;
531 display_update_view(filer_window
, item
, view
, TRUE
);
536 /* Set the display style to the desired style. If the desired style
537 * is AUTO_SIZE_ICONS, choose an appropriate size. Also resizes filer
538 * window, if requested.
540 void display_set_actual_size(FilerWindow
*filer_window
, gboolean force_resize
)
542 display_set_layout(filer_window
, filer_window
->display_style_wanted
,
543 filer_window
->details_type
, force_resize
);
547 /****************************************************************
548 * INTERNAL FUNCTIONS *
549 ****************************************************************/
551 static void options_changed(void)
555 for (next
= all_filer_windows
; next
; next
= next
->next
)
557 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
560 if (o_display_dirs_first
.has_changed
||
561 o_display_caps_first
.has_changed
)
562 view_sort(VIEW(filer_window
->view
));
564 if (o_display_show_headers
.has_changed
)
565 flags
|= VIEW_UPDATE_HEADERS
;
567 if (o_large_width
.has_changed
|| o_small_width
.has_changed
)
568 flags
|= VIEW_UPDATE_NAME
; /* Recreate PangoLayout */
570 view_style_changed(filer_window
->view
, flags
);
574 /* Return a new string giving details of this item, or NULL if details
575 * are not being displayed. If details are not yet available, return
576 * a string of the right length.
578 static char *details(FilerWindow
*filer_window
, DirItem
*item
)
580 mode_t m
= item
->mode
;
582 gboolean scanned
= item
->base_type
!= TYPE_UNKNOWN
;
584 if (filer_window
->details_type
== DETAILS_NONE
)
587 if (scanned
&& item
->lstat_errno
)
588 buf
= g_strdup_printf(_("lstat(2) failed: %s"),
589 g_strerror(item
->lstat_errno
));
590 else if (filer_window
->details_type
== DETAILS_TYPE
)
592 MIME_type
*type
= item
->mime_type
;
595 return g_strdup("application/octet-stream");
597 buf
= g_strdup_printf("%s/%s",
598 type
->media_type
, type
->subtype
);
600 else if (filer_window
->details_type
== DETAILS_TIMES
)
602 guchar
*ctime
, *mtime
, *atime
;
604 ctime
= pretty_time(&item
->ctime
);
605 mtime
= pretty_time(&item
->mtime
);
606 atime
= pretty_time(&item
->atime
);
608 buf
= g_strdup_printf("a[%s] c[%s] m[%s]", atime
, ctime
, mtime
);
613 else if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
616 return g_strdup("---,---,---/--"
620 " 12345678 12345678");
622 buf
= g_strdup_printf("%s %-8.8s %-8.8s",
623 pretty_permissions(m
),
624 user_name(item
->uid
),
625 group_name(item
->gid
));
631 if (filer_window
->display_style
== SMALL_ICONS
)
632 return g_strdup("1234M");
634 return g_strdup("1234 bytes");
637 if (item
->base_type
!= TYPE_DIRECTORY
)
639 if (filer_window
->display_style
== SMALL_ICONS
)
640 buf
= g_strdup(format_size_aligned(item
->size
));
642 buf
= g_strdup(format_size(item
->size
));
651 /* Note: Call style_changed after this */
652 static void display_details_set(FilerWindow
*filer_window
, DetailsType details
)
654 filer_window
->details_type
= details
;
657 /* Note: Call style_changed after this */
658 static void display_style_set(FilerWindow
*filer_window
, DisplayStyle style
)
660 filer_window
->display_style_wanted
= style
;
661 display_set_actual_size_real(filer_window
);
664 /* Each displayed item has a ViewData structure with some cached information
665 * to help quickly draw the item (eg, the PangoLayout). This function updates
668 void display_update_view(FilerWindow
*filer_window
,
671 gboolean update_name_layout
)
673 DisplayStyle style
= filer_window
->display_style
;
677 static PangoFontDescription
*monospace
= NULL
;
678 PangoAttrList
*list
= NULL
;
681 monospace
= pango_font_description_from_string("monospace");
685 g_object_unref(G_OBJECT(view
->details
));
686 view
->details
= NULL
;
689 str
= details(filer_window
, item
);
692 PangoAttrList
*details_list
;
693 int perm_offset
= -1;
695 view
->details
= gtk_widget_create_pango_layout(
696 filer_window
->window
, str
);
699 pango_layout_set_font_description(view
->details
, monospace
);
700 pango_layout_get_size(view
->details
, &w
, &h
);
701 view
->details_width
= w
/ PANGO_SCALE
;
702 view
->details_height
= h
/ PANGO_SCALE
;
704 if (filer_window
->details_type
== DETAILS_PERMISSIONS
)
706 if (perm_offset
> -1)
708 PangoAttribute
*attr
;
710 attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
712 perm_offset
+= 4 * applicable(item
->uid
, item
->gid
);
713 attr
->start_index
= perm_offset
;
714 attr
->end_index
= perm_offset
+ 3;
716 details_list
= pango_attr_list_new();
717 pango_attr_list_insert(details_list
, attr
);
718 pango_layout_set_attributes(view
->details
,
725 g_object_unref(view
->image
);
729 if (filer_window
->show_thumbs
&& item
->base_type
== TYPE_FILE
/*&&
730 strcmp(item->mime_type->media_type, "image") == 0*/)
734 path
= make_path(filer_window
->real_path
, item
->leafname
);
736 view
->image
= g_fscache_lookup_full(pixmap_cache
, path
,
737 FSCACHE_LOOKUP_ONLY_NEW
, NULL
);
742 view
->image
= di_image(item
);
744 g_object_ref(view
->image
);
747 if (view
->layout
&& update_name_layout
)
749 g_object_unref(G_OBJECT(view
->layout
));
757 else if (g_utf8_validate(item
->leafname
, -1, NULL
))
759 view
->layout
= gtk_widget_create_pango_layout(
760 filer_window
->window
, item
->leafname
);
764 PangoAttribute
*attr
;
767 utf8
= to_utf8(item
->leafname
);
768 view
->layout
= gtk_widget_create_pango_layout(
769 filer_window
->window
, utf8
);
772 attr
= pango_attr_foreground_new(0xffff, 0, 0);
773 attr
->start_index
= 0;
774 attr
->end_index
= -1;
776 list
= pango_attr_list_new();
777 pango_attr_list_insert(list
, attr
);
780 if (item
->flags
& ITEM_FLAG_RECENT
)
782 PangoAttribute
*attr
;
784 attr
= pango_attr_weight_new(PANGO_WEIGHT_BOLD
);
785 attr
->start_index
= 0;
786 attr
->end_index
= -1;
788 list
= pango_attr_list_new();
789 pango_attr_list_insert(list
, attr
);
793 pango_layout_set_attributes(view
->layout
, list
);
795 if (filer_window
->details_type
== DETAILS_NONE
)
797 if (style
== HUGE_ICONS
)
798 wrap_width
= HUGE_WRAP
* PANGO_SCALE
;
799 else if (style
== LARGE_ICONS
)
800 wrap_width
= o_large_width
.int_value
* PANGO_SCALE
;
803 #ifdef USE_PANGO_WRAP_WORD_CHAR
804 pango_layout_set_wrap(view
->layout
, PANGO_WRAP_WORD_CHAR
);
806 if (wrap_width
!= -1)
807 pango_layout_set_width(view
->layout
, wrap_width
);
809 pango_layout_get_size(view
->layout
, &w
, &h
);
810 view
->name_width
= w
/ PANGO_SCALE
;
811 view
->name_height
= h
/ PANGO_SCALE
;
814 /* Sets display_style from display_style_wanted.
815 * See also display_set_actual_size().
817 static void display_set_actual_size_real(FilerWindow
*filer_window
)
819 DisplayStyle size
= filer_window
->display_style_wanted
;
822 g_return_if_fail(filer_window
!= NULL
);
824 if (size
== AUTO_SIZE_ICONS
)
826 n
= view_count_items(filer_window
->view
);
828 if (n
>= o_filer_change_size_num
.int_value
)
834 filer_window
->display_style
= size
;