Merged revisions 6293,6296-6306,6308 via svnmerge from
[ardour.git] / libs / gtkmm2 / gtk / gtkmm / listviewtext.cc
1 /* Copyright(C) 2006 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 Library General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 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  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the Free
15  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18 #include <gtkmm/listviewtext.h>
19
20 namespace Gtk
21 {
22
23 ListViewText::TextModelColumns::TextModelColumns(guint columns_count)
24 : m_columns_count(columns_count)
25 {
26   m_columns = new Gtk::TreeModelColumn<Glib::ustring>[m_columns_count];
27
28   if(m_columns)
29   {
30     for(guint i = 0; i < m_columns_count; ++i)
31     {
32       add(m_columns[i]);
33     }
34   }
35 }
36
37 ListViewText::TextModelColumns::~TextModelColumns()
38 {
39   if(m_columns)
40     delete[] m_columns;
41 }
42
43 guint ListViewText::TextModelColumns::get_num_columns() const
44 {
45   return m_columns_count;
46 }
47
48 ListViewText::ListViewText(guint columns_count, bool editable, Gtk::SelectionMode mode)
49 : m_model_columns(columns_count)
50 {
51   char column_title[20];
52   
53   // Create model
54   m_model = Gtk::ListStore::create(m_model_columns);    
55   set_model(m_model);
56
57   // Append columns
58   for(guint i = 0; i < columns_count; ++i)
59   {
60     //Get text for the number:
61     sprintf(column_title, "%d", i);             
62
63     if(editable)
64       append_column_editable(column_title, m_model_columns.m_columns[i]);
65     else
66       append_column(column_title, m_model_columns.m_columns[i]);
67   }
68
69   // Set multiple or simple selection
70   get_selection()->set_mode(mode);
71 }
72
73 ListViewText::~ListViewText()
74 {
75 }
76
77 void ListViewText::set_column_title(guint column, const Glib::ustring& title)
78 {
79   g_return_if_fail( column < get_columns().size() );
80
81   get_column(column)->set_title(title);
82 }
83
84 Glib::ustring ListViewText::get_column_title(guint column) const
85 {
86   g_return_val_if_fail( column < get_columns().size(), "" ); //Using Glib::ustring() fails sometimes: Bug #352226
87
88   return get_column(column)->get_title();
89 }
90
91 guint ListViewText::append_text(const Glib::ustring& column_one_value)
92 {
93   Gtk::TreeModel::Row newRow = *(m_model->append());
94
95   newRow[m_model_columns.m_columns[0]] = column_one_value;
96
97   return size() - 1;
98 }
99
100 void ListViewText::prepend_text(const Glib::ustring& column_one_value)
101 {
102   Gtk::TreeModel::Row newRow = *(m_model->prepend());
103
104   newRow[m_model_columns.m_columns[0]] = column_one_value;
105 }
106
107 void ListViewText::insert_text(guint row, const Glib::ustring& column_one_value)
108 {
109   g_return_if_fail( row < size() );
110
111   Gtk::ListStore::const_iterator iter = m_model->children()[row];
112   Gtk::TreeModel::Row newRow = *(m_model->insert(iter));
113
114   if(!column_one_value.empty())
115   {
116     newRow[m_model_columns.m_columns[0]] = column_one_value;
117   }
118 }
119
120 void ListViewText::clear_items()
121 {
122   m_model->clear();
123 }
124
125 Glib::ustring ListViewText::get_text(guint row, guint column) const
126 {
127   Glib::ustring result;
128
129   g_return_val_if_fail( row < size(), result );
130
131   Gtk::TreeModel::iterator iter = m_model->children()[row];      
132   iter->get_value(column, result);
133
134   return result;
135 }
136
137 void ListViewText::set_text(guint row, guint column, const Glib::ustring& value)
138 {
139   g_return_if_fail( row < size() );
140
141   Gtk::TreeModel::iterator iter = m_model->children()[row];
142   (*iter)->set_value(column, value);
143 }
144
145 void ListViewText::set_text(guint row, const Glib::ustring& value)
146 {
147   g_return_if_fail( row < size() );
148
149   Gtk::TreeModel::iterator iter = m_model->children()[ row ];
150   (*iter)->set_value(0, value);
151 }
152
153 guint ListViewText::size() const
154 {
155   return (guint)m_model->children().size();
156 }
157
158 guint ListViewText::get_num_columns() const
159 {
160   return m_model_columns.get_num_columns();
161 }
162
163 ListViewText::SelectionList ListViewText::get_selected()
164 {
165   Glib::RefPtr<Gtk::TreeSelection> selected = get_selection();
166   Gtk::TreeSelection::ListHandle_Path selectedRows = selected->get_selected_rows();
167
168   // Reserve space
169   SelectionList selectionList;
170   selectionList.reserve( selected->count_selected_rows() );
171
172   // Save selected rows
173
174   for(Gtk::TreeSelection::ListHandle_Path::const_iterator iter = selectedRows.begin(); iter != selectedRows.end(); ++iter)
175   {
176     selectionList.push_back( *((*iter).begin()) );
177   }
178
179   return selectionList;
180 }
181
182 } //namespace Gtk
183