2013-01-04 16:01:50 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2011-09-28 21:47:40 +02:00
|
|
|
// 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.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2011-09-28 21:47:40 +02:00
|
|
|
// 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.
|
2009-07-29 07:43:02 +02:00
|
|
|
|
|
|
|
/// @file dialog_progress.cpp
|
2011-09-28 21:47:40 +02:00
|
|
|
/// @brief Progress-bar dialog box for displaying during long operations
|
2009-07-29 07:43:02 +02:00
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
#include "dialog_progress.h"
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/dispatch.h>
|
|
|
|
#include <libaegisub/exception.h>
|
2013-10-02 01:27:33 +02:00
|
|
|
#include <libaegisub/util_osx.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-01-28 15:58:57 +01:00
|
|
|
#include <atomic>
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/button.h>
|
2011-09-28 21:47:40 +02:00
|
|
|
#include <wx/gauge.h>
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/sizer.h>
|
2011-09-28 21:47:40 +02:00
|
|
|
#include <wx/stattext.h>
|
|
|
|
#include <wx/textctrl.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2013-10-04 04:53:29 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <shobjidl.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
using agi::dispatch::Main;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2013-10-04 04:53:29 +02:00
|
|
|
namespace {
|
|
|
|
void set_taskbar_progress(int progress) {
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
int major, minor;
|
|
|
|
wxGetOsVersion(&major, &minor);
|
|
|
|
if (major < 6 || (major == 6 && minor < 1)) return;
|
|
|
|
|
|
|
|
ITaskbarList3 *taskbar;
|
|
|
|
auto hr = ::CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
__uuidof(ITaskbarList3), (LPVOID *)&taskbar);
|
|
|
|
if (FAILED(hr)) return;
|
|
|
|
|
|
|
|
hr = taskbar->HrInit();
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
taskbar->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hwnd = wxTheApp->GetTopWindow()->GetHWND();
|
|
|
|
if (progress == 0 || progress == 100)
|
|
|
|
taskbar->SetProgressState(hwnd, TBPF_NOPROGRESS);
|
|
|
|
else if (progress == -1)
|
|
|
|
taskbar->SetProgressState(hwnd, TBPF_INDETERMINATE);
|
|
|
|
else
|
|
|
|
taskbar->SetProgressValue(hwnd, progress, 100);
|
|
|
|
|
|
|
|
taskbar->Release();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 02:39:07 +01:00
|
|
|
class DialogProgressSink final : public agi::ProgressSink {
|
2011-09-28 21:47:40 +02:00
|
|
|
DialogProgress *dialog;
|
2014-05-04 01:46:17 +02:00
|
|
|
std::atomic<bool> cancelled{false};
|
|
|
|
int progress = 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
public:
|
2014-05-04 01:46:17 +02:00
|
|
|
DialogProgressSink(DialogProgress *dialog) : dialog(dialog) { }
|
2008-10-28 05:03:29 +01:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void SetTitle(std::string const& title) override {
|
2013-01-04 16:01:50 +01:00
|
|
|
Main().Async([=]{ dialog->title->SetLabelText(to_wx(title)); });
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
2008-10-28 05:03:29 +01:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void SetMessage(std::string const& msg) override {
|
2013-01-04 16:01:50 +01:00
|
|
|
Main().Async([=]{ dialog->text->SetLabelText(to_wx(msg)); });
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void SetProgress(int64_t cur, int64_t max) override {
|
2014-05-04 01:46:17 +02:00
|
|
|
int new_progress = mid<int>(0, double(cur) / max * 300, 300);
|
|
|
|
if (new_progress != progress) {
|
|
|
|
progress = new_progress;
|
|
|
|
Main().Async([=]{ dialog->SetProgress(new_progress); });
|
|
|
|
}
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void Log(std::string const& str) override {
|
2013-01-04 16:01:50 +01:00
|
|
|
Main().Async([=]{ dialog->pending_log += to_wx(str); });
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
bool IsCancelled() override {
|
2011-09-28 21:47:40 +02:00
|
|
|
return cancelled;
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void Cancel() {
|
|
|
|
cancelled = true;
|
2008-10-28 05:03:29 +01:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void SetIndeterminate() override {
|
2013-01-04 16:01:50 +01:00
|
|
|
Main().Async([=]{ dialog->pulse_timer.Start(1000); });
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
};
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
DialogProgress::DialogProgress(wxWindow *parent, wxString const& title_text, wxString const& message)
|
|
|
|
: wxDialog(parent, -1, title_text, wxDefaultPosition, wxDefaultSize, wxBORDER_RAISED)
|
|
|
|
, pulse_timer(GetEventHandler())
|
2008-10-28 05:03:29 +01:00
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
title = new wxStaticText(this, -1, title_text, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
|
2013-09-27 00:54:59 +02:00
|
|
|
gauge = new wxGauge(this, -1, 300, wxDefaultPosition, wxSize(300,20));
|
2011-09-28 21:47:40 +02:00
|
|
|
text = new wxStaticText(this, -1, message, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
|
|
|
|
cancel_button = new wxButton(this, wxID_CANCEL);
|
2013-09-22 01:20:19 +02:00
|
|
|
log_output = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxSize(600, 240), wxTE_MULTILINE | wxTE_READONLY);
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// make the title a slightly larger font
|
|
|
|
wxFont title_font = title->GetFont();
|
|
|
|
int fontsize = title_font.GetPointSize();
|
|
|
|
title_font.SetPointSize(fontsize * 1.375);
|
|
|
|
title_font.SetWeight(wxFONTWEIGHT_BOLD);
|
|
|
|
title->SetFont(title_font);
|
|
|
|
|
|
|
|
wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
sizer->Add(title, wxSizerFlags().Expand().Center());
|
|
|
|
sizer->Add(gauge, wxSizerFlags(1).Expand().Border());
|
|
|
|
sizer->Add(text, wxSizerFlags().Expand().Center());
|
|
|
|
sizer->Add(cancel_button, wxSizerFlags().Center().Border());
|
|
|
|
sizer->Add(log_output, wxSizerFlags().Expand().Border(wxALL & ~wxTOP));
|
|
|
|
sizer->Hide(log_output);
|
|
|
|
|
|
|
|
SetSizerAndFit(sizer);
|
|
|
|
CenterOnParent();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
Bind(wxEVT_SHOW, &DialogProgress::OnShow, this);
|
2012-12-22 20:51:08 +01:00
|
|
|
Bind(wxEVT_TIMER, [=](wxTimerEvent&) { gauge->Pulse(); });
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-09-25 01:35:27 +02:00
|
|
|
void DialogProgress::Run(std::function<void(agi::ProgressSink*)> task, int priority) {
|
2011-09-28 21:47:40 +02:00
|
|
|
DialogProgressSink ps(this);
|
|
|
|
this->ps = &ps;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-10-02 01:27:33 +02:00
|
|
|
auto current_title = from_wx(title->GetLabelText());
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::dispatch::Background().Async([=]{
|
2013-10-02 01:27:33 +02:00
|
|
|
agi::osx::AppNapDisabler app_nap_disabler(current_title);
|
2013-01-04 16:01:50 +01:00
|
|
|
try {
|
|
|
|
task(this->ps);
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
this->ps->Log(e.GetMessage());
|
2013-01-04 16:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Main().Async([this]{
|
|
|
|
pulse_timer.Stop();
|
2014-04-05 16:51:31 +02:00
|
|
|
Unbind(wxEVT_IDLE, &DialogProgress::OnIdle, this);
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
// Unbind the cancel handler so that the default behavior happens (i.e. the
|
|
|
|
// dialog is closed) as there's no longer a task to cancel
|
2013-12-12 03:25:13 +01:00
|
|
|
Unbind(wxEVT_BUTTON, &DialogProgress::OnCancel, this, wxID_CANCEL);
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
// If it ran to completion and there is debug output, leave the window open
|
|
|
|
// so the user can read the debug output and switch the cancel button to a
|
|
|
|
// close button
|
|
|
|
bool cancelled = this->ps->IsCancelled();
|
2014-04-05 16:51:31 +02:00
|
|
|
if (cancelled || (log_output->IsEmpty() && !pending_log))
|
2013-01-04 16:01:50 +01:00
|
|
|
EndModal(!cancelled);
|
2014-03-28 03:17:24 +01:00
|
|
|
else {
|
2014-04-28 20:01:57 +02:00
|
|
|
if (!pending_log.empty()) {
|
|
|
|
wxIdleEvent evt;
|
|
|
|
OnIdle(evt);
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
cancel_button->SetLabelText(_("Close"));
|
2014-04-05 16:51:31 +02:00
|
|
|
gauge->SetValue(300);
|
2014-03-28 03:17:24 +01:00
|
|
|
}
|
2014-04-05 16:51:31 +02:00
|
|
|
set_taskbar_progress(0);
|
2013-01-04 16:01:50 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-23 19:00:21 +02:00
|
|
|
if (!ShowModal())
|
2011-09-28 21:47:40 +02:00
|
|
|
throw agi::UserCancelException("Cancelled by user");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 16:51:31 +02:00
|
|
|
void DialogProgress::OnShow(wxShowEvent& evt) {
|
|
|
|
if (!evt.IsShown()) return;
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
// Restore the cancel button in case it was previously switched to a close
|
|
|
|
// button
|
2013-12-12 03:25:13 +01:00
|
|
|
Bind(wxEVT_BUTTON, &DialogProgress::OnCancel, this, wxID_CANCEL);
|
2014-04-05 16:51:31 +02:00
|
|
|
Bind(wxEVT_IDLE, &DialogProgress::OnIdle, this);
|
2011-09-28 21:47:40 +02:00
|
|
|
cancel_button->SetLabelText(_("Cancel"));
|
|
|
|
cancel_button->Enable();
|
|
|
|
|
|
|
|
wxSizer *sizer = GetSizer();
|
|
|
|
if (sizer->IsShown(log_output)) {
|
|
|
|
sizer->Hide(log_output);
|
|
|
|
Layout();
|
|
|
|
sizer->Fit(this);
|
|
|
|
log_output->Clear();
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-02-16 22:22:04 +01:00
|
|
|
void DialogProgress::OnIdle(wxIdleEvent&) {
|
2014-03-27 15:19:58 +01:00
|
|
|
if (progress_current > progress_target) {
|
|
|
|
progress_current = progress_target;
|
|
|
|
gauge->SetValue(progress_current);
|
|
|
|
set_taskbar_progress(progress_current / 3);
|
|
|
|
}
|
|
|
|
else if (progress_current < progress_target) {
|
2013-09-27 00:54:59 +02:00
|
|
|
using namespace std::chrono;
|
|
|
|
auto now = steady_clock::now();
|
|
|
|
int ms = mid<int>(0, duration_cast<milliseconds>(now - progress_anim_start_time).count(), progress_anim_duration);
|
|
|
|
int dist = (progress_target - progress_anim_start_value) * ms / progress_anim_duration;
|
|
|
|
if (dist) {
|
|
|
|
progress_current = progress_anim_start_value + dist;
|
|
|
|
gauge->SetValue(progress_current);
|
2013-10-04 04:53:29 +02:00
|
|
|
set_taskbar_progress(progress_current / 3);
|
2013-09-27 00:54:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 22:22:04 +01:00
|
|
|
if (!pending_log) return;
|
|
|
|
|
|
|
|
if (log_output->IsEmpty()) {
|
|
|
|
wxSizer *sizer = GetSizer();
|
|
|
|
sizer->Show(log_output);
|
|
|
|
Layout();
|
|
|
|
sizer->Fit(this);
|
2013-09-22 01:20:19 +02:00
|
|
|
CenterOnParent();
|
2012-02-16 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*log_output << pending_log;
|
|
|
|
log_output->SetInsertionPointEnd();
|
|
|
|
pending_log.clear();
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:09:31 +01:00
|
|
|
void DialogProgress::OnCancel(wxCommandEvent &) {
|
2011-09-28 21:47:40 +02:00
|
|
|
ps->Cancel();
|
|
|
|
cancel_button->Enable(false);
|
|
|
|
cancel_button->SetLabelText(_("Cancelling..."));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2013-09-27 00:54:59 +02:00
|
|
|
|
|
|
|
void DialogProgress::SetProgress(int target) {
|
2014-03-21 16:39:58 +01:00
|
|
|
if (target == progress_target) return;
|
2013-09-27 00:54:59 +02:00
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
progress_anim_start_value = progress_current;
|
|
|
|
auto now = steady_clock::now();
|
|
|
|
if (progress_target == 0)
|
|
|
|
progress_anim_duration = 1000;
|
|
|
|
else
|
|
|
|
progress_anim_duration = std::max<int>(100, duration_cast<milliseconds>(now - progress_anim_start_time).count() * 11 / 10);
|
|
|
|
progress_anim_start_time = now;
|
|
|
|
progress_target = target;
|
|
|
|
}
|