Audacity  3.0.3
AudacityCommand.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  AudacityCommand.cpp
6 
7  James Crook
8 
9 *******************************************************************//****************************************************************//*******************************************************************/
21 
22 
23 #include "AudacityCommand.h"
24 
25 #include "CommandContext.h"
26 
27 #include <algorithm>
28 
29 #include <wx/defs.h>
30 #include <wx/sizer.h>
31 #include <wx/stockitem.h>
32 #include <wx/string.h>
33 #include <wx/tglbtn.h>
34 #include <wx/timer.h>
35 #include <wx/utils.h>
36 #include <wx/log.h>
37 
38 #include "ConfigInterface.h"
39 
40 #include "../Shuttle.h"
41 #include "../ShuttleGui.h"
42 #include "../widgets/ProgressDialog.h"
43 #include "../widgets/HelpSystem.h"
44 #include "../widgets/AudacityMessageBox.h"
45 
46 #include <unordered_map>
47 
48 namespace {
49 
51 {
52  static AudacityCommand::VetoDialogHook sHook = nullptr;
53  return sHook;
54 }
55 
56 }
57 
60 {
61  auto &theHook = GetVetoDialogHook();
62  auto result = theHook;
63  theHook = hook;
64  return result;
65 }
66 
68 {
69  mProgress = NULL;
70  mUIParent = NULL;
71  mUIDialog = NULL;
72  mUIDebug = false;
73  mIsBatch = false;
74  mNeedsInit = true;
75 }
76 
78 {
79  if (mUIDialog)
80  mUIDialog->Close();
81 }
82 
83 
86 wxString AudacityCommand::GetVersion(){ return AUDACITY_VERSION_STRING;}
87 
88 
90  if( !mNeedsInit )
91  return true;
92  mNeedsInit = false;
93  ShuttleDefaults DefaultSettingShuttle;
94  return DefineParams( DefaultSettingShuttle );
95 }
96 
97 bool AudacityCommand::ShowInterface(wxWindow *parent, bool WXUNUSED(forceModal))
98 {
99  if (mUIDialog)
100  {
101  if ( mUIDialog->Close(true) )
102  mUIDialog = nullptr;
103  return false;
104  }
105 
106  // mUIDialog is null
107  auto cleanup = valueRestorer( mUIDialog );
108 
109  mUIDialog = CreateUI(parent, this);
110  if (!mUIDialog)
111  return false;
112 
113  mUIDialog->Layout();
114  mUIDialog->Fit();
115  mUIDialog->SetMinSize(mUIDialog->GetSize());
116 
117  // The Screenshot command might be popping this dialog up, just to capture it.
118  auto hook = GetVetoDialogHook();
119  if( hook && hook( mUIDialog ) )
120  return false;
121 
122  bool res = mUIDialog->ShowModal() != 0;
123  return res;
124 }
125 
126 wxDialog *AudacityCommand::CreateUI(wxWindow *parent, AudacityCommand * WXUNUSED(client))
127 {
129  parent, GetName(), this}};
130 
131  if (dlg->Init())
132  {
133  // release() is safe because parent will own it
134  return dlg.release();
135  }
136  return NULL;
137 }
138 
140 {
141  CommandParameters eap;
142 
144  {
145  return false;
146  }
147 
149  S.mpEap = &eap;
150  bool bResult = DefineParams( S );
151  wxASSERT_MSG( bResult, "You did not define DefineParameters() for this command" );
152  static_cast<void>(bResult); // fix unused variable warning in release mode
153 
154  return eap.GetParameters(parms);
155 }
156 
157 bool AudacityCommand::SetAutomationParameters(const wxString & parms)
158 {
159  wxString preset = parms;
160 
161  CommandParameters eap(parms);
163 
164  S.SetForWriting( &eap );
165  bool bResult = DefineParams( S );
166  wxASSERT_MSG( bResult, "You did not define DefineParameters() for this command" );
167  static_cast<void>(bResult); // fix unused variable warning in release mode
168  if (!S.bOK)
169  {
171  XO(
172 "%s: Could not load settings below. Default settings will be used.\n\n%s")
173  .Format( GetName(), preset ) );
174 
175  // fror now always succeed, so that we can prompt the user.
176  return true;
177  }
178 
179  return TransferDataToWindow();
180 }
181 
183  const CommandContext & context,
184  bool shouldPrompt /* = true */)
185 {
186  // Note: Init may read parameters from preferences
187  if (!Init())
188  {
189  return false;
190  }
191 
192  // Prompting will be bypassed when applying a command that has already
193  // been configured, e.g. repeating the last effect on a different selection.
194  // Prompting may call AudacityCommand::Preview
195  if (shouldPrompt && /*IsInteractive() && */!PromptUser(parent))
196  {
197  return false;
198  }
199 
200  auto cleanup = finally( [&] {
201  End();
202  } );
203 
204  bool returnVal = true;
205  bool skipFlag = CheckWhetherSkipAudacityCommand();
206  if (skipFlag == false)
207  {
208  auto name = GetName();
209  ProgressDialog progress{
210  name,
211  XO("Applying %s...").Format( name ),
213  };
214  auto vr = valueRestorer( mProgress, &progress );
215 
216  returnVal = Apply(context);
217  }
218  return returnVal;
219 }
220 
221 // This is used from Macros.
222 bool AudacityCommand::PromptUser(wxWindow *parent)
223 {
224  return ShowInterface(parent, IsBatchProcessing());
225 }
226 
228 {
229  if (mUIParent && !mUIParent->TransferDataToWindow())
230  return false;
231  return true;
232 }
233 
235 {
236  if (mUIParent && (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow()))
237  return false;
238  return true;
239 }
240 
242  const TranslatableString& message, long style,
243  const TranslatableString &titleStr)
244 {
245  auto title = titleStr.empty()
246  ? GetName()
247  : XO("%s: %s").Format( GetName(), titleStr );
248  return AudacityMessageBox(message, title, style, mUIParent);
249 }
250 
251 BEGIN_EVENT_TABLE(AudacityCommandDialog, wxDialogWrapper)
256 
258  const TranslatableString & title,
259  AudacityCommand * pCommand,
260  int type,
261  int flags,
262  int additionalButtons)
263 : wxDialogWrapper(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, flags)
264 {
265  mType = type;
266  wxASSERT( pCommand );
267  mpCommand = pCommand;
268  mAdditionalButtons = additionalButtons |eCancelButton;
269  if( !pCommand->ManualPage().empty() )
270  mAdditionalButtons |= eHelpButton;
271 }
272 
274 {
275  ShuttleGui S(this, eIsCreating);
276 
277  S.SetBorder(5);
278  S.StartVerticalLay(true);
279  {
281 
282  long buttons = eOkButton;
284  }
285  S.EndVerticalLay();
286 
287  Layout();
288  Fit();
289  SetMinSize(GetSize());
290  Center();
291  return true;
292 }
293 
298 {
299  wxASSERT( mpCommand );
301 }
302 
304 {
307  return true;
308 }
309 
311 {
314  return true;
315 }
316 
318 {
319  return true;
320 }
321 
322 void AudacityCommandDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
323 {
324  // On wxGTK (wx2.8.12), the default action is still executed even if
325  // the button is disabled. This appears to affect all wxDialogs, not
326  // just our AudacityCommands dialogs. So, this is a only temporary workaround
327  // for legacy effects that disable the OK button. Hopefully this has
328  // been corrected in wx3.
329  if (FindWindow(wxID_OK)->IsEnabled() && Validate() && TransferDataFromWindow())
330  {
331  EndModal(true);
332  }
333 }
334 
335 
336 void AudacityCommandDialog::OnCancel(wxCommandEvent & WXUNUSED(evt))
337 {
338  EndModal(false);
339 }
340 
341 void AudacityCommandDialog::OnHelp(wxCommandEvent & WXUNUSED(event))
342 {
343  if( mpCommand )
344  {
345  // otherwise use ShowHelp
346  HelpSystem::ShowHelp(FindWindow(wxID_HELP), mpCommand->ManualPage(), true);
347  }
348 }
349 
350 
AudacityCommand::mIsBatch
bool mIsBatch
Definition: AudacityCommand.h:135
EVT_BUTTON
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
ShuttleSetAutomation
Shuttle that sets parameters to a value (from a string)
Definition: Shuttle.h:106
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
CommandParameters
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
Definition: EffectAutomationParameters.h:67
eIsCreating
@ eIsCreating
Definition: ShuttleGui.h:38
TranslatableString::empty
bool empty() const
Definition: TranslatableString.h:72
valueRestorer
ValueRestorer< T > valueRestorer(T &var)
inline functions provide convenient parameter type deduction
Definition: MemoryX.h:354
AudacityCommand::DoAudacityCommand
bool DoAudacityCommand(wxWindow *parent, const CommandContext &context, bool shouldPrompt=true)
Definition: AudacityCommand.cpp:182
anonymous_namespace{AudacityCommand.cpp}::GetVetoDialogHook
AudacityCommand::VetoDialogHook & GetVetoDialogHook()
Definition: AudacityCommand.cpp:50
ShuttleGuiBase::StartVerticalLay
void StartVerticalLay(int iProp=1)
Definition: ShuttleGui.cpp:1184
AudacityCommand::CheckWhetherSkipAudacityCommand
virtual bool CheckWhetherSkipAudacityCommand()
Definition: AudacityCommand.h:116
AudacityCommand::PromptUser
virtual bool PromptUser(wxWindow *parent)
Definition: AudacityCommand.cpp:222
AudacityCommand::PopulateOrExchange
virtual void PopulateOrExchange(ShuttleGui &WXUNUSED(S))
Definition: AudacityCommand.h:122
AudacityMessageBox
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
Definition: AudacityMessageBox.cpp:17
eIsGettingFromDialog
@ eIsGettingFromDialog
Definition: ShuttleGui.h:39
AudacityCommandDialog::Init
bool Init()
Definition: AudacityCommand.cpp:273
AudacityCommand::End
virtual void End()
Definition: AudacityCommand.h:121
AudacityCommandDialog::OnOk
virtual void OnOk(wxCommandEvent &evt)
Definition: AudacityCommand.cpp:322
AudacityCommandDialog::TransferDataToWindow
bool TransferDataToWindow() override
Definition: AudacityCommand.cpp:303
eHelpButton
@ eHelpButton
Definition: ShuttleGui.h:604
PluginPath
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
pdlgHideStopButton
@ pdlgHideStopButton
Definition: ProgressDialog.h:38
Format
Abstract base class used in importing a file.
AudacityCommand::mUIParent
wxWindow * mUIParent
Definition: AudacityCommand.h:131
AudacityCommand::SetVetoDialogHook
static VetoDialogHook SetVetoDialogHook(VetoDialogHook hook)
Definition: AudacityCommand.cpp:58
AudacityCommand::~AudacityCommand
virtual ~AudacityCommand()
Definition: AudacityCommand.cpp:77
ShuttleDefaults
Shuttle that sets parameters to their default values.
Definition: Shuttle.h:131
AudacityCommandDialog::OnCancel
virtual void OnCancel(wxCommandEvent &evt)
Definition: AudacityCommand.cpp:336
AudacityCommand::ManualPage
virtual ManualPageID ManualPage()
Definition: AudacityCommand.h:68
XO
#define XO(s)
Definition: Internat.h:31
CommandParameters::GetParameters
bool GetParameters(wxString &parms)
Definition: EffectAutomationParameters.h:258
AudacityCommand::MessageBox
int MessageBox(const TranslatableString &message, long style=DefaultMessageBoxStyle, const TranslatableString &titleStr={})
Definition: AudacityCommand.cpp:241
AudacityCommand::AudacityCommand
AudacityCommand()
Definition: AudacityCommand.cpp:67
AudacityCommand::mUIDialog
wxDialog * mUIDialog
Definition: AudacityCommand.h:130
AudacityCommandDialog::TransferDataFromWindow
bool TransferDataFromWindow() override
Definition: AudacityCommand.cpp:310
AudacityCommand::VetoDialogHook
bool(*)(wxDialog *) VetoDialogHook
Definition: AudacityCommand.h:51
HelpSystem::ShowHelp
static void ShowHelp(wxWindow *parent, const FilePath &localFileName, const URLString &remoteURL, bool bModal=false, bool alwaysDefaultBrowser=false)
Definition: HelpSystem.cpp:237
eIsSettingToDialog
@ eIsSettingToDialog
Definition: ShuttleGui.h:40
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
AudacityCommand::GetVendor
VendorSymbol GetVendor() override
Definition: AudacityCommand.cpp:85
AudacityCommand::IsBatchProcessing
virtual bool IsBatchProcessing()
Definition: AudacityCommand.h:69
AudacityCommand::mNeedsInit
bool mNeedsInit
Definition: AudacityCommand.h:137
AudacityCommandDialog::PopulateOrExchange
virtual void PopulateOrExchange(ShuttleGui &S)
Definition: AudacityCommand.cpp:297
CommandContext.h
eCancelButton
@ eCancelButton
Definition: ShuttleGui.h:601
ShuttleParams::mpEap
CommandParameters * mpEap
Definition: Shuttle.h:66
ProgressDialog
ProgressDialog Class.
Definition: ProgressDialog.h:51
CommandContext
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
Definition: CommandContext.h:34
AudacityCommand::ShowInterface
bool ShowInterface(wxWindow *parent, bool forceModal=false)
Definition: AudacityCommand.cpp:97
ShuttleGetAutomation
Shuttle that gets parameter values into a string.
Definition: Shuttle.h:88
AudacityCommand::Apply
virtual bool Apply(const CommandContext &WXUNUSED(context))
Definition: AudacityCommand.h:72
ShuttleGuiBase::EndVerticalLay
void EndVerticalLay()
Definition: ShuttleGui.cpp:1203
AudacityCommand::GetSymbol
ComponentInterfaceSymbol GetSymbol() override=0
AudacityCommand::GetAutomationParameters
virtual bool GetAutomationParameters(wxString &parms)
Definition: AudacityCommand.cpp:139
AudacityCommand
Base class for command in Audacity.
Definition: AudacityCommand.h:41
name
const TranslatableString name
Definition: Distortion.cpp:98
AudacityCommand::mProgress
ProgressDialog * mProgress
Definition: AudacityCommand.h:128
AudacityCommand::GetPath
PluginPath GetPath() override
Definition: AudacityCommand.cpp:84
AudacityCommand::CreateUI
wxDialog * CreateUI(wxWindow *parent, AudacityCommand *client)
Definition: AudacityCommand.cpp:126
ConfigInterface.h
AudacityCommandDialog::mpCommand
AudacityCommand * mpCommand
Definition: AudacityCommand.h:167
AudacityCommand::Init
virtual bool Init()
Definition: AudacityCommand.cpp:89
wxDialogWrapper
Definition: wxPanelWrapper.h:81
title
static const auto title
Definition: NoUpdatesAvailableDialog.cpp:22
eOkButton
@ eOkButton
Definition: ShuttleGui.h:600
AudacityCommand::SetAutomationParameters
virtual bool SetAutomationParameters(const wxString &parms)
Definition: AudacityCommand.cpp:157
AudacityCommand.h
AudacityCommand::GetVersion
wxString GetVersion() override
Definition: AudacityCommand.cpp:86
AudacityCommandDialog
Default dialog used for commands. Is populated using ShuttleGui.
Definition: AudacityCommand.h:143
BUILTIN_GENERIC_COMMAND_PREFIX
#define BUILTIN_GENERIC_COMMAND_PREFIX
Definition: AudacityCommand.h:30
AudacityCommandDialog::mAdditionalButtons
int mAdditionalButtons
Definition: AudacityCommand.h:166
ComponentInterfaceSymbol::Internal
const wxString & Internal() const
Definition: ComponentInterfaceSymbol.h:55
AudacityCommandDialog::OnHelp
virtual void OnHelp(wxCommandEvent &evt)
Definition: AudacityCommand.cpp:341
ShuttleSetAutomation::bOK
bool bOK
Definition: Shuttle.h:108
ShuttleGui::AddStandardButtons
void AddStandardButtons(long buttons=eOkButton|eCancelButton, wxWindow *extra=NULL)
Definition: ShuttleGui.cpp:2444
ShuttleGuiBase::SetBorder
void SetBorder(int Border)
Definition: ShuttleGui.h:489
ComponentInterface::GetName
TranslatableString GetName()
Definition: PluginManager.cpp:2083
Destroy_ptr
std::unique_ptr< T, Destroyer< T > > Destroy_ptr
a convenience for using Destroyer
Definition: MemoryX.h:290
safenew
#define safenew
Definition: MemoryX.h:10
ComponentInterface::DefineParams
virtual bool DefineParams(ShuttleParams &WXUNUSED(S))
Definition: ComponentInterface.h:85
AudacityCommand::mUIDebug
bool mUIDebug
Definition: AudacityCommand.h:136
END_EVENT_TABLE
END_EVENT_TABLE()
ShuttleSetAutomation::SetForWriting
void SetForWriting(CommandParameters *pEap)
Definition: Shuttle.h:114
AudacityCommand::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: AudacityCommand.cpp:227
AudacityCommandDialog::Validate
bool Validate() override
Definition: AudacityCommand.cpp:317
AudacityCommand::TransferDataFromWindow
virtual bool TransferDataFromWindow()
Definition: AudacityCommand.cpp:234
ShuttleGui
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:631