add new sigc++2 directory
[ardour.git] / libs / gtkmm2 / gtk / gtkmm / comboboxtext.cc
1 // -*- c++ -*-
2 /* $Id$ */
3
4 /* 
5  *
6  * Copyright 2003 The gtkmm Development Team
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <gtkmm/comboboxtext.h>
24
25 #include <gtkmm/liststore.h>
26 #include <gtkmm/cellrenderertext.h>
27 #include <gtk/gtkcombobox.h>
28 #include <gtk/gtkcelllayout.h>
29
30 namespace Gtk
31 {
32
33 ComboBoxText::ComboBoxText()
34 {
35   set_model( Gtk::ListStore::create(m_text_columns) );
36   pack_start(m_text_columns.m_column);
37 }
38
39 ComboBoxText::ComboBoxText(GtkComboBox* castitem)
40 : Gtk::ComboBox(castitem)
41 {
42   set_model( Gtk::ListStore::create(m_text_columns) );
43   pack_start(m_text_columns.m_column);
44 }
45
46
47 void ComboBoxText::append_text(const Glib::ustring& text)
48 {
49   //We can not use gtk_combo_box_append_text() here, because that can only be used if gtk_combo_box_new_text() has been used.
50
51   //Ideally, we would just store the ListStore as a member variable, but we forgot to do that and not it would break the ABI.
52   Glib::RefPtr<Gtk::TreeModel> model = get_model();
53   Glib::RefPtr<Gtk::ListStore> list_model = Glib::RefPtr<ListStore>::cast_dynamic(model);
54   
55   if(list_model)
56   {
57     Gtk::TreeModel::iterator iter = list_model->append();
58     Gtk::TreeModel::Row row = *iter;
59     row[m_text_columns.m_column] = text;
60   }
61 }
62
63 void ComboBoxText::insert_text(int position, const Glib::ustring& text)
64 {
65   //TODO: We should not use gtk_combo_box_insert_text() here, because that can only be used if gtk_combo_box_new_text() has been used.
66   gtk_combo_box_insert_text(gobj(), position, text.c_str());
67 }
68
69 void ComboBoxText::prepend_text(const Glib::ustring& text)
70 {
71   //We can not use gtk_combo_box_prepend_text() here, because that can only be used if gtk_combo_box_new_text() has been used.
72
73   //Ideally, we would just store the ListStore as a member variable, but we forgot to do that and not it would break the ABI.
74   Glib::RefPtr<Gtk::TreeModel> model = get_model();
75   Glib::RefPtr<Gtk::ListStore> list_model = Glib::RefPtr<ListStore>::cast_dynamic(model);
76   
77   if(list_model)
78   {
79     Gtk::TreeModel::iterator iter = list_model->prepend();
80     Gtk::TreeModel::Row row = *iter;
81     row[m_text_columns.m_column] = text;
82   }
83 }
84
85 Glib::ustring ComboBoxText::get_active_text() const
86 {
87   //We can not use gtk_combobox_get_active_text() here, because that can only be used if gtk_combo_box_new_text() has been used.
88
89   Glib::ustring result;
90
91   //Get the active row:
92   TreeModel::iterator active_row = get_active();
93   if(active_row)
94   {
95     Gtk::TreeModel::Row row = *active_row;
96     result = row[m_text_columns.m_column];
97   }
98
99   return result;
100 }
101
102 //deprecated.
103 void ComboBoxText::clear()
104 {
105   clear_items();
106 }
107
108 void ComboBoxText::clear_items()
109 {
110   //Ideally, we would just store the ListStore as a member variable, but we forgot to do that and not it would break the ABI.
111   Glib::RefPtr<Gtk::TreeModel> model = get_model();
112   Glib::RefPtr<Gtk::ListStore> list_model = Glib::RefPtr<ListStore>::cast_dynamic(model);
113   
114   if(list_model)  
115     list_model->clear();
116 }
117
118 void ComboBoxText::remove_text(const Glib::ustring& text)
119 {
120   //Ideally, we would just store the ListStore as a member variable, but we forgot to do that and not it would break the ABI.
121   Glib::RefPtr<Gtk::TreeModel> model = get_model();
122   Glib::RefPtr<Gtk::ListStore> list_model = Glib::RefPtr<ListStore>::cast_dynamic(model);
123
124   //Look for the row with this text, and remove it:
125   if(list_model)
126   {
127     for(Gtk::TreeModel::iterator iter = list_model->children().begin(); iter != list_model->children().end(); ++iter)
128     {
129       const Glib::ustring& this_text = (*iter)[m_text_columns.m_column];
130
131       if(this_text == text)
132       {
133         list_model->erase(iter);
134         return; //success
135       }
136     }
137   }
138 }
139
140 void ComboBoxText::set_active_text(const Glib::ustring& text)
141 {
142   //Look for the row with this text, and activate it:
143   Glib::RefPtr<Gtk::TreeModel> model = get_model();
144   if(model)
145   {
146     for(Gtk::TreeModel::iterator iter = model->children().begin(); iter != model->children().end(); ++iter)
147     {
148       const Glib::ustring& this_text = (*iter)[m_text_columns.m_column];
149
150       if(this_text == text)
151       {
152         set_active(iter);
153         return; //success
154       }
155     }
156   }
157
158   //Not found, so mark it as blank:
159   unset_active();
160 }
161
162
163
164 } // namespace Gtk
165
166