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
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <map>
#include <list>
#include <stack>
#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptor/map.hpp>
#include <wx/event.h>
#include <wx/clipbrd.h>
#include <view/view.h>
#include <view/view_controls.h>
#include <tool/tool_base.h>
#include <tool/tool_interactive.h>
#include <tool/tool_manager.h>
#include <tool/context_menu.h>
#include <tool/coroutine.h>
#include <tool/action_manager.h>
#include <wxPcbStruct.h>
#include <confirm.h>
#include <class_draw_panel_gal.h>
using boost::optional;
/// Struct describing the current execution state of a TOOL
struct TOOL_MANAGER::TOOL_STATE
{
TOOL_STATE( TOOL_BASE* aTool ) :
theTool( aTool )
{
clear();
}
TOOL_STATE( const TOOL_STATE& aState )
{
theTool = aState.theTool;
idle = aState.idle;
pendingWait = aState.pendingWait;
pendingContextMenu = aState.pendingContextMenu;
contextMenu = aState.contextMenu;
contextMenuTrigger = aState.contextMenuTrigger;
cofunc = aState.cofunc;
wakeupEvent = aState.wakeupEvent;
waitEvents = aState.waitEvents;
transitions = aState.transitions;
// do not copy stateStack
}
~TOOL_STATE()
{
assert( stateStack.empty() );
}
/// The tool itself
TOOL_BASE* theTool;
/// Is the tool active (pending execution) or disabled at the moment
bool idle;
/// Flag defining if the tool is waiting for any event (i.e. if it
/// issued a Wait() call).
bool pendingWait;
/// Is there a context menu being displayed
bool pendingContextMenu;
/// Context menu currently used by the tool
CONTEXT_MENU* contextMenu;
/// Defines when the context menu is opened
CONTEXT_MENU_TRIGGER contextMenuTrigger;
/// Tool execution context
COROUTINE<int, const TOOL_EVENT&>* cofunc;
/// The event that triggered the execution/wakeup of the tool after Wait() call
TOOL_EVENT wakeupEvent;
/// List of events the tool is currently waiting for
TOOL_EVENT_LIST waitEvents;
/// List of possible transitions (ie. association of events and state handlers that are executed
/// upon the event reception
std::vector<TRANSITION> transitions;
void operator=( const TOOL_STATE& aState )
{
theTool = aState.theTool;
idle = aState.idle;
pendingWait = aState.pendingWait;
pendingContextMenu = aState.pendingContextMenu;
contextMenu = aState.contextMenu;
contextMenuTrigger = aState.contextMenuTrigger;
cofunc = aState.cofunc;
wakeupEvent = aState.wakeupEvent;
waitEvents = aState.waitEvents;
transitions = aState.transitions;
// do not copy stateStack
}
bool operator==( const TOOL_MANAGER::TOOL_STATE& aRhs ) const
{
return aRhs.theTool == this->theTool;
}
bool operator!=( const TOOL_MANAGER::TOOL_STATE& aRhs ) const
{
return aRhs.theTool != this->theTool;
}
/**
* Function Push()
* Stores the current state of the tool on stack. Stacks are stored internally and are not
* shared between different TOOL_STATE objects.
*/
void Push()
{
stateStack.push( new TOOL_STATE( *this ) );
clear();
}
/**
* Function Pop()
* Restores state of the tool from stack. Stacks are stored internally and are not
* shared between different TOOL_STATE objects.
* @return True if state was restored, false if the stack was empty.
*/
bool Pop()
{
delete cofunc;
if( !stateStack.empty() )
{
*this = *stateStack.top();
delete stateStack.top();
stateStack.pop();
return true;
}
else
{
cofunc = NULL;
return false;
}
}
private:
///> Stack preserving previous states of a TOOL.
std::stack<TOOL_STATE*> stateStack;
///> Restores the initial state.
void clear()
{
idle = true;
pendingWait = false;
pendingContextMenu = false;
cofunc = NULL;
contextMenu = NULL;
contextMenuTrigger = CMENU_OFF;
transitions.clear();
}
};
TOOL_MANAGER::TOOL_MANAGER() :
m_model( NULL ),
m_view( NULL ),
m_viewControls( NULL ),
m_editFrame( NULL ),
m_passEvent( false )
{
m_actionMgr = new ACTION_MANAGER( this );
}
TOOL_MANAGER::~TOOL_MANAGER()
{
std::map<TOOL_BASE*, TOOL_STATE*>::iterator it, it_end;
for( it = m_toolState.begin(), it_end = m_toolState.end(); it != it_end; ++it )
{
delete it->second->cofunc; // delete cofunction
delete it->second; // delete TOOL_STATE
delete it->first; // delete the tool itself
}
delete m_actionMgr;
}
void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool )
{
wxASSERT_MSG( m_toolNameIndex.find( aTool->GetName() ) == m_toolNameIndex.end(),
wxT( "Adding two tools with the same name may result in unexpected behaviour.") );
wxASSERT_MSG( m_toolIdIndex.find( aTool->GetId() ) == m_toolIdIndex.end(),
wxT( "Adding two tools with the same ID may result in unexpected behaviour.") );
wxASSERT_MSG( m_toolTypes.find( typeid( *aTool ).name() ) == m_toolTypes.end(),
wxT( "Adding two tools of the same type may result in unexpected behaviour.") );
TOOL_STATE* st = new TOOL_STATE( aTool );
m_toolState[aTool] = st;
m_toolNameIndex[aTool->GetName()] = st;
m_toolIdIndex[aTool->GetId()] = st;
m_toolTypes[typeid( *aTool ).name()] = st->theTool;
aTool->attachManager( this );
if( !aTool->Init() )
{
std::string msg = StrPrintf( "Initialization of the %s tool failed",
aTool->GetName().c_str() );
DisplayError( NULL, wxString::FromUTF8( msg.c_str() ) );
// Unregister the tool
m_toolState.erase( aTool );
m_toolNameIndex.erase( aTool->GetName() );
m_toolIdIndex.erase( aTool->GetId() );
m_toolTypes.erase( typeid( *aTool ).name() );
delete st;
delete aTool;
}
}
bool TOOL_MANAGER::InvokeTool( TOOL_ID aToolId )
{
TOOL_BASE* tool = FindTool( aToolId );
if( tool && tool->GetType() == INTERACTIVE )
return invokeTool( tool );
return false; // there is no tool with the given id
}
bool TOOL_MANAGER::InvokeTool( const std::string& aToolName )
{
TOOL_BASE* tool = FindTool( aToolName );
if( tool && tool->GetType() == INTERACTIVE )
return invokeTool( tool );
return false; // there is no tool with the given name
}
void TOOL_MANAGER::RegisterAction( TOOL_ACTION* aAction )
{
m_actionMgr->RegisterAction( aAction );
}
void TOOL_MANAGER::UnregisterAction( TOOL_ACTION* aAction )
{
m_actionMgr->UnregisterAction( aAction );
}
bool TOOL_MANAGER::RunAction( const std::string& aActionName, bool aNow, void* aParam )
{
TOOL_ACTION* action = m_actionMgr->FindAction( aActionName );
if( action )
{
TOOL_EVENT event = action->MakeEvent();
// Allow to override the action parameter
if( aParam )
event.SetParameter( aParam );
if( aNow )
ProcessEvent( event );
else
PostEvent( event );
return true;
}
wxASSERT_MSG( action != NULL, wxString::Format( wxT( "Could not find action %s." ), aActionName ) );
return false;
}
void TOOL_MANAGER::RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam )
{
TOOL_EVENT event = aAction.MakeEvent();
// Allow to override the action parameter
if( aParam )
event.SetParameter( aParam );
if( aNow )
ProcessEvent( event );
else
PostEvent( event );
}
int TOOL_MANAGER::GetHotKey( const TOOL_ACTION& aAction )
{
return m_actionMgr->GetHotKey( aAction );
}
void TOOL_MANAGER::UpdateHotKeys()
{
m_actionMgr->UpdateHotKeys();
}
bool TOOL_MANAGER::invokeTool( TOOL_BASE* aTool )
{
wxASSERT( aTool != NULL );
TOOL_EVENT evt( TC_COMMAND, TA_ACTIVATE, aTool->GetName() );
ProcessEvent( evt );
return true;
}
bool TOOL_MANAGER::runTool( TOOL_ID aToolId )
{
TOOL_BASE* tool = FindTool( aToolId );
if( tool && tool->GetType() == INTERACTIVE )
return runTool( tool );
return false; // there is no tool with the given id
}
bool TOOL_MANAGER::runTool( const std::string& aToolName )
{
TOOL_BASE* tool = FindTool( aToolName );
if( tool && tool->GetType() == INTERACTIVE )
return runTool( tool );
return false; // there is no tool with the given name
}
bool TOOL_MANAGER::runTool( TOOL_BASE* aTool )
{
wxASSERT( aTool != NULL );
if( !isRegistered( aTool ) )
{
wxASSERT_MSG( false, wxT( "You cannot run unregistered tools" ) );
return false;
}
// If the tool is already active, bring it to the top of the active tools stack
if( isActive( aTool ) )
{
m_activeTools.erase( std::find( m_activeTools.begin(), m_activeTools.end(),
aTool->GetId() ) );
m_activeTools.push_front( aTool->GetId() );
return false;
}
aTool->Reset( TOOL_INTERACTIVE::RUN );
aTool->SetTransitions();
// Add the tool on the front of the processing queue (it gets events first)
m_activeTools.push_front( aTool->GetId() );
return true;
}
TOOL_BASE* TOOL_MANAGER::FindTool( int aId ) const
{
std::map<TOOL_ID, TOOL_STATE*>::const_iterator it = m_toolIdIndex.find( aId );
if( it != m_toolIdIndex.end() )
return it->second->theTool;
return NULL;
}
TOOL_BASE* TOOL_MANAGER::FindTool( const std::string& aName ) const
{
std::map<std::string, TOOL_STATE*>::const_iterator it = m_toolNameIndex.find( aName );
if( it != m_toolNameIndex.end() )
return it->second->theTool;
return NULL;
}
void TOOL_MANAGER::DeactivateTool()
{
TOOL_EVENT evt( TC_COMMAND, TA_ACTIVATE, "" ); // deactivate the active tool
ProcessEvent( evt );
}
void TOOL_MANAGER::ResetTools( TOOL_BASE::RESET_REASON aReason )
{
DeactivateTool();
BOOST_FOREACH( TOOL_BASE* tool, m_toolState | boost::adaptors::map_keys )
{
tool->Reset( aReason );
tool->SetTransitions();
}
}
int TOOL_MANAGER::GetPriority( int aToolId ) const
{
int priority = 0;
for( std::list<TOOL_ID>::const_iterator it = m_activeTools.begin(),
itEnd = m_activeTools.end(); it != itEnd; ++it )
{
if( *it == aToolId )
return priority;
++priority;
}
return -1;
}
void TOOL_MANAGER::ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
const TOOL_EVENT_LIST& aConditions )
{
TOOL_STATE* st = m_toolState[aTool];
st->transitions.push_back( TRANSITION( aConditions, aHandler ) );
}
optional<TOOL_EVENT> TOOL_MANAGER::ScheduleWait( TOOL_BASE* aTool,
const TOOL_EVENT_LIST& aConditions )
{
TOOL_STATE* st = m_toolState[aTool];
assert( !st->pendingWait ); // everything collapses on two Yield() in a row
// indicate to the manager that we are going to sleep and we shall be
// woken up when an event matching aConditions arrive
st->pendingWait = true;
st->waitEvents = aConditions;
// switch context back to event dispatcher loop
st->cofunc->Yield();
return st->wakeupEvent;
}
void TOOL_MANAGER::dispatchInternal( const TOOL_EVENT& aEvent )
{
// iterate over all registered tools
for( std::list<TOOL_ID>::iterator it = m_activeTools.begin();
it != m_activeTools.end(); /* iteration is done inside */)
{
std::list<TOOL_ID>::iterator curIt = it;
TOOL_STATE* st = m_toolIdIndex[*it];
++it; // it might be overwritten, if the tool is removed the m_activeTools list
// the tool state handler is waiting for events (i.e. called Wait() method)
if( st->pendingWait )
{
if( st->waitEvents.Matches( aEvent ) )
{
// By default, only messages are passed further
m_passEvent = ( aEvent.Category() == TC_MESSAGE );
// got matching event? clear wait list and wake up the coroutine
st->wakeupEvent = aEvent;
st->pendingWait = false;
st->waitEvents.clear();
if( st->cofunc && !st->cofunc->Resume() )
{
if( finishTool( st, false ) ) // The couroutine has finished
it = m_activeTools.erase( curIt );
}
// If the tool did not request to propagate
// the event to other tools, we should stop it now
if( !m_passEvent )
break;
}
}
}
BOOST_FOREACH( TOOL_STATE* st, m_toolState | boost::adaptors::map_values )
{
// no state handler in progress - check if there are any transitions (defined by
// Go() method that match the event.
if( !st->pendingWait && !st->transitions.empty() )
{
BOOST_FOREACH( TRANSITION& tr, st->transitions )
{
if( tr.first.Matches( aEvent ) )
{
// if there is already a context, then store it
if( st->cofunc )
st->Push();
// as the state changes, the transition table has to be set up again
st->transitions.clear();
st->cofunc = new COROUTINE<int, const TOOL_EVENT&>( tr.second );
// got match? Run the handler.
st->cofunc->Call( aEvent );
if( !st->cofunc->Running() )
finishTool( st ); // The couroutine has finished immediately?
// there is no point in further checking, as transitions got cleared
break;
}
}
}
}
}
bool TOOL_MANAGER::dispatchStandardEvents( const TOOL_EVENT& aEvent )
{
if( aEvent.Action() == TA_KEY_PRESSED )
{
// Check if there is a hotkey associated
if( m_actionMgr->RunHotKey( aEvent.Modifier() | aEvent.KeyCode() ) )
return false; // hotkey event was handled so it does not go any further
}
return true;
}
bool TOOL_MANAGER::dispatchActivation( const TOOL_EVENT& aEvent )
{
if( aEvent.IsActivate() )
{
std::map<std::string, TOOL_STATE*>::iterator tool = m_toolNameIndex.find( *aEvent.GetCommandStr() );
if( tool != m_toolNameIndex.end() )
{
runTool( tool->second->theTool );
return true;
}
}
return false;
}
void TOOL_MANAGER::dispatchContextMenu( const TOOL_EVENT& aEvent )
{
BOOST_FOREACH( TOOL_ID toolId, m_activeTools )
{
TOOL_STATE* st = m_toolIdIndex[toolId];
// the tool requested a context menu. The menu is activated on RMB click (CMENU_BUTTON mode)
// or immediately (CMENU_NOW) mode. The latter is used for clarification lists.
if( st->contextMenuTrigger != CMENU_OFF )
{
if( st->contextMenuTrigger == CMENU_BUTTON && !aEvent.IsClick( BUT_RIGHT ) )
break;
st->pendingWait = true;
st->waitEvents = TOOL_EVENT( TC_ANY, TA_ANY );
// Store the menu pointer in case it is changed by the TOOL when handling menu events
CONTEXT_MENU* m = st->contextMenu;
if( st->contextMenuTrigger == CMENU_NOW )
st->contextMenuTrigger = CMENU_OFF;
// Temporarily store the cursor position, so the tools could execute actions
// using the point where the user has invoked a context menu
bool forcedCursor = m_viewControls->IsCursorPositionForced();
VECTOR2D cursorPos = m_viewControls->GetCursorPosition();
m_viewControls->ForceCursorPosition( true, m_viewControls->GetCursorPosition() );
// Run update handlers
m->UpdateAll();
boost::scoped_ptr<CONTEXT_MENU> menu( new CONTEXT_MENU( *m ) );
GetEditFrame()->PopupMenu( menu.get() );
// If nothing was chosen from the context menu, we must notify the tool as well
if( menu->GetSelected() < 0 )
{
TOOL_EVENT evt( TC_COMMAND, TA_CONTEXT_MENU_CHOICE, -1 );
evt.SetParameter( m );
dispatchInternal( evt );
}
TOOL_EVENT evt( TC_COMMAND, TA_CONTEXT_MENU_CLOSED );
evt.SetParameter( m );
dispatchInternal( evt );
m_viewControls->ForceCursorPosition( forcedCursor, cursorPos );
break;
}
}
}
bool TOOL_MANAGER::finishTool( TOOL_STATE* aState, bool aDeactivate )
{
bool shouldDeactivate = false;
// Reset VIEW_CONTROLS only if the most recent tool is finished
if( m_activeTools.empty() || m_activeTools.front() == aState->theTool->GetId() )
m_viewControls->Reset();
if( !aState->Pop() ) // if there are no other contexts saved on the stack
{
// find the tool and deactivate it
std::list<TOOL_ID>::iterator tool = std::find( m_activeTools.begin(), m_activeTools.end(),
aState->theTool->GetId() );
if( tool != m_activeTools.end() )
{
shouldDeactivate = true;
if( aDeactivate )
m_activeTools.erase( tool );
}
}
aState->theTool->SetTransitions();
return shouldDeactivate;
}
bool TOOL_MANAGER::ProcessEvent( const TOOL_EVENT& aEvent )
{
// Early dispatch of events destined for the TOOL_MANAGER
if( !dispatchStandardEvents( aEvent ) )
return false;
dispatchInternal( aEvent );
dispatchActivation( aEvent );
dispatchContextMenu( aEvent );
// Dispatch queue
while( !m_eventQueue.empty() )
{
TOOL_EVENT event = m_eventQueue.front();
m_eventQueue.pop_front();
ProcessEvent( event );
}
if( m_view->IsDirty() )
{
EDA_DRAW_FRAME* f = static_cast<EDA_DRAW_FRAME*>( GetEditFrame() );
f->GetGalCanvas()->Refresh(); // fixme: ugly hack, provide a method in TOOL_DISPATCHER.
}
return false;
}
void TOOL_MANAGER::ScheduleContextMenu( TOOL_BASE* aTool, CONTEXT_MENU* aMenu,
CONTEXT_MENU_TRIGGER aTrigger )
{
TOOL_STATE* st = m_toolState[aTool];
st->contextMenu = aMenu;
st->contextMenuTrigger = aTrigger;
}
bool TOOL_MANAGER::SaveClipboard( const std::string& aText )
{
if( wxTheClipboard->Open() )
{
wxTheClipboard->SetData( new wxTextDataObject( wxString( aText.c_str(), wxConvUTF8 ) ) );
wxTheClipboard->Close();
return true;
}
return false;
}
std::string TOOL_MANAGER::GetClipboard() const
{
std::string result;
if( wxTheClipboard->Open() )
{
if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
{
wxTextDataObject data;
wxTheClipboard->GetData( data );
result = data.GetText().mb_str();
}
wxTheClipboard->Close();
}
return result;
}
TOOL_ID TOOL_MANAGER::MakeToolId( const std::string& aToolName )
{
static int currentId;
return currentId++;
}
void TOOL_MANAGER::SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
KIGFX::VIEW_CONTROLS* aViewControls, wxWindow* aFrame )
{
m_model = aModel;
m_view = aView;
m_viewControls = aViewControls;
m_editFrame = aFrame;
m_actionMgr->UpdateHotKeys();
}
bool TOOL_MANAGER::isActive( TOOL_BASE* aTool )
{
if( !isRegistered( aTool ) )
return false;
// Just check if the tool is on the active tools stack
return std::find( m_activeTools.begin(), m_activeTools.end(), aTool->GetId() ) != m_activeTools.end();
}
|