summaryrefslogtreecommitdiff
path: root/libs/libglademm/libglademm/xml.h
blob: b0e5c440f7c5c0542c4ea4546d519405b26b2df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// -*- c++ -*-
// Generated by gtkmmproc -- DO NOT MODIFY!
#ifndef _LIBGLADEMM_XML_H
#define _LIBGLADEMM_XML_H

#include <glibmm.h>

/* $Id$ */

/* Copyright (C) 2002 The libglademm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gtkmm/container.h>


#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef struct _GladeXML GladeXML;
typedef struct _GladeXMLClass GladeXMLClass;
#endif /* DOXYGEN_SHOULD_SKIP_THIS */


namespace Gnome
{

namespace Glade
{ class Xml_Class; } // namespace Glade

} // namespace Gnome
namespace Gnome
{

namespace Glade
{

class XmlError : public Glib::Exception
{
public:
  explicit XmlError(const Glib::ustring& message);
  virtual ~XmlError() throw();

  XmlError(const XmlError& other);
  XmlError& operator=(const XmlError& other);

  virtual Glib::ustring what() const;

private:
  Glib::ustring message_;
};


class Xml : public Glib::Object
{
  
#ifndef DOXYGEN_SHOULD_SKIP_THIS

public:
  typedef Xml CppObjectType;
  typedef Xml_Class CppClassType;
  typedef GladeXML BaseObjectType;
  typedef GladeXMLClass BaseClassType;

private:  friend class Xml_Class;
  static CppClassType xml_class_;

private:
  // noncopyable
  Xml(const Xml&);
  Xml& operator=(const Xml&);

protected:
  explicit Xml(const Glib::ConstructParams& construct_params);
  explicit Xml(GladeXML* castitem);

#endif /* DOXYGEN_SHOULD_SKIP_THIS */

public:
  virtual ~Xml();

#ifndef DOXYGEN_SHOULD_SKIP_THIS
  static GType get_type()      G_GNUC_CONST;
  static GType get_base_type() G_GNUC_CONST;
#endif

  ///Provides access to the underlying C GObject.
  GladeXML*       gobj()       { return reinterpret_cast<GladeXML*>(gobject_); }

  ///Provides access to the underlying C GObject.
  const GladeXML* gobj() const { return reinterpret_cast<GladeXML*>(gobject_); }

  ///Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
  GladeXML* gobj_copy();

private:

  
   //Ignore private glade_xml functions.

protected:
  /** Loads a glade XML file.
   * @throw XmlError
   */
  Xml(const std::string& filename, const Glib::ustring& root, const Glib::ustring& domain);

  /** Reads glade XML data from memory.
   * @throw XmlError
   */
  Xml(const char* buffer, int size, const Glib::ustring& root, const Glib::ustring& domain);

  Gtk::Widget* get_widget_checked(const Glib::ustring& name, GType type);
  GtkWidget* get_cwidget(const Glib::ustring& name);

public:
  typedef Gnome::Glade::XmlError Error;

  /** Loads a glade XML file.
   * @throw XmlError
   */
  static Glib::RefPtr<Xml> create(const std::string& filename,
                                  const Glib::ustring& root   = Glib::ustring(),
                                  const Glib::ustring& domain = Glib::ustring());

  /** Reads glade XML data from memory.
   * @throw XmlError
   */
  static Glib::RefPtr<Xml> create_from_buffer(const char* buffer, int size,
                                              const Glib::ustring& root   = Glib::ustring(),
                                              const Glib::ustring& domain = Glib::ustring());

  std::string get_filename() const;

//void       glade_xml_signal_connect      (GladeXML *self,
//					  const char *handlername,
//					  GCallback func);
//void       glade_xml_signal_connect_data (GladeXML *self,
//					  const char *handlername,
//					  GCallback func,
//					  gpointer user_data);
//
//void       glade_xml_signal_autoconnect  (GladeXML *self);
//
//
//typedef void (*GladeXMLConnectFunc) (const gchar *handler_name,
//				     GObject *object,
//				     const gchar *signal_name,
//				     const gchar *signal_data,
//				     GObject *connect_object,
//				     gboolean after,
//				     gpointer user_data);
//
//
//void       glade_xml_signal_connect_full     (GladeXML *self,
//					      const gchar *handler_name,
//					      GladeXMLConnectFunc func,
//					      gpointer user_data);
//
//void       glade_xml_signal_autoconnect_full (GladeXML *self,
//					      GladeXMLConnectFunc func,
//					      gpointer user_data);
//
//
  /** Get a widget from the glade file.
   * For instance:
   * @code
   * Gtk::Table* pTable = dynamic_cast<Gtk::Table*>(refXml->get_widget("mytable"));
   * @endcode
   * @param name The name of the widget.
   * @return A pointer to the widget, or <tt>0</tt> on failure.
   */
  
  Gtk::Widget* get_widget(const Glib::ustring& name);

