6 * Copyright 2003 The gtkmm Development Team
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.
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.
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.
23 #include <gtkmm/comboboxtext.h>
25 #include <gtkmm/liststore.h>
26 #include <gtkmm/cellrenderertext.h>
27 #include <gtk/gtkcombobox.h>
28 #include <gtk/gtkcelllayout.h>
33 ComboBoxText::ComboBoxText()
35 set_model( Gtk::ListStore::create(m_text_columns) );
36 pack_start(m_text_columns.m_column);
39 ComboBoxText::ComboBoxText(GtkComboBox* castitem)
40 : Gtk::ComboBox(castitem)
42 set_model( Gtk::ListStore::create(m_text_columns) );
43 pack_start(m_text_columns.m_column);
47 void ComboBoxText::append_text(const Glib::ustring& text)
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.
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);
57 Gtk::TreeModel::iterator iter = list_model->append();
58 Gtk::TreeModel::Row row = *iter;
59 row[m_text_columns.m_column] = text;
63 void ComboBoxText::insert_text(int position, const Glib::ustring& text)
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());
69 void ComboBoxText::prepend_text(const Glib::ustring& text)
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.
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);
79 Gtk::TreeModel::iterator iter = list_model->prepend();
80 Gtk::TreeModel::Row row = *iter;
81 row[m_text_columns.m_column] = text;
85 Glib::ustring ComboBoxText::get_active_text() const
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.
92 TreeModel::iterator active_row = get_active();
95 Gtk::TreeModel::Row row = *active_row;
96 result = row[m_text_columns.m_column];
103 void ComboBoxText::clear()
108 void ComboBoxText::clear_items()
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);
118 void ComboBoxText::remove_text(const Glib::ustring& text)
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);
124 //Look for the row with this text, and remove it:
127 for(Gtk::TreeModel::iterator iter = list_model->children().begin(); iter != list_model->children().end(); ++iter)
129 const Glib::ustring& this_text = (*iter)[m_text_columns.m_column];
131 if(this_text == text)
133 list_model->erase(iter);
140 void ComboBoxText::set_active_text(const Glib::ustring& text)
142 //Look for the row with this text, and activate it:
143 Glib::RefPtr<Gtk::TreeModel> model = get_model();
146 for(Gtk::TreeModel::iterator iter = model->children().begin(); iter != model->children().end(); ++iter)
148 const Glib::ustring& this_text = (*iter)[m_text_columns.m_column];
150 if(this_text == text)
158 //Not found, so mark it as blank: