Audacity  2.2.2
HistoryWindow.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  HistoryWindow.cpp
6 
7  Joshua Haberman
8  Leland Lucius
9 
10 *******************************************************************//*******************************************************************/
18 
19 #include "Audacity.h"
20 
21 #include <wx/app.h>
22 #include <wx/defs.h>
23 #include <wx/button.h>
24 #include <wx/dialog.h>
25 #include <wx/event.h>
26 #include <wx/imaglist.h>
27 #include <wx/intl.h>
28 #include <wx/listctrl.h>
29 #include <wx/settings.h>
30 #include <wx/stattext.h>
31 #include <wx/textctrl.h>
32 
33 #include "AudioIO.h"
34 #include "../images/Arrow.xpm"
35 #include "../images/Empty9x16.xpm"
36 #include "HistoryWindow.h"
37 #include "UndoManager.h"
38 #include "Project.h"
39 #include "ShuttleGui.h"
40 #include "Track.h"
41 
42 enum {
43  ID_AVAIL = 1000,
48 };
49 
50 BEGIN_EVENT_TABLE(HistoryWindow, wxDialogWrapper)
51  EVT_SIZE(HistoryWindow::OnSize)
52  EVT_CLOSE(HistoryWindow::OnCloseWindow)
53  EVT_LIST_ITEM_SELECTED(wxID_ANY, HistoryWindow::OnItemSelected)
55  EVT_BUTTON(ID_DISCARD_CLIPBOARD, HistoryWindow::OnDiscardClipboard)
57 
59  wxDialogWrapper((wxWindow*)parent, wxID_ANY, wxString(_("History")),
60  wxDefaultPosition, wxDefaultSize,
61  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
62 {
63  SetName(GetTitle());
64 
65  mManager = manager;
66  mProject = parent;
67  mSelected = 0;
68  mAudioIOBusy = false;
69 
70  auto imageList = std::make_unique<wxImageList>(9, 16);
71  imageList->Add(wxIcon(empty9x16_xpm));
72  imageList->Add(wxIcon(arrow_xpm));
73 
74  //------------------------- Main section --------------------
75  // Construct the GUI.
76  ShuttleGui S(this, eIsCreating);
77 
78  S.SetBorder(5);
79  S.StartVerticalLay(true);
80  {
81  S.StartStatic(_("&Manage History"), 1);
82  {
83  mList = S.AddListControlReportMode();
84  // Do this BEFORE inserting the columns. On the Mac at least, the
85  // columns are deleted and later InsertItem()s will cause Audacity to crash.
86  mList->SetSingleStyle(wxLC_SINGLE_SEL);
87  mList->InsertColumn(0, _("Action"), wxLIST_FORMAT_LEFT, 260);
88  mList->InsertColumn(1, _("Reclaimable Space"), wxLIST_FORMAT_LEFT, 125);
89 
90  //Assign rather than set the image list, so that it is deleted later.
91  // AssignImageList takes ownership
92  mList->AssignImageList(imageList.release(), wxIMAGE_LIST_SMALL);
93 
94  S.StartMultiColumn(3, wxCENTRE);
95  {
96  // FIXME: Textbox labels have inconsistent capitalization
97  mTotal = S.Id(ID_TOTAL).AddTextBox(_("&Total space used"), wxT("0"), 10);
98  mTotal->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(HistoryWindow::OnChar));
99  S.AddVariableText( {} )->Hide();
100 
101  mAvail = S.Id(ID_AVAIL).AddTextBox(_("&Undo Levels Available"), wxT("0"), 10);
102  mAvail->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(HistoryWindow::OnChar));
103  S.AddVariableText( {} )->Hide();
104 
105  S.AddPrompt(_("&Levels To Discard"));
106  mLevels = safenew wxSpinCtrl(this,
107  ID_LEVELS,
108  wxT("1"),
109  wxDefaultPosition,
110  wxDefaultSize,
111  wxSP_ARROW_KEYS,
112  0,
113  mManager->GetCurrentState() - 1,
114  0);
115  S.AddWindow(mLevels);
116  /* i18n-hint: (verb)*/
117  mDiscard = S.Id(ID_DISCARD).AddButton(_("&Discard"));
118 
119  mClipboard = S.AddTextBox(_("Clipboard space used"), wxT("0"), 10);
120  mClipboard->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(HistoryWindow::OnChar));
121  S.Id(ID_DISCARD_CLIPBOARD).AddButton(_("Discard"));
122  }
123  S.EndMultiColumn();
124  }
125  S.EndStatic();
126 
127  S.StartHorizontalLay(wxALIGN_RIGHT, false);
128  {
129  S.SetBorder(10);
130  S.Id(wxID_OK).AddButton(_("&OK"))->SetDefault();
131  }
132  S.EndHorizontalLay();
133  }
134  S.EndVerticalLay();
135  // ----------------------- End of main section --------------
136 
137  DoUpdate();
138  mList->SetMinSize(mList->GetSize());
139  Fit();
140  SetMinSize(GetSize());
141  mList->SetColumnWidth(0, mList->GetClientSize().x - mList->GetColumnWidth(1));
142  mList->SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
143 
144  wxTheApp->Connect(EVT_AUDIOIO_PLAYBACK,
145  wxCommandEventHandler(HistoryWindow::OnAudioIO),
146  NULL,
147  this);
148 
149  wxTheApp->Connect(EVT_AUDIOIO_CAPTURE,
150  wxCommandEventHandler(HistoryWindow::OnAudioIO),
151  NULL,
152  this);
153 }
154 
156 {
157  wxTheApp->Disconnect(EVT_AUDIOIO_PLAYBACK,
158  wxCommandEventHandler(HistoryWindow::OnAudioIO),
159  NULL,
160  this);
161 
162  wxTheApp->Disconnect(EVT_AUDIOIO_CAPTURE,
163  wxCommandEventHandler(HistoryWindow::OnAudioIO),
164  NULL,
165  this);
166 
167  mAvail->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(HistoryWindow::OnChar));
168 }
169 
170 void HistoryWindow::OnAudioIO(wxCommandEvent& evt)
171 {
172  evt.Skip();
173 
174  if (evt.GetInt() != 0)
175  mAudioIOBusy = true;
176  else
177  mAudioIOBusy = false;
178 
179  mDiscard->Enable(!mAudioIOBusy);
180 }
181 
183 {
184  if(IsShown())
185  DoUpdate();
186 }
187 
189 {
190  int i;
191 
193 
194  mList->DeleteAllItems();
195 
196  wxLongLong_t total = 0;
198  for (i = 0; i < (int)mManager->GetNumStates(); i++) {
199  wxString desc, size;
200 
201  total += mManager->GetLongDescription(i + 1, &desc, &size);
202  mList->InsertItem(i, desc, i == mSelected ? 1 : 0);
203  mList->SetItem(i, 1, size);
204  }
205 
206  mTotal->SetValue(Internat::FormatSize(total));
207 
208  auto clipboardUsage = mManager->GetClipboardSpaceUsage();
209  mClipboard->SetValue(Internat::FormatSize(clipboardUsage));
210  FindWindowById(ID_DISCARD_CLIPBOARD)->Enable(clipboardUsage > 0);
211 
212  mList->EnsureVisible(mSelected);
213 
214  mList->SetItemState(mSelected,
215  wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
216  wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
217 
218  UpdateLevels();
219 }
220 
222 {
223  wxWindow *focus;
224  int value = mLevels->GetValue();
225 
226  if (value > mSelected) {
227  value = mSelected;
228  }
229 
230  if (value == 0) {
231  value = 1;
232  }
233 
234  mLevels->SetValue(value);
235  mLevels->SetRange(1, mSelected);
236 
237  mAvail->SetValue(wxString::Format(wxT("%d"), mSelected));
238 
239  focus = FindFocus();
240  if ((focus == mDiscard || focus == mLevels) && mSelected == 0) {
241  mList->SetFocus();
242  }
243 
244  mLevels->Enable(mSelected > 0);
245  mDiscard->Enable(!mAudioIOBusy && mSelected > 0);
246 }
247 
248 void HistoryWindow::OnDiscard(wxCommandEvent & WXUNUSED(event))
249 {
250  int i = mLevels->GetValue();
251 
252  mSelected -= i;
255 
256  while(--i >= 0)
257  mList->DeleteItem(i);
258 
259  DoUpdate();
260 }
261 
262 void HistoryWindow::OnDiscardClipboard(wxCommandEvent & WXUNUSED(event))
263 {
265  DoUpdate();
266 }
267 
268 void HistoryWindow::OnItemSelected(wxListEvent &event)
269 {
270  if (mAudioIOBusy) {
271  mList->SetItemState(mSelected,
272  wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
273  wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
274  return;
275  }
276 
277  int selected = event.GetIndex();
278  int i;
279 
280  for (i = 0; i < mList->GetItemCount(); i++) {
281  mList->SetItemImage(i, 0);
282  if (i > selected)
283  mList->SetItemTextColour(i, *wxLIGHT_GREY);
284  else
285  mList->SetItemTextColour(i, mList->GetTextColour());
286  }
287  mList->SetItemImage(selected, 1);
288 
289  // Do not do a SetStateTo() if we're not actually changing the selected
290  // entry. Doing so can cause unnecessary delays upon initial load or while
291  // clicking the same entry over and over.
292  if (selected != mSelected) {
293  mProject->SetStateTo(selected + 1);
294  }
295  mSelected = selected;
296 
297  UpdateLevels();
298 }
299 
300 void HistoryWindow::OnCloseWindow(wxCloseEvent & WXUNUSED(event))
301 {
302  this->Show(false);
303 }
304 
305 void HistoryWindow::OnSize(wxSizeEvent & WXUNUSED(event))
306 {
307  Layout();
308  mList->SetColumnWidth(0, mList->GetClientSize().x - mList->GetColumnWidth(1));
309  if (mList->GetItemCount() > 0)
310  mList->EnsureVisible(mSelected);
311 }
312 
313 void HistoryWindow::OnChar(wxKeyEvent &event)
314 {
315  event.Skip(false);
316  return;
317 }
static wxString FormatSize(wxLongLong size)
Convert a number to a string while formatting it in bytes, KB, MB, GB.
Definition: Internat.cpp:190
wxLongLong_t GetClipboardSpaceUsage() const
Definition: UndoManager.h:131
void OnDiscardClipboard(wxCommandEvent &event)
unsigned int GetNumStates()
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.
Definition: HistoryWindow.h:27
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:366
wxWindow * AddWindow(wxWindow *pWindow, int Flags=wxALIGN_CENTRE|wxALL)
Definition: ShuttleGui.cpp:257
const wxChar * desc
Definition: ExportPCM.cpp:58
unsigned int GetCurrentState()
wxListCtrl * mList
Definition: HistoryWindow.h:49
void OnChar(wxKeyEvent &event)
void CalculateSpaceUsage()
wxLongLong_t GetLongDescription(unsigned int n, wxString *desc, wxString *size)
wxTextCtrl * mAvail
Definition: HistoryWindow.h:52
void EndMultiColumn()
void RemoveStates(int num)
wxSpinCtrl * mLevels
Definition: HistoryWindow.h:53
void OnItemSelected(wxListEvent &event)
#define safenew
Definition: Audacity.h:223
void OnCloseWindow(wxCloseEvent &WXUNUSED(event))
void EndHorizontalLay()
Definition: ShuttleGui.cpp:975
void AddPrompt(const wxString &Prompt)
Right aligned text string.
Definition: ShuttleGui.cpp:215
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:161
void EndVerticalLay()
Definition: ShuttleGui.cpp:991
wxTextCtrl * AddTextBox(const wxString &Caption, const wxString &Value, const int nChars)
Definition: ShuttleGui.cpp:493
void OnSize(wxSizeEvent &event)
void StartHorizontalLay(int PositionFlags=wxALIGN_CENTRE, int iProp=1)
Definition: ShuttleGui.cpp:966
wxListCtrl * AddListControlReportMode()
Definition: ShuttleGui.cpp:627
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
Definition: ShuttleGui.cpp:998
AudacityProject * mProject
Definition: HistoryWindow.h:47
static void ClearClipboard()
Definition: Project.cpp:4684
ShuttleGui & Id(int id)
void OnDiscard(wxCommandEvent &event)
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
wxButton * mDiscard
Definition: HistoryWindow.h:54
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
wxTextCtrl * mClipboard
Definition: HistoryWindow.h:51
void OnAudioIO(wxCommandEvent &evt)
wxStaticText * AddVariableText(const wxString &Str, bool bCenter=false, int PositionFlags=0)
Definition: ShuttleGui.cpp:373
wxStaticBox * StartStatic(const wxString &Str, int iProp=0)
Definition: ShuttleGui.cpp:701
Works with HistoryWindow to provide the Undo functionality.
Definition: UndoManager.h:93
END_EVENT_TABLE()
void SetBorder(int Border)
Definition: ShuttleGui.h:251
wxTextCtrl * mTotal
Definition: HistoryWindow.h:50
wxButton * AddButton(const wxString &Text, int PositionFlags=wxALIGN_CENTRE)
Definition: ShuttleGui.cpp:301
void SetStateTo(unsigned int n)
Definition: Project.cpp:4598
UndoManager * mManager
Definition: HistoryWindow.h:48
EVT_LIST_ITEM_SELECTED(CurvesListID, EditCurvesDialog::OnListSelectionChange) EVT_LIST_ITEM_DESELECTED(CurvesListID
Constructor.
void StartVerticalLay(int iProp=1)
Definition: ShuttleGui.cpp:982