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)
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
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 */
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
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.
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
72 /****************************************************************
73 * EXTERNAL INTERFACE *
74 ****************************************************************/
76 GType
view_iface_get_type(void)
78 static GType iface_type
= 0;
82 static const GTypeInfo iface_info
=
84 sizeof (ViewIfaceClass
),
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
);
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
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
),
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
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 /* TRUE if this item is selected */
247 gboolean
view_get_selected(ViewIface
*obj
, ViewIter
*iter
)
249 g_return_val_if_fail(VIEW_IS_IFACE(obj
), FALSE
);
251 return VIEW_IFACE_GET_CLASS(obj
)->get_selected(obj
, iter
);
254 /* Flash / draw attention to this item */
255 void view_wink_item(ViewIface
*obj
, ViewIter
*iter
)
257 g_return_if_fail(VIEW_IS_IFACE(obj
));
259 VIEW_IFACE_GET_CLASS(obj
)->wink_item(obj
, iter
);
262 /* Clear the selection, then select this item. Does it atomically to avoid
263 * problems with giving up and quickly reclaiming the primary selection.
265 void view_select_only(ViewIface
*obj
, ViewIter
*iter
)
267 g_return_if_fail(VIEW_IS_IFACE(obj
));
269 VIEW_IFACE_GET_CLASS(obj
)->select_only(obj
, iter
);
272 void view_select_if(ViewIface
*obj
,
273 gboolean (*test
)(ViewIter
*iter
, gpointer data
),
277 gboolean should_select_first
;
279 g_return_if_fail(VIEW_IS_IFACE(obj
));
281 view_get_iter(obj
, &iter
, 0);
283 if (!iter
.next(&iter
))
284 return; /* No items */
288 /* If anything is currently selected then select the first item now
289 * and set it to its correct value at the end (avoids losing the
290 * primary and regaining it quickly). Do the test first in case it
291 * relies on the selected state!
293 should_select_first
= test(&iter
, data
);
294 if (view_count_selected(obj
))
295 view_set_selected(obj
, &iter
, TRUE
);
297 while (iter
.next(&iter
))
298 view_set_selected(obj
, &iter
, test(&iter
, data
));
300 view_get_iter(obj
, &iter
, 0);
302 view_set_selected(obj
, &iter
, should_select_first
);
307 /* Prevent selection_changed events from being emitted */
308 void view_freeze(ViewIface
*obj
)
310 g_return_if_fail(VIEW_IS_IFACE(obj
));
312 VIEW_IFACE_GET_CLASS(obj
)->set_frozen(obj
, TRUE
);
315 /* Undo a view_freeze (and emit the changed signal) */
316 void view_thaw(ViewIface
*obj
)
318 g_return_if_fail(VIEW_IS_IFACE(obj
));
320 VIEW_IFACE_GET_CLASS(obj
)->set_frozen(obj
, FALSE
);
323 /* Resize the filer window to a sensible size.
324 * v_border is the height of the toolbar + the minibuffer (if visible).
328 void view_autosize(ViewIface
*obj
)
330 g_return_if_fail(VIEW_IS_IFACE(obj
));
332 VIEW_IFACE_GET_CLASS(obj
)->autosize(obj
);
335 /* Return TRUE if the cursor is shown. Note that the cursor may be visible
336 * even if their are no items (so get_cursor().peek() would return NULL).
338 gboolean
view_cursor_visible(ViewIface
*obj
)
340 g_return_val_if_fail(VIEW_IS_IFACE(obj
), FALSE
);
342 return VIEW_IFACE_GET_CLASS(obj
)->cursor_visible(obj
);
345 /* The 'base' position is used to record the position of the cursor
346 * when the minibuffer is opened, for interactive searching.
348 void view_set_base(ViewIface
*obj
, ViewIter
*iter
)
350 g_return_if_fail(VIEW_IS_IFACE(obj
));
352 VIEW_IFACE_GET_CLASS(obj
)->set_base(obj
, iter
);