r1996: Cope slightly better with invalid filenames in various places (reported by
[rox-filer.git] / ROX-Filer / src / view_iface.c
blobe809163169232bde3971c864e66f30bfbfa9b30a
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 /* view_iface.c - operations supported by all views */
24 #include "config.h"
26 #include "global.h"
28 #include "view_iface.h"
30 /* A word about interfaces:
32 * gobject's documentation's explanation of interfaces leaves something[1] to
33 * be desired, so I'd better explain here...
35 * [1] Like, eg, an explanation.
37 * A ViewIfaceClass is a struct which contains a number of function
38 * pointers. Each class that implements the View interface creates its
39 * own ViewIfaceClass with pointers to its implementation. This is stored
40 * with the class.
42 * When you want to call a method (eg, sort()) on a View, you call
43 * view_sort(object) here, which gets the class of object and then looks
44 * for that class's implementation of the View interface, and then calls
45 * the actual function through that.
48 /* ViewIters
50 * A ViewIter is used to index items. They are usually allocated
51 * on the stack and then initialised using view_get_iter().
53 * Normally, an iterator starts of not pointing at any item, but
54 * each call to iter->next(iter) returns the next item, and leaves
55 * the iterator pointing at the returned item. If you like the item,
56 * you can then pass the iterator to view_cursor_to_iter(), etc.
58 * Using flags passed to view_get_iter, you can start the sequence from the
59 * beginning, end, cursor or 'base' (a saved cursor position). You can
60 * go forwards or backwards. You can opt to only get selected items returned.
62 * You can also have one-shot iterators which already point to an item, and
63 * you never call the next method (view_get_cursor, for example). In fact,
64 * these iterators return a sequence of one item, but next() gets called
65 * automatically for you.
67 * You don't need to free iterators, and they become invalid if the
68 * View changes (items added, removed or altered), so don't hang on to
69 * them!
72 /****************************************************************
73 * EXTERNAL INTERFACE *
74 ****************************************************************/
76 GType view_iface_get_type(void)
78 static GType iface_type = 0;
80 if (!iface_type)
82 static const GTypeInfo iface_info =
84 sizeof (ViewIfaceClass),
85 NULL, /* base_init */
86 NULL, /* base_finalize */
89 iface_type = g_type_register_static(G_TYPE_INTERFACE,
90 "ViewIface", &iface_info, 0);
92 /* Actually, all Views should be GTK_TYPE_WIDGETs, to be more
93 * accurate, but including gtk.h takes so long, and noone's
94 * going to get this wrong ;-)
96 g_type_interface_add_prerequisite(iface_type, G_TYPE_OBJECT);
99 return iface_type;
102 /* The sort function has changed -- resort */
103 void view_sort(ViewIface *obj)
105 g_return_if_fail(VIEW_IS_IFACE(obj));
106 VIEW_IFACE_GET_CLASS(obj)->sort(obj);
109 /* The style has changed -- shrink the grid and redraw.
110 * Also update ViewData (and name layout too) if appropriate
111 * flags are set.
113 void view_style_changed(ViewIface *obj, int flags)
115 g_return_if_fail(VIEW_IS_IFACE(obj));
116 VIEW_IFACE_GET_CLASS(obj)->style_changed(obj, flags);
119 /* Wink or move the cursor to this item, if present. Return TRUE on
120 * success (iff leaf was present).
122 gboolean view_autoselect(ViewIface *obj, const gchar *leaf)
124 g_return_val_if_fail(VIEW_IS_IFACE(obj), FALSE);
125 g_return_val_if_fail(leaf != NULL, FALSE);
127 return VIEW_IFACE_GET_CLASS(obj)->autoselect(obj, leaf);
130 /* Scanning has turned up some new items... */
131 void view_add_items(ViewIface *obj, GPtrArray *items)
133 VIEW_IFACE_GET_CLASS(obj)->add_items(obj, items);
136 /* These items are already known, but have changed... */
137 void view_update_items(ViewIface *obj, GPtrArray *items)
139 VIEW_IFACE_GET_CLASS(obj)->update_items(obj, items);
142 /* Call test(item) for each item in the view and delete all those for
143 * which it returns TRUE.
145 void view_delete_if(ViewIface *obj,
146 gboolean (*test)(gpointer item, gpointer data),
147 gpointer data)
149 g_return_if_fail(VIEW_IS_IFACE(obj));
151 VIEW_IFACE_GET_CLASS(obj)->delete_if(obj, test, data);
154 /* Remove all items from the view (used when changing directory) */
155 void view_clear(ViewIface *obj)
157 g_return_if_fail(VIEW_IS_IFACE(obj));
159 VIEW_IFACE_GET_CLASS(obj)->clear(obj);
162 /* Select all items */
163 void view_select_all(ViewIface *obj)
165 g_return_if_fail(VIEW_IS_IFACE(obj));
167 VIEW_IFACE_GET_CLASS(obj)->select_all(obj);
170 /* Unselect all items */
171 void view_clear_selection(ViewIface *obj)
173 g_return_if_fail(VIEW_IS_IFACE(obj));
175 VIEW_IFACE_GET_CLASS(obj)->clear_selection(obj);
178 /* Return the total number of items */
179 int view_count_items(ViewIface *obj)
181 g_return_val_if_fail(VIEW_IS_IFACE(obj), 0);
183 return VIEW_IFACE_GET_CLASS(obj)->count_items(obj);
186 /* Return the number of selected items */
187 int view_count_selected(ViewIface *obj)
189 g_return_val_if_fail(VIEW_IS_IFACE(obj), 0);
191 return VIEW_IFACE_GET_CLASS(obj)->count_selected(obj);
194 void view_show_cursor(ViewIface *obj)
196 g_return_if_fail(VIEW_IS_IFACE(obj));
198 VIEW_IFACE_GET_CLASS(obj)->show_cursor(obj);
201 /* Create an iterator which will return each element selected by 'flags'
202 * from successive calls to iter.next(&iter). NULL indicates the end
203 * of the sequence.
205 * The iterator does not need to be freed. It becomes invalid if the
206 * view is changed in any way.
208 void view_get_iter(ViewIface *obj, ViewIter *iter, IterFlags flags)
210 g_return_if_fail(VIEW_IS_IFACE(obj));
211 g_return_if_fail(iter != NULL);
213 VIEW_IFACE_GET_CLASS(obj)->get_iter(obj, iter, flags);
216 /* Make an 'iter' for the cursor item, if any. Use iter->peek() to get
217 * the DirItem (will be NULL if the cursor isn't on an item).
219 void view_get_cursor(ViewIface *obj, ViewIter *iter)
221 g_return_if_fail(VIEW_IS_IFACE(obj));
222 g_return_if_fail(iter != NULL);
224 VIEW_IFACE_GET_CLASS(obj)->get_iter(obj, iter,
225 VIEW_ITER_FROM_CURSOR | VIEW_ITER_ONE_ONLY);
228 /* Position cursor on the last item returned by iter.next().
229 * If iter is NULL, remove the cursor.
231 void view_cursor_to_iter(ViewIface *obj, ViewIter *iter)
233 g_return_if_fail(VIEW_IS_IFACE(obj));
235 VIEW_IFACE_GET_CLASS(obj)->cursor_to_iter(obj, iter);
238 /* Select the item at this iter */
239 void view_set_selected(ViewIface *obj, ViewIter *iter, gboolean selected)
241 g_return_if_fail(VIEW_IS_IFACE(obj));
243 VIEW_IFACE_GET_CLASS(obj)->set_selected(obj, iter, selected);
246 gboolean view_get_selected(ViewIface *obj, ViewIter *iter)
248 g_return_val_if_fail(VIEW_IS_IFACE(obj), FALSE);
250 return VIEW_IFACE_GET_CLASS(obj)->get_selected(obj, iter);
253 /* Flash / draw attention to this item */
254 void view_wink_item(ViewIface *obj, ViewIter *iter)
256 g_return_if_fail(VIEW_IS_IFACE(obj));
258 VIEW_IFACE_GET_CLASS(obj)->wink_item(obj, iter);
261 /* Clear the selection, then select this item. Does it atomically to avoid
262 * problems with giving up and quickly reclaiming the primary selection.
264 void view_select_only(ViewIface *obj, ViewIter *iter)
266 g_return_if_fail(VIEW_IS_IFACE(obj));
268 VIEW_IFACE_GET_CLASS(obj)->select_only(obj, iter);
271 void view_select_if(ViewIface *obj,
272 gboolean (*test)(ViewIter *iter, gpointer data),
273 gpointer data)
275 ViewIter iter;
276 gboolean should_select_first;
278 g_return_if_fail(VIEW_IS_IFACE(obj));
280 view_get_iter(obj, &iter, 0);
282 if (!iter.next(&iter))
283 return; /* No items */
285 view_freeze(obj);
287 /* If anything is currently selected then select the first item now
288 * and set it to its correct value at the end (avoids losing the
289 * primary and regaining it quickly). Do the test first in case it
290 * relies on the selected state!
292 should_select_first = test(&iter, data);
293 if (view_count_selected(obj))
294 view_set_selected(obj, &iter, TRUE);
296 while (iter.next(&iter))
297 view_set_selected(obj, &iter, test(&iter, data));
299 view_get_iter(obj, &iter, 0);
300 iter.next(&iter);
301 view_set_selected(obj, &iter, should_select_first);
303 view_thaw(obj);
306 /* Prevent selection_changed events from being emitted */
307 void view_freeze(ViewIface *obj)
309 g_return_if_fail(VIEW_IS_IFACE(obj));
311 VIEW_IFACE_GET_CLASS(obj)->set_frozen(obj, TRUE);
314 /* Undo a view_freeze (and emit the changed signal) */
315 void view_thaw(ViewIface *obj)
317 g_return_if_fail(VIEW_IS_IFACE(obj));
319 VIEW_IFACE_GET_CLASS(obj)->set_frozen(obj, FALSE);
322 /* Resize the filer window to a sensible size.
323 * v_border is the height of the toolbar + the minibuffer (if visible).
324 * space is
325 * If allow_shrink is
327 void view_autosize(ViewIface *obj)
329 g_return_if_fail(VIEW_IS_IFACE(obj));
331 VIEW_IFACE_GET_CLASS(obj)->autosize(obj);
334 /* Return TRUE if the cursor is shown. Note that the cursor may be visible
335 * even if their are no items (so get_cursor().peek() would return NULL).
337 gboolean view_cursor_visible(ViewIface *obj)
339 g_return_val_if_fail(VIEW_IS_IFACE(obj), FALSE);
341 return VIEW_IFACE_GET_CLASS(obj)->cursor_visible(obj);
344 /* The 'base' position is used to record the position of the cursor
345 * when the minibuffer is opened, for interactive searching.
347 void view_set_base(ViewIface *obj, ViewIter *iter)
349 g_return_if_fail(VIEW_IS_IFACE(obj));
351 VIEW_IFACE_GET_CLASS(obj)->set_base(obj, iter);