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