summaryrefslogtreecommitdiff
path: root/libs/gtkmm2/gtk/gtkmm/treepath.h
blob: 8e41e81284c731d44890cda6442275b415acfd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// -*- c++ -*-
// Generated by gtkmmproc -- DO NOT MODIFY!
#ifndef _GTKMM_TREEPATH_H
#define _GTKMM_TREEPATH_H

#include <glibmm.h>

/* $Id$ */

/* Copyright(C) 1998-2002 The gtkmm Development Team
 *
 * This library is free software, ) you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation, ) either
 * version 2 of the License, or(at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, ) without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library, ) if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <gtkmm/treemodel.h>
#include <gtkmm/selectiondata.h>
#include <gtkmmconfig.h>

GLIBMM_USING_STD(random_access_iterator_tag)
GLIBMM_USING_STD(reverse_iterator)


#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" { typedef struct _GtkTreePath GtkTreePath; }
#endif

namespace Gtk
{

/** A path is essentially a potential node. It is a location on a model that may
 * or may not actually correspond to a node on a specific model.
 *
 * A Path can be converted into either an array of unsigned integers or a string. The string
 * form is a list of numbers separated by a colon. Each number refers to the
 * offset at that level. Thus, the path "0" refers to the root node and the
 * path "2:4" refers to the fifth child of the third node.
 *
 * Typedefed as Gtk::TreeModel::Path.
 * @ingroup TreeView
 */
class TreePath
{
  public:
#ifndef DOXYGEN_SHOULD_SKIP_THIS
  typedef TreePath CppObjectType;
  typedef GtkTreePath BaseObjectType;

  static GType get_type() G_GNUC_CONST;
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

  TreePath();

  explicit TreePath(GtkTreePath* gobject, bool make_a_copy = true);

  TreePath(const TreePath& other);
  TreePath& operator=(const TreePath& other);

  ~TreePath();

  void swap(TreePath& other);

  ///Provides access to the underlying C instance.
  GtkTreePath*       gobj()       { return gobject_; }

  ///Provides access to the underlying C instance.
  const GtkTreePath* gobj() const { return gobject_; }

  ///Provides access to the underlying C instance. The caller is responsible for freeing it. Use when directly setting fields in structs.
  GtkTreePath* gobj_copy() const;

protected:
  GtkTreePath* gobject_;

private:

  
public:
  typedef unsigned int  size_type;
  typedef int           difference_type;

  typedef int           value_type;
  typedef int&          reference;
  typedef const int&    const_reference;

  // Use plain pointers for simplicity.
  typedef int*          iterator;
  typedef const int*    const_iterator;

#ifndef GLIBMM_HAVE_SUN_REVERSE_ITERATOR

  typedef std::reverse_iterator<iterator>       reverse_iterator;
  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

#else

  typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
                                int, int&, int*, ptrdiff_t> reverse_iterator;

  typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
                                int, const int&, const int*, ptrdiff_t> const_reverse_iterator;

#endif /* GLIBMM_HAVE_SUN_REVERSE_ITERATOR */

  explicit TreePath(size_type n, value_type value = 0);
  explicit TreePath(const Glib::ustring& path);
  explicit TreePath(const TreeModel::iterator& iter);

  template <class In> inline TreePath(In pbegin, In pend);

  void clear();

  // I think it's OK for this assignment to be implicit.  It's very useful.
  TreePath& operator=(const TreeModel::iterator& iter);

  template <class In> inline void assign(In pbegin, In pend);
  template <class In>        void append(In pbegin, In pend);

  
  /** Appends a new index to a path.  As a result, the depth of the path is
   * increased.
   * @param index The index.
   */
  void push_back(int index);
  
  /** Prepends a new index to a path.  As a result, the depth of the path is
   * increased.
   * @param index The index.
   */
  void push_front(int index);

  size_type size() const;
  bool empty() const;

  reference       operator[](size_type i);
  const_reference operator[](size_type i) const;

  iterator begin();
  iterator end();
  const_iterator begin() const;
  const_iterator end()   const;

  // Note: there is no advantage in not inlining these methods.
  // We can't change them without breaking ABI anyway.
  reverse_iterator       rbegin()       { return reverse_iterator(end());         }
  reverse_iterator       rend()         { return reverse_iterator(begin());       }
  const_reverse_iterator rbegin() const { return const_reverse_iterator(end());   }
  const_reverse_iterator rend()   const { return const_reverse_iterator(begin()); }

  reference       front()       { return *begin();  }
  const_reference front() const { return *begin();  }
  reference       back()        { return *rbegin(); }
  const_reference back()  const { return *rbegin(); }

  
  /** Moves the @a path  to point to the next node at the current depth.
   */
  void next();
  
  /** Moves the @a path  to point to the previous node at the current depth, if it exists.
   * @return <tt>true</tt> if @a path  has a previous node, and the move was made.
   */
  bool prev();
  
  /** Moves the @a path  to point to its parent node, if it has a parent.
   * @return <tt>true</tt> if @a path  has a parent, and the move was made.
   */
  bool up();
  
  /** Moves @a path  to point to the first child of the current path.
   */
  void down();

  
  /** Returns <tt>true</tt> if @a descendant  is a descendant of @a path .
   * @param descendant Another Gtk::TreePath.
   * @return <tt>true</tt> if @a descendant  is contained inside @a path .
   */
  bool is_ancestor(const TreePath& descendant) const;
  
