forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryWindow.cpp
488 lines (403 loc) · 13.9 KB
/
HistoryWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**********************************************************************
Sneedacity: A Digital Audio Editor
HistoryWindow.cpp
Joshua Haberman
Leland Lucius
*******************************************************************//*!
\class HistoryDialog
\brief Works with UndoManager to allow user to see descriptions of
and undo previous commands. Also allows you to selectively clear the
undo memory so as to free up space.
*//*******************************************************************/
#include "HistoryWindow.h"
#include <wx/app.h>
#include <wx/defs.h>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/event.h>
#include <wx/frame.h>
#include <wx/imaglist.h>
#include <wx/intl.h>
#include <wx/listctrl.h>
#include <wx/settings.h>
#include <wx/spinctrl.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include "AudioIO.h"
#include "Clipboard.h"
#include "CommonCommandFlags.h"
#include "../images/Arrow.xpm"
#include "../images/Empty9x16.xpm"
#include "UndoManager.h"
#include "Project.h"
#include "ProjectFileIO.h"
#include "ProjectHistory.h"
#include "ShuttleGui.h"
#include "widgets/SneedacityMessageBox.h"
#include "widgets/HelpSystem.h"
enum {
ID_AVAIL = 1000,
ID_FILESIZE,
ID_TOTAL,
ID_LEVELS,
ID_DISCARD,
ID_DISCARD_CLIPBOARD,
ID_COMPACT
};
BEGIN_EVENT_TABLE(HistoryDialog, wxDialogWrapper)
EVT_SHOW(HistoryDialog::OnShow)
EVT_SIZE(HistoryDialog::OnSize)
EVT_CLOSE(HistoryDialog::OnCloseWindow)
EVT_LIST_ITEM_SELECTED(wxID_ANY, HistoryDialog::OnItemSelected)
EVT_BUTTON(ID_DISCARD, HistoryDialog::OnDiscard)
EVT_BUTTON(ID_DISCARD_CLIPBOARD, HistoryDialog::OnDiscardClipboard)
EVT_BUTTON(ID_COMPACT, HistoryDialog::OnCompact)
EVT_BUTTON(wxID_HELP, HistoryDialog::OnGetURL)
END_EVENT_TABLE()
#define HistoryTitle XO("History")
HistoryDialog::HistoryDialog(SneedacityProject *parent, UndoManager *manager):
wxDialogWrapper(FindProjectFrame( parent ), wxID_ANY, HistoryTitle,
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
{
SetName();
mManager = manager;
mProject = parent;
mSelected = 0;
mAudioIOBusy = false;
//------------------------- Main section --------------------
// Construct the GUI.
ShuttleGui S(this, eIsCreating);
Populate(S);
wxTheApp->Bind(EVT_AUDIOIO_PLAYBACK,
&HistoryDialog::OnAudioIO,
this);
wxTheApp->Bind(EVT_AUDIOIO_CAPTURE,
&HistoryDialog::OnAudioIO,
this);
Clipboard::Get().Bind(
EVT_CLIPBOARD_CHANGE, &HistoryDialog::UpdateDisplay, this);
parent->Bind(EVT_UNDO_PUSHED, &HistoryDialog::UpdateDisplay, this);
parent->Bind(EVT_UNDO_MODIFIED, &HistoryDialog::UpdateDisplay, this);
parent->Bind(EVT_UNDO_OR_REDO, &HistoryDialog::UpdateDisplay, this);
parent->Bind(EVT_UNDO_RESET, &HistoryDialog::UpdateDisplay, this);
parent->Bind(EVT_UNDO_PURGE, &HistoryDialog::UpdateDisplay, this);
}
void HistoryDialog::Populate(ShuttleGui & S)
{
auto imageList = std::make_unique<wxImageList>(9, 16);
imageList->Add(wxIcon(empty9x16_xpm));
imageList->Add(wxIcon(arrow_xpm));
S.SetBorder(5);
S.StartVerticalLay(true);
{
S.StartStatic(XO("&Manage History"), 1);
{
mList = S
.MinSize()
.ConnectRoot(wxEVT_KEY_DOWN, &HistoryDialog::OnListKeyDown)
.AddListControlReportMode(
{ { XO("Action"), wxLIST_FORMAT_LEFT, 260 },
{ XO("Used Space"), wxLIST_FORMAT_LEFT, 125 } },
wxLC_SINGLE_SEL
);
//Assign rather than set the image list, so that it is deleted later.
// AssignImageList takes ownership
mList->AssignImageList(imageList.release(), wxIMAGE_LIST_SMALL);
S.StartMultiColumn(3, wxCENTRE);
{
S.AddPrompt(XXO("&Total space used"));
mTotal = S.Id(ID_TOTAL).Style(wxTE_READONLY).AddTextBox({}, wxT(""), 10);
S.AddVariableText( {} )->Hide();
#if defined(ALLOW_DISCARD)
S.AddPrompt(XXO("&Undo levels available"));
mAvail = S.Id(ID_AVAIL).Style(wxTE_READONLY).AddTextBox({}, wxT(""), 10);
S.AddVariableText( {} )->Hide();
S.AddPrompt(XXO("&Levels to discard"));
mLevels = safenew wxSpinCtrl(S.GetParent(),
ID_LEVELS,
wxT("1"),
wxDefaultPosition,
wxDefaultSize,
wxSP_ARROW_KEYS,
0,
mManager->GetCurrentState(),
0);
S.AddWindow(mLevels);
/* i18n-hint: (verb)*/
mDiscard = S.Id(ID_DISCARD).AddButton(XXO("&Discard"));
#endif
S.AddPrompt(XXO("Clip&board space used"));
mClipboard = S.Style(wxTE_READONLY).AddTextBox({}, wxT(""), 10);
#if defined(ALLOW_DISCARD)
S.Id(ID_DISCARD_CLIPBOARD).AddButton(XXO("D&iscard"));
#endif
}
S.EndMultiColumn();
}
S.EndStatic();
#if defined(ALLOW_DISCARD)
mCompact = safenew wxButton(this, ID_COMPACT, _("&Compact"));
S.AddStandardButtons(eOkButton | eHelpButton, mCompact);
#else
S.AddStandardButtons(eOkButton | eHelpButton);
#endif
}
S.EndVerticalLay();
// ----------------------- End of main section --------------
Layout();
Fit();
SetMinSize(GetSize());
mList->SetColumnWidth(0, mList->GetClientSize().x - mList->GetColumnWidth(1));
mList->SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
}
void HistoryDialog::OnAudioIO(wxCommandEvent& evt)
{
evt.Skip();
if (evt.GetInt() != 0)
mAudioIOBusy = true;
else
mAudioIOBusy = false;
#if defined(ALLOW_DISCARD)
mDiscard->Enable(!mAudioIOBusy);
mCompact->Enable(!mAudioIOBusy);
#endif
}
void HistoryDialog::UpdateDisplay(wxEvent& e)
{
e.Skip();
if(IsShown())
DoUpdate();
}
bool HistoryDialog::Show( bool show )
{
if ( show && !IsShown())
DoUpdate();
return wxDialogWrapper::Show( show );
}
void HistoryDialog::DoUpdate()
{
int i;
mManager->CalculateSpaceUsage();
mList->DeleteAllItems();
wxLongLong_t total = 0;
mSelected = mManager->GetCurrentState();
for (i = 0; i < (int)mManager->GetNumStates(); i++) {
TranslatableString desc, size;
total += mManager->GetLongDescription(i, &desc, &size);
mList->InsertItem(i, desc.Translation(), i == mSelected ? 1 : 0);
mList->SetItem(i, 1, size.Translation());
}
mTotal->SetValue(Internat::FormatSize(total).Translation());
auto clipboardUsage = mManager->GetClipboardSpaceUsage();
mClipboard->SetValue(Internat::FormatSize(clipboardUsage).Translation());
#if defined(ALLOW_DISCARD)
FindWindowById(ID_DISCARD_CLIPBOARD)->Enable(clipboardUsage > 0);
#endif
mList->EnsureVisible(mSelected);
mList->SetItemState(mSelected,
wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
UpdateLevels();
}
void HistoryDialog::UpdateLevels()
{
#if defined(ALLOW_DISCARD)
wxWindow *focus;
int value = mLevels->GetValue();
if (value > mSelected) {
value = mSelected;
}
if (value == 0) {
value = 1;
}
mLevels->SetValue(value);
mLevels->SetRange(1, mSelected);
mAvail->SetValue(wxString::Format(wxT("%d"), mSelected));
focus = FindFocus();
if ((focus == mDiscard || focus == mLevels) && mSelected == 0) {
mList->SetFocus();
}
mLevels->Enable(mSelected > 0);
mDiscard->Enable(!mAudioIOBusy && mSelected > 0);
#endif
}
void HistoryDialog::OnDiscard(wxCommandEvent & WXUNUSED(event))
{
int i = mLevels->GetValue();
mSelected -= i;
mManager->RemoveStates(0, i);
ProjectHistory::Get( *mProject ).SetStateTo(mSelected);
while(--i >= 0)
mList->DeleteItem(i);
DoUpdate();
}
void HistoryDialog::OnDiscardClipboard(wxCommandEvent & WXUNUSED(event))
{
Clipboard::Get().Clear();
}
void HistoryDialog::OnCompact(wxCommandEvent & WXUNUSED(event))
{
auto &projectFileIO = ProjectFileIO::Get(*mProject);
projectFileIO.ReopenProject();
auto baseFile = wxFileName(projectFileIO.GetFileName());
auto walFile = wxFileName(projectFileIO.GetFileName() + wxT("-wal"));
auto before = baseFile.GetSize() + walFile.GetSize();
projectFileIO.Compact({}, true);
auto after = baseFile.GetSize() + walFile.GetSize();
SneedacityMessageBox(
XO("Compacting actually freed %s of disk space.")
.Format(Internat::FormatSize((before - after).GetValue())),
XO("History"));
}
void HistoryDialog::OnGetURL(wxCommandEvent & WXUNUSED(event))
{
HelpSystem::ShowHelp(this, L"Undo,_Redo_and_History");
}
void HistoryDialog::OnItemSelected(wxListEvent &event)
{
if (mAudioIOBusy) {
mList->SetItemState(mSelected,
wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
return;
}
int selected = event.GetIndex();
int i;
for (i = 0; i < mList->GetItemCount(); i++) {
mList->SetItemImage(i, 0);
if (i > selected)
mList->SetItemTextColour(i, *wxLIGHT_GREY);
else
mList->SetItemTextColour(i, mList->GetTextColour());
}
mList->SetItemImage(selected, 1);
// Do not do a SetStateTo() if we're not actually changing the selected
// entry. Doing so can cause unnecessary delays upon initial load or while
// clicking the same entry over and over.
if (selected != mSelected) {
ProjectHistory::Get( *mProject ).SetStateTo(selected);
}
mSelected = selected;
UpdateLevels();
}
void HistoryDialog::OnListKeyDown(wxKeyEvent & event)
{
switch (event.GetKeyCode())
{
case WXK_RETURN:
// Don't know why wxListCtrls prevent default dialog action,
// but they do, so handle it.
EmulateButtonClickIfPresent(GetAffirmativeId());
break;
default:
event.Skip();
break;
}
}
void HistoryDialog::OnCloseWindow(wxCloseEvent & WXUNUSED(event))
{
this->Show(false);
}
void HistoryDialog::OnShow(wxShowEvent & event)
{
if (event.IsShown())
{
mList->SetFocus();
}
}
void HistoryDialog::OnSize(wxSizeEvent & WXUNUSED(event))
{
Layout();
mList->SetColumnWidth(0, mList->GetClientSize().x - mList->GetColumnWidth(1));
if (mList->GetItemCount() > 0)
mList->EnsureVisible(mSelected);
}
// PrefsListener implementation
void HistoryDialog::UpdatePrefs()
{
bool shown = IsShown();
if (shown) {
Show(false);
}
SetSizer(nullptr);
DestroyChildren();
SetTitle(HistoryTitle);
ShuttleGui S(this, eIsCreating);
Populate(S);
if (shown) {
Show(true);
}
}
// Remaining code hooks this add-on into the application
#include "commands/CommandContext.h"
#include "commands/CommandManager.h"
namespace {
// History window attached to each project is built on demand by:
SneedacityProject::AttachedWindows::RegisteredFactory sHistoryWindowKey{
[]( SneedacityProject &parent ) -> wxWeakRef< wxWindow > {
auto &undoManager = UndoManager::Get( parent );
return safenew HistoryDialog( &parent, &undoManager );
}
};
// Define our extra menu item that invokes that factory
struct Handler : CommandHandlerObject {
void OnHistory(const CommandContext &context)
{
auto &project = context.project;
auto historyWindow = &project.AttachedWindows::Get( sHistoryWindowKey );
historyWindow->Show();
historyWindow->Raise();
}
};
CommandHandlerObject &findCommandHandler(SneedacityProject &) {
// Handler is not stateful. Doesn't need a factory registered with
// SneedacityProject.
static Handler instance;
return instance;
}
// Register that menu item
using namespace MenuTable;
AttachedItem sAttachment{ wxT("View/Windows"),
// History window should be available either for UndoAvailableFlag
// or RedoAvailableFlag,
// but we can't make the AddItem flags and mask have both,
// because they'd both have to be true for the
// command to be enabled.
// If user has Undone the entire stack, RedoAvailableFlag is on
// but UndoAvailableFlag is off.
// If user has done things but not Undone anything,
// RedoAvailableFlag is off but UndoAvailableFlag is on.
// So in either of those cases,
// (AudioIONotBusyFlag | UndoAvailableFlag | RedoAvailableFlag) mask
// would fail.
// The only way to fix this in the current architecture
// is to hack in special cases for RedoAvailableFlag
// in SneedacityProject::UpdateMenus() (ugly)
// and CommandManager::HandleCommandEntry() (*really* ugly --
// shouldn't know about particular command names and flags).
// Here's the hack that would be necessary in
// SneedacityProject::UpdateMenus(), if somebody decides to do it:
// // Because EnableUsingFlags requires all the flag bits match the
// // corresponding mask bits,
// // "UndoHistory" specifies only
// // AudioIONotBusyFlag | UndoAvailableFlag, because that
// // covers the majority of cases where it should be enabled.
// // If history is not empty but we've Undone the whole stack,
// // we also want to enable,
// // to show the Redo's on stack.
// // "UndoHistory" might already be enabled,
// // but add this check for RedoAvailableFlag.
// if (flags & RedoAvailableFlag)
// GetCommandManager()->Enable(wxT("UndoHistory"), true);
// So for now, enable the command regardless of stack.
// It will just show empty sometimes.
// FOR REDESIGN,
// clearly there are some limitations with the flags/mask bitmaps.
( FinderScope{ findCommandHandler },
/* i18n-hint: Clicking this menu item shows the various editing steps
that have been taken.*/
Command( wxT("UndoHistory"), XXO("&History..."), &Handler::OnHistory,
AudioIONotBusyFlag() ) )
};
}