Initial revision
[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 void ComboBoxText::clear()
103 {
104   //Ideally, we would just store the ListStore as a member variable, but we forgot to do that and not it would break the ABI.
105   Glib::RefPtr<Gtk::TreeModel> model = get_model();
106   Glib::RefPtr<Gtk::ListStore> list_model = Glib::RefPtr<ListStore>::cast_dynamic(model);
107   
108   if(list_model)  
109     list_model->clear();
110 }
111
112 void ComboBoxText::set_active_text(const Glib::ustring& text)
113 {
114   //Look for the row with this text, and activate it:
115   Glib::RefPtr<Gtk::TreeModel> model = get_model();
116   if(model)
117   {
118     for(Gtk::TreeModel::iterator iter = model->children().begin(); iter != model->children().end(); ++iter)
119     {
120       const Glib::ustring& this_text = (*iter)[m_text_columns.m_column];
121
122       if(this_text == text)
123       {
124         set_active(iter);
125         return; //success
126       }
127     }
128   }
129
130   //Not found, so mark it as blank:
131   unset_active();
132 }
133
134
135
136 } // namespace Gtk
137
138