diff --git a/include/idialogmanager.h b/include/idialogmanager.h index c0bc58fecc..f5b2616f0b 100644 --- a/include/idialogmanager.h +++ b/include/idialogmanager.h @@ -1,6 +1,6 @@ #pragma once -#include "iuimanager.h" +#include "imodule.h" #include class wxWindow; @@ -92,7 +92,8 @@ typedef std::shared_ptr IDirChooserPtr; class IResourceChooser; // defined in iresourcechooser.h class IAnimationChooser; // defined in ianimationchooser.h -class IDialogManager +class IDialogManager : + public RegisterableModule { public: // Virtual destructor @@ -111,7 +112,7 @@ class IDialogManager * GlobalMainFrame's toplevel window if left at NULL. */ virtual IDialogPtr createDialog(const std::string& title, - wxWindow* parent = NULL) = 0; + wxWindow* parent = nullptr) = 0; /** * Create a simple message box, which can either notify the user about something, @@ -127,7 +128,7 @@ class IDialogManager virtual IDialogPtr createMessageBox(const std::string& title, const std::string& text, IDialog::MessageType type, - wxWindow* parent = NULL) = 0; + wxWindow* parent = nullptr) = 0; /** * Acquire a new filechooser instance with the given parameters. @@ -163,8 +164,11 @@ class IDialogManager } // namespace ui -// Shortcut method + +constexpr const char* const MODULE_DIALOGMANAGER = "DialogManager"; + inline ui::IDialogManager& GlobalDialogManager() { - return GlobalUIManager().getDialogManager(); + static module::InstanceReference _reference(MODULE_DIALOGMANAGER); + return _reference; } diff --git a/include/iuimanager.h b/include/iuimanager.h index b17fef7fef..840c803dc7 100644 --- a/include/iuimanager.h +++ b/include/iuimanager.h @@ -5,13 +5,6 @@ // Forward declarations class IGroupDialog; // see igroupdialog.h for definition -namespace ui -{ - -class IDialogManager; // see idialogmanager.h for definition - -} // namespace ui - const char* const MODULE_UIMANAGER("UIManager"); /** @@ -22,16 +15,15 @@ class IUIManager : { public: virtual IGroupDialog& getGroupDialog() = 0; - virtual ui::IDialogManager& getDialogManager() = 0; }; -// This is the accessor for the UI manager inline IUIManager& GlobalUIManager() { static module::InstanceReference _reference(MODULE_UIMANAGER); return _reference; } -inline IGroupDialog& GlobalGroupDialog() { +inline IGroupDialog& GlobalGroupDialog() +{ return GlobalUIManager().getGroupDialog(); } diff --git a/plugins/script/interfaces/DialogInterface.cpp b/plugins/script/interfaces/DialogInterface.cpp index aca977f436..aa353bfb38 100644 --- a/plugins/script/interfaces/DialogInterface.cpp +++ b/plugins/script/interfaces/DialogInterface.cpp @@ -1,21 +1,21 @@ #include "DialogInterface.h" #include -#include "iuimanager.h" +#include "idialogmanager.h" namespace script { ScriptDialog DialogManagerInterface::createDialog(const std::string& title) { - return ScriptDialog(GlobalUIManager().getDialogManager().createDialog(title)); + return ScriptDialog(GlobalDialogManager().createDialog(title)); } ScriptDialog DialogManagerInterface::createMessageBox(const std::string& title, const std::string& text, ui::IDialog::MessageType type) { - return ScriptDialog(GlobalUIManager().getDialogManager().createMessageBox(title, text, type)); + return ScriptDialog(GlobalDialogManager().createMessageBox(title, text, type)); } // IScriptInterface implementation diff --git a/radiant/uimanager/DialogManager.cpp b/radiant/uimanager/DialogManager.cpp index 4062dfe1a7..f74dc020be 100644 --- a/radiant/uimanager/DialogManager.cpp +++ b/radiant/uimanager/DialogManager.cpp @@ -8,6 +8,7 @@ #include "wxutil/DirChooser.h" #include "SoundChooser.h" #include "ui/animationpreview/MD5AnimationChooser.h" +#include "module/StaticModule.h" namespace ui { @@ -19,16 +20,45 @@ DialogManager::~DialogManager() rMessage() << "DialogManager: " << _dialogs.size() << " dialogs still in memory at shutdown." << std::endl; - _dialogs.clear(); + clear(); } } +const std::string& DialogManager::getName() const +{ + static std::string _name(MODULE_DIALOGMANAGER); + return _name; +} + +const StringSet& DialogManager::getDependencies() const +{ + static StringSet _dependencies + { + MODULE_MAINFRAME + }; + + return _dependencies; +} + +void DialogManager::initialiseModule(const IApplicationContext& ctx) +{ + rMessage() << getName() << "::initialiseModule called." << std::endl; + + GlobalMainFrame().signal_MainFrameShuttingDown().connect( + sigc::mem_fun(this, &DialogManager::clear)); +} + +void DialogManager::clear() +{ + _dialogs.clear(); +} + IDialogPtr DialogManager::createDialog(const std::string& title, wxWindow* parent) { cleanupOldDialogs(); // Allocate a new dialog - wxutil::DialogPtr dialog(new wxutil::Dialog(title, parent)); + auto dialog = std::make_shared(title, parent); _dialogs.push_back(dialog); @@ -43,7 +73,7 @@ IDialogPtr DialogManager::createMessageBox(const std::string& title, cleanupOldDialogs(); // Allocate a new dialog, use the main window if no parent specified - wxutil::MessageboxPtr box(new wxutil::Messagebox(title, text, type, parent)); + auto box = std::make_shared(title, text, type, parent); // Store it in the local map so that references are held _dialogs.push_back(box); @@ -66,7 +96,7 @@ IDirChooserPtr DialogManager::createDirChooser(const std::string& title) void DialogManager::cleanupOldDialogs() { - for (Dialogs::iterator i = _dialogs.begin(); i != _dialogs.end(); /* in-loop increment */) + for (auto i = _dialogs.begin(); i != _dialogs.end(); /* in-loop increment */) { if (i->use_count() <= 1) { @@ -89,4 +119,6 @@ IAnimationChooser* DialogManager::createAnimationChooser(wxWindow* parent) return new MD5AnimationChooser(parent); } +module::StaticModule dialogManagerModule; + } // namespace ui diff --git a/radiant/uimanager/DialogManager.h b/radiant/uimanager/DialogManager.h index ce4f7bb602..7c02169229 100644 --- a/radiant/uimanager/DialogManager.h +++ b/radiant/uimanager/DialogManager.h @@ -35,8 +35,14 @@ class DialogManager : IResourceChooser* createSoundShaderChooser(wxWindow* parent) override; IAnimationChooser* createAnimationChooser(wxWindow* parent) override; + // RegisterableModule + const std::string& getName() const override; + const StringSet& getDependencies() const override; + void initialiseModule(const IApplicationContext& ctx) override; + private: void cleanupOldDialogs(); + void clear(); }; typedef std::shared_ptr DialogManagerPtr; diff --git a/radiant/uimanager/UIManager.cpp b/radiant/uimanager/UIManager.cpp index e67b15c91a..2e90ce1b57 100644 --- a/radiant/uimanager/UIManager.cpp +++ b/radiant/uimanager/UIManager.cpp @@ -8,20 +8,10 @@ namespace ui { -IDialogManager& UIManager::getDialogManager() -{ - return *_dialogManager; -} - IGroupDialog& UIManager::getGroupDialog() { return GroupDialog::Instance(); } -void UIManager::clear() -{ - _dialogManager.reset(); -} - const std::string& UIManager::getName() const { static std::string _name(MODULE_UIMANAGER); @@ -43,12 +33,6 @@ const StringSet& UIManager::getDependencies() const void UIManager::initialiseModule(const IApplicationContext& ctx) { rMessage() << getName() << "::initialiseModule called" << std::endl; - - _dialogManager = std::make_shared(); - - GlobalMainFrame().signal_MainFrameShuttingDown().connect( - sigc::mem_fun(this, &UIManager::clear) - ); } module::StaticModule uiManagerModule; diff --git a/radiant/uimanager/UIManager.h b/radiant/uimanager/UIManager.h index 136c9d3f51..f997c7a59d 100644 --- a/radiant/uimanager/UIManager.h +++ b/radiant/uimanager/UIManager.h @@ -14,18 +14,9 @@ class UIManager : public IUIManager, public std::enable_shared_from_this { - // Sub-manager classes, constructed in initialiseModule to avoid being - // called before the main window is ready. - DialogManagerPtr _dialogManager; - public: IGroupDialog& getGroupDialog() override; - IDialogManager& getDialogManager() override; - - // Called on radiant shutdown - void clear(); - // RegisterableModule implementation const std::string& getName() const override; const StringSet& getDependencies() const override;