r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / display.c
blobab6ce4cd30b291584e23cb28dd13e059d4e7f9cb
1 /*
2 * $Id$
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)
10 * any later version.
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
15 * more details.
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 */
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <math.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <sys/param.h>
32 #include <unistd.h>
33 #include <time.h>
34 #include <ctype.h>
36 #include <gtk/gtk.h>
37 #include <gdk/gdkx.h>
38 #include <gdk/gdkkeysyms.h>
40 #include "global.h"
42 #include "main.h"
43 #include "filer.h"
44 #include "display.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "pixmaps.h"
48 #include "menu.h"
49 #include "dnd.h"
50 #include "run.h"
51 #include "mount.h"
52 #include "type.h"
53 #include "options.h"
54 #include "action.h"
55 #include "minibuffer.h"
56 #include "dir.h"
57 #include "diritem.h"
58 #include "fscache.h"
59 #include "view_iface.h"
61 #define HUGE_WRAP (1.5 * o_large_width.int_value)
63 /* Options bits */
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;
70 Option o_small_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 ****************************************************************/
88 void display_init()
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,
108 int *x, int y)
110 gdk_pixbuf_render_to_drawable_alpha(image->pixbuf,
111 window,
112 0, 0, /* src */
113 *x, y, /* dest */
114 -1, -1,
115 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
116 GDK_RGB_DITHER_NORMAL, 0, 0);
118 *x+=image->width+1;
121 /* Draw this icon (including any symlink or mount symbol) inside the
122 * given rectangle.
124 void draw_huge_icon(GdkWindow *window, GdkRectangle *area,
125 DirItem *item, MaskedPixmap *image, gboolean selected)
127 int width, height;
128 int image_x;
129 int image_y;
131 if (!image)
132 return;
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,
142 window,
143 0, 0, /* src */
144 image_x, area->y + image_y, /* dest */
145 width, height,
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
152 ? im_mounted
153 : im_unmounted;
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
167 * given rectangle.
169 void draw_large_icon(GdkWindow *window,
170 GdkRectangle *area,
171 DirItem *item,
172 MaskedPixmap *image,
173 gboolean selected)
175 int width;
176 int height;
177 int image_x;
178 int image_y;
180 if (!image)
181 return;
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,
190 window,
191 0, 0, /* src */
192 image_x, area->y + image_y, /* dest */
193 width, height,
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
200 ? im_mounted
201 : im_unmounted;
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;
219 if (!image)
220 return;
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,
232 window,
233 0, 0, /* src */
234 image_x, area->y + image_y, /* dest */
235 width, height,
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
242 ? im_mounted
243 : im_unmounted;
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))
263 #define SORT_DIRS \
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;
277 int retval;
279 SORT_DIRS;
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;
290 MIME_type *m1, *m2;
292 int diff = i1->base_type - i2->base_type;
294 if (!diff)
295 diff = (i1->flags & ITEM_FLAG_APPDIR)
296 - (i2->flags & ITEM_FLAG_APPDIR);
297 if (diff)
298 return diff > 0 ? 1 : -1;
300 m1 = i1->mime_type;
301 m2 = i2->mime_type;
303 if (m1 && m2)
305 diff = strcmp(m1->media_type, m2->media_type);
306 if (!diff)
307 diff = strcmp(m1->subtype, m2->subtype);
309 else if (m1 || m2)
310 diff = m1 ? 1 : -1;
311 else
312 diff = 0;
314 if (diff)
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;
324 const gchar *name1;
325 const gchar *name2;
327 if(i1->uid==i2->uid)
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;
340 const gchar *name1;
341 const gchar *name2;
343 if(i1->gid==i2->gid)
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;
369 SORT_DIRS;
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,
377 GtkSortType order)
379 if (filer_window->sort_type == sort_type &&
380 filer_window->sort_order == order)
381 return;
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,
393 DisplayStyle style,
394 DetailsType details,
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)
424 return;
426 filer_window->show_thumbs = thumbs;
428 view_style_changed(filer_window->view, VIEW_UPDATE_VIEWDATA);
430 if (!thumbs)
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)
449 return;
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
469 * appears later.
471 void display_set_autoselect(FilerWindow *filer_window, const gchar *leaf)
473 gchar *new;
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))
483 g_free(new);
484 else
485 filer_window->auto_select = new;
488 /* Change the icon size (wraps) */
489 void display_change_size(FilerWindow *filer_window, gboolean bigger)
491 DisplayStyle new;
493 g_return_if_fail(filer_window != NULL);
495 switch (filer_window->display_style)
497 case LARGE_ICONS:
498 new = bigger ? HUGE_ICONS : SMALL_ICONS;
499 break;
500 case HUGE_ICONS:
501 if (bigger)
502 return;
503 new = LARGE_ICONS;
504 break;
505 default:
506 if (!bigger)
507 return;
508 new = LARGE_ICONS;
509 break;
512 display_set_layout(filer_window, new, filer_window->details_type,
513 FALSE);
516 ViewData *display_create_viewdata(FilerWindow *filer_window, DirItem *item)
518 ViewData *view;
520 view = g_new(ViewData, 1);
522 view->layout = NULL;
523 view->details = NULL;
524 view->image = NULL;
526 display_update_view(filer_window, item, view, TRUE);
528 return view;
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)
548 GList *next;
550 for (next = all_filer_windows; next; next = next->next)
552 FilerWindow *filer_window = (FilerWindow *) next->data;
553 int flags = 0;
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;
576 guchar *buf = NULL;
577 gboolean scanned = item->base_type != TYPE_UNKNOWN;
579 if (filer_window->details_type == DETAILS_NONE)
580 return NULL;
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;
589 if (!scanned)
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);
604 g_free(ctime);
605 g_free(mtime);
606 g_free(atime);
608 else if (filer_window->details_type == DETAILS_PERMISSIONS)
610 if (!scanned)
611 return g_strdup("---,---,---/--"
612 #ifdef S_ISVTX
614 #endif
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));
622 else
624 if (!scanned)
626 if (filer_window->display_style == SMALL_ICONS)
627 return g_strdup("1234M");
628 else
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));
636 else
637 buf = g_strdup(format_size(item->size));
639 else
640 buf = g_strdup("-");
643 return buf;
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
661 * this information.
663 void display_update_view(FilerWindow *filer_window,
664 DirItem *item,
665 ViewData *view,
666 gboolean update_name_layout)
668 DisplayStyle style = filer_window->display_style;
669 int w, h;
670 int wrap_width = -1;
671 char *str;
672 static PangoFontDescription *monospace = NULL;
673 PangoAttrList *list = NULL;
675 if (!monospace)
676 monospace = pango_font_description_from_string("monospace");
678 if (view->details)
680 g_object_unref(G_OBJECT(view->details));
681 view->details = NULL;
684 str = details(filer_window, item);
685 if (str)
687 PangoAttrList *details_list;
688 int perm_offset = -1;
690 view->details = gtk_widget_create_pango_layout(
691 filer_window->window, str);
692 g_free(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)
700 perm_offset = 0;
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,
714 details_list);
718 if (view->image)
720 g_object_unref(view->image);
721 view->image = NULL;
724 if (filer_window->show_thumbs && item->base_type == TYPE_FILE /*&&
725 strcmp(item->mime_type->media_type, "image") == 0*/)
727 const guchar *path;
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);
735 if (!view->image)
737 view->image = di_image(item);
738 if (view->image)
739 g_object_ref(view->image);
742 if (view->layout && update_name_layout)
744 g_object_unref(G_OBJECT(view->layout));
745 view->layout = NULL;
748 if (view->layout)
750 /* Do nothing */
752 else if (g_utf8_validate(item->leafname, -1, NULL))
754 view->layout = gtk_widget_create_pango_layout(
755 filer_window->window, item->leafname);
757 else
759 PangoAttribute *attr;
760 gchar *utf8;
762 utf8 = to_utf8(item->leafname);
763 view->layout = gtk_widget_create_pango_layout(
764 filer_window->window, utf8);
765 g_free(utf8);
767 attr = pango_attr_foreground_new(0xffff, 0, 0);
768 attr->start_index = 0;
769 attr->end_index = -1;
770 if (!list)
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;
782 if (!list)
783 list = pango_attr_list_new();
784 pango_attr_list_insert(list, attr);
787 if (list)
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);
800 #endif
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;
815 int n;
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)
824 size = SMALL_ICONS;
825 else
826 size = LARGE_ICONS;
829 filer_window->display_style = size;