| 1 | #ifndef _GI_GTK_GTK_EXTRA_DEF_HPP_ |
| 2 | #define _GI_GTK_GTK_EXTRA_DEF_HPP_ |
| 3 | |
| 4 | #include <map> |
| 5 | #include <string> |
| 6 | |
| 7 | namespace gi |
| 8 | { |
| 9 | namespace repository |
| 10 | { |
| 11 | namespace Gtk |
| 12 | { |
| 13 | namespace base |
| 14 | { |
| 15 | #ifdef _GI_GTK_LIST_STORE_EXTRA_DEF_HPP_ |
| 16 | // deprecated (as of about 4.9.1) |
| 17 | // so only define implementation here if declaration has been included |
| 18 | template<typename... Args> |
| 19 | Gtk::ListStore |
| 20 | ListStoreExtra::new_type_() noexcept |
| 21 | { |
| 22 | GType columns[] = {traits::gtype<Args>::get_type()...}; |
| 23 | return ListStoreBase::new_(G_N_ELEMENTS(columns), columns); |
| 24 | }; |
| 25 | #endif |
| 26 | } // namespace base |
| 27 | |
| 28 | // class generated parts are needed |
| 29 | #if !GTK_CHECK_VERSION(4, 0, 0) || GI_CLASS_IMPL |
| 30 | namespace impl |
| 31 | { |
| 32 | // gtk widget template helper |
| 33 | class WidgetTemplateHelper |
| 34 | { |
| 35 | public: |
| 36 | enum class ConnectObject { NONE, TAIL, HEAD }; |
| 37 | |
| 38 | protected: |
| 39 | using ConnectData = |
| 40 | std::map<GType, std::map<std::string, gi::repository::GObject::Closure>>; |
| 41 | |
| 42 | // inner namespace |
| 43 | class Internal |
| 44 | { |
| 45 | template<typename TUPLE> |
| 46 | struct tuple_strip_last; |
| 47 | |
| 48 | template<typename... T> |
| 49 | struct tuple_strip_last<std::tuple<T...>> |
| 50 | { |
| 51 | protected: |
| 52 | using Tuple = std::tuple<T...>; |
| 53 | template<typename W> |
| 54 | struct inner; |
| 55 | |
| 56 | template<std::size_t... Index> |
| 57 | struct inner<std::index_sequence<Index...>> |
| 58 | { |
| 59 | using type = std::tuple< |
| 60 | typename std::tuple_element<Index, std::tuple<T...>>::type...>; |
| 61 | }; |
| 62 | |
| 63 | public: |
| 64 | using type = |
| 65 | typename inner<std::make_index_sequence<sizeof...(T) - 1>>::type; |
| 66 | }; |
| 67 | |
| 68 | public: |
| 69 | struct ProxyClosure |
| 70 | { |
| 71 | GClosure base; |
| 72 | // actual target closure |
| 73 | GObject::Closure actual; |
| 74 | // duplicated from actual, holds ownership |
| 75 | GCallback cb; |
| 76 | gpointer user_data; |
| 77 | // perhaps through a thunk call helper |
| 78 | GObject::Closure thunk; |
| 79 | // no ref, guarded by watch |
| 80 | ::GObject *object; |
| 81 | gboolean swapped; |
| 82 | }; |
| 83 | |
| 84 | // FUNCTOR represents C helper that transforms to Cpp call |
| 85 | // so it has C-types of FUNCTOR follows by functor user_data |
| 86 | template<ConnectObject CONNECT, typename FUNCTOR> |
| 87 | struct make_thunk : public std::false_type |
| 88 | { |
| 89 | struct call_type |
| 90 | { |
| 91 | // call should accept (signal emit object, signal args, proxy user_data) |
| 92 | // and transform this to a call in FUNCTOR form |
| 93 | // (suitably adding extra tail object and optionally swapping) |
| 94 | static void call() {} |
| 95 | }; |
| 96 | }; |
| 97 | |
| 98 | // essentially SWAPPED case |
| 99 | template<typename R, typename FirstArg, typename... Args> |
| 100 | struct make_thunk<ConnectObject::HEAD, R (*)(FirstArg, Args...)> |
| 101 | { |
| 102 | // ... so FirstArg is extra object |
| 103 | static_assert(std::is_pointer<FirstArg>::value, ""); |
| 104 | // remove user data |
| 105 | using ArgsType = typename tuple_strip_last<std::tuple<Args...>>::type; |
| 106 | // signal emit object |
| 107 | using ObjectArgType = |
| 108 | typename std::tuple_element<sizeof...(Args) - 2, ArgsType>::type; |
| 109 | static_assert(std::is_pointer<ObjectArgType>::value, ""); |
| 110 | using SignalArgsType = typename tuple_strip_last<ArgsType>::type; |
| 111 | |
| 112 | template<typename T> |
| 113 | struct thunk; |
| 114 | |
| 115 | template<typename... TArgs> |
| 116 | struct thunk<std::tuple<TArgs...>> |
| 117 | { |
| 118 | // transform to call to |
| 119 | // (extra object, signal args, signal emit object, functor user_data) |
| 120 | static R call(ObjectArgType arg, TArgs... args, gpointer user_data) |
| 121 | { |
| 122 | auto tdata = (ProxyClosure *)(user_data); |
| 123 | typedef R (*func_type)( |
| 124 | ::GObject *, TArgs..., ObjectArgType, gpointer); |
| 125 | return ((func_type)(tdata->cb))( |
| 126 | tdata->object, args..., arg, tdata->user_data); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | using call_type = thunk<SignalArgsType>; |
| 131 | }; |
| 132 | |
| 133 | // essentially non-SWAPPED case |
| 134 | template<typename R, typename... Args> |
| 135 | struct make_thunk<ConnectObject::TAIL, R (*)(Args...)> |
| 136 | { |
| 137 | // remove user data |
| 138 | using AllArgsType = typename tuple_strip_last<std::tuple<Args...>>::type; |
| 139 | // extra object |
| 140 | using ObjectArgType = |
| 141 | typename std::tuple_element<sizeof...(Args) - 2, AllArgsType>::type; |
| 142 | static_assert(std::is_pointer<ObjectArgType>::value, ""); |
| 143 | |
| 144 | template<typename T> |
| 145 | struct thunk; |
| 146 | |
| 147 | template<typename... TArgs> |
| 148 | struct thunk<std::tuple<TArgs...>> |
| 149 | { |
| 150 | // transform to call to |
| 151 | // (signal emit object, signal args, extra object, functor user_data) |
| 152 | static R call(TArgs... args, gpointer user_data) |
| 153 | { |
| 154 | auto tdata = (ProxyClosure *)(user_data); |
| 155 | typedef R (*func_type)(TArgs..., ::GObject *, gpointer); |
| 156 | return ((func_type)(tdata->cb))( |
| 157 | args..., tdata->object, tdata->user_data); |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | using SignalArgsType = typename tuple_strip_last<AllArgsType>::type; |
| 162 | using call_type = thunk<SignalArgsType>; |
| 163 | }; |
| 164 | |
| 165 | static void proxy_marshal(GClosure *closure, GValue *return_value, |
| 166 | guint n_param_values, const GValue *param_values, |
| 167 | gpointer invocation_hint, gpointer marshal_data) |
| 168 | { |
| 169 | // should only be used internally in gclosure for class closure cases |
| 170 | g_return_if_fail(marshal_data == NULL); |
| 171 | |
| 172 | // call actual target if one has been provided |
| 173 | auto pclosure = (ProxyClosure *)(closure); |
| 174 | if (pclosure->actual) { |
| 175 | // might have to go through thunk if extra object |
| 176 | g_assert(!!pclosure->thunk == !!pclosure->object); |
| 177 | auto tclosure = pclosure->thunk ? pclosure->thunk.gobj_() |
| 178 | : pclosure->actual.gobj_(); |
| 179 | g_closure_invoke(tclosure, return_value, n_param_values, param_values, |
| 180 | invocation_hint); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static void proxy_finalize(gpointer, GClosure *closure) |
| 185 | { |
| 186 | auto pclosure = (ProxyClosure *)(closure); |
| 187 | pclosure->actual = nullptr; |
| 188 | pclosure->thunk = nullptr; |
| 189 | } |
| 190 | |
| 191 | static GObject::Closure make_proxy_closure( |
| 192 | ::GObject *object = nullptr, bool swapped = false) |
| 193 | { |
| 194 | auto closure = g_closure_new_simple(sizeof(ProxyClosure), nullptr); |
| 195 | g_closure_set_marshal(closure, proxy_marshal); |
| 196 | g_closure_sink(g_closure_ref(closure)); |
| 197 | g_closure_add_finalize_notifier(closure, nullptr, proxy_finalize); |
| 198 | // extra tracking |
| 199 | auto pclosure = (ProxyClosure *)closure; |
| 200 | if (object) { |
| 201 | g_object_watch_closure(object, closure); |
| 202 | pclosure->object = object; |
| 203 | // only relevant if object |
| 204 | pclosure->swapped = swapped; |
| 205 | } |
| 206 | return gi::wrap(closure, gi::transfer_full); |
| 207 | } |
| 208 | |
| 209 | // work-around missing inline support |
| 210 | static ConnectData *&get_connect_data() |
| 211 | { |
| 212 | thread_local ConnectData *connect_data; |
| 213 | return connect_data; |
| 214 | } |
| 215 | |
| 216 | static GQuark get_template_quark() |
| 217 | { |
| 218 | static const char *KEY = "GIOBJECT_TEMPLATE"; |
| 219 | static GQuark q = g_quark_from_static_string(KEY); |
| 220 | return q; |
| 221 | } |
| 222 | |
| 223 | static void builder_connect_function(GtkBuilder *builder, ::GObject *object, |
| 224 | const gchar *signal_name, const gchar *handler_name, |
| 225 | ::GObject *connect_object, GConnectFlags flags, gpointer user_data) |
| 226 | { |
| 227 | (void)builder; |
| 228 | (void)user_data; |
| 229 | |
| 230 | auto closure = Internal::make_proxy_closure( |
| 231 | connect_object, flags & G_CONNECT_SWAPPED); |
| 232 | g_signal_connect_closure( |
| 233 | object, signal_name, closure.gobj_(), flags & G_CONNECT_AFTER); |
| 234 | |
| 235 | auto connect_data = get_connect_data(); |
| 236 | g_assert(connect_data); |
| 237 | if (connect_data) { |
| 238 | (*connect_data)[G_OBJECT_TYPE(object)][handler_name] = closure; |
| 239 | } |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | #if GTK_CHECK_VERSION(4, 0, 0) |
| 244 | class BuilderScope : public Gtk::impl::BuilderCScopeImpl |
| 245 | { |
| 246 | public: |
| 247 | BuilderScope() : Gtk::impl::BuilderCScopeImpl(this) {} |
| 248 | |
| 249 | GObject::Closure create_closure_(Gtk::Builder builder, |
| 250 | const gi::cstring_v function_name, Gtk::BuilderClosureFlags flags, |
| 251 | GObject::Object object, GLib::Error *_error) override |
| 252 | { |
| 253 | (void)_error; |
| 254 | |
| 255 | // current object is the template'd one being init'ed |
| 256 | // a direct call is used as a wrapped call might fiddle with refs |
| 257 | // while the object is still being init'ed, which does not end well |
| 258 | auto current = gtk_builder_get_current_object(builder.gobj_()); |
| 259 | auto connect_data = current ? (ConnectData *)g_object_get_qdata( |
| 260 | current, Internal::get_template_quark()) |
| 261 | : nullptr; |
| 262 | g_assert(connect_data); |
| 263 | if (connect_data) { |
| 264 | auto wrapped = Internal::make_proxy_closure( |
| 265 | object.gobj_(), (flags & Gtk::BuilderClosureFlags::SWAPPED_) == |
| 266 | Gtk::BuilderClosureFlags::SWAPPED_); |
| 267 | (*connect_data)[G_OBJECT_TYPE(current)][function_name] = wrapped; |
| 268 | return wrapped; |
| 269 | } |
| 270 | |
| 271 | return nullptr; |
| 272 | } |
| 273 | }; |
| 274 | #endif |
| 275 | |
| 276 | // owned by qdata |
| 277 | ConnectData *connect_data_; |
| 278 | |
| 279 | public: |
| 280 | WidgetTemplateHelper(gi::repository::GObject::Object object) |
| 281 | { |
| 282 | if (object) { |
| 283 | connect_data_ = (ConnectData *)g_object_get_qdata( |
| 284 | object.gobj_(), Internal::get_template_quark()); |
| 285 | g_assert(connect_data_); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | template<GType (*typefunc)() = nullptr> |
| 290 | static void custom_class_init(gpointer klass, gpointer) |
| 291 | { |
| 292 | G_OBJECT_CLASS(klass)->dispose = custom_dispose<typefunc>; |
| 293 | #if GTK_CHECK_VERSION(4, 0, 0) |
| 294 | auto builder = gi::make_ref<BuilderScope>(); |
| 295 | gtk_widget_class_set_template_scope( |
| 296 | GTK_WIDGET_CLASS(klass), GTK_BUILDER_SCOPE(builder->gobj_())); |
| 297 | #else |
| 298 | gtk_widget_class_set_connect_func(GTK_WIDGET_CLASS(klass), |
| 299 | Internal::builder_connect_function, nullptr, nullptr); |
| 300 | #endif |
| 301 | } |
| 302 | |
| 303 | static void custom_init(GtkWidget *instance, gpointer) |
| 304 | { |
| 305 | // avoid namespace interference |
| 306 | using GObject = ::GObject; |
| 307 | auto tq = Internal::get_template_quark(); |
| 308 | // prepare storage |
| 309 | // in case of multi-level subclassing, it is used for all |
| 310 | ConnectData *cdp; |
| 311 | if (!(cdp = (ConnectData *)g_object_get_qdata(G_OBJECT(instance), tq))) { |
| 312 | auto deleter = [](gpointer d) { delete (ConnectData *)d; }; |
| 313 | auto cd = std::make_unique<ConnectData>(); |
| 314 | cdp = cd.get(); |
| 315 | g_object_set_qdata_full(G_OBJECT(instance), tq, cd.release(), deleter); |
| 316 | } |
| 317 | #if GTK_CHECK_VERSION(4, 0, 0) |
| 318 | (void)cdp; |
| 319 | gtk_widget_init_template(GTK_WIDGET(instance)); |
| 320 | #else |
| 321 | // unfortunate API (which does not provide access to instance) |
| 322 | // make ConnectData instance active on this thread |
| 323 | Internal::get_connect_data() = cdp; |
| 324 | gtk_widget_init_template(GTK_WIDGET(instance)); |
| 325 | // so we got all that during above _init call |
| 326 | Internal::get_connect_data() = nullptr; |
| 327 | #endif |
| 328 | } |
| 329 | |
| 330 | template<GType (*typefunc)()> |
| 331 | static void custom_dispose(::GObject *gobject) |
| 332 | { |
| 333 | // this intermediate step avoids a -Waddress warning on recent gcc |
| 334 | auto tf = typefunc; |
| 335 | auto gtype = tf ? typefunc() : G_OBJECT_TYPE(gobject); |
| 336 | |
| 337 | #if GTK_CHECK_VERSION(4, 8, 0) |
| 338 | gtk_widget_dispose_template(GTK_WIDGET(gobject), gtype); |
| 339 | #endif |
| 340 | // need to know our place in the type chain |
| 341 | // (in C, this is usually handled by the parent_class variable) |
| 342 | // fallback to the leaf type if all else fails, but that too may fail |
| 343 | G_OBJECT_CLASS(g_type_class_peek(g_type_parent(gtype)))->dispose(gobject); |
| 344 | } |
| 345 | |
| 346 | template<typename F, ConnectObject c = ConnectObject::NONE, typename Functor> |
| 347 | bool set_handler(gi::cstring_v handler_name, Functor &&f, GType gtype = 0) |
| 348 | { |
| 349 | auto _cd = (ConnectData *)connect_data_; |
| 350 | if (!_cd) { |
| 351 | g_assert_not_reached(); |
| 352 | return false; |
| 353 | } |
| 354 | auto cd = *_cd; |
| 355 | // find name in collected data |
| 356 | Internal::ProxyClosure *proxy{}; |
| 357 | for (auto &e : cd) { |
| 358 | if (gtype != 0 && gtype != e.first) |
| 359 | continue; |
| 360 | auto it = e.second.find(handler_name); |
| 361 | if (it != e.second.end()) { |
| 362 | // perhaps found one already |
| 363 | if (proxy) { |
| 364 | g_warning("ambiguous template entry for %s", handler_name.c_str()); |
| 365 | return false; |
| 366 | } |
| 367 | proxy = (Internal::ProxyClosure *)(it->second.gobj_()); |
| 368 | // continue search to check for conflicts in case of wildcard type |
| 369 | if (gtype != 0) |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | if (!proxy) |
| 374 | return false; |
| 375 | // sanity checks; provided parameters match UI data |
| 376 | g_return_val_if_fail(!!proxy->object == (c != ConnectObject::NONE), FALSE); |
| 377 | g_return_val_if_fail( |
| 378 | !proxy->object || proxy->swapped == (c == ConnectObject::HEAD), FALSE); |
| 379 | // go go |
| 380 | // standard closure, also owns functor data |
| 381 | auto w = |
| 382 | new gi::detail::transform_signal_wrapper<F>(std::forward<Functor>(f)); |
| 383 | auto closure = g_cclosure_new( |
| 384 | (GCallback)&w->wrapper, w, (GClosureNotify)(GCallback)&w->destroy); |
| 385 | g_closure_sink(g_closure_ref(closure)); |
| 386 | g_closure_set_marshal(closure, g_cclosure_marshal_generic); |
| 387 | proxy->cb = (GCallback)&w->wrapper; |
| 388 | proxy->user_data = w; |
| 389 | proxy->actual = gi::wrap(closure, gi::transfer_full); |
| 390 | proxy->thunk = nullptr; |
| 391 | // optional thunk closure to invoke helper thunk to handle object |
| 392 | if (proxy->object) { |
| 393 | auto cb_thunk = (GCallback)&Internal::make_thunk<c, |
| 394 | decltype(&w->wrapper)>::call_type::call; |
| 395 | auto thunk = g_cclosure_new(cb_thunk, proxy, nullptr); |
| 396 | g_closure_set_marshal(thunk, g_cclosure_marshal_generic); |
| 397 | g_closure_sink(g_closure_ref(thunk)); |
| 398 | proxy->thunk = gi::wrap(thunk, gi::transfer_full); |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | }; |
| 403 | } // namespace impl |
| 404 | #endif |
| 405 | |
| 406 | } // namespace Gtk |
| 407 | |
| 408 | } // namespace repository |
| 409 | |
| 410 | } // namespace gi |
| 411 | |
| 412 | #endif |