Audacity 3.2.0
CommandFlag.h
Go to the documentation of this file.
1//
2// CommandFlag.h
3// Audacity
4//
5// Created by Paul Licameli on 11/22/16.
6//
7//
8
9#ifndef __AUDACITY_COMMAND_FLAG__
10#define __AUDACITY_COMMAND_FLAG__
11
12// Flags used in command handling.
13
14#include "TranslatableString.h"
15
16#include <bitset>
17#include <functional>
18#include <utility>
19
20class AudacityProject;
21
22// Increase the template parameter as needed to allow more flags
23constexpr size_t NCommandFlags = 64;
24static_assert(
25 NCommandFlags <= 8 * sizeof( unsigned long long ),
26 "NoFlagsSpecified may have incorrect value"
27);
28
29// Type to specify conditions for enabling of a menu item
30using CommandFlag = std::bitset<NCommandFlags>;
31
32// Special constant values
33constexpr CommandFlag
34 AlwaysEnabledFlag{}, // all zeroes
35 NoFlagsSpecified{ ~0ULL }; // all ones
36
38 // Supplied the translated name of the command, returns a translated
39 // error message
41 std::function< TranslatableString( const TranslatableString& ) >;
42
43 CommandFlagOptions() = default;
45 const MessageFormatter &message_,
46 const wxString &helpPage_ = {},
47 const TranslatableString &title_ = {}
48 ) : message{ message_ }, helpPage{ helpPage_ }, title{ title_ }
49 {}
50
52 { quickTest = true; return std::move( *this ); }
54 { enableDefaultMessage = false; return std::move( *this ); }
55 CommandFlagOptions && Priority( unsigned priority_ ) &&
56 { priority = priority_; return std::move( *this ); }
57
58 // null, or else computes non-default message for the dialog box when the
59 // condition is not satisfied for the selected command
61
62 // Title and help page are used only if a message function is given
63 wxString helpPage;
64
65 // Empty, or non-default title for the dialog box when the
66 // condition is not satisfied for the selected command
67 // This string must be given UN-translated.
69
70 // Conditions with higher "priority" are preferred over others in choosing
71 // the help message
72 unsigned priority = 0;
73
74 // If false, and no other condition with a message is unsatisfied, then
75 // display no dialog box at all when this condition is not satisfied
77
78 // If true, assume this is a cheap test to be done always. If false, the
79 // test may be skipped and the condition assumed to be unchanged since the
80 // last more comprehensive testing
81 bool quickTest = false;
82};
83
84// Construct one statically to register (and reserve) a bit position in the set
85// an associate it with a test function; those with quickTest = true are cheap
86// to compute and always checked
87class MENUS_API ReservedCommandFlag : public CommandFlag
88{
89public:
90 using Predicate = std::function< bool( const AudacityProject& ) >;
91 using Predicates = std::vector< Predicate >;
92
93 static const std::vector< CommandFlagOptions > &Options();
94 static const Predicates &RegisteredPredicates();
95
96 ReservedCommandFlag( const Predicate &predicate,
97 const CommandFlagOptions &options = {} );
98};
99
100// To describe auto-selection, stop-if-paused, etc.:
101// A structure describing a set of conditions, another set that might be
102// made true given the first, and the function that may make them true.
103// If a menu item requires the second set, while the first set is true,
104// then the enabler will be invoked (unless the menu item is constructed with
105// the useStrictFlags option, or the applicability test first returns false).
106// The item's full set of required flags is passed to the function.
107
108// Computation of the flags is delayed inside a function -- because often you
109// need to name a statically allocated CommandFlag, or a bitwise OR of some,
110// while they may not have been initialized yet, during static initialization.
112 using Flags = std::function< CommandFlag() >;
113 using Test = std::function< bool( const AudacityProject& ) >;
114 using Action = std::function< void( AudacityProject&, CommandFlag ) >;
115
120};
121
122using MenuItemEnablers = std::vector<MenuItemEnabler>;
123
124// Typically this is statically constructed:
126 static const MenuItemEnablers &Enablers();
128};
129
130
131#endif
constexpr size_t NCommandFlags
Definition: CommandFlag.h:23
constexpr CommandFlag AlwaysEnabledFlag
Definition: CommandFlag.h:34
std::bitset< NCommandFlags > CommandFlag
Definition: CommandFlag.h:30
std::vector< MenuItemEnabler > MenuItemEnablers
Definition: CommandFlag.h:122
constexpr CommandFlag NoFlagsSpecified
Definition: CommandFlag.h:35
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
std::function< bool(const AudacityProject &) > Predicate
Definition: CommandFlag.h:90
std::vector< Predicate > Predicates
Definition: CommandFlag.h:91
Holds a msgid for the translation catalog; may also bind format arguments.
CommandFlagOptions && DisableDefaultMessage() &&
Definition: CommandFlag.h:53
CommandFlagOptions()=default
CommandFlagOptions && Priority(unsigned priority_) &&
Definition: CommandFlag.h:55
std::function< TranslatableString(const TranslatableString &) > MessageFormatter
Definition: CommandFlag.h:41
MessageFormatter message
Definition: CommandFlag.h:60
CommandFlagOptions && QuickTest() &&
Definition: CommandFlag.h:51
TranslatableString title
Definition: CommandFlag.h:68
CommandFlagOptions(const MessageFormatter &message_, const wxString &helpPage_={}, const TranslatableString &title_={})
Definition: CommandFlag.h:44
std::function< bool(const AudacityProject &) > Test
Definition: CommandFlag.h:113
const Flags actualFlags
Definition: CommandFlag.h:116
std::function< CommandFlag() > Flags
Definition: CommandFlag.h:112
const Flags possibleFlags
Definition: CommandFlag.h:117
std::function< void(AudacityProject &, CommandFlag) > Action
Definition: CommandFlag.h:114