Audacity 3.2.0
CommandTargets.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity - A Digital Audio Editor
4 Copyright 1999-2009 Audacity Team
5 License: wxwidgets
6
7 Dan Horgan
8
9******************************************************************//*******************************************************************/
23
24
25#include "CommandTargets.h"
26
27#include <wx/app.h>
28#include <wx/statusbr.h>
29#include <wx/textctrl.h>
30#include "../ShuttleGui.h"
31#include "../widgets/AudacityMessageBox.h"
32#include "../widgets/wxPanelWrapper.h"
33
34#include <locale>
35#include <sstream>
36
38{
39 wxString Padding;
40 Padding.Pad( mCounts.size() *2 -2);
41 Update( wxString::Format( "%s%s[ ", ( mCounts.back() > 0 ) ? ",\n" : "\n", Padding ));
42 mCounts.back() += 1;
43 mCounts.push_back( 0 );
44}
45
47 if( mCounts.size() > 1 ){
48 mCounts.pop_back();
49 }
50 Update( " ]" );
51}
53 wxString Padding;
54 Padding.Pad( mCounts.size() *2 -2);
55 Update( wxString::Format( "%s%s{ ", ( mCounts.back() > 0 ) ? ",\n" : "\n", Padding ));
56 mCounts.back() += 1;
57 mCounts.push_back( 0 );
58}
60 if( mCounts.size() > 1 ){
61 mCounts.pop_back();
62 }
63 Update( " }" );
64}
65void CommandMessageTarget::AddItem(const wxString &value, const wxString &name){
66 wxString Padding;
67 Padding.Pad( mCounts.size() *2 -2);
68 Padding = (( value.length() < 15 ) || (mCounts.back()<=0)) ? wxString{} : wxString("\n") + Padding;
69 if( name.empty() )
70 Update( wxString::Format( "%s%s\"%s\"", (mCounts.back()>0)?", ":"", Padding, Escaped(value)));
71 else
72 Update( wxString::Format( "%s%s\"%s\":\"%s\"", (mCounts.back()>0)?", ":"", Padding, name, Escaped(value)));
73 mCounts.back() += 1;
74}
75
76void CommandMessageTarget::AddBool(const bool value, const wxString &name){
77 if( name.empty() )
78 Update( wxString::Format( "%s\"%s\"", (mCounts.back()>0)?", ":"", value?"true":"false"));
79 else
80 Update( wxString::Format( "%s\"%s\":\"%s\"", (mCounts.back()>0)?", ":"", name,value?"true":"false"));
81 mCounts.back() += 1;
82}
83
84void CommandMessageTarget::AddItem(const double value, const wxString &name){
85 std::stringstream str;
86 std::locale nolocale("C");
87 str.imbue(nolocale);
88
89 if( name.empty() )
90 str << ((mCounts.back()>0)? ", " : "") << value;
91 else
92 str << ((mCounts.back()>0)? ", " : "") << "\"" << name << "\"" << ":" << value;
93
94 Update( str.str() );
95 mCounts.back() += 1;
96}
97
99 if( name.empty() )
100 Update( wxString::Format( "%s", (mCounts.back()>0)? ", " : ""));
101 else
102 Update( wxString::Format( "%s\"%s\":", (mCounts.back()>0) ?", ":"", name));
103 mCounts.back() += 1;
104 mCounts.push_back( 0 );
105}
106
108 if( mCounts.size() > 1 ){
109 mCounts.pop_back();
110 }
111}
112
114}
115
116wxString CommandMessageTarget::Escaped( const wxString & str){
117 wxString Temp = str;
118 Temp.Replace( "\"", "\\\"");
119 return Temp;
120}
121
122
123
125{
126 wxString Padding;
127 Padding.Pad( mCounts.size() *2 -2);
128 Update( wxString::Format( (mCounts.back()>0)?"\n%s(":"(", Padding ));
129 mCounts.back() += 1;
130 mCounts.push_back( 0 );
131}
132
134 if( mCounts.size() > 1 ){
135 mCounts.pop_back();
136 }
137 Update( ")" );
138}
140 wxString Padding;
141 Padding.Pad( mCounts.size() *2 -2);
142 Update( wxString::Format( (mCounts.back()>0)?"\n%s(":"(", Padding ));
143 mCounts.back() += 1;
144 mCounts.push_back( 0 );
145}
147 if( mCounts.size() > 1 ){
148 mCounts.pop_back();
149 }
150 Update( ")" );
151}
152void LispyCommandMessageTarget::AddItem(const wxString &value, const wxString &name){
153 wxString Padding;
154 if( name.empty() )
155 Update( wxString::Format( "%s%s\"%s\"", (mCounts.back()>0)?" ":"", Padding, Escaped(value)));
156 else
157 Update( wxString::Format( "%s%s(%s \"%s\")", (mCounts.back()>0)?" ":"", Padding, name, Escaped(value)));
158 mCounts.back() += 1;
159}
160void LispyCommandMessageTarget::AddBool(const bool value, const wxString &name){
161 if( name.empty() )
162 Update( wxString::Format( "%s%s", (mCounts.back()>0)?" ":"",value?"True":"False"));
163 else
164 Update( wxString::Format( "%s(%s %s)", (mCounts.back()>0)?" ":"", name,value?"True":"False"));
165 mCounts.back() += 1;
166}
167void LispyCommandMessageTarget::AddItem(const double value, const wxString &name){
168 if( name.empty() )
169 Update( wxString::Format( "%s%g", (mCounts.back()>0)?" ":"", value));
170 else
171 Update( wxString::Format( "%s(%s %g)", (mCounts.back()>0)?" ":"", name,value));
172 mCounts.back() += 1;
173}
174
176 Update( wxString::Format( "%s(%s", (mCounts.back()>0)?" ":"", name ));
177 mCounts.back() += 1;
178 mCounts.push_back( 0 );
179}
180
182 if( mCounts.size() > 1 ){
183 mCounts.pop_back();
184 }
185 Update( ")" );
186}
187
188
189
190
191
192
194{
195 wxString Padding;
196 Padding.Pad( mCounts.size() *2 -2);
197 if( mCounts.size() <= 3 )
198 Update( wxString::Format( "%s%s ", ( mCounts.back() > 0 ) ? " \n" : "", Padding ));
199 mCounts.back() += 1;
200 mCounts.push_back( 0 );
201}
202
204 if( mCounts.size() > 1 ){
205 mCounts.pop_back();
206 }
207 if( mCounts.size() <= 3 )
208 Update( " " );
209}
211 wxString Padding;
212 Padding.Pad( mCounts.size() *2 -2);
213 if( mCounts.size() <= 3 )
214 Update( wxString::Format( "%s%s ", ( mCounts.back() > 0 ) ? " \n" : "", Padding ));
215 mCounts.back() += 1;
216 mCounts.push_back( 0 );
217}
219 if( mCounts.size() > 1 ){
220 mCounts.pop_back();
221 }
222 if( mCounts.size() <= 3 )
223 Update( " " );
224}
225void BriefCommandMessageTarget::AddItem(const wxString &value, const wxString &WXUNUSED(name)){
226 if( mCounts.size() <= 3 )
227 Update( wxString::Format( "%s\"%s\"", (mCounts.back()>0)?" ":"",Escaped(value)));
228 mCounts.back() += 1;
229}
230void BriefCommandMessageTarget::AddBool(const bool value, const wxString &WXUNUSED(name)){
231 if( mCounts.size() <= 3 )
232 Update( wxString::Format( "%s%s", (mCounts.back()>0)?" ":"",value?"True":"False"));
233 mCounts.back() += 1;
234}
235void BriefCommandMessageTarget::AddItem(const double value, const wxString &WXUNUSED(name)){
236 if( mCounts.size() <= 3 )
237 Update( wxString::Format( "%s%g", (mCounts.back()>0)?" ":"", value));
238 mCounts.back() += 1;
239}
240
241void BriefCommandMessageTarget::StartField(const wxString &WXUNUSED(name)){
242 mCounts.back() += 1;
243 mCounts.push_back( 0 );
244}
245
247 if( mCounts.size() > 1 ){
248 mCounts.pop_back();
249 }
250}
251
252
253void MessageBoxTarget::Update(const wxString &message)
254{
255 // Should these messages be localized?
256 AudacityMessageBox( Verbatim( message ) );
257}
258
259
262 pToRestore( &target )
263{
264 mProgressTarget = std::move(target.mProgressTarget),
265 mStatusTarget = std::make_shared<LispyCommandMessageTarget>( *target.mStatusTarget.get() ),
266 mErrorTarget = std::move( target.mErrorTarget );
267}
268
270{
272 //The status was never captured so does not need restoring.
273 //pToRestore->mStatusTarget = std::move( mStatusTarget );
274 pToRestore->mErrorTarget = std::move( mErrorTarget );
275}
276
279 pToRestore( &target )
280{
281 mProgressTarget = std::move(target.mProgressTarget),
282 mStatusTarget = std::make_shared<BriefCommandMessageTarget>( *target.mStatusTarget.get() ),
283 mErrorTarget = std::move( target.mErrorTarget );
284}
285
287{
289 //The status was never captured so does not need restoring.
290 //pToRestore->mStatusTarget = std::move( mStatusTarget );
291 pToRestore->mErrorTarget = std::move( mErrorTarget );
292}
293
294
295
296
297
298
299
300
301
302
303
304class AUDACITY_DLL_API LongMessageDialog /* not final */ : public wxDialogWrapper
305{
306public:
307 // constructors and destructors
308 LongMessageDialog(wxWindow * parent,
310 int type = 0,
311 int flags = wxDEFAULT_DIALOG_STYLE,
312 int additionalButtons = 0);
314
315 bool Init();
316 virtual void OnOk(wxCommandEvent & evt);
317 virtual void OnCancel(wxCommandEvent & evt);
318
319 static void AcceptText( const wxString & Text );
320 static void Flush();
321
322 wxTextCtrl * mTextCtrl;
323 wxString mText;
325private:
326 int mType;
328
329 DECLARE_EVENT_TABLE()
330 wxDECLARE_NO_COPY_CLASS(LongMessageDialog);
331};
332
333
335
336
337BEGIN_EVENT_TABLE(LongMessageDialog, wxDialogWrapper)
338 EVT_BUTTON(wxID_OK, LongMessageDialog::OnOk)
340
343 int type,
344 int flags,
345 int additionalButtons)
346: wxDialogWrapper(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, flags | wxRESIZE_BORDER)
347{
348 mType = type;
349 mAdditionalButtons = additionalButtons;
350 SetName(XO("Long Message"));
351 // The long message adds lots of short strings onto this one.
352 // So preallocate to make it faster.
353 // Needs 37Kb for all commands.
354 mText.Alloc(40000);
355}
356
358 pDlg = NULL;
359}
360
361
363{
364 ShuttleGui S(this, eIsCreating);
365
366 S.SetBorder(5);
367 S.StartVerticalLay(true);
368 {
369 mTextCtrl = S.AddTextWindow( "" );
370 long buttons = eOkButton;
371 S.AddStandardButtons(buttons|mAdditionalButtons);
372 }
373 S.EndVerticalLay();
374
375 Layout();
376 Fit();
377 SetMinSize(wxSize(600,350));
378 Center();
379 return true;
380}
381
382void LongMessageDialog::OnOk(wxCommandEvent & WXUNUSED(evt)){
383 //Close(true);
384 Destroy();
385}
386
387void LongMessageDialog::OnCancel(wxCommandEvent & WXUNUSED(evt)){
388 //Close(true);
389 Destroy();
390}
391
392void LongMessageDialog::AcceptText( const wxString & Text )
393{
394 if( pDlg == NULL ){
395 pDlg = new LongMessageDialog(
396 wxTheApp->GetTopWindow(), XO( "Long Message" ) );
397 pDlg->Init();
398 pDlg->Show();
399 }
400 pDlg->mText = pDlg->mText + Text;
401}
402
404{
405 if( pDlg ){
406 if( !pDlg->mText.EndsWith( "\n\n" ))
407 {
408 pDlg->mText += "\n\n";
409 pDlg->mTextCtrl->SetValue( pDlg->mText );
410 pDlg->mTextCtrl->ShowPosition( pDlg->mTextCtrl->GetLastPosition() );
411 }
412 }
413}
414
415
416
417
418
419
425{
426public:
427 virtual ~MessageDialogTarget() {Flush();}
428 void Update(const wxString &message) override
429 {
431 }
432 void Flush() override
433 {
435 }
436};
437
438
439
442{
443public:
444 static std::shared_ptr<CommandMessageTarget> LongMessages()
445 {
446 return std::make_shared<MessageDialogTarget>();
447 }
448};
449
450
451
454 ExtTargetFactory::ProgressDefault(),
455 ExtTargetFactory::LongMessages(),
456 ExtTargetFactory::MessageDefault()
457 )
458{
459}
460
461void StatusBarTarget::Update(const wxString &message)
462{
463 mStatus.SetStatusText(message, 0);
464}
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
END_EVENT_TABLE()
#define str(a)
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
const TranslatableString name
Definition: Distortion.cpp:74
XO("Cut/Copy/Paste")
static const auto title
@ eIsCreating
Definition: ShuttleGui.h:39
@ eOkButton
Definition: ShuttleGui.h:597
#define S(N)
Definition: ToChars.cpp:64
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.
virtual void StartStruct() override
virtual void EndArray() override
virtual void EndStruct() override
virtual void AddBool(const bool value, const wxString &name={}) override
virtual void StartField(const wxString &name={}) override
virtual void EndField() override
virtual void AddItem(const wxString &value, const wxString &name={}) override
virtual void StartArray() override
CommandOutputTargets * pToRestore
BriefCommandOutputTargets(CommandOutputTargets &target)
void Update(const wxString &message) override
Interface for objects that can receive (string) messages from a command.
virtual void AddBool(const bool value, const wxString &name={})
std::vector< int > mCounts
virtual void AddItem(const wxString &value, const wxString &name={})
virtual void Update(const wxString &message)=0
virtual void StartField(const wxString &name={})
virtual void EndArray()
virtual void EndField()
virtual void StartStruct()
wxString Escaped(const wxString &str)
virtual void EndStruct()
virtual void StartArray()
CommandOutputTargets a mix of three output classes to output progress indication, status messages and...
std::shared_ptr< CommandMessageTarget > mStatusTarget
std::unique_ptr< CommandProgressTarget > mProgressTarget
std::shared_ptr< CommandMessageTarget > mErrorTarget
Extended Target Factory with more options.
static std::shared_ptr< CommandMessageTarget > LongMessages()
CommandOutputTargets * pToRestore
LispifiedCommandOutputTargets(CommandOutputTargets &target)
virtual void StartStruct() override
virtual void StartField(const wxString &name={}) override
virtual void EndArray() override
virtual void AddBool(const bool value, const wxString &name={}) override
virtual void EndField() override
virtual void EndStruct() override
virtual void StartArray() override
virtual void AddItem(const wxString &value, const wxString &name={}) override
LongMessageDialog is a dialog with a Text Window in it to capture the more lengthy output from some c...
static LongMessageDialog * pDlg
wxTextCtrl * mTextCtrl
static void AcceptText(const wxString &Text)
static void Flush()
virtual void OnOk(wxCommandEvent &evt)
virtual void OnCancel(wxCommandEvent &evt)
void Update(const wxString &message) override
MessageDialogTarget is a CommandOutputTarget that sends its status to the LongMessageDialog.
void Flush() override
void Update(const wxString &message) override
virtual ~MessageDialogTarget()
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:628
wxStatusBar & mStatus
void Update(const wxString &message) override
TargetFactory makes Command output targets. By default, we ignore progress updates but display all ot...
Holds a msgid for the translation catalog; may also bind format arguments.