Audacity  3.0.3
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 
54 #include "Shuttle.h"
55 
56 #include <wx/defs.h>
57 #include <wx/checkbox.h>
58 #include <wx/choice.h>
59 #include <wx/intl.h>
60 #include <wx/sizer.h>
61 #include <wx/statbox.h>
62 #include <wx/stattext.h>
63 #include <wx/textctrl.h>
64 #include <wx/listctrl.h>
65 #include <wx/radiobut.h>
66 #include <wx/button.h>
67 
68 #include "EffectAutomationParameters.h" // for command automation
69 
70 #include "WrappedType.h"
71 //#include "effects/Effect.h"
72 
73 
75 {
76 }
77 
78 bool Shuttle::TransferBool( const wxString & Name, bool & bValue, const bool & bDefault )
79 {
80  if( mbStoreInClient )
81  {
82  bValue = bDefault;
83  if( ExchangeWithMaster( Name ))
84  {
85  if( !mValueString.empty() )
86  bValue = mValueString.GetChar(0) == wxT('y');
87  }
88  }
89  else
90  {
91  mValueString = (bValue==0) ? wxT("no"):wxT("yes");
92  return ExchangeWithMaster( Name );
93  }
94  return true;
95 }
96 
97 bool Shuttle::TransferFloat( const wxString & Name, float & fValue, const float &fDefault )
98 {
99  if( mbStoreInClient )
100  {
101  fValue = fDefault;
102  if( ExchangeWithMaster( Name ))
103  {
104  if( !mValueString.empty() )
105  fValue = wxAtof( mValueString );
106  }
107  }
108  else
109  {
110  mValueString = wxString::Format(wxT("%f"),fValue);
111  return ExchangeWithMaster( Name );
112  }
113  return true;
114 }
115 
116 bool Shuttle::TransferDouble( const wxString & Name, double & dValue, const double &dDefault )
117 {
118  if( mbStoreInClient )
119  {
120  dValue = dDefault;
121  if( ExchangeWithMaster( Name ))
122  {
123  if( !mValueString.empty() )
124  dValue = wxAtof( mValueString );
125  }
126  }
127  else
128  {
129  //%f is format string for double
130  mValueString = wxString::Format(wxT("%f"),dValue);
131  return ExchangeWithMaster( Name );
132  }
133  return true;
134 }
135 
136 bool Shuttle::TransferInt( const wxString & Name, int & iValue, const int & iDefault )
137 {
138  if( mbStoreInClient )
139  {
140  iValue = iDefault;
141  if( ExchangeWithMaster( Name ))
142  {
143  iValue = wxAtoi( mValueString );
144  }
145  }
146  else
147  {
148  mValueString = wxString::Format(wxT("%i"),iValue);
149  return ExchangeWithMaster( Name );
150  }
151  return true;
152 }
153 
154 
155 bool Shuttle::TransferInt( const wxString & Name, wxLongLong_t & iValue, const wxLongLong_t & iDefault )
156 {
157  return TransferLongLong(Name, iValue, iDefault);
158 }
159 
160 bool Shuttle::TransferLongLong( const wxString & Name, wxLongLong_t & iValue, const wxLongLong_t & iDefault )
161 {
162  if( mbStoreInClient )
163  {
164  iValue = iDefault;
165  if( ExchangeWithMaster( Name ))
166  {
167  iValue = wxAtoi( mValueString );
168  }
169  }
170  else
171  {
173  mValueString = wxString::Format(wxT("%lld"), (long long) iValue);
174  return ExchangeWithMaster( Name );
175  }
176  return true;
177 }
178 
179 
180 bool Shuttle::TransferEnum( const wxString & Name, int & iValue,
181  const int nChoices, const wxString * pFirstStr)
182 {
183  if( mbStoreInClient )
184  {
185  iValue = 0;// default index if none other selected.
186  if( ExchangeWithMaster( Name ))
187  {
188  wxString str = mValueString;
189  if( str.Left( 1 ) == wxT('"') && str.Right( 1 ) == wxT('"') )
190  {
191  str = str.Mid( 2, str.length() - 2 );
192  }
193 
194  for( int i = 0; i < nChoices; i++ )
195  {
196  if( str == pFirstStr[i] )
197  {
198  iValue = i;
199  break;
200  }
201  }
202  }
203  }
204  else
205  {
206  //TIDY-ME: Out of range configuration values are silently discarded...
207  if( iValue > nChoices )
208  iValue = 0;
209  if( iValue < 0 )
210  iValue = 0;
211  mValueString = pFirstStr[iValue];
212  if( mValueString.Find( wxT(' ') ) != wxNOT_FOUND )
213  {
214  mValueString = wxT('"') + pFirstStr[iValue] + wxT('"'); //strings have quotes around them
215  }
216  return ExchangeWithMaster( Name );
217  }
218  return true;
219 }
220 
221 bool Shuttle::TransferString( const wxString & Name, wxString & strValue, const wxString & WXUNUSED(strDefault) )
222 {
223  if( mbStoreInClient )
224  {
225  if( ExchangeWithMaster( Name ))
226  {
227  strValue = mValueString;
228  }
229  else
230  return false;
231  }
232  else
233  {
234  mValueString = wxT('"') + strValue + wxT('"'); //strings have quotes around them
235  return ExchangeWithMaster( Name );
236  }
237  return true;
238 }
239 
240 bool Shuttle::TransferWrappedType( const wxString & Name, WrappedType & W )
241 {
242  if( mbStoreInClient )
243  {
244  if( ExchangeWithMaster( Name ))
245  {
247  }
248  }
249  else
250  {
252  return ExchangeWithMaster( Name );
253  }
254  return true;
255 }
256 
257 
258 bool Shuttle::ExchangeWithMaster(const wxString & WXUNUSED(Name))
259 {
260  // ExchangeWithMaster() will usually be over-ridden
261  // in derived classes. We could have made it an
262  // abstract function.
263  wxASSERT( false );
264  return true;
265 }
266 
267 // This variant uses values of the form
268 // param1=value1 param2=value2
269 bool ShuttleCli::ExchangeWithMaster(const wxString & Name)
270 {
271  if( !mbStoreInClient )
272  {
273  mParams += wxT(" ");
274  mParams +=Name;
275  mParams += wxT("=");
277  }
278  else
279  {
280  int i;
281  mParams = wxT(" ")+mParams;
282  i=mParams.Find( wxT(" ")+Name+wxT("=") );
283  if( i>=0 )
284  {
285  int j=i+2+Name.length();
286  wxString terminator = wxT(' ');
287  if(mParams.GetChar(j) == wxT('"')) //Strings are surrounded by quotes
288  {
289  terminator = wxT('"');
290  j++;
291  }
292  else if(mParams.GetChar(j) == wxT('\'')) // or by single quotes.
293  {
294  terminator = wxT('\'');
295  j++;
296  }
297  i=j;
298  while( j<(int)mParams.length() && mParams.GetChar(j) != terminator )
299  j++;
300  mValueString = mParams.Mid(i,j-i);
301  return true;
302  }
303  return false;
304  }
305  return true;
306 }
307 
308 
309 #ifdef _MSC_VER
310 // If this is compiled with MSVC (Visual Studio)
311 #pragma warning( push )
312 #pragma warning( disable: 4100 ) // unused parameters.
313 #endif //_MSC_VER
314 
315 
316 // The ShouldSet and CouldGet functions have an important side effect
317 // on the pOptionalFlag. They 'use it up' and clear it down for the next parameter.
318 
319 
320 // Tests for parameter being optional.
321 // Prepares for next parameter by clearing the pointer.
322 // Reports on whether the parameter should be set, i.e. should set
323 // if it was chosen to be set, or was not optional.
325  if( !pOptionalFlag )
326  return true;
327  bool result = *pOptionalFlag;
328  pOptionalFlag = NULL;
329  return result;
330 }
331 // These are functions to override. They do nothing.
332 void ShuttleParams::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl){;};
333 void ShuttleParams::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl ){;};
334 void ShuttleParams::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl ){;};
335 void ShuttleParams::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl ){;};
336 void ShuttleParams::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl ){;};
337 void ShuttleParams::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl ){;};
338 void ShuttleParams::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl ){;};
339 void ShuttleParams::DefineEnum( int &var, const wxChar * key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings ){;};
340 
341 
342 
343 /*
344 void ShuttleParams::DefineEnum( int &var, const wxChar * key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings )
345 {
346 }
347 */
348 
349 // ShuttleGetAutomation gets from the shuttle into typically a string.
351  pOptionalFlag = &var;
352  return *this;
353 };
354 
355 void ShuttleGetAutomation::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl )
356 {
357  if( !ShouldSet() ) return;
358  mpEap->Write(key, var);
359 }
360 
361 void ShuttleGetAutomation::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
362 {
363  if( !ShouldSet() ) return;
364  mpEap->Write(key, var);
365 }
366 
367 void ShuttleGetAutomation::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
368 {
369  if( !ShouldSet() ) return;
370  mpEap->Write(key, (int) var);
371 }
372 
373 void ShuttleGetAutomation::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
374 {
375  if( !ShouldSet() ) return;
376  mpEap->WriteFloat(key, var);
377 }
378 
379 void ShuttleGetAutomation::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
380 {
381  if( !ShouldSet() ) return;
382  mpEap->WriteFloat(key, var);
383 }
384 
385 void ShuttleGetAutomation::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl )
386 {
387  if( !ShouldSet() ) return;
388  mpEap->Write(key, var);
389 }
390 
391 
392 void ShuttleGetAutomation::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl )
393 {
394  if( !ShouldSet() ) return;
395  mpEap->Write(key, var);
396 }
397 
398 
399 void ShuttleGetAutomation::DefineEnum( int &var, const wxChar * key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings )
400 {
401  if( !ShouldSet() ) return;
402  mpEap->Write(key, strings[var].Internal());
403 }
404 
405 
406 
408  pOptionalFlag = &var;
409  return *this;
410 };
411 
412 // Tests for parameter being optional.
413 // Prepares for next parameter by clearing the pointer.
414 // If the parameter is optional, finds out if it was actually provided.
415 // i.e. could it be got from automation?
416 // The result goes into the flag variable, so we typically ignore the result.
417 bool ShuttleSetAutomation::CouldGet( const wxString &key ){
418  // Not optional? Can get as we will get the default, at worst.
419  if( !pOptionalFlag )
420  return true;
421  bool result = mpEap->HasEntry( key );
422  *pOptionalFlag = result;
423  pOptionalFlag = NULL;
424  return result;
425 }
426 
427 void ShuttleSetAutomation::Define( bool & var, const wxChar * key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl )
428 {
429  CouldGet( key );
430  if( !bOK )
431  return;
432  // Use of temp in this and related functions is to handle the case of
433  // only committing values if all values pass verification.
434  bool temp =var;
435  bOK = mpEap->ReadAndVerify(key, &temp, vdefault);
436  if( bWrite && bOK)
437  var = temp;
438 }
439 
440 void ShuttleSetAutomation::Define( int & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
441 {
442  CouldGet( key );
443  if( !bOK )
444  return;
445  int temp =var;
446  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
447  if( bWrite && bOK)
448  var = temp;
449 }
450 
451 void ShuttleSetAutomation::Define( size_t & var, const wxChar * key, const int vdefault, const int vmin, const int vmax, const int vscl )
452 {
453  CouldGet( key );
454  if( !bOK )
455  return;
456  int temp = var;
457  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
458  if( bWrite && bOK )
459  var = temp;
460 }
461 
462 void ShuttleSetAutomation::Define( float & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
463 {
464  CouldGet( key );
465  if( !bOK )
466  return;
467  float temp = var;
468  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
469  if( bWrite && bOK )
470  var = temp;
471 }
472 
473 
474 void ShuttleSetAutomation::Define( double & var, const wxChar * key, const float vdefault, const float vmin, const float vmax, const float vscl )
475 {
476  CouldGet( key );
477  if( !bOK )
478  return;
479  double temp = var;
480  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
481  if( bWrite && bOK)
482  var = temp;
483 }
484 
485 void ShuttleSetAutomation::Define( double & var, const wxChar * key, const double vdefault, const double vmin, const double vmax, const double vscl )
486 {
487  CouldGet( key );
488  if( !bOK )
489  return;
490  double temp = var;
491  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, vmin, vmax);
492  if( bWrite && bOK)
493  var = temp;
494 }
495 
496 
497 void ShuttleSetAutomation::Define( wxString &var, const wxChar * key, const wxString vdefault, const wxString vmin, const wxString vmax, const wxString vscl )
498 {
499  CouldGet( key );
500  if( !bOK )
501  return;
502  wxString temp = var;
503  bOK = mpEap->ReadAndVerify(key, &temp, vdefault);
504  if( bWrite && bOK )
505  var = temp;
506 }
507 
508 
509 void ShuttleSetAutomation::DefineEnum( int &var, const wxChar * key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings )
510 {
511  CouldGet( key );
512  if( !bOK )
513  return;
514  int temp = var;
515  bOK = mpEap->ReadAndVerify(key, &temp, vdefault, strings, nStrings);
516  if( bWrite && bOK)
517  var = temp;
518 }
519 
520 
521 #ifdef _MSC_VER
522 // If this is compiled with MSVC (Visual Studio)
523 #pragma warning( pop )
524 #endif //_MSC_VER
525 
526 
527 
528 
529 
WrappedType::ReadAsString
wxString ReadAsString()
Definition: WrappedType.cpp:42
ShuttleGetAutomation::DefineEnum
void DefineEnum(int &var, const wxChar *key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings) override
Definition: Shuttle.cpp:399
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, float *val, float defVal, float min, float max) const
Definition: EffectAutomationParameters.h:213
Shuttle::TransferFloat
virtual bool TransferFloat(const wxString &Name, float &fValue, const float &fDefault)
Definition: Shuttle.cpp:97
ShuttleGetAutomation::Optional
ShuttleParams & Optional(bool &var) override
Definition: Shuttle.cpp:350
Shuttle::TransferDouble
virtual bool TransferDouble(const wxString &Name, double &dValue, const double &dDefault)
Definition: Shuttle.cpp:116
Shuttle::TransferBool
virtual bool TransferBool(const wxString &Name, bool &bValue, const bool &bDefault)
Definition: Shuttle.cpp:78
Shuttle::TransferEnum
virtual bool TransferEnum(const wxString &Name, int &iValue, const int nChoices, const wxString *pFirstStr)
Definition: Shuttle.cpp:180
ShuttleParams
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:62
Shuttle::TransferWrappedType
virtual bool TransferWrappedType(const wxString &Name, WrappedType &W)
Definition: Shuttle.cpp:240
ShuttleSetAutomation::CouldGet
bool CouldGet(const wxString &key)
Definition: Shuttle.cpp:417
CommandParameters::WriteFloat
bool WriteFloat(const wxString &key, float f)
Definition: EffectAutomationParameters.h:155
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
ShuttleSetAutomation::DefineEnum
void DefineEnum(int &var, const wxChar *key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings) override
Definition: Shuttle.cpp:509
ShuttleCli::mParams
wxString mParams
Definition: Shuttle.h:49
WrappedType.h
ShuttleGetAutomation::Define
void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl) override
Definition: Shuttle.cpp:355
ShuttleParams::mpEap
CommandParameters * mpEap
Definition: Shuttle.h:66
EffectAutomationParameters.h
Shuttle::TransferLongLong
virtual bool TransferLongLong(const wxString &Name, wxLongLong_t &iValue, const wxLongLong_t &iDefault)
Definition: Shuttle.cpp:160
WrappedType
Used in type conversions, this wrapper for ints, strings, doubles and enums provides conversions betw...
Definition: WrappedType.h:31
ShuttleParams::DefineEnum
virtual void DefineEnum(int &var, const wxChar *key, const int vdefault, const EnumValueSymbol strings[], size_t nStrings)
Definition: Shuttle.cpp:339
ShuttleSetAutomation::Optional
ShuttleParams & Optional(bool &var) override
Definition: Shuttle.cpp:407
Shuttle::TransferString
virtual bool TransferString(const wxString &Name, wxString &strValue, const wxString &strDefault)
Definition: Shuttle.cpp:221
Shuttle::ExchangeWithMaster
virtual bool ExchangeWithMaster(const wxString &Name)
Definition: Shuttle.cpp:258
ShuttleSetAutomation::Define
void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin, const bool vmax, const bool vscl) override
Definition: Shuttle.cpp:427
ShuttleParams::ShouldSet
bool ShouldSet()
Definition: Shuttle.cpp:324
ShuttleCli::ExchangeWithMaster
bool ExchangeWithMaster(const wxString &Name) override
Definition: Shuttle.cpp:269
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: CommandManager.cpp:201
CommandParameters::HasEntry
virtual bool HasEntry(const wxString &strName) const override
Definition: EffectAutomationParameters.h:87
Shuttle::TransferInt
virtual bool TransferInt(const wxString &Name, int &iValue, const int &iDefault)
Definition: Shuttle.cpp:136
Shuttle::mValueString
wxString mValueString
Definition: Shuttle.h:28
Shuttle::mbStoreInClient
bool mbStoreInClient
Definition: Shuttle.h:27
ShuttleSetAutomation::bWrite
bool bWrite
Definition: Shuttle.h:110
ExceptionType::Internal
@ Internal
Indicates internal failure from Audacity.
ShuttleSetAutomation::bOK
bool bOK
Definition: Shuttle.h:108
Shuttle::Shuttle
Shuttle()
Definition: Shuttle.cpp:74
WrappedType::WriteToAsString
void WriteToAsString(const wxString &InStr)
Definition: WrappedType.cpp:152
Shuttle.h
ShuttleParams::Define
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:332
ShuttleParams::pOptionalFlag
bool * pOptionalFlag
Definition: Shuttle.h:65