Audacity 3.2.0
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#include "MemoryX.h"
25
26#include "CommandContext.h"
27
28#include <algorithm>
29
30#include <wx/defs.h>
31#include <wx/stockitem.h>
32#include <wx/tglbtn.h>
33#include <wx/log.h>
34
35#include "ConfigInterface.h"
36
37#include "ShuttleAutomation.h"
38#include "ShuttleGui.h"
39#include "ProgressDialog.h"
40#include "HelpSystem.h"
41#include "AudacityMessageBox.h"
42
43#include <unordered_map>
44
46{
47 mProgress = NULL;
48 mUIParent = NULL;
49 mUIDialog = NULL;
50 mIsBatch = false;
51 mNeedsInit = true;
52}
53
55{
56 if (mUIDialog)
57 mUIDialog->Close();
58}
59
60
62VendorSymbol AudacityCommand::GetVendor() const { return XO("Audacity");}
63wxString AudacityCommand::GetVersion() const { return AUDACITY_VERSION_STRING;}
64
65
67 if( !mNeedsInit )
68 return true;
69 mNeedsInit = false;
70 ShuttleDefaults DefaultSettingShuttle;
71 return VisitSettings( DefaultSettingShuttle );
72}
73
74bool AudacityCommand::ShowInterface(wxWindow *parent, bool WXUNUSED(forceModal))
75{
76 if (mUIDialog)
77 {
78 if ( mUIDialog->Close(true) )
79 mUIDialog = nullptr;
80 return false;
81 }
82
83 // mUIDialog is null
84 auto cleanup = valueRestorer( mUIDialog );
85
86 mUIDialog = CreateUI(parent, this);
87 if (!mUIDialog)
88 return false;
89
90 mUIDialog->Layout();
91 mUIDialog->Fit();
92 mUIDialog->SetMinSize(mUIDialog->GetSize());
93
94 bool res = mUIDialog->ShowModal() != 0;
95 return res;
96}
97
98wxDialog *AudacityCommand::CreateUI(wxWindow *parent, AudacityCommand * WXUNUSED(client))
99{
101 parent, GetName(), this}};
102
103 if (dlg->Init())
104 {
105 // release() is safe because parent will own it
106 return dlg.release();
107 }
108 return NULL;
109}
110
112{
114
116 {
117 return false;
118 }
119
121 S.mpEap = &eap;
122 bool bResult = VisitSettings( S );
123 wxASSERT_MSG( bResult, "You did not define DefineParameters() for this command" );
124 static_cast<void>(bResult); // fix unused variable warning in release mode
125
126 return eap.GetParameters(parms);
127}
128
129bool AudacityCommand::LoadSettingsFromString(const wxString & parms)
130{
131 wxString preset = parms;
132
133 CommandParameters eap(parms);
135
136 S.SetForWriting( &eap );
137 bool bResult = VisitSettings( S );
138 wxASSERT_MSG( bResult, "You did not define DefineParameters() for this command" );
139 static_cast<void>(bResult); // fix unused variable warning in release mode
140 if (!S.bOK)
141 {
143 XO(
144"%s: Could not load settings below. Default settings will be used.\n\n%s")
145 .Format( GetName(), preset ) );
146
147 // fror now always succeed, so that we can prompt the user.
148 return true;
149 }
150
151 return TransferDataToWindow();
152}
153
155 const CommandContext & context,
156 bool shouldPrompt /* = true */)
157{
158 // Note: Init may read parameters from preferences
159 if (!Init())
160 {
161 return false;
162 }
163
164 // Prompting will be bypassed when applying a command that has already
165 // been configured, e.g. repeating the last effect on a different selection.
166 // Prompting may call AudacityCommand::Preview
167 if (shouldPrompt && /*IsInteractive() && */!PromptUser(parent))
168 {
169 return false;
170 }
171
172 auto cleanup = finally( [&] {
173 End();
174 } );
175
176 bool returnVal = true;
177 bool skipFlag = CheckWhetherSkipAudacityCommand();
178 if (skipFlag == false)
179 {
180 auto name = GetName();
181 ProgressDialog progress{
182 name,
183 XO("Applying %s...").Format( name ),
185 };
186 auto vr = valueRestorer( mProgress, &progress );
187
188 returnVal = Apply(context);
189 }
190 return returnVal;
191}
192
193// This is used from Macros.
194bool AudacityCommand::PromptUser(wxWindow *parent)
195{
196 return ShowInterface(parent, IsBatchProcessing());
197}
198
200{
201 if (mUIParent && !mUIParent->TransferDataToWindow())
202 return false;
203 return true;
204}
205
207{
208 if (mUIParent && (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow()))
209 return false;
210 return true;
211}
212
214{
215 return false;
216}
217
219{
220 return false;
221}
222
224 const TranslatableString& message, long style,
225 const TranslatableString &titleStr)
226{
227 auto title = titleStr.empty()
228 ? GetName()
229 : XO("%s: %s").Format( GetName(), titleStr );
230 return AudacityMessageBox(message, title, style, mUIParent);
231}
232
233BEGIN_EVENT_TABLE(AudacityCommandDialog, wxDialogWrapper)
238
241 AudacityCommand * pCommand,
242 int type,
243 int flags,
244 int additionalButtons)
245: wxDialogWrapper(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, flags)
246{
247 mType = type;
248 wxASSERT( pCommand );
249 mpCommand = pCommand;
250 mAdditionalButtons = additionalButtons |eCancelButton;
251 if( !pCommand->ManualPage().empty() )
252 mAdditionalButtons |= eHelpButton;
253}
254
256{
257 ShuttleGui S(this, eIsCreating);
258
259 S.SetBorder(5);
260 S.StartVerticalLay(true);
261 {
263
264 long buttons = eOkButton;
265 S.AddStandardButtons(buttons|mAdditionalButtons);
266 }
267 S.EndVerticalLay();
268
269 Layout();
270 Fit();
271 SetMinSize(GetSize());
272 Center();
273 return true;
274}
275
280{
281 wxASSERT( mpCommand );
283}
284
286{
289 return true;
290}
291
293{
296 return true;
297}
298
300{
301 return true;
302}
303
304void AudacityCommandDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
305{
306 // On wxGTK (wx2.8.12), the default action is still executed even if
307 // the button is disabled. This appears to affect all wxDialogs, not
308 // just our AudacityCommands dialogs. So, this is a only temporary workaround
309 // for legacy effects that disable the OK button. Hopefully this has
310 // been corrected in wx3.
311 if (FindWindow(wxID_OK)->IsEnabled() && Validate() && TransferDataFromWindow())
312 {
313 EndModal(true);
314 }
315}
316
317
318void AudacityCommandDialog::OnCancel(wxCommandEvent & WXUNUSED(evt))
319{
320 EndModal(false);
321}
322
323void AudacityCommandDialog::OnHelp(wxCommandEvent & WXUNUSED(event))
324{
325 if( mpCommand )
326 {
327 // otherwise use ShowHelp
328 HelpSystem::ShowHelp(FindWindow(wxID_HELP), mpCommand->ManualPage(), true);
329 }
330}
331
332
#define BUILTIN_GENERIC_COMMAND_PREFIX
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
END_EVENT_TABLE()
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
ValueRestorer< T > valueRestorer(T &var)
inline functions provide convenient parameter type deduction
Definition: MemoryX.h:250
#define safenew
Definition: MemoryX.h:9
std::unique_ptr< T, Destroyer< T > > Destroy_ptr
a convenience for using Destroyer
Definition: MemoryX.h:161
static const auto title
@ pdlgHideStopButton
EffectReverbSettings preset
Definition: Reverb.cpp:44
@ eIsSettingToDialog
Definition: ShuttleGui.h:39
@ eIsCreating
Definition: ShuttleGui.h:37
@ eIsGettingFromDialog
Definition: ShuttleGui.h:38
@ eOkButton
Definition: ShuttleGui.h:599
@ eCancelButton
Definition: ShuttleGui.h:600
@ eHelpButton
Definition: ShuttleGui.h:603
#define S(N)
Definition: ToChars.cpp:64
Default dialog used for commands. Is populated using ShuttleGui.
virtual void OnCancel(wxCommandEvent &evt)
AudacityCommand * mpCommand
bool Validate() override
virtual void OnHelp(wxCommandEvent &evt)
virtual void OnOk(wxCommandEvent &evt)
virtual void PopulateOrExchange(ShuttleGui &S)
bool TransferDataFromWindow() override
bool TransferDataToWindow() override
Base class for command in Audacity.
virtual bool IsBatchProcessing() const
wxDialog * mUIDialog
virtual bool Init()
virtual bool Apply(const CommandContext &WXUNUSED(context))
bool LoadSettingsFromString(const wxString &parms)
VendorSymbol GetVendor() const override
bool SaveSettingsAsString(wxString &parms)
virtual bool VisitSettings(SettingsVisitor &)
virtual ~AudacityCommand()
virtual void PopulateOrExchange(ShuttleGui &WXUNUSED(S))
virtual bool PromptUser(wxWindow *parent)
wxString GetVersion() const override
ComponentInterfaceSymbol GetSymbol() const override=0
wxWindow * mUIParent
virtual ManualPageID ManualPage()
bool ShowInterface(wxWindow *parent, bool forceModal=false)
virtual bool TransferDataFromWindow()
bool DoAudacityCommand(wxWindow *parent, const CommandContext &context, bool shouldPrompt=true)
virtual void End()
ProgressDialog * mProgress
PluginPath GetPath() const override
virtual bool TransferDataToWindow()
wxDialog * CreateUI(wxWindow *parent, AudacityCommand *client)
int MessageBox(const TranslatableString &message, long style=DefaultMessageBoxStyle, const TranslatableString &titleStr={})
virtual bool CheckWhetherSkipAudacityCommand()
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the SettingsVis...
bool GetParameters(wxString &parms)
TranslatableString GetName() const
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
const wxString & Internal() const
Abstract base class used in importing a file.
static void ShowHelp(wxWindow *parent, const FilePath &localFileName, const URLString &remoteURL, bool bModal=false, bool alwaysDefaultBrowser=false)
Definition: HelpSystem.cpp:231
ProgressDialog Class.
Visitor of effect or command parameters. This is a base class with lots of virtual functions that do ...
SettingsVisitor that sets parameters to their default values.
SettingsVisitor that gets parameter values into a string.
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:630
SettingsVisitor that sets parameters to a value (from a string)
Holds a msgid for the translation catalog; may also bind format arguments.