From b4d0a86b77fb5146f5a4f6c76690c16204210f1a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 21 Aug 2023 17:34:08 +0300 Subject: [PATCH] Refactor Dialog to separate the autodeletion functionality The base Dialog class now only hides itself, allowing reusable dialogs. There's a new AutoDialog class which can be used to create autodeleting dialogs, ensuring that they're allocated in the correct way. --- source/dialog.cpp | 28 +--------------------------- source/dialog.h | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/source/dialog.cpp b/source/dialog.cpp index 9f9fc20..5e5e390 100644 --- a/source/dialog.cpp +++ b/source/dialog.cpp @@ -24,37 +24,11 @@ void Dialog::set_modal(bool m) signal_ungrab_pointer.emit(); } -void Dialog::button_release(int x, int y, unsigned button) -{ - Panel::button_release(x, y, button); - check_stale(); -} - -bool Dialog::key_release(unsigned key, unsigned mod) -{ - bool result = Panel::key_release(key, mod); - check_stale(); - return result; -} - -bool Dialog::navigate(Navigation nav) -{ - bool result = Panel::navigate(nav); - check_stale(); - return result; -} - void Dialog::response(int code) { on_response(code); signal_response.emit(code); - stale = true; -} - -void Dialog::check_stale() -{ - if(stale) - delete this; + set_visible(false); } diff --git a/source/dialog.h b/source/dialog.h index 6f35565..cc5ed41 100644 --- a/source/dialog.h +++ b/source/dialog.h @@ -11,7 +11,7 @@ class Button; /** A Dialog is used for temporary interaction with the user. When any of the -Dialog's action buttons are clicked, it will emit a signal and delete itself. +Dialog's action buttons are clicked, it will emit a signal and hide itself. */ class MSPGLTK_API Dialog: public Panel { @@ -27,10 +27,6 @@ public: sigc::signal signal_response; -private: - bool stale = false; - -public: const char *get_class() const override { return "dialog"; } /** Adds an action button to the dialog. Pressing the button will invoke @@ -41,17 +37,47 @@ public: away from the dialog. */ void set_modal(bool); - void button_release(int, int, unsigned) override; - bool key_release(unsigned, unsigned) override; - bool navigate(Navigation) override; protected: void response(int); - void check_stale(); /** Called when an action button is pressed. */ virtual void on_response(int) { } }; + +/** +A dialog which automatically deletes itself after being responded to. +*/ +template +class MSPGLTK_API AutoDialog: public D +{ +private: + struct StaleChecker + { + AutoDialog *dialog; + + StaleChecker(AutoDialog &d): dialog(&d) { } + ~StaleChecker() { if(dialog->stale) delete dialog; } + }; + + bool stale = false; + + template + AutoDialog(Args &&... args): D(std::forward(args)...) { } + +public: + // Ensure that AutoDialog is always created with new. + template + static AutoDialog *create(Args &&... args) { return new AutoDialog(std::forward(args)...); } + + void button_release(int x, int y, unsigned b) override { StaleChecker sc(*this); D::button_release(x, y, b); } + bool key_release(unsigned k, unsigned m) override { StaleChecker sc(*this); return D::key_release(k, m); } + bool navigate(Navigation n) override { StaleChecker sc(*this); return D::navigate(n); } + +protected: + void on_response(int c) override { D::on_response(c); stale = true; } +}; + } // namespace GLtk } // namespace Msp -- 2.43.0