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