Audacity  3.0.3
MeterToolBar.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  MeterToolBar.cpp
6 
7  Dominic Mazzoni
8  Leland Lucius
9 
10  See MeterToolBar.h for details
11 
12 *******************************************************************//*******************************************************************/
18 
19 
20 
21 #include "MeterToolBar.h"
22 
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include <wx/wxprec.h>
25 
26 #include <wx/setup.h> // for wxUSE_* macros
27 
28 #ifndef WX_PRECOMP
29 #include <wx/event.h>
30 #include <wx/intl.h>
31 #include <wx/tooltip.h>
32 #endif
33 
34 #include <wx/gbsizer.h>
35 
36 #include "AllThemeResources.h"
37 #include "../ProjectAudioIO.h"
38 #include "../widgets/MeterPanel.h"
39 
41 
45 
46 BEGIN_EVENT_TABLE( MeterToolBar, ToolBar )
47  EVT_SIZE( MeterToolBar::OnSize )
49 
50 //Standard constructor
52 : ToolBar(project, type, XO("Combined Meter"), wxT("CombinedMeter"), true)
53 {
54  if( mType == RecordMeterBarID ){
55  mWhichMeters = kWithRecordMeter;
56  mLabel = XO("Recording Meter");
57  mSection = wxT("RecordMeter");
58  } else if( mType == PlayMeterBarID ){
59  mWhichMeters = kWithPlayMeter;
60  mLabel = XO("Playback Meter");
61  mSection = wxT("PlayMeter");
62  } else {
63  mWhichMeters = kWithPlayMeter | kWithRecordMeter;
64  }
65  mSizer = NULL;
66  mPlayMeter = NULL;
67  mRecordMeter = NULL;
68 }
69 
71 {
72 }
73 
74 void MeterToolBar::Create(wxWindow * parent)
75 {
76  ToolBar::Create(parent);
77 
78  UpdatePrefs();
79 
80  // Simulate a size event to set initial meter placement/size
81  wxSizeEvent dummy;
82  OnSize(dummy);
83 }
84 
86 {
87  MeterPanel::State playState{ false }, recordState{ false };
88 
89  auto &projectAudioIO = ProjectAudioIO::Get( mProject );
90  if (mPlayMeter &&
91  projectAudioIO.GetPlaybackMeter() == mPlayMeter->GetMeter())
92  {
93  playState = mPlayMeter->SaveState();
94  projectAudioIO.SetPlaybackMeter( nullptr );
95  }
96 
97  if (mRecordMeter &&
98  projectAudioIO.GetCaptureMeter() == mRecordMeter->GetMeter())
99  {
100  recordState = mRecordMeter->SaveState();
101  projectAudioIO.SetCaptureMeter( nullptr );
102  }
103 
105 
106  mPlayMeter->RestoreState(playState);
107  if( playState.mSaved ){
108  projectAudioIO.SetPlaybackMeter( mPlayMeter->GetMeter() );
109  }
110  mRecordMeter->RestoreState(recordState);
111  if( recordState.mSaved ){
112  projectAudioIO.SetCaptureMeter( mRecordMeter->GetMeter() );
113  }
114 }
115 
117 {
118  SetBackgroundColour( theTheme.Colour( clrMedium ) );
119  Add((mSizer = safenew wxGridBagSizer()), 1, wxEXPAND);
120 
122  //JKC: Record on left, playback on right. Left to right flow
123  //(maybe we should do it differently for Arabic language :-) )
125  this,
126  wxID_ANY,
127  true,
128  wxDefaultPosition,
129  wxSize( 260, 28 ) );
130  /* i18n-hint: (noun) The meter that shows the loudness of the audio being recorded.*/
131  mRecordMeter->SetName( XO("Record Meter"));
132  /* i18n-hint: (noun) The meter that shows the loudness of the audio being recorded.
133  This is the name used in screen reader software, where having 'Meter' first
134  apparently is helpful to partially sighted people. */
135  mRecordMeter->SetLabel( XO("Meter-Record") );
136  mSizer->Add( mRecordMeter, wxGBPosition( 0, 0 ), wxDefaultSpan, wxEXPAND );
137  }
138 
141  this,
142  wxID_ANY,
143  false,
144  wxDefaultPosition,
145  wxSize( 260, 28 ) );
146  /* i18n-hint: (noun) The meter that shows the loudness of the audio playing.*/
147  mPlayMeter->SetName( XO("Play Meter"));
148  /* i18n-hint: (noun) The meter that shows the loudness of the audio playing.
149  This is the name used in screen reader software, where having 'Meter' first
150  apparently is helpful to partially sighted people. */
151  mPlayMeter->SetLabel( XO("Meter-Play"));
152  mSizer->Add( mPlayMeter, wxGBPosition( (mWhichMeters & kWithRecordMeter)?1:0, 0 ), wxDefaultSpan, wxEXPAND );
153  }
154 
156 }
157 
159 {
161 
162  // Set label to pull in language change
163  SetLabel(XO("Meter"));
164 
165  // Give base class a chance
167 }
168 
170 {
171 #if wxUSE_TOOLTIPS
172  if( mPlayMeter )
173  mPlayMeter->SetToolTip( XO("Playback Level") );
174  if( mRecordMeter )
175  mRecordMeter->SetToolTip( XO("Recording Level") );
176 #endif
177 }
178 
179 void MeterToolBar::OnSize( wxSizeEvent & event) //WXUNUSED(event) )
180 {
181  event.Skip();
182  int width, height;
183 
184  // We can be resized before populating...protect against it
185  if( !mSizer ) {
186  return;
187  }
188 
189  // Update the layout
190  Layout();
191 
192  // Get the usable area
193  wxSize sz = GetSizer()->GetSize();
194  width = sz.x; height = sz.y;
195 
196  int nMeters =
197  ((mRecordMeter ==NULL) ? 0:1) +
198  ((mPlayMeter ==NULL) ? 0:1);
199 
200  bool bHorizontal = ( width > height );
201  bool bEndToEnd = ( nMeters > 1 ) && wxMin( width, height ) < (60 * nMeters);
202 
203  // Default location for second meter
204  wxGBPosition pos( 0, 0 );
205  // If 2 meters, share the height or width.
206  if( nMeters > 1 ){
207  if( bHorizontal ^ bEndToEnd ){
208  height /= nMeters;
209  pos = wxGBPosition( 1, 0 );
210  } else {
211  width /= nMeters;
212  pos = wxGBPosition( 0, 1 );
213  }
214  }
215 
216  if( mRecordMeter ) {
217  mRecordMeter->SetMinSize( wxSize( width, height ));
218  }
219  if( mPlayMeter ) {
220  mPlayMeter->SetMinSize( wxSize( width, height));
221  mSizer->SetItemPosition( mPlayMeter, pos );
222  }
223 
224  // And make it happen
225  Layout();
226  Fit();
227 }
228 
229 bool MeterToolBar::Expose( bool show )
230 {
231  auto &projectAudioIO = ProjectAudioIO::Get( mProject );
232  if( show ) {
233  if( mPlayMeter ) {
234  projectAudioIO.SetPlaybackMeter( mPlayMeter->GetMeter() );
235  }
236 
237  if( mRecordMeter ) {
238  projectAudioIO.SetCaptureMeter( mRecordMeter->GetMeter() );
239  }
240  } else {
241  if( mPlayMeter &&
242  projectAudioIO.GetPlaybackMeter() == mPlayMeter->GetMeter() ) {
243  projectAudioIO.SetPlaybackMeter( nullptr );
244  }
245 
246  if( mRecordMeter &&
247  projectAudioIO.GetCaptureMeter() == mRecordMeter->GetMeter() ) {
248  projectAudioIO.SetCaptureMeter( nullptr );
249  }
250  }
251 
252  return ToolBar::Expose( show );
253 }
254 
255 // The meter's sizing code does not take account of the resizer
256 // Hence after docking we need to enlarge the bar (using fit)
257 // so that the resizer can be reached.
258 void MeterToolBar::SetDocked(ToolDock *dock, bool pushed) {
259  ToolBar::SetDocked(dock, pushed);
260  Fit();
261 }
262 
264  []( AudacityProject &project ){
265  return ToolBar::Holder{
266  safenew MeterToolBar{ project, RecordMeterBarID } }; }
267 };
269  []( AudacityProject &project ){
270  return ToolBar::Holder{
271  safenew MeterToolBar{ project, PlayMeterBarID } }; }
272 };
274  []( AudacityProject &project ){
275  return ToolBar::Holder{
276  safenew MeterToolBar{ project, MeterBarID } }; }
277 };
278 
279 #include "ToolManager.h"
280 
281 namespace {
283  /* i18n-hint: Clicking this menu item shows the toolbar
284  with the recording level meters */
285  RecordMeterBarID, wxT("ShowRecordMeterTB"), XXO("&Recording Meter Toolbar"),
286  {}, { MeterBarID }
287 };
289  /* i18n-hint: Clicking this menu item shows the toolbar
290  with the playback level meter */
291  PlayMeterBarID, wxT("ShowPlayMeterTB"), XXO("&Playback Meter Toolbar"),
292  {}, { MeterBarID }
293 };
294 //AttachedToolBarMenuItem sAttachment3{
295 // /* --i18nhint: Clicking this menu item shows the toolbar
296 // which has sound level meters */
297 // MeterBarID, wxT("ShowMeterTB"), XXO("Co&mbined Meter Toolbar"),
298 // { Registry::OrderingHint::After, "ShowPlayMeterTB" },
299 // { PlayMeterBarID, RecordMeterBarID }
300 //};
301 }
MeterToolBar::Expose
bool Expose(bool show) override
Definition: MeterToolBar.cpp:229
ToolDock
A dynamic panel where a ToolBar can be docked.
Definition: ToolDock.h:290
ToolManager.h
wxPanelWrapper::SetToolTip
void SetToolTip(const TranslatableString &toolTip)
Definition: wxPanelWrapper.cpp:56
AllThemeResources.h
wxPanelWrapper::SetLabel
void SetLabel(const TranslatableString &label)
Definition: wxPanelWrapper.cpp:46
ToolBar::mProject
AudacityProject & mProject
Definition: ToolBar.h:235
MeterPanel::State
Definition: MeterPanel.h:183
ToolBar::SetLabel
void SetLabel(const wxString &label) override
Definition: ToolBar.cpp:398
PlayMeterBarID
@ PlayMeterBarID
Definition: ToolBar.h:76
MeterToolBar
A ToolBar that holds the VU Meter.
Definition: MeterToolBar.h:32
anonymous_namespace{MeterToolBar.cpp}::sAttachment2
AttachedToolBarMenuItem sAttachment2
Definition: MeterToolBar.cpp:288
wxPanelWrapper::SetName
void SetName(const TranslatableString &name)
Definition: wxPanelWrapper.cpp:51
XO
#define XO(s)
Definition: Internat.h:31
Meter
AudioIO uses this to send sample buffers for real-time display updates.
Definition: Meter.h:16
ToolBar::GetSizer
wxBoxSizer * GetSizer()
Definition: ToolBar.cpp:678
factory1
static RegisteredToolbarFactory factory1
Definition: MeterToolBar.cpp:263
ToolBar::Holder
wxWindowPtr< ToolBar > Holder
Definition: ToolBar.h:102
ToolBar::ReCreateButtons
virtual void ReCreateButtons()
Definition: ToolBar.cpp:516
MeterToolBar::UpdatePrefs
void UpdatePrefs() override
Definition: MeterToolBar.cpp:158
ProjectAudioIO::Get
static ProjectAudioIO & Get(AudacityProject &project)
Definition: ProjectAudioIO.cpp:22
MeterPanel::SaveState
State SaveState()
Definition: MeterPanel.cpp:1939
MeterPanel::RestoreState
void RestoreState(const State &state)
Definition: MeterPanel.cpp:1944
AttachedToolBarMenuItem
Definition: ToolManager.h:224
MeterToolBar::Populate
void Populate() override
Definition: MeterToolBar.cpp:116
factory2
static RegisteredToolbarFactory factory2
Definition: MeterToolBar.cpp:268
XXO
#define XXO(s)
Definition: Internat.h:44
MeterToolBar::ReCreateButtons
void ReCreateButtons() override
Definition: MeterToolBar.cpp:85
MeterToolBar::mSizer
wxGridBagSizer * mSizer
Definition: MeterToolBar.h:62
MeterToolBar::Create
void Create(wxWindow *parent) override
Definition: MeterToolBar.cpp:74
MeterToolBar::OnSize
void OnSize(wxSizeEvent &event)
Definition: MeterToolBar.cpp:179
MeterPanelBase::GetMeter
std::shared_ptr< Meter > GetMeter() const
Definition: MeterPanelBase.cpp:85
MeterToolBar::RegenerateTooltips
void RegenerateTooltips() override
Definition: MeterToolBar.cpp:169
factory3
static RegisteredToolbarFactory factory3
Definition: MeterToolBar.cpp:273
ToolBar::UpdatePrefs
void UpdatePrefs() override
Definition: ToolBar.cpp:605
IMPLEMENT_CLASS
IMPLEMENT_CLASS(MeterToolBar, ToolBar)
kWithRecordMeter
const int kWithRecordMeter
Definition: MeterToolBar.h:29
MeterToolBar.h
ToolBar::Create
virtual void Create(wxWindow *parent)
Definition: ToolBar.cpp:475
MeterToolBar::mPlayMeter
MeterPanel * mPlayMeter
Definition: MeterToolBar.h:63
theTheme
THEME_API Theme theTheme
Definition: Theme.cpp:79
RegisteredToolbarFactory
Definition: ToolBar.h:261
ToolBar::Expose
virtual bool Expose(bool show=true)
Definition: ToolBar.cpp:444
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
MeterToolBar::mRecordMeter
MeterPanel * mRecordMeter
Definition: MeterToolBar.h:64
MeterPanel
MeterPanel is a panel that paints the meter used for monitoring or playback.
Definition: MeterPanel.h:97
ToolBar
Works with ToolManager and ToolDock to provide a dockable window in which buttons can be placed.
Definition: ToolBar.h:98
ThemeBase::Colour
wxColour & Colour(int iIndex)
Definition: Theme.cpp:1189
MeterBarID
@ MeterBarID
Definition: ToolBar.h:74
MeterToolBar::~MeterToolBar
virtual ~MeterToolBar()
Definition: MeterToolBar.cpp:70
MeterToolBar::mWhichMeters
int mWhichMeters
Definition: MeterToolBar.h:61
anonymous_namespace{MeterToolBar.cpp}::sAttachment1
AttachedToolBarMenuItem sAttachment1
Definition: MeterToolBar.cpp:282
kWithPlayMeter
const int kWithPlayMeter
Definition: MeterToolBar.h:30
MeterToolBar::SetDocked
virtual void SetDocked(ToolDock *dock, bool pushed) override
Definition: MeterToolBar.cpp:258
ToolBar::SetDocked
virtual void SetDocked(ToolDock *dock, bool pushed)
Definition: ToolBar.cpp:638
RecordMeterBarID
@ RecordMeterBarID
Definition: ToolBar.h:75
safenew
#define safenew
Definition: MemoryX.h:10
END_EVENT_TABLE
END_EVENT_TABLE()
ToolBar::Add
void Add(wxWindow *window, int proportion=0, int flag=wxALIGN_TOP, int border=0, wxObject *userData=NULL)
Definition: ToolBar.cpp:686