Audacity  2.2.2
Shuttle.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Shuttle.cpp
4 
5  James Crook
6  (C) Audacity Developers, 2007
7 
8  wxWidgets license. See Licensing.txt
9 
10 *******************************************************************//****************************************************************//****************************************************************//****************************************************************//*******************************************************************/
52 
53 #include "Audacity.h"
54 
55 #include <wx/defs.h>
56 #include <wx/checkbox.h>
57 #include <wx/choice.h>
58 #include <wx/intl.h>
59 #include <wx/sizer.h>
60 #include <wx/statbox.h>
61 #include <wx/stattext.h>
62 #include <wx/textctrl.h>
63 #include <wx/listctrl.h>
64 #include <wx/radiobut.h>
65 #include <wx/button.h>
66 
67 #include "../include/audacity/EffectAutomationParameters.h" // for command automation
68 
69 //#include "Project.h"
70 #include "Shuttle.h"
71 #include "WrappedType.h"
72 //#include "commands/CommandManager.h"
73 //#include "effects/Effect.h"
74 
75 
77 {
78 }
79 
80 bool Shuttle::TransferBool( const wxString & Name, bool & bValue, const bool & bDefault )
81 {
82  if( mbStoreInClient )
83  {
84  bValue = bDefault;
85  if( ExchangeWithMaster( Name ))
86  {
87  if( !mValueString.IsEmpty() )
88  bValue = mValueString.GetChar(0) == wxT('y');
89  }
90  }
91  else
92  {
93  mValueString = (bValue==0) ? wxT("no"):wxT("yes");
94  return ExchangeWithMaster( Name );
95  }
96  return true;
97 }
98 
99 bool Shuttle::TransferFloat( const wxString & Name, float & fValue, const float &fDefault )
100 {
101  if( mbStoreInClient )
102  {
103  fValue = fDefault;
104  if( ExchangeWithMaster( Name ))
105  {
106  if( !mValueString.IsEmpty() )
107  fValue = wxAtof( mValueString );
108  }
109  }
110  else
111  {
112  mValueString = wxString::Format(wxT("%f"),fValue);
113  return ExchangeWithMaster( Name );
114  }
115  return true;
116 }
117 
118 bool Shuttle::TransferDouble( const wxString & Name, double & dValue, const double &dDefault )
119 {
120  if( mbStoreInClient )
121  {
122  dValue = dDefault;
123  if( ExchangeWithMaster( Name ))
124  {
125  if( !mValueString.IsEmpty() )
126  dValue = wxAtof( mValueString );
127  }
128  }
129  else
130  {
131  //%f is format string for double
132  mValueString = wxString::Format(wxT("%f"),dValue);
133  return ExchangeWithMaster( Name );
134  }
135  return true;
136 }
137 
138 bool Shuttle::TransferInt( const wxString & Name, int & iValue, const int & iDefault )
139 {
140  if( mbStoreInClient )
141  {
142  iValue = iDefault;
143  if( ExchangeWithMaster( Name ))
144  {
145  iValue = wxAtoi( mValueString );
146  }
147  }
148  else
149  {
150  mValueString = wxString::Format(wxT("%i"),iValue);
151  return ExchangeWithMaster( Name );
152  }
153  return true;
154 }
155 
156 
157 bool Shuttle::TransferInt( const wxString & Name, wxLongLong_t & iValue, const wxLongLong_t & iDefault )
158 {
159  return TransferLongLong(Name, iValue, iDefault);
160 }
161 
162 bool Shuttle::TransferLongLong( const wxString & Name, wxLongLong_t & iValue, const wxLongLong_t & iDefault )
163 {
164  if( mbStoreInClient )
165  {
166  iValue = iDefault;
167  if( ExchangeWithMaster( Name ))
168  {
169  iValue = wxAtoi( mValueString );
170  }
171  }
172  else
173  {
175  mValueString = wxString::Format(wxT("%lld"), (long long) iValue);
176  return ExchangeWithMaster( Name );
177  }
178  return true;
179 }
180 
181 
182 bool Shuttle::TransferEnum( const wxString & Name, int & iValue,
183  const int nChoices, const wxString * pFirstStr)
184 {
185  if( mbStoreInClient )
186  {
187  iValue = 0;// default index if none other selected.
188  if( ExchangeWithMaster( Name ))
189  {
190  wxString str = mValueString;
191  if( str.Left( 1 ) == wxT('"') && str.Right( 1 ) == wxT('"') )
192  {
193  str = str.Mid( 2, str.Length() - 2 );
194  }
195 
196  for( int i = 0; i < nChoices; i++ )
197  {
198  if( str.IsSameAs( pFirstStr[i] ))
199  {
200  iValue = i;
201  break;
202  }
203  }
204  }
205  }
206  else
207  {
208  //TIDY-ME: Out of range configuration values are silently discarded...
209  if( iValue > nChoices )
210  iValue = 0;
211  if( iValue < 0 )
212  iValue = 0;
213  mValueString = pFirstStr[iValue];
214  if( mValueString.Find( wxT(' ') ) != wxNOT_FOUND )
215  {
216  mValueString = wxT('"') + pFirstStr[iValue] + wxT('"'); //strings have quotes around them
217  }
218  return ExchangeWithMaster( Name );
219  }
220  return true;
221 }
222 
223 bool Shuttle::TransferString( const wxString & Name, wxString & strValue, const wxString & WXUNUSED(strDefault) )
224 {
225  if( mbStoreInClient )
226  {
227  if( ExchangeWithMaster( Name ))
228  {
229  strValue = mValueString;
230  }
231  else
232  return false;
233  }
234  else
235  {
236  mValueString = wxT('"') + strValue + wxT('"'); //strings have quotes around them
237  return ExchangeWithMaster( Name );
238  }
239  return true;
240 }
241 
242 bool Shuttle::TransferWrappedType( const wxString & Name, WrappedType & W )
243 {
244  if( mbStoreInClient )
245  {
246  if( ExchangeWithMaster( Name ))
247  {
249  }
250  }
251  else
252  {
254  return ExchangeWithMaster( Name );
255  }
256  return true;
257 }
258 
259 
260 bool Shuttle::ExchangeWithMaster(const wxString & WXUNUSED(Name))
261 {
262  // ExchangeWithMaster() will usually be over-ridden
263  // in derived classes. We could have made it an
264  // abstract function.
265  wxASSERT( false );
266  return true;
267 }
268 
269 // This variant uses values of the form
270 // param1=value1 param2=value2
271 bool ShuttleCli::ExchangeWithMaster(const wxString & Name)
272 {
273  if( !mbStoreInClient )
274  {
275  mParams += wxT(" ");
276  mParams +=Name;
277  mParams += wxT("=");
279  }
280  else
281  {
282  int i;
283  mParams = wxT(" ")+mParams;
284  i=mParams.Find( wxT(" ")+Name+wxT("=") );
285  if( i>=0 )
286  {
287  int j=i+2+Name.Length();
288  wxString terminator = wxT(' ');
289  if(mParams.GetChar(j) == wxT('"')) //Strings are surrounded by quotes
290  {
291  terminator = wxT('"');
292  j++;
293  }
294  else if(mParams.GetChar(j) == wxT('\'')) // or by single quotes.
295  {
296  terminator = wxT('\'');
297  j++;
298  }
299  i=j;
300  while( j<(int)mParams.Length() && mParams.GetChar(j) != terminator )
301  j++;
302  mValueString = mParams.Mid(i,j-i);
303  return true;
304  }
305  return false;
306  }
307  return true;
308 }
309 
310 
311 bool ShuttleParams::ExchangeWithMaster(const wxString & WXUNUSED(Name))
312 {
313  return true;
314 }
315 
316 #ifdef _MSC_VER
317 // If this is compiled with MSVC (Visual Studio)
318 #pragma warning( push )
319 #pragma warning( disable: 4100 ) // unused parameters.
320 #endif //_MSC_VER
321 
322 
323 // The ShouldSet and CouldGet functions have an important side effect
324 // on the pOptionalFlag. They 'use it up' and clear it down for the next parameter.
325 
326 
327 // Tests for parameter being optional.
328 // Prepares for next parameter by clearing the pointer.
329 // Reports on whether the parameter should be set, i.e. should set
330 // if it was chosen to be set, or was not optional.
332  if( !pOptionalFlag )
333  return true;
334  bool result = *pOptionalFlag;
335  pOptionalFlag = NULL;
336  return result;
337 }
338 // These are functions to override. They do nothing.
339 void ShuttleParams::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl){;};
340 void ShuttleParams::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl ){;};
341 void ShuttleParams::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl ){;};
342 void ShuttleParams::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl ){;};
343 void ShuttleParams::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl ){;};
344 void ShuttleParams::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl ){;};
345 void ShuttleParams::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl ){;};
346 void ShuttleParams::DefineEnum( int &var, const wxChar * key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings ){;};
347 
348 
349 
350 /*
351 void ShuttleParams::DefineEnum( int &var, const wxChar * key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings )
352 {
353 }
354 */
355 
356 // ShuttleGetAutomation gets from the shuttle into typically a string.
358  pOptionalFlag = &var;
359  return *this;
360 };
361 
362 void ShuttleGetAutomation::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl )
363 {
364  if( !ShouldSet() ) return;
365  mpEap->Write(key, var);
366 }
367 
368 void ShuttleGetAutomation::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
369 {
370  if( !ShouldSet() ) return;
371  mpEap->Write(key, var);
372 }
373 
374 void ShuttleGetAutomation::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
375 {
376  if( !ShouldSet() ) return;
377  mpEap->Write(key, var);
378 }
379 
380 void ShuttleGetAutomation::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
381 {
382  if( !ShouldSet() ) return;
383  mpEap->WriteFloat(key, var);
384 }
385 
386 void ShuttleGetAutomation::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
387 {
388  if( !ShouldSet() ) return;
389  mpEap->WriteFloat(key, var);
390 }
391 
392 void ShuttleGetAutomation::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl )
393 {
394  if( !ShouldSet() ) return;
395  mpEap->Write(key, var);
396 }
397 
398 
399 void ShuttleGetAutomation::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl )
400 {
401  if( !ShouldSet() ) return;
402  mpEap->Write(key, var);
403 }
404 
405 
406 void ShuttleGetAutomation::DefineEnum( int &var, const wxChar * key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings )
407 {
408  if( !ShouldSet() ) return;
409  mpEap->Write(key, strings[var].Internal());
410 }
411 
412 
413 
415  pOptionalFlag = &var;
416  return *this;
417 };
418 
419 // Tests for parameter being optional.
420 // Prepares for next parameter by clearing the pointer.
421 // If the parameter is optional, finds out if it was actually provided.
422 // i.e. could it be got from automation?
423 // The result goes into the flag variable, so we typically ignore the result.
424 bool ShuttleSetAutomation::CouldGet( const wxString &key ){
425  // Not optional? Can get as we will get the default, at worst.
426  if( !pOptionalFlag )
427  return true;
428  bool result = mpEap->HasEntry( key );
429  *pOptionalFlag = result;
430  pOptionalFlag = NULL;
431  return result;
432 }
433 
434 void ShuttleSetAutomation::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl )
435 {
436  CouldGet( key );
437  if( !bOK )
438  return;
439  // Use of temp in this and related functions is to handle the case of
440  // only committing values if all values pass verification.
441  bool temp =var;
442  bOK = mpEap->ReadAndVerify(key, &temp, vdefault);
443  if( bWrite && bOK)
444  var = temp;
445 }
446 
447 void ShuttleSetAutomation::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
448 {
449  CouldGet( key );
450  if( !bOK )
451  return;
452  int temp =var;
453  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
454  if( bWrite && bOK)
455  var = temp;
456 }
457 
458 void ShuttleSetAutomation::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
459 {
460  CouldGet( key );
461  if( !bOK )
462  return;
463  int temp = var;
464  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
465  if( bWrite && bOK )
466  var = temp;
467 }
468 
469 void ShuttleSetAutomation::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
470 {
471  CouldGet( key );
472  if( !bOK )
473  return;
474  float temp = var;
475  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
476  if( bWrite && bOK )
477  var = temp;
478 }
479 
480 
481 void ShuttleSetAutomation::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
482 {
483  CouldGet( key );
484  if( !bOK )
485  return;
486  double temp = var;
487  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
488  if( bWrite && bOK)
489  var = temp;
490 }
491 
492 void ShuttleSetAutomation::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl )
493 {
494  CouldGet( key );
495  if( !bOK )
496  return;
497  double temp = var;
498  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
499  if( bWrite && bOK)
500  var = temp;
501 }
502 
503 
504 void ShuttleSetAutomation::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl )
505 {
506  CouldGet( key );
507  if( !bOK )
508  return;
509  wxString temp = var;
510  bOK = mpEap->ReadAndVerify(key, &temp, vdefault);
511  if( bWrite && bOK )
512  var = temp;
513 }
514 
515 
516 void ShuttleSetAutomation::DefineEnum( int &var, const wxChar * key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings )
517 {
518  CouldGet( key );
519  if( !bOK )
520  return;
521  int temp = var;
522  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, strings, nStrings);
523  if( bWrite && bOK)
524  var = temp;
525 }
526 
528  bool result = pOptionalFlag !=NULL;
529  pOptionalFlag = NULL;
530  return result;
531 }
532 
533 // Definition distinguishes optional from not.
535  pOptionalFlag = &var;
536  return *this;
537 };
538 
540 {
541 }
542 
543 // JSON definitions.
544 void ShuttleGetDefinition::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl )
545 {
546  StartStruct();
547  AddItem( wxString(key), "key" );
548  AddItem( "bool", "type" );
549  if( IsOptional() )
550  AddItem( "unchanged", "default" );
551  else
552  AddItem( vdefault ? "True" : "False", "default" );
553  EndStruct();
554 }
555 
556 void ShuttleGetDefinition::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
557 {
558  StartStruct();
559  AddItem( wxString(key), "key" );
560  AddItem( "int", "type" );
561  if( IsOptional() )
562  AddItem( "unchanged", "default" );
563  else
564  AddItem( (double)vdefault, "default" );
565  EndStruct();
566 }
567 
568 void ShuttleGetDefinition::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
569 {
570  StartStruct();
571  AddItem( wxString(key), "key" );
572  AddItem( "size_t", "type" );
573  if( IsOptional() )
574  AddItem( "unchanged", "default" );
575  else
576  AddItem( (double)vdefault, "default" );
577  EndStruct();
578 
579 }
580 
581 void ShuttleGetDefinition::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
582 {
583  StartStruct();
584  AddItem( wxString(key), "key" );
585  AddItem( "float", "type" );
586  if( IsOptional() )
587  AddItem( "unchanged", "default" );
588  else
589  AddItem( (double)vdefault, "default" );
590  EndStruct();
591 }
592 
593 void ShuttleGetDefinition::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
594 {
595  StartStruct();
596  AddItem( wxString(key), "key" );
597  AddItem( "float", "type" );
598  if( IsOptional() )
599  AddItem( "unchanged", "default" );
600  else
601  AddItem( (double)vdefault, "default" );
602  EndStruct();
603 }
604 
605 void ShuttleGetDefinition::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl )
606 {
607  StartStruct();
608  AddItem( wxString(key), "key" );
609  AddItem( "double", "type" );
610  if( IsOptional() )
611  AddItem( "unchanged", "default" );
612  else
613  AddItem( (double)vdefault, "default" );
614  EndStruct();
615 }
616 
617 
618 void ShuttleGetDefinition::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl )
619 {
620  StartStruct();
621  AddItem( wxString(key), "key" );
622  AddItem( "string", "type" );
623  if( IsOptional() )
624  AddItem( "unchanged", "default" );
625  else
626  AddItem( vdefault, "default" );
627  EndStruct();
628 }
629 
630 
632  const wxChar * key, const int vdefault,
633  const IdentInterfaceSymbol strings[], size_t nStrings )
634 {
635  StartStruct();
636  AddItem( wxString(key), "key" );
637  AddItem( "enum", "type" );
638  if( IsOptional() )
639  AddItem( "unchanged", "default" );
640  else
641  AddItem( strings[vdefault].Internal(), "default" );
642  StartField( "enum" );
643  StartArray();
644  for( size_t i = 0; i < nStrings; i++ )
645  AddItem( strings[i].Internal() );
646  EndArray();
647  EndField();
648  EndStruct();
649 }
650 
651 #ifdef _MSC_VER
652 // If this is compiled with MSVC (Visual Studio)
653 #pragma warning( pop )
654 #endif //_MSC_VER
655 
656 
657 
658 
659 
void DefineEnum(int &var, const wxChar *key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings) override
Definition: Shuttle.cpp:516
virtual bool TransferEnum(const wxString &Name, int &iValue, const int nChoices, const wxString *pFirstStr)
Definition: Shuttle.cpp:182
void DefineEnum(int &var, const wxChar *key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings) override
Definition: Shuttle.cpp:406
void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl) override
Definition: Shuttle.cpp:434
virtual bool TransferInt(const wxString &Name, int &iValue, const int &iDefault)
Definition: Shuttle.cpp:138
CommandMessageTargetDecorator is a CommandOutputTarget that forwards its work on to another one...
wxString mParams
Definition: Shuttle.h:49
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:60
virtual bool ExchangeWithMaster(const wxString &Name)
Definition: Shuttle.cpp:260
wxString ReadAsString()
bool WriteFloat(const wxString &key, float f)
bool CouldGet(const wxString &key)
Definition: Shuttle.cpp:424
virtual void DefineEnum(int &var, const wxChar *key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings)
Definition: Shuttle.cpp:346
virtual bool TransferBool(const wxString &Name, bool &bValue, const bool &bDefault)
Definition: Shuttle.cpp:80
virtual bool TransferString(const wxString &Name, wxString &strValue, const wxString &strDefault)
Definition: Shuttle.cpp:223
void WriteToAsString(const wxString &InStr)
void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl) override
Definition: Shuttle.cpp:544
ShuttleParams & Optional(bool &var) override
Definition: Shuttle.cpp:357
CommandParameters * mpEap
Definition: Shuttle.h:65
virtual void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin=false, const bool vmax=false, const bool vscl=false)
Definition: Shuttle.cpp:339
bool ShouldSet()
Definition: Shuttle.cpp:331
virtual bool TransferFloat(const wxString &Name, float &fValue, const float &fDefault)
Definition: Shuttle.cpp:99
bool * pOptionalFlag
Definition: Shuttle.h:64
bool ExchangeWithMaster(const wxString &Name) override
Definition: Shuttle.cpp:311
ShuttleParams & Optional(bool &var) override
Definition: Shuttle.cpp:414
virtual bool TransferWrappedType(const wxString &Name, WrappedType &W)
Definition: Shuttle.cpp:242
Interface for objects that can receive (string) messages from a command.
void StartField(const wxString &name="") override
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
ShuttleParams & Optional(bool &var) override
Definition: Shuttle.cpp:534
void AddItem(const wxString &value, const wxString &name="") override
bool mbStoreInClient
Definition: Shuttle.h:27
virtual bool TransferLongLong(const wxString &Name, wxLongLong_t &iValue, const wxLongLong_t &iDefault)
Definition: Shuttle.cpp:162
virtual bool TransferDouble(const wxString &Name, double &dValue, const double &dDefault)
Definition: Shuttle.cpp:118
void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl) override
Definition: Shuttle.cpp:362
bool ReadAndVerify(const wxString &key, float *val, float defVal, float min, float max) const
wxString mValueString
Definition: Shuttle.h:28
bool ExchangeWithMaster(const wxString &Name) override
Definition: Shuttle.cpp:271
void DefineEnum(int &var, const wxChar *key, const int vdefault, const IdentInterfaceSymbol strings[], size_t nStrings) override
Definition: Shuttle.cpp:631
ShuttleGetDefinition(CommandMessageTarget &target)
Definition: Shuttle.cpp:539
virtual bool HasEntry(const wxString &strName) const override
Used in type conversions, this wrapper for ints, strings, doubles and enums provides conversions betw...
Definition: WrappedType.h:31
Shuttle()
Definition: Shuttle.cpp:76