1 /**********************************************************************
2 
3    Audacity - A Digital Audio Editor
4    Copyright 1999-2018 Audacity Team
5    File License: wxWidgets
6 
7    Dan Horgan
8    James Crook
9 
10 ******************************************************************//**
11 
12 \file PreferenceCommands.cpp
13 \brief Contains definitions for the GetPreferenceCommand and
14 SetPreferenceCommand classes
15 
16 *//*******************************************************************/
17 
18 
19 #include "PreferenceCommands.h"
20 
21 #include "LoadCommands.h"
22 #include "Prefs.h"
23 #include "../Shuttle.h"
24 #include "../ShuttleGui.h"
25 #include "../commands/CommandContext.h"
26 #include "../prefs/PrefsDialog.h"
27 
28 const ComponentInterfaceSymbol GetPreferenceCommand::Symbol
29 { XO("Get Preference") };
30 
31 namespace{ BuiltinCommandsModule::Registration< GetPreferenceCommand > reg; }
32 
DefineParams(ShuttleParams & S)33 bool GetPreferenceCommand::DefineParams( ShuttleParams & S ){
34    S.Define( mName, wxT("Name"),   wxT("") );
35    return true;
36 }
37 
PopulateOrExchange(ShuttleGui & S)38 void GetPreferenceCommand::PopulateOrExchange(ShuttleGui & S)
39 {
40    S.AddSpace(0, 5);
41 
42    S.StartMultiColumn(2, wxALIGN_CENTER);
43    {
44       S.TieTextBox(XXO("Name:"),mName);
45    }
46    S.EndMultiColumn();
47 }
48 
49 
Apply(const CommandContext & context)50 bool GetPreferenceCommand::Apply(const CommandContext & context)
51 {
52    wxString prefValue;
53    if (!gPrefs->Read(mName, &prefValue))
54       return false;
55 
56    context.Status(prefValue);
57    return true;
58 }
59 
60 const ComponentInterfaceSymbol SetPreferenceCommand::Symbol
61 { XO("Set Preference") };
62 
63 namespace{ BuiltinCommandsModule::Registration< SetPreferenceCommand > reg2; }
64 
DefineParams(ShuttleParams & S)65 bool SetPreferenceCommand::DefineParams( ShuttleParams & S ){
66    S.Define(    mName,   wxT("Name"),    wxT("") );
67    S.Define(   mValue,   wxT("Value"),   wxT("") );
68    S.Define( mbReload,   wxT("Reload"),  false );
69    return true;
70 }
71 
PopulateOrExchange(ShuttleGui & S)72 void SetPreferenceCommand::PopulateOrExchange(ShuttleGui & S)
73 {
74    S.AddSpace(0, 5);
75 
76    S.StartMultiColumn(2, wxALIGN_CENTER);
77    {
78       S.TieTextBox(XXO("Name:"),mName);
79       S.TieTextBox(XXO("Value:"),mValue);
80       S.TieCheckBox(XXO("Reload"),mbReload);
81    }
82    S.EndMultiColumn();
83 }
84 
Apply(const CommandContext & context)85 bool SetPreferenceCommand::Apply(const CommandContext & context)
86 {
87    bool bOK = gPrefs->Write(mName, mValue) && gPrefs->Flush();
88    if( bOK && mbReload ){
89       auto &project = context.project;
90       DoReloadPreferences( project );
91    }
92    return bOK;
93 }
94 
95