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