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