r3852: Option to show full MIME type name in details mode
[rox-filer/dt.git] / ROX-Filer / src / display.c
blob1d491ed2a2d9e52c66491eafe22f2c4b2c9f5cfc
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_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 ****************************************************************/
89 void display_init()
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,
110 int *x, int y)
112 gdk_pixbuf_render_to_drawable_alpha(image->pixbuf,
113 window,
114 0, 0, /* src */
115 *x, y, /* dest */
116 -1, -1,
117 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
118 GDK_RGB_DITHER_NORMAL, 0, 0);
120 *x+=image->width+1;
123 /* Draw this icon (including any symlink or mount symbol) inside the
124 * given rectangle.
126 void draw_huge_icon(GdkWindow *window, GdkRectangle *area,
127 DirItem *item, MaskedPixmap *image, gboolean selected)
129 int width, height;
130 int image_x;
131 int image_y;
133 if (!image)
134 return;
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,
144 window,
145 0, 0, /* src */
146 image_x, area->y + image_y, /* dest */
147 width, height,
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
154 ? im_mounted
155 : im_unmounted;
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
169 * given rectangle.
171 void draw_large_icon(GdkWindow *window,
172 GdkRectangle *area,
173 DirItem *item,
174 MaskedPixmap *image,
175 gboolean selected)
177 int width;
178 int height;
179 int image_x;
180 int image_y;
182 if (!image)
183 return;
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,
192 window,
193 0, 0, /* src */
194 image_x, area->y + image_y, /* dest */
195 width, height,
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
202 ? im_mounted
203 : im_unmounted;
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;
221 if (!image)
222 return;
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,
234 window,
235 0, 0, /* src */
236 image_x, area->y + image_y, /* dest */
237 width, height,
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
244 ? im_mounted
245 : im_unmounted;
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))
265 #define SORT_DIRS \
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;
279 int retval;
281 SORT_DIRS;
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;
292 MIME_type *m1, *m2;
294 int diff = i1->base_type - i2->base_type;
296 if (!diff)
297 diff = (i1->flags & ITEM_FLAG_APPDIR)
298 - (i2->flags & ITEM_FLAG_APPDIR);
299 if (diff)
300 return diff > 0 ? 1 : -1;
302 m1 = i1->mime_type;
303 m2 = i2->mime_type;
305 if (m1 && m2)
307 diff = strcmp(m1->media_type, m2->media_type);
308 if (!diff)
309 diff = strcmp(m1->subtype, m2->subtype);
311 else if (m1 || m2)
312 diff = m1 ? 1 : -1;
313 else
314 diff = 0;
316 if (diff)
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;
326 const gchar *name1;
327 const gchar *name2;
329 if(i1->uid==i2->uid)
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;
342 const gchar *name1;
343 const gchar *name2;
345 if(i1->gid==i2->gid)
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;
371 SORT_DIRS;
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,
379 GtkSortType order)
381 if (filer_window->sort_type == sort_type &&
382 filer_window->sort_order == order)
383 return;
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,
395 DisplayStyle style,
396 DetailsType details,
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)
426 return;
428 filer_window->show_thumbs = thumbs;
430 view_style_changed(filer_window->view, VIEW_UPDATE_VIEWDATA);
432 if (!thumbs)
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)
451 return;
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
471 * appears later.
473 void display_set_autoselect(FilerWindow *filer_window, const gchar *leaf)
475 gchar *new;
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))
485 g_free(new);
486 else
487 filer_window->auto_select = new;
490 /* Change the icon size (wraps) */
491 void display_change_size(FilerWindow *filer_window, gboolean bigger)
493 DisplayStyle new;
495 g_return_if_fail(filer_window != NULL);
497 switch (filer_window->display_style)
499 case LARGE_ICONS:
500 new = bigger ? HUGE_ICONS : SMALL_ICONS;
501 break;
502 case HUGE_ICONS:
503 if (bigger)
504 return;
505 new = LARGE_ICONS;
506 break;
507 default:
508 if (!bigger)
509 return;
510 new = LARGE_ICONS;
511 break;
514 display_set_layout(filer_window, new, filer_window->details_type,
515 FALSE);
518 ViewData *display_create_viewdata(FilerWindow *filer_window, DirItem *item)
520 ViewData *view;
522 view = g_new(ViewData, 1);
524 view->layout = NULL;
525 view->details = NULL;
526 view->image = NULL;
528 display_update_view(filer_window, item, view, TRUE);
530 return view;
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)
550 GList *next;
552 for (next = all_filer_windows; next; next = next->next)
554 FilerWindow *filer_window = (FilerWindow *) next->data;
555 int flags = 0;
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;
578 guchar *buf = NULL;
579 gboolean scanned = item->base_type != TYPE_UNKNOWN;
581 if (filer_window->details_type == DETAILS_NONE)
582 return NULL;
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;
591 if (!scanned)
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);
606 g_free(ctime);
607 g_free(mtime);
608 g_free(atime);
610 else if (filer_window->details_type == DETAILS_PERMISSIONS)
612 if (!scanned)
613 return g_strdup("---,---,---/--"
614 #ifdef S_ISVTX
616 #endif
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));
624 else
626 if (!scanned)
628 if (filer_window->display_style == SMALL_ICONS)
629 return g_strdup("1234M");
630 else
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));
638 else
639 buf = g_strdup(format_size(item->size));
641 else
642 buf = g_strdup("-");
645 return buf;
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
663 * this information.
665 void display_update_view(FilerWindow *filer_window,
666 DirItem *item,
667 ViewData *view,
668 gboolean update_name_layout)
670 DisplayStyle style = filer_window->display_style;
671 int w, h;
672 int wrap_width = -1;
673 char *str;
674 static PangoFontDescription *monospace = NULL;
675 PangoAttrList *list = NULL;
677 if (!monospace)
678 monospace = pango_font_description_from_string("monospace");
680 if (view->details)
682 g_object_unref(G_OBJECT(view->details));
683 view->details = NULL;
686 str = details(filer_window, item);
687 if (str)
689 PangoAttrList *details_list;
690 int perm_offset = -1;
692 view->details = gtk_widget_create_pango_layout(
693 filer_window->window, str);
694 g_free(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)
702 perm_offset = 0;
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,
716 details_list);
720 if (view->image)
722 g_object_unref(view->image);
723 view->image = NULL;
726 if (filer_window->show_thumbs && item->base_type == TYPE_FILE /*&&
727 strcmp(item->mime_type->media_type, "image") == 0*/)
729 const guchar *path;
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);
737 if (!view->image)
739 view->image = di_image(item);
740 if (view->image)
741 g_object_ref(view->image);
744 if (view->layout && update_name_layout)
746 g_object_unref(G_OBJECT(view->layout));
747 view->layout = NULL;
750 if (view->layout)
752 /* Do nothing */
754 else if (g_utf8_validate(item->leafname, -1, NULL))
756 view->layout = gtk_widget_create_pango_layout(
757 filer_window->window, item->leafname);
759 else
761 PangoAttribute *attr;
762 gchar *utf8;
764 utf8 = to_utf8(item->leafname);
765 view->layout = gtk_widget_create_pango_layout(
766 filer_window->window, utf8);
767 g_free(utf8);
769 attr = pango_attr_foreground_new(0xffff, 0, 0);
770 attr->start_index = 0;
771 attr->end_index = -1;
772 if (!list)
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;
784 if (!list)
785 list = pango_attr_list_new();
786 pango_attr_list_insert(list, attr);
789 if (list)
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);
802 #endif
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;
817 int n;
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)
826 size = SMALL_ICONS;
827 else
828 size = LARGE_ICONS;
831 filer_window->display_style = size;