]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxVTKRenderWindowInteractor.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / wxVTKRenderWindowInteractor.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /*=========================================================================
27
28   Program:   Visualization Toolkit
29   Module:    $RCSfile: wxVTKRenderWindowInteractor.cxx,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:14:35 $
32   Version:   $Revision: 1.7 $
33
34   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 
35   All rights reserved.
36   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
37
38      This software is distributed WITHOUT ANY WARRANTY; without even 
39      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
40      PURPOSE.  See the above copyright notice for more information.
41
42 =========================================================================*/
43
44 #include <assert.h>
45
46 #include "wxVTKRenderWindowInteractor.h"
47
48 //This is needed for vtk 3.1 :
49 #ifndef VTK_MAJOR_VERSION
50 #  include "vtkVersion.h"
51 #endif
52
53 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
54 #  include "vtkCommand.h"
55 #else
56 #  include "vtkInteractorStyle.h"
57 #endif
58 #include "vtkDebugLeaks.h"
59
60 #ifdef __WXMAC__
61 #include "vtkCarbonRenderWindow.h"
62 #endif
63
64 //Keep this for compatibilty reason, this was introduced in wxGTK 2.4.0
65 #if (!wxCHECK_VERSION(2, 4, 0))
66 wxWindow* wxGetTopLevelParent(wxWindow *win)
67 {
68     while ( win && !win->IsTopLevel() )
69          win = win->GetParent();
70     return win;
71 }
72 #endif
73
74 // To access objc calls on cocoa
75 #ifdef __WXCOCOA__
76 #ifdef VTK_USE_COCOA
77 #import <Cocoa/Cocoa.h>
78 // This trick is no longer need in VTK CVS, should get rid of that:
79 #define id Id
80 #else
81 #error Build mismatch you need both wxWidgets and VTK to be configure against Cocoa to work
82 #endif //VTK_USE_COCOA
83 #endif //__WXCOCOA__
84
85 #ifdef __WXGTK__
86 #    include <gdk/gdkx.h> // GDK_WINDOW_XWINDOW is found here in wxWidgets 2.8.0
87 #    include "gdk/gdkprivate.h"
88 #if wxCHECK_VERSION(2, 8, 0)
89 #ifdef __WXGTK20__
90 #include <wx/gtk/win_gtk.h>
91 #else
92 #include <wx/gtk1/win_gtk.h>
93 #endif
94 #else
95 #include <wx/gtk/win_gtk.h>
96 #endif
97 #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
98                           GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
99                           GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
100 #endif
101
102 #ifdef __WXX11__
103 #include "wx/x11/privx.h"
104 #define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle())
105 #endif
106
107
108 //For more info on this class please go to:
109 //http://wxvtk.sf.net
110 //This hack is for some buggy wxGTK version:
111 #if wxCHECK_VERSION(2, 3, 2) && !wxCHECK_VERSION(2, 4, 1) && defined(__WXGTK__)
112 #  define WX_USE_X_CAPTURE 0
113 #else
114 #  define WX_USE_X_CAPTURE 1
115 #endif
116
117 #define ID_wxVTKRenderWindowInteractor_TIMER 1001
118
119 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
120 IMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxGLCanvas)
121 #else
122 IMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxWindow)
123 #endif  //__WXGTK__
124
125 //---------------------------------------------------------------------------
126 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
127 BEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxGLCanvas)
128 #else
129 BEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxWindow)
130 #endif //__WXGTK__
131   //refresh window by doing a Render
132   EVT_PAINT       (wxVTKRenderWindowInteractor::OnPaint)
133   EVT_ERASE_BACKGROUND(wxVTKRenderWindowInteractor::OnEraseBackground)
134   EVT_MOTION      (wxVTKRenderWindowInteractor::OnMotion)
135
136   //Bind the events to the event converters
137   EVT_LEFT_DOWN   (wxVTKRenderWindowInteractor::OnButtonDown)
138   EVT_MIDDLE_DOWN (wxVTKRenderWindowInteractor::OnButtonDown)
139   EVT_RIGHT_DOWN  (wxVTKRenderWindowInteractor::OnButtonDown)
140   EVT_LEFT_UP     (wxVTKRenderWindowInteractor::OnButtonUp)
141   EVT_MIDDLE_UP   (wxVTKRenderWindowInteractor::OnButtonUp)
142   EVT_RIGHT_UP    (wxVTKRenderWindowInteractor::OnButtonUp)
143 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
144   EVT_ENTER_WINDOW(wxVTKRenderWindowInteractor::OnEnter)
145   EVT_LEAVE_WINDOW(wxVTKRenderWindowInteractor::OnLeave)
146   EVT_MOUSEWHEEL  (wxVTKRenderWindowInteractor::OnMouseWheel)
147   EVT_KEY_DOWN    (wxVTKRenderWindowInteractor::OnKeyDown)
148   EVT_KEY_UP      (wxVTKRenderWindowInteractor::OnKeyUp)
149   EVT_CHAR        (wxVTKRenderWindowInteractor::OnChar)
150 #endif
151   EVT_TIMER       (ID_wxVTKRenderWindowInteractor_TIMER, wxVTKRenderWindowInteractor::OnTimer)
152   EVT_SIZE        (wxVTKRenderWindowInteractor::OnSize)
153 END_EVENT_TABLE()
154
155 vtkCxxRevisionMacro(wxVTKRenderWindowInteractor, "$Revision: 1.7 $")
156 vtkInstantiatorNewMacro(wxVTKRenderWindowInteractor)
157
158 //---------------------------------------------------------------------------
159 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
160 #if (wxCHECK_VERSION(2, 8, 0))
161 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : wxGLCanvas(0, -1, wxDefaultPosition), vtkRenderWindowInteractor()
162 #else
163 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : wxGLCanvas(), vtkRenderWindowInteractor()
164 #endif
165 #else
166 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : wxWindow(), vtkRenderWindowInteractor()
167 #endif //__WXGTK__
168       , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
169       , ActiveButton(wxEVT_NULL)
170       , RenderAllowed(0)
171       , Stereo(0)
172       , Handle(0)
173       , Created(true)
174       , RenderWhenDisabled(1)
175       , UseCaptureMouse(0)
176 {
177 #ifdef VTK_DEBUG_LEAKS
178   vtkDebugLeaks::ConstructClass("wxVTKRenderWindowInteractor");
179 #endif
180   this->RenderWindow = NULL;
181   this->SetRenderWindow(vtkRenderWindow::New());
182   this->RenderWindow->Delete();
183 }
184 //---------------------------------------------------------------------------
185 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor(wxWindow *parent,
186                                                          wxWindowID id,
187                                                          const wxPoint &pos,
188                                                          const wxSize &size,
189                                                          long style,
190                                                          const wxString &name)
191 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
192       : wxGLCanvas(parent, id, pos, size, style, name), vtkRenderWindowInteractor()
193 #else
194       : wxWindow(parent, id, pos, size, style, name), vtkRenderWindowInteractor()
195 #endif //__WXGTK__
196       , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
197       , ActiveButton(wxEVT_NULL)
198       , RenderAllowed(0)
199       , Stereo(0)
200       , Handle(0)
201       , Created(true)
202       , RenderWhenDisabled(1)
203       , UseCaptureMouse(0)
204 {
205 #ifdef VTK_DEBUG_LEAKS
206   vtkDebugLeaks::ConstructClass("wxVTKRenderWindowInteractor");
207 #endif
208   this->RenderWindow = NULL;
209   this->SetRenderWindow(vtkRenderWindow::New());
210   this->RenderWindow->Delete();
211 #ifdef __WXMAC__
212   // On Mac (Carbon) we don't get notified of the initial window size with an EVT_SIZE event,
213   // so we update the size information of the interactor/renderwindow here
214   this->UpdateSize(size.x, size.y);
215 #endif
216 }
217 //---------------------------------------------------------------------------
218 wxVTKRenderWindowInteractor::~wxVTKRenderWindowInteractor()
219 {
220   SetRenderWindow(NULL);
221   SetInteractorStyle(NULL);
222 }
223 //---------------------------------------------------------------------------
224 wxVTKRenderWindowInteractor * wxVTKRenderWindowInteractor::New()
225 {
226   // we don't make use of the objectfactory, because we're not registered
227   return new wxVTKRenderWindowInteractor;
228 }
229 //---------------------------------------------------------------------------
230 void wxVTKRenderWindowInteractor::Initialize()
231 {
232   int *size = RenderWindow->GetSize();
233   // enable everything and start rendering
234   Enable();
235   //RenderWindow->Start();
236
237   // set the size in the render window interactor
238   Size[0] = size[0];
239   Size[1] = size[1];
240
241   // this is initialized
242   Initialized = 1;
243 }
244 //---------------------------------------------------------------------------
245 void wxVTKRenderWindowInteractor::Enable()
246 {
247   // if already enabled then done
248   if (Enabled)
249     return;
250
251   // that's it
252   Enabled = 1;
253 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
254   SetCurrent();
255 #endif
256   Modified();
257 }
258 //---------------------------------------------------------------------------
259 bool wxVTKRenderWindowInteractor::Enable(bool enable)
260 {
261 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
262   return wxGLCanvas::Enable(enable);
263 #else
264   return wxWindow::Enable(enable);
265 #endif
266 }
267 //---------------------------------------------------------------------------
268 void wxVTKRenderWindowInteractor::Disable()
269 {
270   // if already disabled then done
271   if (!Enabled)
272     return;
273
274   // that's it (we can't remove the event handler like it should be...)
275   Enabled = 0;
276   Modified();
277 }
278 //---------------------------------------------------------------------------
279 void wxVTKRenderWindowInteractor::Start()
280 {
281   // the interactor cannot control the event loop
282   vtkErrorMacro( << "wxVTKRenderWindowInteractor::Start() "
283     "interactor cannot control event loop.");
284 }
285 //---------------------------------------------------------------------------
286 void wxVTKRenderWindowInteractor::UpdateSize(int x, int y)
287 {
288   if( RenderWindow )
289   {
290     // if the size changed tell render window
291     if ( x != Size[0] || y != Size[1] )
292     {
293       // adjust our (vtkRenderWindowInteractor size)
294       Size[0] = x;
295       Size[1] = y;
296       // and our RenderWindow's size
297       RenderWindow->SetSize(x, y);
298     }
299   }
300 }
301 //---------------------------------------------------------------------------
302 int wxVTKRenderWindowInteractor::CreateTimer(int WXUNUSED(timertype))
303 {
304   // it's a one shot timer
305   if (!timer.Start(10, TRUE))
306     assert(false);
307
308   return 1;
309   
310 }
311 //---------------------------------------------------------------------------
312 int wxVTKRenderWindowInteractor::DestroyTimer()
313 {
314   // do nothing
315   return 1;
316 }
317 //---------------------------------------------------------------------------
318 void wxVTKRenderWindowInteractor::OnTimer(wxTimerEvent& WXUNUSED(event))
319 {
320   if (!Enabled)
321     return;
322     
323 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
324     // new style
325     InvokeEvent(vtkCommand::TimerEvent, NULL);
326 #else
327     // old style
328     InteractorStyle->OnTimer();
329 #endif
330 }
331
332 //---------------------------------------------------------------------------
333 // NOTE on implementation:
334 // Bad luck you ended up in the only tricky place of this code.
335 // A few note, wxWidgets still refuse to provide such convenient method
336 // so I have to maintain it myself, eventhough this is completely integrated
337 // in wxPython...
338 // Anyway if this happen to break for you then compare to a recent version of wxPython
339 // and look for the function long wxPyGetWinHandle(wxWindow* win)
340 // in wxPython/src/helpers.cpp
341 long wxVTKRenderWindowInteractor::GetHandleHack()
342 {
343   //helper function to hide the MSW vs GTK stuff
344   long handle_tmp = 0;
345
346 // __WXMSW__ is for Win32
347 //__WXMAC__ stands for using Carbon C-headers, using either the CarbonLib/CFM or the native Mach-O builds (which then also use the latest features available)
348 // __WXGTK__ is for both gtk 1.2.x and gtk 2.x
349 #if defined(__WXMSW__) || defined(__WXMAC__)
350     handle_tmp = (long)this->GetHandle();
351 #endif //__WXMSW__
352
353 //__WXCOCOA__ stands for using the objective-c Cocoa API
354 #ifdef __WXCOCOA__
355    // Here is how to find the NSWindow
356    wxTopLevelWindow* toplevel = dynamic_cast<wxTopLevelWindow*>(
357      wxGetTopLevelParent( this ) );
358    if (toplevel != NULL )    
359    {
360      handle_tmp = (long)toplevel->GetNSWindow();
361    }
362    // The NSView will be deducted from 
363    // [(NSWindow*)Handle contentView]
364    // if only I knew how to write that in c++
365 #endif //__WXCOCOA__
366
367     // Find and return the actual X-Window.
368 #if defined(__WXGTK__) || defined(__WXX11__)
369     return (long)GetXWindow(this);
370 #endif
371
372 //#ifdef __WXMOTIF__
373 //    handle_tmp = (long)this->GetXWindow();
374 //#endif
375
376   return handle_tmp;
377 }
378 //---------------------------------------------------------------------------
379 void wxVTKRenderWindowInteractor::OnPaint(wxPaintEvent& WXUNUSED(event))
380 {
381   //must always be here
382   wxPaintDC pDC(this);
383
384   //do it here rather than in the cstor: this is safer.
385   if(!Handle)
386   {
387     Handle = GetHandleHack();
388     RenderWindow->SetWindowId(reinterpret_cast<void *>(Handle));
389 #ifdef __WXMSW__
390     RenderWindow->SetParentId(reinterpret_cast<void *>(this->GetParent()->GetHWND()));
391 #endif //__WXMSW__
392   }
393   // get vtk to render to the wxWindows
394   Render();
395 #ifdef __WXMAC__
396   // This solves a problem with repainting after a window resize
397   // See also: http://sourceforge.net/mailarchive/forum.php?thread_id=31690967&forum_id=41789
398   vtkCarbonRenderWindow* rwin = vtkCarbonRenderWindow::SafeDownCast(RenderWindow);
399   if( rwin )
400   {
401     rwin->UpdateGLRegion();
402   }
403 #endif
404 }
405 //---------------------------------------------------------------------------
406 void wxVTKRenderWindowInteractor::OnEraseBackground(wxEraseEvent &event)
407 {
408   //turn off background erase to reduce flickering on MSW
409   event.Skip(false);
410 }
411 //---------------------------------------------------------------------------
412 void wxVTKRenderWindowInteractor::OnSize(wxSizeEvent& WXUNUSED(event))
413 {
414   int w, h;
415   GetClientSize(&w, &h);
416   UpdateSize(w, h);
417
418   if (!Enabled) 
419     {
420     return;
421     }
422
423 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
424   InvokeEvent(vtkCommand::ConfigureEvent, NULL);
425 #endif
426   //this will check for Handle
427   //Render();
428 }
429 //---------------------------------------------------------------------------
430 void wxVTKRenderWindowInteractor::OnMotion(wxMouseEvent &event)
431 {
432  if (!Enabled) 
433     {
434     return;
435     }
436 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
437   SetEventInformationFlipY(event.GetX(), event.GetY(), 
438     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
439
440   InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
441 #else
442   InteractorStyle->OnMouseMove(event.ControlDown(), event.ShiftDown(),
443     event.GetX(), Size[1] - event.GetY() - 1);
444 #endif
445 }
446 //---------------------------------------------------------------------------
447 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
448 void wxVTKRenderWindowInteractor::OnEnter(wxMouseEvent &event)
449 {
450   if (!Enabled) 
451     {
452     return;
453     }
454
455 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
456     // new style
457   SetEventInformationFlipY(event.GetX(), event.GetY(), 
458       event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
459
460   InvokeEvent(vtkCommand::EnterEvent, NULL);
461 #else
462     // old style
463   InteractorStyle->OnEnter(event.ControlDown(), event.ShiftDown(),
464       event.GetX(), Size[1] - event.GetY() - 1);  
465 #endif
466 }
467 //---------------------------------------------------------------------------
468 void wxVTKRenderWindowInteractor::OnLeave(wxMouseEvent &event)
469 {
470   if (!Enabled) 
471     {
472     return;
473     }
474
475 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
476     // new style
477   SetEventInformationFlipY(event.GetX(), event.GetY(), 
478       event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
479
480   InvokeEvent(vtkCommand::LeaveEvent, NULL);
481 #else
482     // old style
483   InteractorStyle->OnLeave(event.ControlDown(), event.ShiftDown(),
484       event.GetX(), Size[1] - event.GetY() - 1);  
485 #endif
486 }
487 //---------------------------------------------------------------------------
488 void wxVTKRenderWindowInteractor::OnKeyDown(wxKeyEvent &event)
489 {
490   if (!Enabled) 
491     {
492     return;
493     }
494
495 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
496     // new style
497   int keycode = event.GetKeyCode();
498   char key = '\0';
499   if (((unsigned int)keycode) < 256)
500   {
501     // TODO: Unicode in non-Unicode mode ??
502     key = (char)keycode;
503   }
504
505   // we don't get a valid mouse position inside the key event on every platform
506   // so we retrieve the mouse position explicitly and pass it along
507   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
508   SetEventInformationFlipY(mousePos.x, mousePos.y, 
509                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
510   InvokeEvent(vtkCommand::KeyPressEvent, NULL);
511 #else
512   InteractorStyle->OnKeyDown(event.ControlDown(), event.ShiftDown(), 
513     event.GetKeyCode(), 1);
514 #endif
515   event.Skip();
516 }
517 //---------------------------------------------------------------------------
518 void wxVTKRenderWindowInteractor::OnKeyUp(wxKeyEvent &event)
519 {
520   if (!Enabled) 
521     {
522     return;
523     }
524
525 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
526     // new style
527   int keycode = event.GetKeyCode();
528   char key = '\0';
529   if (((unsigned int)keycode) < 256)
530   {
531     // TODO: Unicode in non-Unicode mode ??
532     key = (char)keycode;
533   }
534
535   // we don't get a valid mouse position inside the key event on every platform
536   // so we retrieve the mouse position explicitly and pass it along
537   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
538   SetEventInformationFlipY(mousePos.x, mousePos.y, 
539                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
540   InvokeEvent(vtkCommand::KeyReleaseEvent, NULL);
541 #else
542   InteractorStyle->OnKeyUp(event.ControlDown(), event.ShiftDown(), 
543     event.GetKeyCode(), 1);
544 #endif
545   event.Skip();
546 }
547 #endif //!(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
548  //---------------------------------------------------------------------------
549 void wxVTKRenderWindowInteractor::OnChar(wxKeyEvent &event)
550 {
551   if (!Enabled) 
552     {
553     return;
554     }
555     
556 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
557   // new style
558   int keycode = event.GetKeyCode();
559   char key = '\0';
560   if (((unsigned int)keycode) < 256)
561     {
562     // TODO: Unicode in non-Unicode mode ??
563     key = (char)keycode;
564     }
565
566   // we don't get a valid mouse position inside the key event on every platform
567   // so we retrieve the mouse position explicitly and pass it along
568   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
569   SetEventInformationFlipY(mousePos.x, mousePos.y, 
570                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
571   InvokeEvent(vtkCommand::CharEvent, NULL);
572 #endif
573   event.Skip();
574 }
575 //---------------------------------------------------------------------------
576 void wxVTKRenderWindowInteractor::OnButtonDown(wxMouseEvent &event)
577 {
578   if (!Enabled || (ActiveButton != wxEVT_NULL))
579     {
580     return;
581     }
582   ActiveButton = event.GetEventType();
583
584     // On Mac (Carbon) and Windows we don't automatically get the focus when
585     // you click inside the window
586     // we therefore set the focus explicitly
587     // Apparently we need that on linux (GTK) too:
588     this->SetFocus();
589
590 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
591   SetEventInformationFlipY(event.GetX(), event.GetY(), 
592     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
593 #endif
594
595   if(event.RightDown())
596   {
597 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
598     // new style
599     InvokeEvent(vtkCommand::RightButtonPressEvent, NULL);
600 #else            
601     // old style
602     InteractorStyle->OnRightButtonDown(event.ControlDown(), event.ShiftDown(),
603       event.GetX(), Size[1] - event.GetY() - 1);
604 #endif
605   }
606   else if(event.LeftDown())
607   {
608 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
609     // new style
610     InvokeEvent(vtkCommand::LeftButtonPressEvent, NULL);
611 #else            
612     // old style
613     InteractorStyle->OnLeftButtonDown(event.ControlDown(),  event.ShiftDown(),
614       event.GetX(), Size[1] - event.GetY() - 1);
615 #endif
616   }
617   else if(event.MiddleDown())
618   {
619 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
620     // new style
621     InvokeEvent(vtkCommand::MiddleButtonPressEvent, NULL);
622 #else            
623     // old style
624     InteractorStyle->OnMiddleButtonDown(event.ControlDown(), event.ShiftDown(),
625       event.GetX(), Size[1] - event.GetY() - 1);
626 #endif
627   }
628   //save the button and capture mouse until the button is released
629   //Only if :
630   //1. it is possible (WX_USE_X_CAPTURE)
631   //2. user decided to.
632   if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
633   {
634     CaptureMouse();
635   }
636 }
637 //---------------------------------------------------------------------------
638 void wxVTKRenderWindowInteractor::OnButtonUp(wxMouseEvent &event)
639 {
640   //EVT_xxx_DOWN == EVT_xxx_UP - 1
641   //This is only needed if two mouse buttons are pressed at the same time.
642   //In wxWindows 2.4 and later: better use of wxMOUSE_BTN_RIGHT or 
643   //wxEVT_COMMAND_RIGHT_CLICK
644   if (!Enabled || (ActiveButton != (event.GetEventType()-1))) 
645     {
646     return;
647     }
648
649 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
650   SetEventInformationFlipY(event.GetX(), event.GetY(), 
651     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
652 #endif
653   
654   if(ActiveButton == wxEVT_RIGHT_DOWN)
655   {
656 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
657     // new style
658     InvokeEvent(vtkCommand::RightButtonReleaseEvent, NULL);
659 #else            
660     // old style
661     InteractorStyle->OnRightButtonUp(event.ControlDown(), event.ShiftDown(),
662       event.GetX(), Size[1] - event.GetY() - 1);
663 #endif
664   }
665   else if(ActiveButton == wxEVT_LEFT_DOWN)
666   {
667 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
668     // new style
669     InvokeEvent(vtkCommand::LeftButtonReleaseEvent, NULL);
670 #else            
671     // old style
672     InteractorStyle->OnLeftButtonUp(event.ControlDown(), event.ShiftDown(),
673       event.GetX(), Size[1] - event.GetY() - 1);
674 #endif
675   }
676   else if(ActiveButton == wxEVT_MIDDLE_DOWN)
677   {
678 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
679     // new style
680     InvokeEvent(vtkCommand::MiddleButtonReleaseEvent, NULL);
681 #else            
682     // old style
683     InteractorStyle->OnMiddleButtonUp(event.ControlDown(), event.ShiftDown(),
684       event.GetX(), Size[1] - event.GetY() - 1);
685 #endif
686   }
687   //if the ActiveButton is realeased, then release mouse capture
688   if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
689   {
690     ReleaseMouse();
691   }
692   ActiveButton = wxEVT_NULL;
693 }
694 //---------------------------------------------------------------------------
695 void wxVTKRenderWindowInteractor::OnMouseWheel(wxMouseEvent& event)
696 {
697 // Mouse wheel was only added after VTK 4.4 (I think...)
698 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 2)
699   // new style
700   //Set vtk event information ... The numebr of wheel rotations is stored in
701   //the x varible.  y varible is zero
702   SetEventInformationFlipY(event.GetWheelRotation() / event.GetWheelDelta(), 0, 
703                            event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
704   if(event.GetWheelRotation() > 0)
705     {
706       //Send event to VTK
707       InvokeEvent(vtkCommand::MouseWheelForwardEvent, NULL);
708     }
709   else
710     {
711       //Send event to VTK
712       InvokeEvent(vtkCommand::MouseWheelBackwardEvent, NULL);
713     }
714 #endif
715     
716 }
717
718 //---------------------------------------------------------------------------
719 void wxVTKRenderWindowInteractor::Render()
720 {
721   RenderAllowed = 1;
722   if (!RenderWhenDisabled)
723     {
724     //the user doesn't want us to render when the toplevel frame
725     //is disabled - first find the top level parent
726     wxWindow *topParent = wxGetTopLevelParent(this);
727     if (topParent)
728       {
729       //if it exists, check whether it's enabled
730       //if it's not enabeld, RenderAllowed will be false
731       RenderAllowed = topParent->IsEnabled();
732       }
733     }
734
735   if (RenderAllowed)
736     {
737     if(Handle && (Handle == GetHandleHack()) )
738       {
739       RenderWindow->Render();
740       }
741 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 2)
742     else if(GetHandleHack())
743       {
744       //this means the user has reparented us; let's adapt to the
745       //new situation by doing the WindowRemap dance
746       //store the new situation
747       Handle = GetHandleHack();
748       RenderWindow->SetNextWindowId(reinterpret_cast<void *>(Handle));
749       RenderWindow->WindowRemap();
750       RenderWindow->Render();
751       }
752 #endif
753     }
754 }
755 //---------------------------------------------------------------------------
756 void wxVTKRenderWindowInteractor::SetRenderWhenDisabled(int newValue)
757 {
758   //Change value of __RenderWhenDisabled ivar.
759   //If __RenderWhenDisabled is false (the default), this widget will not
760   //call Render() on the RenderWindow if the top level frame (i.e. the
761   //containing frame) has been disabled.
762
763   //This prevents recursive rendering during wxSafeYield() calls.
764   //wxSafeYield() can be called during the ProgressMethod() callback of
765   //a VTK object to have progress bars and other GUI elements updated -
766   //it does this by disabling all windows (disallowing user-input to
767   //prevent re-entrancy of code) and then handling all outstanding
768   //GUI events.
769         
770   //However, this often triggers an OnPaint() method for wxVTKRWIs,
771   //resulting in a Render(), resulting in Update() being called whilst
772   //still in progress.
773
774   RenderWhenDisabled = (bool)newValue;
775 }
776 //---------------------------------------------------------------------------
777 //
778 // Set the variable that indicates that we want a stereo capable window
779 // be created. This method can only be called before a window is realized.
780 //
781 void wxVTKRenderWindowInteractor::SetStereo(int capable)
782 {
783   if (Stereo != capable)
784     {
785     Stereo = capable;
786     RenderWindow->StereoCapableWindowOn();
787     RenderWindow->SetStereoTypeToCrystalEyes();
788     Modified();
789     }
790 }
791
792 //---------------------------------------------------------------------------
793 //
794 //
795 void wxVTKRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
796 {
797   this->Superclass::PrintSelf(os, indent);
798 }
799