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