| 1 |
/* Copyright (C) 1998-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/treeview.h> |
| 20 |
#include <gtk/gtk.h> |
| 21 |
|
| 22 |
|
| 23 |
static void proxy_foreach_selection_iter_callback(GtkTreeModel* model, GtkTreePath*, GtkTreeIter* iter, void* data) |
| 24 |
{ |
| 25 |
typedef Gtk::TreeSelection::SlotForeachIter SlotType; |
| 26 |
auto& slot = *static_cast<SlotType*>(data); |
| 27 |
|
| 28 |
try |
| 29 |
{ |
| 30 |
slot(Gtk::TreeModel::iterator(model, iter)); |
| 31 |
} |
| 32 |
catch(...) |
| 33 |
{ |
| 34 |
Glib::exception_handlers_invoke(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
static void proxy_foreach_selection_path_callback(GtkTreeModel*, GtkTreePath* path, GtkTreeIter*, void* data) |
| 39 |
{ |
| 40 |
typedef Gtk::TreeSelection::SlotForeachPath SlotType; |
| 41 |
auto& slot = *static_cast<SlotType*>(data); |
| 42 |
|
| 43 |
try |
| 44 |
{ |
| 45 |
slot(Gtk::TreeModel::Path(path, true)); |
| 46 |
} |
| 47 |
catch(...) |
| 48 |
{ |
| 49 |
Glib::exception_handlers_invoke(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static void proxy_foreach_selection_path_and_iter_callback(GtkTreeModel* model, GtkTreePath* path, |
| 54 |
GtkTreeIter* iter, void* data) |
| 55 |
{ |
| 56 |
typedef Gtk::TreeSelection::SlotForeachPathAndIter SlotType; |
| 57 |
auto& slot = *static_cast<SlotType*>(data); |
| 58 |
|
| 59 |
try |
| 60 |
{ |
| 61 |
slot(Gtk::TreeModel::Path(path, true), Gtk::TreeModel::iterator(model, iter)); |
| 62 |
} |
| 63 |
catch(...) |
| 64 |
{ |
| 65 |
Glib::exception_handlers_invoke(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static gboolean SignalProxy_Select_gtk_callback(GtkTreeSelection*, GtkTreeModel* model, GtkTreePath* path, |
| 71 |
gboolean path_currently_selected, void* data) |
| 72 |
{ |
| 73 |
const auto the_slot = static_cast<Gtk::TreeSelection::SlotSelect*>(data); |
| 74 |
|
| 75 |
try |
| 76 |
{ |
| 77 |
return (*the_slot)(Glib::wrap(model, true), // take copy |
| 78 |
Gtk::TreePath(path, true), // take copy |
| 79 |
path_currently_selected); |
| 80 |
} |
| 81 |
catch(...) |
| 82 |
{ |
| 83 |
Glib::exception_handlers_invoke(); |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static void SignalProxy_Select_gtk_callback_destroy(void* data) |
| 90 |
{ |
| 91 |
delete static_cast<Gtk::TreeSelection::SlotSelect*>(data); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
namespace Gtk |
| 96 |
{ |
| 97 |
|
| 98 |
void TreeSelection::set_select_function(const SlotSelect& slot) |
| 99 |
{ |
| 100 |
// Create a copy of the slot. A pointer to this will be passed |
| 101 |
// through the callback's data parameter. It will be deleted |
| 102 |
// when SignalProxy_Select_gtk_callback_destroy() is called. |
| 103 |
auto slot_copy = new SlotSelect(slot); |
| 104 |
|
| 105 |
gtk_tree_selection_set_select_function(gobj(), |
| 106 |
&SignalProxy_Select_gtk_callback, slot_copy, |
| 107 |
&SignalProxy_Select_gtk_callback_destroy); |
| 108 |
} |
| 109 |
|
| 110 |
Glib::RefPtr<TreeModel> TreeSelection::get_model() |
| 111 |
{ |
| 112 |
const auto tree_view = gtk_tree_selection_get_tree_view(gobj()); |
| 113 |
return Glib::wrap(gtk_tree_view_get_model(tree_view), true); |
| 114 |
} |
| 115 |
|
| 116 |
Glib::RefPtr<const TreeModel> TreeSelection::get_model() const |
| 117 |
{ |
| 118 |
const auto tree_view = gtk_tree_selection_get_tree_view(const_cast<GtkTreeSelection*>(gobj())); |
| 119 |
return Glib::wrap(gtk_tree_view_get_model(tree_view), true); |
| 120 |
} |
| 121 |
|
| 122 |
TreeModel::iterator TreeSelection::get_selected() |
| 123 |
{ |
| 124 |
TreeModel::iterator iter; |
| 125 |
GtkTreeModel* model_gobject = nullptr; |
| 126 |
|
| 127 |
gtk_tree_selection_get_selected(gobj(), &model_gobject, iter.gobj()); |
| 128 |
|
| 129 |
iter.set_model_gobject(model_gobject); |
| 130 |
return iter; |
| 131 |
} |
| 132 |
|
| 133 |
TreeModel::iterator TreeSelection::get_selected(Glib::RefPtr<TreeModel>& model) |
| 134 |
{ |
| 135 |
TreeModel::iterator iter; |
| 136 |
GtkTreeModel* model_gobject = nullptr; |
| 137 |
|
| 138 |
gtk_tree_selection_get_selected(gobj(), &model_gobject, iter.gobj()); |
| 139 |
|
| 140 |
model = Glib::wrap(model_gobject, true); |
| 141 |
|
| 142 |
iter.set_model_refptr(model); |
| 143 |
return iter; |
| 144 |
} |
| 145 |
|
| 146 |
void TreeSelection::selected_foreach_iter(const SlotForeachIter& slot) const |
| 147 |
{ |
| 148 |
SlotForeachIter slot_copy (slot); |
| 149 |
gtk_tree_selection_selected_foreach(const_cast<GtkTreeSelection*>(gobj()), &proxy_foreach_selection_iter_callback, &slot_copy); |
| 150 |
} |
| 151 |
|
| 152 |
void TreeSelection::selected_foreach_path(const SlotForeachPath& slot) const |
| 153 |
{ |
| 154 |
SlotForeachPath slot_copy (slot); |
| 155 |
gtk_tree_selection_selected_foreach(const_cast<GtkTreeSelection*>(gobj()), &proxy_foreach_selection_path_callback, &slot_copy); |
| 156 |
} |
| 157 |
|
| 158 |
void TreeSelection::selected_foreach(const SlotForeachPathAndIter& slot) const |
| 159 |
{ |
| 160 |
SlotForeachPathAndIter slot_copy (slot); |
| 161 |
gtk_tree_selection_selected_foreach(const_cast<GtkTreeSelection*>(gobj()), &proxy_foreach_selection_path_and_iter_callback, &slot_copy); |
| 162 |
} |
| 163 |
|
| 164 |
std::vector<TreeModel::Path> TreeSelection::get_selected_rows() const |
| 165 |
{ |
| 166 |
return Glib::ListHandler<TreeModel::Path, TreePathTraits>::list_to_vector(gtk_tree_selection_get_selected_rows( |
| 167 |
const_cast<GtkTreeSelection*>(gobj()), nullptr), Glib::OWNERSHIP_DEEP); |
| 168 |
} |
| 169 |
|
| 170 |
std::vector<TreeModel::Path> TreeSelection::get_selected_rows(Glib::RefPtr<TreeModel>& model) |
| 171 |
{ |
| 172 |
GtkTreeModel* model_gobject = nullptr; |
| 173 |
|
| 174 |
const std::vector<TreeModel::Path> result (Glib::ListHandler<TreeModel::Path, TreePathTraits>::list_to_vector(gtk_tree_selection_get_selected_rows( |
| 175 |
const_cast<GtkTreeSelection*>(gobj()), &model_gobject), Glib::OWNERSHIP_DEEP)); |
| 176 |
|
| 177 |
model = Glib::wrap(model_gobject, true); |
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
} // namespace Gtk |