1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXRenderWindowTclInteractor.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkXRenderWindowTclInteractor.h"
#include "vtkCallbackCommand.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkXOpenGLRenderWindow.h"
#include <vtksys/stl/map>
#include "vtkTk.h"
//-------------------------------------------------------------------------
vtkStandardNewMacro(vtkXRenderWindowTclInteractor);
//-------------------------------------------------------------------------
// steal the first three elements of the TkMainInfo stuct
// we don't care about the rest of the elements.
struct TkMainInfo
{
int refCount;
struct TkWindow *winPtr;
Tcl_Interp *interp;
};
#if ((TK_MAJOR_VERSION <= 4)||((TK_MAJOR_VERSION == 8)&&(TK_MINOR_VERSION == 0)))
extern TkMainInfo *tkMainWindowList;
#else
extern "C" {TkMainInfo *TkGetMainInfoList();}
#endif
//-------------------------------------------------------------------------
class vtkXTclTimer
{
public:
vtkXTclTimer()
{
this->Interactor = 0;
this->ID = 0;
this->TimerToken = 0;
}
vtkRenderWindowInteractor *Interactor;
int ID;
Tcl_TimerToken TimerToken;
};
//-------------------------------------------------------------------------
extern "C" void vtkXTclTimerProc(ClientData clientData)
{
vtkXTclTimer* timer = static_cast<vtkXTclTimer*>(clientData);
vtkXRenderWindowTclInteractor* me =
static_cast<vtkXRenderWindowTclInteractor*>(timer->Interactor);
int platformTimerId = timer->ID;
int timerId = me->GetVTKTimerId(platformTimerId);
if (me->GetEnabled())
{
me->InvokeEvent(vtkCommand::TimerEvent, &timerId);
}
if (!me->IsOneShotTimer(timerId))
{
me->ResetTimer(timerId);
}
}
//-------------------------------------------------------------------------
// Map between the Tcl native timer token to our own int id. Note this
// is separate from the TimerMap in the vtkRenderWindowInteractor
// superclass. This is used to avoid passing 64-bit values back
// through the "int" return type of InternalCreateTimer.
class vtkXRenderWindowTclInteractorInternals
{
public:
vtkXTclTimer* CreateTimer(vtkRenderWindowInteractor* iren,
int timerId, unsigned long duration)
{
vtkXTclTimer* timer = &this->Timers[timerId];
timer->Interactor = iren;
timer->ID = timerId;
timer->TimerToken = Tcl_CreateTimerHandler(static_cast<int>(duration),
vtkXTclTimerProc,
static_cast<ClientData>(timer));
return timer;
}
int DestroyTimer(int timerId)
{
int destroyed = 0;
vtkXTclTimer* timer = &this->Timers[timerId];
if (0 != timer->ID)
{
Tcl_DeleteTimerHandler(timer->TimerToken);
timer->Interactor = 0;
timer->ID = 0;
timer->TimerToken = 0;
destroyed = 1;
}
this->Timers.erase(timerId);
return destroyed;
}
private:
std::map<int, vtkXTclTimer> Timers;
};
//-------------------------------------------------------------------------
extern "C" int vtkTclEventProc(XtPointer clientData, XEvent *event)
{
Boolean ctd;
vtkXOpenGLRenderWindow *rw;
rw = static_cast<vtkXOpenGLRenderWindow *>
(static_cast<vtkXRenderWindowTclInteractor *>(
clientData)->GetRenderWindow());
if (rw->GetWindowId() == (reinterpret_cast<XAnyEvent *>(event))->window)
{
vtkXRenderWindowInteractorCallback(static_cast<Widget>(NULL), clientData,
event, &ctd);
ctd = 0;
}
else
{
ctd = 1;
}
return !ctd;
}
//-------------------------------------------------------------------------
vtkXRenderWindowTclInteractor::vtkXRenderWindowTclInteractor()
{
this->Internal = new vtkXRenderWindowTclInteractorInternals;
}
//-------------------------------------------------------------------------
vtkXRenderWindowTclInteractor::~vtkXRenderWindowTclInteractor()
{
if (this->Initialized)
{
Tk_DeleteGenericHandler(static_cast<Tk_GenericProc *>(vtkTclEventProc),
static_cast<ClientData>(this));
}
delete this->Internal;
this->Internal = 0;
}
//-------------------------------------------------------------------------
void vtkXRenderWindowTclInteractor::Initialize()
{
if (this->Initialized)
{
return;
}
// make sure we have a RenderWindow
if (!this->RenderWindow)
{
vtkErrorMacro(<<"No RenderWindow defined!");
return;
}
this->Initialized = 1;
vtkXOpenGLRenderWindow* ren =
static_cast<vtkXOpenGLRenderWindow *>(this->RenderWindow);
// Use the same display as tcl/tk:
//
#if ((TK_MAJOR_VERSION <= 4)||((TK_MAJOR_VERSION == 8)&&(TK_MINOR_VERSION == 0)))
ren->SetDisplayId(Tk_Display(tkMainWindowList->winPtr));
#else
ren->SetDisplayId(Tk_Display(TkGetMainInfoList()->winPtr));
#endif
this->DisplayId = ren->GetDisplayId();
// Create a Tcl/Tk event handler:
//
Tk_CreateGenericHandler(static_cast<Tk_GenericProc *>(vtkTclEventProc),
static_cast<ClientData>(this));
ren->Start();
this->WindowId = ren->GetWindowId();
int* size = ren->GetSize();
this->Size[0] = size[0];
this->Size[1] = size[1];
this->Enable();
}
//-------------------------------------------------------------------------
void vtkXRenderWindowTclInteractor::Initialize(XtAppContext app)
{
this->Superclass::Initialize(app);
}
//-------------------------------------------------------------------------
void vtkXRenderWindowTclInteractor::Enable()
{
// avoid cycles of calling Initialize() and Enable()
if (this->Enabled)
{
return;
}
// Select the events that we want to respond to
// (Multiple calls to XSelectInput overrides the previous settings)
XSelectInput(this->DisplayId, this->WindowId,
KeyPressMask | KeyReleaseMask |
ButtonPressMask | ButtonReleaseMask |
ExposureMask | StructureNotifyMask |
EnterWindowMask | LeaveWindowMask |
PointerMotionMask);
// Setup for capturing the window deletion
this->KillAtom = XInternAtom(this->DisplayId,"WM_DELETE_WINDOW",False);
XSetWMProtocols(this->DisplayId,this->WindowId,&this->KillAtom,1);
this->Enabled = 1;
this->Modified();
}
//-------------------------------------------------------------------------
void vtkXRenderWindowTclInteractor::Disable()
{
if (!this->Enabled)
{
return;
}
// Remove the all the events that we registered for EXCEPT for
// StructureNotifyMask event since we need to keep track of the window
// size (we will not render if we are disabled, we simply track the window
// size changes for a possible Enable()). Expose events are disabled.
// (Multiple calls to XSelectInput overrides the previous settings)
XSelectInput(this->DisplayId,this->WindowId,
StructureNotifyMask );
this->Enabled = 0;
this->Modified();
}
//-------------------------------------------------------------------------
void vtkXRenderWindowTclInteractor::Start()
{
// Let the compositing handle the event loop if it wants to.
if (this->HasObserver(vtkCommand::StartEvent) && !this->HandleEventLoop)
{
this->InvokeEvent(vtkCommand::StartEvent, NULL);
return;
}
if (!this->Initialized)
{
this->Initialize();
}
if (!this->Initialized)
{
return;
}
this->BreakLoopFlag = 0;
do
{
Tk_DoOneEvent(0);
}
while (this->BreakLoopFlag == 0);
}
//-------------------------------------------------------------------------
int vtkXRenderWindowTclInteractor::InternalCreateTimer(int timerId,
int vtkNotUsed(timerType),
unsigned long duration)
{
duration = (duration > 0 ? duration : this->TimerDuration);
vtkXTclTimer* timer = this->Internal->CreateTimer(this, timerId, duration);
return timer->ID;
}
//-------------------------------------------------------------------------
int vtkXRenderWindowTclInteractor::InternalDestroyTimer(int platformTimerId)
{
return this->Internal->DestroyTimer(platformTimerId);
}
//-------------------------------------------------------------------------
void vtkXRenderWindowTclInteractor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|