Audacity  3.0.3
CommonCommandFlags.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 CommonCommandFlags.cpp
6 
7 Paul Licameli split from Menus.cpp
8 
9 **********************************************************************/
10 
11 
12 #include "CommonCommandFlags.h"
13 
14 
15 
16 #include <wx/frame.h>
17 
18 #include "AudioIO.h"
19 #include "Menus.h"
20 #include "NoteTrack.h"
21 #include "Project.h"
22 #include "ProjectAudioIO.h"
23 #include "ProjectFileIO.h"
24 #include "ProjectHistory.h"
25 #include "ProjectSettings.h"
26 #include "ProjectWindows.h"
27 #include "UndoManager.h"
28 #include "ViewInfo.h"
29 #include "WaveTrack.h"
31 
32 /*
33 
34 This file registers functions implementing many of the tests for enabling of
35 menu items. Sequence of a few of them has minor significance, but for most
36 there is little reason to keep them in one file. Flags used only by one
37 other file might instead be defined only where used.
38 
39 They are collected here because Menus.cpp is too low level to have the
40 dependencies implied by the include directives above -- it would make dependency
41 cycles.
42 
43 */
44 
45 // Strong predicate excludes tracks that do not support basic editing.
47 {
48  auto range = TrackList::Get( project ).Selected()
49  - []( const Track *pTrack ){
50  return !pTrack->SupportsBasicEditing(); };
51  return !range.empty();
52 };
53 
54 // Weaker predicate.
55 bool AnyTracksSelectedPred( const AudacityProject &project )
56 {
57  auto range = TrackList::Get( project ).Selected();
58  return !range.empty();
59 };
60 
61 bool AudioIOBusyPred( const AudacityProject &project )
62 {
64  ProjectAudioIO::Get( project ).GetAudioIOToken());
65 };
66 
67 bool TimeSelectedPred( const AudacityProject &project )
68 {
69  // This is equivalent to check if there is a valid selection,
70  // so it's used for Zoom to Selection too
71  return !ViewInfo::Get( project ).selectedRegion.isPoint();
72 };
73 
75 static CommandFlagOptions result{
76 // In reporting the issue with cut or copy, we don't tell the user they could also select some text in a label.
77  []( const TranslatableString &Name ) {
78  // PRL: These strings have hard-coded mention of a certain shortcut key,
79  // thus assuming the default shortcuts. That is questionable.
81 #ifdef EXPERIMENTAL_DA
82  // i18n-hint: %s will be replaced by the name of an action, such as Normalize, Cut, Fade.
83  format = XO("You must first select some audio for '%s' to act on.\n\nCtrl + A selects all audio.");
84 #else
85 #ifdef __WXMAC__
86  // i18n-hint: %s will be replaced by the name of an action, such as Normalize, Cut, Fade.
87  format = XO("Select the audio for %s to use (for example, Cmd + A to Select All) then try again."
88  // No need to explain what a help button is for.
89  // "\n\nClick the Help button to learn more about selection methods."
90  );
91 
92 #else
93  // i18n-hint: %s will be replaced by the name of an action, such as Normalize, Cut, Fade.
94  format = XO("Select the audio for %s to use (for example, Ctrl + A to Select All) then try again."
95  // No need to explain what a help button is for.
96  // "\n\nClick the Help button to learn more about selection methods."
97  );
98 #endif
99 #endif
100  return format.Format( Name );
101  },
102  "Selecting_Audio_-_the_basics",
103  XO("No Audio Selected")
104 };
105 return result;
106 }
107 
108 // Noise Reduction has a custom error message, when nothing selected.
110  []( const TranslatableString &Name ) {
111  // i18n-hint: %s will be replaced by the name of an effect, usually 'Noise Reduction'.
112  auto format = XO("Select the audio for %s to use.\n\n1. Select audio that represents noise and use %s to get your 'noise profile'.\n\n2. When you have got your noise profile, select the audio you want to change\nand use %s to change that audio.");
113  return format.Format( Name, Name, Name );
114  },
115  "Noise_Reduction",
116  XO("No Audio Selected")
117 };
118 
119 
120  // The sequence of these definitions has a minor significance in determining
121  // which user error message has precedence if more than one might apply, so
122  // they should be kept in this sequence in one .cpp file if it is important
123  // to preserve that behavior. If they are dispersed to more than one file,
124  // then the precedence will be unspecified.
125  // The ordering of the flags that only disable the default message is not
126  // significant.
127 const ReservedCommandFlag&
129  [](const AudacityProject &project ){
130  return !AudioIOBusyPred( project );
131  },
132  CommandFlagOptions{ []( const TranslatableString& ) { return
133  // This reason will not be shown, because options that require it will be greyed out.
134  XO("You can only do this when playing and recording are\nstopped. (Pausing is not sufficient.)");
135  } ,"FAQ:Errors:Audio Must Be Stopped"}
136  .QuickTest()
137  .Priority( 1 )
138  }; return flag; }//lll
139 const ReservedCommandFlag&
141  [](const AudacityProject &project){
142  // True iff at least one stereo track is selected, i.e., at least
143  // one right channel is selected.
144  // TODO: more-than-two-channels
145  auto range = TrackList::Get( project ).Selected<const WaveTrack>()
146  - &Track::IsLeader;
147  return !range.empty();
148  },
149  { []( const TranslatableString& ) { return
150  // This reason will not be shown, because the stereo-to-mono is greyed out if not allowed.
151  XO("You must first select some stereo audio to perform this\naction. (You cannot use this with mono.)");
152  } ,"Audacity_Selection"}
153  }; return flag; } //lda
154 const ReservedCommandFlag&
158  }; return flag; }
159 const ReservedCommandFlag&
163  }; return flag; }
164 const ReservedCommandFlag&
166  [](const AudacityProject &project){
167  return !TrackList::Get( project ).Selected<const WaveTrack>().empty();
168  },
169  { []( const TranslatableString& ) { return
170  XO("You must first select some audio to perform this action.\n(Selecting other kinds of track won't work.)");
171  } ,"Audacity_Selection"}
172  }; return flag; }
173 const ReservedCommandFlag&
175  [](const AudacityProject &project){
176  return !TrackList::Get( project ).Any().empty();
177  },
179  }; return flag; }
180 const ReservedCommandFlag&
183  { []( const TranslatableString &Name ){ return
184  // i18n-hint: %s will be replaced by the name of an action, such as "Remove Tracks".
185  XO("\"%s\" requires one or more tracks to be selected.").Format( Name );
186  },"Audacity_Selection" }
187  }; return flag; }
188 const ReservedCommandFlag&
191  { []( const TranslatableString &Name ){ return
192  // i18n-hint: %s will be replaced by the name of an action, such as "Remove Tracks".
193  XO("\"%s\" requires one or more tracks to be selected.").Format( Name );
194  },"Audacity_Selection" }
195  }; return flag; }
196 const ReservedCommandFlag&
198  [](const AudacityProject &project){
199  for (auto w = wxWindow::FindFocus(); w; w = w->GetParent()) {
200  if (dynamic_cast<const NonKeystrokeInterceptingWindow*>(w))
201  return true;
202  }
203  return false;
204  },
206  }; return flag; }; //lll
207 
208 const ReservedCommandFlag&
212  }; return flag; } //lll
213 const ReservedCommandFlag&
215  [](const AudacityProject &){
216  auto gAudioIO = AudioIO::Get();
217  return !(
218  gAudioIO->IsBusy() &&
219  gAudioIO->GetNumCaptureChannels() > 0
220  );
221  }
222  }; return flag; };
223 
224 const ReservedCommandFlag&
226  [](const AudacityProject &project){
227  return !TrackList::Get( project ).Any<const LabelTrack>().empty();
228  }
229  }; return flag; }
230 const ReservedCommandFlag&
232  [](const AudacityProject &project){
233  auto &undoManager = UndoManager::Get( project );
234  return
235  undoManager.UnsavedChanges()
236  ||
237  ProjectFileIO::Get( project ).IsModified()
238  ;
239  }
240  }; return flag; }
241 const ReservedCommandFlag&
243  [](const AudacityProject &project){
244  return ProjectHistory::Get( project ).UndoAvailable();
245  }
246  }; return flag; }
247 const ReservedCommandFlag&
249  [](const AudacityProject &project){
250  return ProjectHistory::Get( project ).RedoAvailable();
251  }
252  }; return flag; }
253 const ReservedCommandFlag&
255  [](const AudacityProject &project){
256  return
257  ViewInfo::Get( project ).ZoomInAvailable()
258  &&
259  !TrackList::Get( project ).Any().empty()
260  ;
261  }
262  }; return flag; }
263 const ReservedCommandFlag&
265  [](const AudacityProject &project){
266  return
267  ViewInfo::Get( project ).ZoomOutAvailable()
268  &&
269  !TrackList::Get( project ).Any().empty()
270  ;
271  }
272  }; return flag; }
273 const ReservedCommandFlag&
275  [](const AudacityProject &project){
276  return ViewInfo::Get(project).playRegion.Locked();
277  }
278  }; return flag; } //msmeyer
279 const ReservedCommandFlag&
281  [](const AudacityProject &project){
282  const auto &playRegion = ViewInfo::Get(project).playRegion;
283  return !playRegion.Locked() && !playRegion.Empty();
284  }
285  }; return flag; } //msmeyer
286 const ReservedCommandFlag&
288  [](const AudacityProject &project){
289  return !TrackList::Get( project ).Any<const WaveTrack>().empty();
290  }
291  }; return flag; }
292 #ifdef USE_MIDI
293 const ReservedCommandFlag&
295  [](const AudacityProject &project){
296  return !TrackList::Get( project ).Any<const NoteTrack>().empty();
297  }
298  }; return flag; } //gsw
299 const ReservedCommandFlag&
301  [](const AudacityProject &project){
302  return !TrackList::Get( project ).Selected<const NoteTrack>().empty();
303  }
304  }; return flag; } //gsw
305 #endif
306 const ReservedCommandFlag&
308  [](const AudacityProject &project){
309  return !ProjectSettings::Get( project ).IsSyncLocked();
310  }
311  }; return flag; } //awd
312 const ReservedCommandFlag&
314  [](const AudacityProject &project){
315  return ProjectSettings::Get( project ).IsSyncLocked();
316  }
317  }; return flag; } //awd
318 const ReservedCommandFlag&
320  [](const AudacityProject &project){
321  const wxWindow *focus = FindProjectFrame( &project );
322  if (focus) {
323  while (focus && focus->GetParent())
324  focus = focus->GetParent();
325  }
326  return (focus &&
327  !static_cast<const wxTopLevelWindow*>(focus)->IsIconized()
328  );
329  },
331  }; return flag; } // prl
332 const ReservedCommandFlag&
334  [](const AudacityProject&){
335  return AudioIOBase::Get()->IsPaused();
336  },
338  }; return flag; }
339 const ReservedCommandFlag&
341  [](const AudacityProject &project){
342  auto &tracks = TrackList::Get( project );
343  return
344 #ifdef EXPERIMENTAL_MIDI_OUT
345  !tracks.Any<const NoteTrack>().empty()
346  ||
347 #endif
348  !tracks.Any<const WaveTrack>().empty()
349  ;
350  }
351  }; return flag; }
352 const ReservedCommandFlag&
354  [](const AudacityProject &project){
355  auto &tracks = TrackList::Get( project );
356  return
357 #ifdef USE_MIDI
358  !tracks.Selected<const NoteTrack>().empty()
359  // even if not EXPERIMENTAL_MIDI_OUT
360  ||
361 #endif
362  !tracks.Selected<const WaveTrack>().empty()
363  ;
364  }
365  }; return flag; }
366 const ReservedCommandFlag&
368  [](const AudacityProject &){ return true; }
369  }; return flag; } // jkc
370 ;
371 
WaveTrack.h
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:161
CommandFlagOptions::DisableDefaultMessage
CommandFlagOptions && DisableDefaultMessage() &&
Definition: CommandFlag.h:54
WaveTrack
A Track that contains audio waveform data.
Definition: WaveTrack.h:69
ProjectFileIO.h
PlayRegionNotLockedFlag
const ReservedCommandFlag & PlayRegionNotLockedFlag()
Definition: CommonCommandFlags.cpp:280
flag
static std::once_flag flag
Definition: WaveformView.cpp:1124
IsSyncLockedFlag
const ReservedCommandFlag & IsSyncLockedFlag()
Definition: CommonCommandFlags.cpp:313
NoAutoSelect
const ReservedCommandFlag & NoAutoSelect()
Definition: CommonCommandFlags.cpp:367
AudioIOBusyPred
bool AudioIOBusyPred(const AudacityProject &project)
Definition: CommonCommandFlags.cpp:61
Project.h
ViewInfo::playRegion
PlayRegion playRegion
Definition: ViewInfo.h:197
AudioIONotBusyFlag
const ReservedCommandFlag & AudioIONotBusyFlag()
Definition: CommonCommandFlags.cpp:128
AudioIOBusyFlag
const ReservedCommandFlag & AudioIOBusyFlag()
Definition: CommonCommandFlags.cpp:209
WaveTracksExistFlag
const ReservedCommandFlag & WaveTracksExistFlag()
Definition: CommonCommandFlags.cpp:287
ReservedCommandFlag
Definition: CommandFlag.h:89
PlayRegionLockedFlag
const ReservedCommandFlag & PlayRegionLockedFlag()
Definition: CommonCommandFlags.cpp:274
LabelTracksExistFlag
const ReservedCommandFlag & LabelTracksExistFlag()
Definition: CommonCommandFlags.cpp:225
UnsavedChangesFlag
const ReservedCommandFlag & UnsavedChangesFlag()
Definition: CommonCommandFlags.cpp:231
XO
#define XO(s)
Definition: Internat.h:31
ProjectFileIO::Get
static ProjectFileIO & Get(AudacityProject &project)
Definition: ProjectFileIO.cpp:265
ProjectSettings::Get
static ProjectSettings & Get(AudacityProject &project)
Definition: ProjectSettings.cpp:41
LabelTrack
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:88
NoteTracksSelectedFlag
const ReservedCommandFlag & NoteTracksSelectedFlag()
Definition: CommonCommandFlags.cpp:300
NoteTrack.h
Track::Any
bool Any() const
Definition: Track.cpp:370
ProjectSettings.h
ProjectAudioIO::Get
static ProjectAudioIO & Get(AudacityProject &project)
Definition: ProjectAudioIO.cpp:22
ProjectHistory::UndoAvailable
bool UndoAvailable() const
Definition: ProjectHistory.cpp:59
EditableTracksSelectedFlag
const ReservedCommandFlag & EditableTracksSelectedFlag()
Definition: CommonCommandFlags.cpp:181
AudioIOBase::IsPaused
bool IsPaused() const
Find out if playback / recording is currently paused.
Definition: AudioIOBase.cpp:341
WaveTracksSelectedFlag
const ReservedCommandFlag & WaveTracksSelectedFlag()
Definition: CommonCommandFlags.cpp:165
NoteTracksExistFlag
const ReservedCommandFlag & NoteTracksExistFlag()
Definition: CommonCommandFlags.cpp:294
cutCopyOptions
const CommandFlagOptions & cutCopyOptions()
Definition: CommonCommandFlags.cpp:74
ProjectSettings::IsSyncLocked
bool IsSyncLocked() const
Definition: ProjectSettings.cpp:175
ProjectHistory::RedoAvailable
bool RedoAvailable() const
Definition: ProjectHistory.cpp:68
ZoomInfo::ZoomInAvailable
bool ZoomInAvailable() const
Definition: ZoomInfo.cpp:70
CommandFlagOptions::QuickTest
CommandFlagOptions && QuickTest() &&
Definition: CommandFlag.h:52
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:196
noiseReductionOptions
const CommandFlagOptions noiseReductionOptions
Definition: CommonCommandFlags.cpp:109
ZoomInfo::ZoomOutAvailable
bool ZoomOutAvailable() const
Definition: ZoomInfo.cpp:75
PlayRegion::Locked
bool Locked() const
Definition: ViewInfo.h:130
AnyTracksSelectedFlag
const ReservedCommandFlag & AnyTracksSelectedFlag()
Definition: CommonCommandFlags.cpp:189
UndoAvailableFlag
const ReservedCommandFlag & UndoAvailableFlag()
Definition: CommonCommandFlags.cpp:242
format
int format
Definition: ExportPCM.cpp:56
AudioIOBase::Get
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:91
UndoManager.h
PausedFlag
const ReservedCommandFlag & PausedFlag()
Definition: CommonCommandFlags.cpp:333
TracksExistFlag
const ReservedCommandFlag & TracksExistFlag()
Definition: CommonCommandFlags.cpp:174
EditableTracksSelectedPred
bool EditableTracksSelectedPred(const AudacityProject &project)
Definition: CommonCommandFlags.cpp:46
ViewInfo.h
ZoomOutAvailableFlag
const ReservedCommandFlag & ZoomOutAvailableFlag()
Definition: CommonCommandFlags.cpp:264
CommandFlagOptions
Definition: CommandFlag.h:38
Menus.h
CaptureNotBusyFlag
const ReservedCommandFlag & CaptureNotBusyFlag()
Definition: CommonCommandFlags.cpp:214
ProjectWindows.h
accessors for certain important windows associated with each project
AudioIOBase::IsAudioTokenActive
bool IsAudioTokenActive(int token) const
Returns true if the stream is active, or even if audio I/O is busy cleaning up its data or writing to...
Definition: AudioIOBase.cpp:373
NonKeystrokeInterceptingWindow
Definition: CommandManagerWindowClasses.h:20
StereoRequiredFlag
const ReservedCommandFlag & StereoRequiredFlag()
Definition: CommonCommandFlags.cpp:140
TrackPanelHasFocus
const ReservedCommandFlag & TrackPanelHasFocus()
Definition: CommonCommandFlags.cpp:197
ZoomInAvailableFlag
const ReservedCommandFlag & ZoomInAvailableFlag()
Definition: CommonCommandFlags.cpp:254
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:506
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:239
Track::IsLeader
bool IsLeader() const
Definition: Track.cpp:379
UndoManager::Get
static UndoManager & Get(AudacityProject &project)
Definition: UndoManager.cpp:57
AudioIO.h
PlayableTracksExistFlag
const ReservedCommandFlag & PlayableTracksExistFlag()
Definition: CommonCommandFlags.cpp:340
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
TimeSelectedFlag
const ReservedCommandFlag & TimeSelectedFlag()
Definition: CommonCommandFlags.cpp:160
CommandManagerWindowClasses.h
ProjectHistory.h
IsNotSyncLockedFlag
const ReservedCommandFlag & IsNotSyncLockedFlag()
Definition: CommonCommandFlags.cpp:307
FindProjectFrame
wxFrame * FindProjectFrame(AudacityProject *project)
Get a pointer to the window associated with a project, or null if the given pointer is null,...
Definition: ProjectWindows.cpp:88
RedoAvailableFlag
const ReservedCommandFlag & RedoAvailableFlag()
Definition: CommonCommandFlags.cpp:248
TimeSelectedPred
bool TimeSelectedPred(const AudacityProject &project)
Definition: CommonCommandFlags.cpp:67
NotifyingSelectedRegion::isPoint
bool isPoint() const
Definition: ViewInfo.h:52
TrackList::Any
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1371
ProjectFileIO::IsModified
bool IsModified() const
Definition: ProjectFileIO.cpp:2210
NoiseReductionTimeSelectedFlag
const ReservedCommandFlag & NoiseReductionTimeSelectedFlag()
Definition: CommonCommandFlags.cpp:155
AudioIO::Get
static AudioIO * Get()
Definition: AudioIO.cpp:606
CommonCommandFlags.h
TrackList::Selected
auto Selected() -> TrackIterRange< TrackType >
Definition: Track.h:1388
NotMinimizedFlag
const ReservedCommandFlag & NotMinimizedFlag()
Definition: CommonCommandFlags.cpp:319
ProjectAudioIO.h
AudioTracksSelectedFlag
const ReservedCommandFlag & AudioTracksSelectedFlag()
Definition: CommonCommandFlags.cpp:353
AnyTracksSelectedPred
bool AnyTracksSelectedPred(const AudacityProject &project)
Definition: CommonCommandFlags.cpp:55
ProjectHistory::Get
static ProjectHistory & Get(AudacityProject &project)
Definition: ProjectHistory.cpp:26
NoteTrack
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:66