Debian Codegraph @

gtk/src/listbox.hg

raw
1 /* Copyright (C) 2013 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <gtkmm/container.h>
18 #include <gtkmm/listboxrow.h>
19 #include <gtkmm/enums.h>
20 #include <giomm/liststore.h>
21 #include <glibmm/exceptionhandler.h>
22 #include <vector>
23 #include <utility> // std::forward
24
25 _DEFS(gtkmm,gtk)
26 _PINCLUDE(gtkmm/private/container_p.h)
27
28 namespace Gtk
29 {
30 class GTKMM_API Adjustment;
31
32 /** A ListBox is a vertical container that contains ListBoxRow
33 * children. These rows can by dynamically sorted and filtered, and
34 * headers can be added dynamically depending on the row content.
35 * It also allows keyboard and mouse navigation and selection like
36 * a typical list.
37 *
38 * Using ListBox is often an alternative to TreeView, especially
39 * when the list contents have a more complicated layout than what is allowed
40 * by a CellRenderer, or when the contents are interactive (e.g. contain a
41 * button).
42 *
43 * Although a ListBox must have only ListBoxRow children you can
44 * add any kind of widget to it via Container::add(), and a ListBoxRow
45 * widget will automatically be inserted between the list and the widget.
46 *
47 * Also see FlowBox.
48 *
49 * A ListBox looks like this:
50 * @image html listbox1.png
51 *
52 * @ingroup Widgets
53 * @ingroup Containers
54 *
55 * @newin{3,10}
56 */
57 class GTKMM_API ListBox : public Container
58 {
59 _CLASS_GTKOBJECT(ListBox, GtkListBox, GTK_LIST_BOX, Gtk::Container, GtkContainer, , , GTKMM_API)
60 public:
61 _CTOR_DEFAULT
62
63 /** For instance: bool on_filter(ListBoxRow* row);
64 *
65 * Will be called whenever the row changes or is added and lets you control
66 * if the row should be visible or not.
67 *
68 * @param row The row that may be filtered
69 * @returns <tt>true</tt> if the row should be visible, <tt>false</tt> otherwise
70 *
71 * @newin{3,10}
72 */
73 typedef sigc::slot<bool, ListBoxRow*> SlotFilter;
74
75 /** For instance: int on_sort(ListBoxRow* row1, ListBoxRow* row2);
76 *
77 * Compare two rows to determine which should be first.
78 *
79 * @param row1 The first row
80 * @param row2 The second row
81 * @returns < 0 if @a row1 should be before @a row2, 0 if they are
82 * equal and > 0 otherwise
83 *
84 * @newin{3,10}
85 */
86 typedef sigc::slot<int, ListBoxRow*, ListBoxRow*> SlotSort;
87
88 /** For instance: void on_update_header(ListBoxRow* row, ListBoxRow* before);
89 *
90 * Whenever @a row changes or which row is before @a row changes this
91 * is called, which lets you update the header on @a row. You may
92 * remove or set a new one via ListBoxRow::set_header() or
93 * just change the state of the current header widget.
94 *
95 * @param row The row to update
96 * @param before The row before @a row, or <tt>0</tt> if it is first
97 *
98 * @newin{3,10}
99 */
100 typedef sigc::slot<void, ListBoxRow*, ListBoxRow*> SlotUpdateHeader;
101
102 _WRAP_METHOD(void prepend(Widget& child), gtk_list_box_prepend)
103 _WRAP_METHOD(void insert(Widget& child, int position), gtk_list_box_insert)
104
105 //We added this in the gtkmm API, because this is clearer than the magic -1 value for position with insert().
106 //The C API developers disagree: https://bugzilla.gnome.org/show_bug.cgi?id=705558#c12
107 /** Append a widget to the list. If a sort function is set, the widget will
108 * actually be inserted at the calculated position and this function has the
109 * same effect of Gtk::Container::add().
110 *
111 * @newin{3,10}
112 *
113 * @param child The Gtk::Widget to add.
114 */
115 void append(Widget& child);
116
117 _WRAP_METHOD(ListBoxRow* get_selected_row(), gtk_list_box_get_selected_row)
118 _WRAP_METHOD(const ListBoxRow* get_selected_row() const, gtk_list_box_get_selected_row, constversion)
119 _WRAP_METHOD(ListBoxRow* get_row_at_index(int index), gtk_list_box_get_row_at_index)
120 _WRAP_METHOD(const ListBoxRow* get_row_at_index(int index) const, gtk_list_box_get_row_at_index, constversion)
121 _WRAP_METHOD(ListBoxRow* get_row_at_y(int y), gtk_list_box_get_row_at_y)
122 _WRAP_METHOD(const ListBoxRow* get_row_at_y(int y) const, gtk_list_box_get_row_at_y, constversion)
123 _WRAP_METHOD(void select_row(ListBoxRow& row), gtk_list_box_select_row)
124
125 /** Unselects the currently selected row, if any.
126 *
127 * @newin{3,10}
128 */
129 void unselect_row();
130
131 _WRAP_METHOD(void set_placeholder(Widget& placeholder), gtk_list_box_set_placeholder)
132
133 /** Removes the placeholder widget, if any.
134 *
135 * @newin{3,10}
136 */
137 void unset_placeholder();
138
139 _WRAP_METHOD(void set_adjustment(const Glib::RefPtr<Adjustment>& adjustment), gtk_list_box_set_adjustment)
140 _WRAP_METHOD(Glib::RefPtr<Adjustment> get_adjustment(), gtk_list_box_get_adjustment, refreturn)
141 _WRAP_METHOD(Glib::RefPtr<const Adjustment> get_adjustment() const, gtk_list_box_get_adjustment, refreturn, constversion)
142
143 /** For instance,
144 * void on_foreach(ListBoxRow* row);
145 */
146 typedef sigc::slot<void, ListBoxRow*> SlotForeach;
147
148 void selected_foreach(const SlotForeach& slot);
149 _IGNORE(gtk_list_box_selected_foreach)
150
151 #m4 _CONVERSION(`GList*',`std::vector<ListBoxRow*>',`Glib::ListHandler<ListBoxRow*>::list_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
152 _WRAP_METHOD(std::vector<ListBoxRow*> get_selected_rows(), gtk_list_box_get_selected_rows)
153
154 #m4 _CONVERSION(`GList*',`std::vector<const ListBoxRow*>',`Glib::ListHandler<ListBoxRow*>::list_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
155 _WRAP_METHOD(std::vector<ListBoxRow*> get_selected_rows() const, gtk_list_box_get_selected_rows)
156
157
158 _WRAP_METHOD(void unselect_row(ListBoxRow* row), gtk_list_box_unselect_row)
159 _WRAP_METHOD(void select_all(), gtk_list_box_select_all)
160 _WRAP_METHOD(void unselect_all(), gtk_list_box_unselect_all)
161
162
163 _WRAP_METHOD(void set_selection_mode(SelectionMode mode), gtk_list_box_set_selection_mode)
164 _WRAP_METHOD(SelectionMode get_selection_mode() const, gtk_list_box_get_selection_mode)
165
166 /** Sets a filter function.
167 *
168 * By setting a filter function on the ListBox one can decide dynamically which
169 * of the rows to show. For instance, to implement a search function on a list that
170 * filters the original list to only show the matching rows.
171 *
172 * The @a slot will be called for each row after the call, and it will
173 * continue to be called each time a row changes (via ListBoxRow::changed()) or
174 * when invalidate_filter() is called.
175 *
176 * @param slot Callback that lets you filter which rows to show
177 * @newin{3,10}
178 */
179 void set_filter_func(const SlotFilter& slot);
180 _IGNORE(gtk_list_box_set_filter_func)
181
182 /** Removes the filter function, if any.
183 * @newin{3,10}
184 */
185 void unset_filter_func();
186
187 /** Sets a sort function.
188 *
189 * By setting a sort function on the ListBox one can dynamically reorder the rows
190 * of the list, based on the contents of the rows.
191 *
192 * The @a slot will be called for each row after the call, and will continue to
193 * be called each time a row changes (via ListBoxRow::changed()) and when
194 * invalidate_sort() is called.
195 *
196 * @param slot The sort function.
197 * @newin{3,10}
198 */
199 void set_sort_func(const SlotSort& slot);
200 _IGNORE(gtk_list_box_set_sort_func)
201
202 /** Removes the sort function, if any.
203 * @newin{3,10}
204 */
205 void unset_sort_func();
206
207 /** Sets a header function.
208 *
209 * By setting a header function on the ListBox one can dynamically add headers
210 * in front of rows, depending on the contents of the row and its position in the list.
211 * For instance, one could use it to add headers in front of the first item of a
212 * new kind, in a list sorted by the kind.
213 *
214 * The @a slot can look at the current header widget using ListBoxRow::get_header()
215 * and either update the state of the widget as needed, or set a new one using
216 * ListBoxRow::set_header(). If no header is needed, use ListBoxRow::unset_header().
217 *
218 * Note that you may get many calls to this @a slot for a particular row when e.g.
219 * changing things that don't affect the header. In this case it is important for performance
220 * to not blindly replace an exisiting header with an identical one.
221 *
222 * The @a slot function will be called for each row after the call, and it will
223 * continue to be called each time a row changes (via ListBoxRow::changed()) and when
224 * the row before changes (either by ListBoxRow::changed() on the previous row, or when
225 * the previous row becomes a different row). It is also called for all rows when
226 * invalidate_headers() is called.
227 *
228 * @param slot Callback that lets you add row headers
229 * @newin{3,10}
230 */
231 void set_header_func(const SlotUpdateHeader& slot);
232 _IGNORE(gtk_list_box_set_header_func)
233
234 /** Removes the header function, if any.
235 * @newin{3,10}
236 */
237 void unset_header_func();
238
239 _WRAP_METHOD(void invalidate_filter(), gtk_list_box_invalidate_filter)
240 _WRAP_METHOD(void invalidate_sort(), gtk_list_box_invalidate_sort)
241 _WRAP_METHOD(void invalidate_headers(), gtk_list_box_invalidate_headers)
242 _WRAP_METHOD(void set_activate_on_single_click(bool single = true), gtk_list_box_set_activate_on_single_click)
243 _WRAP_METHOD(bool get_activate_on_single_click() const, gtk_list_box_get_activate_on_single_click)
244 _WRAP_METHOD(void drag_unhighlight_row(), gtk_list_box_drag_unhighlight_row)
245 _WRAP_METHOD(void drag_highlight_row(ListBoxRow& row), gtk_list_box_drag_highlight_row)
246
247 /** For instance:
248 * Gtk::Widget* on_create_widget(const Glib::RefPtr<T_item>& item);
249 *
250 * Called for list boxes that are bound to a Gio::ListModel with bind_model()
251 * or bind_list_store() for each item that gets added to the model.
252 *
253 * Versions of GTK+ prior to 3.18 called show_all() on the rows
254 * created by the SlotCreateWidget, but this forced all widgets
255 * inside the row to be shown, and is no longer the case. Applications should
256 * be updated to show the desired row widgets.
257 *
258 * @newin{3,22}
259 *
260 * @tparam T_item Base class of the items in the Gio::ListModel. All items must
261 * be of type T_item or a type derived from T_item.
262 * T_item must be Glib::Object or a type derived from Glib::Object.
263 * @param item The item from the model for which to create a widget.
264 * @eturn A Gtk::Widget that represents @a item.
265 */
266 template <typename T_item>
267 using SlotCreateWidget = sigc::slot<Gtk::Widget*, const Glib::RefPtr<T_item>&>;
268
269 /** Binds a Gio::ListModel.
270 *
271 * If this ListBox was already bound to a model, that previous binding is
272 * destroyed.
273 *
274 * The contents of the ListBox are cleared and then filled with widgets that
275 * represent items from @a model. The ListBox is updated whenever @a model changes.
276 * If @a model is an empty Glib::RefPtr, the ListBox is left empty.
277 *
278 * It is undefined to add or remove widgets directly (for example, with
279 * insert() or Gtk::Container::add()) while the ListBox is bound to a model.
280 *
281 * Note that using a model is incompatible with the filtering and sorting
282 * functionality in ListBox. When using a model, filtering and sorting
283 * should be implemented by the model.
284 *
285 * @newin{3,22}
286 *
287 * @param model The Gio::ListModel to be bound.
288 * @param slot_create_widget A slot that creates widgets for items.
289 *
290 * @see bind_list_store()
291 */
292 void bind_model(const Glib::RefPtr<Gio::ListModel>& model,
293 const SlotCreateWidget<Glib::Object>& slot_create_widget);
294 _IGNORE(gtk_list_box_bind_model)
295
296 /** Binds a Gio::ListStore<>.
297 *
298 * If this ListBox was already bound to a Gio::ListModel, that previous binding is
299 * destroyed. (Gio::ListStore is a Gio::ListModel.)
300 *
301 * The contents of the ListBox are cleared and then filled with widgets that
302 * represent items from @a store. The ListBox is updated whenever @a store changes.
303 * If @a store is an empty Glib::RefPtr, the ListBox is left empty.
304 *
305 * It is undefined to add or remove widgets directly (for example, with
306 * insert() or Gtk::Container::add()) while the ListBox is bound to a model.
307 *
308 * Note that using a model is incompatible with the filtering and sorting
309 * functionality in ListBox. When using a model, filtering and sorting
310 * should be implemented by the model.
311 *
312 * @newin{3,22}
313 *
314 * @tparam T_item Base class of the items in the Gio::ListStore. All items must
315 * be of type T_item or a type derived from T_item.
316 * T_item must be Glib::Object or a type derived from Glib::Object.
317 * @tparam T_slot SlotCreateWidget<T_item> or a type that can be converted
318 * to SlotCreateWidget<T_item>.
319 * @param store The Gio::ListStore<> to be bound.
320 * @param slot_create_widget A slot that creates widgets for items.
321 */
322 template <typename T_item, typename T_slot>
323 void bind_list_store(const Glib::RefPtr<Gio::ListStore<T_item>>& store,
324 T_slot&& slot_create_widget);
325
326 _WRAP_PROPERTY("selection-mode", SelectionMode)
327 _WRAP_PROPERTY("activate-on-single-click", bool)
328
329 _WRAP_SIGNAL(void row_selected(ListBoxRow* row), row-selected)
330 _WRAP_SIGNAL(void row_activated(ListBoxRow* row), row-activated)
331
332 //TODO: Remove no_default_handler when we can break ABI.
333 _WRAP_SIGNAL(void selected_rows_changed(), selected-rows-changed, no_default_handler)
334
335 _IGNORE_SIGNAL(activate-cursor-row, toggle-cursor-row, move-cursor, select-all, unselect-all) // Action signals
336
337 private:
338 template <typename T_item>
339 static GtkWidget* proxy_bind_list_store_create_widget_callback(void* item, void* data);
340 };
341
342 #ifndef DOXYGEN_SHOULD_SKIP_THIS
343
344 template <typename T_item, typename T_slot>
345 void ListBox::bind_list_store(const Glib::RefPtr<Gio::ListStore<T_item>>& store,
346 T_slot&& slot_create_widget)
347 {
348 // Create a copy of the slot.
349 // It will be deleted by Glib::destroy_notify_delete<SlotCreateWidget>.
350 auto slot_copy = new SlotCreateWidget<T_item>(std::forward<T_slot>(slot_create_widget));
351
352 gtk_list_box_bind_model(gobj(),
353 Glib::unwrap(Glib::RefPtr<Gio::ListModel>::cast_static(store)),
354 &proxy_bind_list_store_create_widget_callback<T_item>,
355 slot_copy, &Glib::destroy_notify_delete<SlotCreateWidget<T_item>>);
356 }
357
358 template <typename T_item>
359 GtkWidget* ListBox::proxy_bind_list_store_create_widget_callback(void* item, void* data)
360 {
361 auto& slot = *static_cast<SlotCreateWidget<T_item>*>(data);
362 auto cobject = static_cast<typename T_item::BaseObjectType*>(item);
363
364 try
365 {
366 // cast_dynamic is necessary if T_item is a user-defined type, such as
367 // class MyObject : public Glib::Object
368 // take_copy is true here, because wrap() returns a Glib::RefPtr<>.
369 // cobject will be unreferenced when the RefPtr is deleted.
370 Gtk::Widget* widget = slot(Glib::RefPtr<T_item>::cast_dynamic(Glib::wrap(cobject, true)));
371 if (widget)
372 return widget->gobj();
373 }
374 catch(...)
375 {
376 Glib::exception_handlers_invoke();
377 }
378 return nullptr;
379 }
380
381 #endif // DOXYGEN_SHOULD_SKIP_THIS
382
383 } // namespace Gtk