From 9565056e6e10c7db97ff842ef1db50a340d7b10e Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 28 Sep 2024 22:39:02 +0200 Subject: [PATCH] [example] Add `ImageTextButton` in widgets_example Fix `ImageTextButton` --- examples/widgets_example.hpp | 12 ++++++++++- include/guisan/widgets/imagetextbutton.hpp | 24 +--------------------- src/widgets/imagetextbutton.cpp | 19 ----------------- 3 files changed, 12 insertions(+), 43 deletions(-) diff --git a/examples/widgets_example.hpp b/examples/widgets_example.hpp index 5bfb458..ab91954 100644 --- a/examples/widgets_example.hpp +++ b/examples/widgets_example.hpp @@ -63,6 +63,13 @@ namespace WidgetsExample imageButton->setSize(220, 90); imageButton->addActionListener(this); + imageTextButton = std::make_unique("guisan-logo.png", "Change color"); + imageTextButton->setSize(220, 100); + imageTextButton->setAlignment(gcn::ImageTextButton::TOP); + imageTextButton->addActionListener(this); + + imageButton->setSize(220, 90); + imageButton->addActionListener(this); inputBox = std::make_unique("Change name", " Set name of label "); inputBox->setVisible(false); @@ -141,6 +148,7 @@ namespace WidgetsExample top->add(icon.get(), 10, 10); top->add(button.get(), 325, 10); top->add(imageButton.get(), 10, 290); + top->add(imageTextButton.get(), 10, 380); top->add(textField.get(), 375, 10); top->add(textBoxScrollArea.get(), 290, 50); top->add(inputBox.get(), 270, 180); @@ -170,7 +178,8 @@ namespace WidgetsExample inputBox->setVisible(true); top->moveToTop(inputBox.get()); } - else if (actionEvent.getSource() == imageButton.get()) + else if (actionEvent.getSource() == imageButton.get() + || actionEvent.getSource() == imageTextButton.get()) { const gcn::Color colours[] = {gcn::Color(212, 255, 150, 190), gcn::Color(212, 0, 0, 190), @@ -216,6 +225,7 @@ namespace WidgetsExample std::unique_ptr icon; // An icon (image) std::unique_ptr button; // A button std::unique_ptr imageButton; // An image button + std::unique_ptr imageTextButton; // An image text button std::unique_ptr inputBox; // An input box std::unique_ptr textField; // One-line text field std::unique_ptr textBox; // Multi-line text box diff --git a/include/guisan/widgets/imagetextbutton.hpp b/include/guisan/widgets/imagetextbutton.hpp index 67481eb..96e9933 100644 --- a/include/guisan/widgets/imagetextbutton.hpp +++ b/include/guisan/widgets/imagetextbutton.hpp @@ -91,27 +91,13 @@ namespace gcn /** * Destructor. */ - ~ImageTextButton() override; + ~ImageTextButton() override = default; /** * Adjusts the size of the image button to fit the image. */ void adjustSize(); - /** - * Sets the image to display. - * - * @param image The image to display. - */ - void setImage(Image* image); - - /** - * Gets the image of the image button. - * - * @return The image of the image button. - */ - Image* getImage(); - // Inherited from Widget void draw(gcn::Graphics* graphics) override; @@ -141,14 +127,6 @@ namespace gcn }; protected: - gcn::Image* mImage = nullptr; - - /** - * True if the image has been loaded internally, false otherwise. - * An image not loaded internally should not be deleted in the - * destructor. - */ - bool mInternalImage = false; unsigned int mAlignment = ImageTextButton::BOTTOM; }; } diff --git a/src/widgets/imagetextbutton.cpp b/src/widgets/imagetextbutton.cpp index dffce97..0fabaed 100644 --- a/src/widgets/imagetextbutton.cpp +++ b/src/widgets/imagetextbutton.cpp @@ -81,12 +81,6 @@ namespace gcn setHeight(mImage->getHeight() + mImage->getHeight() / 2); } - ImageTextButton::~ImageTextButton() - { - if (mInternalImage) - delete mImage; - } - void ImageTextButton::adjustSize() { switch(getAlignment()) @@ -113,19 +107,6 @@ namespace gcn } } - void ImageTextButton::setImage(Image* image) - { - if (mInternalImage) - delete mImage; - mImage = image; - mInternalImage = false; - } - - Image* ImageTextButton::getImage() - { - return mImage; - } - void ImageTextButton::draw(Graphics* graphics) { gcn::Color faceColor = getBaseColor();