]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxVTKRenderWindowInteractor.cxx
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[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 //EED 2017-01-01 Migration VTK7
156 #if VTK_MAJOR_VERSION <= 5
157 vtkCxxRevisionMacro(wxVTKRenderWindowInteractor, "$Revision: 1.7 $")
158 #else
159   //...
160 #endif
161 vtkInstantiatorNewMacro(wxVTKRenderWindowInteractor)
162
163 //---------------------------------------------------------------------------
164 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
165 #if (wxCHECK_VERSION(2, 8, 0))
166 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : wxGLCanvas(0, -1, wxDefaultPosition), vtkRenderWindowInteractor()
167 #else
168 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : wxGLCanvas(), vtkRenderWindowInteractor()
169 #endif
170 #else
171 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : wxWindow(), vtkRenderWindowInteractor()
172 #endif //__WXGTK__
173       , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
174       , ActiveButton(wxEVT_NULL)
175       , RenderAllowed(0)
176       , Stereo(0)
177       , Handle(0)
178       , Created(true)
179       , RenderWhenDisabled(1)
180       , UseCaptureMouse(0)
181 {
182 #ifdef VTK_DEBUG_LEAKS
183   vtkDebugLeaks::ConstructClass("wxVTKRenderWindowInteractor");
184 #endif
185   this->RenderWindow = NULL;
186   this->SetRenderWindow(vtkRenderWindow::New());
187   this->RenderWindow->Delete();
188 }
189 //---------------------------------------------------------------------------
190 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor(wxWindow *parent,
191                                                          wxWindowID id,
192                                                          const wxPoint &pos,
193                                                          const wxSize &size,
194                                                          long style,
195                                                          const wxString &name)
196 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
197       : wxGLCanvas(parent, id, pos, size, style, name), vtkRenderWindowInteractor()
198 #else
199       : wxWindow(parent, id, pos, size, style, name), vtkRenderWindowInteractor()
200 #endif //__WXGTK__
201       , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
202       , ActiveButton(wxEVT_NULL)
203       , RenderAllowed(0)
204       , Stereo(0)
205       , Handle(0)
206       , Created(true)
207       , RenderWhenDisabled(1)
208       , UseCaptureMouse(0)
209 {
210 #ifdef VTK_DEBUG_LEAKS
211   vtkDebugLeaks::ConstructClass("wxVTKRenderWindowInteractor");
212 #endif
213   this->RenderWindow = NULL;
214   this->SetRenderWindow(vtkRenderWindow::New());
215   this->RenderWindow->Delete();
216 #ifdef __WXMAC__
217   // On Mac (Carbon) we don't get notified of the initial window size with an EVT_SIZE event,
218   // so we update the size information of the interactor/renderwindow here
219   this->UpdateSize(size.x, size.y);
220 #endif
221 }
222 //---------------------------------------------------------------------------
223 wxVTKRenderWindowInteractor::~wxVTKRenderWindowInteractor()
224 {
225   SetRenderWindow(NULL);
226   SetInteractorStyle(NULL);
227 }
228 //---------------------------------------------------------------------------
229 wxVTKRenderWindowInteractor * wxVTKRenderWindowInteractor::New()
230 {
231   // we don't make use of the objectfactory, because we're not registered
232   return new wxVTKRenderWindowInteractor;
233 }
234 //---------------------------------------------------------------------------
235 void wxVTKRenderWindowInteractor::Initialize()
236 {
237   int *size = RenderWindow->GetSize();
238   // enable everything and start rendering
239   Enable();
240   //RenderWindow->Start();
241
242   // set the size in the render window interactor
243   Size[0] = size[0];
244   Size[1] = size[1];
245
246   // this is initialized
247   Initialized = 1;
248 }
249 //---------------------------------------------------------------------------
250 void wxVTKRenderWindowInteractor::Enable()
251 {
252   // if already enabled then done
253   if (Enabled)
254     return;
255
256   // that's it
257   Enabled = 1;
258 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
259   SetCurrent();
260 #endif
261   Modified();
262 }
263 //---------------------------------------------------------------------------
264 bool wxVTKRenderWindowInteractor::Enable(bool enable)
265 {
266 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
267   return wxGLCanvas::Enable(enable);
268 #else
269   return wxWindow::Enable(enable);
270 #endif
271 }
272 //---------------------------------------------------------------------------
273 void wxVTKRenderWindowInteractor::Disable()
274 {
275   // if already disabled then done
276   if (!Enabled)
277     return;
278
279   // that's it (we can't remove the event handler like it should be...)
280   Enabled = 0;
281   Modified();
282 }
283 //---------------------------------------------------------------------------
284 void wxVTKRenderWindowInteractor::Start()
285 {
286   // the interactor cannot control the event loop
287   vtkErrorMacro( << "wxVTKRenderWindowInteractor::Start() "
288     "interactor cannot control event loop.");
289 }
290 //---------------------------------------------------------------------------
291 void wxVTKRenderWindowInteractor::UpdateSize(int x, int y)
292 {
293   if( RenderWindow )
294   {
295     // if the size changed tell render window
296     if ( x != Size[0] || y != Size[1] )
297     {
298       // adjust our (vtkRenderWindowInteractor size)
299       Size[0] = x;
300       Size[1] = y;
301       // and our RenderWindow's size
302       RenderWindow->SetSize(x, y);
303     }
304   }
305 }
306 //---------------------------------------------------------------------------
307 int wxVTKRenderWindowInteractor::CreateTimer(int WXUNUSED(timertype))
308 {
309   // it's a one shot timer
310   if (!timer.Start(10, TRUE))
311     assert(false);
312
313   return 1;
314   
315 }
316 //---------------------------------------------------------------------------
317 int wxVTKRenderWindowInteractor::DestroyTimer()
318 {
319   // do nothing
320   return 1;
321 }
322 //---------------------------------------------------------------------------
323 void wxVTKRenderWindowInteractor::OnTimer(wxTimerEvent& WXUNUSED(event))
324 {
325   if (!Enabled)
326     return;
327     
328 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
329     // new style
330     InvokeEvent(vtkCommand::TimerEvent, NULL);
331 #else
332     // old style
333     InteractorStyle->OnTimer();
334 #endif
335 }
336
337 //---------------------------------------------------------------------------
338 // NOTE on implementation:
339 // Bad luck you ended up in the only tricky place of this code.
340 // A few note, wxWidgets still refuse to provide such convenient method
341 // so I have to maintain it myself, eventhough this is completely integrated
342 // in wxPython...
343 // Anyway if this happen to break for you then compare to a recent version of wxPython
344 // and look for the function long wxPyGetWinHandle(wxWindow* win)
345 // in wxPython/src/helpers.cpp
346 long wxVTKRenderWindowInteractor::GetHandleHack()
347 {
348   //helper function to hide the MSW vs GTK stuff
349   long handle_tmp = 0;
350
351 // __WXMSW__ is for Win32
352 //__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)
353 // __WXGTK__ is for both gtk 1.2.x and gtk 2.x
354 #if defined(__WXMSW__) || defined(__WXMAC__)
355     handle_tmp = (long)this->GetHandle();
356 #endif //__WXMSW__
357
358 //__WXCOCOA__ stands for using the objective-c Cocoa API
359 #ifdef __WXCOCOA__
360    // Here is how to find the NSWindow
361    wxTopLevelWindow* toplevel = dynamic_cast<wxTopLevelWindow*>(
362      wxGetTopLevelParent( this ) );
363    if (toplevel != NULL )    
364    {
365      handle_tmp = (long)toplevel->GetNSWindow();
366    }
367    // The NSView will be deducted from 
368    // [(NSWindow*)Handle contentView]
369    // if only I knew how to write that in c++
370 #endif //__WXCOCOA__
371
372     // Find and return the actual X-Window.
373 #if defined(__WXGTK__) || defined(__WXX11__)
374     return (long)GetXWindow(this);
375 #endif
376
377 //#ifdef __WXMOTIF__
378 //    handle_tmp = (long)this->GetXWindow();
379 //#endif
380
381   return handle_tmp;
382 }
383 //---------------------------------------------------------------------------
384 void wxVTKRenderWindowInteractor::OnPaint(wxPaintEvent& WXUNUSED(event))
385 {
386   //must always be here
387   wxPaintDC pDC(this);
388
389   //do it here rather than in the cstor: this is safer.
390   if(!Handle)
391   {
392     Handle = GetHandleHack();
393     RenderWindow->SetWindowId(reinterpret_cast<void *>(Handle));
394 #ifdef __WXMSW__
395     RenderWindow->SetParentId(reinterpret_cast<void *>(this->GetParent()->GetHWND()));
396 #endif //__WXMSW__
397   }
398   // get vtk to render to the wxWindows
399   Render();
400 #ifdef __WXMAC__
401   // This solves a problem with repainting after a window resize
402   // See also: http://sourceforge.net/mailarchive/forum.php?thread_id=31690967&forum_id=41789
403   vtkCarbonRenderWindow* rwin = vtkCarbonRenderWindow::SafeDownCast(RenderWindow);
404   if( rwin )
405   {
406     rwin->UpdateGLRegion();
407   }
408 #endif
409 }
410 //---------------------------------------------------------------------------
411 void wxVTKRenderWindowInteractor::OnEraseBackground(wxEraseEvent &event)
412 {
413   //turn off background erase to reduce flickering on MSW
414   event.Skip(false);
415 }
416 //---------------------------------------------------------------------------
417 void wxVTKRenderWindowInteractor::OnSize(wxSizeEvent& WXUNUSED(event))
418 {
419   int w, h;
420   GetClientSize(&w, &h);
421   UpdateSize(w, h);
422
423   if (!Enabled) 
424     {
425     return;
426     }
427
428 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
429   InvokeEvent(vtkCommand::ConfigureEvent, NULL);
430 #endif
431   //this will check for Handle
432   //Render();
433 }
434 //---------------------------------------------------------------------------
435 void wxVTKRenderWindowInteractor::OnMotion(wxMouseEvent &event)
436 {
437  if (!Enabled) 
438     {
439     return;
440     }
441 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
442   SetEventInformationFlipY(event.GetX(), event.GetY(), 
443     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
444
445   InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
446 #else
447   InteractorStyle->OnMouseMove(event.ControlDown(), event.ShiftDown(),
448     event.GetX(), Size[1] - event.GetY() - 1);
449 #endif
450 }
451 //---------------------------------------------------------------------------
452 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
453 void wxVTKRenderWindowInteractor::OnEnter(wxMouseEvent &event)
454 {
455   if (!Enabled) 
456     {
457     return;
458     }
459
460 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
461     // new style
462   SetEventInformationFlipY(event.GetX(), event.GetY(), 
463       event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
464
465   InvokeEvent(vtkCommand::EnterEvent, NULL);
466 #else
467     // old style
468   InteractorStyle->OnEnter(event.ControlDown(), event.ShiftDown(),
469       event.GetX(), Size[1] - event.GetY() - 1);  
470 #endif
471 }
472 //---------------------------------------------------------------------------
473 void wxVTKRenderWindowInteractor::OnLeave(wxMouseEvent &event)
474 {
475   if (!Enabled) 
476     {
477     return;
478     }
479
480 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
481     // new style
482   SetEventInformationFlipY(event.GetX(), event.GetY(), 
483       event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
484
485   InvokeEvent(vtkCommand::LeaveEvent, NULL);
486 #else
487     // old style
488   InteractorStyle->OnLeave(event.ControlDown(), event.ShiftDown(),
489       event.GetX(), Size[1] - event.GetY() - 1);  
490 #endif
491 }
492 //---------------------------------------------------------------------------
493 void wxVTKRenderWindowInteractor::OnKeyDown(wxKeyEvent &event)
494 {
495   if (!Enabled) 
496     {
497     return;
498     }
499
500 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
501     // new style
502   int keycode = event.GetKeyCode();
503   char key = '\0';
504   if (((unsigned int)keycode) < 256)
505   {
506     // TODO: Unicode in non-Unicode mode ??
507     key = (char)keycode;
508   }
509
510   // we don't get a valid mouse position inside the key event on every platform
511   // so we retrieve the mouse position explicitly and pass it along
512   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
513   SetEventInformationFlipY(mousePos.x, mousePos.y, 
514                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
515   InvokeEvent(vtkCommand::KeyPressEvent, NULL);
516 #else
517   InteractorStyle->OnKeyDown(event.ControlDown(), event.ShiftDown(), 
518     event.GetKeyCode(), 1);
519 #endif
520   event.Skip();
521 }
522 //---------------------------------------------------------------------------
523 void wxVTKRenderWindowInteractor::OnKeyUp(wxKeyEvent &event)
524 {
525   if (!Enabled) 
526     {
527     return;
528     }
529
530 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
531     // new style
532   int keycode = event.GetKeyCode();
533   char key = '\0';
534   if (((unsigned int)keycode) < 256)
535   {
536     // TODO: Unicode in non-Unicode mode ??
537     key = (char)keycode;
538   }
539
540   // we don't get a valid mouse position inside the key event on every platform
541   // so we retrieve the mouse position explicitly and pass it along
542   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
543   SetEventInformationFlipY(mousePos.x, mousePos.y, 
544                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
545   InvokeEvent(vtkCommand::KeyReleaseEvent, NULL);
546 #else
547   InteractorStyle->OnKeyUp(event.ControlDown(), event.ShiftDown(), 
548     event.GetKeyCode(), 1);
549 #endif
550   event.Skip();
551 }
552 #endif //!(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
553  //---------------------------------------------------------------------------
554 void wxVTKRenderWindowInteractor::OnChar(wxKeyEvent &event)
555 {
556   if (!Enabled) 
557     {
558     return;
559     }
560     
561 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
562   // new style
563   int keycode = event.GetKeyCode();
564   char key = '\0';
565   if (((unsigned int)keycode) < 256)
566     {
567     // TODO: Unicode in non-Unicode mode ??
568     key = (char)keycode;
569     }
570
571   // we don't get a valid mouse position inside the key event on every platform
572   // so we retrieve the mouse position explicitly and pass it along
573   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
574   SetEventInformationFlipY(mousePos.x, mousePos.y, 
575                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
576   InvokeEvent(vtkCommand::CharEvent, NULL);
577 #endif
578   event.Skip();
579 }
580 //---------------------------------------------------------------------------
581 void wxVTKRenderWindowInteractor::OnButtonDown(wxMouseEvent &event)
582 {
583   if (!Enabled || (ActiveButton != wxEVT_NULL))
584     {
585     return;
586     }
587   ActiveButton = event.GetEventType();
588
589     // On Mac (Carbon) and Windows we don't automatically get the focus when
590     // you click inside the window
591     // we therefore set the focus explicitly
592     // Apparently we need that on linux (GTK) too:
593     this->SetFocus();
594
595 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
596   SetEventInformationFlipY(event.GetX(), event.GetY(), 
597     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
598 #endif
599
600   if(event.RightDown())
601   {
602 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
603     // new style
604     InvokeEvent(vtkCommand::RightButtonPressEvent, NULL);
605 #else            
606     // old style
607     InteractorStyle->OnRightButtonDown(event.ControlDown(), event.ShiftDown(),
608       event.GetX(), Size[1] - event.GetY() - 1);
609 #endif
610   }
611   else if(event.LeftDown())
612   {
613 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
614     // new style
615     InvokeEvent(vtkCommand::LeftButtonPressEvent, NULL);
616 #else            
617     // old style
618     InteractorStyle->OnLeftButtonDown(event.ControlDown(),  event.ShiftDown(),
619       event.GetX(), Size[1] - event.GetY() - 1);
620 #endif
621   }
622   else if(event.MiddleDown())
623   {
624 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
625     // new style
626     InvokeEvent(vtkCommand::MiddleButtonPressEvent, NULL);
627 #else            
628     // old style
629     InteractorStyle->OnMiddleButtonDown(event.ControlDown(), event.ShiftDown(),
630       event.GetX(), Size[1] - event.GetY() - 1);
631 #endif
632   }
633   //save the button and capture mouse until the button is released
634   //Only if :
635   //1. it is possible (WX_USE_X_CAPTURE)
636   //2. user decided to.
637   if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
638   {
639     CaptureMouse();
640   }
641 }
642 //---------------------------------------------------------------------------
643 void wxVTKRenderWindowInteractor::OnButtonUp(wxMouseEvent &event)
644 {
645   //EVT_xxx_DOWN == EVT_xxx_UP - 1
646   //This is only needed if two mouse buttons are pressed at the same time.
647   //In wxWindows 2.4 and later: better use of wxMOUSE_BTN_RIGHT or 
648   //wxEVT_COMMAND_RIGHT_CLICK
649   if (!Enabled || (ActiveButton != (event.GetEventType()-1))) 
650     {
651     return;
652     }
653
654 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
655   SetEventInformationFlipY(event.GetX(), event.GetY(), 
656     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
657 #endif
658   
659   if(ActiveButton == wxEVT_RIGHT_DOWN)
660   {
661 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
662     // new style
663     InvokeEvent(vtkCommand::RightButtonReleaseEvent, NULL);
664 #else            
665     // old style
666     InteractorStyle->OnRightButtonUp(event.ControlDown(), event.ShiftDown(),
667       event.GetX(), Size[1] - event.GetY() - 1);
668 #endif
669   }
670   else if(ActiveButton == wxEVT_LEFT_DOWN)
671   {
672 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
673     // new style
674     InvokeEvent(vtkCommand::LeftButtonReleaseEvent, NULL);
675 #else            
676     // old style
677     InteractorStyle->OnLeftButtonUp(event.ControlDown(), event.ShiftDown(),
678       event.GetX(), Size[1] - event.GetY() - 1);
679 #endif
680   }
681   else if(ActiveButton == wxEVT_MIDDLE_DOWN)
682   {
683 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
684     // new style
685     InvokeEvent(vtkCommand::MiddleButtonReleaseEvent, NULL);
686 #else            
687     // old style
688     InteractorStyle->OnMiddleButtonUp(event.ControlDown(), event.ShiftDown(),
689       event.GetX(), Size[1] - event.GetY() - 1);
690 #endif
691   }
692   //if the ActiveButton is realeased, then release mouse capture
693   if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
694   {
695     ReleaseMouse();
696   }
697   ActiveButton = wxEVT_NULL;
698 }
699 //---------------------------------------------------------------------------
700 void wxVTKRenderWindowInteractor::OnMouseWheel(wxMouseEvent& event)
701 {
702 // Mouse wheel was only added after VTK 4.4 (I think...)
703 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 2)
704   // new style
705   //Set vtk event information ... The numebr of wheel rotations is stored in
706   //the x varible.  y varible is zero
707   SetEventInformationFlipY(event.GetWheelRotation() / event.GetWheelDelta(), 0, 
708                            event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
709   if(event.GetWheelRotation() > 0)
710     {
711       //Send event to VTK
712       InvokeEvent(vtkCommand::MouseWheelForwardEvent, NULL);
713     }
714   else
715     {
716       //Send event to VTK
717       InvokeEvent(vtkCommand::MouseWheelBackwardEvent, NULL);
718     }
719 #endif
720     
721 }
722
723 //---------------------------------------------------------------------------
724 void wxVTKRenderWindowInteractor::Render()
725 {
726   RenderAllowed = 1;
727   if (!RenderWhenDisabled)
728     {
729     //the user doesn't want us to render when the toplevel frame
730     //is disabled - first find the top level parent
731     wxWindow *topParent = wxGetTopLevelParent(this);
732     if (topParent)
733       {
734       //if it exists, check whether it's enabled
735       //if it's not enabeld, RenderAllowed will be false
736       RenderAllowed = topParent->IsEnabled();
737       }
738     }
739
740   if (RenderAllowed)
741     {
742     if(Handle && (Handle == GetHandleHack()) )
743       {
744       RenderWindow->Render();
745       }
746 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 2)
747     else if(GetHandleHack())
748       {
749       //this means the user has reparented us; let's adapt to the
750       //new situation by doing the WindowRemap dance
751       //store the new situation
752       Handle = GetHandleHack();
753       RenderWindow->SetNextWindowId(reinterpret_cast<void *>(Handle));
754       RenderWindow->WindowRemap();
755       RenderWindow->Render();
756       }
757 #endif
758     }
759 }
760 //---------------------------------------------------------------------------
761 void wxVTKRenderWindowInteractor::SetRenderWhenDisabled(int newValue)
762 {
763   //Change value of __RenderWhenDisabled ivar.
764   //If __RenderWhenDisabled is false (the default), this widget will not
765   //call Render() on the RenderWindow if the top level frame (i.e. the
766   //containing frame) has been disabled.
767
768   //This prevents recursive rendering during wxSafeYield() calls.
769   //wxSafeYield() can be called during the ProgressMethod() callback of
770   //a VTK object to have progress bars and other GUI elements updated -
771   //it does this by disabling all windows (disallowing user-input to
772   //prevent re-entrancy of code) and then handling all outstanding
773   //GUI events.
774         
775   //However, this often triggers an OnPaint() method for wxVTKRWIs,
776   //resulting in a Render(), resulting in Update() being called whilst
777   //still in progress.
778
779   RenderWhenDisabled = (bool)newValue;
780 }
781 //---------------------------------------------------------------------------
782 //
783 // Set the variable that indicates that we want a stereo capable window
784 // be created. This method can only be called before a window is realized.
785 //
786 void wxVTKRenderWindowInteractor::SetStereo(int capable)
787 {
788   if (Stereo != capable)
789     {
790     Stereo = capable;
791     RenderWindow->StereoCapableWindowOn();
792     RenderWindow->SetStereoTypeToCrystalEyes();
793     Modified();
794     }
795 }
796
797 //---------------------------------------------------------------------------
798 //
799 //
800 void wxVTKRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
801 {
802   this->Superclass::PrintSelf(os, indent);
803 }
804