diff options
author | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2023-01-16 14:28:13 +0100 |
---|---|---|
committer | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2023-01-16 14:28:13 +0100 |
commit | 6a7f6d36715ec91114b27b7568ef482837e8c651 (patch) | |
tree | e8e1a73822f550035c0800b0d97b55a56923aa0d /gio/src/settings.ccg | |
parent | 368335a2970865e79c16eb0f71b43e88422dce15 (diff) | |
download | glibmm-6a7f6d36715ec91114b27b7568ef482837e8c651.tar.gz |
Gio::Settings: Add bind() overloads and unbind()
Add the bind() overloads with mapping functions.
Diffstat (limited to 'gio/src/settings.ccg')
-rw-r--r-- | gio/src/settings.ccg | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/gio/src/settings.ccg b/gio/src/settings.ccg index 05ff5e21..7343bb64 100644 --- a/gio/src/settings.ccg +++ b/gio/src/settings.ccg @@ -19,9 +19,94 @@ #include <glibmm/vectorutils.h> #include <giomm/settingsschema.h> +namespace +{ +struct SettingsMapSlots +{ + SettingsMapSlots(const Gio::Settings::SlotGetMapping& get_mapping, + const Gio::Settings::SlotSetMapping& set_mapping) + : from_setting_to_property(get_mapping), from_property_to_setting(set_mapping) + { + } + + Gio::Settings::SlotGetMapping from_setting_to_property; + Gio::Settings::SlotSetMapping from_property_to_setting; +}; + +gboolean +Settings_get_mapping_callback( + GValue* to_value, GVariant* from_variant, gpointer user_data) +{ + Gio::Settings::SlotGetMapping& the_slot = + static_cast<SettingsMapSlots*>(user_data)->from_setting_to_property; + + bool result = false; + try + { + result = the_slot(to_value, from_variant); + } + catch (...) + { + Glib::exception_handlers_invoke(); + } + return result; +} + +GVariant* +Settings_set_mapping_callback( + const GValue* from_value, const GVariantType* expected_type, gpointer user_data) +{ + Gio::Settings::SlotSetMapping& the_slot = + static_cast<SettingsMapSlots*>(user_data)->from_property_to_setting; + + GVariant* result = nullptr; + try + { + result = the_slot(from_value, expected_type); + } + catch (...) + { + Glib::exception_handlers_invoke(); + } + return result; +} + +void +Settings_map_callback_destroy(gpointer user_data) +{ + delete static_cast<SettingsMapSlots*>(user_data); +} + +} // anonymous namespace + namespace Gio { +void Settings::bind_value(const Glib::ustring& key, + Glib::ObjectBase* object, const Glib::ustring& property, BindFlags flags, + const SlotGetMapping& get_mapping, const SlotSetMapping& set_mapping) +{ + if (get_mapping.empty() && set_mapping.empty()) + { + // No user-supplied mappings. + g_settings_bind(gobj(), key.c_str(), object->gobj(), + property.c_str(), (GSettingsBindFlags)flags); + } + else + { + // Create copies of the slots. A pointer to this will be passed + // through the callback's data parameter. It will be deleted + // when Settings_map_callback_destroy() is called. + SettingsMapSlots* slots_copy = new SettingsMapSlots(get_mapping, set_mapping); + + g_settings_bind_with_mapping(gobj(), key.c_str(), + object->gobj(), property.c_str(), (GSettingsBindFlags)flags, + get_mapping.empty() ? nullptr : &Settings_get_mapping_callback, + set_mapping.empty() ? nullptr : &Settings_set_mapping_callback, slots_copy, + &Settings_map_callback_destroy); + } +} + void Settings::get_value(const Glib::ustring& key, Glib::VariantBase& value) const { @@ -66,4 +151,10 @@ Settings::bind_writable( bind_writable(key, property_proxy.get_object(), property_proxy.get_name(), inverted); } +//static +void Settings::unbind(const Glib::PropertyProxy_Base& property_proxy) +{ + unbind(property_proxy.get_object(), property_proxy.get_name()); +} + } |