  /** Returns <tt>true</tt> if @a path  is a descendant of @a ancestor .
   * @param ancestor Another Gtk::TreePath.
   * @return <tt>true</tt> if @a ancestor  contains @a path  somewhere below it.
   */
  bool is_descendant(const TreePath& ancestor) const;

  
  /** Generates a string representation of the path.  This string is a ':'
   * separated list of numbers.  For example, "4:10:0:3" would be an acceptable return value for this string.
   * @return The string.
   */
  Glib::ustring to_string() const;


#ifndef GTKMM_DISABLE_DEPRECATED
  /// @deprecated replaced by push_back():
  
  /** Appends a new index to a path.  As a result, the depth of the path is
   * increased.
   * @param index The index.
   */
  void append_index(int index);
  /// @deprecated replaced by push_front():
  
  /** Prepends a new index to a path.  As a result, the depth of the path is
   * increased.
   * @param index The index.
   */
  void prepend_index(int index);
  /// @deprecated replaced by size():
  
  /** Returns the current depth of @a path .
   * @return The depth of @a path .
   */
  int get_depth() const;
  
  /// @deprecated replaced by begin(), end(), and operator[]
  Glib::ArrayHandle<int> get_indices() const;
#endif
  
  
  /**
   * Obtains a Gtk::TreeModel and Gtk::TreeModel::Path from selection data of target type
   * "GTK_TREE_MODEL_ROW". Normally called from a drag_data_received handler.
   * This function can only be used if @a selection_data originates from the same
   * process that's calling this function, because a pointer to the tree model
   * is being passed around. If you aren't in the same process, then you'll
   * get memory corruption. In the Gtk::TreeDragDest drag_data_received signal handler,
   * you can assume that selection data of type "GTK_TREE_MODEL_ROW" is
   * from the current process. 
   *
   * @param selection_data a #SelectionData
   * @param model a Gtk::TreeModel
   * @param path a row in model
   *
   * @return true if the selection_data had target type "GTK_TREE_MODEL_ROW" and
   *  is otherwise valid
   **/
  static bool get_from_selection_data(const SelectionData& selection_data, Glib::RefPtr<TreeModel>& model, TreePath& path);
  //TODO: Add an override that takes a const TreeModel (and deprecate the current version).
  
  /// See description in the other overload.
  static bool get_from_selection_data(const SelectionData& selection_data, TreePath& path);

  /**
   * Sets selection data of target type "GTK_TREE_MODEL_ROW". Normally used
   * in a drag_data_get signal handler.
   *
   * @param selection_data some #SelectionData
   * @param model: a Gtk::TreeModel
   *
   * @return true if the selection_data had the proper target type to allow us to set a tree row
   **/
  bool set_in_selection_data(SelectionData& selection_data, const Glib::RefPtr<const TreeModel>& model) const;


};

#ifndef DOXYGEN_SHOULD_SKIP_THIS

template <class In>
void TreePath::append(In pbegin, In pend)
{
  // push_back() can't throw -- if it could, this code wouldn't be strongly exception-safe.
  for(; pbegin != pend; ++pbegin)
    this->push_back(*pbegin);
}

template <class In> inline
TreePath::TreePath(In pbegin, In pend)
{
  this->append(pbegin, pend);
}

template <class In> inline
void TreePath::assign(In pbegin, In pend)
{
  TreePath temp (pbegin, pend);
  this->swap(temp);
}


/* Traits for use of TreePath in a Glib::ListHandle<>.
 */
struct TreePath_Traits
{
  typedef TreePath            CppType;
  typedef const GtkTreePath*  CType;
  typedef GtkTreePath*        CTypeNonConst;

  static CType to_c_type(const CppType& item)
    { return item.gobj(); }

  static CType to_c_type(CType item)
    { return item; }

  static CppType to_cpp_type(CType item)
    { return CppType(const_cast<CTypeNonConst>(item)); } 

  static void release_c_type(CType item)
    { gtk_tree_path_free(const_cast<CTypeNonConst>(item)); }  
};

#endif /* DOXYGEN_SHOULD_SKIP_THIS */


} // namespace Gtk


namespace Gtk
{

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 * @result The result
 */
bool operator==(const TreePath& lhs, const TreePath& rhs);

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 * @result The result
 */
bool operator!=(const TreePath& lhs, const TreePath& rhs);

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 * @result The result
 */
bool operator<(const TreePath& lhs, const TreePath& rhs);

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 * @result The result
 */
bool operator>(const TreePath& lhs, const TreePath& rhs);

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 * @result The result
 */
bool operator<=(const TreePath& lhs, const TreePath& rhs);

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 * @result The result
 */
bool operator>=(const TreePath& lhs, const TreePath& rhs);


} // namespace Gtk


namespace Gtk
{

/** @relates Gtk::TreePath
 * @param lhs The left-hand side
 * @param rhs The right-hand side
 */
inline void swap(TreePath& lhs, TreePath& rhs)
  { lhs.swap(rhs); }

} // namespace Gtk

namespace Glib
{

/** @relates Gtk::TreePath
 * @param object The C instance
 * @param take_copy False if the result should take ownership of the C instance. True if it should take a new copy or ref.
 * @result A C++ instance that wraps this C instance.
 */
Gtk::TreePath wrap(GtkTreePath* object, bool take_copy = false);

#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <>
class Value<Gtk::TreePath> : public Glib::Value_Boxed<Gtk::TreePath>
{};
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

} // namespace Glib

#endif /* _GTKMM_TREEPATH_H */