forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenus.cpp
813 lines (692 loc) · 23 KB
/
Menus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/**********************************************************************
Sneedacity: A Digital Audio Editor
Menus.cpp
Dominic Mazzoni
Brian Gunlogson
et al.
*******************************************************************//**
\file Menus.cpp
\brief Functions for building toobar menus and enabling and disabling items
*//****************************************************************//**
\class MenuCreator
\brief MenuCreator is responsible for creating the main menu bar.
*//****************************************************************//**
\class MenuManager
\brief MenuManager handles updates to menu state.
*//*******************************************************************/
#include "Menus.h"
#include <wx/frame.h>
#include "Project.h"
#include "ProjectHistory.h"
#include "ProjectSettings.h"
#include "UndoManager.h"
#include "commands/CommandManager.h"
#include "toolbars/ToolManager.h"
#include "widgets/SneedacityMessageBox.h"
#include "widgets/ErrorDialog.h"
#include <unordered_set>
#include <wx/menu.h>
#include <wx/windowptr.h>
MenuCreator::MenuCreator()
{
mLastAnalyzerRegistration = repeattypenone;
mLastToolRegistration = repeattypenone;
mRepeatGeneratorFlags = 0;
mRepeatEffectFlags = 0;
mRepeatAnalyzerFlags = 0;
mRepeatToolFlags = 0;
}
MenuCreator::~MenuCreator()
{
}
static const SneedacityProject::AttachedObjects::RegisteredFactory key{
[]( SneedacityProject &project ){
return std::make_shared< MenuManager >( project ); }
};
MenuManager &MenuManager::Get( SneedacityProject &project )
{
return project.AttachedObjects::Get< MenuManager >( key );
}
const MenuManager &MenuManager::Get( const SneedacityProject &project )
{
return Get( const_cast< SneedacityProject & >( project ) );
}
MenuManager::MenuManager( SneedacityProject &project )
: mProject{ project }
{
UpdatePrefs();
mProject.Bind( EVT_UNDO_OR_REDO, &MenuManager::OnUndoRedo, this );
mProject.Bind( EVT_UNDO_RESET, &MenuManager::OnUndoRedo, this );
mProject.Bind( EVT_UNDO_PUSHED, &MenuManager::OnUndoRedo, this );
mProject.Bind( EVT_UNDO_RENAMED, &MenuManager::OnUndoRedo, this );
}
MenuManager::~MenuManager()
{
mProject.Unbind( EVT_UNDO_OR_REDO, &MenuManager::OnUndoRedo, this );
mProject.Unbind( EVT_UNDO_RESET, &MenuManager::OnUndoRedo, this );
mProject.Unbind( EVT_UNDO_PUSHED, &MenuManager::OnUndoRedo, this );
}
void MenuManager::UpdatePrefs()
{
bool bSelectAllIfNone;
gPrefs->Read(wxT("/GUI/SelectAllOnNone"), &bSelectAllIfNone, false);
// 0 is grey out, 1 is Autoselect, 2 is Give warnings.
#ifdef EXPERIMENTAL_DA
// DA warns or greys out.
mWhatIfNoSelection = bSelectAllIfNone ? 2 : 0;
#else
// Sneedacity autoselects or warns.
mWhatIfNoSelection = bSelectAllIfNone ? 1 : 2;
#endif
mStopIfWasPaused = true; // not configurable for now, but could be later.
}
void MenuVisitor::BeginGroup( Registry::GroupItem &item, const Path &path )
{
bool isMenu = false;
bool isExtension = false;
auto pItem = &item;
if ( pItem->Transparent() ) {
}
else if ( dynamic_cast<MenuTable::MenuSection*>( pItem ) ) {
if ( !needSeparator.empty() )
needSeparator.back() = true;
}
else if ( auto pWhole = dynamic_cast<MenuTable::WholeMenu*>( pItem ) ) {
isMenu = true;
isExtension = pWhole->extension;
MaybeDoSeparator();
}
DoBeginGroup( item, path );
if ( isMenu ) {
needSeparator.push_back( false );
firstItem.push_back( !isExtension );
}
}
void MenuVisitor::EndGroup( Registry::GroupItem &item, const Path &path )
{
auto pItem = &item;
if ( pItem->Transparent() ) {
}
else if ( dynamic_cast<MenuTable::MenuSection*>( pItem ) ) {
if ( !needSeparator.empty() )
needSeparator.back() = true;
}
else if ( dynamic_cast<MenuTable::WholeMenu*>( pItem ) ) {
firstItem.pop_back();
needSeparator.pop_back();
}
DoEndGroup( item, path );
}
void MenuVisitor::Visit( Registry::SingleItem &item, const Path &path )
{
MaybeDoSeparator();
DoVisit( item, path );
}
void MenuVisitor::MaybeDoSeparator()
{
bool separate = false;
if ( !needSeparator.empty() ) {
separate = needSeparator.back() && !firstItem.back();
needSeparator.back() = false;
firstItem.back() = false;
}
if ( separate )
DoSeparator();
}
void MenuVisitor::DoBeginGroup( Registry::GroupItem &, const Path & )
{
}
void MenuVisitor::DoEndGroup( Registry::GroupItem &, const Path & )
{
}
void MenuVisitor::DoVisit( Registry::SingleItem &, const Path & )
{
}
void MenuVisitor::DoSeparator()
{
}
namespace MenuTable {
MenuItem::MenuItem( const Identifier &internalName,
const TranslatableString &title_, BaseItemPtrs &&items_ )
: ConcreteGroupItem< false, ToolbarMenuVisitor >{
internalName, std::move( items_ ) }, title{ title_ }
{
wxASSERT( !title.empty() );
}
MenuItem::~MenuItem() {}
ConditionalGroupItem::ConditionalGroupItem(
const Identifier &internalName, Condition condition_, BaseItemPtrs &&items_ )
: ConcreteGroupItem< false, ToolbarMenuVisitor >{
internalName, std::move( items_ ) }, condition{ condition_ }
{
}
ConditionalGroupItem::~ConditionalGroupItem() {}
CommandItem::CommandItem(const CommandID &name_,
const TranslatableString &label_in_,
CommandFunctorPointer callback_,
CommandFlag flags_,
const CommandManager::Options &options_,
CommandHandlerFinder finder_)
: SingleItem{ name_ }, label_in{ label_in_ }
, finder{ finder_ }, callback{ callback_ }
, flags{ flags_ }, options{ options_ }
{}
CommandItem::~CommandItem() {}
CommandGroupItem::CommandGroupItem(const Identifier &name_,
std::vector< ComponentInterfaceSymbol > items_,
CommandFunctorPointer callback_,
CommandFlag flags_,
bool isEffect_,
CommandHandlerFinder finder_)
: SingleItem{ name_ }, items{ std::move(items_) }
, finder{ finder_ }, callback{ callback_ }
, flags{ flags_ }, isEffect{ isEffect_ }
{}
CommandGroupItem::~CommandGroupItem() {}
SpecialItem::~SpecialItem() {}
MenuSection::~MenuSection() {}
WholeMenu::~WholeMenu() {}
CommandHandlerFinder FinderScope::sFinder =
[](SneedacityProject &project) -> CommandHandlerObject & {
// If this default finder function is reached, then FinderScope should
// have been used somewhere, or an explicit CommandHandlerFinder passed
// to menu item constructors
wxASSERT( false );
return project;
};
}
/// CreateMenusAndCommands builds the menus, and also rebuilds them after
/// changes in configured preferences - for example changes in key-bindings
/// affect the short-cut key legend that appears beside each command,
namespace {
using namespace Registry;
const auto MenuPathStart = wxT("MenuBar");
static Registry::GroupItem &sRegistry()
{
static Registry::TransparentGroupItem<> registry{ MenuPathStart };
return registry;
}
}
MenuTable::AttachedItem::AttachedItem(
const Placement &placement, BaseItemPtr pItem )
{
Registry::RegisterItem( sRegistry(), placement, std::move( pItem ) );
}
void MenuTable::DestroyRegistry()
{
sRegistry().items.clear();
}
namespace {
using namespace MenuTable;
struct MenuItemVisitor : ToolbarMenuVisitor
{
MenuItemVisitor( SneedacityProject &proj, CommandManager &man )
: ToolbarMenuVisitor(proj), manager( man ) {}
void DoBeginGroup( GroupItem &item, const Path& ) override
{
auto pItem = &item;
if (const auto pMenu =
dynamic_cast<MenuItem*>( pItem )) {
manager.BeginMenu( pMenu->title );
}
else
if (const auto pConditionalGroup =
dynamic_cast<ConditionalGroupItem*>( pItem )) {
const auto flag = pConditionalGroup->condition();
if (!flag)
manager.BeginOccultCommands();
// to avoid repeated call of condition predicate in EndGroup():
flags.push_back(flag);
}
else
if ( pItem->Transparent() ) {
}
else
if ( const auto pGroup = dynamic_cast<MenuSection*>( pItem ) ) {
}
else
wxASSERT( false );
}
void DoEndGroup( GroupItem &item, const Path& ) override
{
auto pItem = &item;
if (const auto pMenu =
dynamic_cast<MenuItem*>( pItem )) {
manager.EndMenu();
}
else
if (const auto pConditionalGroup =
dynamic_cast<ConditionalGroupItem*>( pItem )) {
const bool flag = flags.back();
if (!flag)
manager.EndOccultCommands();
flags.pop_back();
}
else
if ( pItem->Transparent() ) {
}
else
if ( const auto pGroup = dynamic_cast<MenuSection*>( pItem ) ) {
}
else
wxASSERT( false );
}
void DoVisit( SingleItem &item, const Path& ) override
{
const auto pCurrentMenu = manager.CurrentMenu();
if ( !pCurrentMenu ) {
// There may have been a mistake in the placement hint that registered
// this single item. It's not within any menu.
wxASSERT( false );
return;
}
auto pItem = &item;
if (const auto pCommand =
dynamic_cast<CommandItem*>( pItem )) {
manager.AddItem( project,
pCommand->name, pCommand->label_in,
pCommand->finder, pCommand->callback,
pCommand->flags, pCommand->options
);
}
else
if (const auto pCommandList =
dynamic_cast<CommandGroupItem*>( pItem ) ) {
manager.AddItemList(pCommandList->name,
pCommandList->items.data(), pCommandList->items.size(),
pCommandList->finder, pCommandList->callback,
pCommandList->flags, pCommandList->isEffect);
}
else
if (const auto pSpecial =
dynamic_cast<SpecialItem*>( pItem )) {
wxASSERT( pCurrentMenu );
pSpecial->fn( project, *pCurrentMenu );
}
else
wxASSERT( false );
}
void DoSeparator() override
{
manager.AddSeparator();
}
CommandManager &manager;
std::vector<bool> flags;
};
}
void MenuCreator::CreateMenusAndCommands(SneedacityProject &project)
{
// Once only, cause initial population of preferences for the ordering
// of some menu items that used to be given in tables but are now separately
// registered in several .cpp files; the sequence of registration depends
// on unspecified accidents of static initialization order across
// compilation units, so we need something specific here to preserve old
// default appearance of menus.
// But this needs only to mention some strings -- there is no compilation or
// link dependency of this source file on those other implementation files.
static Registry::OrderingPreferenceInitializer init{
MenuPathStart,
{
{wxT(""), wxT(
"File,Edit,Select,View,Transport,Tracks,Generate,Effect,Analyze,Tools,Window,Optional,Help"
)},
{wxT("/Optional/Extra/Part1"), wxT(
"Transport,Tools,Mixer,Edit,PlayAtSpeed,Seek,Device,Select"
)},
{wxT("/Optional/Extra/Part2"), wxT(
"Navigation,Focus,Cursor,Track,Scriptables1,Scriptables2"
)},
{wxT("/View/Windows"), wxT("UndoHistory,Karaoke,MixerBoard")},
{wxT("/Analyze/Analyzers/Windows"), wxT("ContrastAnalyser,PlotSpectrum")},
{wxT("/Transport/Basic"), wxT("Play,Record,Scrubbing,Cursor")},
{wxT("/View/Other/Toolbars/Toolbars/Other"), wxT(
"ShowTransportTB,ShowToolsTB,ShowRecordMeterTB,ShowPlayMeterTB,"
//"ShowMeterTB,"
"ShowMixerTB,"
"ShowEditTB,ShowTranscriptionTB,ShowScrubbingTB,ShowDeviceTB,ShowSelectionTB,"
"ShowSpectralSelectionTB") }
}
};
auto &commandManager = CommandManager::Get( project );
// The list of defaults to exclude depends on
// preference wxT("/GUI/Shortcuts/FullDefaults"), which may have changed.
commandManager.SetMaxList();
auto menubar = commandManager.AddMenuBar(wxT("appmenu"));
wxASSERT(menubar);
MenuItemVisitor visitor{ project, commandManager };
MenuManager::Visit( visitor );
GetProjectFrame( project ).SetMenuBar(menubar.release());
mLastFlags = AlwaysEnabledFlag;
#if defined(_DEBUG)
// c->CheckDups();
#endif
}
void MenuManager::Visit( ToolbarMenuVisitor &visitor )
{
static const auto menuTree = MenuTable::Items( MenuPathStart );
Registry::Visit( visitor, menuTree.get(), &sRegistry() );
}
// TODO: This surely belongs in CommandManager?
void MenuManager::ModifyUndoMenuItems(SneedacityProject &project)
{
TranslatableString desc;
auto &undoManager = UndoManager::Get( project );
auto &commandManager = CommandManager::Get( project );
int cur = undoManager.GetCurrentState();
if (undoManager.UndoAvailable()) {
undoManager.GetShortDescription(cur, &desc);
commandManager.Modify(wxT("Undo"),
XXO("&Undo %s")
.Format( desc ));
commandManager.Enable(wxT("Undo"),
ProjectHistory::Get( project ).UndoAvailable());
}
else {
commandManager.Modify(wxT("Undo"),
XXO("&Undo"));
}
if (undoManager.RedoAvailable()) {
undoManager.GetShortDescription(cur+1, &desc);
commandManager.Modify(wxT("Redo"),
XXO("&Redo %s")
.Format( desc ));
commandManager.Enable(wxT("Redo"),
ProjectHistory::Get( project ).RedoAvailable());
}
else {
commandManager.Modify(wxT("Redo"),
XXO("&Redo"));
commandManager.Enable(wxT("Redo"), false);
}
}
// Get hackcess to a protected method
class wxFrameEx : public wxFrame
{
public:
using wxFrame::DetachMenuBar;
};
void MenuCreator::RebuildMenuBar(SneedacityProject &project)
{
// On OSX, we can't rebuild the menus while a modal dialog is being shown
// since the enabled state for menus like Quit and Preference gets out of
// sync with wxWidgets idea of what it should be.
#if defined(__WXMAC__) && defined(_DEBUG)
{
wxDialog *dlg =
wxDynamicCast(wxGetTopLevelParent(wxWindow::FindFocus()), wxDialog);
wxASSERT((!dlg || !dlg->IsModal()));
}
#endif
// Delete the menus, since we will soon recreate them.
// Rather oddly, the menus don't vanish as a result of doing this.
{
auto &window = static_cast<wxFrameEx&>( GetProjectFrame( project ) );
wxWindowPtr<wxMenuBar> menuBar{ window.GetMenuBar() };
window.DetachMenuBar();
// menuBar gets deleted here
}
CommandManager::Get( project ).PurgeData();
CreateMenusAndCommands(project);
}
void MenuManager::OnUndoRedo( wxCommandEvent &evt )
{
evt.Skip();
ModifyUndoMenuItems( mProject );
UpdateMenus();
}
namespace{
using Predicates = std::vector< ReservedCommandFlag::Predicate >;
Predicates &RegisteredPredicates()
{
static Predicates thePredicates;
return thePredicates;
}
std::vector< CommandFlagOptions > &Options()
{
static std::vector< CommandFlagOptions > options;
return options;
}
}
ReservedCommandFlag::ReservedCommandFlag(
const Predicate &predicate, const CommandFlagOptions &options )
{
static size_t sNextReservedFlag = 0;
// This will throw std::out_of_range if the constant NCommandFlags is too
// small
set( sNextReservedFlag++ );
RegisteredPredicates().emplace_back( predicate );
Options().emplace_back( options );
}
CommandFlag MenuManager::GetUpdateFlags( bool checkActive ) const
{
// This method determines all of the flags that determine whether
// certain menu items and commands should be enabled or disabled,
// and returns them in a bitfield. Note that if none of the flags
// have changed, it's not necessary to even check for updates.
// static variable, used to remember flags for next time.
static CommandFlag lastFlags;
CommandFlag flags, quickFlags;
const auto &options = Options();
size_t ii = 0;
for ( const auto &predicate : RegisteredPredicates() ) {
if ( options[ii].quickTest ) {
quickFlags[ii] = true;
if( predicate( mProject ) )
flags[ii] = true;
}
++ii;
}
if ( checkActive && !GetProjectFrame( mProject ).IsActive() )
// quick 'short-circuit' return.
flags = (lastFlags & ~quickFlags) | flags;
else {
ii = 0;
for ( const auto &predicate : RegisteredPredicates() ) {
if ( !options[ii].quickTest && predicate( mProject ) )
flags[ii] = true;
++ii;
}
}
lastFlags = flags;
return flags;
}
void MenuManager::ModifyAllProjectToolbarMenus()
{
for (auto pProject : AllProjects{}) {
auto &project = *pProject;
MenuManager::Get(project).ModifyToolbarMenus(project);
}
}
void MenuManager::ModifyToolbarMenus(SneedacityProject &project)
{
// Refreshes can occur during shutdown and the toolmanager may already
// be deleted, so protect against it.
auto &toolManager = ToolManager::Get( project );
auto &settings = ProjectSettings::Get( project );
// Now, go through each toolbar, and call EnableDisableButtons()
for (int i = 0; i < ToolBarCount; i++) {
auto bar = toolManager.GetToolBar(i);
if (bar)
bar->EnableDisableButtons();
}
// These don't really belong here, but it's easier and especially so for
// the Edit toolbar and the sync-lock menu item.
bool active;
gPrefs->Read(wxT("/GUI/SyncLockTracks"), &active, false);
settings.SetSyncLock(active);
CommandManager::Get( project ).UpdateCheckmarks( project );
}
namespace
{
using MenuItemEnablers = std::vector<MenuItemEnabler>;
MenuItemEnablers &Enablers()
{
static MenuItemEnablers enablers;
return enablers;
}
}
RegisteredMenuItemEnabler::RegisteredMenuItemEnabler(
const MenuItemEnabler &enabler )
{
Enablers().emplace_back( enabler );
}
// checkActive is a temporary hack that should be removed as soon as we
// get multiple effect preview working
void MenuManager::UpdateMenus( bool checkActive )
{
auto &project = mProject;
auto flags = GetUpdateFlags(checkActive);
// Return from this function if nothing's changed since
// the last time we were here.
if (flags == mLastFlags)
return;
mLastFlags = flags;
auto flags2 = flags;
// We can enable some extra items if we have select-all-on-none.
//EXPLAIN-ME: Why is this here rather than in GetUpdateFlags()?
//ANSWER: Because flags2 is used in the menu enable/disable.
//The effect still needs flags to determine whether it will need
//to actually do the 'select all' to make the command valid.
for ( const auto &enabler : Enablers() ) {
auto actual = enabler.actualFlags();
if (
enabler.applicable( project ) && (flags & actual) == actual
)
flags2 |= enabler.possibleFlags();
}
auto &commandManager = CommandManager::Get( project );
// With select-all-on-none, some items that we don't want enabled may have
// been enabled, since we changed the flags. Here we manually disable them.
// 0 is grey out, 1 is Autoselect, 2 is Give warnings.
commandManager.EnableUsingFlags(
flags2, // the "lax" flags
(mWhatIfNoSelection == 0 ? flags2 : flags) // the "strict" flags
);
MenuManager::ModifyToolbarMenus(project);
}
/// The following method moves to the previous track
/// selecting and unselecting depending if you are on the start of a
/// block or not.
void MenuCreator::RebuildAllMenuBars()
{
for( auto p : AllProjects{} ) {
MenuManager::Get(*p).RebuildMenuBar(*p);
#if defined(__WXGTK__)
// Workaround for:
//
// http://bugzilla.sneedacityteam.org/show_bug.cgi?id=458
//
// This workaround should be removed when Sneedacity updates to wxWidgets 3.x which has a fix.
auto &window = GetProjectFrame( *p );
wxRect r = window.GetRect();
window.SetSize(wxSize(1,1));
window.SetSize(r.GetSize());
#endif
}
}
bool MenuManager::ReportIfActionNotAllowed(
const TranslatableString & Name, CommandFlag & flags, CommandFlag flagsRqd )
{
auto &project = mProject;
bool bAllowed = TryToMakeActionAllowed( flags, flagsRqd );
if( bAllowed )
return true;
auto &cm = CommandManager::Get( project );
TellUserWhyDisallowed( Name, flags & flagsRqd, flagsRqd);
return false;
}
/// Determines if flags for command are compatible with current state.
/// If not, then try some recovery action to make it so.
/// @return whether compatible or not after any actions taken.
bool MenuManager::TryToMakeActionAllowed(
CommandFlag & flags, CommandFlag flagsRqd )
{
auto &project = mProject;
if( flags.none() )
flags = GetUpdateFlags();
// Visit the table of recovery actions
auto &enablers = Enablers();
auto iter = enablers.begin(), end = enablers.end();
while ((flags & flagsRqd) != flagsRqd && iter != end) {
const auto &enabler = *iter;
auto actual = enabler.actualFlags();
auto MissingFlags = (~flags & flagsRqd);
if (
// Do we have the right precondition?
(flags & actual) == actual
&&
// Can we get the condition we need?
(MissingFlags & enabler.possibleFlags()).any()
) {
// Then try the function
enabler.tryEnable( project, flagsRqd );
flags = GetUpdateFlags();
}
++iter;
}
return (flags & flagsRqd) == flagsRqd;
}
void MenuManager::TellUserWhyDisallowed(
const TranslatableString & Name, CommandFlag flagsGot, CommandFlag flagsRequired )
{
// The default string for 'reason' is a catch all. I hope it won't ever be seen
// and that we will get something more specific.
auto reason = XO("There was a problem with your last action. If you think\nthis is a bug, please tell us exactly where it occurred.");
// The default title string is 'Disallowed'.
auto untranslatedTitle = XO("Disallowed");
wxString helpPage;
bool enableDefaultMessage = true;
bool defaultMessage = true;
auto doOption = [&](const CommandFlagOptions &options) {
if ( options.message ) {
reason = options.message( Name );
defaultMessage = false;
if ( !options.title.empty() )
untranslatedTitle = options.title;
helpPage = options.helpPage;
return true;
}
else {
enableDefaultMessage =
enableDefaultMessage && options.enableDefaultMessage;
return false;
}
};
const auto &alloptions = Options();
auto missingFlags = flagsRequired & ~flagsGot;
// Find greatest priority
unsigned priority = 0;
for ( const auto &options : alloptions )
priority = std::max( priority, options.priority );
// Visit all unsatisfied conditions' options, by descending priority,
// stopping when we find a message
++priority;
while( priority-- ) {
size_t ii = 0;
for ( const auto &options : alloptions ) {
if (
priority == options.priority
&&
missingFlags[ii]
&&
doOption( options ) )
goto done;
++ii;
}
}
done:
if (
// didn't find a message
defaultMessage
&&
// did find a condition that suppresses the default message
!enableDefaultMessage
)
return;
// Does not have the warning icon...
ShowErrorDialog(
NULL,
untranslatedTitle,
reason,
helpPage);
}