File: vtkBorlandRenderWindow.cpp

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (403 lines) | stat: -rw-r--r-- 11,937 bytes parent folder | download | duplicates (6)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <vcl\Sysutils.hpp>

#include "vtkBorlandRenderWindow.h"

#include "vtkInteractorStyleSwitch.h"
#include "vtkInteractorStyleFlight.h"
#include "vtkInteractorStyleImage.h"
#include "vtkInteractorStyleUser.h"  

#pragma package(smart_init)

//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TvtkBorlandRenderWindow *)
{
  new TvtkBorlandRenderWindow(NULL);
}

//---------------------------------------------------------------------------
namespace Vtkborlandrenderwindow
{
  void __fastcall PACKAGE Register()
    {
    TComponentClass classes[1] = {__classid(TvtkBorlandRenderWindow)};
    RegisterComponents("Samples", classes, 0);
    }
}

//---------------------------------------------------------------------------
__fastcall TvtkBorlandRenderWindow::TvtkBorlandRenderWindow(TComponent* Owner)
        : inherited(Owner)
{
  // Do want these
  ControlStyle << csCaptureMouse << csClickEvents << csOpaque << csDoubleClicks;
  // Don't want these
  ControlStyle >> csAcceptsControls >> csSetCaption;
  
  FUsevtkInteractor   = true;
  FInteractorMode     = IM_TrackballCamera;
  FInteractor         = 0;
  FOnVtkClose         = 0;
  FRenderWindow       = 0;
  FRenderer           = 0;
  FAbortCallback      = vtkAbortCallback::New();
}

//---------------------------------------------------------------------------
__fastcall TvtkBorlandRenderWindow::~TvtkBorlandRenderWindow()
{
    // Delete this first because Renderwindow has a hold on it too
  if ( FInteractor )
    {
    FInteractor->Delete();
    FInteractor = 0;
    }
  if ( FRenderer )
    {
    FRenderer->GetViewProps()->RemoveAllItems();
    FRenderWindow->RemoveRenderer(FRenderer);
    FRenderer->Delete();
    FRenderer = 0;
    }
  if ( FRenderWindow )
    {
    FRenderWindow->RemoveObserver(FAbortCallback);
    FRenderWindow->Delete();
    FRenderWindow = 0;
    }
  FAbortCallback->Delete();
}

//---------------------------------------------------------------------------
vtkWin32OpenGLRenderWindow * __fastcall TvtkBorlandRenderWindow::GetRenderWindow(void)
{
  if ( ! FRenderWindow )
    {
    // Stuff the renderwindow into our window
    FRenderWindow = vtkWin32OpenGLRenderWindow::New();
    FRenderWindow->AddObserver( vtkCommand::AbortCheckEvent, FAbortCallback);
    FRenderWindow->SetParentId(Parent->Handle);
    FRenderWindow->SetWindowId(Handle);
    FRenderWindow->DoubleBufferOn();
    FRenderWindow->SwapBuffersOn();
    // Frame to avoid unsightly garbage during initial
    // display which may be long when a complex scene is first rendered
    FRenderWindow->Frame();
    Invalidate();
    }
    // We create the interactor here because it makes maintenance better
  if (!FInteractor)
    {
    FInteractor = vtkWin32RenderWindowInteractor::New();
    FInteractor->SetRenderWindow(FRenderWindow);
    FInteractor->SetInstallMessageProc(false);
    SetInteractorMode(FInteractorMode);
    FInteractor->UpdateSize(Width,Height);
    FInteractor->Initialize();
    }
  return FRenderWindow;
}

//---------------------------------------------------------------------------
vtkWin32RenderWindowInteractor * __fastcall TvtkBorlandRenderWindow::GetInteractor(void)
{
  if (FRenderWindow)
    {
    if (!FInteractor)
      {
      throw Exception("Window exists but no Interactor, this shouldn't happen");
      }  
    }
  else
    {
    this->GetRenderWindow();  
    }
  return FInteractor;
}

