r4567: Get ATK headers through Zero Install.
[rox-filer/translations.git] / ROX-Filer / src / view_details.c
blob0ed71c38a3266abf47ea822eb77458c669eba89b
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* view_details.c - display a list of files in a TreeView */
22 #include "config.h"
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
27 #include "global.h"
29 #include "view_iface.h"
30 #include "view_details.h"
31 #include "dir.h"
32 #include "diritem.h"
33 #include "support.h"
34 #include "type.h"
35 #include "filer.h"
36 #include "display.h"
37 #include "pixmaps.h"
38 #include "dnd.h"
39 #include "bind.h"
40 #include "gui_support.h"
41 #include "menu.h"
42 #include "options.h"
43 #include "cell_icon.h"
45 /* These are the column numbers in the ListStore */
46 #define COL_LEAF 0
47 #define COL_TYPE 1
48 #define COL_PERM 2
49 #define COL_OWNER 3
50 #define COL_GROUP 4
51 #define COL_SIZE 5
52 #define COL_MTIME 6
53 #define COL_ITEM 7
54 #define COL_COLOUR 8
55 #define COL_WEIGHT 9
56 #define COL_VIEW_ITEM 10
57 #define N_COLUMNS 11
59 static gpointer parent_class = NULL;
61 struct _ViewDetailsClass {
62 GtkTreeViewClass parent;
65 /* Static prototypes */
66 static void view_details_finialize(GObject *object);
67 static void view_details_class_init(gpointer gclass, gpointer data);
68 static void view_details_init(GTypeInstance *object, gpointer gclass);
70 static void view_details_iface_init(gpointer giface, gpointer iface_data);
72 static void view_details_sort(ViewIface *view);
73 static void view_details_style_changed(ViewIface *view, int flags);
74 static void view_details_add_items(ViewIface *view, GPtrArray *items);
75 static void view_details_update_items(ViewIface *view, GPtrArray *items);
76 static void view_details_delete_if(ViewIface *view,
77 gboolean (*test)(gpointer item, gpointer data),
78 gpointer data);
79 static void view_details_clear(ViewIface *view);
80 static void view_details_select_all(ViewIface *view);
81 static void view_details_clear_selection(ViewIface *view);
82 static int view_details_count_items(ViewIface *view);
83 static int view_details_count_selected(ViewIface *view);
84 static void view_details_show_cursor(ViewIface *view);
85 static void view_details_get_iter(ViewIface *view,
86 ViewIter *iter, IterFlags flags);
87 static void view_details_get_iter_at_point(ViewIface *view, ViewIter *iter,
88 GdkWindow *src, int x, int y);
89 static void view_details_cursor_to_iter(ViewIface *view, ViewIter *iter);
90 static void view_details_set_selected(ViewIface *view,
91 ViewIter *iter,
92 gboolean selected);
93 static gboolean view_details_get_selected(ViewIface *view, ViewIter *iter);
94 static void view_details_select_only(ViewIface *view, ViewIter *iter);
95 static void view_details_set_frozen(ViewIface *view, gboolean frozen);
96 static void view_details_wink_item(ViewIface *view, ViewIter *iter);
97 static void view_details_autosize(ViewIface *view);
98 static gboolean view_details_cursor_visible(ViewIface *view);
99 static void view_details_set_base(ViewIface *view, ViewIter *iter);
100 static void view_details_start_lasso_box(ViewIface *view,
101 GdkEventButton *event);
102 static void view_details_extend_tip(ViewIface *view,
103 ViewIter *iter, GString *tip);
104 static gboolean view_details_auto_scroll_callback(ViewIface *view);
106 static DirItem *iter_peek(ViewIter *iter);
107 static DirItem *iter_prev(ViewIter *iter);
108 static DirItem *iter_next(ViewIter *iter);
109 static void make_iter(ViewDetails *view_details, ViewIter *iter,
110 IterFlags flags);
111 static void make_item_iter(ViewDetails *view_details, ViewIter *iter, int i);
112 static void view_details_tree_model_init(GtkTreeModelIface *iface);
113 static gboolean details_get_sort_column_id(GtkTreeSortable *sortable,
114 gint *sort_column_id,
115 GtkSortType *order);
116 static void details_set_sort_column_id(GtkTreeSortable *sortable,
117 gint sort_column_id,
118 GtkSortType order);
119 static void details_set_sort_func(GtkTreeSortable *sortable,
120 gint sort_column_id,
121 GtkTreeIterCompareFunc func,
122 gpointer data,
123 GtkDestroyNotify destroy);
124 static void details_set_default_sort_func(GtkTreeSortable *sortable,
125 GtkTreeIterCompareFunc func,
126 gpointer data,
127 GtkDestroyNotify destroy);
128 static gboolean details_has_default_sort_func(GtkTreeSortable *sortable);
129 static void view_details_sortable_init(GtkTreeSortableIface *iface);
130 static void set_selected(ViewDetails *view_details, int i, gboolean selected);
131 static gboolean get_selected(ViewDetails *view_details, int i);
132 static void free_view_item(ViewItem *view_item);
133 static void details_update_header_visibility(ViewDetails *view_details);
134 static void set_lasso(ViewDetails *view_details, int x, int y);
135 static void cancel_wink(ViewDetails *view_details);
138 /****************************************************************
139 * EXTERNAL INTERFACE *
140 ****************************************************************/
142 GtkWidget *view_details_new(FilerWindow *filer_window)
144 ViewDetails *view_details;
146 view_details = g_object_new(view_details_get_type(), NULL);
147 view_details->filer_window = filer_window;
149 gtk_range_set_adjustment(GTK_RANGE(filer_window->scrollbar),
150 gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(view_details)));
152 if (filer_window->sort_type != -1)
153 view_details_sort((ViewIface *) view_details);
155 details_update_header_visibility(view_details);
157 return GTK_WIDGET(view_details);
160 GType view_details_get_type(void)
162 static GType type = 0;
164 if (!type)
166 static const GTypeInfo info =
168 sizeof (ViewDetailsClass),
169 NULL, /* base_init */
170 NULL, /* base_finalise */
171 view_details_class_init,
172 NULL, /* class_finalise */
173 NULL, /* class_data */
174 sizeof(ViewDetails),
175 0, /* n_preallocs */
176 view_details_init
178 static const GInterfaceInfo view_iface_info = {
179 view_details_iface_init,
180 NULL, NULL
182 static const GInterfaceInfo tree_model_info = {
183 (GInterfaceInitFunc) view_details_tree_model_init,
184 NULL, NULL
186 static const GInterfaceInfo sortable_info = {
187 (GInterfaceInitFunc) view_details_sortable_init,
188 NULL, NULL
192 type = g_type_register_static(gtk_tree_view_get_type(),
193 "ViewDetails", &info, 0);
195 g_type_add_interface_static(type, VIEW_TYPE_IFACE,
196 &view_iface_info);
197 g_type_add_interface_static(type, GTK_TYPE_TREE_MODEL,
198 &tree_model_info);
199 g_type_add_interface_static(type, GTK_TYPE_TREE_SORTABLE,
200 &sortable_info);
203 return type;
206 /****************************************************************
207 * INTERNAL FUNCTIONS *
208 ****************************************************************/
210 /* Update the visibility of the list headers */
211 static void details_update_header_visibility(ViewDetails *view_details)
213 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view_details),
214 o_display_show_headers.int_value);
217 /* Fulfill the GtkTreeModel requirements */
218 static guint details_get_flags(GtkTreeModel *tree_model)
220 return GTK_TREE_MODEL_LIST_ONLY;
223 static gint details_get_n_columns(GtkTreeModel *tree_model)
225 return N_COLUMNS;
228 static GType details_get_column_type(GtkTreeModel *tree_model, gint index)
230 g_return_val_if_fail(index < N_COLUMNS && index >= 0, G_TYPE_INVALID);
232 if (index == COL_COLOUR)
233 return GDK_TYPE_COLOR;
234 else if (index == COL_ITEM || index == COL_VIEW_ITEM)
235 return G_TYPE_POINTER;
236 else if (index == COL_WEIGHT)
237 return G_TYPE_INT;
238 return G_TYPE_STRING;
241 static gboolean details_get_iter(GtkTreeModel *tree_model,
242 GtkTreeIter *iter,
243 GtkTreePath *path)
245 ViewDetails *view_details = (ViewDetails *) tree_model;
246 gint i;
248 g_return_val_if_fail(gtk_tree_path_get_depth (path) > 0, FALSE);
250 i = gtk_tree_path_get_indices(path)[0];
252 if (i >= view_details->items->len)
253 return FALSE;
255 iter->user_data = GINT_TO_POINTER(i);
257 return TRUE;
260 static GtkTreePath *details_get_path(GtkTreeModel *tree_model,
261 GtkTreeIter *iter)
263 GtkTreePath *retval;
265 retval = gtk_tree_path_new();
266 gtk_tree_path_append_index(retval, GPOINTER_TO_INT(iter->user_data));
268 return retval;
271 static void details_get_value(GtkTreeModel *tree_model,
272 GtkTreeIter *iter,
273 gint column,
274 GValue *value)
276 ViewDetails *view_details = (ViewDetails *) tree_model;
277 gint i;
278 GPtrArray *items = view_details->items;
279 ViewItem *view_item;
280 DirItem *item;
281 mode_t m;
283 g_return_if_fail(column >= 0 && column < N_COLUMNS);
285 i = GPOINTER_TO_INT(iter->user_data);
286 g_return_if_fail(i >= 0 && i < items->len);
287 view_item = (ViewItem *) items->pdata[i];
288 item = view_item->item;
290 if (column == COL_LEAF)
292 g_value_init(value, G_TYPE_STRING);
293 g_value_set_string(value,
294 view_item->utf8_name ? view_item->utf8_name
295 : item->leafname);
296 return;
298 else if (column == COL_VIEW_ITEM)
300 g_value_init(value, G_TYPE_POINTER);
301 g_value_set_pointer(value, view_item);
302 return;
304 else if (column == COL_ITEM)
306 g_value_init(value, G_TYPE_POINTER);
307 g_value_set_pointer(value, item);
308 return;
311 if (item->base_type == TYPE_UNKNOWN)
313 GType type;
314 type = details_get_column_type(tree_model, column);
315 g_value_init(value, type);
316 if (type == G_TYPE_STRING)
317 g_value_set_string(value, "");
318 else if (type == GDK_TYPE_COLOR)
319 g_value_set_boxed(value, NULL);
320 else if (type == G_TYPE_INT)
321 g_value_set_int(value, PANGO_WEIGHT_NORMAL);
322 else
323 g_value_set_object(value, NULL);
325 return;
327 m = item->mode;
329 switch (column)
331 case COL_LEAF:
332 g_value_init(value, G_TYPE_STRING);
333 g_value_set_string(value, item->leafname);
334 break;
335 case COL_COLOUR:
336 g_value_init(value, GDK_TYPE_COLOR);
337 if (view_item->utf8_name)
339 GdkColor red;
340 red.red = 0xffff;
341 red.green = 0;
342 red.blue = 0;
343 g_value_set_boxed(value, &red);
345 else
346 g_value_set_boxed(value,
347 type_get_colour(item, NULL));
348 break;
349 case COL_OWNER:
350 g_value_init(value, G_TYPE_STRING);
351 g_value_set_string(value, user_name(item->uid));
352 break;
353 case COL_GROUP:
354 g_value_init(value, G_TYPE_STRING);
355 g_value_set_string(value, group_name(item->gid));
356 break;
357 case COL_MTIME:
359 gchar *time;
360 time = pretty_time(&item->mtime);
361 g_value_init(value, G_TYPE_STRING);
362 g_value_set_string(value, time);
363 g_free(time);
364 break;
366 case COL_PERM:
367 g_value_init(value, G_TYPE_STRING);
368 g_value_set_string(value, pretty_permissions(m));
369 break;
370 case COL_SIZE:
371 g_value_init(value, G_TYPE_STRING);
372 if (item->base_type != TYPE_DIRECTORY)
373 g_value_set_string(value,
374 format_size(item->size));
375 break;
376 case COL_TYPE:
377 g_value_init(value, G_TYPE_STRING);
378 if(o_display_show_full_type.int_value)
379 g_value_set_string(value,
380 item->flags & ITEM_FLAG_APPDIR? "Application" :
381 mime_type_comment(item->mime_type));
382 else
383 g_value_set_string(value,
384 item->flags & ITEM_FLAG_APPDIR? "App" :
385 S_ISDIR(m) ? "Dir" :
386 S_ISCHR(m) ? "Char" :
387 S_ISBLK(m) ? "Blck" :
388 S_ISLNK(m) ? "Link" :
389 S_ISSOCK(m) ? "Sock" :
390 S_ISFIFO(m) ? "Pipe" :
391 S_ISDOOR(m) ? "Door" :
392 "File");
394 break;
395 case COL_WEIGHT:
396 g_value_init(value, G_TYPE_INT);
397 if (item->flags & ITEM_FLAG_RECENT)
398 g_value_set_int(value, PANGO_WEIGHT_BOLD);
399 else
400 g_value_set_int(value, PANGO_WEIGHT_NORMAL);
401 break;
402 default:
403 g_value_init(value, G_TYPE_STRING);
404 g_value_set_string(value, "Hello");
405 break;
409 static gboolean details_iter_next(GtkTreeModel *tree_model, GtkTreeIter *iter)
411 ViewDetails *view_details = (ViewDetails *) tree_model;
412 int i;
414 i = GPOINTER_TO_INT(iter->user_data) + 1;
415 iter->user_data = GINT_TO_POINTER(i);
417 return i < view_details->items->len;
420 static gboolean details_iter_children(GtkTreeModel *tree_model,
421 GtkTreeIter *iter,
422 GtkTreeIter *parent)
424 ViewDetails *view_details = (ViewDetails *) tree_model;
426 /* this is a list, nodes have no children */
427 if (parent)
428 return FALSE;
430 /* but if parent == NULL we return the list itself as children of the
431 * "root"
434 if (view_details->items->len)
436 iter->user_data = GINT_TO_POINTER(0);
437 return TRUE;
439 else
440 return FALSE;
443 static gboolean details_iter_has_child(GtkTreeModel *tree_model,
444 GtkTreeIter *iter)
446 return FALSE;
449 static gint details_iter_n_children(GtkTreeModel *tree_model, GtkTreeIter *iter)
451 ViewDetails *view_details = (ViewDetails *) tree_model;
453 if (iter == NULL)
454 return view_details->items->len;
456 return 0;
459 static gboolean details_iter_nth_child(GtkTreeModel *tree_model,
460 GtkTreeIter *iter,
461 GtkTreeIter *parent,
462 gint n)
464 ViewDetails *view_details = (ViewDetails *) tree_model;
466 if (parent)
467 return FALSE;
469 if (n >= 0 && n < view_details->items->len)
471 iter->user_data = GINT_TO_POINTER(n);
472 return TRUE;
474 else
475 return FALSE;
478 static gboolean details_iter_parent(GtkTreeModel *tree_model,
479 GtkTreeIter *iter,
480 GtkTreeIter *child)
482 return FALSE;
485 /* A ViewDetails is both a GtkTreeView and a GtkTreeModel.
486 * The following functions implement the model interface...
489 static void view_details_tree_model_init(GtkTreeModelIface *iface)
491 iface->get_flags = details_get_flags;
492 iface->get_n_columns = details_get_n_columns;
493 iface->get_column_type = details_get_column_type;
494 iface->get_iter = details_get_iter;
495 iface->get_path = details_get_path;
496 iface->get_value = details_get_value;
497 iface->iter_next = details_iter_next;
498 iface->iter_children = details_iter_children;
499 iface->iter_has_child = details_iter_has_child;
500 iface->iter_n_children = details_iter_n_children;
501 iface->iter_nth_child = details_iter_nth_child;
502 iface->iter_parent = details_iter_parent;
505 static void view_details_sortable_init(GtkTreeSortableIface *iface)
507 iface->get_sort_column_id = details_get_sort_column_id;
508 iface->set_sort_column_id = details_set_sort_column_id;
509 iface->set_sort_func = details_set_sort_func;
510 iface->set_default_sort_func = details_set_default_sort_func;
511 iface->has_default_sort_func = details_has_default_sort_func;
514 static gboolean details_get_sort_column_id(GtkTreeSortable *sortable,
515 gint *sort_column_id,
516 GtkSortType *order)
518 ViewDetails *view_details = (ViewDetails *) sortable;
519 FilerWindow *filer_window = view_details->filer_window;
520 int col;
522 if (!filer_window)
523 return FALSE; /* Not yet initialised */
525 switch (filer_window->sort_type)
527 case SORT_NAME: col = COL_LEAF; break;
528 case SORT_TYPE: col = COL_TYPE; break;
529 case SORT_DATE: col = COL_MTIME; break;
530 case SORT_SIZE: col = COL_SIZE; break;
531 case SORT_OWNER: col = COL_OWNER; break;
532 case SORT_GROUP: col = COL_GROUP; break;
533 default:
534 g_warning("details_get_sort_column_id(): error!");
535 return FALSE;
537 if (sort_column_id)
538 *sort_column_id = col;
539 if (order)
540 *order = filer_window->sort_order;
541 return TRUE;
544 static void details_set_sort_column_id(GtkTreeSortable *sortable,
545 gint sort_column_id,
546 GtkSortType order)
548 ViewDetails *view_details = (ViewDetails *) sortable;
549 FilerWindow *filer_window = view_details->filer_window;
551 if (!filer_window)
552 return; /* Not yet initialised */
554 switch (sort_column_id)
556 case COL_LEAF:
557 display_set_sort_type(filer_window, SORT_NAME, order);
558 break;
559 case COL_SIZE:
560 display_set_sort_type(filer_window, SORT_SIZE, order);
561 break;
562 case COL_MTIME:
563 display_set_sort_type(filer_window, SORT_DATE, order);
564 break;
565 case COL_TYPE:
566 display_set_sort_type(filer_window, SORT_TYPE, order);
567 break;
568 case COL_OWNER:
569 display_set_sort_type(filer_window, SORT_OWNER, order);
570 break;
571 case COL_GROUP:
572 display_set_sort_type(filer_window, SORT_GROUP, order);
573 break;
574 default:
575 g_assert_not_reached();
579 static void details_set_sort_func(GtkTreeSortable *sortable,
580 gint sort_column_id,
581 GtkTreeIterCompareFunc func,
582 gpointer data,
583 GtkDestroyNotify destroy)
585 g_assert_not_reached();
588 static void details_set_default_sort_func(GtkTreeSortable *sortable,
589 GtkTreeIterCompareFunc func,
590 gpointer data,
591 GtkDestroyNotify destroy)
593 g_assert_not_reached();
596 static gboolean details_has_default_sort_func(GtkTreeSortable *sortable)
598 return FALSE;
602 /* End of model implementation */
604 static gboolean is_selected(ViewDetails *view_details, int i)
606 ViewIter iter;
607 iter.i = i;
608 return view_details_get_selected((ViewIface *) view_details, &iter);
611 static gboolean view_details_scroll(GtkWidget *widget, GdkEventScroll *event)
613 GtkTreeView *tree = (GtkTreeView *) widget;
614 GtkTreePath *path = NULL;
616 if (!gtk_tree_view_get_path_at_pos(tree, 0, 1, &path, NULL, NULL, NULL))
617 return TRUE; /* Empty? */
619 if (event->direction == GDK_SCROLL_UP)
620 gtk_tree_path_prev(path);
621 else if (event->direction == GDK_SCROLL_DOWN)
622 gtk_tree_path_next(path);
623 else
624 goto out;
626 gtk_tree_view_scroll_to_cell(tree, path, NULL, TRUE, 0, 0);
627 out:
628 gtk_tree_path_free(path);
629 return TRUE;
632 static gint view_details_key_press(GtkWidget *widget, GdkEventKey *event)
634 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
635 event->keyval == GDK_Prior || event->keyval == GDK_Next ||
636 event->keyval == GDK_Home || event->keyval == GDK_End)
637 return GTK_WIDGET_CLASS(parent_class)->key_press_event(widget,
638 event);
639 return FALSE;
642 static gboolean view_details_button_press(GtkWidget *widget,
643 GdkEventButton *bev)
645 FilerWindow *filer_window = ((ViewDetails *) widget)->filer_window;
646 GtkTreeView *tree = (GtkTreeView *) widget;
648 if (bev->window != gtk_tree_view_get_bin_window(tree))
649 return GTK_WIDGET_CLASS(parent_class)->button_press_event(
650 widget, bev);
652 if (dnd_motion_press(widget, bev))
653 filer_perform_action(filer_window, bev);
655 return TRUE;
658 static int get_lasso_index(ViewDetails *view_details, int y)
660 GtkTreeViewColumn *column = NULL;
661 GtkTreePath *path = NULL;
662 GtkTreeView *tree = (GtkTreeView *) view_details;
663 gint cell_y;
664 int i;
666 if (y < 0)
667 y = 0; /* gtk_tree_view_get_path_at_pos can't handle negatives */
669 if (gtk_tree_view_get_path_at_pos(tree, 4, y, &path,
670 &column, NULL, &cell_y))
672 GdkRectangle rect;
673 i = gtk_tree_path_get_indices(path)[0];
674 gtk_tree_view_get_cell_area(tree, path, column, &rect);
675 gtk_tree_path_free(path);
677 if (2 * cell_y > rect.height)
678 i += 1;
680 else
681 i = view_details->items->len;
683 return i;
686 static gboolean select_lasso_cb(ViewIter *iter, gpointer data)
688 int start = ((int *) data)[0];
689 int end = ((int *) data)[1];
690 GdkFunction fn = ((int *) data)[2];
691 ViewDetails *view_details = (ViewDetails *) iter->view;
692 GtkTreeIter titer;
694 titer.user_data = GINT_TO_POINTER(iter->i);
696 if (iter->i < start || iter->i >= end)
697 return gtk_tree_selection_iter_is_selected(
698 view_details->selection, &titer);
700 if (fn == GDK_SET)
701 return TRUE;
703 return !gtk_tree_selection_iter_is_selected(view_details->selection,
704 &titer);
707 static void select_lasso(ViewDetails *view_details, GdkFunction fn)
709 GtkAdjustment *adj;
710 int range[3];
712 adj = gtk_tree_view_get_vadjustment((GtkTreeView *) view_details);
714 range[0] = view_details->lasso_start_index;
715 range[1] = get_lasso_index(view_details,
716 view_details->drag_box_y[1] - adj->value);
717 range[2] = fn;
719 if (range[0] == range[1])
720 return;
721 if (range[0] > range[1])
723 int tmp = range[0];
724 range[0] = range[1];
725 range[1] = tmp;
728 view_select_if((ViewIface *) view_details, select_lasso_cb, &range);
731 static gboolean view_details_button_release(GtkWidget *widget,
732 GdkEventButton *bev)
734 ViewDetails *view_details = (ViewDetails *) widget;
735 FilerWindow *filer_window = view_details->filer_window;
736 GtkTreeView *tree = (GtkTreeView *) widget;
738 if (bev->window != gtk_tree_view_get_bin_window(tree))
739 return GTK_WIDGET_CLASS(parent_class)->button_release_event(
740 widget, bev);
742 if (!dnd_motion_release(bev))
743 filer_perform_action(filer_window, bev);
745 if (motion_buttons_pressed == 0 && view_details->lasso_box)
747 select_lasso(view_details,
748 bev->button == 1 ? GDK_SET : GDK_INVERT);
749 filer_set_autoscroll(filer_window, FALSE);
750 set_lasso(view_details,
751 view_details->drag_box_x[0],
752 view_details->drag_box_y[0]);
753 view_details->lasso_box = FALSE;
756 return TRUE;
759 static gint view_details_motion_notify(GtkWidget *widget, GdkEventMotion *event)
761 ViewDetails *view_details = (ViewDetails *) widget;
762 GtkTreeView *tree = (GtkTreeView *) widget;
764 if (event->window != gtk_tree_view_get_bin_window(tree))
765 return GTK_WIDGET_CLASS(parent_class)->motion_notify_event(
766 widget, event);
768 if (view_details->lasso_box)
770 GtkAdjustment *adj;
771 adj = gtk_tree_view_get_vadjustment(tree);
772 set_lasso(view_details, event->x, event->y + adj->value);
773 return TRUE;
776 return filer_motion_notify(view_details->filer_window, event);
779 static gboolean view_details_expose(GtkWidget *widget, GdkEventExpose *event)
781 GtkTreeView *tree = (GtkTreeView *) widget;
782 GtkTreePath *path = NULL;
783 GdkRectangle focus_rectangle;
784 ViewDetails *view_details = (ViewDetails *) widget;
785 gboolean had_cursor;
787 had_cursor = (GTK_WIDGET_FLAGS(widget) & GTK_HAS_FOCUS) != 0;
789 if (view_details->filer_window->selection_state == GTK_STATE_SELECTED)
790 GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_FOCUS);
791 else
792 GTK_WIDGET_UNSET_FLAGS(widget, GTK_HAS_FOCUS);
793 GTK_WIDGET_CLASS(parent_class)->expose_event(widget, event);
794 if (had_cursor)
795 GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_FOCUS);
797 if (event->window != gtk_tree_view_get_bin_window(tree))
798 return FALSE; /* Not the main area */
800 if (view_details->lasso_box)
802 int x, y, width, height;
803 GtkAdjustment *adj;
805 adj = gtk_tree_view_get_vadjustment(tree);
806 x = MIN(view_details->drag_box_x[0],
807 view_details->drag_box_x[1]);
808 y = MIN(view_details->drag_box_y[0],
809 view_details->drag_box_y[1]);
810 width = abs(view_details->drag_box_x[1] -
811 view_details->drag_box_x[0]);
812 height = abs(view_details->drag_box_y[1] -
813 view_details->drag_box_y[0]);
814 y -= adj->value;
816 if (width && height)
817 gdk_draw_rectangle(event->window,
818 widget->style->fg_gc[GTK_STATE_NORMAL],
819 FALSE, x, y, width - 1, height - 1);
822 if (view_details->wink_item != -1 && view_details->wink_step & 1)
824 GtkTreePath *wink_path;
825 GdkRectangle wink_area;
827 wink_path = gtk_tree_path_new();
828 gtk_tree_path_append_index(wink_path, view_details->wink_item);
829 gtk_tree_view_get_background_area(tree, wink_path,
830 NULL, &wink_area);
831 gtk_tree_path_free(wink_path);
833 if (wink_area.height)
835 /* (visible) */
836 wink_area.width = widget->allocation.width;
837 gdk_draw_rectangle(event->window,
838 widget->style->fg_gc[GTK_STATE_NORMAL],
839 FALSE,
840 wink_area.x + 1,
841 wink_area.y + 1,
842 wink_area.width - 3,
843 wink_area.height - 3);
847 gtk_tree_view_get_cursor(tree, &path, NULL);
848 if (!path)
849 return FALSE; /* No cursor */
850 gtk_tree_view_get_background_area(tree, path, NULL, &focus_rectangle);
851 gtk_tree_path_free(path);
853 if (!focus_rectangle.height)
854 return FALSE; /* Off screen */
856 focus_rectangle.width = widget->allocation.width;
858 gtk_paint_focus(widget->style,
859 event->window,
860 GTK_STATE_NORMAL,
861 NULL,
862 widget,
863 "treeview",
864 focus_rectangle.x,
865 focus_rectangle.y,
866 focus_rectangle.width,
867 focus_rectangle.height);
869 return FALSE;
872 static void view_details_size_request(GtkWidget *widget,
873 GtkRequisition *requisition)
875 ViewDetails *view_details = (ViewDetails *) widget;
877 (*GTK_WIDGET_CLASS(parent_class)->size_request)(widget, requisition);
879 view_details->desired_size = *requisition;
881 requisition->height = 50;
882 requisition->width = 50;
885 static void view_details_drag_data_received(GtkWidget *widget,
886 GdkDragContext *drag_context,
887 gint x, gint y, GtkSelectionData *data, guint info, guint time)
889 /* Just here to override annoying default handler */
892 static void view_details_destroy(GtkObject *obj)
894 ViewDetails *view_details = VIEW_DETAILS(obj);
896 view_details->filer_window = NULL;
897 cancel_wink(view_details);
900 static void view_details_finialize(GObject *object)
902 ViewDetails *view_details = (ViewDetails *) object;
904 g_ptr_array_free(view_details->items, TRUE);
905 view_details->items = NULL;
907 G_OBJECT_CLASS(parent_class)->finalize(object);
910 static void view_details_class_init(gpointer gclass, gpointer data)
912 GObjectClass *object = (GObjectClass *) gclass;
913 GtkWidgetClass *widget = (GtkWidgetClass *) gclass;
915 parent_class = g_type_class_peek_parent(gclass);
917 object->finalize = view_details_finialize;
918 GTK_OBJECT_CLASS(object)->destroy = view_details_destroy;
920 widget->scroll_event = view_details_scroll;
921 widget->key_press_event = view_details_key_press;
922 widget->button_press_event = view_details_button_press;
923 widget->button_release_event = view_details_button_release;
924 widget->motion_notify_event = view_details_motion_notify;
925 widget->expose_event = view_details_expose;
926 widget->size_request = view_details_size_request;
927 widget->drag_data_received = view_details_drag_data_received;
930 * Add the ViewDetails::mono-font style property.
931 * To use this add something like
933 * style "details" {
934 * ViewDetails::mono-font = "Courier 8"
936 * class "ViewDetails" style "details"
938 * to your ~/.gtkrc-2.0
940 gtk_widget_class_install_style_property(widget,
941 g_param_spec_string("mono-font",
942 _("Mono font"),
943 _("Font for displaying mono-spaced text"),
944 "monospace",
945 G_PARAM_READABLE));
949 static gboolean block_focus(GtkWidget *button, GtkDirectionType dir,
950 ViewDetails *view_details)
952 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
953 return FALSE;
956 static gboolean test_can_change_selection(GtkTreeSelection *sel,
957 GtkTreeModel *model,
958 GtkTreePath *path,
959 gboolean path_currently_selected,
960 gpointer data)
962 ViewDetails *view_details;
964 view_details = VIEW_DETAILS(gtk_tree_selection_get_tree_view(sel));
966 return view_details->can_change_selection != 0;
969 static void selection_changed(GtkTreeSelection *selection,
970 gpointer user_data)
972 ViewDetails *view_details = VIEW_DETAILS(user_data);
974 filer_selection_changed(view_details->filer_window,
975 gtk_get_current_event_time());
979 * Set the font used for a treeview column to that given for the
980 * "mono-font" style property of a widget. This has to be done _after_ the
981 * widget has been realized, because that is when the style information is
982 * set up. This is done by connecting this function to run after the
983 * "realize" signal.
985 static void set_column_mono_font(GtkWidget *widget, GObject *object)
987 const gchar *font_name;
989 gtk_widget_style_get(widget, "mono-font", &font_name, NULL);
990 g_object_set(object, "font", font_name, NULL);
993 #define ADD_TEXT_COLUMN(name, model_column) \
994 cell = gtk_cell_renderer_text_new(); \
995 column = gtk_tree_view_column_new_with_attributes(name, cell, \
996 "text", model_column, \
997 "foreground-gdk", COL_COLOUR, \
998 "weight", COL_WEIGHT, \
999 NULL); \
1000 gtk_tree_view_append_column(treeview, column); \
1001 g_signal_connect(column->button, "grab-focus", \
1002 G_CALLBACK(block_focus), view_details);
1004 static void view_details_init(GTypeInstance *object, gpointer gclass)
1006 GtkTreeView *treeview = (GtkTreeView *) object;
1007 GtkTreeViewColumn *column;
1008 GtkCellRenderer *cell;
1009 GtkTreeSortable *sortable_list;
1010 ViewDetails *view_details = (ViewDetails *) object;
1012 view_details->items = g_ptr_array_new();
1013 view_details->cursor_base = -1;
1014 view_details->wink_item = -1;
1015 view_details->desired_size.width = -1;
1016 view_details->desired_size.height = -1;
1017 view_details->can_change_selection = 0;
1018 view_details->lasso_box = FALSE;
1020 view_details->selection = gtk_tree_view_get_selection(treeview);
1021 gtk_tree_selection_set_mode(view_details->selection,
1022 GTK_SELECTION_MULTIPLE);
1023 gtk_tree_selection_set_select_function(view_details->selection,
1024 test_can_change_selection, view_details, NULL);
1026 /* Sorting */
1027 view_details->sort_fn = NULL;
1028 sortable_list = GTK_TREE_SORTABLE(object);
1030 gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(view_details));
1031 /* Do this after set_model, because that can generate this
1032 * signal...
1034 g_signal_connect(view_details->selection, "changed",
1035 G_CALLBACK(selection_changed), view_details);
1037 /* Icon */
1038 cell = cell_icon_new(view_details);
1039 column = gtk_tree_view_column_new_with_attributes(NULL, cell,
1040 "item", COL_VIEW_ITEM,
1041 NULL);
1042 gtk_tree_view_append_column(treeview, column);
1044 ADD_TEXT_COLUMN(_("_Name"), COL_LEAF);
1045 gtk_tree_view_column_set_sort_column_id(column, COL_LEAF);
1046 gtk_tree_view_column_set_resizable(column, TRUE);
1047 ADD_TEXT_COLUMN(_("_Type"), COL_TYPE);
1048 gtk_tree_view_column_set_sort_column_id(column, COL_TYPE);
1049 gtk_tree_view_column_set_resizable(column, TRUE);
1050 ADD_TEXT_COLUMN(_("_Permissions"), COL_PERM);
1051 g_object_set(G_OBJECT(cell), "font", "monospace", NULL);
1052 g_signal_connect_after(object, "realize",
1053 G_CALLBACK(set_column_mono_font),
1054 G_OBJECT(cell));
1055 ADD_TEXT_COLUMN(_("_Owner"), COL_OWNER);
1056 gtk_tree_view_column_set_sort_column_id(column, COL_OWNER);
1057 ADD_TEXT_COLUMN(_("_Group"), COL_GROUP);
1058 gtk_tree_view_column_set_sort_column_id(column, COL_GROUP);
1059 ADD_TEXT_COLUMN(_("_Size"), COL_SIZE);
1060 g_object_set(G_OBJECT(cell), "xalign", 1.0, "font", "monospace", NULL);
1061 g_signal_connect_after(object, "realize",
1062 G_CALLBACK(set_column_mono_font),
1063 G_OBJECT(cell));
1064 gtk_tree_view_column_set_sort_column_id(column, COL_SIZE);
1065 ADD_TEXT_COLUMN(_("Last _Modified"), COL_MTIME);
1066 gtk_tree_view_column_set_sort_column_id(column, COL_MTIME);
1069 /* Create the handers for the View interface */
1070 static void view_details_iface_init(gpointer giface, gpointer iface_data)
1072 ViewIfaceClass *iface = giface;
1074 g_assert(G_TYPE_FROM_INTERFACE(iface) == VIEW_TYPE_IFACE);
1076 /* override stuff */
1077 iface->sort = view_details_sort;
1078 iface->style_changed = view_details_style_changed;
1079 iface->add_items = view_details_add_items;
1080 iface->update_items = view_details_update_items;
1081 iface->delete_if = view_details_delete_if;
1082 iface->clear = view_details_clear;
1083 iface->select_all = view_details_select_all;
1084 iface->clear_selection = view_details_clear_selection;
1085 iface->count_items = view_details_count_items;
1086 iface->count_selected = view_details_count_selected;
1087 iface->show_cursor = view_details_show_cursor;
1088 iface->get_iter = view_details_get_iter;
1089 iface->get_iter_at_point = view_details_get_iter_at_point;
1090 iface->cursor_to_iter = view_details_cursor_to_iter;
1091 iface->set_selected = view_details_set_selected;
1092 iface->get_selected = view_details_get_selected;
1093 iface->set_frozen = view_details_set_frozen;
1094 iface->select_only = view_details_select_only;
1095 iface->wink_item = view_details_wink_item;
1096 iface->autosize = view_details_autosize;
1097 iface->cursor_visible = view_details_cursor_visible;
1098 iface->set_base = view_details_set_base;
1099 iface->start_lasso_box = view_details_start_lasso_box;
1100 iface->extend_tip = view_details_extend_tip;
1101 iface->auto_scroll_callback = view_details_auto_scroll_callback;
1104 /* Implementations of the View interface. See view_iface.c for comments. */
1106 static void view_details_style_changed(ViewIface *view, int flags)
1108 ViewDetails *view_details = (ViewDetails *) view;
1109 GtkTreeModel *model = (GtkTreeModel *) view;
1110 GtkTreePath *path;
1111 ViewItem **items = (ViewItem **) view_details->items->pdata;
1112 int i;
1113 int n = view_details->items->len;
1115 path = gtk_tree_path_new();
1116 gtk_tree_path_append_index(path, 0);
1118 for (i = 0; i < n; i++)
1120 GtkTreeIter iter;
1121 ViewItem *item = items[i];
1123 iter.user_data = GINT_TO_POINTER(i);
1124 if (item->image)
1126 g_object_unref(G_OBJECT(item->image));
1127 item->image = NULL;
1129 gtk_tree_model_row_changed(model, path, &iter);
1130 gtk_tree_path_next(path);
1133 gtk_tree_path_free(path);
1135 gtk_tree_view_columns_autosize((GtkTreeView *) view);
1137 if (flags & VIEW_UPDATE_HEADERS)
1138 details_update_header_visibility(view_details);
1141 static gint wrap_sort(gconstpointer a, gconstpointer b,
1142 ViewDetails *view_details)
1144 ViewItem *ia = *(ViewItem **) a;
1145 ViewItem *ib = *(ViewItem **) b;
1147 if (view_details->filer_window->sort_order == GTK_SORT_ASCENDING)
1148 return view_details->sort_fn(ia->item, ib->item);
1149 else
1150 return -view_details->sort_fn(ia->item, ib->item);
1153 static void resort(ViewDetails *view_details)
1155 ViewItem **items = (ViewItem **) view_details->items->pdata;
1156 gint i, len = view_details->items->len;
1157 guint *new_order;
1158 GtkTreePath *path;
1159 int wink_item = view_details->wink_item;
1161 if (!len)
1162 return;
1164 for (i = len - 1; i >= 0; i--)
1165 items[i]->old_pos = i;
1167 switch (view_details->filer_window->sort_type)
1169 case SORT_NAME: view_details->sort_fn = sort_by_name; break;
1170 case SORT_TYPE: view_details->sort_fn = sort_by_type; break;
1171 case SORT_DATE: view_details->sort_fn = sort_by_date; break;
1172 case SORT_SIZE: view_details->sort_fn = sort_by_size; break;
1173 case SORT_OWNER: view_details->sort_fn = sort_by_owner; break;
1174 case SORT_GROUP: view_details->sort_fn = sort_by_group; break;
1175 default:
1176 g_assert_not_reached();
1179 g_ptr_array_sort_with_data(view_details->items,
1180 (GCompareDataFunc) wrap_sort,
1181 view_details);
1183 new_order = g_new(guint, len);
1184 for (i = len - 1; i >= 0; i--)
1186 new_order[i] = items[i]->old_pos;
1187 if (wink_item == items[i]->old_pos)
1188 wink_item = i;
1191 view_details->wink_item = wink_item;
1193 path = gtk_tree_path_new();
1194 gtk_tree_model_rows_reordered((GtkTreeModel *) view_details,
1195 path, NULL, new_order);
1196 gtk_tree_path_free(path);
1197 g_free(new_order);
1200 static void view_details_sort(ViewIface *view)
1202 resort((ViewDetails *) view);
1203 gtk_tree_sortable_sort_column_changed((GtkTreeSortable *) view);
1206 static void view_details_add_items(ViewIface *view, GPtrArray *new_items)
1208 ViewDetails *view_details = (ViewDetails *) view;
1209 FilerWindow *filer_window = view_details->filer_window;
1210 GPtrArray *items = view_details->items;
1211 GtkTreeIter iter;
1212 int i;
1213 GtkTreePath *path;
1214 GtkTreeModel *model = (GtkTreeModel *) view;
1216 iter.user_data = GINT_TO_POINTER(items->len);
1217 path = details_get_path(model, &iter);
1219 for (i = 0; i < new_items->len; i++)
1221 DirItem *item = (DirItem *) new_items->pdata[i];
1222 char *leafname = item->leafname;
1223 ViewItem *vitem;
1225 if(!filer_match_filter(filer_window, item))
1226 continue;
1227 if (leafname[0] == '.')
1229 if (leafname[1] == '\0')
1230 continue; /* Never show '.' */
1232 if (leafname[1] == '.' &&
1233 leafname[2] == '\0')
1234 continue; /* Never show '..' */
1237 vitem = g_new(ViewItem, 1);
1238 vitem->item = item;
1239 vitem->image = NULL;
1240 if (!g_utf8_validate(leafname, -1, NULL))
1241 vitem->utf8_name = to_utf8(leafname);
1242 else
1243 vitem->utf8_name = NULL;
1245 g_ptr_array_add(items, vitem);
1247 iter.user_data = GINT_TO_POINTER(items->len - 1);
1248 gtk_tree_model_row_inserted(model, path, &iter);
1249 gtk_tree_path_next(path);
1252 gtk_tree_path_free(path);
1254 resort(view_details);
1257 /* Find an item in the sorted array.
1258 * Returns the item number, or -1 if not found.
1260 static int details_find_item(ViewDetails *view_details, DirItem *item)
1262 ViewItem **items, tmp, *tmpp;
1263 int lower, upper;
1265 g_return_val_if_fail(view_details != NULL, -1);
1266 g_return_val_if_fail(item != NULL, -1);
1268 tmp.item = item;
1269 tmpp = &tmp;
1271 items = (ViewItem **) view_details->items->pdata;
1273 /* If item is here, then: lower <= i < upper */
1274 lower = 0;
1275 upper = view_details->items->len;
1277 while (lower < upper)
1279 int i, cmp;
1281 i = (lower + upper) >> 1;
1283 cmp = wrap_sort(&items[i], &tmpp, view_details);
1284 if (cmp == 0)
1285 return i;
1287 if (cmp > 0)
1288 upper = i;
1289 else
1290 lower = i + 1;
1293 return -1;
1296 static void view_details_update_items(ViewIface *view, GPtrArray *items)
1298 ViewDetails *view_details = (ViewDetails *) view;
1299 FilerWindow *filer_window = view_details->filer_window;
1300 int i;
1301 GtkTreeModel *model = (GtkTreeModel *) view_details;
1303 g_return_if_fail(items->len > 0);
1305 /* The item data has already been modified, so this gives the
1306 * final sort order...
1308 resort(view_details);
1310 for (i = 0; i < items->len; i++)
1312 DirItem *item = (DirItem *) items->pdata[i];
1313 const gchar *leafname = item->leafname;
1314 int j;
1316 if (!filer_match_filter(filer_window, item))
1317 continue;
1319 j = details_find_item(view_details, item);
1321 if (j < 0)
1322 g_warning("Failed to find '%s'\n", leafname);
1323 else
1325 GtkTreePath *path;
1326 GtkTreeIter iter;
1327 ViewItem *view_item = view_details->items->pdata[j];
1328 if (view_item->image)
1330 g_object_unref(G_OBJECT(view_item->image));
1331 view_item->image = NULL;
1333 path = gtk_tree_path_new();
1334 gtk_tree_path_append_index(path, j);
1335 iter.user_data = GINT_TO_POINTER(j);
1336 gtk_tree_model_row_changed(model, path, &iter);
1341 static void view_details_delete_if(ViewIface *view,
1342 gboolean (*test)(gpointer item, gpointer data),
1343 gpointer data)
1345 GtkTreePath *path;
1346 ViewDetails *view_details = (ViewDetails *) view;
1347 int i = 0;
1348 GPtrArray *items = view_details->items;
1349 GtkTreeModel *model = (GtkTreeModel *) view;
1351 path = gtk_tree_path_new();
1353 gtk_tree_path_append_index(path, i);
1355 while (i < items->len)
1357 ViewItem *item = items->pdata[i];
1359 if (test(item->item, data))
1361 free_view_item(items->pdata[i]);
1362 g_ptr_array_remove_index(items, i);
1363 gtk_tree_model_row_deleted(model, path);
1365 else
1367 i++;
1368 gtk_tree_path_next(path);
1372 gtk_tree_path_free(path);
1375 static void view_details_clear(ViewIface *view)
1377 GtkTreePath *path;
1378 GPtrArray *items = ((ViewDetails *) view)->items;
1379 GtkTreeModel *model = (GtkTreeModel *) view;
1381 path = gtk_tree_path_new();
1382 gtk_tree_path_append_index(path, items->len);
1384 while (gtk_tree_path_prev(path))
1385 gtk_tree_model_row_deleted(model, path);
1387 g_ptr_array_set_size(items, 0);
1388 gtk_tree_path_free(path);
1391 static void view_details_select_all(ViewIface *view)
1393 ViewDetails *view_details = (ViewDetails *) view;
1395 view_details->can_change_selection++;
1396 gtk_tree_selection_select_all(view_details->selection);
1397 view_details->can_change_selection--;
1400 static void view_details_clear_selection(ViewIface *view)
1402 ViewDetails *view_details = (ViewDetails *) view;
1404 view_details->can_change_selection++;
1405 gtk_tree_selection_unselect_all(view_details->selection);
1406 view_details->can_change_selection--;
1409 static int view_details_count_items(ViewIface *view)
1411 ViewDetails *view_details = (ViewDetails *) view;
1413 return view_details->items->len;
1416 #if GTK_MINOR_VERSION < 2
1417 static void view_details_count_inc(GtkTreeModel *model, GtkTreePath *path,
1418 GtkTreeIter *iter, gpointer data)
1420 int *count = (int *) data;
1421 (*count) += 1;
1423 #endif
1425 static int view_details_count_selected(ViewIface *view)
1427 ViewDetails *view_details = (ViewDetails *) view;
1429 #if GTK_MINOR_VERSION >= 2
1430 return gtk_tree_selection_count_selected_rows(view_details->selection);
1431 #else
1432 int count = 0;
1434 gtk_tree_selection_selected_foreach(view_details->selection,
1435 view_details_count_inc, &count);
1436 return count;
1437 #endif
1440 static void view_details_show_cursor(ViewIface *view)
1444 static void view_details_get_iter(ViewIface *view,
1445 ViewIter *iter, IterFlags flags)
1447 make_iter((ViewDetails *) view, iter, flags);
1450 static void view_details_get_iter_at_point(ViewIface *view, ViewIter *iter,
1451 GdkWindow *src, int x, int y)
1453 ViewDetails *view_details = (ViewDetails *) view;
1454 GtkTreeModel *model;
1455 GtkTreeView *tree = (GtkTreeView *) view;
1456 GtkTreePath *path = NULL;
1457 int i = -1;
1458 gint cell_y;
1460 model = gtk_tree_view_get_model(tree);
1462 if (gtk_tree_view_get_path_at_pos(tree, x, y + 4, &path, NULL,
1463 NULL, &cell_y))
1465 g_return_if_fail(path != NULL);
1467 if (cell_y > 8)
1468 i = gtk_tree_path_get_indices(path)[0];
1469 gtk_tree_path_free(path);
1472 make_item_iter(view_details, iter, i);
1475 static void view_details_cursor_to_iter(ViewIface *view, ViewIter *iter)
1477 GtkTreePath *path;
1478 ViewDetails *view_details = (ViewDetails *) view;
1480 path = gtk_tree_path_new();
1482 if (iter)
1483 gtk_tree_path_append_index(path, iter->i);
1484 else
1486 /* Using depth zero or index -1 gives an error, but this
1487 * is OK!
1489 gtk_tree_path_append_index(path, view_details->items->len);
1492 gtk_tree_view_set_cursor((GtkTreeView *) view, path, NULL, FALSE);
1493 gtk_tree_path_free(path);
1496 static void set_selected(ViewDetails *view_details, int i, gboolean selected)
1498 GtkTreeIter iter;
1500 iter.user_data = GINT_TO_POINTER(i);
1501 view_details->can_change_selection++;
1502 if (selected)
1503 gtk_tree_selection_select_iter(view_details->selection, &iter);
1504 else
1505 gtk_tree_selection_unselect_iter(view_details->selection,
1506 &iter);
1507 view_details->can_change_selection--;
1510 static void view_details_set_selected(ViewIface *view,
1511 ViewIter *iter,
1512 gboolean selected)
1514 set_selected((ViewDetails *) view, iter->i, selected);
1517 static gboolean get_selected(ViewDetails *view_details, int i)
1519 GtkTreeIter iter;
1521 iter.user_data = GINT_TO_POINTER(i);
1523 return gtk_tree_selection_iter_is_selected(view_details->selection,
1524 &iter);
1527 static gboolean view_details_get_selected(ViewIface *view, ViewIter *iter)
1529 return get_selected((ViewDetails *) view, iter->i);
1532 static void view_details_select_only(ViewIface *view, ViewIter *iter)
1534 ViewDetails *view_details = (ViewDetails *) view;
1535 GtkTreePath *path;
1537 path = gtk_tree_path_new();
1538 gtk_tree_path_append_index(path, iter->i);
1539 view_details->can_change_selection++;
1540 gtk_tree_selection_unselect_all(view_details->selection);
1541 gtk_tree_selection_select_range(view_details->selection, path, path);
1542 view_details->can_change_selection--;
1543 gtk_tree_path_free(path);
1546 static void view_details_set_frozen(ViewIface *view, gboolean frozen)
1550 static void redraw_wink_area(ViewDetails *view_details)
1552 GtkTreePath *wink_path;
1553 GdkRectangle wink_area;
1554 GtkTreeView *tree = (GtkTreeView *) view_details;
1556 g_return_if_fail(view_details->wink_item >= 0);
1558 wink_path = gtk_tree_path_new();
1559 gtk_tree_path_append_index(wink_path, view_details->wink_item);
1560 gtk_tree_view_get_background_area(tree, wink_path, NULL, &wink_area);
1561 gtk_tree_path_free(wink_path);
1563 if (wink_area.height)
1565 GdkWindow *window;
1566 window = gtk_tree_view_get_bin_window(tree);
1568 wink_area.width = GTK_WIDGET(tree)->allocation.width;
1569 gdk_window_invalidate_rect(window, &wink_area, FALSE);
1573 static void cancel_wink(ViewDetails *view_details)
1575 if (view_details->wink_item == -1)
1576 return;
1578 if (view_details->filer_window)
1579 redraw_wink_area(view_details);
1581 view_details->wink_item = -1;
1582 g_source_remove(view_details->wink_timeout);
1585 static gboolean wink_timeout(ViewDetails *view_details)
1587 view_details->wink_step--;
1588 if (view_details->wink_step < 1)
1590 cancel_wink(view_details);
1591 return FALSE;
1594 redraw_wink_area(view_details);
1596 return TRUE;
1599 static void view_details_wink_item(ViewIface *view, ViewIter *iter)
1601 ViewDetails *view_details = (ViewDetails *) view;
1602 GtkTreePath *path;
1604 cancel_wink(view_details);
1605 if (!iter)
1606 return;
1608 view_details->wink_item = iter->i;
1609 view_details->wink_timeout = g_timeout_add(70,
1610 (GSourceFunc) wink_timeout, view_details);
1611 view_details->wink_step = 7;
1612 redraw_wink_area(view_details);
1614 path = gtk_tree_path_new();
1615 gtk_tree_path_append_index(path, iter->i);
1616 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(view),
1617 path, NULL, FALSE, 0, 0);
1618 gtk_tree_path_free(path);
1621 static void view_details_autosize(ViewIface *view)
1623 ViewDetails *view_details = (ViewDetails *) view;
1624 FilerWindow *filer_window = view_details->filer_window;
1625 int max_width = (o_filer_size_limit.int_value * monitor_width) / 100;
1626 int max_height = (o_filer_size_limit.int_value * monitor_height) / 100;
1627 int h;
1628 GtkRequisition req;
1630 gtk_widget_queue_resize(GTK_WIDGET(view));
1631 gtk_widget_size_request(GTK_WIDGET(view), &req);
1633 h = MAX(view_details->desired_size.height, SMALL_HEIGHT);
1635 filer_window_set_size(filer_window,
1636 MIN(view_details->desired_size.width, max_width),
1637 MIN(h, max_height));
1640 static gboolean view_details_cursor_visible(ViewIface *view)
1642 GtkTreePath *path = NULL;
1644 gtk_tree_view_get_cursor((GtkTreeView *) view, &path, NULL);
1646 if (path)
1647 gtk_tree_path_free(path);
1649 return path != NULL;
1652 static void view_details_set_base(ViewIface *view, ViewIter *iter)
1654 ViewDetails *view_details = (ViewDetails *) view;
1656 view_details->cursor_base = iter->i;
1659 /* Change the dynamic corner of the lasso box and trigger a redraw */
1660 static void set_lasso(ViewDetails *view_details, int x, int y)
1662 GdkWindow *window;
1663 GdkRectangle area;
1664 int minx, miny, maxx, maxy;
1666 if (x == view_details->drag_box_x[1] &&
1667 y == view_details->drag_box_y[1])
1668 return;
1670 /* Get old region */
1671 minx = MIN(view_details->drag_box_x[0], view_details->drag_box_x[1]);
1672 miny = MIN(view_details->drag_box_y[0], view_details->drag_box_y[1]);
1673 maxx = MAX(view_details->drag_box_x[0], view_details->drag_box_x[1]);
1674 maxy = MAX(view_details->drag_box_y[0], view_details->drag_box_y[1]);
1676 /* Enlarge to cover new point */
1677 minx = MIN(minx, x);
1678 miny = MIN(miny, y);
1679 maxx = MAX(maxx, x);
1680 maxy = MAX(maxy, y);
1682 area.x = minx;
1683 area.y = miny;
1684 area.width = maxx - minx;
1685 area.height = maxy - miny;
1687 view_details->drag_box_x[1] = x;
1688 view_details->drag_box_y[1] = y;
1690 window = gtk_tree_view_get_bin_window((GtkTreeView *) view_details);
1691 if (area.width && area.height)
1693 GtkAdjustment *adj;
1695 adj = gtk_tree_view_get_vadjustment((GtkTreeView *)
1696 view_details);
1697 area.y -= adj->value;
1698 gdk_window_invalidate_rect(window, &area, FALSE);
1702 static void view_details_start_lasso_box(ViewIface *view, GdkEventButton *event)
1704 ViewDetails *view_details = (ViewDetails *) view;
1705 GtkAdjustment *adj;
1707 adj = gtk_tree_view_get_vadjustment((GtkTreeView *) view_details);
1709 view_details->lasso_start_index = get_lasso_index(view_details,
1710 event->y);
1712 filer_set_autoscroll(view_details->filer_window, TRUE);
1714 view_details->drag_box_x[0] = view_details->drag_box_x[1] = event->x;
1715 view_details->drag_box_y[0] = view_details->drag_box_y[1] = event->y +
1716 adj->value;
1717 view_details->lasso_box = TRUE;
1720 static void view_details_extend_tip(ViewIface *view,
1721 ViewIter *iter, GString *tip)
1725 static DirItem *iter_init(ViewIter *iter)
1727 ViewDetails *view_details = (ViewDetails *) iter->view;
1728 int i = -1;
1729 int n = view_details->items->len;
1730 int flags = iter->flags;
1732 iter->peek = iter_peek;
1734 if (iter->n_remaining == 0)
1735 return NULL;
1737 if (flags & VIEW_ITER_FROM_CURSOR)
1739 GtkTreePath *path;
1740 gtk_tree_view_get_cursor((GtkTreeView *) view_details,
1741 &path, NULL);
1742 if (!path)
1743 return NULL; /* No cursor */
1744 i = gtk_tree_path_get_indices(path)[0];
1745 gtk_tree_path_free(path);
1747 else if (flags & VIEW_ITER_FROM_BASE)
1748 i = view_details->cursor_base;
1750 if (i < 0 || i >= n)
1752 /* Either a normal iteration, or an iteration from an
1753 * invalid starting point.
1755 if (flags & VIEW_ITER_BACKWARDS)
1756 i = n - 1;
1757 else
1758 i = 0;
1761 if (i < 0 || i >= n)
1762 return NULL; /* No items at all! */
1764 iter->next = flags & VIEW_ITER_BACKWARDS ? iter_prev : iter_next;
1765 iter->n_remaining--;
1766 iter->i = i;
1768 if (flags & VIEW_ITER_SELECTED && !is_selected(view_details, i))
1769 return iter->next(iter);
1770 return iter->peek(iter);
1773 static DirItem *iter_prev(ViewIter *iter)
1775 ViewDetails *view_details = (ViewDetails *) iter->view;
1776 int n = view_details->items->len;
1777 int i = iter->i;
1779 g_return_val_if_fail(iter->n_remaining >= 0, NULL);
1781 /* i is the last item returned (always valid) */
1783 g_return_val_if_fail(i >= 0 && i < n, NULL);
1785 while (iter->n_remaining)
1787 i--;
1788 iter->n_remaining--;
1790 if (i == -1)
1791 i = n - 1;
1793 g_return_val_if_fail(i >= 0 && i < n, NULL);
1795 if (iter->flags & VIEW_ITER_SELECTED &&
1796 !is_selected(view_details, i))
1797 continue;
1799 iter->i = i;
1800 return ((ViewItem *) view_details->items->pdata[i])->item;
1803 iter->i = -1;
1804 return NULL;
1807 static DirItem *iter_next(ViewIter *iter)
1809 ViewDetails *view_details = (ViewDetails *) iter->view;
1810 int n = view_details->items->len;
1811 int i = iter->i;
1813 g_return_val_if_fail(iter->n_remaining >= 0, NULL);
1815 /* i is the last item returned (always valid) */
1817 g_return_val_if_fail(i >= 0 && i < n, NULL);
1819 while (iter->n_remaining)
1821 i++;
1822 iter->n_remaining--;
1824 if (i == n)
1825 i = 0;
1827 g_return_val_if_fail(i >= 0 && i < n, NULL);
1829 if (iter->flags & VIEW_ITER_SELECTED &&
1830 !is_selected(view_details, i))
1831 continue;
1833 iter->i = i;
1834 return ((ViewItem *) view_details->items->pdata[i])->item;
1837 iter->i = -1;
1838 return NULL;
1841 static DirItem *iter_peek(ViewIter *iter)
1843 ViewDetails *view_details = (ViewDetails *) iter->view;
1844 int n = view_details->items->len;
1845 int i = iter->i;
1847 if (i == -1)
1848 return NULL;
1850 g_return_val_if_fail(i >= 0 && i < n, NULL);
1852 return ((ViewItem *) view_details->items->pdata[i])->item;
1855 /* Set the iterator to return 'i' on the next peek().
1856 * If i is -1, returns NULL on next peek().
1858 static void make_item_iter(ViewDetails *view_details, ViewIter *iter, int i)
1860 make_iter(view_details, iter, 0);
1862 g_return_if_fail(i >= -1 && i < (int) view_details->items->len);
1864 iter->i = i;
1865 iter->next = iter_next;
1866 iter->peek = iter_peek;
1867 iter->n_remaining = 0;
1870 static void make_iter(ViewDetails *view_details, ViewIter *iter,
1871 IterFlags flags)
1873 iter->view = (ViewIface *) view_details;
1874 iter->next = iter_init;
1875 iter->peek = NULL;
1876 iter->i = -1;
1878 iter->flags = flags;
1880 if (flags & VIEW_ITER_ONE_ONLY)
1882 iter->n_remaining = 1;
1883 iter->next(iter);
1885 else
1886 iter->n_remaining = view_details->items->len;
1889 static void free_view_item(ViewItem *view_item)
1891 if (view_item->image)
1892 g_object_unref(G_OBJECT(view_item->image));
1893 g_free(view_item->utf8_name);
1894 g_free(view_item);
1897 static gboolean view_details_auto_scroll_callback(ViewIface *view)
1899 GtkTreeView *tree = (GtkTreeView *) view;
1900 ViewDetails *view_details = (ViewDetails *) view;
1901 FilerWindow *filer_window = view_details->filer_window;
1902 GtkRange *scrollbar = (GtkRange *) filer_window->scrollbar;
1903 GtkAdjustment *adj;
1904 GdkWindow *window;
1905 gint x, y, w, h;
1906 GdkModifierType mask;
1907 int diff = 0;
1909 window = gtk_tree_view_get_bin_window(tree);
1911 gdk_window_get_pointer(window, &x, &y, &mask);
1912 gdk_drawable_get_size(window, &w, NULL);
1914 adj = gtk_range_get_adjustment(scrollbar);
1915 h = adj->page_size;
1917 if ((x < 0 || x > w || y < 0 || y > h) && !view_details->lasso_box)
1918 return FALSE; /* Out of window - stop */
1920 if (y < AUTOSCROLL_STEP)
1921 diff = y - AUTOSCROLL_STEP;
1922 else if (y > h - AUTOSCROLL_STEP)
1923 diff = AUTOSCROLL_STEP + y - h;
1925 if (diff)
1927 int old = adj->value;
1928 int value = old + diff;
1930 value = CLAMP(value, 0, adj->upper - adj->page_size);
1931 gtk_adjustment_set_value(adj, value);
1933 if (adj->value != old)
1934 dnd_spring_abort();
1937 return TRUE;