  /** This is for convenience.
   * It allows for a shorter syntax with less repetition. For instance:
   * @code
   * Gtk::Table* pTable = 0;
   * refXml->get_widget("mytable", pTable);
   * @endcode
   * This method prints a warning message to the console if the widget
   * doesn't exist or has the wrong type, so you don't need to check that
   * manually.
   * @param name The name of the widget.
   * @retval widget A pointer to the widget, or <tt>0</tt> on failure.
   * @return The value of @a widget.
   */
  template <class T_Widget> inline
  T_Widget* get_widget(const Glib::ustring& name, T_Widget*& widget)
  {
    // The dynamic_cast<> should never fail if get_widget_checked() succeeded.
    widget = dynamic_cast<T_Widget*>(this->get_widget_checked(name, T_Widget::get_base_type()));

    if(!widget)
      g_critical("Gnome::Glade::Xml::get_widget(): dynamic_cast<> failed.");
      
    return widget;
  }

  /** This provides a pointer to a widget whose details are specified in the Glade file, but which is implemented
   * by your own derived class. Your class must have a constructor like so:
   * @code
   * DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade)
   * : Gtk::Dialog(cobject) //Calls the base class constructor
   * @endcode
   *
   * For instance:
   * @code
   * Gtk::DerivedBox* pBox = 0;
   * refXml->get_widget_derived("mybox", pBox);
   * @endcode
   *
   * @param name The name of the widget.
   * @retval widget A pointer to the widget, or <tt>0</tt> on failure.
   * @return The value of @a widget.
   */
  template <class T_Widget> inline
  T_Widget* get_widget_derived(const Glib::ustring& name, T_Widget*& widget)
  {
     // initialize input parameter
     widget = 0;
     
     // Get the widget from the glade file.
     typedef  typename T_Widget::BaseObjectType cwidget_type;
     cwidget_type* pCWidget = (cwidget_type*)get_cwidget(name);

     //The error was already reported by get_cwidget().
     if(!pCWidget)
       return 0;

     //Check whether there is already a C++ wrapper instance associated with this C instance:
     Glib::ObjectBase* pObjectBase = ObjectBase::_get_current_wrapper((GObject*)pCWidget);

     //If there is already a C++ instance, then return it again:
     if(pObjectBase)
     {
       widget = dynamic_cast<T_Widget*>( Glib::wrap((GtkWidget*)pCWidget) );
       //Newer, more spec-complaint, versions of g++ not resolve a specific wrap() function in a template.

       //The dynamic cast checks that it is of the correct type.
       //Somebody might be trying to call get_widget_derived() after already calling get_widget(),
       //or after already calling get_widget_derived() with a different derived C++ type.
       if(!widget)
        g_critical("Gnome::Glade::Xml::get_widget_derived(): dynamic_cast<> failed. An existing C++ instance, of a different type, seems to exist.");      
     }
     else
     {
       //Create a new C++ instance to wrap the existing C instance:
       
       //Set the output variable. We needed to do this because we can not template the return type.
       Glib::RefPtr<Gnome::Glade::Xml> refThis(this);
       refThis->reference(); //take a copy.
       widget = new T_Widget(pCWidget, refThis);
     }

     //We return it as well (we need the parameter as well, because C++ can not just template the return type.)
     return widget;
  }

  
  Glib::ListHandle<Gtk::Widget*> get_widget_prefix(const Glib::ustring& name);

  ///Take the widget from the glade-generated container and put it in another container.
  void reparent_widget(const Glib::ustring& name, Gtk::Container& container);

  
  std::string relative_file(const std::string& filename) const;

  
  static Glib::ustring get_widget_name(Gtk::Widget& widget);
  
  static Glib::RefPtr<Xml> get_widget_tree(Gtk::Widget& widget);

              
  /** Connect a Gtk::Button's clicked signal or a Gtk::MenuItem's activated signal to a slot.
   *
   * For instance:
   * @code
   * refXml->connect_button("button", sigc::mem_fun(*this, &ExampleWindow::on_button_clicked) );
   * @endcode
   *
   * @param name The name of the widget.
   * @param pslot The slot to connect to.
   */
  void connect_clicked(const Glib::ustring& name, const sigc::slot<void>& slot_);


///* interface for changing the custom widget handling */
//typedef GtkWidget *(* GladeXMLCustomWidgetHandler) (GladeXML *xml,
//						    gchar *func_name,
//						    gchar *name,
//						    gchar *string1,
//						    gchar *string2,
//						    gint int1,
//						    gint int2,
//						    gpointer user_data);

//void glade_set_custom_handler(GladeXMLCustomWidgetHandler handler,
//			      gpointer user_data);


protected:

  
  virtual GType lookup_type_vfunc(const Glib::ustring& classname);


public:

public:
  //C++ methods used to invoke GTK+ virtual functions:

protected:
  //GTK+ Virtual Functions (override these to change behaviour):

  //Default Signal Handlers::


};

} // namespace Glade
} // namespace Gnome


namespace Glib
{
  /** @relates Gnome::Glade::Xml
   * @param object The C instance
   * @param take_copy False if the result should take ownership of the C instance. True if it should take a new copy or ref.
   * @result A C++ instance that wraps this C instance.
   */
  Glib::RefPtr<Gnome::Glade::Xml> wrap(GladeXML* object, bool take_copy = false);
}


#endif /* _LIBGLADEMM_XML_H */