Debian Codegraph @

gtk/src/treeview.ccg

raw
1 /* Copyright (C) 2002 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 <https://www.gnu.org/licenses/>.
15 */
16
17 #include <glibmm/vectorutils.h>
18
19 #include <gtkmm/treeviewcolumn.h>
20 #include <gtkmm/treeview_private.h>
21 #include <gtkmm/treemodel.h>
22 #include <gtkmm/treemodelfilter.h>
23 #include <gtkmm/treemodelsort.h>
24 #include <gtkmm/entry.h>
25 #include <gtk/gtk.h>
26
27 namespace
28 {
29
30 //This target name is used in the GTK+ implementation:
31 static const char treeview_target_row[] = "GTK_TREE_MODEL_ROW";
32
33 } // anonymous namespace
34
35
36 static void SignalProxy_Mapping_gtk_callback(GtkTreeView* tree_view, GtkTreePath* path, void* data)
37 {
38 auto the_slot = static_cast<Gtk::TreeView::SlotMapping*>(data);
39
40 try
41 {
42 (*the_slot)(Glib::wrap(tree_view), Gtk::TreePath(path, true));
43 }
44 catch(...)
45 {
46 Glib::exception_handlers_invoke();
47 }
48 }
49
50 static gboolean SignalProxy_SearchEqual_gtk_callback(GtkTreeModel* model, int column, const char* key,
51 GtkTreeIter* iter, void* data)
52 {
53 auto the_slot = static_cast<Gtk::TreeView::SlotSearchEqual*>(data);
54
55 try
56 {
57 return (*the_slot)(Glib::wrap(model, true), column, key, Gtk::TreeIter(model, iter));
58 }
59 catch(...)
60 {
61 Glib::exception_handlers_invoke();
62 }
63
64 return 0; // arbitrary value
65 }
66
67 static void SignalProxy_SearchEqual_gtk_callback_destroy(void* data)
68 {
69 delete static_cast<Gtk::TreeView::SlotSearchEqual*>(data);
70 }
71
72 static gboolean SignalProxy_ColumnDrop_gtk_callback(GtkTreeView* tree_view, GtkTreeViewColumn* column,
73 GtkTreeViewColumn* prev_column,
74 GtkTreeViewColumn* next_column, void* data)
75 {
76 auto the_slot = static_cast<Gtk::TreeView::SlotColumnDrop*>(data);
77
78 try
79 {
80 return (*the_slot)(Glib::wrap(tree_view), Glib::wrap(column),
81 Glib::wrap(prev_column), Glib::wrap(next_column));
82 }
83 catch(...)
84 {
85 Glib::exception_handlers_invoke();
86 }
87
88 return 0; // arbitrary value
89 }
90
91 static void SignalProxy_ColumnDrop_gtk_callback_destroy(void* data)
92 {
93 delete static_cast<Gtk::TreeView::SlotColumnDrop*>(data);
94 }
95
96
97 static void SignalProxy_SearchPosition_gtk_callback(GtkTreeView* /* tree_view */, GtkWidget* search_dialog, gpointer user_data)
98 {
99 auto the_slot = static_cast<Gtk::TreeView::SlotSearchPosition*>(user_data);
100
101 try
102 {
103 (*the_slot)(Glib::wrap(search_dialog));
104 }
105 catch(...)
106 {
107 Glib::exception_handlers_invoke();
108 }
109 }
110
111 static void SignalProxy_SearchPosition_gtk_callback_destroy(void* data)
112 {
113 delete static_cast<Gtk::TreeView::SlotSearchPosition*>(data);
114 }
115
116 namespace Gtk
117 {
118
119 int TreeView::insert_column_with_data_func(int position, const Glib::ustring& title, CellRenderer& cell, const SlotTreeCellData& slot)
120 {
121 //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
122 //It will be deleted when TreeView_Private::SignalProxy_CellData_gtk_callback_destroy() is called.
123 auto slot_copy = new SlotTreeCellData(slot);
124
125 return gtk_tree_view_insert_column_with_data_func(
126 gobj(), position, title.c_str(), cell.gobj(),
127 &TreeView_Private::SignalProxy_CellData_gtk_callback, slot_copy,
128 &TreeView_Private::SignalProxy_CellData_gtk_callback_destroy);
129 }
130
131 void TreeView::set_cursor(const TreeModel::Path& path)
132 {
133 gtk_tree_view_set_cursor(gobj(), const_cast<GtkTreePath*>(path.gobj()), nullptr, false);
134 }
135
136 void TreeView::get_cursor(TreeModel::Path& path, TreeViewColumn*& focus_column)
137 {
138 GtkTreePath* pTreePath = nullptr;
139 GtkTreeViewColumn* pTreeViewColumn = nullptr;
140 gtk_tree_view_get_cursor(gobj(), &pTreePath, &pTreeViewColumn);
141
142 path = TreeModel::Path(pTreePath, false); /* Use the existing underlying GtkTreePath instance without copying and freeing, because gtk_tree_view_get_cursor() gives us ownernship. */
143 focus_column = Glib::wrap(pTreeViewColumn);
144 }
145
146
147 void TreeView::enable_model_drag_source(const std::vector<TargetEntry>& targets,
148 Gdk::ModifierType start_button_mask,
149 Gdk::DragAction actions)
150 {
151 gtk_tree_view_enable_model_drag_source(
152 gobj(), (GdkModifierType) start_button_mask,
153 Glib::ArrayHandler<TargetEntry, TargetEntryTraits>::vector_to_array(targets).data(),
154 targets.size(), (GdkDragAction) actions);
155 }
156
157 void TreeView::enable_model_drag_source(Gdk::ModifierType start_button_mask, Gdk::DragAction actions)
158 {
159 std::vector<TargetEntry> targets (1, TargetEntry (treeview_target_row));
160
161 enable_model_drag_source(targets, start_button_mask, actions);
162 }
163
164 void TreeView::enable_model_drag_dest(const std::vector<TargetEntry>& targets, Gdk::DragAction actions)
165 {
166 gtk_tree_view_enable_model_drag_dest(
167 gobj(), Glib::ArrayHandler<TargetEntry, TargetEntryTraits>::vector_to_array(targets).data(),
168 targets.size(), (GdkDragAction) actions);
169 }
170
171 void TreeView::enable_model_drag_dest(Gdk::DragAction actions)
172 {
173 std::vector<TargetEntry> targets (1, TargetEntry (treeview_target_row));
174
175 enable_model_drag_dest(targets, actions);
176 }
177
178 bool TreeView::get_path_at_pos(int x, int y, TreeModel::Path& path, TreeViewColumn*& column, int& cell_x, int& cell_y) const
179 {
180 GtkTreePath* pTreePath = nullptr;
181 GtkTreeViewColumn* pTreeViewColumn = nullptr;
182 const bool result = gtk_tree_view_get_path_at_pos(const_cast<GtkTreeView*>(gobj()), x, y, &pTreePath, &pTreeViewColumn, &cell_x, &cell_y);
183
184 path = TreeModel::Path(pTreePath, false /* don't take a copy, because the gtk_tree_view_get_path_at_pos() docs say that we must free the path */ );
185 column = Glib::wrap(pTreeViewColumn);
186 return result;
187 }
188
189 bool TreeView::get_path_at_pos(int x, int y, TreeModel::Path& path) const
190 {
191 GtkTreePath* pTreePath = nullptr;
192 const bool result = gtk_tree_view_get_path_at_pos(const_cast<GtkTreeView*>(gobj()), x, y, &pTreePath, nullptr, nullptr, nullptr);
193
194 path = TreeModel::Path(pTreePath, false /* don't take a copy, because the gtk_tree_view_get_path_at_pos() docs say that we must free the path */ );
195 return result;
196 }
197
198 int TreeView::insert_column(const Glib::ustring& title, CellRenderer& cell, int position)
199 {
200 return gtk_tree_view_insert_column_with_attributes(
201 gobj(), position, const_cast<char*>(title.c_str()), cell.gobj(), nullptr);
202 }
203
204 int TreeView::append_column(const Glib::ustring& title, CellRenderer& cell)
205 {
206 return insert_column(title, cell, -1 /* at the end */);
207 }
208
209 void TreeView::get_drag_dest_row(TreeModel::Path& path, TreeViewDropPosition& pos) const
210 {
211 GtkTreePath* pTreePath = nullptr;
212 gtk_tree_view_get_drag_dest_row(const_cast<GtkTreeView*>(gobj()), &pTreePath, (GtkTreeViewDropPosition*) &pos);
213 path = TreeModel::Path(pTreePath, true); //true = take_copy.
214 }
215
216 bool TreeView::get_dest_row_at_pos(int drag_x, int drag_y, TreeModel::Path& path, TreeViewDropPosition& pos) const
217 {
218 GtkTreePath* pTreePath = nullptr;
219 const bool bResult = gtk_tree_view_get_dest_row_at_pos(
220 const_cast<GtkTreeView*>(gobj()), drag_x, drag_y, &pTreePath, (GtkTreeViewDropPosition*) &pos);
221
222 path = TreeModel::Path(pTreePath, true); //true = take_copy.
223 return bResult;
224 }
225
226 void TreeView::map_expanded_rows(const SlotMapping& slot)
227 {
228 gtk_tree_view_map_expanded_rows(gobj(), &SignalProxy_Mapping_gtk_callback, const_cast<SlotMapping*>(&slot));
229 }
230
231 void TreeView::set_search_equal_func(const SlotSearchEqual& slot)
232 {
233 //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
234 //It will be deleted when SignalProxy_SearchEqual_gtk_callback_destroy() is called.
235 auto slot_copy = new SlotSearchEqual(slot);
236
237 gtk_tree_view_set_search_equal_func(gobj(),
238 &SignalProxy_SearchEqual_gtk_callback, slot_copy,
239 &SignalProxy_SearchEqual_gtk_callback_destroy);
240 }
241
242 void TreeView::set_column_drag_function(const SlotColumnDrop& slot)
243 {
244 //Create a copt of the slot. A pointer to this will be passed through the callback's data parameter.
245 //It will be deleted when SignalProxy_ColumnDrop_gtk_callback_destroy() is called.
246 auto slot_copy = new SlotColumnDrop(slot);
247
248 gtk_tree_view_set_column_drag_function(gobj(),
249 &SignalProxy_ColumnDrop_gtk_callback, slot_copy,
250 &SignalProxy_ColumnDrop_gtk_callback_destroy);
251
252 }
253
254 void TreeView::unset_column_drag_function()
255 {
256 gtk_tree_view_set_column_drag_function(gobj(),
257 nullptr, nullptr, nullptr); /* See GTK+ docs about the nullptrs. */
258 }
259
260 void TreeView::scroll_to_cell(const TreeModel::Path& path, TreeViewColumn& column, float row_align, float col_align)
261 {
262 gtk_tree_view_scroll_to_cell(gobj(), const_cast<GtkTreePath*>(path.gobj()), column.gobj(), TRUE, row_align, col_align);
263 }
264
265 void TreeView::scroll_to_cell(const TreeModel::Path& path, TreeViewColumn& column)
266 {
267 gtk_tree_view_scroll_to_cell(gobj(), const_cast<GtkTreePath*>(path.gobj()), column.gobj(), FALSE, 0.0, 0.0);
268 }
269
270 void TreeView::scroll_to_row(const TreeModel::Path& path, float row_align)
271 {
272 gtk_tree_view_scroll_to_cell(gobj(), const_cast<GtkTreePath*>(path.gobj()), nullptr, TRUE, row_align, 0.0);
273 }
274
275 void TreeView::scroll_to_row(const TreeModel::Path& path)
276 {
277 gtk_tree_view_scroll_to_cell(gobj(), const_cast<GtkTreePath*>(path.gobj()), nullptr, FALSE, 0.0, 0.0);
278 }
279
280 void TreeView::scroll_to_column(TreeViewColumn& column, float col_align)
281 {
282 gtk_tree_view_scroll_to_cell(gobj(), nullptr, column.gobj(), TRUE, 0.0, col_align);
283 }
284
285 void TreeView::scroll_to_column(TreeViewColumn& column)
286 {
287 gtk_tree_view_scroll_to_cell(gobj(), nullptr, column.gobj(), FALSE, 0.0, 0.0);
288 }
289
290 void TreeView::remove_all_columns()
291 {
292 //This method is not in GTK+, but it seems useful.
293
294 //Remove all View columns:
295 std::vector<Gtk::TreeView::Column*> vecViewColumns (get_columns());
296
297 for (std::vector<Gtk::TreeView::Column*>::iterator iter (vecViewColumns.begin ()), columns_end (vecViewColumns.end ());
298 iter != columns_end;
299 ++iter)
300 {
301 Gtk::TreeView::Column* pViewColumn (*iter);
302
303 if(pViewColumn)
304 {
305 remove_column(*pViewColumn);
306 }
307 }
308 }
309
310
311 CellRenderer* TreeView::get_column_cell_renderer(int n)
312 {
313 auto pColumn = get_column(n);
314 if(pColumn)
315 return pColumn->get_first_cell();
316 else
317 return nullptr;
318 }
319
320
321 const CellRenderer* TreeView::get_column_cell_renderer(int n) const
322 {
323 //Do some const_cast-ing to avoid repetition of code:
324 auto pRenderer = const_cast<TreeView*>(this)->get_column_cell_renderer(n);
325 return pRenderer;
326 }
327
328 void TreeView::reset_expander_column()
329 {
330 gtk_tree_view_set_expander_column(gobj(), nullptr /* see C docs */);
331 }
332
333 void TreeView::_auto_store_on_cellrenderer_toggle_edited(const Glib::ustring& path_string,
334 int model_column)
335 {
336 _auto_store_on_cellrenderer_toggle_edited_with_model(path_string, model_column, get_model());
337 }
338
339 void TreeView::_auto_store_on_cellrenderer_toggle_edited_with_model(const Glib::ustring& path_string,
340 int model_column, const Glib::RefPtr<Gtk::TreeModel>& model)
341 {
342 Gtk::TreePath path (path_string);
343
344 //Get the row from the path:
345 if(model)
346 {
347 auto iter = model->get_iter(path);
348 if(iter)
349 {
350 auto row = *iter;
351
352 //Get the new value:
353 //This seems to get the old value, not the new one,
354 //so we will just NOT the model value ourselves.
355 //bool bActive = cell_renderer->get_active();
356 bool bActive = false;
357 row.get_value(model_column, bActive);
358 bActive = !bActive;
359
360 //Store the user's new text in the model:
361 row.set_value(model_column, bActive);
362 }
363 }
364 }
365
366 void TreeView::move_column_to_start(TreeViewColumn& column)
367 {
368 gtk_tree_view_move_column_after(gobj(), (column).gobj(), nullptr /* See C docs */);
369 }
370
371 void TreeView::set_row_separator_func(const SlotRowSeparator& slot)
372 {
373 //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
374 //It will be deleted when SignalProxy_RowSeparator_gtk_callback_destroy() is called.
375 auto slot_copy = new SlotRowSeparator(slot);
376
377 gtk_tree_view_set_row_separator_func(gobj(),
378 &TreeView_Private::SignalProxy_RowSeparator_gtk_callback, slot_copy,
379 &TreeView_Private::SignalProxy_RowSeparator_gtk_callback_destroy);
380 }
381
382 void TreeView::set_search_position_func(const SlotSearchPosition& slot)
383 {
384 //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
385 //It will be deleted when SignalProxy_SearchPosition_gtk_callback_destroy() is called.
386 auto slot_copy = new SlotSearchPosition(slot);
387
388 gtk_tree_view_set_search_position_func(gobj(),
389 &SignalProxy_SearchPosition_gtk_callback, slot_copy,
390 &SignalProxy_SearchPosition_gtk_callback_destroy);
391 }
392
393 bool TreeView::get_visible_range(TreeModel::Path& start_path, TreeModel::Path& end_path) const
394 {
395 GtkTreePath* pTreePathStart = nullptr;
396 GtkTreePath* pTreePathEnd = nullptr;
397 bool result = gtk_tree_view_get_visible_range(const_cast<GtkTreeView*>(gobj()), &pTreePathStart, &pTreePathEnd);
398
399 start_path = TreeModel::Path(pTreePathStart, false /* don't take a copy, because the gtk_tree_view_get_visible_range() docs say that we must free the path */ );
400 end_path = TreeModel::Path(pTreePathEnd, false /* don't take a copy, because the gtk_tree_view_get_visible_range() docs say that we must free the path */ );
401 return result;
402 }
403
404 void TreeView::unset_model()
405 {
406 gtk_tree_view_set_model(gobj(), nullptr);
407 }
408
409 bool
410 TreeView::get_tooltip_context_path(int& x, int& y,
411 bool keyboard_tip,
412 TreeModel::Path& path)
413 {
414 GtkTreePath* cpath = nullptr;
415
416 gboolean result =
417 gtk_tree_view_get_tooltip_context(gobj(),
418 &x, &y,
419 keyboard_tip,
420 nullptr,
421 &cpath,
422 nullptr);
423
424 //If result is false, cpath is not modified, it's still nullptr.
425 //wrap() can handle that situation.
426 path = Glib::wrap(cpath, false /* take_copy=false */);
427
428 return result;
429 }
430
431 bool
432 TreeView::get_tooltip_context_iter(int& x, int& y,
433 bool keyboard_tip,
434 Gtk::TreeModel::iterator& iter)
435 {
436 GtkTreeIter src_iter;
437
438 gboolean result =
439 gtk_tree_view_get_tooltip_context(gobj(),
440 &x, &y,
441 keyboard_tip,
442 nullptr,
443 nullptr,
444 &src_iter);
445
446 iter = TreeIter(gtk_tree_view_get_model(this->gobj()), &src_iter);
447
448 return result;
449 }
450
451 bool TreeView::is_blank_at_pos(int x, int y, TreePath& path, TreeViewColumn*& column, int& cell_x, int& cell_y) const
452 {
453 GtkTreePath* cpath = nullptr;
454 GtkTreeViewColumn* pcolumn = nullptr;
455 const bool result = gtk_tree_view_is_blank_at_pos(const_cast<GtkTreeView*>(gobj()), x, y, &cpath, &pcolumn, &(cell_x), &(cell_y));
456
457 path = TreePath(cpath, false /* take ownership instead of taking a copy */);
458 column = Glib::wrap(pcolumn);
459 return result;
460 }
461
462 bool TreeView::is_blank_at_pos(int x, int y) const
463 {
464 return gtk_tree_view_is_blank_at_pos(const_cast<GtkTreeView*>(gobj()), x, y, nullptr, nullptr, nullptr, nullptr);
465 }
466
467 void TreeView::unset_drag_dest_row()
468 {
469 // The C docs specify that a 0 path means unset.
470 gtk_tree_view_set_drag_dest_row(gobj(), nullptr, GTK_TREE_VIEW_DROP_BEFORE /* Arbitrary. Ignored anyway. */);
471 }
472
473 void TreeView::unset_row_separator_func()
474 {
475 gtk_tree_view_set_row_separator_func(gobj(), nullptr, nullptr, nullptr);
476 }
477
478 } // namespace Gtk