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