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/frame.h>
32#include <wx/log.h>
33#include <wx/stockitem.h>
34#include <wx/tglbtn.h>
35
36#include "ConfigInterface.h"
37
38#include "AudacityMessageBox.h"
39#include "HelpSystem.h"
40#include "ProgressDialog.h"
41#include "ProjectWindows.h"
42#include "ShuttleAutomation.h"
43#include "ShuttleGui.h"
44
45#include <unordered_map>
46
48{
49 mProgress = NULL;
50 mUIParent = NULL;
51 mUIDialog = NULL;
52 mIsBatch = false;
53 mNeedsInit = true;
54}
55
57{
58 if (mUIDialog)
59 mUIDialog->Close();
60}
61
62
64VendorSymbol AudacityCommand::GetVendor() const { return XO("Audacity");}
65wxString AudacityCommand::GetVersion() const { return AUDACITY_VERSION_STRING;}
66
67
69 if( !mNeedsInit )
70 return true;
71 mNeedsInit = false;
72 ShuttleDefaults DefaultSettingShuttle;
73 return VisitSettings( DefaultSettingShuttle );
74}
75
76bool AudacityCommand::ShowInterface(wxWindow *parent, bool WXUNUSED(forceModal))
77{
78 if (mUIDialog)
79 {
80 if ( mUIDialog->Close(true) )
81 mUIDialog = nullptr;
82 return false;
83 }
84
85 // mUIDialog is null
86 auto cleanup = valueRestorer( mUIDialog );
87
88 mUIDialog = CreateUI(parent, this);
89 if (!mUIDialog)
90 return false;
91
92 mUIDialog->Layout();
93 mUIDialog->Fit();
94 mUIDialog->SetMinSize(mUIDialog->GetSize());
95
96 bool res = mUIDialog->ShowModal() != 0;
97 return res;
98}
99
100wxDialog *AudacityCommand::CreateUI(wxWindow *parent, AudacityCommand * WXUNUSED(client))
101{
103 parent, GetName(), this}};
104
105 if (dlg->Init())
106 {
107 // release() is safe because parent will own it
108 return dlg.release();
109 }
110 return NULL;
111}
112
114{
116
118 {
119 return false;
120 }
121
123 S.mpEap = &eap;
124 bool bResult = VisitSettings( S );
125 wxASSERT_MSG( bResult, "You did not define DefineParameters() for this command" );
126 static_cast<void>(bResult); // fix unused variable warning in release mode
127
128 return eap.GetParameters(parms);
129}
130
131bool AudacityCommand::LoadSettingsFromString(const wxString & parms)
132{
133 wxString preset = parms;
134
135 CommandParameters eap(parms);
137
138 S.SetForWriting( &eap );
139 bool bResult = VisitSettings( S );
140 wxASSERT_MSG( bResult, "You did not define DefineParameters() for this command" );
141 static_cast<void>(bResult); // fix unused variable warning in release mode
142 if (!S.bOK)
143 {
145 XO(
146"%s: Could not load settings below. Default settings will be used.\n\n%s")
147 .Format( GetName(), preset ) );
148
149 // fror now always succeed, so that we can prompt the user.
150 return true;
151 }
152
153 return TransferDataToWindow();
154}
155
157 const CommandContext& context, bool shouldPrompt /* = true */)
158{
159 // Note: Init may read parameters from preferences
160 if (!Init())
161 {
162 return false;
163 }
164
165 // Prompting will be bypassed when applying a command that has already
166 // been configured, e.g. repeating the last effect on a different selection.
167 // Prompting may call AudacityCommand::Preview
168 if (shouldPrompt && /*IsInteractive() && */!PromptUser(context.project))
169 {
170 return false;
171 }
172
173 auto cleanup = finally( [&] {
174 End();
175 } );
176
177 bool returnVal = true;
178 bool skipFlag = CheckWhetherSkipAudacityCommand();
179 if (skipFlag == false)
180 {
181 auto name = GetName();
182 ProgressDialog progress{
183 name,
184 XO("Applying %s...").Format( name ),
186 };
187 auto vr = valueRestorer( mProgress, &progress );
188
189 returnVal = Apply(context);
190 }
191 return returnVal;
192}
193
194// This is used from Macros.
196{
198}
199
201{
202 if (mUIParent && !mUIParent->TransferDataToWindow())
203 return false;
204 return true;
205}
206
208{
209 if (mUIParent && (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow()))
210 return false;
211 return true;
212}
213
215{
216 return false;
217}
218
220{
221 return false;
222}
223
225 const TranslatableString& message, long style,
226 const TranslatableString &titleStr)
227{
228 auto title = titleStr.empty()
229 ? GetName()
230 : XO("%s: %s").Format( GetName(), titleStr );
231 return AudacityMessageBox(message, title, style, mUIParent);
232}
233
234BEGIN_EVENT_TABLE(AudacityCommandDialog, wxDialogWrapper)
239
242 AudacityCommand * pCommand,
243 int type,
244 int flags,
245 int additionalButtons)
246: wxDialogWrapper(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, flags)
247{
248 mType = type;
249 wxASSERT( pCommand );
250 mpCommand = pCommand;
251 mAdditionalButtons = additionalButtons |eCancelButton;
252 if( !pCommand->ManualPage().empty() )
253 mAdditionalButtons |= eHelpButton;
254}
255
257{
258 ShuttleGui S(this, eIsCreating);
259
260 S.SetBorder(5);
261 S.StartVerticalLay(true);
262 {
264
265 long buttons = eOkButton;
266 S.AddStandardButtons(buttons|mAdditionalButtons);
267 }
268 S.EndVerticalLay();
269
270 Layout();
271 Fit();
272 SetMinSize(GetSize());
273 Center();
274 return true;
275}
276
281{
282 wxASSERT( mpCommand );
284}
285
287{
290 return true;
291}
292
294{
297 return true;
298}
299
301{
302 return true;
303}
304
305void AudacityCommandDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
306{
307 // On wxGTK (wx2.8.12), the default action is still executed even if
308 // the button is disabled. This appears to affect all wxDialogs, not
309 // just our AudacityCommands dialogs. So, this is a only temporary workaround
310 // for legacy effects that disable the OK button. Hopefully this has
311 // been corrected in wx3.
312 if (FindWindow(wxID_OK)->IsEnabled() && Validate() && TransferDataFromWindow())
313 {
314 EndModal(true);
315 }
316}
317
318
319void AudacityCommandDialog::OnCancel(wxCommandEvent & WXUNUSED(evt))
320{
321 EndModal(false);
322}
323
324void AudacityCommandDialog::OnHelp(wxCommandEvent & WXUNUSED(event))
325{
326 if( mpCommand )
327 {
328 // otherwise use ShowHelp
329 HelpSystem::ShowHelp(FindWindow(wxID_HELP), mpCommand->ManualPage(), true);
330 }
331}
332
333
#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
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:253
#define safenew
Definition: MemoryX.h:10
std::unique_ptr< T, Destroyer< T > > Destroy_ptr
a convenience for using Destroyer
Definition: MemoryX.h:164
static const auto title
@ pdlgHideStopButton
AUDACITY_DLL_API wxFrame & GetProjectFrame(AudacityProject &project)
Get the top-level window associated with the project (as a wxFrame only, when you do not need to use ...
accessors for certain important windows associated with each project
ReverbSettings preset
Definition: ReverbBase.cpp:25
@ eIsSettingToDialog
Definition: ShuttleGui.h:39
@ eIsCreating
Definition: ShuttleGui.h:37
@ eIsGettingFromDialog
Definition: ShuttleGui.h:38
@ eOkButton
Definition: ShuttleGui.h:609
@ eCancelButton
Definition: ShuttleGui.h:610
@ eHelpButton
Definition: ShuttleGui.h:613
wxString name
Definition: TagsEditor.cpp:166
const auto project
#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 &)
bool DoAudacityCommand(const CommandContext &context, bool shouldPrompt=true)
virtual ~AudacityCommand()
virtual void PopulateOrExchange(ShuttleGui &WXUNUSED(S))
wxString GetVersion() const override
ComponentInterfaceSymbol GetSymbol() const override=0
wxWindow * mUIParent
virtual ManualPageID ManualPage()
bool ShowInterface(wxWindow *parent, bool forceModal=false)
virtual bool TransferDataFromWindow()
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 PromptUser(AudacityProject &)
virtual bool CheckWhetherSkipAudacityCommand()
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
AudacityProject & 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:640
SettingsVisitor that sets parameters to a value (from a string)
Holds a msgid for the translation catalog; may also bind format arguments.