| 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 <vector> |
| 18 |
|
| 19 |
#include <gtkmm/widget.h> |
| 20 |
#include <giomm/resource.h> //For ResourceError, thrown by create_from_resource(). |
| 21 |
#include <utility> //For std::forward() |
| 22 |
|
| 23 |
_DEFS(gtkmm,gtk) |
| 24 |
_PINCLUDE(glibmm/private/object_p.h) |
| 25 |
|
| 26 |
namespace Gtk |
| 27 |
{ |
| 28 |
class GTKMM_API Application; |
| 29 |
|
| 30 |
/** Exception class for Gdk::Builder errors. |
| 31 |
*/ |
| 32 |
_WRAP_GERROR(BuilderError,GtkBuilderError,GTK_BUILDER_ERROR,decl_prefix GTKMM_API) |
| 33 |
|
| 34 |
/** Build an interface from an XML UI definition description. |
| 35 |
* |
| 36 |
* This object represents an `instantiation' of an UI definition description. |
| 37 |
* When one of these objects is created, the XML file is read, and the user interface |
| 38 |
* is created. The %Gtk::Builder object then provides an interface for accessing the |
| 39 |
* widgets in the user interface by the names assigned to them inside the UI description. |
| 40 |
* |
| 41 |
* A %Gtk::Builder holds a reference to all objects that it has constructed |
| 42 |
* and drops these references when it is deleted. This deletion can |
| 43 |
* cause the destruction of non-widget objects or widgets which are not |
| 44 |
* contained in a toplevel window. For toplevel windows constructed by a |
| 45 |
* builder, it is the responsibility of the user to get them with get_widget() |
| 46 |
* or get_widget_derived() and delete them to get rid of them and all |
| 47 |
* the widgets they contain. |
| 48 |
* |
| 49 |
* The methods get_widget() and get_widget_derived() |
| 50 |
* can be used to access the widgets in the interface by the names assigned |
| 51 |
* to them inside the UI description. Toplevel windows returned by these |
| 52 |
* methods will stay around until the user explicitly deletes them. |
| 53 |
* Other widgets will either be part of a |
| 54 |
* larger hierarchy constructed by the builder (in which case you should |
| 55 |
* not have to worry about their lifecycle), or without a parent, in which |
| 56 |
* case they have to be added to some container before the builder is deleted |
| 57 |
* to make use of them. |
| 58 |
* Non-widget objects need to be fetched with get_object() or get_objects() |
| 59 |
* to keep them beyond the lifespan of the builder. |
| 60 |
* |
| 61 |
* @par %Gtk::Builder and Glib::Property |
| 62 |
* A derived widget that contains Glib::Property members must be registered with |
| 63 |
* its own name in the GType system. It must be registered before any of the |
| 64 |
* create_from_*() or add_from_*() methods are called, meaning that you may have |
| 65 |
* to create an instance of your derived widget just to have its class registered. |
| 66 |
* There is no reasonably easy way to combine %Gtk::Builder with non-widget objects |
| 67 |
* containing Glib::Property members. Combining it with widgets containing |
| 68 |
* Glib::Property members requires glibmm 2.62 or newer. |
| 69 |
* See also get_widget_derived() and Glib::Property. |
| 70 |
* |
| 71 |
* @newin{2,12} |
| 72 |
*/ |
| 73 |
class GTKMM_API Builder : public Glib::Object |
| 74 |
{ |
| 75 |
_CLASS_GOBJECT(Builder, GtkBuilder, GTK_BUILDER, Glib::Object, GObject, , , GTKMM_API) |
| 76 |
|
| 77 |
protected: |
| 78 |
_CTOR_DEFAULT |
| 79 |
_IGNORE(gtk_builder_new) |
| 80 |
|
| 81 |
public: |
| 82 |
/** Creates a new builder object. |
| 83 |
*/ |
| 84 |
_WRAP_CREATE() |
| 85 |
|
| 86 |
/** Parses a file containing a GtkBuilder UI definition. |
| 87 |
* |
| 88 |
* @param filename the name of the file to parse. |
| 89 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 90 |
* @throws BuilderError, Glib::MarkupError, Glib::FileError |
| 91 |
* |
| 92 |
* @newin{2,12} |
| 93 |
*/ |
| 94 |
static Glib::RefPtr<Builder> create_from_file(const std::string& filename); |
| 95 |
|
| 96 |
/** Parses a file containing a GtkBuilder UI definition, building only the requested object. |
| 97 |
* |
| 98 |
* If you are adding an object that depends on an object that is not |
| 99 |
* its child (for instance a GtkTreeView that depends on its |
| 100 |
* GtkTreeModel), you have to explicitely list all of them. |
| 101 |
* |
| 102 |
* @param filename the name of the file to parse. |
| 103 |
* @param object_id The object to build. |
| 104 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 105 |
* @throws BuilderError, Glib::MarkupError, Glib::FileError |
| 106 |
* |
| 107 |
* @newin{2,14} |
| 108 |
*/ |
| 109 |
static Glib::RefPtr<Builder> create_from_file(const std::string& filename, const Glib::ustring& object_id); |
| 110 |
|
| 111 |
//This is just to avoid the ambiguous call when using a string literal, |
| 112 |
//caused by the overload that takes a std::vector<Glib::ustring>. |
| 113 |
/** Parses a file containing a GtkBuilder UI definition, building only the requested object. |
| 114 |
* |
| 115 |
* If you are adding an object that depends on an object that is not |
| 116 |
* its child (for instance a GtkTreeView that depends on its |
| 117 |
* GtkTreeModel), you have to explicitely list all of them. |
| 118 |
* |
| 119 |
* @param filename the name of the file to parse. |
| 120 |
* @param object_id The object to build. |
| 121 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 122 |
* @throws BuilderError, Glib::MarkupError, Glib::FileError |
| 123 |
* |
| 124 |
* @newin{2,16} |
| 125 |
*/ |
| 126 |
static Glib::RefPtr<Builder> create_from_file(const std::string& filename, const char* object_id); |
| 127 |
|
| 128 |
/** Parses a file containing a GtkBuilder UI definition, building only the requested objects. |
| 129 |
* |
| 130 |
* If you are adding an object that depends on an object that is not |
| 131 |
* its child (for instance a GtkTreeView that depends on its |
| 132 |
* GtkTreeModel), you have to explicitely list all of them. |
| 133 |
* |
| 134 |
* @param filename the name of the file to parse. |
| 135 |
* @param object_ids The objects to build. |
| 136 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 137 |
* @throws BuilderError, Glib::MarkupError, Glib::FileError |
| 138 |
* |
| 139 |
* @newin{2,14} |
| 140 |
*/ |
| 141 |
static Glib::RefPtr<Builder> create_from_file(const std::string& filename, const std::vector<Glib::ustring>& object_ids); |
| 142 |
|
| 143 |
|
| 144 |
/** Parses a resource file containing a GtkBuilder UI definition. |
| 145 |
* |
| 146 |
* @param resource_path The path of the resource file to parse. |
| 147 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 148 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 149 |
* |
| 150 |
* @newin{3,6} |
| 151 |
*/ |
| 152 |
static Glib::RefPtr<Builder> create_from_resource(const std::string& resource_path); |
| 153 |
|
| 154 |
/** Parses a resource file containing a GtkBuilder UI definition, building only the requested object. |
| 155 |
* |
| 156 |
* If you are adding an object that depends on an object that is not |
| 157 |
* its child (for instance a GtkTreeView that depends on its |
| 158 |
* GtkTreeModel), you have to explicitely list all of them. |
| 159 |
* |
| 160 |
* @param resource_path The path of the resource file to parse. |
| 161 |
* @param object_id The object to build. |
| 162 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 163 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 164 |
* |
| 165 |
* @newin{3,6} |
| 166 |
*/ |
| 167 |
static Glib::RefPtr<Builder> create_from_resource(const std::string& resource_path, const Glib::ustring& object_id); |
| 168 |
|
| 169 |
//This is just to avoid the ambiguous call when using a string literal, |
| 170 |
//caused by the overload that takes a std::vector<Glib::ustring>. |
| 171 |
/** Parses a resource file containing a GtkBuilder UI definition, building only the requested object. |
| 172 |
* |
| 173 |
* If you are adding an object that depends on an object that is not |
| 174 |
* its child (for instance a GtkTreeView that depends on its |
| 175 |
* GtkTreeModel), you have to explicitely list all of them. |
| 176 |
* |
| 177 |
* @param resource_path The path of the resource file to parse. |
| 178 |
* @param object_id The object to build. |
| 179 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 180 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 181 |
* |
| 182 |
* @newin{3,6} |
| 183 |
*/ |
| 184 |
static Glib::RefPtr<Builder> create_from_resource(const std::string& resource_path, const char* object_id); |
| 185 |
|
| 186 |
/** Parses a resource file containing a GtkBuilder UI definition, building only the requested objects. |
| 187 |
* |
| 188 |
* If you are adding an object that depends on an object that is not |
| 189 |
* its child (for instance a GtkTreeView that depends on its |
| 190 |
* GtkTreeModel), you have to explicitely list all of them. |
| 191 |
* |
| 192 |
* @param resource_path The path of the resource file to parse. |
| 193 |
* @param object_ids The objects to build. |
| 194 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 195 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 196 |
* |
| 197 |
* @newin{3,6} |
| 198 |
*/ |
| 199 |
static Glib::RefPtr<Builder> create_from_resource(const std::string& resource_path, const std::vector<Glib::ustring>& object_ids); |
| 200 |
|
| 201 |
|
| 202 |
/** Parses a string containing a GtkBuilder UI definition. |
| 203 |
* |
| 204 |
* @param buffer: the string to parse |
| 205 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 206 |
* @throws BuilderError, Glib::MarkupError |
| 207 |
* |
| 208 |
* @newin{2,12} |
| 209 |
*/ |
| 210 |
static Glib::RefPtr<Builder> create_from_string(const Glib::ustring& buffer); |
| 211 |
|
| 212 |
//This is just to avoid the ambiguous call when using a string literal, |
| 213 |
//caused by the overload that takes a std::vector<Glib::ustring>. |
| 214 |
/** Parses a string containing a GtkBuilder UI definition building only the requested object. |
| 215 |
* |
| 216 |
* @param buffer The string to parse. |
| 217 |
* @param object_id The object to build. |
| 218 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 219 |
* @throws BuilderError, Glib::MarkupError |
| 220 |
* |
| 221 |
* @newin{2,16} |
| 222 |
*/ |
| 223 |
static Glib::RefPtr<Builder> create_from_string(const Glib::ustring& buffer, const char* object_id); |
| 224 |
|
| 225 |
/** Parses a string containing a GtkBuilder UI definition building only the requested object. |
| 226 |
* |
| 227 |
* @param buffer The string to parse. |
| 228 |
* @param object_id The object to build. |
| 229 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 230 |
* @throws BuilderError, Glib::MarkupError |
| 231 |
* |
| 232 |
* @newin{2,14} |
| 233 |
*/ |
| 234 |
static Glib::RefPtr<Builder> create_from_string(const Glib::ustring& buffer, const Glib::ustring& object_id); |
| 235 |
|
| 236 |
/** Parses a string containing a GtkBuilder UI definition building only the requested objects. |
| 237 |
* |
| 238 |
* If you are adding an object that depends on an object that is not |
| 239 |
* its child (for instance a GtkTreeView that depends on its |
| 240 |
* GtkTreeModel), you have to explicitely list all of them. |
| 241 |
* |
| 242 |
* @param buffer the string to parse |
| 243 |
* @param object_ids The objects to build. |
| 244 |
* @result A new Builder object, or a null pointer if an error occurred. |
| 245 |
* @throws BuilderError, Glib::MarkupError |
| 246 |
* |
| 247 |
* @newin{2,14} |
| 248 |
*/ |
| 249 |
static Glib::RefPtr<Builder> create_from_string(const Glib::ustring& buffer, const std::vector<Glib::ustring>& object_ids); |
| 250 |
_IGNORE(gtk_builder_add_objects_from_file) |
| 251 |
|
| 252 |
/** Parses a file containing a GtkBuilder UI definition, |
| 253 |
* and merges it with the current contents of the builder. |
| 254 |
* |
| 255 |
* @param filename The file to parse. |
| 256 |
* @result true on success or false if an error occurred. |
| 257 |
* @throws BuilderError, Glib::MarkupError, Glib::MarkupError |
| 258 |
* |
| 259 |
* @newin{2,14} |
| 260 |
*/ |
| 261 |
_WRAP_METHOD(bool add_from_file(const std::string& filename), gtk_builder_add_from_file, errthrow) |
| 262 |
|
| 263 |
//This is just to avoid the ambiguous call when using a string literal, |
| 264 |
//caused by the overload that takes a std::vector<Glib::ustring>. |
| 265 |
/** Parses a file containing a GtkBuilder UI definition, |
| 266 |
* building only the requested object, |
| 267 |
* and merges it with the current contents of the builder. |
| 268 |
* |
| 269 |
* If you are adding an object that depends on an object that is not |
| 270 |
* its child (for instance a GtkTreeView that depends on its |
| 271 |
* GtkTreeModel), you have to explicitely list all of them. |
| 272 |
* |
| 273 |
* @param filename The file to parse. |
| 274 |
* @param object_id The object to build. |
| 275 |
* @result true on success or false if an error occurred. |
| 276 |
* @throws BuilderError, Glib::MarkupError, Glib::MarkupError |
| 277 |
* |
| 278 |
* @newin{2,16} |
| 279 |
*/ |
| 280 |
bool add_from_file(const std::string& filename, const char* object_id); |
| 281 |
|
| 282 |
/** Parses a file containing a GtkBuilder UI definition, |
| 283 |
* building only the requested object, |
| 284 |
* and merges it with the current contents of the builder. |
| 285 |
* |
| 286 |
* If you are adding an object that depends on an object that is not |
| 287 |
* its child (for instance a GtkTreeView that depends on its |
| 288 |
* GtkTreeModel), you have to explicitely list all of them. |
| 289 |
* |
| 290 |
* @param filename The file to parse. |
| 291 |
* @param object_id The object to build. |
| 292 |
* @result true on success or false if an error occurred. |
| 293 |
* @throws BuilderError, Glib::MarkupError, Glib::MarkupError |
| 294 |
* |
| 295 |
* @newin{2,14} |
| 296 |
*/ |
| 297 |
bool add_from_file(const std::string& filename, const Glib::ustring& object_id); |
| 298 |
|
| 299 |
#m4 _CONVERSION(`const std::vector<Glib::ustring>&',`gchar**',`const_cast<char**>(Glib::ArrayHandler<Glib::ustring>::vector_to_array($3).data())') |
| 300 |
_WRAP_METHOD(bool add_from_file(const std::string& filename, const std::vector<Glib::ustring>& object_ids), gtk_builder_add_objects_from_file, errthrow) |
| 301 |
|
| 302 |
|
| 303 |
/** Parses a resource file containing a GtkBuilder UI definition, |
| 304 |
* and merges it with the current contents of the builder. |
| 305 |
* |
| 306 |
* @param resource_path The path of the resource file to parse. |
| 307 |
* @result true on success or false if an error occurred. |
| 308 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 309 |
* |
| 310 |
* @newin{3,6} |
| 311 |
*/ |
| 312 |
_WRAP_METHOD(bool add_from_resource(const std::string& resource_path), gtk_builder_add_from_resource, errthrow) |
| 313 |
|
| 314 |
//This is just to avoid the ambiguous call when using a string literal, |
| 315 |
//caused by the overload that takes a std::vector<Glib::ustring>. |
| 316 |
/** Parses a resource file containing a GtkBuilder UI definition, |
| 317 |
* building only the requested object, |
| 318 |
* and merges it with the current contents of the builder. |
| 319 |
* |
| 320 |
* If you are adding an object that depends on an object that is not |
| 321 |
* its child (for instance a GtkTreeView that depends on its |
| 322 |
* GtkTreeModel), you have to explicitely list all of them. |
| 323 |
* |
| 324 |
* @param resource_path The path of the resource file to parse. |
| 325 |
* @param object_id The object to build. |
| 326 |
* @result true on success or false if an error occurred. |
| 327 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 328 |
* |
| 329 |
* @newin{3,6} |
| 330 |
*/ |
| 331 |
bool add_from_resource(const std::string& resource_path, const char* object_id); |
| 332 |
|
| 333 |
/** Parses a resource file containing a GtkBuilder UI definition, |
| 334 |
* building only the requested object, |
| 335 |
* and merges it with the current contents of the builder. |
| 336 |
* |
| 337 |
* If you are adding an object that depends on an object that is not |
| 338 |
* its child (for instance a GtkTreeView that depends on its |
| 339 |
* GtkTreeModel), you have to explicitely list all of them. |
| 340 |
* |
| 341 |
* @param resource_path The path of the resource file to parse. |
| 342 |
* @param object_id The object to build. |
| 343 |
* @result true on success or false if an error occurred. |
| 344 |
* @throws BuilderError, Glib::MarkupError, Gio::ResourceError |
| 345 |
* |
| 346 |
* @newin{3,6} |
| 347 |
*/ |
| 348 |
bool add_from_resource(const std::string& resource_path, const Glib::ustring& object_id); |
| 349 |
|
| 350 |
#m4 _CONVERSION(`const std::vector<Glib::ustring>&',`gchar**',`const_cast<char**>(Glib::ArrayHandler<Glib::ustring>::vector_to_array($3).data())') |
| 351 |
_WRAP_METHOD(bool add_from_resource(const std::string& resource_path, const std::vector<Glib::ustring>& object_ids), gtk_builder_add_objects_from_resource, errthrow) |
| 352 |
|
| 353 |
|
| 354 |
//This is just to avoid the ambiguous call when using a string literal, |
| 355 |
//caused by the overload that takes a std::vector<Glib::ustring>. |
| 356 |
/** Parses a string containing a GtkBuilder UI definition |
| 357 |
* and merges it with the current contents of the builder. |
| 358 |
* |
| 359 |
* @param buffer The string to parse. |
| 360 |
* @result true on success or false if an error occurred. |
| 361 |
* @throws BuilderError, Glib::MarkupError |
| 362 |
* |
| 363 |
* @newin{2,12} |
| 364 |
*/ |
| 365 |
bool add_from_string(const Glib::ustring& buffer); |
| 366 |
|
| 367 |
/** Parses a string containing a GtkBuilder UI definition, |
| 368 |
* building only the requested object, |
| 369 |
* and merges it with the current contents of the builder. |
| 370 |
* |
| 371 |
* @param buffer The string to parse. |
| 372 |
* @param object_id The object to build. |
| 373 |
* @result true on success or false if an error occurred. |
| 374 |
* @throws BuilderError, Glib::MarkupError |
| 375 |
* |
| 376 |
* @newin{2,16} |
| 377 |
*/ |
| 378 |
bool add_from_string(const Glib::ustring& buffer, const char* object_id); |
| 379 |
|
| 380 |
/** Parses a string containing a GtkBuilder UI definition, |
| 381 |
* building only the requested object, |
| 382 |
* and merges it with the current contents of the builder. |
| 383 |
* |
| 384 |
* @param buffer The string to parse. |
| 385 |
* @param object_id The object to build. |
| 386 |
* @result true on success or false if an error occurred. |
| 387 |
* @throws BuilderError, Glib::MarkupError |
| 388 |
* |
| 389 |
* @newin{2,14} |
| 390 |
*/ |
| 391 |
bool add_from_string(const Glib::ustring& buffer, const Glib::ustring& object_id); |
| 392 |
|
| 393 |
/** Parses a string containing a GtkBuilder UI definition, |
| 394 |
* building only the requested objects, |
| 395 |
* and merges it with the current contents of the builder. |
| 396 |
* |
| 397 |
* @param buffer The string to parse. |
| 398 |
* @param object_ids The objects to build. |
| 399 |
* @result true on success or false if an error occurred. |
| 400 |
* @throws BuilderError, Glib::MarkupError |
| 401 |
* |
| 402 |
* @newin{2,14} |
| 403 |
*/ |
| 404 |
bool add_from_string(const Glib::ustring& buffer, const std::vector<Glib::ustring>& object_ids); |
| 405 |
_IGNORE(gtk_builder_add_objects_from_string) |
| 406 |
|
| 407 |
/** Parses a string containing a GtkBuilder UI definition |
| 408 |
* and merges it with the current contents of the builder. |
| 409 |
* |
| 410 |
* @param buffer The string to parse. |
| 411 |
* @param length The length of @a buffer (may be -1 if @a buffer is nul-terminated). |
| 412 |
* @result true on success or false if an error occurred. |
| 413 |
* @throws BuilderError, Glib::MarkupError |
| 414 |
* |
| 415 |
* @newin{2,12} |
| 416 |
*/ |
| 417 |
_WRAP_METHOD(bool add_from_string(const char* buffer, gsize length), gtk_builder_add_from_string, errthrow) |
| 418 |
|
| 419 |
_WRAP_METHOD(void expose_object(const Glib::ustring& name, const Glib::RefPtr<Glib::Object>& object), gtk_builder_expose_object) |
| 420 |
#m4 _CONVERSION(`Widget&',`GObject*',`G_OBJECT(($3).gobj())') |
| 421 |
|
| 422 |
/** Add @a widget to the builder object pool so it can be referenced just like any |
| 423 |
* other object built by builder. |
| 424 |
* |
| 425 |
* @newin{3,12} |
| 426 |
* |
| 427 |
* @param name The name of the widget exposed to the builder. |
| 428 |
* @param widget The widget to expose. |
| 429 |
*/ |
| 430 |
_WRAP_METHOD(void expose_widget(const Glib::ustring& name, Widget& widget{object}), gtk_builder_expose_object) |
| 431 |
|
| 432 |
//TODO: Custom-implement this and prevent it from being used with GTK_WIDGET-derived types? |
| 433 |
// A Builder can contain objects that don't derive from Buildable, |
| 434 |
// for instance if objects have been added with expose_object(). |
| 435 |
/** Gets the object named @a name. |
| 436 |
* |
| 437 |
* @newin{2,12} |
| 438 |
* |
| 439 |
* @param name Name of object to get. |
| 440 |
* @return The object named @a name or <tt>nullptr</tt> if it could not be found in the object tree. |
| 441 |
*/ |
| 442 |
_WRAP_METHOD(Glib::RefPtr<Glib::Object> get_object(const Glib::ustring& name), gtk_builder_get_object, refreturn) |
| 443 |
/** Gets the object named @a name. |
| 444 |
* |
| 445 |
* @newin{3,8} |
| 446 |
* |
| 447 |
* @param name Name of object to get. |
| 448 |
* @return The object named @a name or <tt>nullptr</tt> if it could not be found in the object tree. |
| 449 |
*/ |
| 450 |
_WRAP_METHOD(Glib::RefPtr<const Glib::Object> get_object(const Glib::ustring& name) const, gtk_builder_get_object, refreturn, constversion) |
| 451 |
|
| 452 |
/** Gets a widget from the Builder file. |
| 453 |
* For instance: |
| 454 |
* @code |
| 455 |
* Gtk::Grid* pGrid = nullptr; |
| 456 |
* refBuilder->get_widget("mygrid", pGrid); |
| 457 |
* @endcode |
| 458 |
* This method prints a warning message to the console if the widget |
| 459 |
* doesn't exist or has the wrong type, so you don't need to check that |
| 460 |
* manually. |
| 461 |
* |
| 462 |
* Note that you are responsible for deleting top-level widgets (windows and |
| 463 |
* dialogs) instantiated by the Builder object. Other widgets are instantiated as |
| 464 |
* managed so they will be deleted automatically if you add them to a |
| 465 |
* container widget. |
| 466 |
* |
| 467 |
* @param name The name of the widget. |
| 468 |
* @param[out] widget A pointer to the widget, or <tt>nullptr</tt> on failure. |
| 469 |
*/ |
| 470 |
template <class T_Widget> inline |
| 471 |
void get_widget(const Glib::ustring& name, T_Widget*& widget) |
| 472 |
{ |
| 473 |
// Initialize output parameter: |
| 474 |
widget = nullptr; |
| 475 |
|
| 476 |
// The dynamic_cast<> should never fail if get_widget_checked() succeeded. |
| 477 |
widget = dynamic_cast<T_Widget*>(this->get_widget_checked(name, T_Widget::get_base_type())); |
| 478 |
|
| 479 |
if(!widget) |
| 480 |
g_critical("Gtk::Builder::get_widget(): dynamic_cast<> failed."); |
| 481 |
} |
| 482 |
|
| 483 |
/** See the non-const version. |
| 484 |
* @newin{3,8} |
| 485 |
*/ |
| 486 |
template <class T_Widget> inline |
| 487 |
void get_widget(const Glib::ustring& name, const T_Widget*& widget) const |
| 488 |
{ |
| 489 |
const_cast<Builder*>(this)->get_widget(name, widget); |
| 490 |
} |
| 491 |
|
| 492 |
/** Gets a widget whose details are specified in the GtkBuilder file, |
| 493 |
* but which is implemented by your own derived class. |
| 494 |
* |
| 495 |
* Your class must have a constructor like so: |
| 496 |
* @code |
| 497 |
* MyDerivedDialog::MyDerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refBuilder) |
| 498 |
* : Gtk::Dialog(cobject) //Calls the base class constructor |
| 499 |
* @endcode |
| 500 |
* or, with additional parameters, for example: |
| 501 |
* @code |
| 502 |
* MyDerivedDialog::MyDerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refBuilder, |
| 503 |
* const Glib::ustring& msg, bool warning) |
| 504 |
* : Gtk::Dialog(cobject) //Calls the base class constructor |
| 505 |
* @endcode |
| 506 |
* |
| 507 |
* For instance: |
| 508 |
* @code |
| 509 |
* MyDerivedDialog* pDialog1 = nullptr; |
| 510 |
* MyDerivedDialog* pDialog2 = nullptr; |
| 511 |
* refBuilder->get_widget_derived("mydialog1", pDialog1); |
| 512 |
* refBuilder->get_widget_derived("mydialog2", pDialog2, "A storm is imminent!", true); |
| 513 |
* @endcode |
| 514 |
* |
| 515 |
* @note |
| 516 |
* If get_widget_derived() is called more than once for the same widget (the |
| 517 |
* same @a name), only the first call will call the widget's constructor. |
| 518 |
* The following calls will return a pointer to the widget instance that |
| 519 |
* was constructed by the first call. |
| 520 |
* |
| 521 |
* @par %Gtk::Builder and Glib::Property |
| 522 |
* If the widget contains Glib::Property members, your constructor must call |
| 523 |
* the Glib::ObjectBase constructor with a new GType name: |
| 524 |
* @code |
| 525 |
* MyDerivedDialog::MyDerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refBuilder) |
| 526 |
* : Glib::ObjectBase("MyDialog"), // The GType name will be gtkmm__CustomObject_MyDialog |
| 527 |
* Gtk::Dialog(cobject) // Calls the base class constructor |
| 528 |
* m_my_property(*this, "my-property") |
| 529 |
* @endcode |
| 530 |
* |
| 531 |
* @param name The name of the widget. |
| 532 |
* @param args Additional arguments to pass to the constructor of the derived class. |
| 533 |
* @param[out] widget A pointer to the widget, or <tt>nullptr</tt> on failure. |
| 534 |
*/ |
| 535 |
template <typename T_Widget, typename... Args> inline |
| 536 |
void get_widget_derived(const Glib::ustring& name, T_Widget*& widget, Args&&... args) |
| 537 |
{ |
| 538 |
// Initialize output parameter: |
| 539 |
widget = nullptr; |
| 540 |
|
| 541 |
// Get the widget from the GtkBuilder file. |
| 542 |
using cwidget_type = typename T_Widget::BaseObjectType; |
| 543 |
auto pCWidget = (cwidget_type*)get_cwidget(name); |
| 544 |
|
| 545 |
//The error was already reported by get_cwidget(). |
| 546 |
if(!pCWidget) |
| 547 |
return; |
| 548 |
|
| 549 |
//Check whether there is already a C++ wrapper instance associated with this C instance: |
| 550 |
Glib::ObjectBase* pObjectBase = ObjectBase::_get_current_wrapper((GObject*)pCWidget); |
| 551 |
|
| 552 |
//If there is already a C++ instance, then return it again: |
| 553 |
if(pObjectBase) |
| 554 |
{ |
| 555 |
widget = dynamic_cast<T_Widget*>( Glib::wrap((GtkWidget*)pCWidget) ); |
| 556 |
//Newer, more spec-complaint, versions of g++ cannot resolve a specific wrap() function in a template. |
| 557 |
|
| 558 |
//The dynamic cast checks that it is of the correct type. |
| 559 |
//Somebody might be trying to call get_widget_derived() after already calling get_widget(), |
| 560 |
//or after already calling get_widget_derived() with a different derived C++ type. |
| 561 |
if(!widget) |
| 562 |
g_critical("Gtk::Builder::get_widget_derived(): dynamic_cast<> failed. An existing C++ instance, of a different type, seems to exist."); |
| 563 |
} |
| 564 |
else |
| 565 |
{ |
| 566 |
//Create a new C++ instance to wrap the existing C instance: |
| 567 |
|
| 568 |
//Set the output variable. We needed to do this because we can not template the return type. |
| 569 |
Glib::RefPtr<Gtk::Builder> refThis(this); |
| 570 |
refThis->reference(); //take a copy. |
| 571 |
widget = new T_Widget(pCWidget, refThis, std::forward<Args>(args)...); |
| 572 |
// Don't add reference to widget. That's done only in methods that return |
| 573 |
// a Glib::RefPtr<Something>. |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/** See the non-const version. |
| 578 |
* @newin{3,8} |
| 579 |
*/ |
| 580 |
template <typename T_Widget, typename... Args> inline |
| 581 |
void get_widget_derived(const Glib::ustring& name, const T_Widget*& widget, Args&&... args) const |
| 582 |
{ |
| 583 |
const_cast<Builder*>(this)->get_widget_derived(name, widget, std::forward<Args>(args)...); |
| 584 |
} |
| 585 |
|
| 586 |
#m4 _CONVERSION(`GSList*',`std::vector<Glib::RefPtr<Glib::Object> >',`Glib::SListHandler<Glib::RefPtr<Glib::Object> >::slist_to_vector($3, Glib::OWNERSHIP_SHALLOW)') |
| 587 |
_WRAP_METHOD(std::vector<Glib::RefPtr<Glib::Object> > get_objects(), gtk_builder_get_objects) |
| 588 |
|
| 589 |
#m4 _CONVERSION(`GSList*',`std::vector<Glib::RefPtr<const Glib::Object> >',`Glib::SListHandler<Glib::RefPtr<const Glib::Object> >::slist_to_vector($3, Glib::OWNERSHIP_SHALLOW)') |
| 590 |
_WRAP_METHOD(std::vector<Glib::RefPtr<const Glib::Object> > get_objects() const, gtk_builder_get_objects) |
| 591 |
|
| 592 |
//_WRAP_METHOD(void connect_signals(gpointer user_data), gtk_builder_connect_signals) |
| 593 |
//_WRAP_METHOD(void connect_signals_full(GtkBuilderConnectFunc func, gpointer user_data), gtk_builder_connect_signals_full) |
| 594 |
_WRAP_METHOD(void set_translation_domain(const Glib::ustring& domain), gtk_builder_set_translation_domain) |
| 595 |
_WRAP_METHOD(Glib::ustring get_translation_domain() const, gtk_builder_get_translation_domain) |
| 596 |
|
| 597 |
_WRAP_METHOD(void set_application(const Glib::RefPtr<Application>& application), gtk_builder_set_application) |
| 598 |
_WRAP_METHOD(Glib::RefPtr<Application> get_application(), gtk_builder_get_application, refreturn) |
| 599 |
_WRAP_METHOD(Glib::RefPtr<const Application> get_application() const, gtk_builder_get_application, refreturn, constversion) |
| 600 |
|
| 601 |
//We ignore gtk_builder_get_type_from_name() because it only seems useful when implementing GtkBuildable for widgets. |
| 602 |
_IGNORE(gtk_builder_get_type_from_name) |
| 603 |
|
| 604 |
_WRAP_PROPERTY("translation-domain", Glib::ustring) |
| 605 |
|
| 606 |
protected: |
| 607 |
Gtk::Widget* get_widget_checked(const Glib::ustring& name, GType type); |
| 608 |
GtkWidget* get_cwidget(const Glib::ustring& name); |
| 609 |
|
| 610 |
private: |
| 611 |
void set_no_gtkmm_derived_types(bool status); |
| 612 |
friend class GTKMM_API Buildable_Class; |
| 613 |
|
| 614 |
#m4begin |
| 615 |
dnl Custom-coded vfunc: |
| 616 |
dnl |
| 617 |
_PUSH(SECTION_PCC_CLASS_INIT_VFUNCS) |
| 618 |
klass->get_type_from_name = &get_type_from_name_vfunc_callback; |
| 619 |
_POP() |
| 620 |
#m4end |
| 621 |
}; |
| 622 |
|
| 623 |
} // namespace Gtk |