From cca66b9ec4e494c1d919bff0f71a820d8afab1fa Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:24:48 +0200 Subject: Adding upstream version 1.2.2. Signed-off-by: Daniel Baumann --- src/extension/prefdialog/widget-image.cpp | 87 +++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/extension/prefdialog/widget-image.cpp (limited to 'src/extension/prefdialog/widget-image.cpp') diff --git a/src/extension/prefdialog/widget-image.cpp b/src/extension/prefdialog/widget-image.cpp new file mode 100644 index 0000000..4f5e11d --- /dev/null +++ b/src/extension/prefdialog/widget-image.cpp @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Image widget for extensions + *//* + * Authors: + * Patrick Storz + * + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "widget-image.h" + +#include +#include +#include + +#include "xml/node.h" +#include "extension/extension.h" + +namespace Inkscape { +namespace Extension { + + +WidgetImage::WidgetImage(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext) + : InxWidget(xml, ext) +{ + std::string image_path; + + // get path to image + const char *content = nullptr; + if (xml->firstChild()) { + content = xml->firstChild()->content(); + } + if (content) { + image_path = content; + } else { + g_warning("Missing path for image widget in extension '%s'.", _extension->get_id()); + return; + } + + // make sure path is absolute (relative paths are relative to .inx file's location) + if (!Glib::path_is_absolute(image_path)) { + image_path = Glib::build_filename(_extension->get_base_directory(), image_path); + } + + // check if image exists + if (Glib::file_test(image_path, Glib::FILE_TEST_IS_REGULAR)) { + _image_path = image_path; + } else { + g_warning("Image file ('%s') not found for image widget in extension '%s'.", + image_path.c_str(), _extension->get_id()); + } + + // parse width/height attributes + const char *width = xml->attribute("width"); + const char *height = xml->attribute("height"); + if (width && height) { + _width = strtoul(width, nullptr, 0); + _height = strtoul(height, nullptr, 0); + } +} + +/** \brief Create a label for the description */ +Gtk::Widget *WidgetImage::get_widget(sigc::signal * /*changeSignal*/) +{ + if (_hidden || _image_path.empty()) { + return nullptr; + } + + Gtk::Image *image = Gtk::manage(new Gtk::Image(_image_path)); + + // resize if requested + if (_width && _height) { + Glib::RefPtr pixbuf = image->get_pixbuf(); + pixbuf = pixbuf->scale_simple(_width, _height, Gdk::INTERP_BILINEAR); + image->set(pixbuf); + } + + image->show(); + + return dynamic_cast(image); +} + +} /* namespace Extension */ +} /* namespace Inkscape */ -- cgit v1.2.3