r1476: Options to forward button-3 clicks on the pinboard to the window manager
[rox-filer.git] / ROX-Filer / src / display.c
blobc67893f35086a024198148745007be4ae5811b04
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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 "display.h"
44 #include "collection.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "filer.h"
48 #include "pixmaps.h"
49 #include "menu.h"
50 #include "dnd.h"
51 #include "run.h"
52 #include "mount.h"
53 #include "type.h"
54 #include "options.h"
55 #include "action.h"
56 #include "minibuffer.h"
57 #include "dir.h"
58 #include "diritem.h"
59 #include "fscache.h"
61 #define ROW_HEIGHT_SMALL 20
62 #define ROW_HEIGHT_FULL_INFO 44
63 #define MIN_ITEM_WIDTH 64
65 #define MIN_TRUNCATE 0
66 #define MAX_TRUNCATE 250
68 #define HUGE_WRAP (1.5 * o_large_width.int_value)
70 /* Options bits */
71 static Option o_intelligent_sort;
72 static Option o_display_dirs_first;
73 Option o_display_size;
74 Option o_display_details;
75 Option o_display_sort_by;
76 static Option o_large_width;
77 static Option o_small_width;
78 Option o_display_show_hidden;
79 Option o_display_show_thumbs;
80 Option o_display_inherit_options;
83 /* GC for drawing colour filenames */
84 static GdkGC *type_gc = NULL;
86 typedef struct _Template Template;
88 struct _Template {
89 GdkRectangle icon;
90 GdkRectangle leafname;
91 GdkRectangle details;
94 #define SHOW_RECT(ite, template) \
95 g_print("%s: %dx%d+%d+%d %dx%d+%d+%d\n", \
96 item->leafname, \
97 (template)->leafname.width, (template)->leafname.height,\
98 (template)->leafname.x, (template)->leafname.y, \
99 (template)->icon.width, (template)->icon.height, \
100 (template)->icon.x, (template)->icon.y)
102 /* Static prototypes */
103 static void fill_template(GdkRectangle *area, CollectionItem *item,
104 FilerWindow *filer_window, Template *template);
105 static void huge_template(GdkRectangle *area, CollectionItem *colitem,
106 FilerWindow *filer_window, Template *template);
107 static void large_template(GdkRectangle *area, CollectionItem *colitem,
108 FilerWindow *filer_window, Template *template);
109 static void small_template(GdkRectangle *area, CollectionItem *colitem,
110 FilerWindow *filer_window, Template *template);
111 static void huge_full_template(GdkRectangle *area, CollectionItem *colitem,
112 FilerWindow *filer_window, Template *template);
113 static void large_full_template(GdkRectangle *area, CollectionItem *colitem,
114 FilerWindow *filer_window, Template *template);
115 static void small_full_template(GdkRectangle *area, CollectionItem *colitem,
116 FilerWindow *filer_window, Template *template);
117 static void draw_item(GtkWidget *widget,
118 CollectionItem *item,
119 GdkRectangle *area,
120 FilerWindow *filer_window);
121 static gboolean test_point(Collection *collection,
122 int point_x, int point_y,
123 CollectionItem *item,
124 int width, int height,
125 FilerWindow *filer_window);
126 static void display_details_set(FilerWindow *filer_window, DetailsType details);
127 static void display_style_set(FilerWindow *filer_window, DisplayStyle style);
128 static void options_changed(void);
129 static char *details(FilerWindow *filer_window, DirItem *item);
130 static void draw_string(GtkWidget *widget,
131 PangoLayout *layout,
132 GdkRectangle *area, /* Area available on screen */
133 int width, /* Width of the full string */
134 GtkStateType selection_state,
135 gboolean selected,
136 gboolean box);
138 enum {
139 SORT_BY_NAME = 0,
140 SORT_BY_TYPE = 1,
141 SORT_BY_DATE = 2,
142 SORT_BY_SIZE = 3,
145 /****************************************************************
146 * EXTERNAL INTERFACE *
147 ****************************************************************/
149 void display_init()
151 option_add_int(&o_intelligent_sort, "display_intelligent_sort", 1);
152 option_add_int(&o_display_dirs_first, "display_dirs_first", FALSE);
153 option_add_int(&o_display_size, "display_size", LARGE_ICONS);
154 option_add_int(&o_display_details, "display_details", DETAILS_NONE);
155 option_add_int(&o_display_sort_by, "display_sort_by", SORT_BY_NAME);
156 option_add_int(&o_large_width, "display_large_width", 89);
157 option_add_int(&o_small_width, "display_small_width", 250);
158 option_add_int(&o_display_show_hidden, "display_show_hidden", FALSE);
159 option_add_int(&o_display_show_thumbs, "display_show_thumbs", FALSE);
160 option_add_int(&o_display_inherit_options,"display_inherit_options",
161 FALSE);
162 option_add_notify(options_changed);
165 /* A template contains the locations of the three rectangles (for the icon,
166 * name and extra details).
167 * Fill in the empty 'template' with the rectanges for this item.
169 static void fill_template(GdkRectangle *area, CollectionItem *colitem,
170 FilerWindow *filer_window, Template *template)
172 DisplayStyle style = filer_window->display_style;
173 ViewData *view = (ViewData *) colitem->view_data;
175 if (view->details)
177 template->details.width = view->details_width;
178 template->details.height = view->details_height;
180 if (style == SMALL_ICONS)
181 small_full_template(area, colitem,
182 filer_window, template);
183 else if (style == LARGE_ICONS)
184 large_full_template(area, colitem,
185 filer_window, template);
186 else
187 huge_full_template(area, colitem,
188 filer_window, template);
190 else
192 if (style == HUGE_ICONS)
193 huge_template(area, colitem, filer_window, template);
194 else if (style == LARGE_ICONS)
195 large_template(area, colitem, filer_window, template);
196 else
197 small_template(area, colitem, filer_window, template);
201 /* Return the size needed for this item */
202 void calc_size(FilerWindow *filer_window, CollectionItem *colitem,
203 int *width, int *height)
205 int pix_width, pix_height;
206 int w;
207 DisplayStyle style = filer_window->display_style;
208 ViewData *view = (ViewData *) colitem->view_data;
210 if (filer_window->details_type == DETAILS_NONE)
212 if (style == HUGE_ICONS)
214 if (view->image)
216 if (!view->image->huge_pixbuf)
217 pixmap_make_huge(view->image);
218 pix_width = view->image->huge_width;
219 pix_height = view->image->huge_height;
221 else
223 pix_width = HUGE_WIDTH * 3 / 2;
224 pix_height = HUGE_HEIGHT * 3 / 2;
226 *width = MAX(pix_width, view->name_width) + 4;
227 *height = view->name_height + pix_height + 4;
229 else if (style == SMALL_ICONS)
231 w = MIN(view->name_width, o_small_width.int_value);
232 *width = SMALL_WIDTH + 12 + w;
233 *height = MAX(view->name_height, SMALL_HEIGHT) + 4;
235 else
237 if (view->image)
238 pix_width = view->image->width;
239 else
240 pix_width = HUGE_WIDTH * 3 / 2;
241 *width = MAX(pix_width, view->name_width) + 4;
242 *height = view->name_height + ICON_HEIGHT + 2;
245 else
247 w = view->details_width;
248 if (style == HUGE_ICONS)
250 *width = HUGE_WIDTH + 12 + MAX(w, view->name_width);
251 *height = HUGE_HEIGHT - 4;
253 else if (style == SMALL_ICONS)
255 int text_height;
257 *width = SMALL_WIDTH + view->name_width + 12 + w;
258 text_height = MAX(view->name_height,
259 view->details_height);
260 *height = MAX(text_height, SMALL_HEIGHT) + 4;
262 else
264 *width = ICON_WIDTH + 12 + MAX(w, view->name_width);
265 *height = ICON_HEIGHT;
270 /* Draw this icon (including any symlink or mount symbol) inside the
271 * given rectangle.
273 void draw_huge_icon(GtkWidget *widget,
274 GdkRectangle *area,
275 DirItem *item,
276 MaskedPixmap *image,
277 gboolean selected)
279 int width, height;
280 int image_x;
281 int image_y;
283 if (!image)
284 return;
286 width = image->huge_width;
287 height = image->huge_height;
288 image_x = area->x + ((area->width - width) >> 1);
289 image_y = MAX(0, area->height - height - 6);
291 gdk_pixbuf_render_to_drawable_alpha(
292 selected ? image->huge_pixbuf_lit
293 : image->huge_pixbuf,
294 widget->window,
295 0, 0, /* src */
296 image_x, area->y + image_y, /* dest */
297 width, height,
298 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
299 GDK_RGB_DITHER_NORMAL, 0, 0);
301 if (item->flags & ITEM_FLAG_SYMLINK)
303 gdk_pixbuf_render_to_drawable_alpha(im_symlink->pixbuf,
304 widget->window,
305 0, 0, /* src */
306 image_x, area->y + 2, /* dest */
307 -1, -1,
308 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
309 GDK_RGB_DITHER_NORMAL, 0, 0);
311 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
313 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
314 ? im_mounted
315 : im_unmounted;
317 gdk_pixbuf_render_to_drawable_alpha(mp->pixbuf,
318 widget->window,
319 0, 0, /* src */
320 image_x, area->y + 2, /* dest */
321 -1, -1,
322 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
323 GDK_RGB_DITHER_NORMAL, 0, 0);
327 /* Draw this icon (including any symlink or mount symbol) inside the
328 * given rectangle.
330 void draw_large_icon(GtkWidget *widget,
331 GdkRectangle *area,
332 DirItem *item,
333 MaskedPixmap *image,
334 gboolean selected)
336 int width;
337 int height;
338 int image_x;
339 int image_y;
341 if (!image)
342 return;
344 width = MIN(image->width, ICON_WIDTH);
345 height = MIN(image->height, ICON_HEIGHT);
346 image_x = area->x + ((area->width - width) >> 1);
347 image_y = MAX(0, area->height - height - 6);
349 gdk_pixbuf_render_to_drawable_alpha(
350 selected ? image->pixbuf_lit : image->pixbuf,
351 widget->window,
352 0, 0, /* src */
353 image_x, area->y + image_y, /* dest */
354 width, height,
355 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
356 GDK_RGB_DITHER_NORMAL, 0, 0);
358 if (item->flags & ITEM_FLAG_SYMLINK)
360 gdk_pixbuf_render_to_drawable_alpha(im_symlink->pixbuf,
361 widget->window,
362 0, 0, /* src */
363 image_x, area->y + 2, /* dest */
364 -1, -1,
365 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
366 GDK_RGB_DITHER_NORMAL, 0, 0);
368 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
370 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
371 ? im_mounted
372 : im_unmounted;
374 gdk_pixbuf_render_to_drawable_alpha(mp->pixbuf,
375 widget->window,
376 0, 0, /* src */
377 image_x, area->y + 2, /* dest */
378 -1, -1,
379 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
380 GDK_RGB_DITHER_NORMAL, 0, 0);
384 /* The sort functions aren't called from outside, but they are
385 * passed as arguments to display_set_sort_fn().
388 #define IS_A_DIR(item) (item->base_type == TYPE_DIRECTORY && \
389 !(item->flags & ITEM_FLAG_APPDIR))
391 #define SORT_DIRS \
392 if (o_display_dirs_first.int_value) { \
393 gboolean id1 = IS_A_DIR(i1); \
394 gboolean id2 = IS_A_DIR(i2); \
395 if (id1 && !id2) return -1; \
396 if (id2 && !id1) return 1; \
399 int sort_by_name(const void *item1, const void *item2)
401 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
402 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
403 char *n1 = i1->leafname_collate;
404 char *n2 = i2->leafname_collate;
406 SORT_DIRS;
408 if (!o_intelligent_sort.int_value)
409 return strcmp(i1->leafname, i2->leafname);
411 /* The following code was copied from PicoGUI (was LGPL) */
413 /* Sort the files, in a way that should make sense to users.
414 * Case is ignored, punctuation is ignored. If the file contains
415 * numbers, the numbers are sorted numerically.
417 while (*n1 && *n2)
419 char c1 = *n1, c2 = *n2;
421 /* If they are both numbers, sort them numerically */
422 if (isdigit(c1) && isdigit(c2))
424 char *p;
425 unsigned long u1,u2;
426 u1 = strtoul(n1, &p, 10);
427 n1 = p;
428 u2 = strtoul(n2, &p, 10);
429 n2 = p;
430 if (u1 < u2)
431 return -1;
432 else if (u1 > u2)
433 return 1;
434 continue;
437 /* Do a case-insensitive asciibetical sort */
438 c1 = tolower(c1);
439 c2 = tolower(c2);
440 if (c1 < c2)
441 return -1;
442 else if (c1 > c2)
443 return 1;
444 n1++;
445 n2++;
448 /* Compare length */
449 if (*n1)
450 return 1; /* First string is longer, so comes after */
451 if (*n2)
452 return -1;
454 /* If the strings are equal at the end of this, fallback to a straight
455 * ASCII sort so at least it's deterministic.
457 return strcmp(i1->leafname, i2->leafname);
460 int sort_by_type(const void *item1, const void *item2)
462 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
463 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
464 MIME_type *m1, *m2;
466 int diff = i1->base_type - i2->base_type;
468 if (!diff)
469 diff = (i1->flags & ITEM_FLAG_APPDIR)
470 - (i2->flags & ITEM_FLAG_APPDIR);
471 if (diff)
472 return diff > 0 ? 1 : -1;
474 m1 = i1->mime_type;
475 m2 = i2->mime_type;
477 if (m1 && m2)
479 diff = strcmp(m1->media_type, m2->media_type);
480 if (!diff)
481 diff = strcmp(m1->subtype, m2->subtype);
483 else if (m1 || m2)
484 diff = m1 ? 1 : -1;
485 else
486 diff = 0;
488 if (diff)
489 return diff > 0 ? 1 : -1;
491 return sort_by_name(item1, item2);
494 int sort_by_date(const void *item1, const void *item2)
496 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
497 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
499 SORT_DIRS;
501 return i1->mtime > i2->mtime ? -1 :
502 i1->mtime < i2->mtime ? 1 :
503 sort_by_name(item1, item2);
506 int sort_by_size(const void *item1, const void *item2)
508 const DirItem *i1 = (DirItem *) ((CollectionItem *) item1)->data;
509 const DirItem *i2 = (DirItem *) ((CollectionItem *) item2)->data;
511 SORT_DIRS;
513 return i1->size > i2->size ? -1 :
514 i1->size < i2->size ? 1 :
515 sort_by_name(item1, item2);
518 /* Make the items as small as possible */
519 void shrink_grid(FilerWindow *filer_window)
521 int i;
522 Collection *col = filer_window->collection;
523 int width = MIN_ITEM_WIDTH;
524 int height = SMALL_HEIGHT;
526 for (i = 0; i < col->number_of_items; i++)
528 int w, h;
530 calc_size(filer_window, &col->items[i], &w, &h);
531 if (w > width)
532 width = w;
533 if (h > height)
534 height = h;
537 collection_set_item_size(col, width, height);
540 void display_set_sort_fn(FilerWindow *filer_window,
541 int (*fn)(const void *a, const void *b))
543 if (filer_window->sort_fn == fn)
544 return;
546 filer_window->sort_fn = fn;
548 collection_qsort(filer_window->collection,
549 filer_window->sort_fn);
552 void display_set_layout(FilerWindow *filer_window,
553 DisplayStyle style,
554 DetailsType details)
556 g_return_if_fail(filer_window != NULL);
558 display_style_set(filer_window, style);
559 display_details_set(filer_window, details);
561 shrink_grid(filer_window);
563 if (o_filer_auto_resize.int_value != RESIZE_NEVER)
564 filer_window_autosize(filer_window, TRUE);
567 /* Set the 'Show Thumbnails' flag for this window */
568 void display_set_thumbs(FilerWindow *filer_window, gboolean thumbs)
570 if (filer_window->show_thumbs == thumbs)
571 return;
573 filer_window->show_thumbs = thumbs;
575 display_update_views(filer_window);
577 gtk_widget_queue_draw(GTK_WIDGET(filer_window->collection));
579 if (!thumbs)
580 filer_cancel_thumbnails(filer_window);
582 filer_set_title(filer_window);
584 filer_create_thumbs(filer_window);
587 /* Set the 'Show Hidden' flag for this window */
588 void display_set_hidden(FilerWindow *filer_window, gboolean hidden)
590 if (filer_window->show_hidden == hidden)
591 return;
593 filer_window->show_hidden = hidden;
595 filer_detach_rescan(filer_window);
597 filer_set_title(filer_window);
600 /* Highlight (wink or cursor) this item in the filer window. If the item
601 * isn't already there but we're scanning then highlight it if it
602 * appears later.
604 void display_set_autoselect(FilerWindow *filer_window, const gchar *leaf)
606 Collection *col;
607 int i;
609 g_return_if_fail(filer_window != NULL);
610 col = filer_window->collection;
612 for (i = 0; i < col->number_of_items; i++)
614 DirItem *item = (DirItem *) col->items[i].data;
616 if (strcmp(item->leafname, leaf) == 0)
618 if (col->cursor_item != -1)
619 collection_set_cursor_item(col, i);
620 else
621 collection_wink_item(col, i);
622 leaf = NULL;
623 goto out;
627 out:
628 g_free(filer_window->auto_select);
629 if (leaf)
630 filer_window->auto_select = g_strdup(leaf);
631 else
632 filer_window->auto_select = NULL;
635 gboolean display_is_truncated(FilerWindow *filer_window, int i)
637 Template template;
638 Collection *collection = filer_window->collection;
639 CollectionItem *colitem = &collection->items[i];
640 int col = i % collection->columns;
641 int row = i / collection->columns;
642 GdkRectangle area;
643 ViewData *view = (ViewData *) colitem->view_data;
645 if (filer_window->display_style == LARGE_ICONS ||
646 filer_window->display_style == HUGE_ICONS)
647 return FALSE; /* These wrap rather than truncate */
649 area.x = col * collection->item_width;
650 area.y = row * collection->item_height;
651 area.height = collection->item_height;
653 if (col == collection->columns - 1)
654 area.width = GTK_WIDGET(collection)->allocation.width - area.x;
655 else
656 area.width = collection->item_width;
658 fill_template(&area, colitem, filer_window, &template);
660 return template.leafname.width < view->name_width;
663 /* Change the icon size (wraps) */
664 void display_change_size(FilerWindow *filer_window, gboolean bigger)
666 DisplayStyle new;
668 g_return_if_fail(filer_window != NULL);
670 switch (filer_window->display_style)
672 case LARGE_ICONS:
673 new = bigger ? HUGE_ICONS : SMALL_ICONS;
674 break;
675 case HUGE_ICONS:
676 new = bigger ? SMALL_ICONS : LARGE_ICONS;
677 break;
678 default:
679 new = bigger ? LARGE_ICONS : HUGE_ICONS;
680 break;
683 display_set_layout(filer_window, new, filer_window->details_type);
686 ViewData *display_create_viewdata(FilerWindow *filer_window, DirItem *item)
688 ViewData *view;
690 view = g_new(ViewData, 1);
692 view->layout = NULL;
693 view->details = NULL;
694 view->image = NULL;
696 display_update_view(filer_window, item, view, TRUE);
698 return view;
701 void display_free_colitem(Collection *collection, CollectionItem *colitem)
703 ViewData *view = (ViewData *) colitem->view_data;
705 if (!view)
706 return;
708 if (view->layout)
710 g_object_unref(G_OBJECT(view->layout));
711 view->layout = NULL;
713 if (view->details)
714 g_object_unref(G_OBJECT(view->details));
716 if (view->image)
717 g_object_unref(view->image);
719 g_free(view);
722 /* Recalculate all the ViewData structs for this window.
723 * Useful when the display style has changed.
725 void display_update_views(FilerWindow *filer_window)
727 Collection *collection = filer_window->collection;
728 int i;
730 for (i = 0; i < collection->number_of_items; i++)
732 CollectionItem *ci = &collection->items[i];
734 display_update_view(filer_window, (DirItem *) ci->data,
735 (ViewData *) ci->view_data, TRUE);
739 /****************************************************************
740 * INTERNAL FUNCTIONS *
741 ****************************************************************/
743 static void options_changed(void)
745 GList *next = all_filer_windows;
747 while (next)
749 FilerWindow *filer_window = (FilerWindow *) next->data;
751 if (o_large_width.has_changed || o_small_width.has_changed)
753 /* Recreate PangoLayout */
754 display_update_views(filer_window);
757 if (o_intelligent_sort.has_changed ||
758 o_display_dirs_first.has_changed)
760 collection_qsort(filer_window->collection,
761 filer_window->sort_fn);
763 shrink_grid(filer_window);
764 gtk_widget_queue_draw(filer_window->window);
766 next = next->next;
770 static void huge_template(GdkRectangle *area, CollectionItem *colitem,
771 FilerWindow *filer_window, Template *template)
773 int col_width = filer_window->collection->item_width;
774 int text_x, text_y;
775 ViewData *view = (ViewData *) colitem->view_data;
776 MaskedPixmap *image = view->image;
778 if (image)
780 if (!image->huge_pixbuf)
781 pixmap_make_huge(image);
782 template->icon.width = image->huge_width;
783 template->icon.height = image->huge_height;
785 else
787 template->icon.width = HUGE_WIDTH * 3 / 2;
788 template->icon.height = HUGE_HEIGHT;
791 template->leafname.width = view->name_width;
792 template->leafname.height = view->name_height;
794 text_x = area->x + ((col_width - template->leafname.width) >> 1);
795 text_y = area->y + area->height - template->leafname.height;
797 template->leafname.x = text_x;
798 template->leafname.y = text_y;
800 template->icon.x = area->x + ((col_width - template->icon.width) >> 1);
801 template->icon.y = template->leafname.y - template->icon.height - 2;
804 static void large_template(GdkRectangle *area, CollectionItem *colitem,
805 FilerWindow *filer_window, Template *template)
807 int col_width = filer_window->collection->item_width;
808 int iwidth, iheight;
809 int image_x;
810 int image_y;
811 ViewData *view = (ViewData *) colitem->view_data;
812 MaskedPixmap *image = view->image;
814 int text_x, text_y;
816 if (image)
818 iwidth = MIN(image->width, ICON_WIDTH);
819 iheight = MIN(image->height + 6, ICON_HEIGHT);
821 else
823 iwidth = ICON_WIDTH;
824 iheight = ICON_HEIGHT;
826 image_x = area->x + ((col_width - iwidth) >> 1);
828 template->leafname.width = view->name_width;
829 template->leafname.height = view->name_height;
831 text_x = area->x + ((col_width - template->leafname.width) >> 1);
832 text_y = area->y + ICON_HEIGHT + 2;
834 template->leafname.x = text_x;
835 template->leafname.y = text_y;
837 image_y = text_y - iheight;
838 image_y = MAX(area->y, image_y);
840 template->icon.x = image_x;
841 template->icon.y = image_y;
842 template->icon.width = iwidth;
843 template->icon.height = MIN(ICON_HEIGHT, iheight);
846 static void small_template(GdkRectangle *area, CollectionItem *colitem,
847 FilerWindow *filer_window, Template *template)
849 int text_x = area->x + SMALL_WIDTH + 4;
850 int low_text_y;
851 int max_text_width = area->width - SMALL_WIDTH - 4;
852 ViewData *view = (ViewData *) colitem->view_data;
854 low_text_y = area->y + area->height / 2 - view->name_height / 2;
856 template->leafname.x = text_x;
857 template->leafname.y = low_text_y;
858 template->leafname.width = MIN(max_text_width, view->name_width);
859 template->leafname.height = view->name_height;
861 template->icon.x = area->x;
862 template->icon.y = area->y + 1;
863 template->icon.width = SMALL_WIDTH;
864 template->icon.height = SMALL_HEIGHT;
867 static void huge_full_template(GdkRectangle *area, CollectionItem *colitem,
868 FilerWindow *filer_window, Template *template)
870 int max_text_width = area->width - HUGE_WIDTH - 4;
871 ViewData *view = (ViewData *) colitem->view_data;
872 MaskedPixmap *image = view->image;
874 if (image)
876 if (!image->huge_pixbuf)
877 pixmap_make_huge(image);
878 template->icon.width = image->huge_width;
879 template->icon.height = image->huge_height;
881 else
883 template->icon.width = HUGE_WIDTH * 3 / 2;
884 template->icon.height = HUGE_HEIGHT;
887 template->icon.x = area->x + (HUGE_WIDTH - template->icon.width) / 2;
888 template->icon.y = area->y + (area->height - template->icon.height) / 2;
890 template->leafname.x = area->x + HUGE_WIDTH + 4;
891 template->leafname.y = area->y + area->height / 2
892 - (view->name_height + 2 + view->details_height) / 2;
893 template->leafname.width = MIN(max_text_width, view->name_width);
894 template->leafname.height = view->name_height;
896 if (!image)
897 return; /* Not scanned yet */
899 template->details.x = template->leafname.x;
900 template->details.y = template->leafname.y + view->name_height + 2;
903 static void large_full_template(GdkRectangle *area, CollectionItem *colitem,
904 FilerWindow *filer_window, Template *template)
906 int max_text_width = area->width - ICON_WIDTH - 4;
907 ViewData *view = (ViewData *) colitem->view_data;
908 MaskedPixmap *image = view->image;
910 if (image)
912 template->icon.width = image->width;
913 template->icon.height = image->height;
915 else
917 template->icon.width = ICON_WIDTH;
918 template->icon.height = ICON_HEIGHT;
921 template->icon.x = area->x + (ICON_WIDTH - template->icon.width) / 2;
922 template->icon.y = area->y + (area->height - template->icon.height) / 2;
925 template->leafname.x = area->x + ICON_WIDTH + 4;
926 template->leafname.y = area->y + area->height / 2
927 - (view->name_height + 2 + view->details_height) / 2;
928 template->leafname.width = MIN(max_text_width, view->name_width);
929 template->leafname.height = view->name_height;
931 if (!image)
932 return; /* Not scanned yet */
934 template->details.x = template->leafname.x;
935 template->details.y = template->leafname.y + view->name_height + 2;
938 static void small_full_template(GdkRectangle *area, CollectionItem *colitem,
939 FilerWindow *filer_window, Template *template)
941 int col_width = filer_window->collection->item_width;
942 ViewData *view = (ViewData *) colitem->view_data;
944 small_template(area, colitem, filer_window, template);
946 if (!view->image)
947 return; /* Not scanned yet */
949 template->details.x = area->x + col_width - template->details.width;
950 template->details.y = area->y + area->height / 2 - \
951 view->details_height / 2;
954 #define INSIDE(px, py, area) \
955 (px >= area.x && py >= area.y && \
956 px <= area.x + area.width && py <= area.y + area.height)
958 static gboolean test_point(Collection *collection,
959 int point_x, int point_y,
960 CollectionItem *colitem,
961 int width, int height,
962 FilerWindow *filer_window)
964 Template template;
965 GdkRectangle area;
966 ViewData *view = (ViewData *) colitem->view_data;
968 area.x = 0;
969 area.y = 0;
970 area.width = width;
971 area.height = height;
973 fill_template(&area, colitem, filer_window, &template);
975 return INSIDE(point_x, point_y, template.leafname) ||
976 INSIDE(point_x, point_y, template.icon) ||
977 (view->details && INSIDE(point_x, point_y, template.details));
980 static void draw_small_icon(GtkWidget *widget,
981 GdkRectangle *area,
982 DirItem *item,
983 MaskedPixmap *image,
984 gboolean selected)
986 int width, height, image_x, image_y;
988 if (!image)
989 return;
991 if (!image->sm_pixbuf)
992 pixmap_make_small(image);
994 width = MIN(image->sm_width, SMALL_WIDTH);
995 height = MIN(image->sm_height, SMALL_HEIGHT);
996 image_x = area->x + ((area->width - width) >> 1);
997 image_y = MAX(0, SMALL_HEIGHT - image->sm_height);
999 gdk_pixbuf_render_to_drawable_alpha(
1000 selected ? image->sm_pixbuf_lit : image->sm_pixbuf,
1001 widget->window,
1002 0, 0, /* src */
1003 image_x, area->y + image_y, /* dest */
1004 width, height,
1005 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
1006 GDK_RGB_DITHER_NORMAL, 0, 0);
1008 if (item->flags & ITEM_FLAG_SYMLINK)
1010 gdk_pixbuf_render_to_drawable_alpha(im_symlink->pixbuf,
1011 widget->window,
1012 0, 0, /* src */
1013 image_x, area->y + 8, /* dest */
1014 -1, -1,
1015 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
1016 GDK_RGB_DITHER_NORMAL, 0, 0);
1018 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
1020 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
1021 ? im_mounted
1022 : im_unmounted;
1024 gdk_pixbuf_render_to_drawable_alpha(mp->pixbuf,
1025 widget->window,
1026 0, 0, /* src */
1027 image_x + 2, area->y + 2, /* dest */
1028 -1, -1,
1029 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
1030 GDK_RGB_DITHER_NORMAL, 0, 0);
1034 /* Return a new string giving details of this item, or NULL if details
1035 * are not being displayed. If details are not yet available, return
1036 * a string of the right length.
1038 static char *details(FilerWindow *filer_window, DirItem *item)
1040 mode_t m = item->mode;
1041 guchar *buf = NULL;
1042 gboolean scanned = item->base_type != TYPE_UNKNOWN;
1044 if (filer_window->details_type == DETAILS_NONE)
1045 return NULL;
1047 if (scanned && item->lstat_errno)
1048 buf = g_strdup_printf(_("lstat(2) failed: %s"),
1049 g_strerror(item->lstat_errno));
1050 else if (filer_window->details_type == DETAILS_SUMMARY)
1052 gchar *time;
1054 if (!scanned)
1055 return g_strdup("XXXX ---,---,---/--- "
1056 "12345678 12345678 "
1057 "12345M 00:00:00 01 Mmm Yyyy");
1059 time = pretty_time(&item->mtime);
1061 buf = g_strdup_printf("%s %s %-8.8s %-8.8s %s %s",
1062 item->flags & ITEM_FLAG_APPDIR? "App " :
1063 S_ISDIR(m) ? "Dir " :
1064 S_ISCHR(m) ? "Char" :
1065 S_ISBLK(m) ? "Blck" :
1066 S_ISLNK(m) ? "Link" :
1067 S_ISSOCK(m) ? "Sock" :
1068 S_ISFIFO(m) ? "Pipe" : "File",
1069 pretty_permissions(m),
1070 user_name(item->uid),
1071 group_name(item->gid),
1072 format_size_aligned(item->size),
1073 time);
1074 g_free(time);
1076 else if (filer_window->details_type == DETAILS_TYPE)
1078 MIME_type *type = item->mime_type;
1080 if (!scanned)
1081 return g_strdup("(application/octet-stream)");
1083 buf = g_strdup_printf("(%s/%s)",
1084 type->media_type, type->subtype);
1086 else if (filer_window->details_type == DETAILS_TIMES)
1088 guchar *ctime, *mtime, *atime;
1090 ctime = pretty_time(&item->ctime);
1091 mtime = pretty_time(&item->mtime);
1092 atime = pretty_time(&item->atime);
1094 buf = g_strdup_printf("a[%s] c[%s] m[%s]", atime, ctime, mtime);
1095 g_free(ctime);
1096 g_free(mtime);
1097 g_free(atime);
1099 else if (filer_window->details_type == DETAILS_PERMISSIONS)
1101 if (!scanned)
1102 return g_strdup("---,---,---/--- 12345678 12345678");
1104 buf = g_strdup_printf("%s %-8.8s %-8.8s",
1105 pretty_permissions(m),
1106 user_name(item->uid),
1107 group_name(item->gid));
1109 else
1111 if (!scanned)
1113 if (filer_window->display_style == SMALL_ICONS)
1114 return g_strdup("12345M");
1115 else
1116 return g_strdup("12345 bytes");
1119 if (item->base_type != TYPE_DIRECTORY)
1121 if (filer_window->display_style == SMALL_ICONS)
1122 buf = g_strdup(format_size_aligned(item->size));
1123 else
1124 buf = g_strdup(format_size(item->size));
1126 else
1127 buf = g_strdup("-");
1130 return buf;
1133 static void draw_item(GtkWidget *widget,
1134 CollectionItem *colitem,
1135 GdkRectangle *area,
1136 FilerWindow *filer_window)
1138 DirItem *item = (DirItem *) colitem->data;
1139 gboolean selected = colitem->selected;
1140 Template template;
1141 ViewData *view = (ViewData *) colitem->view_data;
1143 g_return_if_fail(view != NULL);
1145 fill_template(area, colitem, filer_window, &template);
1147 /* Set up GC for coloured file types */
1148 if (!type_gc)
1149 type_gc = gdk_gc_new(widget->window);
1151 gdk_gc_set_foreground(type_gc, type_get_colour(item,
1152 &widget->style->fg[GTK_STATE_NORMAL]));
1154 if (template.icon.width <= SMALL_WIDTH &&
1155 template.icon.height <= SMALL_HEIGHT)
1157 draw_small_icon(widget, &template.icon,
1158 item, view->image, selected);
1160 else if (template.icon.width <= ICON_WIDTH &&
1161 template.icon.height <= ICON_HEIGHT)
1163 draw_large_icon(widget, &template.icon,
1164 item, view->image, selected);
1166 else
1168 draw_huge_icon(widget, &template.icon,
1169 item, view->image, selected);
1172 draw_string(widget, view->layout,
1173 &template.leafname,
1174 view->name_width,
1175 filer_window->selection_state,
1176 selected, TRUE);
1177 if (view->details)
1178 draw_string(widget, view->details,
1179 &template.details,
1180 template.details.width,
1181 filer_window->selection_state,
1182 selected, TRUE);
1185 /* Note: Call shrink_grid after this */
1186 static void display_details_set(FilerWindow *filer_window, DetailsType details)
1188 if (filer_window->details_type == details)
1189 return;
1190 filer_window->details_type = details;
1191 display_update_views(filer_window);
1193 gtk_widget_queue_draw(GTK_WIDGET(filer_window->collection));
1196 /* Note: Call shrink_grid after this */
1197 static void display_style_set(FilerWindow *filer_window, DisplayStyle style)
1199 if (filer_window->display_style == style)
1200 return;
1202 filer_window->display_style = style;
1204 display_update_views(filer_window);
1206 collection_set_functions(filer_window->collection,
1207 (CollectionDrawFunc) draw_item,
1208 (CollectionTestFunc) test_point,
1209 filer_window);
1212 void display_update_view(FilerWindow *filer_window,
1213 DirItem *item,
1214 ViewData *view,
1215 gboolean update_name_layout)
1217 DisplayStyle style = filer_window->display_style;
1218 int w, h;
1219 int wrap_width = -1;
1220 char *str;
1221 static PangoFontDescription *monospace = NULL;
1223 if (!monospace)
1224 monospace = pango_font_description_from_string("monospace");
1226 if (view->details)
1228 g_object_unref(G_OBJECT(view->details));
1229 view->details = NULL;
1232 str = details(filer_window, item);
1233 if (str)
1235 PangoAttrList *list;
1236 int perm_offset = -1;
1238 view->details = gtk_widget_create_pango_layout(
1239 filer_window->window, str);
1240 g_free(str);
1242 pango_layout_set_font_description(view->details, monospace);
1243 pango_layout_get_size(view->details, &w, &h);
1244 view->details_width = w / PANGO_SCALE;
1245 view->details_height = h / PANGO_SCALE;
1247 if (filer_window->details_type == DETAILS_SUMMARY)
1248 perm_offset = 5;
1249 else if (filer_window->details_type == DETAILS_PERMISSIONS)
1250 perm_offset = 0;
1251 if (perm_offset > -1)
1253 PangoAttribute *attr;
1255 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1257 perm_offset += 4 * applicable(item->uid, item->gid);
1258 attr->start_index = perm_offset;
1259 attr->end_index = perm_offset + 3;
1261 list = pango_attr_list_new();
1262 pango_attr_list_insert(list, attr);
1263 pango_layout_set_attributes(view->details, list);
1267 if (view->image)
1269 g_object_unref(view->image);
1270 view->image = NULL;
1273 if (filer_window->show_thumbs && item->base_type == TYPE_FILE &&
1274 strcmp(item->mime_type->media_type, "image") == 0)
1276 gchar *path;
1278 path = make_path(filer_window->real_path, item->leafname)->str;
1280 view->image = g_fscache_lookup_full(pixmap_cache, path,
1281 FSCACHE_LOOKUP_ONLY_NEW, NULL);
1284 if (!view->image)
1286 view->image = item->image;
1287 if (view->image)
1288 g_object_ref(view->image);
1291 if (view->layout && update_name_layout)
1293 g_object_unref(G_OBJECT(view->layout));
1294 view->layout = NULL;
1297 if (view->layout)
1299 /* Do nothing */
1301 else if (g_utf8_validate(item->leafname, -1, NULL))
1303 view->layout = gtk_widget_create_pango_layout(
1304 filer_window->window, item->leafname);
1306 else
1308 PangoAttrList *list;
1309 PangoAttribute *attr;
1310 gchar *utf8;
1312 utf8 = to_utf8(item->leafname);
1313 view->layout = gtk_widget_create_pango_layout(
1314 filer_window->window, utf8);
1315 g_free(utf8);
1317 attr = pango_attr_foreground_new(0xffff, 0, 0);
1318 attr->start_index = 0;
1319 attr->end_index = 1000;
1320 list = pango_attr_list_new();
1321 pango_attr_list_insert(list, attr);
1322 pango_layout_set_attributes(view->layout, list);
1325 if (filer_window->details_type == DETAILS_NONE)
1327 if (style == HUGE_ICONS)
1328 wrap_width = HUGE_WRAP * PANGO_SCALE;
1329 else if (style == LARGE_ICONS)
1330 wrap_width = o_large_width.int_value * PANGO_SCALE;
1333 if (wrap_width != -1)
1334 pango_layout_set_width(view->layout, wrap_width);
1336 pango_layout_get_size(view->layout, &w, &h);
1337 view->name_width = w / PANGO_SCALE;
1338 view->name_height = h / PANGO_SCALE;
1341 /* 'box' renders a background box if the string is also selected */
1342 static void draw_string(GtkWidget *widget,
1343 PangoLayout *layout,
1344 GdkRectangle *area, /* Area available on screen */
1345 int width, /* Width of the full string */
1346 GtkStateType selection_state,
1347 gboolean selected,
1348 gboolean box)
1350 GdkGC *gc = selected
1351 ? widget->style->fg_gc[selection_state]
1352 : type_gc;
1354 if (selected && box)
1355 gtk_paint_flat_box(widget->style, widget->window,
1356 selection_state, GTK_SHADOW_NONE,
1357 NULL, widget, "text",
1358 area->x, area->y,
1359 MIN(width, area->width),
1360 area->height);
1362 if (width > area->width)
1364 gdk_gc_set_clip_origin(gc, 0, 0);
1365 gdk_gc_set_clip_rectangle(gc, area);
1368 gdk_draw_layout(widget->window, gc, area->x, area->y, layout);
1370 if (width > area->width)
1372 static GdkGC *red_gc = NULL;
1374 if (!red_gc)
1376 gboolean success;
1377 GdkColor red = {0, 0xffff, 0, 0};
1379 red_gc = gdk_gc_new(widget->window);
1380 gdk_colormap_alloc_colors(
1381 gtk_widget_get_colormap(widget),
1382 &red, 1, FALSE, TRUE, &success);
1383 gdk_gc_set_foreground(red_gc, &red);
1385 gdk_draw_rectangle(widget->window, red_gc, TRUE,
1386 area->x + area->width - 1, area->y,
1387 1, area->height);
1388 gdk_gc_set_clip_rectangle(gc, NULL);