2012-03-09 01:23:41 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2014-05-22 21:07:15 +02:00
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "utils.h"
|
2012-03-09 01:23:41 +01:00
|
|
|
|
2014-05-22 21:07:15 +02:00
|
|
|
#include <libaegisub/exception.h>
|
2012-03-09 01:23:41 +01:00
|
|
|
|
2014-07-05 18:00:02 +02:00
|
|
|
#include <vector>
|
2012-03-09 01:23:41 +01:00
|
|
|
#include <typeinfo>
|
2012-03-10 03:15:58 +01:00
|
|
|
#include <wx/dialog.h>
|
2012-03-09 01:23:41 +01:00
|
|
|
|
2014-05-22 21:07:15 +02:00
|
|
|
namespace agi { struct Context; }
|
2012-06-07 04:48:13 +02:00
|
|
|
|
2012-03-09 01:23:41 +01:00
|
|
|
/// @brief A manager for dialogs
|
|
|
|
///
|
|
|
|
/// DialogManager keeps track of modal and modeless dialogs which have been
|
|
|
|
/// created, so that commands can be send to the appropriate places and so that
|
|
|
|
/// the same dialog can't be opened twice at once.
|
|
|
|
class DialogManager {
|
2014-07-05 18:00:02 +02:00
|
|
|
using dialog_pair = std::pair<const std::type_info *, wxDialog *>;
|
2012-03-09 01:23:41 +01:00
|
|
|
/// Dialogs which currently exist
|
2014-07-05 18:00:02 +02:00
|
|
|
std::vector<dialog_pair> created_dialogs;
|
2012-03-09 01:23:41 +01:00
|
|
|
|
|
|
|
/// Close handler which deletes and unregisters closed modeless dialogs
|
2013-07-09 17:33:04 +02:00
|
|
|
template<typename Event>
|
|
|
|
void OnClose(Event &evt) {
|
2012-03-09 01:23:41 +01:00
|
|
|
evt.Skip();
|
2013-07-09 17:33:04 +02:00
|
|
|
auto dialog = static_cast<wxWindow *>(evt.GetEventObject());
|
|
|
|
while (!dialog->IsTopLevel()) dialog = dialog->GetParent();
|
2012-03-09 01:23:41 +01:00
|
|
|
dialog->Destroy();
|
|
|
|
|
2012-12-23 00:35:13 +01:00
|
|
|
for (auto it = created_dialogs.begin(); it != created_dialogs.end(); ++it) {
|
2012-03-09 01:23:41 +01:00
|
|
|
if (it->second == dialog) {
|
|
|
|
created_dialogs.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 18:00:02 +02:00
|
|
|
template<typename T>
|
|
|
|
std::vector<dialog_pair>::iterator Find() {
|
|
|
|
for (auto it = begin(created_dialogs); it != end(created_dialogs); ++it) {
|
|
|
|
if (*it->first == typeid(T))
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return end(created_dialogs);
|
|
|
|
}
|
|
|
|
|
2012-03-09 01:23:41 +01:00
|
|
|
public:
|
|
|
|
/// Show a modeless dialog of the given type, creating it if needed
|
|
|
|
/// @tparam DialogType Type of dialog to show
|
|
|
|
template<class DialogType>
|
|
|
|
void Show(agi::Context *c) {
|
2014-07-05 18:00:02 +02:00
|
|
|
for (auto const& diag : created_dialogs) {
|
|
|
|
if (*diag.first == typeid(DialogType)) {
|
|
|
|
diag.second->Show();
|
|
|
|
diag.second->SetFocus();
|
2012-05-18 16:01:56 +02:00
|
|
|
}
|
2012-03-09 01:23:41 +01:00
|
|
|
}
|
2014-07-05 18:00:02 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
wxDialog *d = new DialogType(c);
|
|
|
|
created_dialogs.emplace_back(&typeid(DialogType), d);
|
|
|
|
d->Bind(wxEVT_CLOSE_WINDOW, &DialogManager::OnClose<wxCloseEvent>, this);
|
|
|
|
d->Bind(wxEVT_BUTTON, &DialogManager::OnClose<wxCommandEvent>, this, wxID_CANCEL);
|
|
|
|
d->Show();
|
|
|
|
SetFloatOnParent(d);
|
|
|
|
}
|
|
|
|
catch (agi::UserCancelException const&) { }
|
2012-03-09 01:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Show a modal dialog of the given type, creating it if needed
|
|
|
|
/// @tparam DialogType Type of dialog to show
|
|
|
|
template<class DialogType>
|
|
|
|
void ShowModal(agi::Context *c) {
|
|
|
|
DialogType diag(c);
|
2014-07-05 18:00:02 +02:00
|
|
|
created_dialogs.emplace_back(&typeid(DialogType), &diag);
|
2012-03-09 01:23:41 +01:00
|
|
|
try {
|
|
|
|
diag.ShowModal();
|
|
|
|
}
|
|
|
|
catch (...) {
|
2014-07-05 18:00:02 +02:00
|
|
|
created_dialogs.erase(Find<DialogType>());
|
2012-03-09 01:23:41 +01:00
|
|
|
throw;
|
|
|
|
}
|
2014-07-05 18:00:02 +02:00
|
|
|
created_dialogs.erase(Find<DialogType>());
|
2012-03-09 01:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the dialog of the given type
|
|
|
|
/// @tparam DialogType Type of dialog to get
|
2012-11-13 17:51:01 +01:00
|
|
|
/// @return A pointer to a DialogType or nullptr if no dialog of the given type has been created
|
2012-03-09 01:23:41 +01:00
|
|
|
template<class DialogType>
|
|
|
|
DialogType *Get() const {
|
2014-07-05 18:00:02 +02:00
|
|
|
auto it = const_cast<DialogManager *>(this)->Find<DialogType>();
|
2013-09-16 21:10:00 +02:00
|
|
|
return it != created_dialogs.end() ? static_cast<DialogType*>(it->second) : nullptr;
|
2012-03-09 01:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~DialogManager() {
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& it : created_dialogs) {
|
2013-07-09 17:33:04 +02:00
|
|
|
it.second->Unbind(wxEVT_CLOSE_WINDOW, &DialogManager::OnClose<wxCloseEvent>, this);
|
2013-12-12 03:25:13 +01:00
|
|
|
it.second->Unbind(wxEVT_BUTTON, &DialogManager::OnClose<wxCommandEvent>, this, wxID_CANCEL);
|
2012-11-04 04:53:03 +01:00
|
|
|
it.second->Destroy();
|
2012-03-09 01:23:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|