00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "gui/wxvtk/wxVTKRenderWindowInteractor.h"
00017
00018
00019 #ifndef VTK_MAJOR_VERSION
00020 # include "vtkVersion.h"
00021 #endif
00022
00023 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00024 # include "vtkCommand.h"
00025 #else
00026 # include "vtkInteractorStyle.h"
00027 #endif
00028
00029
00030 #if (!wxCHECK_VERSION(2, 4, 0))
00031 wxWindow* wxGetTopLevelParent(wxWindow *win)
00032 {
00033 while ( win && !win->IsTopLevel() )
00034 win = win->GetParent();
00035 return win;
00036 }
00037 #endif
00038
00039
00040 #ifdef __WXCOCOA__
00041 #ifdef VTK_USE_COCOA
00042 #import <Cocoa/Cocoa.h>
00043
00044 #define id Id
00045 #else
00046 #error Build mismatch you need both wxWidgets and VTK to be configure agains Cocoa to work
00047 #endif //VTK_USE_COCOA
00048 #endif //__WXCOCOA__
00049
00050 #ifdef __WXGTK__
00051 # include "gdk/gdkprivate.h"
00052 # include <wx/gtk/win_gtk.h>
00053 # include <gdk/gdkx.h>
00054 #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
00055 GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
00056 GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
00057 #endif
00058
00059 #ifdef __WXX11__
00060 #include "wx/x11/privx.h"
00061 #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
00062 #endif
00063
00064
00065
00066
00067
00068 #if wxCHECK_VERSION(2, 3, 2) && !wxCHECK_VERSION(2, 4, 1) && defined(__WXGTK__)
00069 # define WX_USE_X_CAPTURE 0
00070 #else
00071 # define WX_USE_X_CAPTURE 1
00072 #endif
00073
00074 #define ID_wxVTKRenderWindowInteractor_TIMER 1001
00075
00076 #ifdef __WXGTK__
00077 IMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxGLCanvas)
00078 #else
00079 IMPLEMENT_DYNAMIC_CLASS(wxVTKRenderWindowInteractor, wxWindow)
00080 #endif //__WXGTK__
00081
00082
00083 #ifdef __WXGTK__
00084 BEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxGLCanvas)
00085 #else
00086 BEGIN_EVENT_TABLE(wxVTKRenderWindowInteractor, wxWindow)
00087 #endif //__WXGTK__
00088
00089 EVT_PAINT (wxVTKRenderWindowInteractor::OnPaint)
00090 EVT_ERASE_BACKGROUND(wxVTKRenderWindowInteractor::OnEraseBackground)
00091 EVT_MOTION (wxVTKRenderWindowInteractor::OnMotion)
00092
00093
00094 EVT_LEFT_DOWN (wxVTKRenderWindowInteractor::OnButtonDown)
00095 EVT_MIDDLE_DOWN (wxVTKRenderWindowInteractor::OnButtonDown)
00096 EVT_RIGHT_DOWN (wxVTKRenderWindowInteractor::OnButtonDown)
00097 EVT_LEFT_UP (wxVTKRenderWindowInteractor::OnButtonUp)
00098 EVT_MIDDLE_UP (wxVTKRenderWindowInteractor::OnButtonUp)
00099 EVT_RIGHT_UP (wxVTKRenderWindowInteractor::OnButtonUp)
00100 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
00101 EVT_ENTER_WINDOW(wxVTKRenderWindowInteractor::OnEnter)
00102 EVT_LEAVE_WINDOW(wxVTKRenderWindowInteractor::OnLeave)
00103
00104
00105
00106
00107 EVT_CHAR (wxVTKRenderWindowInteractor::OnKeyDown)
00108 EVT_KEY_UP (wxVTKRenderWindowInteractor::OnKeyUp)
00109 #endif
00110 EVT_TIMER (ID_wxVTKRenderWindowInteractor_TIMER, wxVTKRenderWindowInteractor::OnTimer)
00111 EVT_SIZE (wxVTKRenderWindowInteractor::OnSize)
00112 END_EVENT_TABLE()
00113
00114
00115 #ifdef __WXGTK__
00116
00117 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : vtkRenderWindowInteractor(),
00118 wxGLCanvas(NULL,-1,NULL,wxDefaultPosition,wxDefaultSize,0,wxGLCanvasName,wxNullPalette)
00119 #else
00120 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor() : vtkRenderWindowInteractor(), wxWindow()
00121 #endif
00122 , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
00123 , ActiveButton(wxEVT_NULL)
00124 , RenderAllowed(0)
00125 , Stereo(0)
00126 , Handle(0)
00127 , Created(true)
00128 , RenderWhenDisabled(1)
00129 , UseCaptureMouse(0)
00130 {
00131 this->RenderWindow = NULL;
00132 this->SetRenderWindow(vtkRenderWindow::New());
00133 this->RenderWindow->Delete();
00134 }
00135
00136 wxVTKRenderWindowInteractor::wxVTKRenderWindowInteractor(wxWindow *parent,
00137 wxWindowID id,
00138 const wxPoint &pos,
00139 const wxSize &size,
00140 long style,
00141 const wxString &name)
00142 #ifdef __WXGTK__
00143 : vtkRenderWindowInteractor(), wxGLCanvas(parent, id, pos, size, style, name)
00144 #else
00145 : vtkRenderWindowInteractor(), wxWindow(parent, id, pos, size, style, name)
00146 #endif //__WXGTK__
00147 , timer(this, ID_wxVTKRenderWindowInteractor_TIMER)
00148 , ActiveButton(wxEVT_NULL)
00149 , RenderAllowed(0)
00150 , Stereo(0)
00151 , Handle(0)
00152 , Created(true)
00153 , RenderWhenDisabled(1)
00154 , UseCaptureMouse(0)
00155 {
00156 this->RenderWindow = NULL;
00157 this->SetRenderWindow(vtkRenderWindow::New());
00158 this->RenderWindow->Delete();
00159 }
00160
00161 wxVTKRenderWindowInteractor::~wxVTKRenderWindowInteractor()
00162 {
00163 }
00164
00165 wxVTKRenderWindowInteractor * wxVTKRenderWindowInteractor::New()
00166 {
00167
00168 return new wxVTKRenderWindowInteractor;
00169 }
00170
00171 void wxVTKRenderWindowInteractor::Initialize()
00172 {
00173 int *size = RenderWindow->GetSize();
00174
00175 Enable();
00176
00177
00178
00179 Size[0] = size[0];
00180 Size[1] = size[1];
00181
00182
00183 Initialized = 1;
00184 }
00185
00186 void wxVTKRenderWindowInteractor::Enable()
00187 {
00188
00189 if (Enabled)
00190 return;
00191
00192
00193 Enabled = 1;
00194 #ifdef __WXGTK__
00195 this->SetCurrent();
00196 #endif
00197 Modified();
00198 }
00199
00200 bool wxVTKRenderWindowInteractor::Enable(bool enable)
00201 {
00202 #ifdef __WXGTK__
00203 return wxGLCanvas::Enable(enable);
00204 #else
00205 return wxWindow::Enable(enable);
00206 #endif
00207 }
00208
00209 void wxVTKRenderWindowInteractor::Disable()
00210 {
00211
00212 if (!Enabled)
00213 return;
00214
00215
00216 Enabled = 0;
00217 Modified();
00218 }
00219
00220 void wxVTKRenderWindowInteractor::Start()
00221 {
00222
00223 vtkErrorMacro(<<"wxVTKRenderWindowInteractor::Start() \
00224 interactor cannot control event loop.");
00225 }
00226
00227 void wxVTKRenderWindowInteractor::UpdateSize(int x, int y)
00228 {
00229 if( RenderWindow )
00230 {
00231
00232 if ( x != Size[0] || y != Size[1] )
00233 {
00234
00235 Size[0] = x;
00236 Size[1] = y;
00237
00238 RenderWindow->SetSize(x, y);
00239 }
00240 }
00241 }
00242
00243 int wxVTKRenderWindowInteractor::CreateTimer(int WXUNUSED(timertype))
00244 {
00245
00246 if (!timer.Start(10, TRUE))
00247 assert(false);
00248
00249 return 1;
00250
00251 }
00252
00253 int wxVTKRenderWindowInteractor::DestroyTimer()
00254 {
00255
00256 return 1;
00257 }
00258
00259 void wxVTKRenderWindowInteractor::OnTimer(wxTimerEvent& WXUNUSED(event))
00260 {
00261 if (!Enabled)
00262 return;
00263
00264 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00265
00266 InvokeEvent(vtkCommand::TimerEvent, NULL);
00267 #else
00268
00269 InteractorStyle->OnTimer();
00270 #endif
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 long wxVTKRenderWindowInteractor::GetHandleHack()
00283 {
00284
00285 long handle_tmp = 0;
00286
00287
00288
00289
00290 #if defined(__WXMSW__) || defined(__WXMAC__)
00291 handle_tmp = (long)this->GetHandle();
00292 #endif //__WXMSW__
00293
00294
00295 #ifdef __WXCOCOA__
00296
00297 wxTopLevelWindow* toplevel = dynamic_cast<wxTopLevelWindow*>(
00298 wxGetTopLevelParent( this ) );
00299 if (toplevel != NULL )
00300 {
00301 handle_tmp = (long)toplevel->GetNSWindow();
00302 }
00303
00304
00305
00306 #endif //__WXCOCOA__
00307
00308
00309 #if defined(__WXGTK__) || defined(__WXX11)
00310 return (long)GetXWindow(this);
00311 #endif
00312
00313
00314
00315
00316
00317 return handle_tmp;
00318 }
00319
00320 void wxVTKRenderWindowInteractor::OnPaint(wxPaintEvent& WXUNUSED(event))
00321 {
00322
00323 wxPaintDC pDC(this);
00324
00325
00326 if(!Handle)
00327 {
00328 Handle = GetHandleHack();
00329 RenderWindow->SetWindowId(reinterpret_cast<void *>(Handle));
00330 #ifdef __WXMSW__
00331 RenderWindow->SetParentId(reinterpret_cast<void *>(this->GetParent()->GetHWND()));
00332 #endif //__WXMSW__
00333 }
00334
00335
00336 #ifndef __WXMSW__
00337 #ifndef __WXMAC__
00338 this->SetCurrent();
00339 #endif // __WXMAC__
00340 #endif //__WXMSW__
00341
00342 Render();
00343 }
00344
00345 void wxVTKRenderWindowInteractor::OnEraseBackground(wxEraseEvent &event)
00346 {
00347
00348 event.Skip(false);
00349 }
00350
00351 void wxVTKRenderWindowInteractor::OnSize(wxSizeEvent& WXUNUSED(event))
00352 {
00353 int w, h;
00354 GetClientSize(&w, &h);
00355 UpdateSize(w, h);
00356
00357 if (!Enabled)
00358 {
00359 return;
00360 }
00361
00362 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00363 InvokeEvent(vtkCommand::ConfigureEvent, NULL);
00364 #endif
00365
00366
00367 }
00368
00369 void wxVTKRenderWindowInteractor::OnMotion(wxMouseEvent &event)
00370 {
00371 if (!Enabled)
00372 {
00373 return;
00374 }
00375 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00376 SetEventInformationFlipY(event.GetX(), event.GetY(),
00377 event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
00378
00379 InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
00380 #else
00381 InteractorStyle->OnMouseMove(event.ControlDown(), event.ShiftDown(),
00382 event.GetX(), Size[1] - event.GetY() - 1);
00383 #endif
00384 }
00385
00386 #if !(VTK_MAJOR_VERSION == 3 && VTK_MINOR_VERSION == 1)
00387 void wxVTKRenderWindowInteractor::OnEnter(wxMouseEvent &event)
00388 {
00389 if (!Enabled)
00390 {
00391 return;
00392 }
00393
00394 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00395
00396 SetEventInformationFlipY(event.GetX(), event.GetY(),
00397 event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
00398
00399 InvokeEvent(vtkCommand::EnterEvent, NULL);
00400 #else
00401
00402 InteractorStyle->OnEnter(event.ControlDown(), event.ShiftDown(),
00403 event.GetX(), Size[1] - event.GetY() - 1);
00404 #endif
00405 }
00406
00407 void wxVTKRenderWindowInteractor::OnLeave(wxMouseEvent &event)
00408 {
00409 if (!Enabled)
00410 {
00411 return;
00412 }
00413
00414 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00415
00416 SetEventInformationFlipY(event.GetX(), event.GetY(),
00417 event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
00418
00419 InvokeEvent(vtkCommand::LeaveEvent, NULL);
00420 #else
00421
00422 InteractorStyle->OnLeave(event.ControlDown(), event.ShiftDown(),
00423 event.GetX(), Size[1] - event.GetY() - 1);
00424 #endif
00425 }
00426
00427 void wxVTKRenderWindowInteractor::OnKeyDown(wxKeyEvent &event)
00428 {
00429 if (!Enabled)
00430 {
00431 return;
00432 }
00433
00434 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00435
00436 int keycode = event.GetKeyCode();
00437 char key = '\0';
00438 if (keycode < 256)
00439 {
00440
00441 key = (char)keycode;
00442 }
00443
00444 SetEventInformationFlipY(event.GetX(), event.GetY(),
00445 event.ControlDown(), event.ShiftDown(), key, 0, NULL);
00446
00447 InvokeEvent(vtkCommand::KeyPressEvent, NULL);
00448 InvokeEvent(vtkCommand::CharEvent, NULL);
00449 #else
00450 InteractorStyle->OnKeyDown(event.ControlDown(), event.ShiftDown(),
00451 event.GetKeyCode(), 1);
00452 #endif
00453 event.Skip();
00454 }
00455
00456 void wxVTKRenderWindowInteractor::OnKeyUp(wxKeyEvent &event)
00457 {
00458 if (!Enabled)
00459 {
00460 return;
00461 }
00462
00463 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00464
00465 int keycode = event.GetKeyCode();
00466 char key = '\0';
00467 if (keycode < 256)
00468 {
00469
00470 key = (char)keycode;
00471 }
00472
00473 SetEventInformationFlipY(event.GetX(), event.GetY(),
00474 event.ControlDown(), event.ShiftDown(), key, 0, NULL);
00475 InvokeEvent(vtkCommand::KeyReleaseEvent, NULL);
00476 #else
00477 InteractorStyle->OnKeyUp(event.ControlDown(), event.ShiftDown(),
00478 event.GetKeyCode(), 1);
00479 #endif
00480 event.Skip();
00481 }
00482 #endif
00483 //---------------------------------------------------------------------------
00484 void wxVTKRenderWindowInteractor::OnButtonDown(wxMouseEvent &event)
00485 {
00486 if (!Enabled || (ActiveButton != wxEVT_NULL))
00487 {
00488 return;
00489 }
00490 ActiveButton = event.GetEventType();
00491
00492 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00493 SetEventInformationFlipY(event.GetX(), event.GetY(),
00494 event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
00495 #endif
00496
00497 if(event.RightDown())
00498 {
00499 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00500
00501 InvokeEvent(vtkCommand::RightButtonPressEvent, NULL);
00502 #else
00503
00504 InteractorStyle->OnRightButtonDown(event.ControlDown(), event.ShiftDown(),
00505 event.GetX(), Size[1] - event.GetY() - 1);
00506 #endif
00507 }
00508 else if(event.LeftDown())
00509 {
00510 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00511
00512 InvokeEvent(vtkCommand::LeftButtonPressEvent, NULL);
00513 #else
00514
00515 InteractorStyle->OnLeftButtonDown(event.ControlDown(), event.ShiftDown(),
00516 event.GetX(), Size[1] - event.GetY() - 1);
00517 #endif
00518 }
00519 else if(event.MiddleDown())
00520 {
00521 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00522
00523 InvokeEvent(vtkCommand::MiddleButtonPressEvent, NULL);
00524 #else
00525
00526 InteractorStyle->OnMiddleButtonDown(event.ControlDown(), event.ShiftDown(),
00527 event.GetX(), Size[1] - event.GetY() - 1);
00528 #endif
00529 }
00530
00531
00532
00533
00534 if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
00535 {
00536 CaptureMouse();
00537 }
00538 }
00539
00540 void wxVTKRenderWindowInteractor::OnButtonUp(wxMouseEvent &event)
00541 {
00542
00543
00544
00545
00546 if (!Enabled || (ActiveButton != (event.GetEventType()-1)))
00547 {
00548 return;
00549 }
00550
00551 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00552 SetEventInformationFlipY(event.GetX(), event.GetY(),
00553 event.ControlDown(), event.ShiftDown(), '\0', 0, NULL);
00554 #endif
00555
00556 if(ActiveButton == wxEVT_RIGHT_DOWN)
00557 {
00558 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00559
00560 InvokeEvent(vtkCommand::RightButtonReleaseEvent, NULL);
00561 #else
00562
00563 InteractorStyle->OnRightButtonUp(event.ControlDown(), event.ShiftDown(),
00564 event.GetX(), Size[1] - event.GetY() - 1);
00565 #endif
00566 }
00567 else if(ActiveButton == wxEVT_LEFT_DOWN)
00568 {
00569 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00570
00571 InvokeEvent(vtkCommand::LeftButtonReleaseEvent, NULL);
00572 #else
00573
00574 InteractorStyle->OnLeftButtonUp(event.ControlDown(), event.ShiftDown(),
00575 event.GetX(), Size[1] - event.GetY() - 1);
00576 #endif
00577 }
00578 else if(ActiveButton == wxEVT_MIDDLE_DOWN)
00579 {
00580 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 0)
00581
00582 InvokeEvent(vtkCommand::MiddleButtonReleaseEvent, NULL);
00583 #else
00584
00585 InteractorStyle->OnMiddleButtonUp(event.ControlDown(), event.ShiftDown(),
00586 event.GetX(), Size[1] - event.GetY() - 1);
00587 #endif
00588 }
00589
00590 if ((ActiveButton != wxEVT_NULL) && WX_USE_X_CAPTURE && UseCaptureMouse)
00591 {
00592 ReleaseMouse();
00593 }
00594 ActiveButton = wxEVT_NULL;
00595 }
00596
00597 void wxVTKRenderWindowInteractor::Render()
00598 {
00599 RenderAllowed = 1;
00600 if (!RenderWhenDisabled)
00601 {
00602
00603
00604 wxWindow *topParent = wxGetTopLevelParent(this);
00605 if (topParent)
00606 {
00607
00608
00609 RenderAllowed = topParent->IsEnabled();
00610 }
00611 }
00612
00613 if (RenderAllowed)
00614 {
00615 if(Handle && (Handle == GetHandleHack()) )
00616 {
00617 RenderWindow->Render();
00618 }
00619 #if VTK_MAJOR_VERSION == 5 || (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION > 2)
00620 else if(GetHandleHack())
00621 {
00622
00623
00624
00625 Handle = GetHandleHack();
00626 RenderWindow->SetNextWindowId(reinterpret_cast<void *>(Handle));
00627 RenderWindow->WindowRemap();
00628 RenderWindow->Render();
00629 }
00630 #endif
00631 }
00632 }
00633
00634 void wxVTKRenderWindowInteractor::SetRenderWhenDisabled(int newValue)
00635 {
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652 RenderWhenDisabled = (bool)newValue;
00653 }
00654
00655
00656
00657
00658
00659 void wxVTKRenderWindowInteractor::SetStereo(int capable)
00660 {
00661 if (Stereo != capable)
00662 {
00663 Stereo = capable;
00664 RenderWindow->StereoCapableWindowOn();
00665 RenderWindow->SetStereoTypeToCrystalEyes();
00666 Modified();
00667 }
00668 }
00669
00670
00671
00672
00673 void wxVTKRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
00674 {
00675 this->Superclass::PrintSelf(os, indent);
00676 }