Debian Codegraph @

gtk/src/builder.ccg

raw
1 /* Copyright (C) 2007 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <glibmm/vectorutils.h>
18 #include <gtkmm/application.h>
19
20 #include <gtk/gtk.h>
21
22 namespace
23 {
24
25 //TODO: When we can break ABI, replace this GQuark by member data in Gtk::Builder.
26 GQuark quark_no_gtkmm_derived_types = g_quark_from_static_string("gtkmm__Builder::no_gtkmm_derived_types");
27
28 // Allow GtkBuilder to instantiate a gtkmm derived GType instead of the regular
29 // GTK+ GType, so we can, for instance, use our vfuncs and default signal handlers.
30 static GType
31 get_type_from_name_vfunc_callback(GtkBuilder* self, const char* type_name)
32 {
33 if (!type_name)
34 return G_TYPE_INVALID;
35
36 GType gtype = G_TYPE_INVALID;
37
38 // If a TreeModel (such as ListStore or TreeStore) is being built,
39 // Buildable_Class may have requested no search for gtkmm-derived types.
40 // See https://bugzilla.gnome.org/show_bug.cgi?id=742637
41 if (!g_object_get_qdata((GObject*)self, quark_no_gtkmm_derived_types))
42 {
43 // See if there is a gtkmm version of the gclass:
44 Glib::ustring classname_prefixed ("gtkmm__"); // gtkmm uses a prefix
45 classname_prefixed += type_name;
46
47 gtype = g_type_from_name(classname_prefixed.c_str());
48 }
49
50 if (gtype == G_TYPE_INVALID) // If it's not a registered typename
51 {
52 // Get the parent class of the object class (the original underlying C class).
53 Gtk::Builder_Class::BaseClassType* const base =
54 static_cast<Gtk::Builder_Class::BaseClassType*>(
55 g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)));
56
57 // Call the original underlying C function.
58 if (base && base->get_type_from_name)
59 gtype = (*base->get_type_from_name)(self, type_name);
60 else // If that's not possible, just use the normal GType.
61 gtype = g_type_from_name(type_name);
62 }
63
64 return gtype;
65 }
66
67 } // anonymous namespace
68
69 namespace Gtk
70 {
71
72 // Called from Buildable
73 void Builder::set_no_gtkmm_derived_types(bool status)
74 {
75 g_object_set_qdata((GObject*)gobj(), quark_no_gtkmm_derived_types, (gpointer)status);
76 }
77
78 // static
79 Glib::RefPtr<Builder> Builder::create_from_file(const std::string& filename)
80 {
81 auto builder = Builder::create();
82 if(builder->add_from_file(filename))
83 return builder;
84 else
85 return Glib::RefPtr<Builder>();
86 }
87
88 // static
89 Glib::RefPtr<Builder> Builder::create_from_file(const std::string& filename, const char* object_id)
90 {
91 auto builder = Builder::create();
92 if(builder->add_from_file(filename, object_id))
93 return builder;
94 else
95 return Glib::RefPtr<Builder>();
96 }
97
98 //static
99 Glib::RefPtr<Builder> Builder::create_from_file(const std::string& filename, const Glib::ustring& object_id)
100 {
101 auto builder = Builder::create();
102 if(builder->add_from_file(filename, object_id))
103 return builder;
104 else
105 return Glib::RefPtr<Builder>();
106 }
107
108 //static
109 Glib::RefPtr<Builder> Builder::create_from_file(const std::string& filename, const std::vector<Glib::ustring>& object_ids)
110 {
111 auto builder = Builder::create();
112 if(builder->add_from_file(filename, object_ids))
113 return builder;
114 else
115 return Glib::RefPtr<Builder>();
116 }
117
118 // static
119 Glib::RefPtr<Builder> Builder::create_from_resource(const std::string& resource_path)
120 {
121 auto builder = Builder::create();
122 if(builder->add_from_resource(resource_path))
123 return builder;
124 else
125 return Glib::RefPtr<Builder>();
126 }
127
128 // static
129 Glib::RefPtr<Builder> Builder::create_from_resource(const std::string& resource_path, const char* object_id)
130 {
131 auto builder = Builder::create();
132 if(builder->add_from_resource(resource_path, object_id))
133 return builder;
134 else
135 return Glib::RefPtr<Builder>();
136 }
137
138 //static
139 Glib::RefPtr<Builder> Builder::create_from_resource(const std::string& resource_path, const Glib::ustring& object_id)
140 {
141 auto builder = Builder::create();
142 if(builder->add_from_resource(resource_path, object_id))
143 return builder;
144 else
145 return Glib::RefPtr<Builder>();
146 }
147
148 //static
149 Glib::RefPtr<Builder> Builder::create_from_resource(const std::string& resource_path, const std::vector<Glib::ustring>& object_ids)
150 {
151 auto builder = Builder::create();
152 if(builder->add_from_resource(resource_path, object_ids))
153 return builder;
154 else
155 return Glib::RefPtr<Builder>();
156 }
157
158 //static
159 Glib::RefPtr<Builder> Builder::create_from_string(const Glib::ustring& buffer)
160 {
161 auto builder = Builder::create();
162 if(builder->add_from_string(buffer))
163 return builder;
164 else
165 return Glib::RefPtr<Builder>();
166 }
167
168 //static
169 Glib::RefPtr<Builder> Builder::create_from_string(const Glib::ustring& buffer,
170 const char* object_id)
171 {
172 auto builder = Builder::create();
173 if(builder->add_from_string(buffer, object_id))
174 return builder;
175 else
176 return Glib::RefPtr<Builder>();
177 }
178
179 //static
180 Glib::RefPtr<Builder> Builder::create_from_string(const Glib::ustring& buffer,
181 const Glib::ustring& object_id)
182 {
183 auto builder = Builder::create();
184 if(builder->add_from_string(buffer, object_id))
185 return builder;
186 else
187 return Glib::RefPtr<Builder>();
188 }
189
190 //static
191 Glib::RefPtr<Builder> Builder::create_from_string(const Glib::ustring& buffer,
192 const std::vector<Glib::ustring>& object_ids)
193 {
194 auto builder = Builder::create();
195 if(builder->add_from_string(buffer, object_ids))
196 return builder;
197 else
198 return Glib::RefPtr<Builder>();
199 }
200
201 bool Builder::add_from_file(const std::string& filename, const char* object_id)
202 {
203 std::vector<Glib::ustring> object_ids (1, object_id);
204
205 return add_from_file(filename, object_ids);
206 }
207
208 bool Builder::add_from_file(const std::string& filename, const Glib::ustring& object_id)
209 {
210 std::vector<Glib::ustring> object_ids (1, object_id);
211
212 return add_from_file(filename, object_ids);
213 }
214
215 bool Builder::add_from_resource(const std::string& resource_path, const char* object_id)
216 {
217 std::vector<Glib::ustring> object_ids (1, object_id);
218
219 return add_from_resource(resource_path, object_ids);
220 }
221
222 bool Builder::add_from_resource(const std::string& resource_path, const Glib::ustring& object_id)
223 {
224 std::vector<Glib::ustring> object_ids (1, object_id);
225
226 return add_from_resource(resource_path, object_ids);
227 }
228
229 bool Builder::add_from_string(const Glib::ustring& buffer)
230 {
231 GError* gerror = nullptr;
232 bool retvalue = gtk_builder_add_from_string(gobj(), buffer.c_str(), -1 /* means null-terminated */, &(gerror));
233 if(gerror)
234 ::Glib::Error::throw_exception(gerror);
235
236 return retvalue;
237 }
238
239
240 bool Builder::add_from_string(const Glib::ustring& buffer, const char* object_id)
241 {
242 std::vector<Glib::ustring> object_ids (1, object_id);
243
244 return add_from_string(buffer, object_ids);
245 }
246
247 bool Builder::add_from_string(const Glib::ustring& buffer, const Glib::ustring& object_id)
248 {
249 std::vector<Glib::ustring> object_ids (1, object_id);
250
251 return add_from_string(buffer, object_ids);
252 }
253
254 bool Builder::add_from_string(const Glib::ustring& buffer, const std::vector<Glib::ustring>& object_ids)
255 {
256 GError* gerror = nullptr;
257 bool retvalue = gtk_builder_add_objects_from_string(
258 gobj(),
259 buffer.c_str(),
260 -1 /* means null-terminated */,
261 const_cast<char**>(Glib::ArrayHandler<Glib::ustring>::vector_to_array(object_ids).data ()),
262 &(gerror));
263 if(gerror)
264 ::Glib::Error::throw_exception(gerror);
265
266 return retvalue;
267 }
268
269 GtkWidget* Builder::get_cwidget(const Glib::ustring& name)
270 {
271 auto *cobject = gtk_builder_get_object (gobj(), name.c_str());
272 if(!cobject)
273 {
274 g_critical("gtkmm: object `%s' not found in GtkBuilder file.", name.c_str());
275 return nullptr;
276 }
277
278 if (!GTK_IS_WIDGET (cobject))
279 {
280 g_critical("gtkmm: object `%s' (type=`%s') (in GtkBuilder file) is not a widget type.",
281 name.c_str(), G_OBJECT_TYPE_NAME(cobject));
282 // Don't unref cobject. gtk_builder_get_object() does not give us a reference.
283 return nullptr;
284 }
285
286 return GTK_WIDGET (cobject);
287 }
288
289 Gtk::Widget* Builder::get_widget_checked(const Glib::ustring& name, GType type)
290 {
291 // Get the widget from the GtkBuilder file.
292 auto *cobject = get_cwidget(name);
293 if(!cobject)
294 {
295 g_critical("gtkmm: Gtk::Builder: widget `%s' was not found in the GtkBuilder file, or the specified part of it.",
296 name.c_str());
297 return nullptr;
298 }
299
300 // Check if it has the correct type.
301 if(!g_type_is_a(G_OBJECT_TYPE(cobject), type))
302 {
303 g_critical("gtkmm: widget `%s' (in GtkBuilder file) is of type `%s' but `%s' was expected",
304 name.c_str(), G_OBJECT_TYPE_NAME(cobject), g_type_name(type));
305 return nullptr;
306 }
307
308 // Don't take a reference. That's done only in methods that return
309 // a Glib::RefPtr<Something>.
310 return Glib::wrap (GTK_WIDGET (cobject), false /* take ref */);
311 }
312
313 } // namespace Gtk