]> Creatis software - crea.git/blob - src/creawxVTKRenderWindowInteractor.mm
#3227 vtk8itk4wx3-mingw64 MACOS
[crea.git] / src / creawxVTKRenderWindowInteractor.mm
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29
30   Program:   Visualization Toolkit
31   Module:    $RCSfile$
32   Language:  C++
33   Date:      $Date$
34   Version:   $Revision$
35
36   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 
37   All rights reserved.
38   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
39
40      This software is distributed WITHOUT ANY WARRANTY; without even 
41      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
42      PURPOSE.  See the above copyright notice for more information.
43
44 =========================================================================*/
45
46 //#import <Cocoa/Cocoa.h>
47
48 #include <assert.h>
49
50 #include "creawxVTKRenderWindowInteractor.h"
51
52 #include <wx/display.h>
53
54
55 //This is needed for vtk 3.1 :
56 #ifndef VTK_MAJOR_VERSION
57 #  include "vtkVersion.h"
58 #endif
59
60 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
61 #  include "vtkCommand.h"
62 #else
63 #  include "vtkInteractorStyle.h"
64 #endif
65 #include "vtkDebugLeaks.h"
66
67 // AKT: wxOSX 2.9.x defines __WXOSX_COCOA__ rather than __WXCOCOA__
68 #ifdef __WXOSX_COCOA__
69 #define __WXCOCOA__
70 #endif
71
72 #if defined(__WXMAC__) && wxCHECK_VERSION(2,9,0)
73     // ControlDown has been changed to mean Command key down
74     #define ControlDown RawControlDown
75 #endif
76
77 #ifdef __WXMAC__
78 #ifdef __WXCOCOA__
79 #include "vtkCocoaRenderWindow.h"
80 #else
81 #include "vtkCarbonRenderWindow.h"
82 #endif
83 #endif
84
85
86 //=======================================================================
87 // LG : NAMESPACE IS NECESSARY TO AVOID CONFLICTING SYMBOLS IN DYN LIBS
88 namespace crea
89 {
90
91
92 //Keep this for compatibilty reason, this was introduced in wxGTK 2.4.0
93 #if (!wxCHECK_VERSION(2, 4, 0))
94 wxWindow* wxGetTopLevelParent(wxWindow *win)
95 {
96     while ( win && !win->IsTopLevel() )
97          win = win->GetParent();
98     return win;
99 }
100 #endif
101 }
102 // LG : EO namespace bbwxvtk
103 //=======================================================================
104
105 // To access objc calls on cocoa
106 #ifdef __WXCOCOA__
107 #ifdef VTK_USE_COCOA
108 #import <Cocoa/Cocoa.h>
109 // This trick is no longer need in VTK CVS, should get rid of that:
110 #define id Id
111 #else
112 #error Build mismatch you need both wxWidgets and VTK to be configure against Cocoa to work
113 #endif //VTK_USE_COCOA
114 #endif //__WXCOCOA__
115
116 #if wxMAJOR_VERSION <= 2
117
118         #ifdef __WXGTK__
119          #include <gdk/gdkx.h> // GDK_WINDOW_XWINDOW is found here in wxWidgets 2.8.0
120          #include "gdk/gdkprivate.h"
121          #if wxCHECK_VERSION(2, 9, 0)
122           // thanks to: http://thomasfischer.biz/?p=382
123           #include <gtk/gtkfixed.h>
124           #include <gtk/gtkwidget.h>
125           #include <wx/gtk/private/win_gtk.h>
126           #define piz(wxwin) WX_PIZZA((wxwin)->m_wxwindow)
127           #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
128                 GDK_WINDOW_XWINDOW(((GtkWidget*)piz(wxwin))->window) : \
129                 GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
130          #else
131           #if wxCHECK_VERSION(2, 8, 0)
132            #ifdef __WXGTK20__
133             #include <wx/gtk/win_gtk.h>
134            #else
135             #include <wx/gtk1/win_gtk.h>
136            #endif
137           #else
138            #include <wx/gtk/win_gtk.h>
139           #endif
140          
141           #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
142                                   GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
143                                   GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
144          #endif
145         #endif
146
147 #else
148         #ifdef __WXGTK__
149             #include <gdk/gdkx.h>
150             #include <gtk/gtk.h>
151 #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
152                           GDK_WINDOW_XID(gtk_widget_get_window((wxwin)->m_wxwindow)) :  \
153                          GDK_WINDOW_XID(gtk_widget_get_window((wxwin)->m_widget))
154         #endif
155 #endif
156
157 #ifdef __WXX11__
158 #include "wx/x11/privx.h"
159 #define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle())
160 #endif
161
162 //For more info on this class please go to:
163 //http://wxvtk.sf.net
164 //This hack is for some buggy wxGTK version:
165 #if wxCHECK_VERSION(2, 3, 2) && !wxCHECK_VERSION(2, 4, 1) && defined(__WXGTK__)
166 #  define WX_USE_X_CAPTURE 0
167 #else
168 #  define WX_USE_X_CAPTURE 1
169 #endif
170
171 #define ID_wxVTKRenderWindowInteractor_TIMER 1001
172
173 //=======================================================================
174 // LG : NAMESPACE IS NECESSARY TO AVOID CONFLICTING SYMBOLS IN DYN LIBS
175 namespace crea
176 {
177
178 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
179 #else
180 #endif  //__WXGTK__
181
182 //---------------------------------------------------------------------------
183 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
184                 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
185                 #if wxMAJOR_VERSION <= 2
186                         IMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxGLCanvas)
187                         BEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxGLCanvas)
188                 #else
189                         wxIMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxGLCanvas);
190                         wxBEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxGLCanvas)
191                 #endif
192 #else
193                 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
194                 #if wxMAJOR_VERSION <= 2
195                         IMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxWindow)
196                         BEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxWindow)
197                 #else
198                         wxIMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxWindow);
199                         wxBEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxWindow)
200                 #endif
201 #endif //__WXGTK__
202   //refresh window by doing a Render
203   EVT_PAINT       (wxVTKRenderWindowInteractor::OnPaint)
204   EVT_ERASE_BACKGROUND(wxVTKRenderWindowInteractor::OnEraseBackground)
205   EVT_MOTION      (wxVTKRenderWindowInteractor::OnMotion)
206
207   //Bind the events to the event converters
208   EVT_LEFT_DOWN   (wxVTKRenderWindowInteractor::OnButtonDown)
209   EVT_MIDDLE_DOWN (wxVTKRenderWindowInteractor::OnButtonDown)
210   EVT_RIGHT_DOWN  (wxVTKRenderWindowInteractor::OnButtonDown)
211   EVT_LEFT_UP     (wxVTKRenderWindowInteractor::OnButtonUp)
212   EVT_MIDDLE_UP   (wxVTKRenderWindowInteractor::OnButtonUp)
213   EVT_RIGHT_UP    (wxVTKRenderWindowInteractor::OnButtonUp)
214 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
215   EVT_ENTER_WINDOW(wxVTKRenderWindowInteractor::OnEnter)
216   EVT_LEAVE_WINDOW(wxVTKRenderWindowInteractor::OnLeave)
217   EVT_MOUSEWHEEL  (wxVTKRenderWindowInteractor::OnMouseWheel)
218 #if wxCHECK_VERSION(2, 8, 0)
219   EVT_MOUSE_CAPTURE_LOST(wxVTKRenderWindowInteractor::OnMouseCaptureLost)
220 #endif
221   EVT_KEY_DOWN    (wxVTKRenderWindowInteractor::OnKeyDown)
222   EVT_KEY_UP      (wxVTKRenderWindowInteractor::OnKeyUp)
223   EVT_CHAR        (wxVTKRenderWindowInteractor::OnChar)
224 #endif
225   EVT_TIMER       (ID_wxVTKRenderWindowInteractor_TIMER, wxVTKRenderWindowInteractor::OnTimer)
226   EVT_SIZE        (wxVTKRenderWindowInteractor::OnSize)
227 END_EVENT_TABLE()
228
229 //EED win Compilation why??:  vtkCxxRevisionMacro(wxVTKRenderWindowInteractor, "$Revision$")
230
231 //EED2020-04-10
232 #if VTK_MAJOR_VERSION < 8 
233     vtkInstantiatorNewMacro(wxVTKRenderWindowInteractor)
234 #endif
235
236 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
237 static int wxvtk_attributes[]={
238   WX_GL_DOUBLEBUFFER,
239   WX_GL_RGBA,
240   WX_GL_DEPTH_SIZE,
241   16,
242   0
243 };
244 #endif
245
246 //---------------------------------------------------------------------------
247 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() 
248
249 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
250     #if wxCHECK_VERSION(2, 9, 0) // the order of the parameters to wxGLCanvas::wxGLCanvas has changed
251                 : wxGLCanvas(0, -1, wxvtk_attributes, wxDefaultPosition, wxDefaultSize, 0, wxT("wxVTKRenderWindowInteractor")), 
252         #else
253                 : wxGLCanvas(0, -1, wxDefaultPosition, wxDefaultSize, 0, wxT("wxVTKRenderWindowInteractor"), wxvtk_attributes), 
254         #endif
255 #else
256                 : wxWindow(), 
257 #endif //__WXGTK__
258
259           vtkRenderWindowInteractor()
260       , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
261       , ActiveButton(wxEVT_NULL)
262       , Stereo(0)
263       , Handle(0)
264       , Created(true)
265       , RenderWhenDisabled(1)
266       , UseCaptureMouse(0)
267 {
268 #ifdef VTK_DEBUG_LEAKS
269   vtkDebugLeaks::ConstructClass("wxVTKRenderWindowInteractor");
270 #endif
271 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
272   this->context = new wxGLContext(this);
273 #endif
274   this->RenderWindow = NULL;
275   this->SetRenderWindow(vtkRenderWindow::New());
276   this->RenderWindow->Delete();
277 }
278 //---------------------------------------------------------------------------
279 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor(wxWindow *parent,
280                                                          wxWindowID id,
281                                                          const wxPoint &pos,
282                                                          const wxSize &size,
283                                                          long style,
284                                                          const wxString &name)
285 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
286     #if wxCHECK_VERSION(2, 9, 0) // the order of the parameters to wxGLCanvas::wxGLCanvas has changed
287       : wxGLCanvas(parent, id, wxvtk_attributes, pos, size, style, name)
288     #else
289       : wxGLCanvas(parent, id, pos, size, style, name, wxvtk_attributes)
290     #endif
291 #else
292       : wxWindow(parent, id, pos, size, style, name)
293 #endif //__WXGTK__
294           , vtkRenderWindowInteractor()
295       , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
296       , ActiveButton(wxEVT_NULL)
297       , Stereo(0)
298       , Handle(0)
299       , Created(true)
300       , RenderWhenDisabled(1)
301       , UseCaptureMouse(0)
302 {
303 #ifdef VTK_DEBUG_LEAKS
304   vtkDebugLeaks::ConstructClass("wxVTKRenderWindowInteractor");
305 #endif
306 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
307   this->context = new wxGLContext(this);
308 #endif
309   this->RenderWindow = NULL;
310   this->SetRenderWindow(vtkRenderWindow::New());
311   this->RenderWindow->Delete();
312 #ifdef __WXMAC__
313   // On Mac (Carbon) we don't get notified of the initial window size with an EVT_SIZE event,
314   // so we update the size information of the interactor/renderwindow here
315   this->UpdateSize(size.x, size.y);
316 #endif
317 }
318 //---------------------------------------------------------------------------
319 wxVTKRenderWindowInteractor::~wxVTKRenderWindowInteractor()
320 {
321   SetRenderWindow(NULL);
322   SetInteractorStyle(NULL);
323 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
324   delete this->context;
325 #endif
326 }
327 //---------------------------------------------------------------------------
328 wxVTKRenderWindowInteractor * wxVTKRenderWindowInteractor::New()
329 {
330   // we don't make use of the objectfactory, because we're not registered
331   return new wxVTKRenderWindowInteractor;
332 }
333 //---------------------------------------------------------------------------
334 void wxVTKRenderWindowInteractor::Initialize()
335 {
336   int *size = RenderWindow->GetSize();
337   // enable everything and start rendering
338   Enable();
339   //RenderWindow->Start();
340
341   // set the size in the render window interactor
342   Size[0] = size[0];
343   Size[1] = size[1];
344
345   // this is initialized
346   Initialized = 1;
347 }
348 //---------------------------------------------------------------------------
349 void wxVTKRenderWindowInteractor::Enable()
350 {
351   // if already enabled then done
352   if (Enabled)
353     return;
354
355   // that's it
356   Enabled = 1;
357 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
358   wxGLCanvas::SetCurrent(*this->context);
359 #endif
360   Modified();
361 }
362 //---------------------------------------------------------------------------
363 bool wxVTKRenderWindowInteractor::Enable(bool enable)
364 {
365 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
366   return wxGLCanvas::Enable(enable);
367 #else
368   return wxWindow::Enable(enable);
369 #endif
370 }
371 //---------------------------------------------------------------------------
372 void wxVTKRenderWindowInteractor::Disable()
373 {
374   // if already disabled then done
375   if (!Enabled)
376     return;
377
378   // that's it (we can't remove the event handler like it should be...)
379   Enabled = 0;
380   Modified();
381 }
382 //---------------------------------------------------------------------------
383 void wxVTKRenderWindowInteractor::Start()
384 {
385   // the interactor cannot control the event loop
386   vtkErrorMacro( << "wxVTKRenderWindowInteractor::Start() "
387     "interactor cannot control event loop.");
388 }
389 //---------------------------------------------------------------------------
390 void wxVTKRenderWindowInteractor::UpdateSize(int x, int y)
391 {
392   if( RenderWindow )
393   {
394     // if the size changed tell render window
395     if ( x != Size[0] || y != Size[1] )
396     {
397       // adjust our (vtkRenderWindowInteractor size)
398       Size[0] = x;
399       Size[1] = y;
400       // and our RenderWindow's size
401
402 #ifdef __WXCOCOA__
403   #ifdef VTK_USE_COCOA
404   #else
405   #endif //VTK_USE_COCOA
406 #else
407       RenderWindow->SetSize(x, y);
408 #endif //__WXCOCOA__
409       
410 #if defined(__WXMSW__)
411       this->Refresh();
412 #endif //__WXMSW__
413     } // if x y
414   } // if RenderWindow
415 }
416 //---------------------------------------------------------------------------
417 int wxVTKRenderWindowInteractor::CreateTimer(int WXUNUSED(timertype))
418 {
419   // it's a one shot timer
420   if (!timer.Start(10, TRUE))
421     return 0;
422
423   return 1;
424   
425 }
426 #if VTK_MAJOR_VERSION > 5 || (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION >= 2)
427 //------------------------------------------------------------------
428 int wxVTKRenderWindowInteractor::InternalCreateTimer(int timerId, int timerType,
429                                                      unsigned long duration)
430 {
431   if (!timer.Start(duration, timerType == OneShotTimer))
432     return 0;
433     
434   return ID_wxVTKRenderWindowInteractor_TIMER;
435 }
436 //------------------------------------------------------------------
437 int wxVTKRenderWindowInteractor::InternalDestroyTimer(int platformTimerId)
438 {
439   timer.Stop();
440   return 1;
441 }
442 #endif
443 //---------------------------------------------------------------------------
444 int wxVTKRenderWindowInteractor::DestroyTimer()
445 {
446   // do nothing
447   return 1;
448 }
449 //---------------------------------------------------------------------------
450 void wxVTKRenderWindowInteractor::OnTimer(wxTimerEvent& WXUNUSED(event))
451 {
452   if (!Enabled)
453     return;
454     
455 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
456   // new style
457 #if VTK_MAJOR_VERSION > 5 || (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION >= 2)
458   // pass the right timer id
459   int timerId = this->GetCurrentTimerId();
460   this->InvokeEvent(vtkCommand::TimerEvent, &timerId);
461 #else
462   this->InvokeEvent(vtkCommand::TimerEvent, NULL);
463 #endif
464 #else
465   // old style
466   InteractorStyle->OnTimer();
467 #endif
468 }
469
470 //---------------------------------------------------------------------------
471 // NOTE on implementation:
472 // Bad luck you ended up in the only tricky place of this code.
473 // A few note, wxWidgets still refuse to provide such convenient method
474 // so I have to maintain it myself, eventhough this is completely integrated
475 // in wxPython...
476 // Anyway if this happen to break for you then compare to a recent version of wxPython
477 // and look for the function long wxPyGetWinHandle(wxWindow* win)
478 // in wxPython/src/helpers.cpp
479 long wxVTKRenderWindowInteractor::GetHandleHack()
480 {
481   //helper function to hide the MSW vs GTK stuff
482   long handle_tmp = 0;
483
484 // __WXMSW__ is for Win32
485 // __WXMAC__ is for Carbon or Cocoa builds
486 // __WXGTK__ is for both gtk 1.2.x and gtk 2.x
487 #if defined(__WXMSW__) || defined(__WXMAC__)
488     handle_tmp = (long)this->GetHandle();
489 #endif //__WXMSW__
490
491 // using above GetHandle() works fine with wxOSX 2.9.x
492 #if defined(__WXCOCOA__) && !wxCHECK_VERSION(2, 9, 0)
493    // Here is how to find the NSWindow
494    wxTopLevelWindow* toplevel = dynamic_cast<wxTopLevelWindow*>(wxGetTopLevelParent( this ) );
495    if (toplevel != NULL )
496    {
497       handle_tmp = (long)toplevel->GetNSWindow();
498    }
499    // The NSView will be deducted from 
500    // [(NSWindow*)Handle contentView]
501    // if only I knew how to write that in c++
502 #endif //__WXCOCOA__ && !wxCHECK_VERSION(2, 9, 0)
503
504     // Find and return the actual X-Window.
505 #if defined(__WXGTK__) || defined(__WXX11__)
506     return (long)GetXWindow(this);
507 #endif
508
509 //#ifdef __WXMOTIF__
510 //    handle_tmp = (long)this->GetXWindow();
511 //#endif
512
513   return handle_tmp;
514 }
515 //---------------------------------------------------------------------------
516 void wxVTKRenderWindowInteractor::OnPaint(wxPaintEvent& WXUNUSED(event))
517 {
518
519   //must always be here
520 //EED2021-08-26  wxPaintDC pDC(this);
521
522   //do it here rather than in the cstor: this is safer.
523   if(!Handle)
524   {
525     Handle = GetHandleHack();
526         
527 #ifdef __WXCOCOA__
528         vtkCocoaRenderWindow    *rwin           = vtkCocoaRenderWindow::SafeDownCast(RenderWindow);
529 //2021-08-30
530 //      rwin->SetWantsBestResolution(true);
531       
532     NSView                                      *nvsview        = (NSView* )Handle;
533         NSWindow                                *nswindow       = nvsview.window;
534     rwin->SetRootWindow( nswindow );
535     rwin->SetWindowId(  reinterpret_cast<void *>(nvsview) );
536 #else   
537     RenderWindow->SetWindowId(reinterpret_cast<void *>(Handle));
538 #endif
539         
540 // Cocoa
541 // this->GetNSView() <-> DisplayId
542 // this->GetTopLevel()->GetNSWindow() <-> WindowId
543 #ifdef __WXMSW__
544     RenderWindow->SetParentId(reinterpret_cast<void *>(this->GetParent()->GetHWND()));
545 #endif //__WXMSW__
546       
547     // This is another hack to prevent the VTK Render Window from closing the display.
548     // If VTK closes the display, ~wxContext chashes while trying to destroy its
549     // glContext (because the display is closed). The Get -> Set makes this VTK
550     // object think someone else is responsible for the display. 
551     #ifdef __WXCOCOA__
552       // avoid "Method not implemented" messages in Console
553     #else
554       this->RenderWindow->SetDisplayId(this->RenderWindow->GetGenericDisplayId());
555     #endif
556   }
557   // get vtk to render to the wxWindows
558
559 Render();
560 #ifdef __WXMAC__
561   // This solves a problem with repainting after a window resize
562   // See also: http://sourceforge.net/mailarchive/forum.php?thread_id=31690967&forum_id=41789
563 #ifdef __WXCOCOA__
564   #if !wxCHECK_VERSION(2, 9, 0)
565     // this doesn't seem necessary with wxOSX 2.9.x
566     vtkCocoaRenderWindow * rwin = vtkCocoaRenderWindow::SafeDownCast(RenderWindow);
567     if( rwin )
568     {
569       rwin->UpdateContext();
570     }
571   #endif
572 #else
573     
574   vtkCarbonRenderWindow* rwin = vtkCarbonRenderWindow::SafeDownCast(RenderWindow);
575   if( rwin )
576   {
577 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 4)
578     // Must be somewhere after VTK 4.4
579     rwin->UpdateGLRegion();
580 #endif
581   }
582 #endif
583 #endif
584 }
585
586 //---------------------------------------------------------------------------
587 void wxVTKRenderWindowInteractor::OnEraseBackground(wxEraseEvent &event)
588 {
589   //turn off background erase to reduce flickering on MSW
590   event.Skip(false);
591 }
592 //---------------------------------------------------------------------------
593 void wxVTKRenderWindowInteractor::OnSize(wxSizeEvent& WXUNUSED(event))
594 {
595   int w, h;
596   GetClientSize(&w, &h);
597   UpdateSize(w, h);
598
599   if (!Enabled) 
600     {
601     return;
602     }
603
604 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
605   InvokeEvent(vtkCommand::ConfigureEvent, NULL);
606 #endif
607   //this will check for Handle
608   //Render();
609
610 }
611
612 //---------------------------------------------------------------------------
613 void wxVTKRenderWindowInteractor::OnMotion(wxMouseEvent &event)
614 {
615  if (!Enabled) 
616     {
617     return;
618     }
619 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
620
621   SetEventInformationFlipY(event.GetX(), event.GetY(), 
622     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
623
624   InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
625 #else
626   InteractorStyle->OnMouseMove(event.ControlDown(), event.ShiftDown(),
627     event.GetX(), Size[1] - event.GetY() - 1);
628 #endif
629 }
630 //---------------------------------------------------------------------------
631 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
632 void wxVTKRenderWindowInteractor::OnEnter(wxMouseEvent &event)
633 {
634   if (!Enabled) 
635     {
636     return;
637     }
638
639 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
640     // new style
641   SetEventInformationFlipY(event.GetX(), event.GetY(), 
642       event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
643
644   InvokeEvent(vtkCommand::EnterEvent, NULL);
645 #else
646     // old style
647   InteractorStyle->OnEnter(event.ControlDown(), event.ShiftDown(),
648       event.GetX(), Size[1] - event.GetY() - 1);  
649 #endif
650 }
651 //---------------------------------------------------------------------------
652 void wxVTKRenderWindowInteractor::OnLeave(wxMouseEvent &event)
653 {
654   if (!Enabled) 
655     {
656     return;
657     }
658
659 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
660     // new style
661   SetEventInformationFlipY(event.GetX(), event.GetY(), 
662       event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
663
664   InvokeEvent(vtkCommand::LeaveEvent, NULL);
665 #else
666     // old style
667   InteractorStyle->OnLeave(event.ControlDown(), event.ShiftDown(),
668       event.GetX(), Size[1] - event.GetY() - 1);  
669 #endif
670 }
671 //---------------------------------------------------------------------------
672 void wxVTKRenderWindowInteractor::OnKeyDown(wxKeyEvent &event)
673 {
674   if (!Enabled) 
675     {
676     return;
677     }
678
679 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
680     // new style
681   int keycode = event.GetKeyCode();
682   char key = '\0';
683   if (((unsigned int)keycode) < 256)
684   {
685     // TODO: Unicode in non-Unicode mode ??
686     key = (char)keycode;
687   }
688
689   // we don't get a valid mouse position inside the key event on every platform
690   // so we retrieve the mouse position explicitly and pass it along
691   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
692   SetEventInformationFlipY(mousePos.x, mousePos.y, 
693                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
694   InvokeEvent(vtkCommand::KeyPressEvent, NULL);
695 #else
696   InteractorStyle->OnKeyDown(event.ControlDown(), event.ShiftDown(), 
697     event.GetKeyCode(), 1);
698 #endif
699   event.Skip();
700 }
701 //---------------------------------------------------------------------------
702 void wxVTKRenderWindowInteractor::OnKeyUp(wxKeyEvent &event)
703 {
704   if (!Enabled) 
705     {
706     return;
707     }
708
709 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
710     // new style
711   int keycode = event.GetKeyCode();
712   char key = '\0';
713   if (((unsigned int)keycode) < 256)
714   {
715     // TODO: Unicode in non-Unicode mode ??
716     key = (char)keycode;
717   }
718
719   // we don't get a valid mouse position inside the key event on every platform
720   // so we retrieve the mouse position explicitly and pass it along
721   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
722   SetEventInformationFlipY(mousePos.x, mousePos.y, 
723                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
724   InvokeEvent(vtkCommand::KeyReleaseEvent, NULL);
725 #else
726   InteractorStyle->OnKeyUp(event.ControlDown(), event.ShiftDown(), 
727     event.GetKeyCode(), 1);
728 #endif
729   event.Skip();
730 }
731 #endif //!(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
732  //---------------------------------------------------------------------------
733 void wxVTKRenderWindowInteractor::OnChar(wxKeyEvent &event)
734 {
735   if (!Enabled) 
736     {
737     return;
738     }
739     
740 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
741   // new style
742   int keycode = event.GetKeyCode();
743   char key = '\0';
744   if (((unsigned int)keycode) < 256)
745     {
746     // TODO: Unicode in non-Unicode mode ??
747     key = (char)keycode;
748     }
749
750   // we don't get a valid mouse position inside the key event on every platform
751   // so we retrieve the mouse position explicitly and pass it along
752   wxPoint mousePos = ScreenToClient(wxGetMousePosition());
753   SetEventInformationFlipY(mousePos.x, mousePos.y, 
754                            event.ControlDown(), event.ShiftDown(), key, 0, NULL);
755   InvokeEvent(vtkCommand::CharEvent, NULL);
756 #endif
757   event.Skip();
758 }
759 //---------------------------------------------------------------------------
760 void wxVTKRenderWindowInteractor::OnButtonDown(wxMouseEvent &event)
761 {
762   if (!Enabled || (ActiveButton != wxEVT_NULL))
763     {
764     return;
765     }
766   ActiveButton = event.GetEventType();
767
768     // On Mac (Carbon) and Windows we don't automatically get the focus when
769     // you click inside the window
770     // we therefore set the focus explicitly
771     // Apparently we need that on linux (GTK) too:
772     this->SetFocus();
773
774 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
775   SetEventInformationFlipY(event.GetX(), event.GetY(), 
776     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
777 #endif
778
779   if(event.RightDown())
780   {
781 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
782     // new style
783     InvokeEvent(vtkCommand::RightButtonPressEvent, NULL);
784 #else            
785     // old style
786     InteractorStyle->OnRightButtonDown(event.ControlDown(), event.ShiftDown(),
787       event.GetX(), Size[1] - event.GetY() - 1);
788 #endif
789   }
790   else if(event.LeftDown())
791   {
792 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
793     // new style
794     InvokeEvent(vtkCommand::LeftButtonPressEvent, NULL);
795 #else            
796     // old style
797     InteractorStyle->OnLeftButtonDown(event.ControlDown(),  event.ShiftDown(),
798       event.GetX(), Size[1] - event.GetY() - 1);
799 #endif
800   }
801   else if(event.MiddleDown())
802   {
803 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
804     // new style
805     InvokeEvent(vtkCommand::MiddleButtonPressEvent, NULL);
806 #else            
807     // old style
808     InteractorStyle->OnMiddleButtonDown(event.ControlDown(), event.ShiftDown(),
809       event.GetX(), Size[1] - event.GetY() - 1);
810 #endif
811   }
812   //save the button and capture mouse until the button is released
813   //Only if :
814   //1. it is possible (WX_USE_X_CAPTURE)
815   //2. user decided to.
816   if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
817   {
818     CaptureMouse();
819   }
820 }
821 //---------------------------------------------------------------------------
822 void wxVTKRenderWindowInteractor::OnButtonUp(wxMouseEvent &event)
823 {
824   //EVT_xxx_DOWN == EVT_xxx_UP - 1
825   //This is only needed if two mouse buttons are pressed at the same time.
826   //In wxWindows 2.4 and later: better use of wxMOUSE_BTN_RIGHT or 
827   //wxEVT_COMMAND_RIGHT_CLICK
828   if (!Enabled || (ActiveButton != (event.GetEventType()-1))) 
829     {
830     return;
831     }
832
833   // See report by Shang Mu / Kerry Loux on wxVTK mailing list
834     this->SetFocus();
835
836 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
837   SetEventInformationFlipY(event.GetX(), event.GetY(), 
838     event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
839 #endif
840   
841   if(ActiveButton == wxEVT_RIGHT_DOWN)
842   {
843 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
844     // new style
845     InvokeEvent(vtkCommand::RightButtonReleaseEvent, NULL);
846 #else            
847     // old style
848     InteractorStyle->OnRightButtonUp(event.ControlDown(), event.ShiftDown(),
849       event.GetX(), Size[1] - event.GetY() - 1);
850 #endif
851   }
852   else if(ActiveButton == wxEVT_LEFT_DOWN)
853   {
854 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
855     // new style
856     InvokeEvent(vtkCommand::LeftButtonReleaseEvent, NULL);
857 #else            
858     // old style
859     InteractorStyle->OnLeftButtonUp(event.ControlDown(), event.ShiftDown(),
860       event.GetX(), Size[1] - event.GetY() - 1);
861 #endif
862   }
863   else if(ActiveButton == wxEVT_MIDDLE_DOWN)
864   {
865 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
866     // new style
867     InvokeEvent(vtkCommand::MiddleButtonReleaseEvent, NULL);
868 #else            
869     // old style
870     InteractorStyle->OnMiddleButtonUp(event.ControlDown(), event.ShiftDown(),
871       event.GetX(), Size[1] - event.GetY() - 1);
872 #endif
873   }
874   //if the ActiveButton is realeased, then release mouse capture
875   if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
876   {
877     ReleaseMouse();
878   }
879   ActiveButton = wxEVT_NULL;
880 }
881 //---------------------------------------------------------------------------
882 void wxVTKRenderWindowInteractor::OnMouseWheel(wxMouseEvent& event)
883 {
884 // Mouse wheel was only added after VTK 4.4 (I think...)
885 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 4)
886   // new style
887   //Set vtk event information ... The numebr of wheel rotations is stored in
888   //the x varible.  y varible is zero
889   SetEventInformationFlipY(event.GetX() , event.GetY(), 
890                            event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
891   if(event.GetWheelRotation() > 0)
892     {
893       //Send event to VTK
894       InvokeEvent(vtkCommand::MouseWheelForwardEvent, NULL);
895     }
896   else
897     {
898       //Send event to VTK
899       InvokeEvent(vtkCommand::MouseWheelBackwardEvent, NULL);
900     }
901 #endif
902     
903 }
904
905 //---------------------------------------------------------------------------
906 #if wxCHECK_VERSION(2, 8, 0)
907 void wxVTKRenderWindowInteractor::OnMouseCaptureLost(wxMouseCaptureLostEvent& event)
908 {
909    if (ActiveButton != wxEVT_NULL)
910    {
911        //Maybe also invoke the button release event here
912    }
913    // Reset ActiveButton so that
914    // 1. we do not process mouse button up events any more,
915    // 2. the next button down event will be processed and call CaptureMouse().
916    // Otherwise ReleaseMouse() will be called
917    // without a previous CaptureMouse().
918    ActiveButton = wxEVT_NULL;
919 }
920 #endif
921
922 //---------------------------------------------------------------------------
923 void wxVTKRenderWindowInteractor::Render()
924 {
925 #if wxCHECK_VERSION(2, 8, 0)
926   int renderAllowed = !IsFrozen();
927 #else
928   int renderAllowed = 1;
929 #endif
930   if (renderAllowed && !RenderWhenDisabled)
931     {
932     //the user doesn't want us to render when the toplevel frame
933     //is disabled - first find the top level parent
934     wxWindow *topParent = wxGetTopLevelParent(this);
935     if (topParent)
936       {
937       //if it exists, check whether it's enabled
938       //if it's not enabeld, renderAllowed will be false
939       renderAllowed = topParent->IsEnabled();
940       }
941     }
942
943   if (renderAllowed)
944     {
945 #if defined(__WXGTK__) && defined(USE_WXGLCANVAS)
946     wxGLCanvas::SetCurrent(*(this->context));
947 #endif
948     if(Handle && (Handle == GetHandleHack()) )
949       {
950       RenderWindow->Render();
951       }
952 #if VTK_MAJOR_VERSION > 4 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 2)
953     else if(GetHandleHack())
954       {
955       //this means the user has reparented us; let's adapt to the
956       //new situation by doing the WindowRemap dance
957       //store the new situation
958       Handle = GetHandleHack();
959       RenderWindow->SetNextWindowId(reinterpret_cast<void *>(Handle));
960       RenderWindow->WindowRemap();
961       RenderWindow->Render();
962       }
963 #endif
964
965     }
966 }
967 //---------------------------------------------------------------------------
968 void wxVTKRenderWindowInteractor::SetRenderWhenDisabled(int newValue)
969 {
970   //Change value of __RenderWhenDisabled ivar.
971   //If __RenderWhenDisabled is false (the default), this widget will not
972   //call Render() on the RenderWindow if the top level frame (i.e. the
973   //containing frame) has been disabled.
974
975   //This prevents recursive rendering during wxSafeYield() calls.
976   //wxSafeYield() can be called during the ProgressMethod() callback of
977   //a VTK object to have progress bars and other GUI elements updated -
978   //it does this by disabling all windows (disallowing user-input to
979   //prevent re-entrancy of code) and then handling all outstanding
980   //GUI events.
981         
982   //However, this often triggers an OnPaint() method for wxVTKRWIs,
983   //resulting in a Render(), resulting in Update() being called whilst
984   //still in progress.
985
986   RenderWhenDisabled = (bool)newValue;
987 }
988 //---------------------------------------------------------------------------
989 //
990 // Set the variable that indicates that we want a stereo capable window
991 // be created. This method can only be called before a window is realized.
992 //
993 void wxVTKRenderWindowInteractor::SetStereo(int capable)
994 {
995   if (Stereo != capable)
996     {
997     Stereo = capable;
998     RenderWindow->StereoCapableWindowOn();
999     RenderWindow->SetStereoTypeToCrystalEyes();
1000     Modified();
1001     }
1002 }
1003
1004 //---------------------------------------------------------------------------
1005 //
1006 //
1007 void wxVTKRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
1008 {
1009   this->Superclass::PrintSelf(os, indent);
1010 }
1011
1012
1013 }
1014 // LG : EO namespace bbwxvtk
1015 //=======================================================================
1016
1017