| 1 | #ifndef _GI_GTK_BUILDER_EXTRA_DEF_HPP_ |
| 2 | #define _GI_GTK_BUILDER_EXTRA_DEF_HPP_ |
| 3 | |
| 4 | namespace gi |
| 5 | { |
| 6 | namespace repository |
| 7 | { |
| 8 | namespace Gtk |
| 9 | { |
| 10 | class Builder; |
| 11 | |
| 12 | namespace base |
| 13 | { |
| 14 | class BuilderExtra : public GI_GTK_BUILDER_BASE |
| 15 | { |
| 16 | typedef GI_GTK_BUILDER_BASE super; |
| 17 | |
| 18 | public: |
| 19 | using super::get_object; |
| 20 | |
| 21 | // T must be object-side type (not impl type) |
| 22 | template<typename T> |
| 23 | T get_object(const gi::cstring_v name) |
| 24 | { |
| 25 | auto obj = super::get_object(name); |
| 26 | return gi::object_cast<T>(obj); |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * T_Derived should be a custom subclass of some widget/object BaseClass. |
| 31 | * It should have a constructor of following signature that delegates |
| 32 | * to use the latter's special constructor (see also gtk example); |
| 33 | * |
| 34 | * T_Derived(BaseClass::instance_type instance, Gtk::Builder) : |
| 35 | * BaseClass(instance, this) {} |
| 36 | * |
| 37 | * Of course, it may also have additional arguments, and specify some |
| 38 | * additional arguments to super class constructor, if needed. |
| 39 | * See also comments on the latter. |
| 40 | * |
| 41 | * Note that this function *should* be called at some stage for any created |
| 42 | * C++-type based widget to ensure full and proper "C++ side" setup. |
| 43 | * See also gtk example (for additional comments). |
| 44 | */ |
| 45 | template<typename T_Derived, typename... Args> |
| 46 | gi::ref_ptr<T_Derived> get_object_derived( |
| 47 | const gi::cstring_v name, Args &&...args) |
| 48 | { |
| 49 | // instance type of class that T_Derived is based on |
| 50 | // (Gtk::Window for Gtk::WindowClass) |
| 51 | using instance_type = typename T_Derived::instance_type; |
| 52 | |
| 53 | auto obj = super::get_object(name); |
| 54 | if (!obj) |
| 55 | return {}; |
| 56 | // should be of expected base type |
| 57 | auto wobj = gi::object_cast<instance_type>(obj); |
| 58 | if (!wobj) { |
| 59 | g_error("wrong type (%s)", name.c_str()); |
| 60 | return {}; |
| 61 | } |
| 62 | |
| 63 | // perhaps wrapper already of suitable type |
| 64 | auto wrapper = detail::ObjectClass::instance(obj.gobj_()); |
| 65 | auto wrapper_cast = dynamic_cast<T_Derived *>(wrapper); |
| 66 | if (wrapper && !wrapper_cast) { |
| 67 | g_error("wrong C++ instance type (%s)", name.c_str()); |
| 68 | return {}; |
| 69 | } else if (wrapper_cast) { |
| 70 | return ref_ptr<T_Derived>(wrapper_cast, false); |
| 71 | } |
| 72 | |
| 73 | // obtain builder from this |
| 74 | // (Builder subclass not yet declared/defined at this stage) |
| 75 | auto &builder = *(Builder *)(this); |
| 76 | // make wrapper using suitable constructor signature |
| 77 | // which does not add ref on provided instance |
| 78 | // so ref_ptr needs to grab an extra one |
| 79 | return ref_ptr<T_Derived>( |
| 80 | new T_Derived(wobj, builder, std::forward<Args>(args)...), false); |
| 81 | } |
| 82 | |
| 83 | }; // class |
| 84 | |
| 85 | #undef GI_GTK_BUILDER_BASE |
| 86 | #define GI_GTK_BUILDER_BASE base::BuilderExtra |
| 87 | |
| 88 | } // namespace base |
| 89 | |
| 90 | } // namespace Gtk |
| 91 | |
| 92 | } // namespace repository |
| 93 | |
| 94 | } // namespace gi |
| 95 | |
| 96 | #endif |