1/*=========================================================================
2
3  Program:   Visualization Toolkit
4  Module:    vtkIOSRenderWindowInteractor.mm
5
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10     This software is distributed WITHOUT ANY WARRANTY; without even
11     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12     PURPOSE.  See the above copyright notice for more information.
13
14=========================================================================*/
15#include "vtkOpenGLRenderWindow.h"
16
17#import "vtkIOSRenderWindowInteractor.h"
18#import "vtkIOSRenderWindow.h"
19#import "vtkCommand.h"
20#import "vtkObjectFactory.h"
21
22
23//----------------------------------------------------------------------------
24vtkStandardNewMacro(vtkIOSRenderWindowInteractor);
25
26//----------------------------------------------------------------------------
27void (*vtkIOSRenderWindowInteractor::ClassExitMethod)(void *) = (void (*)(void *))NULL;
28void *vtkIOSRenderWindowInteractor::ClassExitMethodArg = (void *)NULL;
29void (*vtkIOSRenderWindowInteractor::ClassExitMethodArgDelete)(void *) = (void (*)(void *))NULL;
30
31//----------------------------------------------------------------------------
32vtkIOSRenderWindowInteractor::vtkIOSRenderWindowInteractor()
33{
34  for (int i=0; i < VTKI_MAX_POINTERS; i++)
35    {
36    this->IDLookup[i] = NULL;
37    }
38}
39
40//----------------------------------------------------------------------------
41vtkIOSRenderWindowInteractor::~vtkIOSRenderWindowInteractor()
42{
43  this->Enabled = 0;
44}
45
46//----------------------------------------------------------------------------
47void vtkIOSRenderWindowInteractor::StartEventLoop()
48{
49}
50
51//----------------------------------------------------------------------------
52// Begin processing keyboard strokes.
53void vtkIOSRenderWindowInteractor::Initialize()
54{
55  // make sure we have a RenderWindow and camera
56  if ( !this->RenderWindow )
57    {
58    vtkErrorMacro(<<"No renderer defined!");
59    return;
60    }
61  if (this->Initialized)
62    {
63    return;
64    }
65  this->Initialized = 1;
66  // get the info we need from the RenderingWindow
67  vtkIOSRenderWindow *renWin = (vtkIOSRenderWindow *)(this->RenderWindow);
68  renWin->Start();
69  int *size = renWin->GetSize();
70
71  renWin->GetPosition(); // update values of this->Position[2]
72
73  this->Enable();
74  this->Size[0] = size[0];
75  this->Size[1] = size[1];
76}
77
78//----------------------------------------------------------------------------
79void vtkIOSRenderWindowInteractor::Enable()
80{
81  if (this->Enabled)
82    {
83    return;
84    }
85
86  // Set the RenderWindow's interactor so that when the vtkIOSGLView tries
87  // to handle events from the OS it will either handle them or ignore them
88  this->GetRenderWindow()->SetInteractor(this);
89
90  this->Enabled = 1;
91  this->Modified();
92}
93
94//----------------------------------------------------------------------------
95void vtkIOSRenderWindowInteractor::Disable()
96{
97  if (!this->Enabled)
98    {
99    return;
100    }
101
102#ifdef VTK_USE_TDX
103  if(this->Device->GetInitialized())
104    {
105    this->Device->Close();
106    }
107#endif
108
109  // Set the RenderWindow's interactor so that when the vtkIOSGLView tries
110  // to handle events from the OS it will either handle them or ignore them
111  this->GetRenderWindow()->SetInteractor(NULL);
112
113  this->Enabled = 0;
114  this->Modified();
115}
116
117//----------------------------------------------------------------------------
118void vtkIOSRenderWindowInteractor::TerminateApp()
119{
120}
121
122//----------------------------------------------------------------------------
123int vtkIOSRenderWindowInteractor::InternalCreateTimer(int timerId,
124  int timerType, unsigned long duration)
125{
126  // In this implementation, timerId and platformTimerId are the same
127  int platformTimerId = timerId;
128
129  return platformTimerId;
130}
131
132//----------------------------------------------------------------------------
133int vtkIOSRenderWindowInteractor::InternalDestroyTimer(int platformTimerId)
134{
135  // In this implementation, timerId and platformTimerId are the same;
136  // but calling this anyway is more correct
137  int timerId = this->GetVTKTimerId(platformTimerId);
138
139  return 0; // fail
140}
141
142//----------------------------------------------------------------------------
143// Specify the default function to be called when an interactor needs to exit.
144// This callback is overridden by an instance ExitMethod that is defined.
145void vtkIOSRenderWindowInteractor::SetClassExitMethod(void (*f)(void *),void *arg)
146{
147  if ( f != vtkIOSRenderWindowInteractor::ClassExitMethod
148  || arg != vtkIOSRenderWindowInteractor::ClassExitMethodArg)
149    {
150    // delete the current arg if there is a delete method
151    if ((vtkIOSRenderWindowInteractor::ClassExitMethodArg)
152     && (vtkIOSRenderWindowInteractor::ClassExitMethodArgDelete))
153      {
154      (*vtkIOSRenderWindowInteractor::ClassExitMethodArgDelete)
155        (vtkIOSRenderWindowInteractor::ClassExitMethodArg);
156      }
157    vtkIOSRenderWindowInteractor::ClassExitMethod = f;
158    vtkIOSRenderWindowInteractor::ClassExitMethodArg = arg;
159
160    // no call to this->Modified() since this is a class member function
161    }
162}
163
164//----------------------------------------------------------------------------
165// Set the arg delete method.  This is used to free user memory.
166void vtkIOSRenderWindowInteractor::SetClassExitMethodArgDelete(void (*f)(void *))
167{
168  if (f != vtkIOSRenderWindowInteractor::ClassExitMethodArgDelete)
169    {
170    vtkIOSRenderWindowInteractor::ClassExitMethodArgDelete = f;
171
172    // no call to this->Modified() since this is a class member function
173    }
174}
175
176// This function is used to return an index given an ID
177int vtkIOSRenderWindowInteractor::GetContactIndex(void *dwID)
178{
179  for (int i=0; i < VTKI_MAX_POINTERS; i++)
180    {
181    if (this->IDLookup[i] == dwID)
182      {
183      return i;
184      }
185    }
186
187  for (int i=0; i < VTKI_MAX_POINTERS; i++)
188    {
189    if (this->IDLookup[i] == NULL)
190      {
191      this->IDLookup[i] = dwID;
192      return i;
193      }
194    }
195
196  // Out of contacts
197  return -1;
198}
199
200// This function is used to return an index given an ID
201void vtkIOSRenderWindowInteractor::ClearContactIndex(void *dwID)
202{
203  for (int i=0; i < VTKI_MAX_POINTERS; i++)
204    {
205    if (this->IDLookup[i] == dwID)
206      {
207      this->IDLookup[i] = NULL;
208      return;
209      }
210    }
211}
212
213//----------------------------------------------------------------------------
214void vtkIOSRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
215{
216  this->Superclass::PrintSelf(os,indent);
217}
218
219//----------------------------------------------------------------------------
220void vtkIOSRenderWindowInteractor::ExitCallback()
221{
222  if (this->HasObserver(vtkCommand::ExitEvent))
223    {
224    this->InvokeEvent(vtkCommand::ExitEvent,NULL);
225    }
226  else if (this->ClassExitMethod)
227    {
228    (*this->ClassExitMethod)(this->ClassExitMethodArg);
229    }
230  this->TerminateApp();
231}
232