3 * Copyright (C) 2002 The libglademm Development Team
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <libglademm/variablesmap.h>
21 #include <gtkmm/togglebutton.h>
22 #include <gtkmm/entry.h>
23 #include <gtkmm/calendar.h>
24 #include <gtkmm/scale.h>
25 #include <gtkmm/comboboxentry.h>
33 VariablesMap::VariablesMap(const Glib::RefPtr<Glade::Xml>& glade)
38 VariablesMap::~VariablesMap()
43 void VariablesMap::connect_widget(const Glib::ustring& widget_name, bool& variable)
45 Gtk::ToggleButton* pToggleButton = 0;
46 m_refGlade->get_widget(widget_name, pToggleButton); //Glade::Xml will complain if it is not a ToggleButton.
49 m_mapWidgetsToVariables[pToggleButton] = (void*)(&variable);
53 void VariablesMap::connect_widget(const Glib::ustring& widget_name, Glib::ustring& variable)
55 Gtk::Widget* pWidget = 0;
56 m_refGlade->get_widget(widget_name, pWidget);
58 Gtk::Entry* pEntry = dynamic_cast<Gtk::Entry*>(pWidget); //it mange both Gtk::entry and Gtk::SpinButton
59 Gtk::ComboBoxEntry* pComboBoxEntry = dynamic_cast<Gtk::ComboBoxEntry*>(pWidget);
62 m_mapWidgetsToVariables[pEntry] = (void*)(&variable);
66 m_mapWidgetsToVariables[pComboBoxEntry] = (void*)(&variable);
70 void VariablesMap::connect_widget(const Glib::ustring& widget_name, double& variable)
72 Gtk::Widget* pWidget = 0;
73 m_refGlade->get_widget(widget_name, pWidget);
75 Gtk::Scale* pScale = dynamic_cast<Gtk::Scale*>(pWidget);
78 m_mapWidgetsToVariables[pScale] = (void*)(&variable);
82 void VariablesMap::connect_widget(const Glib::ustring& widget_name, Glib::Date& variable)
84 Gtk::Widget* pWidget = 0;
85 m_refGlade->get_widget(widget_name, pWidget);
87 Gtk::Calendar* pCalendar = dynamic_cast<Gtk::Calendar*>(pWidget);
90 m_mapWidgetsToVariables[pCalendar] = (void*)(&variable);
94 void VariablesMap::transfer_widgets_to_variables()
96 if(validate_widgets()) //If the widgets' data is correct. Useful to override.
98 for(type_mapWidgetsToVariables::iterator iter = m_mapWidgetsToVariables.begin(); iter != m_mapWidgetsToVariables.end(); ++iter)
100 transfer_one_widget(iter->first, true); //true = to_variable.
105 void VariablesMap::transfer_variables_to_widgets()
107 for(type_mapWidgetsToVariables::iterator iter = m_mapWidgetsToVariables.begin(); iter != m_mapWidgetsToVariables.end(); ++iter)
109 transfer_one_widget(iter->first, false); //false = to_widget.
114 void VariablesMap::transfer_one_widget(Gtk::Widget* pWidget, bool to_variable)
116 //Find the widget in the map:
117 type_mapWidgetsToVariables::iterator iterFind = m_mapWidgetsToVariables.find(pWidget);
118 if(iterFind != m_mapWidgetsToVariables.end())
120 //Get the variable for the widget:
121 void* pVariable = iterFind->second;
124 //Cast the variable appropriately and set it appropriately:
125 Gtk::Entry* pEntry = dynamic_cast<Gtk::Entry*>(pWidget);
126 Gtk::ComboBoxEntry* pComboBoxEntry = dynamic_cast<Gtk::ComboBoxEntry*>(pWidget);
128 Gtk::ToggleButton* pToggleButton = dynamic_cast<Gtk::ToggleButton*>(pWidget); //CheckButtons and RadioButtons.
129 Gtk::Scale* pScale = dynamic_cast<Gtk::Scale*>(pWidget);
130 Gtk::Calendar* pCalendar = dynamic_cast<Gtk::Calendar*>(pWidget);
134 Glib::ustring* pVar = (Glib::ustring*)(pVariable);
137 (*pVar) = pEntry->get_text();
139 pEntry->set_text(*pVar);
143 Glib::ustring* pVar = (Glib::ustring*)(pVariable);
144 Gtk::Entry* pIEntry = dynamic_cast<Gtk::Entry*>(pComboBoxEntry->get_child());
148 (*pVar) = pIEntry->get_text();
151 pIEntry->set_text(*pVar);
156 bool* pVar = (bool*)(pVariable);
159 (*pVar) = pToggleButton->get_active();
161 pToggleButton->set_active(*pVar);
165 double* pVar = (double*)(pVariable);
168 (*pVar) = pScale->get_value();
170 pScale->set_value(*pVar);
174 Glib::Date* pVar = (Glib::Date*)(pVariable);
177 guint year,month,day;
178 pCalendar->get_date(year,month,day);
179 (*pVar) = Glib::Date(day,(Glib::Date::Month)month,year);
181 pCalendar->select_day(pVar->get_day());
182 pCalendar->select_month(pVar->get_month(), pVar->get_year());
189 bool VariablesMap::validate_widgets()
191 //Override to add validation.
192 //TODO: We could add some automatic data-range and text-length validation.
198 } /* namespace Glade */
199 } /* namespace Gnome */