//---------------------------------------------------------------------------
vtkRenderer * __fastcall TvtkBorlandRenderWindow::GetRenderer(void)
{
  if (!FRenderer)
    {
    FRenderer = vtkRenderer::New();
    GetRenderWindow()->AddRenderer(FRenderer);
    FRenderer->ResetCamera();
    DWORD  L = ColorToRGB(Color);
    double rgb[3] = { GetRValue(L)/255.0, GetGValue(L)/255.0, GetBValue(L)/255.0 };
    FRenderer->SetBackground(rgb);
    }
  return FRenderer;
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::SetInteractorMode(const vtkBorlandInteractorMode& im)
{
  if ( im <= IM_TrackballActor && im >= IM_JoystickCamera )
    {
    vtkInteractorStyleSwitch *iass = vtkInteractorStyleSwitch::SafeDownCast(FInteractor->GetInteractorStyle());
    if (!iass)
      {
      iass = vtkInteractorStyleSwitch::New();
      FInteractor->SetInteractorStyle(iass);
      iass->Delete();
      }

    switch ( im )
      {
      case IM_JoystickCamera: iass->SetCurrentStyleToJoystickCamera(); break;
      case IM_JoystickActor: iass->SetCurrentStyleToJoystickActor(); break;
      case IM_TrackballCamera: iass->SetCurrentStyleToTrackballCamera(); break;
      case IM_TrackballActor: iass->SetCurrentStyleToTrackballActor(); break;
      default: break;
      }
      FInteractorMode = im;
    }
    else if (im==IM_Flight)
      {
      vtkInteractorStyleFlight *iafl = vtkInteractorStyleFlight::SafeDownCast(FInteractor->GetInteractorStyle());
      if (!iafl)
        {
        iafl = vtkInteractorStyleFlight::New();
        FInteractor->SetInteractorStyle(iafl);
        iafl->Delete();
        }
      FInteractorMode = IM_Flight;
      }
    else if (im==IM_Image)
      {
      vtkInteractorStyleImage *iasi = vtkInteractorStyleImage::SafeDownCast(FInteractor->GetInteractorStyle());
      if (!iasi)
        {
        iasi = vtkInteractorStyleImage::New();
        FInteractor->SetInteractorStyle(iasi);
        iasi->Delete();
        }
      FInteractorMode = IM_Image;
      }
    else if (im==IM_User)
      {
      vtkInteractorStyleUser *iasu = vtkInteractorStyleUser::SafeDownCast(FInteractor->GetInteractorStyle());
      if (!iasu)
        {
        iasu = vtkInteractorStyleUser::New();
        FInteractor->SetInteractorStyle(iasu);
        iasu->Delete();
        }
      FInteractorMode = IM_User;
      }
    else
      {
      return;
      }
}

//---------------------------------------------------------------------------
// Paint
//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::Paint(void)
{
  if (FRenderWindow)
    {
    try
      {
      FRenderWindow->Render();
      }
    catch (...)
      {
      // Some error trap should occurr here
      ShowMessage("An exception occurred whilst rendering");
      }
    }
  else
    { // Design time or before RenderWindow creation
    inherited::Paint();
    }
}

//---------------------------------------------------------------------------
// Event handlers
//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::WMEraseBkgnd(TWMEraseBkgnd &Message)
{
  if (!FRenderWindow)
    {
    inherited::Dispatch(&Message);
    }
  else
    {
    Message.Result = 1; // No, but thanks for asking.
    } 
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::WMGetDlgCode(TMessage &Message)
{
  Message.Result = DLGC_WANTARROWS;
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::WMChar(TWMKey &Message)
{
  switch (Message.CharCode)
    {
    case 'e':
    case 'E':
    case 'q':
    case 'Q': if (!FOnVtkClose || (FOnVtkClose && FOnVtkClose(this)))
                {
                FInteractor->OnChar(Handle,Message.CharCode, 0, 0);
                }
              break;
    default:  FInteractor->OnChar(Handle,Message.CharCode, 0, 0);
    }
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::WMKeyDown(TWMKey &Message)
{
  FInteractor->OnKeyDown(Handle,Message.CharCode, 0, 0);
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::WMKeyUp(TWMKey &Message)
{
  FInteractor->OnKeyUp(Handle,Message.CharCode, 0, 0);
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::MouseMove(TShiftState shift, int x, int y )
{
  if(this->OnMouseMove && shift.Contains(ssCtrl))
    {
    this->OnMouseMove(this,shift,x,y);
    return;
    }

  if (FInteractor && FUsevtkInteractor)
    {
    int flags = 0;
    if (shift.Contains(ssShift)) flags += MK_SHIFT;
    if (shift.Contains(ssCtrl))  flags += MK_CONTROL;
    FInteractor->OnMouseMove(Handle, flags, x, y );
    }
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::MouseDown(TMouseButton button, TShiftState shift, int x, int y )
{
  if (::GetFocus()!=Handle) SetFocus();

  if(this->OnMouseDown && shift.Contains(ssCtrl))
    {
    this->OnMouseDown(this,button,shift,x,y);
    return;
    }

  if (FInteractor && FUsevtkInteractor)
    {
    int flags = 0;
    if (shift.Contains(ssShift)) flags += MK_SHIFT;
    if (shift.Contains(ssCtrl))  flags += MK_CONTROL;
    switch (button)
      {
      case mbLeft:   FInteractor->OnLButtonDown(Handle, flags, x,y); break;
      case mbRight:  FInteractor->OnRButtonDown(Handle, flags, x,y); break;
      case mbMiddle: FInteractor->OnMButtonDown(Handle, flags, x,y); break;
      }
    }
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::MouseUp(TMouseButton button, TShiftState shift, int x, int y )
{
  if(this->OnMouseUp  && shift.Contains(ssCtrl))
    {
    this->OnMouseUp(this,button,shift,x,y);
    return;
    }

  if (FInteractor && FUsevtkInteractor)
    {
    int flags = 0;
    if (shift.Contains(ssShift)) flags += MK_SHIFT;
    if (shift.Contains(ssCtrl))  flags += MK_CONTROL;
    switch (button)
      {
      case mbLeft:   FInteractor->OnLButtonUp(Handle, flags, x,y); break;
      case mbRight:  FInteractor->OnRButtonUp(Handle, flags, x,y); break;
      case mbMiddle: FInteractor->OnMButtonUp(Handle, flags, x,y); break;
      }
    }
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::Resize(void)
{
  if (FInteractor)
    {
    FInteractor->OnSize(this->Handle, 0, this->Width, this->Height);
    }
}

//---------------------------------------------------------------------------
void __fastcall TvtkBorlandRenderWindow::WMTimer(TWMTimer &Message)
{
  if (FInteractor)
    {
    FInteractor->OnTimer(Handle,Message.TimerID);
    }
}

//---------------------------------------------------------------------------
bool __fastcall TvtkBorlandRenderWindow::DoMouseWheelDown(TShiftState Shift, const TPoint &MousePos)
{
  bool result;
  if (this->OnMouseWheelDown && Shift.Contains(ssCtrl))
    {
    this->OnMouseWheelDown(this,Shift,MousePos,result);
    return result;
    }

  if (FInteractor && FUsevtkInteractor)
    {
    int flags = 0;
    if (Shift.Contains(ssShift)) { flags += MK_SHIFT; }
    if (Shift.Contains(ssCtrl))  { flags += MK_CONTROL; }
    FInteractor->OnMouseWheelBackward(Handle, flags, MousePos.x,MousePos.y);
    result = true;
    }
  return result;
}

//---------------------------------------------------------------------------
bool __fastcall TvtkBorlandRenderWindow::DoMouseWheelUp(TShiftState Shift, const TPoint &MousePos)
{
  bool result;
  if (this->OnMouseWheelUp && Shift.Contains(ssCtrl))
    {
    this->OnMouseWheelUp(this,Shift,MousePos,result);
    return result;
    }

  if (FInteractor && FUsevtkInteractor)
    {
    int flags = 0;
    if (Shift.Contains(ssShift)) { flags += MK_SHIFT; }
    if (Shift.Contains(ssCtrl))  { flags += MK_CONTROL; }
    FInteractor->OnMouseWheelForward(Handle, flags, MousePos.x,MousePos.y);
    result = true;
    }
  return result;
}