1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkWidgetEventTranslator.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkWidgetEventTranslator.h"
16 #include "vtkCommand.h"
17 #include "vtkObjectFactory.h"
18 #include "vtkWidgetEvent.h"
19 #include "vtkRenderWindowInteractor.h"
20 #include "vtkCallbackCommand.h"
21 #include "vtkEvent.h"
22 #include "vtkSmartPointer.h"
23 #include "vtkAbstractWidget.h"
24 #include <map>
25 #include <list>
26
27 vtkStandardNewMacro(vtkWidgetEventTranslator);
28
29
30 // This is what is place in the list
31 struct EventItem {
32 vtkSmartPointer< vtkEvent > VTKEvent;
33 unsigned long WidgetEvent;
34
EventItemEventItem35 EventItem(vtkEvent *e, unsigned long we)
36 {
37 this->VTKEvent = e;
38 this->WidgetEvent = we;
39 }
40 };
41
42 // A list of events
43 struct EventList : public std::list<EventItem>
44 {
findEventList45 unsigned long find(unsigned long VTKEvent)
46 {
47 std::list<EventItem>::iterator liter = this->begin();
48 for ( ; liter != this->end(); ++liter)
49 {
50 if ( VTKEvent == liter->VTKEvent->GetEventId() )
51 {
52 return liter->WidgetEvent;
53 }
54 }
55 return vtkWidgetEvent::NoEvent;
56 }
57
findEventList58 unsigned long find(vtkEvent *VTKEvent)
59 {
60 std::list<EventItem>::iterator liter = this->begin();
61 for ( ; liter != this->end(); ++liter)
62 {
63 if ( *VTKEvent == liter->VTKEvent )
64 {
65 return liter->WidgetEvent;
66 }
67 }
68 return vtkWidgetEvent::NoEvent;
69 }
70
71 // Remove a mapping
RemoveEventList72 int Remove( vtkEvent *VTKEvent )
73 {
74 std::list<EventItem>::iterator liter = this->begin();
75 for ( ; liter != this->end(); ++liter)
76 {
77 if ( *VTKEvent == liter->VTKEvent )
78 {
79 this->erase( liter );
80 return 1;
81 }
82 }
83 return 0;
84 }
85 };
86
87
88
89 // A STL map used to translate VTK events into lists of events. The reason
90 // that we have this list is because of the modifiers on the event. The
91 // VTK event id maps to the list, and then comparisons are done to
92 // determine which event matches.
93 class vtkEventMap : public std::map<unsigned long, EventList> {};
94 typedef std::map<unsigned long,EventList>::iterator EventMapIterator;
95
96 //----------------------------------------------------------------------------
vtkWidgetEventTranslator()97 vtkWidgetEventTranslator::vtkWidgetEventTranslator()
98 {
99 this->EventMap = new vtkEventMap;
100 this->Event = vtkEvent::New();
101 }
102
103 //----------------------------------------------------------------------------
~vtkWidgetEventTranslator()104 vtkWidgetEventTranslator::~vtkWidgetEventTranslator()
105 {
106 delete this->EventMap;
107 this->Event->Delete();
108 }
109
110 //----------------------------------------------------------------------------
SetTranslation(unsigned long VTKEvent,unsigned long widgetEvent)111 void vtkWidgetEventTranslator::SetTranslation(unsigned long VTKEvent,
112 unsigned long widgetEvent)
113 {
114 vtkSmartPointer< vtkEvent > e = vtkSmartPointer< vtkEvent >::New();
115 e->SetEventId(VTKEvent); //default modifiers
116 if (widgetEvent != vtkWidgetEvent::NoEvent)
117 {
118 (*this->EventMap)[VTKEvent].push_back(EventItem(e,widgetEvent));
119 }
120 else
121 {
122 this->RemoveTranslation( e );
123 }
124 }
125
126 //----------------------------------------------------------------------------
SetTranslation(const char * VTKEvent,const char * widgetEvent)127 void vtkWidgetEventTranslator::SetTranslation(const char *VTKEvent,
128 const char *widgetEvent)
129 {
130 this->SetTranslation(vtkCommand::GetEventIdFromString(VTKEvent),
131 vtkWidgetEvent::GetEventIdFromString(widgetEvent));
132 }
133
134 //----------------------------------------------------------------------------
SetTranslation(unsigned long VTKEvent,int modifier,char keyCode,int repeatCount,const char * keySym,unsigned long widgetEvent)135 void vtkWidgetEventTranslator::SetTranslation(unsigned long VTKEvent,
136 int modifier, char keyCode,
137 int repeatCount, const char* keySym,
138 unsigned long widgetEvent)
139 {
140 vtkSmartPointer< vtkEvent > e = vtkSmartPointer< vtkEvent >::New();
141 e->SetEventId(VTKEvent);
142 e->SetModifier(modifier);
143 e->SetKeyCode(keyCode);
144 e->SetRepeatCount(repeatCount);
145 e->SetKeySym(keySym);
146 if (widgetEvent != vtkWidgetEvent::NoEvent)
147 {
148 (*this->EventMap)[VTKEvent].push_back(EventItem(e,widgetEvent));
149 }
150 else
151 {
152 this->RemoveTranslation( e );
153 }
154 }
155
156 //----------------------------------------------------------------------------
SetTranslation(vtkEvent * VTKEvent,unsigned long widgetEvent)157 void vtkWidgetEventTranslator::SetTranslation(vtkEvent *VTKEvent,
158 unsigned long widgetEvent)
159 {
160 if (widgetEvent != vtkWidgetEvent::NoEvent)
161 {
162 (*this->EventMap)[VTKEvent->GetEventId()].push_back(
163 EventItem(VTKEvent,widgetEvent));
164 }
165 else
166 {
167 this->RemoveTranslation( VTKEvent );
168 }
169 }
170
171
172 //----------------------------------------------------------------------------
GetTranslation(unsigned long VTKEvent)173 unsigned long vtkWidgetEventTranslator::GetTranslation(unsigned long VTKEvent)
174 {
175 EventMapIterator iter = this->EventMap->find(VTKEvent);
176 if ( iter != this->EventMap->end() )
177 {
178 EventList &elist = (*iter).second;
179 return elist.find(VTKEvent);
180 }
181 else
182 {
183 return vtkWidgetEvent::NoEvent;
184 }
185 }
186
187 //----------------------------------------------------------------------------
GetTranslation(const char * VTKEvent)188 const char *vtkWidgetEventTranslator::GetTranslation(const char *VTKEvent)
189 {
190 return vtkWidgetEvent::GetStringFromEventId(
191 this->GetTranslation(vtkCommand::GetEventIdFromString(VTKEvent)) );
192 }
193
194 //----------------------------------------------------------------------------
GetTranslation(unsigned long VTKEvent,int modifier,char keyCode,int repeatCount,char * keySym)195 unsigned long vtkWidgetEventTranslator::GetTranslation(unsigned long VTKEvent,
196 int modifier, char keyCode,
197 int repeatCount, char* keySym)
198 {
199 EventMapIterator iter = this->EventMap->find(VTKEvent);
200 if ( iter != this->EventMap->end() )
201 {
202 this->Event->SetEventId(VTKEvent);
203 this->Event->SetModifier(modifier);
204 this->Event->SetKeyCode(keyCode);
205 this->Event->SetRepeatCount(repeatCount);
206 this->Event->SetKeySym(keySym);
207 EventList &elist = (*iter).second;
208 return elist.find(this->Event);
209 }
210 else
211 {
212 return vtkWidgetEvent::NoEvent;
213 }
214 }
215
216 //----------------------------------------------------------------------------
GetTranslation(vtkEvent * VTKEvent)217 unsigned long vtkWidgetEventTranslator::GetTranslation(vtkEvent *VTKEvent)
218 {
219 EventMapIterator iter = this->EventMap->find(VTKEvent->GetEventId());
220 if ( iter != this->EventMap->end() )
221 {
222 EventList &elist = (*iter).second;
223 return elist.find(VTKEvent);
224 }
225 else
226 {
227 return vtkWidgetEvent::NoEvent;
228 }
229 }
230
231 //----------------------------------------------------------------------------
RemoveTranslation(unsigned long VTKEvent,int modifier,char keyCode,int repeatCount,char * keySym)232 int vtkWidgetEventTranslator::RemoveTranslation(unsigned long VTKEvent,
233 int modifier, char keyCode,
234 int repeatCount, char* keySym)
235 {
236 vtkSmartPointer< vtkEvent > e = vtkSmartPointer< vtkEvent >::New();
237 e->SetEventId(VTKEvent);
238 e->SetModifier(modifier);
239 e->SetKeyCode(keyCode);
240 e->SetRepeatCount(repeatCount);
241 e->SetKeySym(keySym);
242 return this->RemoveTranslation( e );
243 }
244
245 //----------------------------------------------------------------------------
RemoveTranslation(vtkEvent * e)246 int vtkWidgetEventTranslator::RemoveTranslation( vtkEvent *e )
247 {
248 EventMapIterator iter = this->EventMap->find(e->GetEventId());
249 int numTranslationsRemoved = 0;
250 if (iter != this->EventMap->end())
251 {
252 while (iter->second.Remove(e))
253 {
254 ++numTranslationsRemoved;
255 iter = this->EventMap->find(e->GetEventId());
256 if (iter == this->EventMap->end())
257 {
258 break;
259 }
260 }
261 }
262
263 return numTranslationsRemoved;
264 }
265
266 //----------------------------------------------------------------------------
RemoveTranslation(unsigned long VTKEvent)267 int vtkWidgetEventTranslator::RemoveTranslation(unsigned long VTKEvent)
268 {
269 vtkSmartPointer< vtkEvent > e = vtkSmartPointer< vtkEvent >::New();
270 e->SetEventId(VTKEvent);
271 return this->RemoveTranslation( e );
272 }
273
274 //----------------------------------------------------------------------------
RemoveTranslation(const char * VTKEvent)275 int vtkWidgetEventTranslator::RemoveTranslation(const char *VTKEvent)
276 {
277 vtkSmartPointer< vtkEvent > e = vtkSmartPointer< vtkEvent >::New();
278 e->SetEventId(vtkCommand::GetEventIdFromString(VTKEvent));
279 return this->RemoveTranslation( e );
280 }
281
282 //----------------------------------------------------------------------------
ClearEvents()283 void vtkWidgetEventTranslator::ClearEvents()
284 {
285 EventMapIterator iter = this->EventMap->begin();
286 for ( ; iter != this->EventMap->end(); ++iter )
287 {
288 EventList &elist = (*iter).second;
289 elist.clear();
290 }
291 this->EventMap->clear();
292 }
293
294 //----------------------------------------------------------------------------
AddEventsToInteractor(vtkRenderWindowInteractor * i,vtkCallbackCommand * command,float priority)295 void vtkWidgetEventTranslator::AddEventsToInteractor(vtkRenderWindowInteractor *i,
296 vtkCallbackCommand *command,
297 float priority)
298 {
299 EventMapIterator iter = this->EventMap->begin();
300 for ( ; iter != this->EventMap->end(); ++iter )
301 {
302 i->AddObserver((*iter).first, command, priority);
303 }
304 }
305
306 //----------------------------------------------------------------------------
AddEventsToParent(vtkAbstractWidget * w,vtkCallbackCommand * command,float priority)307 void vtkWidgetEventTranslator::AddEventsToParent(vtkAbstractWidget *w,
308 vtkCallbackCommand *command,
309 float priority)
310 {
311 EventMapIterator iter = this->EventMap->begin();
312 for ( ; iter != this->EventMap->end(); ++iter )
313 {
314 w->AddObserver((*iter).first, command, priority);
315 }
316 }
317
318 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)319 void vtkWidgetEventTranslator::PrintSelf(ostream& os, vtkIndent indent)
320 {
321 //Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
322 this->Superclass::PrintSelf(os,indent);
323
324 //List all the events and their translations
325 os << indent << "Event Table:\n";
326 EventMapIterator iter = this->EventMap->begin();
327 for ( ; iter != this->EventMap->end(); ++iter )
328 {
329 EventList &elist = (*iter).second;
330 std::list<EventItem>::iterator liter = elist.begin();
331 for ( ; liter != elist.end(); ++liter)
332 {
333 os << "VTKEvent(" << vtkCommand::GetStringFromEventId(liter->VTKEvent->GetEventId()) << ","
334 << liter->VTKEvent->GetModifier() << "," << liter->VTKEvent->GetKeyCode() << ","
335 << liter->VTKEvent->GetRepeatCount() << ",";
336 os << (liter->VTKEvent->GetKeySym() ? liter->VTKEvent->GetKeySym() : "(any)");
337 os << ") maps to " << vtkWidgetEvent::GetStringFromEventId(liter->WidgetEvent) << "\n";
338 }
339 }
340 }
341