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.
48 /****************************************************************
49 * EXTERNAL INTERFACE *
50 ****************************************************************/
52 GType
view_iface_get_type(void)
54 static GType iface_type
= 0;
58 static const GTypeInfo iface_info
=
60 sizeof (ViewIfaceClass
),
62 NULL
, /* base_finalize */
65 iface_type
= g_type_register_static(G_TYPE_INTERFACE
,
66 "ViewIface", &iface_info
, 0);
68 /* Actually, all Views should be GTK_TYPE_WIDGETs, to be more
69 * accurate, but including gtk.h takes so long, and noone's
70 * going to get this wrong ;-)
72 g_type_interface_add_prerequisite(iface_type
, G_TYPE_OBJECT
);
78 /* The sort function has changed -- resort */
79 void view_sort(ViewIface
*obj
)
81 g_return_if_fail(VIEW_IS_IFACE(obj
));
82 VIEW_IFACE_GET_CLASS(obj
)->sort(obj
);
85 /* The style has changed -- shrink the grid and redraw.
86 * Also update ViewData (and name layout too) if appropriate
89 void view_style_changed(ViewIface
*obj
, int flags
)
91 g_return_if_fail(VIEW_IS_IFACE(obj
));
92 VIEW_IFACE_GET_CLASS(obj
)->style_changed(obj
, flags
);
95 /* Wink or move the cursor to this item, if present. Return TRUE on
96 * success (iff leaf was present).
98 gboolean
view_autoselect(ViewIface
*obj
, const gchar
*leaf
)
100 g_return_val_if_fail(VIEW_IS_IFACE(obj
), FALSE
);
101 g_return_val_if_fail(leaf
!= NULL
, FALSE
);
103 return VIEW_IFACE_GET_CLASS(obj
)->autoselect(obj
, leaf
);
106 /* Scanning has turned up some new items... */
107 void view_add_items(ViewIface
*obj
, GPtrArray
*items
)
109 VIEW_IFACE_GET_CLASS(obj
)->add_items(obj
, items
);
112 /* These items are already known, but have changed... */
113 void view_update_items(ViewIface
*obj
, GPtrArray
*items
)
115 VIEW_IFACE_GET_CLASS(obj
)->update_items(obj
, items
);
118 /* Call test(item) for each item in the view and delete all those for
119 * which it returns TRUE.
121 void view_delete_if(ViewIface
*obj
,
122 gboolean (*test
)(gpointer item
, gpointer data
),
125 g_return_if_fail(VIEW_IS_IFACE(obj
));
127 VIEW_IFACE_GET_CLASS(obj
)->delete_if(obj
, test
, data
);
130 /* Remove all items from the view (used when changing directory) */
131 void view_clear(ViewIface
*obj
)
133 g_return_if_fail(VIEW_IS_IFACE(obj
));
135 VIEW_IFACE_GET_CLASS(obj
)->clear(obj
);
138 /* Select all items */
139 void view_select_all(ViewIface
*obj
)
141 g_return_if_fail(VIEW_IS_IFACE(obj
));
143 VIEW_IFACE_GET_CLASS(obj
)->select_all(obj
);
146 /* Unselect all items */
147 void view_clear_selection(ViewIface
*obj
)
149 g_return_if_fail(VIEW_IS_IFACE(obj
));
151 VIEW_IFACE_GET_CLASS(obj
)->clear_selection(obj
);
154 /* Return the total number of items */
155 int view_count_items(ViewIface
*obj
)
157 g_return_val_if_fail(VIEW_IS_IFACE(obj
), 0);
159 return VIEW_IFACE_GET_CLASS(obj
)->count_items(obj
);
162 /* Return the number of selected items */
163 int view_count_selected(ViewIface
*obj
)
165 g_return_val_if_fail(VIEW_IS_IFACE(obj
), 0);
167 return VIEW_IFACE_GET_CLASS(obj
)->count_selected(obj
);
170 void view_show_cursor(ViewIface
*obj
)
172 g_return_if_fail(VIEW_IS_IFACE(obj
));
174 VIEW_IFACE_GET_CLASS(obj
)->show_cursor(obj
);
177 /* Create an iterator which will return each element selected by 'flags'
178 * from successive calls to iter.next(&iter). NULL indicates the end
181 * The iterator does not need to be freed. It becomes invalid if the
182 * view is changed in any way.
184 void view_get_iter(ViewIface
*obj
, ViewIter
*iter
, IterFlags flags
)
186 g_return_if_fail(VIEW_IS_IFACE(obj
));
187 g_return_if_fail(iter
!= NULL
);
189 VIEW_IFACE_GET_CLASS(obj
)->get_iter(obj
, iter
, flags
);
192 /* Make an 'iter' whose next method will return the cursor item, if any */
193 void view_get_cursor(ViewIface
*obj
, ViewIter
*iter
)
195 g_return_if_fail(VIEW_IS_IFACE(obj
));
196 g_return_if_fail(iter
!= NULL
);
198 VIEW_IFACE_GET_CLASS(obj
)->get_iter(obj
, iter
,
199 VIEW_ITER_FROM_CURSOR
| VIEW_ITER_ONE_ONLY
);
202 /* Position cursor on the last item returned by iter.next().
203 * If iter is NULL, remove the cursor.
205 void view_cursor_to_iter(ViewIface
*obj
, ViewIter
*iter
)
207 g_return_if_fail(VIEW_IS_IFACE(obj
));
209 VIEW_IFACE_GET_CLASS(obj
)->cursor_to_iter(obj
, iter
);
212 /* Select the item at this iter */
213 void view_set_selected(ViewIface
*obj
, ViewIter
*iter
, gboolean selected
)
215 g_return_if_fail(VIEW_IS_IFACE(obj
));
217 VIEW_IFACE_GET_CLASS(obj
)->set_selected(obj
, iter
, selected
);
220 gboolean
view_get_selected(ViewIface
*obj
, ViewIter
*iter
)
222 g_return_val_if_fail(VIEW_IS_IFACE(obj
), FALSE
);
224 return VIEW_IFACE_GET_CLASS(obj
)->get_selected(obj
, iter
);
227 /* Flash / draw attention to this item */
228 void view_wink_item(ViewIface
*obj
, ViewIter
*iter
)
230 g_return_if_fail(VIEW_IS_IFACE(obj
));
232 VIEW_IFACE_GET_CLASS(obj
)->wink_item(obj
, iter
);
235 /* Clear the selection, then select this item. Does it atomically to avoid
236 * problems with giving up and quickly reclaiming the primary selection.
238 void view_select_only(ViewIface
*obj
, ViewIter
*iter
)
240 g_return_if_fail(VIEW_IS_IFACE(obj
));
242 VIEW_IFACE_GET_CLASS(obj
)->select_only(obj
, iter
);
245 void view_select_if(ViewIface
*obj
,
246 gboolean (*test
)(ViewIter
*iter
, gpointer data
),
251 g_return_if_fail(VIEW_IS_IFACE(obj
));
253 view_get_iter(obj
, &iter
, 0);
255 if (!iter
.next(&iter
))
256 return; /* No items */
260 /* If anything is currently selected then select the first item now
261 * and set it to its correct value at the end (avoids losing the
262 * primary and regaining it quickly).
264 if (view_count_selected(obj
))
265 view_set_selected(obj
, &iter
, TRUE
);
267 while (iter
.next(&iter
))
268 view_set_selected(obj
, &iter
, test(&iter
, data
));
270 view_get_iter(obj
, &iter
, 0);
272 view_set_selected(obj
, &iter
, test(&iter
, data
));
277 /* Prevent selection_changed events from being emitted */
278 void view_freeze(ViewIface
*obj
)
280 g_return_if_fail(VIEW_IS_IFACE(obj
));
282 VIEW_IFACE_GET_CLASS(obj
)->set_frozen(obj
, TRUE
);
285 /* Undo a view_freeze (and emit the changed signal) */
286 void view_thaw(ViewIface
*obj
)
288 g_return_if_fail(VIEW_IS_IFACE(obj
));
290 VIEW_IFACE_GET_CLASS(obj
)->set_frozen(obj
, FALSE
);
293 /* Resize the filer window to a sensible size.
294 * v_border is the height of the toolbar + the minibuffer (if visible).
298 void view_autosize(ViewIface
*obj
)
300 g_return_if_fail(VIEW_IS_IFACE(obj
));
302 VIEW_IFACE_GET_CLASS(obj
)->autosize(obj
);
305 /* Return TRUE if the cursor is shown. Note that the cursor may be visible
306 * even if their are no items (so get_cursor().peek() would return NULL).
308 gboolean
view_cursor_visible(ViewIface
*obj
)
310 g_return_val_if_fail(VIEW_IS_IFACE(obj
), FALSE
);
312 return VIEW_IFACE_GET_CLASS(obj
)->cursor_visible(obj
);
315 /* The 'base' position is used to record the position of the cursor
316 * when the minibuffer is opened, for interactive searching.
318 void view_set_base(ViewIface
*obj
, ViewIter
*iter
)
320 g_return_if_fail(VIEW_IS_IFACE(obj
));
322 VIEW_IFACE_GET_CLASS(obj
)->set_base(obj
, iter
);