Install ardour as a binary, a script and a set of shared
[ardour.git] / libs / gtkmm2 / gtk / gtkmm / treemodelcolumn.h
1 #ifndef _GTKMM_TREEMODELCOLUMN_H
2 #define _GTKMM_TREEMODELCOLUMN_H
3 /* $Id$ */
4
5 /* Copyright (c) 2002 The gtkmm Development Team
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or(at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <glib-object.h>
23 #include <glibmm/value.h>
24
25 #include <vector>
26 #include <gtkmmconfig.h>
27
28 GLIBMM_USING_STD(vector)
29
30
31 namespace Gtk
32 {
33
34 class TreeModelColumnBase;
35
36
37 /** Typedefed as TreeModel::ColumnRecord.
38  * Keeps a record of @link TreeModelColumn TreeModelColumns@endlink.
39  * @ingroup TreeView
40  * ColumnRecord objects are used to setup a new instance of a TreeModel
41  * (or rather, a new instance of an implementation of the model, such as Gtk::ListStore
42  * or Gtk::TreeStore).  It is convenient to do that by deriving from
43  * TreeModel::ColumnRecord:
44  * @code
45  * class MyModelColumns : public Gtk::TreeModel::ColumnRecord
46  * {
47  * public:
48  *   Gtk::TreeModelColumn<Glib::ustring>                filename;
49  *   Gtk::TreeModelColumn<Glib::ustring>                description;
50  *   Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Pixbuf> >  thumbnail;
51  *
52  *   MyModelColumns() { add(filename); add(description); add(thumbnail); }
53  * };
54  * @endcode
55  *
56  * Whether or not you derive your own ColumnRecord, you need to add the
57  * @link TreeModelColumn TreeModelColumns@endlink to the ColumnRecord with the
58  * add() method.
59  *
60  * A ColumnRecord instance, such as an instance of @c MyModelColumns should then
61  * be passed to ListStore::create() or TreeStore::create().
62  * The @link TreeModelColumn TreeModelColumns@endlink, such as the members
63  * @c filename, @c description and @c thumbnail can then be used with Gtk::TreeRow::operator[]()
64  * to specify the column you're interested in.
65  *
66  * Neither TreeModel::ColumnRecord nor the
67  * @link TreeModelColumn TreeModelColumns@endlink contain any real data --
68  * they merely describe what C++ type is stored in which column
69  * of a TreeModel, and save you from having to repeat that type information in several places.
70  * Thus it's absolutely legal to use a statically allocated
71  * TreeModel::ColumnRecord (as long as you make sure it's instantiated after
72  * Gtk::Main), even when creating multiple models from it.
73  */
74 class TreeModelColumnRecord
75 {
76 public:
77   TreeModelColumnRecord();
78   virtual ~TreeModelColumnRecord();
79
80   /** Adds a TreeModelColumn to this record.
81    * add() not only registers the @a column, but also assigns a column
82    * index to it.  Once registered, the TreeModelColumn is final, and
83    * you're free to pass it around by value.
84    */
85   void add(TreeModelColumnBase& column);
86   
87   unsigned int size()  const;
88   const GType* types() const;
89
90 private:
91   std::vector<GType> column_types_;
92
93   // noncopyable
94   TreeModelColumnRecord(const TreeModelColumnRecord&);
95   TreeModelColumnRecord& operator=(const TreeModelColumnRecord&);
96 };
97
98
99 /** Base class of TreeModelColumn templates.
100  * @ingroup TreeView
101  */
102 class TreeModelColumnBase
103 {
104 public:
105   GType type()  const { return type_;  }
106   int index() const { return index_; }
107
108 protected:
109   explicit TreeModelColumnBase(GType type);
110
111 private:
112   GType type_;
113   int   index_;
114
115   friend class Gtk::TreeModelColumnRecord;
116 };
117
118 /** @relates Gtk::TreeModelColumnBase */
119 inline bool operator==(const TreeModelColumnBase& lhs, const TreeModelColumnBase& rhs)
120   { return (lhs.index() == rhs.index()); }
121
122 /** @relates Gtk::TreeModelColumnBase */
123 inline bool operator!=(const TreeModelColumnBase& lhs, const TreeModelColumnBase& rhs)
124   { return (lhs.index() != rhs.index()); }
125
126
127 /** A Gtk::TreeModelColumn describes the C++ type of the data in a model column, and identifies that column in the model.
128  * See @link TreeModelColumnRecord Gtk::TreeModel::Columns@endlink for a usage example.
129  * @ingroup TreeView
130  */
131 template <class T>
132 class TreeModelColumn : public TreeModelColumnBase
133 {
134 public:
135   typedef T               ElementType;
136   typedef Glib::Value<T>  ValueType;
137
138   TreeModelColumn() : TreeModelColumnBase(ValueType::value_type()) {}
139 };
140
141 } // namespace Gtk
142
143
144 #endif /* _GTKMM_TREEMODELCOLUMN_H */
145