From b77078715aa5af3401cccf2b270f5042b4f66f1e Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 28 Sep 2024 23:03:14 +0200 Subject: [PATCH 1/9] [cleanup] Use `std::shared_ptr` for `Icon` --- demo/ff/include/ffdemo.hpp | 3 --- demo/ff/src/ffdemo.cpp | 10 +++---- examples/widgets_example.hpp | 13 +++++---- include/guisan/widgets/icon.hpp | 36 +++++++++++++++---------- src/widgets/icon.cpp | 47 +++++++++++++++++++-------------- 5 files changed, 58 insertions(+), 51 deletions(-) diff --git a/demo/ff/include/ffdemo.hpp b/demo/ff/include/ffdemo.hpp index a9fa572..52ed358 100644 --- a/demo/ff/include/ffdemo.hpp +++ b/demo/ff/include/ffdemo.hpp @@ -103,9 +103,6 @@ class FFDemo : public gcn::ActionListener, public gcn::KeyListener std::unique_ptr mPerIcon; std::unique_ptr mOlofIcon; std::unique_ptr mTomasIcon; - std::unique_ptr mPerImage; - std::unique_ptr mOlofImage; - std::unique_ptr mTomasImage; std::unique_ptr mSplashImage; std::unique_ptr mFontWhite; std::unique_ptr mFontCyan; diff --git a/demo/ff/src/ffdemo.cpp b/demo/ff/src/ffdemo.cpp index 2552ce9..de17533 100644 --- a/demo/ff/src/ffdemo.cpp +++ b/demo/ff/src/ffdemo.cpp @@ -166,13 +166,9 @@ void FFDemo::initMain() mMain->setDimension(gcn::Rectangle(0, 0, 320, 240)); mTop->add(mMain.get()); - mPerImage.reset(gcn::Image::load("images/finalman.png")); - mOlofImage.reset(gcn::Image::load("images/yakslem.png")); - mTomasImage.reset(gcn::Image::load("images/peak.png")); - - mPerIcon = std::make_unique(mPerImage.get()); - mOlofIcon = std::make_unique(mOlofImage.get()); - mTomasIcon = std::make_unique(mTomasImage.get()); + mPerIcon = std::make_unique("images/finalman.png"); + mOlofIcon = std::make_unique("images/yakslem.png"); + mTomasIcon = std::make_unique("images/peak.png"); mPerInfo1 = std::make_unique("\n LV\n HP\n MP"); mPerInfo1->setFont(mFontCyan.get()); diff --git a/examples/widgets_example.hpp b/examples/widgets_example.hpp index 79728f7..8746b62 100644 --- a/examples/widgets_example.hpp +++ b/examples/widgets_example.hpp @@ -41,18 +41,19 @@ namespace WidgetsExample // The global font is static and must be set. gcn::Widget::setGlobalFont(font.get()); - top = std::make_unique < gcn::Container>(); + top = std::make_unique(); // Set the dimension of the top container to match the screen. top->setDimension(gcn::Rectangle(0, 0, width, height)); // Set the top container gui.setTop(top.get()); + + guisanLogoImage.reset(gcn::Image::load("guisan-logo.png")); /* * Create all the widgets */ label = std::make_unique("Label"); - image.reset(gcn::Image::load("guisan.png")); - icon = std::make_unique(image.get()); + icon = std::make_unique("guisan.png"); button = std::make_unique("Button"); @@ -125,8 +126,7 @@ namespace WidgetsExample messageBox->setFrameSize(1); messageBox->addActionListener(this); - guisanLogoImage.reset(gcn::Image::load("guisan-logo.png")); - guisanLogoIcon = std::make_unique(guisanLogoImage.get()); + guisanLogoIcon = std::make_unique(guisanLogoImage); window->add(guisanLogoIcon.get()); window->resizeToContent(); @@ -239,11 +239,10 @@ namespace WidgetsExample std::unique_ptr radioButton3; std::unique_ptr toggleButton; std::unique_ptr slider; // A slider - std::unique_ptr image; // An image for the icon std::unique_ptr window; std::unique_ptr messageBox; std::unique_ptr progress; - std::unique_ptr guisanLogoImage; + std::shared_ptr guisanLogoImage; std::unique_ptr guisanLogoIcon; std::unique_ptr nestedScrollArea; std::unique_ptr nestedContainer; diff --git a/include/guisan/widgets/icon.hpp b/include/guisan/widgets/icon.hpp index ee12acb..d5f2889 100644 --- a/include/guisan/widgets/icon.hpp +++ b/include/guisan/widgets/icon.hpp @@ -61,6 +61,8 @@ #include "guisan/platform.hpp" #include "guisan/widget.hpp" +#include + namespace gcn { /** @@ -79,28 +81,41 @@ namespace gcn * * @param filename The filename of the image to display. */ - Icon(const std::string& filename); + explicit Icon(const std::string& filename); + + /** + * Constructor. + * + * @param image The image to display. + */ + explicit Icon(const Image* image); /** * Constructor. * * @param image The image to display. */ - Icon(const Image* image); + explicit Icon(std::shared_ptr image); /** * Destructor. */ - ~Icon() override; + ~Icon() override = default; /** - * Sets the image to display. Existing image is freed automatically - * if it was loaded internally. + * Sets the image to display. * - * @param image The image to display. + * @param image The image to display. */ void setImage(const Image* image); + /** + * Sets the image to display. + * + * @param image The image to display. + */ + void setImage(std::shared_ptr image); + /** * Gets the current image. * @@ -116,14 +131,7 @@ namespace gcn /** * The image to display. */ - const 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; + std::shared_ptr mImage; }; } diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 7ad2338..58d7c2a 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -71,39 +71,46 @@ namespace gcn setSize(0, 0); } - Icon::Icon(const std::string& filename) : mImage(Image::load(filename)), mInternalImage(true) - { - setSize(mImage->getWidth(), mImage->getHeight()); - } + Icon::Icon(const std::string& filename) : + Icon({Image::load(filename), std::default_delete{}}) + {} - Icon::Icon(const Image* image) : mImage(image) - { - setSize(mImage->getWidth(), mImage->getHeight()); - } + Icon::Icon(const Image* image) : Icon({image, [](const Image*) {}}) + {} - Icon::~Icon() + Icon::Icon(std::shared_ptr image) : mImage(std::move(image)) { - if (mInternalImage) + if (mImage) + { + setSize(mImage->getWidth(), mImage->getHeight()); + } + else { - delete mImage; + setSize(0, 0); } } void Icon::setImage(const Image* image) { - if (mInternalImage) + setImage({image, [](const Image*) {}}); + } + + void Icon::setImage(std::shared_ptr image) + { + mImage = std::move(image); + if (mImage) { - delete mImage; + setSize(mImage->getWidth(), mImage->getHeight()); + } + else + { + setSize(0, 0); } - - mImage = image; - mInternalImage = false; - setSize(mImage->getWidth(), mImage->getHeight()); } const Image* Icon::getImage() const { - return mImage; + return mImage.get(); } void Icon::draw(Graphics* graphics) @@ -112,8 +119,8 @@ namespace gcn { const int x = (getWidth() - mImage->getWidth()) / 2; const int y = (getHeight() - mImage->getHeight()) / 2; - graphics->drawImage(mImage, x, y); + graphics->drawImage(mImage.get(), x, y); } } -} +} // namespace gcn From 69ed9260cc8b84f14b484a8926395473063d1f46 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 28 Sep 2024 23:32:01 +0200 Subject: [PATCH 2/9] [cleanup] Use `std::shared_ptr` for `ImageButton` --- include/guisan/widgets/imagebutton.hpp | 34 +++++++++------ include/guisan/widgets/imagetextbutton.hpp | 10 ++++- src/widgets/imagebutton.cpp | 49 ++++++++++++---------- src/widgets/imagetextbutton.cpp | 15 +++++-- 4 files changed, 67 insertions(+), 41 deletions(-) diff --git a/include/guisan/widgets/imagebutton.hpp b/include/guisan/widgets/imagebutton.hpp index c476ea3..03f5ba4 100644 --- a/include/guisan/widgets/imagebutton.hpp +++ b/include/guisan/widgets/imagebutton.hpp @@ -60,6 +60,8 @@ #include "guisan/platform.hpp" #include "guisan/widgets/button.hpp" +#include + namespace gcn { class Image; @@ -80,19 +82,26 @@ namespace gcn * * @param filename The filename of the image to display. */ - ImageButton(const std::string& filename); + explicit ImageButton(const std::string& filename); + + /** + * Constructor. + * + * @param image The image to display. + */ + explicit ImageButton(const Image* image); /** * Constructor. * * @param image The image to display. */ - ImageButton(const Image* image); + explicit ImageButton(std::shared_ptr image); /** * Destructor. */ - ~ImageButton() override; + ~ImageButton() override = default; /** * Adjusts the size of the image button to fit the image. @@ -100,13 +109,19 @@ namespace gcn void adjustSize(); /** - * Sets the image to display. Existing Image is freed automatically, - * if it was loaded internally. + * Sets the image to display. * * @param image The image to display. */ void setImage(const Image* image); + /** + * Sets the image to display. + * + * @param image The image to display. + */ + void setImage(std::shared_ptr image); + /** * Gets current image. * @@ -119,14 +134,7 @@ namespace gcn void draw(gcn::Graphics* graphics) override; protected: - const 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; + std::shared_ptr mImage; }; } #endif diff --git a/include/guisan/widgets/imagetextbutton.hpp b/include/guisan/widgets/imagetextbutton.hpp index 96e9933..8d26c10 100644 --- a/include/guisan/widgets/imagetextbutton.hpp +++ b/include/guisan/widgets/imagetextbutton.hpp @@ -86,7 +86,15 @@ namespace gcn * @param image The image to display. * @param caption The text to display. */ - ImageTextButton(Image* image, const std::string& caption); + ImageTextButton(const Image* image, const std::string& caption); + + /** + * Constructor. + * + * @param image The image to display. + * @param caption The text to display. + */ + ImageTextButton(std::shared_ptr image, const std::string& caption); /** * Destructor. diff --git a/src/widgets/imagebutton.cpp b/src/widgets/imagebutton.cpp index f88f0ac..e768a53 100644 --- a/src/widgets/imagebutton.cpp +++ b/src/widgets/imagebutton.cpp @@ -72,23 +72,23 @@ namespace gcn } ImageButton::ImageButton(const std::string& filename) : - mImage(Image::load(filename)), - mInternalImage(true) - { - setWidth(mImage->getWidth() + mImage->getWidth() / 2); - setHeight(mImage->getHeight() + mImage->getHeight() / 2); - } + ImageButton({Image::load(filename), std::default_delete{}}) + {} - ImageButton::ImageButton(const Image* image) : mImage(image) - { - setWidth(mImage->getWidth() + mImage->getWidth() / 2); - setHeight(mImage->getHeight() + mImage->getHeight() / 2); - } + ImageButton::ImageButton(const Image* image) : ImageButton({image, [](const auto*) {}}) + {} - ImageButton::~ImageButton() + ImageButton::ImageButton(std::shared_ptr image) : mImage(std::move(image)) { - if (mInternalImage) - delete mImage; + if (mImage) + { + setWidth(mImage->getWidth() + mImage->getWidth() / 2); + setHeight(mImage->getHeight() + mImage->getHeight() / 2); + } + else + { + setSize(0, 0); + } } void ImageButton::adjustSize() @@ -99,18 +99,17 @@ namespace gcn void ImageButton::setImage(const Image* image) { - if (mInternalImage) - { - delete mImage; - } + setImage({image, [](const auto*) {}}); + } - mImage = image; - mInternalImage = false; + void ImageButton::setImage(std::shared_ptr image) + { + mImage = std::move(image); } const Image* ImageButton::getImage() const { - return mImage; + return mImage.get(); } void ImageButton::draw(Graphics* graphics) @@ -158,12 +157,16 @@ namespace gcn if (isPressed()) { if (mImage) - graphics->drawImage(mImage, textX + 1, textY + 1); + { + graphics->drawImage(mImage.get(), textX + 1, textY + 1); + } } else { if (mImage) - graphics->drawImage(mImage, textX, textY); + { + graphics->drawImage(mImage.get(), textX, textY); + } if (isFocused()) { diff --git a/src/widgets/imagetextbutton.cpp b/src/widgets/imagetextbutton.cpp index 0fabaed..03aac96 100644 --- a/src/widgets/imagetextbutton.cpp +++ b/src/widgets/imagetextbutton.cpp @@ -67,14 +67,21 @@ namespace gcn { - ImageTextButton::ImageTextButton(const std::string& filename, const std::string& caption) : ImageButton(filename) + ImageTextButton::ImageTextButton(const std::string& filename, const std::string& caption) : + ImageButton(filename) { setCaption(caption); setWidth(mImage->getWidth() + mImage->getWidth() / 2); setHeight(mImage->getHeight() + mImage->getHeight() / 2); } - ImageTextButton::ImageTextButton(Image* image, const std::string& caption) : ImageButton(image) + ImageTextButton::ImageTextButton(const Image* image, const std::string& caption) : + ImageTextButton({image, [](const auto*) {}}, caption) + {} + + ImageTextButton::ImageTextButton(std::shared_ptr image, + const std::string& caption) : + ImageButton(std::move(image)) { setCaption(caption); setWidth(mImage->getWidth() + mImage->getWidth() / 2); @@ -181,12 +188,12 @@ namespace gcn if (isPressed()) { - graphics->drawImage(mImage, imageX + 1, imageY + 1); + graphics->drawImage(mImage.get(), imageX + 1, imageY + 1); graphics->drawText(mCaption, textX + 1, textY + 1, Graphics::Left, isEnabled()); } else { - graphics->drawImage(mImage, imageX, imageY); + graphics->drawImage(mImage.get(), imageX, imageY); graphics->drawText(mCaption, textX, textY, Graphics::Left, isEnabled()); if (isFocused()) From f83f207500fb152272319fa0c85f5d9b918e8ffe Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sun, 29 Sep 2024 23:55:25 +0200 Subject: [PATCH 3/9] [cleanup] Use `std::shared_ptr` for `ImageFont` --- include/guisan/imagefont.hpp | 34 ++++--- src/imagefont.cpp | 166 +++++++++++------------------------ 2 files changed, 73 insertions(+), 127 deletions(-) diff --git a/include/guisan/imagefont.hpp b/include/guisan/imagefont.hpp index c25e253..b501d43 100644 --- a/include/guisan/imagefont.hpp +++ b/include/guisan/imagefont.hpp @@ -63,6 +63,8 @@ #include "guisan/platform.hpp" #include "guisan/rectangle.hpp" +#include + namespace gcn { class Color; @@ -119,7 +121,19 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode * @throws Exception when glyph list is incorrect or the font image is * is missing. */ - ImageFont(Image* image, const std::string& glyphs); + [[deprecated]] ImageFont(Image* image, const std::string& glyphs); + + /** + * Constructor. Takes an image containing the font and + * a string containing the glyphs. The glyphs in the string should + * be in the same order as they appear in the font image. + * + * @param image The image with font glyphs. + * @param glyphs The glyphs found in the image. + * @throws Exception when glyph list is incorrect or the font image is + * is missing. + */ + ImageFont(std::shared_ptr image, const std::string& glyphs); /** * Constructor. Takes an image file containing the font and @@ -142,7 +156,7 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode /** * Destructor. */ - ~ImageFont() override; + ~ImageFont() override = default; /** * Draws a glyph. @@ -228,6 +242,12 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode */ Rectangle scanForGlyph(unsigned char glyph, int x, int y, const Color& separator) const; + protected: + /** + * Holds the image with the font data. + */ + std::shared_ptr mImage; + /** * Holds the glyphs areas in the image. */ @@ -247,16 +267,6 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode * Holds the row spacing of the image font. */ int mRowSpacing = 0; - - /** - * Holds the image with the font data. - */ - Image* mImage = nullptr; - - /** - * Holds the filename of the image with the font data. - */ - std::string mFilename; }; } diff --git a/src/imagefont.cpp b/src/imagefont.cpp index 3aff936..6080dac 100644 --- a/src/imagefont.cpp +++ b/src/imagefont.cpp @@ -69,89 +69,60 @@ namespace gcn { - ImageFont::ImageFont(const std::string& filename, const std::string& glyphs) - { - mFilename = filename; - mImage = Image::load(filename, false); - - Color separator = mImage->getPixel(0, 0); - int i = 0; - for (i = 0; - i < mImage->getWidth() && separator == mImage->getPixel(i, 0); - ++i) - { - } - - if (i >= mImage->getWidth()) + namespace + { + int computeGlyphHeight(Image* imagePtr) { - throw GCN_EXCEPTION("Corrupt image."); - } + if (imagePtr == nullptr) + { + throw GCN_EXCEPTION("Font image is nullptr"); + } + Image& image = *imagePtr; + const Color separator = image.getPixel(0, 0); - int j; - for (j = 0; j < mImage->getHeight(); ++j) - { - if (separator == mImage->getPixel(i, j)) + for (int i = 1; i != image.getWidth(); ++i) { - break; + if (separator == image.getPixel(i, 0)) + { + continue; + } + for (int j = 1; j != image.getHeight(); ++j) + { + if (separator == image.getPixel(i, j)) + { + return j; + } + } + return image.getHeight(); } + throw GCN_EXCEPTION("Corrupt image."); } + } // namespace - mHeight = j; - int x = 0, y = 0; - unsigned char k; + ImageFont::ImageFont(const std::string& filename, const std::string& glyphs) : + ImageFont( + std::shared_ptr{Image::load(filename, false), std::default_delete{}}, + glyphs) + {} - for (i = 0; i < (int) glyphs.size(); ++i) - { - k = glyphs.at(i); - mGlyph[k] = scanForGlyph(k, x, y, separator); - // Update x och y with new coordinates. - x = mGlyph[k].x + mGlyph[k].width; - y = mGlyph[k].y; - } - - mImage->convertToDisplayFormat(); - } + ImageFont::ImageFont(Image* image, const std::string& glyphs) : + ImageFont(std::shared_ptr{image, std::default_delete{}}, glyphs) + {} - ImageFont::ImageFont(Image* image, const std::string& glyphs) + ImageFont::ImageFont(std::shared_ptr image, const std::string& glyphs) : + mImage(std::move(image)), + mHeight(computeGlyphHeight(mImage.get())) { - mFilename = "Image*"; - if (image == nullptr) - { - throw GCN_EXCEPTION("Font image is nullptr"); - } - mImage = image; - const Color separator = mImage->getPixel(0, 0); - - int i = 0; - for (i = 0; i < mImage->getWidth() && separator == mImage->getPixel(i, 0); ++i) - {} - - if (i >= mImage->getWidth()) - { - throw GCN_EXCEPTION("Corrupt image."); - } - - int j = 0; - for (j = 0; j < mImage->getHeight(); ++j) - { - if (separator == mImage->getPixel(i, j)) - { - break; - } - } - - mHeight = j; - int x = 0, y = 0; - - for (i = 0; i < (int) glyphs.size(); ++i) + int x = 0; + int y = 0; + for (unsigned char c : glyphs) { - const unsigned char k = glyphs.at(i); - mGlyph[k] = scanForGlyph(k, x, y, separator); + mGlyph[c] = scanForGlyph(c, x, y, separator); // Update x and y with new coordinates. - x = mGlyph[k].x + mGlyph[k].width; - y = mGlyph[k].y; + x = mGlyph[c].x + mGlyph[c].width; + y = mGlyph[c].y; } mImage->convertToDisplayFormat(); @@ -159,53 +130,22 @@ namespace gcn ImageFont::ImageFont(const std::string& filename, unsigned char glyphsFrom, - unsigned char glyphsTo) + unsigned char glyphsTo) : + mImage{Image::load(filename, false), std::default_delete{}}, + mHeight(computeGlyphHeight(mImage.get())) { - mFilename = filename; - mImage = Image::load(filename, false); - const Color separator = mImage->getPixel(0, 0); - int i; - for (i=0; separator == mImage->getPixel(i, 0) - && i < mImage->getWidth(); ++i) - { - } - - if (i >= mImage->getWidth()) - { - throw GCN_EXCEPTION("Corrupt image."); - } - - int j; - for (j = 0; j < mImage->getHeight(); ++j) - { - if (separator == mImage->getPixel(i, j)) - { - break; - } - } - - mHeight = j; - int x = 0, y = 0; - - for (i = glyphsFrom; i < glyphsTo + 1; i++) + int x = 0; + int y = 0; + for (int i = glyphsFrom; i < glyphsTo + 1; i++) { mGlyph[i] = scanForGlyph(i, x, y, separator); - // Update x och y with new coordinates. + // Update x and y with new coordinates. x = mGlyph[i].x + mGlyph[i].width; y = mGlyph[i].y; } - mImage->convertToDisplayFormat(); - - mRowSpacing = 0; - mGlyphSpacing = 0; - } - - ImageFont::~ImageFont() - { - delete mImage; } int ImageFont::getWidth(unsigned char glyph) const @@ -236,7 +176,7 @@ namespace gcn return mGlyph[(int)(' ')].width + mGlyphSpacing; } - graphics->drawImage(mImage, + graphics->drawImage(mImage.get(), mGlyph[glyph].x, mGlyph[glyph].y, x, @@ -294,9 +234,7 @@ namespace gcn { std::string str; std::ostringstream os(str); - os << "Image "; - os << mFilename; - os << " with font is corrupt near character '"; + os << "Image with font is corrupt near character '"; os << glyph; os << "'"; throw GCN_EXCEPTION(os.str()); @@ -317,9 +255,7 @@ namespace gcn { std::string str; std::ostringstream os(str); - os << "Image "; - os << mFilename; - os << " with font is corrupt near character '"; + os << "Image with font is corrupt near character '"; os << glyph; os << "'"; throw GCN_EXCEPTION(os.str()); From ef7b702c422bf6d8e57c9bf7333f1228cd02fed1 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Tue, 15 Oct 2024 11:18:22 +0200 Subject: [PATCH 4/9] [cleanup] Use `std::shared_ptr` for `DropDown` --- include/guisan/widgets/dropdown.hpp | 24 ++++++++++++++++++----- src/widgets/dropdown.cpp | 30 +++++++++++++++-------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/include/guisan/widgets/dropdown.hpp b/include/guisan/widgets/dropdown.hpp index add22e3..8b1f5ee 100644 --- a/include/guisan/widgets/dropdown.hpp +++ b/include/guisan/widgets/dropdown.hpp @@ -66,6 +66,8 @@ #include "guisan/selectionlistener.hpp" #include "guisan/widget.hpp" +#include + namespace gcn { class ListBox; @@ -105,9 +107,21 @@ namespace gcn * @param listBox the listBox to use. * @see ListModel, ScrollArea, ListBox. */ - DropDown(ListModel *listModel = nullptr, - ScrollArea *scrollArea = nullptr, - ListBox *listBox = nullptr); + DropDown(ListModel* listModel, + ScrollArea* scrollArea, + ListBox* listBox = nullptr); + + /** + * Constructor. + * + * @param listModel the ListModel to use. + * @param scrollArea the ScrollArea to use. + * @param listBox the listBox to use. + * @see ListModel, ScrollArea, ListBox. + */ + DropDown(ListModel* listModel = nullptr, + std::shared_ptr scrollArea = nullptr, + std::shared_ptr listBox = nullptr); /** * Destructor. @@ -269,8 +283,8 @@ namespace gcn /** * The scroll area used. */ - ScrollArea* mScrollArea = nullptr; - ListBox* mListBox = nullptr; + std::shared_ptr mScrollArea; + std::shared_ptr mListBox; /** * The internal focus handler used to keep track of focus for the diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index 8d84fd3..8c3f0a5 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -67,10 +67,17 @@ namespace gcn { - DropDown::DropDown(ListModel *listModel, - ScrollArea *scrollArea, ListBox* listBox) : - mScrollArea(scrollArea ? scrollArea : new ScrollArea()), - mListBox(listBox ? listBox : new ListBox()), + DropDown::DropDown(ListModel* listModel, ScrollArea* scrollArea, ListBox* listBox) : + DropDown(listModel, + std::shared_ptr(scrollArea, [](ScrollArea*) {}), + std::shared_ptr(listBox, [](ListBox*) {})) + {} + + DropDown::DropDown(ListModel* listModel, + std::shared_ptr scrollArea, + std::shared_ptr listBox) : + mScrollArea(scrollArea ? scrollArea : std::make_shared()), + mListBox(listBox ? listBox : std::make_shared()), mInternalScrollArea(scrollArea == nullptr), mInternalListBox(listBox == nullptr) { @@ -79,8 +86,8 @@ namespace gcn setInternalFocusHandler(&mInternalFocusHandler); - mScrollArea->setContent(mListBox); - add(mScrollArea); + mScrollArea->setContent(mListBox.get()); + add(mScrollArea.get()); mListBox->addActionListener(this); mListBox->addSelectionListener(this); @@ -101,15 +108,9 @@ namespace gcn DropDown::~DropDown() { - if (widgetExists(mListBox)) + if (widgetExists(mListBox.get())) mListBox->removeSelectionListener(this); - if (mInternalScrollArea) - delete mScrollArea; - - if (mInternalListBox) - delete mListBox; - setInternalFocusHandler(nullptr); } @@ -464,7 +465,7 @@ namespace gcn void DropDown::death(const Event& event) { - if (event.getSource() == mScrollArea) + if (event.getSource() == mScrollArea.get()) { mScrollArea = nullptr; } @@ -491,6 +492,7 @@ namespace gcn void DropDown::setBaseColor(const Color& color) { + if (mInternalScrollArea) { mScrollArea->setBaseColor(color); From c758bd449c0c784563b8d777c48701a56ef95ec7 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Tue, 15 Oct 2024 11:46:07 +0200 Subject: [PATCH 5/9] [cleanup] Use `std::unique_ptr` for `Tab` --- include/guisan/widgets/tab.hpp | 9 +++++---- src/widgets/tab.cpp | 9 +++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/guisan/widgets/tab.hpp b/include/guisan/widgets/tab.hpp index 67649c7..a76b2ae 100644 --- a/include/guisan/widgets/tab.hpp +++ b/include/guisan/widgets/tab.hpp @@ -57,13 +57,14 @@ #ifndef GCN_TAB_HPP #define GCN_TAB_HPP -#include -#include - #include "guisan/mouselistener.hpp" #include "guisan/platform.hpp" #include "guisan/widget.hpp" +#include +#include +#include + namespace gcn { class Label; @@ -144,7 +145,7 @@ namespace gcn /** * Holds the label of the tab. */ - Label* mLabel = nullptr; + std::unique_ptr