1 /* $Id: treepath.hg,v 1.15 2006/02/25 12:38:11 murrayc Exp $ */
3 /* Copyright(C) 1998-2002 The gtkmm Development Team
5 * This library is free software, ) you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation, ) either
8 * version 2 of the License, or(at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, ) without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library, ) if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 // This is for including the config header before any code (such as
21 // the #ifndef GTKMM_DISABLE_DEPRECATED in deprecated classes) is generated:
22 _CONFIGINCLUDE(gtkmmconfig.h)
26 #include <gtkmm/treemodel.h>
27 #include <gtkmm/selectiondata.h>
28 #include <gtkmmconfig.h>
30 GLIBMM_USING_STD(random_access_iterator_tag)
31 GLIBMM_USING_STD(reverse_iterator)
37 /** A path is essentially a potential node. It is a location on a model that may
38 * or may not actually correspond to a node on a specific model.
40 * A Path can be converted into either an array of unsigned integers or a string. The string
41 * form is a list of numbers separated by a colon. Each number refers to the
42 * offset at that level. Thus, the path "0" refers to the root node and the
43 * path "2:4" refers to the fifth child of the third node.
45 * Typedefed as Gtk::TreeModel::Path.
50 _CLASS_BOXEDTYPE(TreePath, GtkTreePath, gtk_tree_path_new, gtk_tree_path_copy, gtk_tree_path_free)
51 _IGNORE(gtk_tree_path_copy, gtk_tree_path_free, gtk_tree_path_compare, gtk_tree_path_get_indices)
54 typedef unsigned int size_type;
55 typedef int difference_type;
57 typedef int value_type;
58 typedef int& reference;
59 typedef const int& const_reference;
61 // Use plain pointers for simplicity.
62 typedef int* iterator;
63 typedef const int* const_iterator;
65 #ifndef GLIBMM_HAVE_SUN_REVERSE_ITERATOR
67 typedef std::reverse_iterator<iterator> reverse_iterator;
68 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
72 typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
73 int, int&, int*, ptrdiff_t> reverse_iterator;
75 typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
76 int, const int&, const int*, ptrdiff_t> const_reverse_iterator;
78 #endif /* GLIBMM_HAVE_SUN_REVERSE_ITERATOR */
80 explicit TreePath(size_type n, value_type value = 0);
81 explicit TreePath(const Glib::ustring& path);
82 explicit TreePath(const TreeModel::iterator& iter);
84 template <class In> inline TreePath(In pbegin, In pend);
88 // I think it's OK for this assignment to be implicit. It's very useful.
89 TreePath& operator=(const TreeModel::iterator& iter);
91 template <class In> inline void assign(In pbegin, In pend);
92 template <class In> void append(In pbegin, In pend);
94 _WRAP_METHOD(void push_back(int index), gtk_tree_path_append_index)
95 _WRAP_METHOD(void push_front(int index), gtk_tree_path_prepend_index)
97 size_type size() const;
100 reference operator[](size_type i);
101 const_reference operator[](size_type i) const;
105 const_iterator begin() const;
106 const_iterator end() const;
108 // Note: there is no advantage in not inlining these methods.
109 // We can't change them without breaking ABI anyway.
110 reverse_iterator rbegin() { return reverse_iterator(end()); }
111 reverse_iterator rend() { return reverse_iterator(begin()); }
112 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
113 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
115 reference front() { return *begin(); }
116 const_reference front() const { return *begin(); }
117 reference back() { return *rbegin(); }
118 const_reference back() const { return *rbegin(); }
120 _WRAP_METHOD(void next(), gtk_tree_path_next)
121 _WRAP_METHOD(bool prev(), gtk_tree_path_prev)
122 _WRAP_METHOD(bool up(), gtk_tree_path_up)
123 _WRAP_METHOD(void down(), gtk_tree_path_down)
125 _WRAP_METHOD(bool is_ancestor(const TreePath& descendant) const, gtk_tree_path_is_ancestor)
126 _WRAP_METHOD(bool is_descendant(const TreePath& ancestor) const, gtk_tree_path_is_descendant)
128 _WRAP_METHOD(Glib::ustring to_string() const, gtk_tree_path_to_string)
131 _WRAP_COMPARE(gtk_tree_path_compare)
135 _WRAP_METHOD(void append_index(int index), gtk_tree_path_append_index, deprecated "replaced by push_back()")
136 _WRAP_METHOD(void prepend_index(int index), gtk_tree_path_prepend_index, deprecated "replaced by push_front().")
137 _WRAP_METHOD(int get_depth() const, gtk_tree_path_get_depth, deprecated "replaced by size().")
139 _DEPRECATE_IFDEF_START
140 /// @deprecated replaced by begin(), end(), and operator[]
141 Glib::ArrayHandle<int> get_indices() const;
143 _IGNORE(gtk_tree_path_get_depth)
146 * Obtains a Gtk::TreeModel and Gtk::TreeModel::Path from selection data of target type
147 * "GTK_TREE_MODEL_ROW". Normally called from a drag_data_received handler.
148 * This function can only be used if @a selection_data originates from the same
149 * process that's calling this function, because a pointer to the tree model
150 * is being passed around. If you aren't in the same process, then you'll
151 * get memory corruption. In the Gtk::TreeDragDest drag_data_received signal handler,
152 * you can assume that selection data of type "GTK_TREE_MODEL_ROW" is
153 * from the current process.
155 * @param selection_data a #SelectionData
156 * @param model a Gtk::TreeModel
157 * @param path a row in model
159 * @return true if the selection_data had target type "GTK_TREE_MODEL_ROW" and
162 static bool get_from_selection_data(const SelectionData& selection_data, Glib::RefPtr<TreeModel>& model, TreePath& path);
163 //TODO: Add an override that takes a const TreeModel (and deprecate the current version).
165 /// See description in the other overload.
166 static bool get_from_selection_data(const SelectionData& selection_data, TreePath& path);
169 * Sets selection data of target type "GTK_TREE_MODEL_ROW". Normally used
170 * in a drag_data_get signal handler.
172 * @param selection_data some #SelectionData
173 * @param model: a Gtk::TreeModel
175 * @return true if the selection_data had the proper target type to allow us to set a tree row
177 bool set_in_selection_data(SelectionData& selection_data, const Glib::RefPtr<const TreeModel>& model) const;
180 #ifndef DOXYGEN_SHOULD_SKIP_THIS
183 void TreePath::append(In pbegin, In pend)
185 // push_back() can't throw -- if it could, this code wouldn't be strongly exception-safe.
186 for(; pbegin != pend; ++pbegin)
187 this->push_back(*pbegin);
190 template <class In> inline
191 TreePath::TreePath(In pbegin, In pend)
193 this->append(pbegin, pend);
196 template <class In> inline
197 void TreePath::assign(In pbegin, In pend)
199 TreePath temp (pbegin, pend);
204 /* Traits for use of TreePath in a Glib::ListHandle<>.
206 struct TreePath_Traits
208 typedef TreePath CppType;
209 typedef const GtkTreePath* CType;
210 typedef GtkTreePath* CTypeNonConst;
212 static CType to_c_type(const CppType& item)
213 { return item.gobj(); }
215 static CType to_c_type(CType item)
218 static CppType to_cpp_type(CType item)
219 { return CppType(const_cast<CTypeNonConst>(item)); }
221 static void release_c_type(CType item)
222 { gtk_tree_path_free(const_cast<CTypeNonConst>(item)); }
225 #endif /* DOXYGEN_SHOULD_SKIP_THIS */