add new sigc++2 directory
[ardour.git] / libs / gtkmm2 / gtk / src / treepath.ccg
1 // -*- c++ -*-
2 /* $Id: treepath.ccg,v 1.5 2005/11/29 16:38:10 murrayc Exp $ */
3
4 /* Copyright 1998-2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <gtkmm/treemodel.h>
22 #include <glibmm/utility.h>
23 #include <gtk/gtktreemodel.h>
24 #include <gtk/gtktreednd.h>
25
26
27 namespace Gtk
28 {
29
30 TreePath::TreePath(TreePath::size_type n, TreePath::value_type value)
31 :
32   gobject_ (gtk_tree_path_new())
33 {
34   for(; n > 0; --n)
35     gtk_tree_path_append_index(gobject_, value);
36 }
37
38 TreePath::TreePath(const Glib::ustring& path)
39 :
40   gobject_ (gtk_tree_path_new_from_string(path.c_str()))
41 {}
42
43 TreePath::TreePath(const TreeModel::iterator& iter)
44 :
45   // The GtkTreePath* is always newly created.
46   gobject_ (gtk_tree_model_get_path(iter.get_model_gobject(), const_cast<GtkTreeIter*>(iter.gobj())))
47 {}
48
49 TreePath& TreePath::operator=(const TreeModel::iterator& iter)
50 {
51   TreePath temp (iter);
52   swap(temp);
53   return *this;
54 }
55
56 void TreePath::clear()
57 {
58   TreePath empty_path;
59   swap(empty_path);
60 }
61
62 TreePath::size_type TreePath::size() const
63 {
64   return gtk_tree_path_get_depth(gobject_);
65 }
66
67 bool TreePath::empty() const
68 {
69   return (gtk_tree_path_get_depth(gobject_) == 0);
70 }
71
72 TreePath::reference TreePath::operator[](TreePath::size_type i)
73 {
74   int *const indices = gtk_tree_path_get_indices(gobject_);
75   return indices[i];
76 }
77
78 TreePath::const_reference TreePath::operator[](TreePath::size_type i) const
79 {
80   const int *const indices = gtk_tree_path_get_indices(gobject_);
81   return indices[i];
82 }
83
84 TreePath::iterator TreePath::begin()
85 {
86   return gtk_tree_path_get_indices(gobject_);
87 }
88
89 TreePath::iterator TreePath::end()
90 {
91   return gtk_tree_path_get_indices(gobject_) + gtk_tree_path_get_depth(gobject_);
92 }
93
94 TreePath::const_iterator TreePath::begin() const
95 {
96   return gtk_tree_path_get_indices(gobject_);
97 }
98
99 TreePath::const_iterator TreePath::end() const
100 {
101   return gtk_tree_path_get_indices(gobject_) + gtk_tree_path_get_depth(gobject_);
102 }
103
104 bool TreePath::get_from_selection_data(const SelectionData& selection_data, Glib::RefPtr<TreeModel>& model, TreePath& path) //static 
105 {
106   GtkTreeModel* src_model = 0;
107   GtkTreePath* src_path = 0;
108   gboolean result = gtk_tree_get_row_drag_data(const_cast<GtkSelectionData*>(selection_data.gobj()), &src_model, &src_path);
109
110   model = Glib::wrap(src_model, true /* take_copy=true */);
111   
112   //gtk_tree_get_row_drag_data gives us ownership of src_path.
113   path = Glib::wrap(src_path, false /* take_copy=false */);
114
115   return result;
116 }
117
118 bool TreePath::get_from_selection_data(const SelectionData& selection_data, TreePath& path) //static
119 {
120   GtkTreePath* src_path = 0;
121   gboolean result = gtk_tree_get_row_drag_data(const_cast<GtkSelectionData*>(selection_data.gobj()), 0, &src_path);
122
123   //gtk_tree_get_row_drag_data gives us ownership of src_path.
124   path = Glib::wrap(src_path, false /* take_copy=false */);
125
126   return result;
127 }
128
129
130 bool TreePath::set_in_selection_data(SelectionData& selection_data, const Glib::RefPtr<const TreeModel>& model) const
131 {
132   return gtk_tree_set_row_drag_data(selection_data.gobj(), const_cast<GtkTreeModel*>(model->gobj()), const_cast<GtkTreePath*>(gobj()));  
133 }
134
135 _DEPRECATE_IFDEF_START
136 Glib::ArrayHandle<int> TreePath::get_indices() const
137 {
138   // gtk_tree_path_get_indices() returns a pointer to an internal array,
139   // similar to std::string::data().  Thus the OWNERSHIP_NONE flag.
140
141   return Glib::ArrayHandle<int>(gtk_tree_path_get_indices(gobject_),
142                                 gtk_tree_path_get_depth(gobject_),
143                                 Glib::OWNERSHIP_NONE);
144 }
145 _DEPRECATE_IFDEF_END
146   
147 } // namespace Gtk
148