Added 2.1-staging branch.
[ardour.git] / libs / libglademm / libglademm / variablesmap.cc
1 /* variablesmap.cc
2  *
3  * Copyright (C) 2002 The libglademm Development Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
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>
26
27 namespace Gnome
28 {
29
30 namespace Glade
31 {
32
33 VariablesMap::VariablesMap(const Glib::RefPtr<Glade::Xml>& glade)
34 : m_refGlade(glade)
35 {
36 }
37
38 VariablesMap::~VariablesMap()
39 {
40 }
41
42
43 void VariablesMap::connect_widget(const Glib::ustring& widget_name, bool& variable)
44 {
45   Gtk::ToggleButton* pToggleButton = 0;
46   m_refGlade->get_widget(widget_name, pToggleButton); //Glade::Xml will complain if it is not a ToggleButton.
47   if(pToggleButton)
48   {
49     m_mapWidgetsToVariables[pToggleButton] = (void*)(&variable);
50   }
51 }
52
53 void VariablesMap::connect_widget(const Glib::ustring& widget_name, Glib::ustring& variable)
54 {
55   Gtk::Widget* pWidget = 0;
56   m_refGlade->get_widget(widget_name, pWidget); 
57
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);
60   if(pEntry)
61   {
62     m_mapWidgetsToVariables[pEntry] = (void*)(&variable);
63   }
64   if(pComboBoxEntry)
65   {
66     m_mapWidgetsToVariables[pComboBoxEntry] = (void*)(&variable);
67   }
68 }
69
70 void VariablesMap::connect_widget(const Glib::ustring& widget_name, double& variable)
71 {
72   Gtk::Widget* pWidget = 0;
73   m_refGlade->get_widget(widget_name, pWidget); 
74
75   Gtk::Scale* pScale = dynamic_cast<Gtk::Scale*>(pWidget); 
76   if(pScale)
77   {
78     m_mapWidgetsToVariables[pScale] = (void*)(&variable);
79   }
80 }
81
82 void VariablesMap::connect_widget(const Glib::ustring& widget_name, Glib::Date& variable)
83 {
84   Gtk::Widget* pWidget = 0;
85   m_refGlade->get_widget(widget_name, pWidget); 
86
87   Gtk::Calendar* pCalendar = dynamic_cast<Gtk::Calendar*>(pWidget); 
88   if(pCalendar)
89   {
90     m_mapWidgetsToVariables[pCalendar] = (void*)(&variable);
91   }
92 }
93
94 void VariablesMap::transfer_widgets_to_variables()
95 {
96   if(validate_widgets()) //If the widgets' data is correct. Useful to override.
97   {
98     for(type_mapWidgetsToVariables::iterator iter =  m_mapWidgetsToVariables.begin(); iter != m_mapWidgetsToVariables.end(); ++iter)
99     {
100       transfer_one_widget(iter->first, true); //true = to_variable.
101     }
102   }
103 }
104
105 void VariablesMap::transfer_variables_to_widgets()
106 {
107   for(type_mapWidgetsToVariables::iterator iter =  m_mapWidgetsToVariables.begin(); iter != m_mapWidgetsToVariables.end(); ++iter)
108   {
109     transfer_one_widget(iter->first, false); //false = to_widget.
110   }
111 }
112
113
114 void VariablesMap::transfer_one_widget(Gtk::Widget* pWidget, bool to_variable)
115 {
116   //Find the widget in the map:
117   type_mapWidgetsToVariables::iterator iterFind = m_mapWidgetsToVariables.find(pWidget);
118   if(iterFind != m_mapWidgetsToVariables.end())
119   {
120     //Get the variable for the widget:
121     void* pVariable = iterFind->second;
122     if(pVariable)
123     {
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);
127
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); 
131
132       if(pEntry)
133       {
134         Glib::ustring* pVar = (Glib::ustring*)(pVariable);
135
136         if(to_variable)
137           (*pVar) = pEntry->get_text();
138         else
139           pEntry->set_text(*pVar);
140       }
141       if(pComboBoxEntry)
142       {
143         Glib::ustring* pVar = (Glib::ustring*)(pVariable);
144         Gtk::Entry* pIEntry = dynamic_cast<Gtk::Entry*>(pComboBoxEntry->get_child());
145
146         if(to_variable){
147           if(pIEntry) 
148                 (*pVar) = pIEntry->get_text();
149         } else {
150           if(pIEntry) 
151                   pIEntry->set_text(*pVar);
152         }
153       }
154       if(pToggleButton)
155       {
156         bool* pVar = (bool*)(pVariable);
157
158         if(to_variable)
159           (*pVar) = pToggleButton->get_active();
160         else
161           pToggleButton->set_active(*pVar);
162       }
163       if(pScale)
164       {
165         double* pVar = (double*)(pVariable);
166
167         if(to_variable)
168           (*pVar) = pScale->get_value();
169         else
170           pScale->set_value(*pVar);
171       }
172       if(pCalendar)
173       {
174         Glib::Date* pVar = (Glib::Date*)(pVariable);
175
176         if(to_variable){
177            guint year,month,day;
178            pCalendar->get_date(year,month,day);
179           (*pVar) = Glib::Date(day,(Glib::Date::Month)month,year);
180         } else {
181           pCalendar->select_day(pVar->get_day());
182           pCalendar->select_month(pVar->get_month(), pVar->get_year());
183         }
184       }
185     }
186   }
187 }
188
189 bool VariablesMap::validate_widgets()
190 {
191   //Override to add validation.
192   //TODO: We could add some automatic data-range and text-length validation.
193   return true;
194 }
195
196
197
198 } /* namespace Glade */
199 } /* namespace Gnome */