5b672bc5826e1ffa27d1463a08e81c95c0ffc53b
[ardour.git] / libs / libglademm / libglademm / xml.cc
1 // Generated by gtkmmproc -- DO NOT MODIFY!
2
3 #include <libglademm/xml.h>
4 #include <libglademm/private/xml_p.h>
5
6 /* $Id$ */
7
8 /* Copyright (C) 2002 The libglademm Development Team
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <glade/glade-xml.h>
26 #include <gtkmm/button.h>
27 #include <gtkmm/menuitem.h>
28
29 namespace Gnome
30 {
31
32 namespace Glade
33 {
34
35 XmlError::XmlError(const Glib::ustring& message)
36 :
37   message_ (message)
38 {}
39
40 XmlError::~XmlError() throw()
41 {}
42
43 XmlError::XmlError(const XmlError& other)
44 :
45   Glib::Exception (other),
46   message_        (other.message_)
47 {}
48
49 XmlError& XmlError::operator=(const XmlError& other)
50 {
51   message_ = other.message_;
52   return *this;
53 }
54
55 Glib::ustring XmlError::what() const
56 {
57   return message_;
58 }
59
60
61 Xml::Xml(const std::string& filename, const Glib::ustring& root, const Glib::ustring& domain)
62 :
63   Glib::ObjectBase(0), //Mark this class as gtkmmproc-generated, rather than a custom class, to allow vfunc optimisations.
64   Glib::Object(Glib::ConstructParams(xml_class_.init(), (char*) 0))
65
66   if(!glade_xml_construct(gobj(), filename.c_str(), root.empty()   ? 0 : root.c_str(),
67                                                     domain.empty() ? 0 : domain.c_str()))
68   {
69     throw XmlError("Failed to load glade file `" + Glib::filename_to_utf8(filename) + '\'');
70   }
71 }
72
73 // TODO: we need glade_xml_construct_from_buffer()
74 Xml::Xml(const char* buffer, int size, const Glib::ustring& root, const Glib::ustring& domain)
75 :
76   Glib::ObjectBase(0),
77   Glib::Object((GObject*) glade_xml_new_from_buffer(
78       buffer, size, root.empty() ? 0 : root.c_str(), domain.empty() ? 0 : domain.c_str()))
79 {
80   if(!gobj())
81     throw XmlError("Failed to read glade input buffer");
82 }
83
84 // static
85 Glib::RefPtr<Xml> Xml::create(const std::string& filename,
86                               const Glib::ustring& root, const Glib::ustring& domain)
87 {
88   return Glib::RefPtr<Xml>(new Xml(filename, root, domain));
89 }
90
91 // static
92 Glib::RefPtr<Xml> Xml::create_from_buffer(const char* buffer, int size,
93                                           const Glib::ustring& root, const Glib::ustring& domain)
94 {
95   return Glib::RefPtr<Xml>(new Xml(buffer, size, root, domain));
96 }
97
98 void Xml::reparent_widget(const Glib::ustring& name, Gtk::Container& container)
99 {
100   Gtk::Widget* pWidget = 0;
101   get_widget(name, pWidget);
102
103   g_return_if_fail(pWidget != 0);
104
105   pWidget->reparent(container);
106 }
107
108 Gtk::Widget* Xml::get_widget_checked(const Glib::ustring& name, GType type)
109 {
110   // Get the widget from the glade file.
111   Gtk::Widget *const pWidget = get_widget(name);
112
113   if(!pWidget)
114   {
115     g_critical("widget `%s' not found in glade file `%s'", name.c_str(), gobj()->filename);
116     return 0;
117   }
118
119   // Check if it has the correct type.
120   if(!g_type_is_a(G_OBJECT_TYPE(pWidget->gobj()), type))
121   {
122     g_critical("widget `%s' (in glade file `%s') is of type `%s' but `%s' was expected",
123                name.c_str(), gobj()->filename, G_OBJECT_TYPE_NAME(pWidget->gobj()), g_type_name(type));
124     return 0;
125   }
126
127   return pWidget;
128 }
129
130 GtkWidget* Xml::get_cwidget(const Glib::ustring& name)
131 {
132   GtkWidget* pCWidget = glade_xml_get_widget(gobj(), name.c_str());
133   if(!pCWidget)
134     g_critical("Glade::Xml::get_cwidget(): glade_xml_get_widget() failed.");
135   
136   return pCWidget;
137 }
138
139 // Custom coded so that we can custom-code the vfunc in the Xml class.
140 // This is marginally better than modifying gtkmmproc to allow this.
141 //
142 GType Xml_Class::lookup_type_vfunc_callback(GladeXML* self, const char* classname)
143 {
144   CppObjectType *const obj = dynamic_cast<CppObjectType*>(
145       Glib::ObjectBase::_get_current_wrapper((GObject*) self));
146
147   if(obj)
148   {
149     try
150     {
151       return obj->lookup_type_vfunc(Glib::convert_const_gchar_ptr_to_ustring(classname));
152     }
153     catch(...)
154     {
155       Glib::exception_handlers_invoke();
156     }
157   }
158   else
159   {
160     BaseClassType *const base = static_cast<BaseClassType*>(
161         g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class.
162     );
163
164     if(base && base->lookup_type)
165       return (*base->lookup_type)(self, classname);
166   }
167
168   return 0;
169 }
170
171 GType Xml::lookup_type_vfunc(const Glib::ustring& classname)
172 {
173   // See if there is a gtkmm version of the gclass:
174   Glib::ustring classname_prefixed ("gtkmm__"); // gtkmm uses a prefix
175   classname_prefixed += classname;
176
177   GType gtype = g_type_from_name(classname_prefixed.c_str());
178
179   if(gtype == G_TYPE_INVALID) // if it's not a registered typename
180   {
181     // There's no gtkmm derived type, so just use the normal one.
182     gtype = g_type_from_name(classname.c_str());
183   }
184
185   return gtype;
186 }
187
188 void Xml::connect_clicked(const Glib::ustring& widget_name, const sigc::slot<void>& slot_ )
189 {
190   Gtk::Widget* pWidget = 0;
191   get_widget(widget_name, pWidget);
192
193   Gtk::Button* pButton = dynamic_cast<Gtk::Button*>(pWidget);
194   Gtk::MenuItem* pMenuItem = dynamic_cast<Gtk::MenuItem*>(pWidget);
195
196   if(pButton)
197     pButton->signal_clicked().connect( slot_);
198
199   if(pMenuItem)
200     pMenuItem->signal_activate().connect( slot_ );
201 }
202
203 } // namespace Glade
204 } // namespace Gnome
205
206
207 namespace
208 {
209 } // anonymous namespace
210
211
212 namespace Glib
213 {
214
215 Glib::RefPtr<Gnome::Glade::Xml> wrap(GladeXML* object, bool take_copy)
216 {
217   return Glib::RefPtr<Gnome::Glade::Xml>( dynamic_cast<Gnome::Glade::Xml*> (Glib::wrap_auto ((GObject*)(object), take_copy)) );
218   //We use dynamic_cast<> in case of multiple inheritance.
219 }
220
221 } /* namespace Glib */
222
223
224 namespace Gnome
225 {
226
227 namespace Glade
228 {
229
230
231 /* The *_Class implementation: */
232
233 const Glib::Class& Xml_Class::init()
234 {
235   if(!gtype_) // create the GType if necessary
236   {
237     // Glib::Class has to know the class init function to clone custom types.
238     class_init_func_ = &Xml_Class::class_init_function;
239
240     // This is actually just optimized away, apparently with no harm.
241     // Make sure that the parent type has been created.
242     //CppClassParent::CppObjectType::get_type();
243
244     // Create the wrapper type, with the same class/instance size as the base type.
245     register_derived_type(glade_xml_get_type());
246
247     // Add derived versions of interfaces, if the C type implements any interfaces:
248   }
249
250   return *this;
251 }
252
253 void Xml_Class::class_init_function(void* g_class, void* class_data)
254 {
255   BaseClassType *const klass = static_cast<BaseClassType*>(g_class);
256   CppClassParent::class_init_function(klass, class_data);
257
258   klass->lookup_type = &lookup_type_vfunc_callback;
259   }
260
261
262 Glib::ObjectBase* Xml_Class::wrap_new(GObject* object)
263 {
264   return new Xml((GladeXML*)object);
265 }
266
267
268 /* The implementation: */
269
270 GladeXML* Xml::gobj_copy()
271 {
272   reference();
273   return gobj();
274 }
275
276 Xml::Xml(const Glib::ConstructParams& construct_params)
277 :
278   Glib::Object(construct_params)
279 {}
280
281 Xml::Xml(GladeXML* castitem)
282 :
283   Glib::Object((GObject*)(castitem))
284 {}
285
286 Xml::~Xml()
287 {}
288
289
290 Xml::CppClassType Xml::xml_class_; // initialize static member
291
292 GType Xml::get_type()
293 {
294   return xml_class_.init().get_type();
295 }
296
297 GType Xml::get_base_type()
298 {
299   return glade_xml_get_type();
300 }
301
302
303 std::string Xml::get_filename() const
304 {
305   return Glib::convert_const_gchar_ptr_to_stdstring(gobj()->filename);
306 }
307
308 Gtk::Widget* Xml::get_widget(const Glib::ustring& name)
309 {
310   return Glib::wrap(glade_xml_get_widget(gobj(), name.c_str()));
311 }
312
313 Glib::ListHandle<Gtk::Widget*> Xml::get_widget_prefix(const Glib::ustring& name)
314 {
315   return Glib::ListHandle<Gtk::Widget*>(glade_xml_get_widget_prefix(gobj(), name.c_str()), Glib::OWNERSHIP_SHALLOW);
316 }
317
318 std::string Xml::relative_file(const std::string& filename) const
319 {
320   return Glib::convert_return_gchar_ptr_to_stdstring(glade_xml_relative_file(const_cast<GladeXML*>(gobj()), filename.c_str()));
321 }
322
323 Glib::ustring Xml::get_widget_name(Gtk::Widget& widget)
324 {
325   return Glib::convert_const_gchar_ptr_to_ustring(glade_get_widget_name((widget).gobj()));
326 }
327
328 Glib::RefPtr<Xml> Xml::get_widget_tree(Gtk::Widget& widget)
329 {
330
331   Glib::RefPtr<Xml> retvalue = Glib::wrap(glade_get_widget_tree((widget).gobj()));
332
333   if(retvalue)
334     retvalue->reference(); //The function does not do a ref for us.
335   return retvalue;
336 }
337
338
339 } // namespace Glade
340
341 } // namespace Gnome
342
343