| 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 |
// This is for including the config header before any code (such as |
| 18 |
// the #ifndef GTKMM_DISABLE_DEPRECATED in deprecated classes) is generated: |
| 19 |
_CONFIGINCLUDE(gtkmmconfig.h) |
| 20 |
|
| 21 |
_DEFS(gtkmm,gtk) |
| 22 |
|
| 23 |
#include <gtkmm/treemodel.h> |
| 24 |
#include <gtkmm/selectiondata.h> |
| 25 |
#ifdef GLIBMM_HAVE_SUN_REVERSE_ITERATOR |
| 26 |
#include <cstddef> /* for std::ptrdiff_t */ |
| 27 |
#endif |
| 28 |
|
| 29 |
namespace Gtk |
| 30 |
{ |
| 31 |
|
| 32 |
/** A path is essentially a potential node. It is a location on a model that may |
| 33 |
* or may not actually correspond to a node on a specific model. |
| 34 |
* |
| 35 |
* A Path can be converted into either an array of unsigned integers or a string. The string |
| 36 |
* form is a list of numbers separated by a colon. Each number refers to the |
| 37 |
* offset at that level. Thus, the path "0" refers to the root node and the |
| 38 |
* path "2:4" refers to the fifth child of the third node. |
| 39 |
* |
| 40 |
* Typedefed as Gtk::TreeModel::Path. |
| 41 |
* @ingroup TreeView |
| 42 |
*/ |
| 43 |
class GTKMM_API TreePath |
| 44 |
{ |
| 45 |
_CLASS_BOXEDTYPE(TreePath, GtkTreePath, gtk_tree_path_new, gtk_tree_path_copy, gtk_tree_path_free, GTKMM_API) |
| 46 |
_IGNORE(gtk_tree_path_copy, gtk_tree_path_free, gtk_tree_path_compare) |
| 47 |
|
| 48 |
public: |
| 49 |
typedef unsigned int size_type; |
| 50 |
typedef int difference_type; |
| 51 |
|
| 52 |
typedef int value_type; |
| 53 |
typedef int& reference; |
| 54 |
typedef const int& const_reference; |
| 55 |
|
| 56 |
// Use plain pointers for simplicity. |
| 57 |
typedef int* iterator; |
| 58 |
typedef const int* const_iterator; |
| 59 |
|
| 60 |
#ifndef GLIBMM_HAVE_SUN_REVERSE_ITERATOR |
| 61 |
|
| 62 |
typedef std::reverse_iterator<iterator> reverse_iterator; |
| 63 |
typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 64 |
|
| 65 |
#else |
| 66 |
|
| 67 |
typedef std::reverse_iterator<iterator, std::random_access_iterator_tag, |
| 68 |
int, int&, int*, std::ptrdiff_t> reverse_iterator; |
| 69 |
|
| 70 |
typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag, |
| 71 |
int, const int&, const int*, std::ptrdiff_t> const_reverse_iterator; |
| 72 |
|
| 73 |
#endif /* GLIBMM_HAVE_SUN_REVERSE_ITERATOR */ |
| 74 |
|
| 75 |
_CUSTOM_CTOR_CAST() |
| 76 |
explicit TreePath(GtkTreePath* gobject, bool make_a_copy = true); |
| 77 |
explicit TreePath(size_type n, value_type value = 0); |
| 78 |
explicit TreePath(const Glib::ustring& path); |
| 79 |
explicit TreePath(const TreeModel::iterator& iter); |
| 80 |
|
| 81 |
template <class In> inline TreePath(In pbegin, In pend); |
| 82 |
|
| 83 |
void clear(); |
| 84 |
|
| 85 |
// I think it's OK for this assignment to be implicit. It's very useful. |
| 86 |
TreePath& operator=(const TreeModel::iterator& iter); |
| 87 |
|
| 88 |
_DEPRECATE_IFDEF_START |
| 89 |
/** This typedef is just to make it more obvious that |
| 90 |
* our operator const void* should be used like operator bool(). |
| 91 |
* |
| 92 |
* @deprecated Use the explicit operator bool() instead. |
| 93 |
*/ |
| 94 |
typedef const void* BoolExpr; |
| 95 |
|
| 96 |
/** Checks that the path is not empty, by calling empty(). |
| 97 |
* For instance, |
| 98 |
* @code |
| 99 |
* if(treepath) |
| 100 |
* do_something() |
| 101 |
* @endcode |
| 102 |
* |
| 103 |
* @deprecated Use the explicit operator bool() instead. |
| 104 |
* |
| 105 |
* @newin{2,16} |
| 106 |
*/ |
| 107 |
operator BoolExpr() const; |
| 108 |
_DEPRECATE_IFDEF_END |
| 109 |
|
| 110 |
/** Checks that the path is not empty, by calling empty(). |
| 111 |
* For instance, |
| 112 |
* @code |
| 113 |
* if(treepath) |
| 114 |
* do_something() |
| 115 |
* @endcode |
| 116 |
* |
| 117 |
* @newin{3,22} |
| 118 |
*/ |
| 119 |
explicit operator bool() const; |
| 120 |
|
| 121 |
template <class In> inline void assign(In pbegin, In pend); |
| 122 |
template <class In> void append(In pbegin, In pend); |
| 123 |
|
| 124 |
_WRAP_METHOD(void push_back(int index), gtk_tree_path_append_index) |
| 125 |
_WRAP_METHOD(void push_front(int index), gtk_tree_path_prepend_index) |
| 126 |
|
| 127 |
size_type size() const; |
| 128 |
bool empty() const; |
| 129 |
|
| 130 |
reference operator[](size_type i); |
| 131 |
const_reference operator[](size_type i) const; |
| 132 |
|
| 133 |
_IGNORE(gtk_tree_path_get_indices, gtk_tree_path_get_indices_with_depth) |
| 134 |
|
| 135 |
iterator begin(); |
| 136 |
iterator end(); |
| 137 |
const_iterator begin() const; |
| 138 |
const_iterator end() const; |
| 139 |
|
| 140 |
// Note: there is no advantage in not inlining these methods. |
| 141 |
// We can't change them without breaking ABI anyway. |
| 142 |
reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 143 |
reverse_iterator rend() { return reverse_iterator(begin()); } |
| 144 |
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } |
| 145 |
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } |
| 146 |
|
| 147 |
reference front() { return *begin(); } |
| 148 |
const_reference front() const { return *begin(); } |
| 149 |
reference back() { return *rbegin(); } |
| 150 |
const_reference back() const { return *rbegin(); } |
| 151 |
|
| 152 |
_WRAP_METHOD(void next(), gtk_tree_path_next) |
| 153 |
_WRAP_METHOD(bool prev(), gtk_tree_path_prev) |
| 154 |
_WRAP_METHOD(bool up(), gtk_tree_path_up) |
| 155 |
_WRAP_METHOD(void down(), gtk_tree_path_down) |
| 156 |
|
| 157 |
_WRAP_METHOD(bool is_ancestor(const TreePath& descendant) const, gtk_tree_path_is_ancestor) |
| 158 |
_WRAP_METHOD(bool is_descendant(const TreePath& ancestor) const, gtk_tree_path_is_descendant) |
| 159 |
|
| 160 |
_WRAP_METHOD(Glib::ustring to_string() const, gtk_tree_path_to_string) |
| 161 |
|
| 162 |
#m4begin |
| 163 |
_WRAP_COMPARE(gtk_tree_path_compare, , GTKMM_API) |
| 164 |
#m4end |
| 165 |
|
| 166 |
_IGNORE(gtk_tree_path_append_index) //replaced by push_back() |
| 167 |
_IGNORE(gtk_tree_path_prepend_index) //replaced by push_front() |
| 168 |
_IGNORE(gtk_tree_path_get_depth) //replaced by size() |
| 169 |
_IGNORE(gtk_tree_path_get_indices_with_depth) |
| 170 |
|
| 171 |
/** |
| 172 |
* Obtains a Gtk::TreeModel and Gtk::TreeModel::Path from selection data of target type |
| 173 |
* "GTK_TREE_MODEL_ROW". Normally called from a drag_data_received handler. |
| 174 |
* This function can only be used if @a selection_data originates from the same |
| 175 |
* process that's calling this function, because a pointer to the tree model |
| 176 |
* is being passed around. If you aren't in the same process, then you'll |
| 177 |
* get memory corruption. In the Gtk::TreeDragDest drag_data_received signal handler, |
| 178 |
* you can assume that selection data of type "GTK_TREE_MODEL_ROW" is |
| 179 |
* from the current process. |
| 180 |
* |
| 181 |
* @param selection_data a #SelectionData |
| 182 |
* @param model a Gtk::TreeModel |
| 183 |
* @param path a row in model |
| 184 |
* |
| 185 |
* @return true if the selection_data had target type "GTK_TREE_MODEL_ROW" and |
| 186 |
* is otherwise valid |
| 187 |
**/ |
| 188 |
static bool get_from_selection_data(const SelectionData& selection_data, Glib::RefPtr<TreeModel>& model, TreePath& path); |
| 189 |
//TODO: Add an override that takes a const TreeModel (and deprecate the current version). |
| 190 |
|
| 191 |
/// See description in the other overload. |
| 192 |
static bool get_from_selection_data(const SelectionData& selection_data, TreePath& path); |
| 193 |
|
| 194 |
/** |
| 195 |
* Sets selection data of target type "GTK_TREE_MODEL_ROW". Normally used |
| 196 |
* in a drag_data_get signal handler. |
| 197 |
* |
| 198 |
* @param selection_data some #SelectionData |
| 199 |
* @param model: a Gtk::TreeModel |
| 200 |
* |
| 201 |
* @return true if the selection_data had the proper target type to allow us to set a tree row |
| 202 |
**/ |
| 203 |
bool set_in_selection_data(SelectionData& selection_data, const Glib::RefPtr<const TreeModel>& model) const; |
| 204 |
}; |
| 205 |
|
| 206 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 207 |
|
| 208 |
template <class In> |
| 209 |
void TreePath::append(In pbegin, In pend) |
| 210 |
{ |
| 211 |
// push_back() can't throw -- if it could, this code wouldn't be strongly exception-safe. |
| 212 |
for(; pbegin != pend; ++pbegin) |
| 213 |
this->push_back(*pbegin); |
| 214 |
} |
| 215 |
|
| 216 |
template <class In> inline |
| 217 |
TreePath::TreePath(In pbegin, In pend) |
| 218 |
{ |
| 219 |
this->append(pbegin, pend); |
| 220 |
} |
| 221 |
|
| 222 |
template <class In> inline |
| 223 |
void TreePath::assign(In pbegin, In pend) |
| 224 |
{ |
| 225 |
TreePath temp (pbegin, pend); |
| 226 |
this->swap(temp); |
| 227 |
} |
| 228 |
|
| 229 |
struct TreePathTraits |
| 230 |
{ |
| 231 |
typedef TreePath CppType; |
| 232 |
typedef const GtkTreePath* CType; |
| 233 |
typedef GtkTreePath* CTypeNonConst; |
| 234 |
|
| 235 |
static CType to_c_type(const CppType& item) |
| 236 |
{ return item.gobj(); } |
| 237 |
|
| 238 |
static CType to_c_type(CType item) |
| 239 |
{ return item; } |
| 240 |
|
| 241 |
static CppType to_cpp_type(CType item) |
| 242 |
{ return CppType(const_cast<CTypeNonConst>(item)); } |
| 243 |
|
| 244 |
static void release_c_type(CType item) |
| 245 |
{ gtk_tree_path_free(const_cast<CTypeNonConst>(item)); } |
| 246 |
}; |
| 247 |
|
| 248 |
#endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 249 |
|
| 250 |
} // namespace Gtk |