From adcc1d98ea0f7afb79b5687d4f652fbc1f7cb7ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Tue, 13 Aug 2019 00:49:47 +0200 Subject: gjs-private: Move GTK override into util Now that the function no longer uses GTK directly, there is no reason for keeping it separate from other utility functions. https://gitlab.gnome.org/GNOME/gjs/issues/99 --- libgjs-private/gjs-gtk-util.c | 109 ------------------------------------------ libgjs-private/gjs-gtk-util.h | 42 ---------------- libgjs-private/gjs-util.c | 81 +++++++++++++++++++++++++++++++ libgjs-private/gjs-util.h | 6 +++ 4 files changed, 87 insertions(+), 151 deletions(-) delete mode 100644 libgjs-private/gjs-gtk-util.c delete mode 100644 libgjs-private/gjs-gtk-util.h (limited to 'libgjs-private') diff --git a/libgjs-private/gjs-gtk-util.c b/libgjs-private/gjs-gtk-util.c deleted file mode 100644 index 2b4ab563..00000000 --- a/libgjs-private/gjs-gtk-util.c +++ /dev/null @@ -1,109 +0,0 @@ -/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ -/* Copyright 2014 Endless Mobile, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#include // for NULL - -#include -#include -#include - -#include "libgjs-private/gjs-gtk-util.h" - -static GParamSpec* gjs_gtk_container_class_find_child_property( - GIObjectInfo* container_info, GObject* container, const char* property) { - GIBaseInfo* class_info = NULL; - GIBaseInfo* find_child_property_fun = NULL; - - GIArgument ret; - GIArgument find_child_property_args[2]; - - class_info = g_object_info_get_class_struct(container_info); - find_child_property_fun = - g_struct_info_find_method(class_info, "find_child_property"); - - find_child_property_args[0].v_pointer = G_OBJECT_GET_CLASS(container); - find_child_property_args[1].v_string = (char*)property; - - g_function_info_invoke(find_child_property_fun, find_child_property_args, 2, - NULL, 0, &ret, NULL); - - g_clear_pointer(&class_info, g_base_info_unref); - g_clear_pointer(&find_child_property_fun, g_base_info_unref); - - return (GParamSpec*)ret.v_pointer; -} - -void gjs_gtk_container_child_set_property(GObject* container, GObject* child, - const char* property, - const GValue* value) { - GParamSpec* pspec = NULL; - GIBaseInfo* base_info = NULL; - GIBaseInfo* child_set_property_fun = NULL; - GIObjectInfo* container_info; - GValue value_arg = G_VALUE_INIT; - GIArgument ret; - - GIArgument child_set_property_args[4]; - - base_info = g_irepository_find_by_name(NULL, "Gtk", "Container"); - container_info = (GIObjectInfo*)base_info; - - pspec = gjs_gtk_container_class_find_child_property(container_info, - container, property); - if (pspec == NULL) { - g_warning("%s does not have a property called %s", - g_type_name(G_OBJECT_TYPE(container)), property); - goto out; - } - - if ((G_VALUE_TYPE(value) == G_TYPE_POINTER) && - (g_value_get_pointer(value) == NULL) && - !g_value_type_transformable(G_VALUE_TYPE(value), pspec->value_type)) { - /* Set an empty value. This will happen when we set a NULL value from - * JS. Since GJS doesn't know the GParamSpec for this property, it will - * just put NULL into a G_TYPE_POINTER GValue, which will later fail - * when trying to transform it to the GParamSpec's GType. - */ - g_value_init(&value_arg, pspec->value_type); - } else { - g_value_init(&value_arg, G_VALUE_TYPE(value)); - g_value_copy(value, &value_arg); - } - - child_set_property_fun = - g_object_info_find_method(container_info, "child_set_property"); - - child_set_property_args[0].v_pointer = container; - child_set_property_args[1].v_pointer = child; - child_set_property_args[2].v_string = (char*)property; - child_set_property_args[3].v_pointer = &value_arg; - - g_function_info_invoke(child_set_property_fun, child_set_property_args, 4, - NULL, 0, &ret, NULL); - - g_value_unset(&value_arg); - -out: - g_clear_pointer(&pspec, g_param_spec_unref); - g_clear_pointer(&base_info, g_base_info_unref); - g_clear_pointer(&child_set_property_fun, g_base_info_unref); -} diff --git a/libgjs-private/gjs-gtk-util.h b/libgjs-private/gjs-gtk-util.h deleted file mode 100644 index 20492aa5..00000000 --- a/libgjs-private/gjs-gtk-util.h +++ /dev/null @@ -1,42 +0,0 @@ -/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ -/* Copyright 2014 Endless Mobile, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef LIBGJS_PRIVATE_GJS_GTK_UTIL_H_ -#define LIBGJS_PRIVATE_GJS_GTK_UTIL_H_ - -#include /* for ENABLE_GTK */ - -#include -#include - -#include "gjs/macros.h" - -G_BEGIN_DECLS - -GJS_EXPORT -void gjs_gtk_container_child_set_property(GObject* container, GObject* child, - const char* property, - const GValue* value); - -G_END_DECLS - -#endif /* LIBGJS_PRIVATE_GJS_GTK_UTIL_H_ */ diff --git a/libgjs-private/gjs-util.c b/libgjs-private/gjs-util.c index f27e4c76..00e3f6d1 100644 --- a/libgjs-private/gjs-util.c +++ b/libgjs-private/gjs-util.c @@ -28,6 +28,7 @@ #include #include +#include #include #include /* for bindtextdomain, bind_textdomain_codeset, textdomain */ @@ -204,3 +205,83 @@ int gjs_open_bytes(GBytes* bytes, GError** error) { g_error("%s is currently supported on UNIX only", __func__); #endif } + +static GParamSpec* gjs_gtk_container_class_find_child_property( + GIObjectInfo* container_info, GObject* container, const char* property) { + GIBaseInfo* class_info = NULL; + GIBaseInfo* find_child_property_fun = NULL; + + GIArgument ret; + GIArgument find_child_property_args[2]; + + class_info = g_object_info_get_class_struct(container_info); + find_child_property_fun = + g_struct_info_find_method(class_info, "find_child_property"); + + find_child_property_args[0].v_pointer = G_OBJECT_GET_CLASS(container); + find_child_property_args[1].v_string = (char*)property; + + g_function_info_invoke(find_child_property_fun, find_child_property_args, 2, + NULL, 0, &ret, NULL); + + g_clear_pointer(&class_info, g_base_info_unref); + g_clear_pointer(&find_child_property_fun, g_base_info_unref); + + return (GParamSpec*)ret.v_pointer; +} + +void gjs_gtk_container_child_set_property(GObject* container, GObject* child, + const char* property, + const GValue* value) { + GParamSpec* pspec = NULL; + GIBaseInfo* base_info = NULL; + GIBaseInfo* child_set_property_fun = NULL; + GIObjectInfo* container_info; + GValue value_arg = G_VALUE_INIT; + GIArgument ret; + + GIArgument child_set_property_args[4]; + + base_info = g_irepository_find_by_name(NULL, "Gtk", "Container"); + container_info = (GIObjectInfo*)base_info; + + pspec = gjs_gtk_container_class_find_child_property(container_info, + container, property); + if (pspec == NULL) { + g_warning("%s does not have a property called %s", + g_type_name(G_OBJECT_TYPE(container)), property); + goto out; + } + + if ((G_VALUE_TYPE(value) == G_TYPE_POINTER) && + (g_value_get_pointer(value) == NULL) && + !g_value_type_transformable(G_VALUE_TYPE(value), pspec->value_type)) { + /* Set an empty value. This will happen when we set a NULL value from + * JS. Since GJS doesn't know the GParamSpec for this property, it will + * just put NULL into a G_TYPE_POINTER GValue, which will later fail + * when trying to transform it to the GParamSpec's GType. + */ + g_value_init(&value_arg, pspec->value_type); + } else { + g_value_init(&value_arg, G_VALUE_TYPE(value)); + g_value_copy(value, &value_arg); + } + + child_set_property_fun = + g_object_info_find_method(container_info, "child_set_property"); + + child_set_property_args[0].v_pointer = container; + child_set_property_args[1].v_pointer = child; + child_set_property_args[2].v_string = (char*)property; + child_set_property_args[3].v_pointer = &value_arg; + + g_function_info_invoke(child_set_property_fun, child_set_property_args, 4, + NULL, 0, &ret, NULL); + + g_value_unset(&value_arg); + +out: + g_clear_pointer(&pspec, g_param_spec_unref); + g_clear_pointer(&base_info, g_base_info_unref); + g_clear_pointer(&child_set_property_fun, g_base_info_unref); +} diff --git a/libgjs-private/gjs-util.h b/libgjs-private/gjs-util.h index a1c907c1..945dc8ad 100644 --- a/libgjs-private/gjs-util.h +++ b/libgjs-private/gjs-util.h @@ -67,6 +67,12 @@ GType gjs_param_spec_get_value_type (GParamSpec *pspec); GJS_EXPORT GType gjs_param_spec_get_owner_type (GParamSpec *pspec); +/* For imports.overrides.Gtk */ +GJS_EXPORT +void gjs_gtk_container_child_set_property(GObject* container, GObject* child, + const char* property, + const GValue* value); + /* For tests */ GJS_EXPORT int gjs_open_bytes(GBytes* bytes, GError** error); -- cgit v1.2.1