| 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 <gtkmm/treemodel.h> |
| 18 |
#include <gtk/gtk.h> |
| 19 |
|
| 20 |
|
| 21 |
namespace Gtk |
| 22 |
{ |
| 23 |
|
| 24 |
/**** Gtk::TreeIter ********************************************************/ |
| 25 |
|
| 26 |
TreeIter::TreeIter() |
| 27 |
: |
| 28 |
TreeIterBase(), |
| 29 |
model_ (nullptr), |
| 30 |
is_end_ (false) |
| 31 |
{} |
| 32 |
|
| 33 |
TreeIter::TreeIter(TreeModel* model) |
| 34 |
: |
| 35 |
TreeIterBase(), |
| 36 |
model_ (model), |
| 37 |
is_end_ (false) |
| 38 |
{} |
| 39 |
|
| 40 |
TreeIter::TreeIter(GtkTreeModel* model, const GtkTreeIter* iter) |
| 41 |
: |
| 42 |
TreeIterBase(iter), |
| 43 |
model_ (dynamic_cast<TreeModel*>(Glib::wrap_auto((GObject*) model))), |
| 44 |
is_end_ (iter == nullptr) |
| 45 |
{} |
| 46 |
|
| 47 |
TreeIter& TreeIter::operator++() |
| 48 |
{ |
| 49 |
g_assert(!is_end_); |
| 50 |
|
| 51 |
auto previous = gobject_; |
| 52 |
|
| 53 |
if(!gtk_tree_model_iter_next(model_->gobj(), &gobject_)) |
| 54 |
{ |
| 55 |
is_end_ = true; |
| 56 |
gtk_tree_model_iter_parent(Glib::unwrap(model_), &gobject_, &previous); |
| 57 |
} |
| 58 |
|
| 59 |
return *this; |
| 60 |
} |
| 61 |
|
| 62 |
const TreeIter TreeIter::operator++(int) |
| 63 |
{ |
| 64 |
g_assert(!is_end_); |
| 65 |
|
| 66 |
TreeIter previous (*this); |
| 67 |
|
| 68 |
if(!gtk_tree_model_iter_next(model_->gobj(), &gobject_)) |
| 69 |
{ |
| 70 |
is_end_ = true; |
| 71 |
gtk_tree_model_iter_parent(Glib::unwrap(model_), &gobject_, &previous.gobject_); |
| 72 |
} |
| 73 |
|
| 74 |
return previous; |
| 75 |
} |
| 76 |
|
| 77 |
TreeIter& TreeIter::operator--() |
| 78 |
{ |
| 79 |
if(!is_end_) |
| 80 |
{ |
| 81 |
gtk_tree_model_iter_previous(Glib::unwrap(model_), &gobject_); |
| 82 |
} |
| 83 |
else // --end yields last |
| 84 |
{ |
| 85 |
auto next = gobject_; |
| 86 |
const auto parent = (next.stamp != 0) ? &next : nullptr; |
| 87 |
|
| 88 |
const int index = gtk_tree_model_iter_n_children(Glib::unwrap(model_), parent) - 1; |
| 89 |
is_end_ = !gtk_tree_model_iter_nth_child(Glib::unwrap(model_), &gobject_, parent, index); |
| 90 |
|
| 91 |
g_assert(!is_end_); |
| 92 |
} |
| 93 |
|
| 94 |
return *this; |
| 95 |
} |
| 96 |
|
| 97 |
const TreeIter TreeIter::operator--(int) |
| 98 |
{ |
| 99 |
TreeIter next (*this); |
| 100 |
|
| 101 |
if(!is_end_) |
| 102 |
{ |
| 103 |
gtk_tree_model_iter_previous(Glib::unwrap(model_), &gobject_); |
| 104 |
} |
| 105 |
else // --end yields last |
| 106 |
{ |
| 107 |
const auto parent = (next.gobject_.stamp != 0) ? &next.gobject_ : nullptr; |
| 108 |
|
| 109 |
const int index = gtk_tree_model_iter_n_children(model_->gobj(), parent) - 1; |
| 110 |
is_end_ = !gtk_tree_model_iter_nth_child(model_->gobj(), &gobject_, parent, index); |
| 111 |
|
| 112 |
g_assert(!is_end_); |
| 113 |
} |
| 114 |
|
| 115 |
return next; |
| 116 |
} |
| 117 |
|
| 118 |
/* There is no public gtk_tree_iter_equal(), so we must write our own. |
| 119 |
*/ |
| 120 |
bool TreeIter::equal(const TreeIter& other) const |
| 121 |
{ |
| 122 |
g_assert(model_ == other.model_); |
| 123 |
|
| 124 |
// A GtkTreeIter has the same stamp value as its model. |
| 125 |
g_assert(gobject_.stamp == other.gobject_.stamp || is_end_ || other.is_end_); |
| 126 |
|
| 127 |
// If all user_data pointers are equal we can assume the iterators to be |
| 128 |
// equal. This should be safe since GtkTreeIter lacks destroy notification, |
| 129 |
// thus there is no way to attach more data fields to the iterator. |
| 130 |
return (is_end_ == other.is_end_) && |
| 131 |
(gobject_.user_data == other.gobject_.user_data) && |
| 132 |
(gobject_.user_data2 == other.gobject_.user_data2) && |
| 133 |
(gobject_.user_data3 == other.gobject_.user_data3); |
| 134 |
} |
| 135 |
|
| 136 |
_DEPRECATE_IFDEF_START |
| 137 |
TreeIter::operator const void*() const |
| 138 |
{ |
| 139 |
// Test whether the GtkTreeIter is valid and not an end iterator. This check |
| 140 |
// is almost the same as the private VALID_ITER() macro in gtkliststore.c and |
| 141 |
// gtktreestore.c. |
| 142 |
return (!is_end_ && gobject_.stamp) ? GINT_TO_POINTER(1) : nullptr; |
| 143 |
} |
| 144 |
_DEPRECATE_IFDEF_END |
| 145 |
|
| 146 |
TreeIter::operator bool() const |
| 147 |
{ |
| 148 |
// Test whether the GtkTreeIter is valid and not an end iterator. This check |
| 149 |
// is almost the same as the private VALID_ITER() macro in gtkliststore.c and |
| 150 |
// gtktreestore.c. |
| 151 |
return !is_end_ && gobject_.stamp; |
| 152 |
} |
| 153 |
|
| 154 |
void TreeIter::setup_end_iterator(const TreeIter& last_valid) |
| 155 |
{ |
| 156 |
g_assert(model_ == last_valid.model_); |
| 157 |
|
| 158 |
if(last_valid.is_end_) |
| 159 |
gobject_ = last_valid.gobject_; |
| 160 |
else |
| 161 |
gtk_tree_model_iter_parent(model_->gobj(), &gobject_, const_cast<GtkTreeIter*>(&last_valid.gobject_)); |
| 162 |
|
| 163 |
is_end_ = true; |
| 164 |
} |
| 165 |
|
| 166 |
void TreeIter::set_model_refptr(const Glib::RefPtr<TreeModel>& model) |
| 167 |
{ |
| 168 |
model_ = model.operator->(); |
| 169 |
} |
| 170 |
|
| 171 |
void TreeIter::set_model_gobject(GtkTreeModel* model) |
| 172 |
{ |
| 173 |
model_ = dynamic_cast<TreeModel*>(Glib::wrap_auto((GObject*) model)); |
| 174 |
} |
| 175 |
|
| 176 |
GtkTreeModel* TreeIter::get_model_gobject() const |
| 177 |
{ |
| 178 |
return (model_) ? model_->gobj() : nullptr; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
int TreeIter::get_stamp() const |
| 183 |
{ |
| 184 |
return gobj()->stamp; |
| 185 |
} |
| 186 |
|
| 187 |
void TreeIter::set_stamp(int stamp) |
| 188 |
{ |
| 189 |
gobj()->stamp = stamp; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/**** Gtk::TreeRow *********************************************************/ |
| 194 |
|
| 195 |
const TreeNodeChildren& TreeRow::children() const |
| 196 |
{ |
| 197 |
g_assert(!is_end_); |
| 198 |
|
| 199 |
return static_cast<const TreeNodeChildren&>(static_cast<const TreeIter&>(*this)); |
| 200 |
} |
| 201 |
|
| 202 |
TreeIter TreeRow::parent() const |
| 203 |
{ |
| 204 |
TreeIter iter (model_); |
| 205 |
|
| 206 |
if(is_end_) |
| 207 |
iter.gobject_ = gobject_; |
| 208 |
else |
| 209 |
gtk_tree_model_iter_parent(model_->gobj(), iter.gobj(), const_cast<GtkTreeIter*>(&gobject_)); |
| 210 |
|
| 211 |
return iter; |
| 212 |
} |
| 213 |
|
| 214 |
void TreeRow::set_value_impl(int column, const Glib::ValueBase& value) const |
| 215 |
{ |
| 216 |
model_->set_value_impl(*this, column, value); |
| 217 |
} |
| 218 |
|
| 219 |
void TreeRow::get_value_impl(int column, Glib::ValueBase& value) const |
| 220 |
{ |
| 221 |
model_->get_value_impl(*this, column, value); |
| 222 |
} |
| 223 |
|
| 224 |
_DEPRECATE_IFDEF_START |
| 225 |
TreeRow::operator const void*() const |
| 226 |
{ |
| 227 |
return TreeIter::operator const void*(); |
| 228 |
} |
| 229 |
_DEPRECATE_IFDEF_END |
| 230 |
|
| 231 |
TreeRow::operator bool() const |
| 232 |
{ |
| 233 |
return TreeIter::operator bool(); |
| 234 |
|
| 235 |
} |
| 236 |
/**** Gtk::TreeNodeChildren ************************************************/ |
| 237 |
|
| 238 |
TreeNodeChildren::iterator TreeNodeChildren::begin() |
| 239 |
{ |
| 240 |
iterator iter (model_); |
| 241 |
|
| 242 |
// If the iterator is invalid (stamp == 0), assume a 'virtual' toplevel |
| 243 |
// node. This behaviour is needed to implement Gtk::TreeModel::children(). |
| 244 |
|
| 245 |
if(gobject_.stamp != 0) |
| 246 |
{ |
| 247 |
if(!gtk_tree_model_iter_children(model_->gobj(), iter.gobj(), const_cast<GtkTreeIter*>(&gobject_))) |
| 248 |
{ |
| 249 |
// Assign the already known parent, in order to create an end iterator. |
| 250 |
iter.gobject_ = gobject_; |
| 251 |
iter.is_end_ = true; |
| 252 |
} |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
if(!gtk_tree_model_get_iter_first(model_->gobj(), iter.gobj())) |
| 257 |
{ |
| 258 |
// No need to copy the GtkTreeIter, since iter.gobject_ is already empty. |
| 259 |
iter.is_end_ = true; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return iter; |
| 264 |
} |
| 265 |
|
| 266 |
TreeNodeChildren::const_iterator TreeNodeChildren::begin() const |
| 267 |
{ |
| 268 |
//TODO: Reduce the copy/paste from the non-const begin()? |
| 269 |
|
| 270 |
const_iterator iter (model_); |
| 271 |
|
| 272 |
// If the iterator is invalid (stamp == 0), assume a 'virtual' toplevel |
| 273 |
// node. This behaviour is needed to implement Gtk::TreeModel::children(). |
| 274 |
|
| 275 |
if(gobject_.stamp != 0) |
| 276 |
{ |
| 277 |
if(!gtk_tree_model_iter_children(model_->gobj(), iter.gobj(), const_cast<GtkTreeIter*>(&gobject_))) |
| 278 |
{ |
| 279 |
// Assign the already known parent, in order to create an end iterator. |
| 280 |
iter.gobject_ = gobject_; |
| 281 |
iter.is_end_ = true; |
| 282 |
} |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
if(!gtk_tree_model_get_iter_first(model_->gobj(), iter.gobj())) |
| 287 |
{ |
| 288 |
// No need to copy the GtkTreeIter, since iter.gobject_ is already empty. |
| 289 |
iter.is_end_ = true; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return iter; |
| 294 |
} |
| 295 |
|
| 296 |
TreeNodeChildren::iterator TreeNodeChildren::end() |
| 297 |
{ |
| 298 |
// Just copy the parent, and turn it into an end iterator. |
| 299 |
iterator iter (*this); |
| 300 |
iter.is_end_ = true; |
| 301 |
return iter; |
| 302 |
} |
| 303 |
|
| 304 |
TreeNodeChildren::const_iterator TreeNodeChildren::end() const |
| 305 |
{ |
| 306 |
// Just copy the parent, and turn it into an end iterator. |
| 307 |
const_iterator iter (*this); |
| 308 |
iter.is_end_ = true; |
| 309 |
return iter; |
| 310 |
} |
| 311 |
|
| 312 |
TreeNodeChildren::value_type TreeNodeChildren::operator[](TreeNodeChildren::size_type index) const |
| 313 |
{ |
| 314 |
iterator iter (model_); |
| 315 |
|
| 316 |
const auto parent = const_cast<GtkTreeIter*>(get_parent_gobject()); |
| 317 |
|
| 318 |
if(!gtk_tree_model_iter_nth_child(model_->gobj(), iter.gobj(), parent, index)) |
| 319 |
{ |
| 320 |
// Assign the already known parent, in order to create an end iterator. |
| 321 |
iter.gobject_ = gobject_; |
| 322 |
iter.is_end_ = true; |
| 323 |
} |
| 324 |
|
| 325 |
return *iter; |
| 326 |
} |
| 327 |
|
| 328 |
TreeNodeChildren::size_type TreeNodeChildren::size() const |
| 329 |
{ |
| 330 |
const auto parent = const_cast<GtkTreeIter*>(get_parent_gobject()); |
| 331 |
|
| 332 |
return gtk_tree_model_iter_n_children(model_->gobj(), parent); |
| 333 |
} |
| 334 |
|
| 335 |
bool TreeNodeChildren::empty() const |
| 336 |
{ |
| 337 |
// If the iterator is invalid (stamp == 0), assume a 'virtual' toplevel |
| 338 |
// node. This behaviour is needed to implement Gtk::TreeModel::children(). |
| 339 |
|
| 340 |
if(gobject_.stamp == 0) |
| 341 |
{ |
| 342 |
GtkTreeIter dummy; |
| 343 |
return !gtk_tree_model_get_iter_first(model_->gobj(), &dummy); |
| 344 |
} |
| 345 |
|
| 346 |
return !gtk_tree_model_iter_has_child(model_->gobj(), const_cast<GtkTreeIter*>(&gobject_)); |
| 347 |
} |
| 348 |
|
| 349 |
} // namespace Gtk |