From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- src/ui/builder-utils.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/ui/builder-utils.h (limited to 'src/ui/builder-utils.h') diff --git a/src/ui/builder-utils.h b/src/ui/builder-utils.h new file mode 100644 index 0000000..83fd059 --- /dev/null +++ b/src/ui/builder-utils.h @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * @brief Gtk builder utilities + */ +/* Authors: + * Michael Kowalski + * + * Copyright (C) 2021 Michael Kowalski + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_BUILDER_UTILS_H +#define SEEN_BUILDER_UTILS_H + +#include +#include + +namespace Inkscape { +namespace UI { + +// get widget from builder or throw +template W& get_widget(const Glib::RefPtr& builder, const char* id) { + W* widget; + builder->get_widget(id, widget); + if (!widget) { + throw std::runtime_error("Missing widget in a glade resource file"); + } + return *widget; +} + +template +W& get_derived_widget(const Glib::RefPtr& builder, const char* id, Args&&... args) { + W* widget; + builder->get_widget_derived(id, widget, std::forward(args)...); + if (!widget) { + throw std::runtime_error("Missing widget in a glade resource file"); + } + return *widget; +} + +template Glib::RefPtr get_object(Glib::RefPtr& builder, const char* id) { + auto object = Glib::RefPtr::cast_dynamic(builder->get_object(id)); + if (!object) { + throw std::runtime_error("Missing object in a glade resource file"); + } + return object; +} + +/** + * This version of get_object is needed for Gtk::CellRenderer objects which can not be + * put into Glib::RefPtr by the compiler, but are somehow passed to us as RefPtrs anyway. + */ +template +Ob &get_object_raw(Glib::RefPtr &builder, const char *id) +{ + auto object = dynamic_cast(builder->get_object(id).get()); + if (!object) { + throw std::runtime_error("Missing object in a glade resource file"); + } + return *object; +} + +// load glade file from share/ui folder and return builder; throws on errors +Glib::RefPtr create_builder(const char* filename); + +} } // namespace + +#endif // SEEN_BUILDER_UTILS_H -- cgit v1.2.3