vtkKWRenderWidget.cxx 34.3 KB
Newer Older
1
2
/*=========================================================================

Brad King's avatar
Brad King committed
3
  Module:    vtkKWRenderWidget.cxx
4

Brad King's avatar
Brad King committed
5
6
7
  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8

Brad King's avatar
Brad King committed
9
10
11
     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.
12
13
14
15

=========================================================================*/
#include "vtkKWRenderWidget.h"

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
16
#include "vtkCallbackCommand.h"
17
#include "vtkCamera.h"
18
#include "vtkCommand.h"
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
19
#include "vtkCornerAnnotation.h"
20
#include "vtkKWApplication.h"
21
#include "vtkKWEvent.h"
22
#include "vtkKWGenericRenderWindowInteractor.h"
23
#include "vtkKWRenderWidgetCallbackCommand.h"
24
#include "vtkKWWindow.h"
25
#include "vtkMath.h"
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
26
#include "vtkObjectFactory.h"
Amy Squillacote's avatar
Amy Squillacote committed
27
#include "vtkProperty2D.h"
28
#include "vtkRenderWindow.h"
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
29
#include "vtkRenderer.h"
30
#include "vtkRendererCollection.h"
Amy Squillacote's avatar
Amy Squillacote committed
31
#include "vtkTextActor.h"
32
#include "vtkTextProperty.h"
33
34
35
36
37

#ifdef _WIN32
#include "vtkWin32OpenGLRenderWindow.h"
#endif

38
vtkStandardNewMacro(vtkKWRenderWidget);
39
vtkCxxRevisionMacro(vtkKWRenderWidget, "1.77");
40

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
41
//----------------------------------------------------------------------------
42
43
vtkKWRenderWidget::vtkKWRenderWidget()
{
44
45
46
47
48
  // The main callback

  this->Observer = vtkKWRenderWidgetCallbackCommand::New();
  this->EventIdentifier = -1;

49
50
51
52
  // The vtkTkRenderWidget

  this->ParentWindow = NULL;
  
53
54
  this->VTKWidget = vtkKWWidget::New();
  this->VTKWidget->SetParent(this);
55
56
57

  // Create two renderers by default (main one and overlay)

58
  this->Renderer = vtkRenderer::New();
59
  this->Renderer->SetLayer(0);
60

61
  this->OverlayRenderer = vtkRenderer::New();
62
  this->OverlayRenderer->SetLayer(1);
63
64
65

  // Create a render window and add two renderers

66
  this->RenderWindow = vtkRenderWindow::New();
67
68
  this->RenderWindow->SetNumberOfLayers(2);
  this->RenderWindow->AddRenderer(this->OverlayRenderer);
Ken Martin's avatar
Ken Martin committed
69
  this->RenderWindow->AddRenderer(this->Renderer);
70
71
72
73
74
75
76

  // Create a default (generic) interactor, which is pretty much
  // the only way to interact with a VTK Tk render widget

  this->Interactor = vtkKWGenericRenderWindowInteractor::New();
  this->Interactor->SetRenderWidget(this);  
  this->Interactor->SetRenderWindow(this->RenderWindow);
77
78
79
  this->Interactor->AddObserver(vtkCommand::CreateTimerEvent, this->Observer);
  this->Interactor->AddObserver(vtkCommand::DestroyTimerEvent, this->Observer);
  this->InteractorTimerToken = NULL;
80
81

  // Corner annotation
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
82
83
84

  this->CornerAnnotation = vtkCornerAnnotation::New();
  this->CornerAnnotation->SetMaximumLineHeight(0.07);
85
  this->CornerAnnotation->VisibilityOff();
86

87
88
  // Header annotation

89
  this->HeaderAnnotation = vtkTextActor::New();
90
  this->HeaderAnnotation->SetNonLinearFontScale(0.7,10);
91
92
93
94
95
  this->HeaderAnnotation->GetTextProperty()->SetJustificationToCentered();
  this->HeaderAnnotation->GetTextProperty()->SetVerticalJustificationToTop();
  this->HeaderAnnotation->GetTextProperty()->ShadowOff();
  this->HeaderAnnotation->ScaledTextOn();
  this->HeaderAnnotation->GetPositionCoordinate()
96
    ->SetCoordinateSystemToNormalizedViewport();
97
  this->HeaderAnnotation->GetPositionCoordinate()->SetValue(0.2, 0.84);
98
  this->HeaderAnnotation->GetPosition2Coordinate()
99
    ->SetCoordinateSystemToNormalizedViewport();
100
  this->HeaderAnnotation->GetPosition2Coordinate()->SetValue(0.6, 0.1);
101
  this->HeaderAnnotation->VisibilityOff();
Amy Squillacote's avatar
Amy Squillacote committed
102
  
103
104
  // Distance units

105
  this->DistanceUnits = NULL;
106

107
108
  // Get the camera, use it in overlay renderer too

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
109
110
111
112
113
  vtkCamera *cam = this->GetCurrentCamera();
  if (cam)
    {
    cam->ParallelProjectionOn();
    }
114
115
116
117
118
119
120
121
122
123
  this->GetOverlayRenderer()->SetActiveCamera(cam);

  // Current state (render mode, in expose, printing, etc)

  this->RenderMode         = vtkKWRenderWidget::STILL_RENDER;
  this->PreviousRenderMode = this->RenderMode;
  this->RenderState        = 1;
  this->CollapsingRenders  = 0;
  this->InExpose           = 0;
  this->Printing           = 0;
124
125
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
126
//----------------------------------------------------------------------------
127
128
vtkKWRenderWidget::~vtkKWRenderWidget()
{
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  if (this->Observer)
    {
    this->Observer->Delete();
    this->Observer = NULL;
    }

  if (this->Renderer)
    {
    this->Renderer->Delete();
    this->Renderer = NULL;
    }

  if (this->OverlayRenderer)
    {
    this->OverlayRenderer->Delete();
    this->OverlayRenderer = NULL;
    }
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
146

147
148
149
150
151
  if (this->RenderWindow)
    {
    this->RenderWindow->Delete();
    this->RenderWindow = NULL;
    }
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
152

153
  this->SetParentWindow(NULL);
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
154

155
156
157
158
159
160
161
162
  if (this->Interactor)
    {
    this->Interactor->SetRenderWidget(NULL);
    this->Interactor->SetInteractorStyle(NULL);
    this->Interactor->Delete();
    this->Interactor = NULL;
    }

163
164
165
166
167
168
  if (this->InteractorTimerToken)
    {
    Tcl_DeleteTimerHandler(this->InteractorTimerToken);
    this->InteractorTimerToken = NULL;
    }

169
170
171
172
173
  if (this->VTKWidget)
    {
    this->VTKWidget->Delete();
    this->VTKWidget = NULL;
    }
174
  
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
175
176
177
178
179
  if (this->CornerAnnotation)
    {
    this->CornerAnnotation->Delete();
    this->CornerAnnotation = NULL;
    }
Amy Squillacote's avatar
Amy Squillacote committed
180

181
182
183
184
185
  if (this->HeaderAnnotation)
    {
    this->HeaderAnnotation->Delete();
    this->HeaderAnnotation = NULL;
    }
186
  
187
  this->SetDistanceUnits(NULL);
188
189
}

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//----------------------------------------------------------------------------
vtkRenderer* vtkKWRenderWidget::GetNthRenderer(int id)
{
  if (id != 0)
    {
    return NULL;
    }

  return this->Renderer;
}

//----------------------------------------------------------------------------
int vtkKWRenderWidget::GetRendererId(vtkRenderer *ren)
{
  if (!ren || ren != this->Renderer)
    {
    return -1;
    }

  return 0;
}

212
213
214
215
216
217
//----------------------------------------------------------------------------
vtkRenderer* vtkKWRenderWidget::GetOverlayRenderer()
{
  return this->OverlayRenderer;
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
218
219
220
221
222
223
224
225
226
227
228
229
//----------------------------------------------------------------------------
vtkCamera* vtkKWRenderWidget::GetCurrentCamera()
{
  vtkRenderer *ren = this->GetRenderer();
  if (ren)
    {
    return ren->GetActiveCamera();
    }

  return NULL;
}

230
//----------------------------------------------------------------------------
231
void vtkKWRenderWidget::SetDistanceUnits(const char* _arg)
232
{
233
  if (this->DistanceUnits == NULL && _arg == NULL) 
234
235
236
237
    { 
    return;
    }

238
  if (this->DistanceUnits && _arg && (!strcmp(this->DistanceUnits, _arg))) 
239
240
241
242
    {
    return;
    }

243
  if (this->DistanceUnits) 
244
    { 
245
    delete [] this->DistanceUnits; 
246
247
248
249
    }

  if (_arg)
    {
250
251
    this->DistanceUnits = new char[strlen(_arg)+1];
    strcpy(this->DistanceUnits, _arg);
252
253
254
    }
  else
    {
255
    this->DistanceUnits = NULL;
256
257
258
259
260
261
262
    }

  this->Modified();
  
  this->UpdateAccordingToUnits();
} 

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
263
//----------------------------------------------------------------------------
264
265
void vtkKWRenderWidget::Create(vtkKWApplication *app, const char *args)
{
266
  // Call the superclass to create the widget and set the appropriate flags
Sebastien Barre's avatar
Sebastien Barre committed
267

268
  if (!this->Superclass::Create(app, "frame", args))
269
    {
270
    vtkErrorMacro("Failed creating widget " << this->GetClassName());
271
272
273
    return;
    }
  
274
275
  // Create the VTK Tk render widget in VTKWidget

276
277
278
  char *local = new char[(args ? strlen(args) : 0) + 100];
  sprintf(local, "%s -rw Addr=%p", (args ? args : ""), this->RenderWindow);
  this->VTKWidget->Create(app, "vtkTkRenderWidget", local);
279
  delete [] local;
280

281
282
283
  this->Script("grid rowconfigure %s 0 -weight 1", this->GetWidgetName());
  this->Script("grid columnconfigure %s 0 -weight 1", this->GetWidgetName());
  this->Script("grid %s -sticky nsew", this->VTKWidget->GetWidgetName());
284
285
  
  this->RenderWindow->Render();
Sebastien Barre's avatar
Sebastien Barre committed
286

287
288
289
290
  // Make the corner annotation visibile

  this->SetCornerAnnotationVisibility(1);

Sebastien Barre's avatar
Sebastien Barre committed
291
292
293
  // Update enable state

  this->UpdateEnableState();
294
295
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
296
//----------------------------------------------------------------------------
297
void vtkKWRenderWidget::AddBindings()
298
{
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
299
300
301
302
  // First remove the old one so that bindings don't get duplicated

  this->RemoveBindings();

303
304
  const char *tname = this->GetTclName();

305
306
307
  if (this->VTKWidget->IsCreated())
    {
    const char *wname = this->VTKWidget->GetWidgetName();
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
308

309
310
311
312
    // Setup some default bindings
    
    this->Script("bind %s <Expose> {%s Exposed}",
                 wname, tname);
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
313
  
314
315
    this->Script("bind %s <Enter> {%s Enter %%x %%y}",
                 wname, tname);
316
317
318
319
320
321

    this->Script("bind %s <FocusIn> {%s FocusInCallback}", 
                 wname, tname);

    this->Script("bind %s <FocusOut> {%s FocusOutCallback}", 
                 wname, tname);
322
    }
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
323

324
325
326
327
328
  if (this->IsCreated())
    {
    this->Script("bind %s <Configure> {%s Configure %%w %%h}",
                 this->GetWidgetName(), tname);
    }
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
329
  
330
  this->AddInteractionBindings();
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
331
332
333
334
335
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::RemoveBindings()
{
336
337
338
  if (this->VTKWidget->IsCreated())
    {
    const char *wname = this->VTKWidget->GetWidgetName();
339
  
340
341
    this->Script("bind %s <Expose> {}", wname);
    this->Script("bind %s <Enter> {}", wname);
342
343
344

    this->Script("bind %s <FocusIn> {}", wname);
    this->Script("bind %s <FocusOut> {}", wname);
345
346
347
348
349
350
    }

  if (this->IsCreated())
    {
    this->Script("bind %s <Configure> {}", this->GetWidgetName());
    }
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
351
352
353
354
355

  this->RemoveInteractionBindings();
}

//----------------------------------------------------------------------------
356
void vtkKWRenderWidget::AddInteractionBindings()
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
357
{
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
358
359
360
361
362
363
364
365
366
367
368
  // First remove the old one so that bindings don't get duplicated

  this->RemoveInteractionBindings();

  // If we are disabled, don't do anything

  if (!this->Enabled)
    {
    return;
    }

369
370
371
372
  if (this->VTKWidget->IsCreated())
    {
    const char *wname = this->VTKWidget->GetWidgetName();
    const char *tname = this->GetTclName();
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
373

374
375
376
    this->Script(
      "bind %s <Any-ButtonPress> {%s AButtonPress %%b %%x %%y 0 0}",
      wname, tname);
377

378
379
380
    this->Script(
      "bind %s <Any-ButtonRelease> {%s AButtonRelease %%b %%x %%y}",
      wname, tname);
381

382
383
384
    this->Script(
      "bind %s <Shift-Any-ButtonPress> {%s AButtonPress %%b %%x %%y 0 1}",
      wname, tname);
385

386
387
388
    this->Script(
      "bind %s <Shift-Any-ButtonRelease> {%s AButtonRelease %%b %%x %%y}",
      wname, tname);
389

390
391
392
    this->Script(
      "bind %s <Control-Any-ButtonPress> {%s AButtonPress %%b %%x %%y 1 0}",
      wname, tname);
393

394
395
396
    this->Script(
      "bind %s <Control-Any-ButtonRelease> {%s AButtonRelease %%b %%x %%y}",
      wname, tname);
397

398
399
400
    this->Script(
      "bind %s <B1-Motion> {%s MouseMove 1 %%x %%y}",
      wname, tname);
401

402
403
404
    this->Script(
      "bind %s <B2-Motion> {%s MouseMove 2 %%x %%y}", 
      wname, tname);
405
  
406
407
408
    this->Script(
      "bind %s <B3-Motion> {%s MouseMove 3 %%x %%y}", 
      wname, tname);
409

410
411
412
    this->Script(
      "bind %s <Shift-B1-Motion> {%s MouseMove 1 %%x %%y}", 
      wname, tname);
413

414
415
416
    this->Script(
      "bind %s <Shift-B2-Motion> {%s MouseMove 2 %%x %%y}", 
      wname, tname);
417
  
418
419
420
    this->Script(
      "bind %s <Shift-B3-Motion> {%s MouseMove 3 %%x %%y}", 
      wname, tname);
421

422
423
424
    this->Script(
      "bind %s <Control-B1-Motion> {%s MouseMove 1 %%x %%y}",
      wname, tname);
425

426
427
428
    this->Script(
      "bind %s <Control-B2-Motion> {%s MouseMove 2 %%x %%y}",
      wname, tname);
429
  
430
431
432
    this->Script(
      "bind %s <Control-B3-Motion> {%s MouseMove 3 %%x %%y}",
      wname, tname);
433

434
435
436
    this->Script(
      "bind %s <KeyPress> {%s AKeyPress %%A %%x %%y 0 0 %%K}",
      wname, tname);
437
  
438
439
440
    this->Script(
      "bind %s <Shift-KeyPress> {%s AKeyPress %%A %%x %%y 0 1 %%K}",
      wname, tname);
441
  
442
443
444
    this->Script(
      "bind %s <Control-KeyPress> {%s AKeyPress %%A %%x %%y 1 0 %%K}",
      wname, tname);
445
  
446
447
448
449
    this->Script(
      "bind %s <Motion> {%s MouseMove 0 %%x %%y}", 
      wname, tname);
    }
450
451
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
452
//----------------------------------------------------------------------------
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
453
void vtkKWRenderWidget::RemoveInteractionBindings()
454
{
455
456
457
  if (this->VTKWidget->IsCreated())
    {
    const char *wname = this->VTKWidget->GetWidgetName();
458
  
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    this->Script("bind %s <Any-ButtonPress> {}", wname);
    this->Script("bind %s <Any-ButtonRelease> {}", wname);
    this->Script("bind %s <Shift-Any-ButtonPress> {}", wname);
    this->Script("bind %s <Shift-Any-ButtonRelease> {}", wname);
    this->Script("bind %s <Control-Any-ButtonPress> {}", wname);
    this->Script("bind %s <Control-Any-ButtonRelease> {}", wname);

    this->Script("bind %s <B1-Motion> {}", wname);
    this->Script("bind %s <B2-Motion> {}", wname);
    this->Script("bind %s <B3-Motion> {}", wname);

    this->Script("bind %s <Shift-B1-Motion> {}", wname);
    this->Script("bind %s <Shift-B2-Motion> {}", wname);
    this->Script("bind %s <Shift-B3-Motion> {}", wname);

    this->Script("bind %s <Control-B1-Motion> {}", wname);
    this->Script("bind %s <Control-B2-Motion> {}", wname);
    this->Script("bind %s <Control-B3-Motion> {}", wname);

    this->Script("bind %s <KeyPress> {}", wname);
    this->Script("bind %s <Shift-KeyPress> {}", wname);
    this->Script("bind %s <Control-KeyPress> {}", wname);

    this->Script("bind %s <Motion> {}", wname);
    }
484
485
}

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//----------------------------------------------------------------------------
void vtkKWRenderWidget::MouseMove(int vtkNotUsed(num), int x, int y)
{
  this->Interactor->SetEventPositionFlipY(x, y);
  this->Interactor->MouseMoveEvent();
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::AButtonPress(int num, int x, int y,
                                     int ctrl, int shift)
{
  this->VTKWidget->Focus();
  
  this->Interactor->SetEventInformationFlipY(x, y, ctrl, shift);
  
  switch (num)
    {
    case 1:
      this->Interactor->LeftButtonPressEvent();
      break;
    case 2:
      this->Interactor->MiddleButtonPressEvent();
      break;
    case 3:
      this->Interactor->RightButtonPressEvent();
      break;
    }
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::AButtonRelease(int num, int x, int y)
{
  this->Interactor->SetEventInformationFlipY(x, y, 0, 0);
  
  switch (num)
    {
    case 1:
      this->Interactor->LeftButtonReleaseEvent();
      break;
    case 2:
      this->Interactor->MiddleButtonReleaseEvent();
      break;
    case 3:
      this->Interactor->RightButtonReleaseEvent();
      break;
    }
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::AKeyPress(char key, 
                                  int x, int y,
                                  int ctrl, int shift,
                                  char *keysym)
{
  this->Interactor->SetEventPositionFlipY(x, y);
  this->Interactor->SetControlKey(ctrl);
  this->Interactor->SetShiftKey(shift);
  this->Interactor->SetKeyCode(key);
  this->Interactor->SetKeySym(keysym);
545
  this->Interactor->CharEvent();
546
547
548
549
550
551
552
553
554
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::Configure(int width, int height)
{
  this->Interactor->UpdateSize(width, height);
  this->Interactor->ConfigureEvent();
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
555
//----------------------------------------------------------------------------
556
557
558
559
560
561
562
563
564
565
566
567
568
void vtkKWRenderWidget::Exposed()
{
  if (this->InExpose)
    {
    return;
    }
  
  this->InExpose = 1;
  this->Script("update");
  this->Render();
  this->InExpose = 0;
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
569
//----------------------------------------------------------------------------
570
571
572
573
574
575
576
577
578
579
580
void vtkKWRenderWidget::FocusInCallback()
{
  this->InvokeEvent(vtkKWEvent::FocusInEvent, NULL);
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::FocusOutCallback()
{
  this->InvokeEvent(vtkKWEvent::FocusOutEvent, NULL);
}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
//----------------------------------------------------------------------------
void vtkKWRenderWidget::Reset()
{
  this->ResetCamera();
  this->Render();
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::ResetCamera()
{
  double bounds[6];
  double center[3];
  double distance, width, viewAngle, vn[3], *vup;
  
  this->GetRenderer()->ComputeVisiblePropBounds(bounds);
  if (bounds[0] == VTK_LARGE_FLOAT)
    {
    vtkDebugMacro(<< "Cannot reset camera!");
    return;
    }

  vtkCamera *cam = this->GetCurrentCamera();
  if (cam != NULL)
    {
    cam->GetViewPlaneNormal(vn);
    }
  else
    {
    vtkErrorMacro(<< "Trying to reset non-existant camera");
    return;
    }

  center[0] = (double)(((double)bounds[0] + (double)bounds[1]) / 2.0);
  center[1] = (double)(((double)bounds[2] + (double)bounds[3]) / 2.0);
  center[2] = (double)(((double)bounds[4] + (double)bounds[5]) / 2.0);

  width = (double)bounds[3] - (double)bounds[2];
  if (width < ((double)bounds[1] - (double)bounds[0]))
    {
    width = (double)bounds[1] - (double)bounds[0];
    }
  if (width < ((double)bounds[5] - (double)bounds[4]))
    {
    width = (double)bounds[5] - (double)bounds[4];
    }
  
  if (cam->GetParallelProjection())
    {
    viewAngle = 30;  // the default in vtkCamera
    }
  else
    {
    viewAngle = cam->GetViewAngle();
    }
  
  distance = width / (double)tan(viewAngle * vtkMath::Pi() / 360.0);

  // Check view-up vector against view plane normal

  vup = cam->GetViewUp();
  if (fabs(vtkMath::Dot(vup,vn)) > 0.999)
    {
    vtkWarningMacro(<<"Resetting view-up since view plane normal is parallel");
    cam->SetViewUp(-vup[2], vup[0], vup[1]);
    }

  // Update the camera

  cam->SetFocalPoint(center[0], center[1], center[2]);
  cam->SetPosition((double)((double)center[0] + distance * vn[0]),
                   (double)((double)center[1] + distance * vn[1]),
                   (double)((double)center[2] + distance * vn[2]));

  this->GetRenderer()->ResetCameraClippingRange(bounds);

  // Setup default parallel scale

  cam->SetParallelScale(0.5 * width);
}

661
//----------------------------------------------------------------------------
662
663
void vtkKWRenderWidget::Render()
{
664
  if (this->CollapsingRenders)
665
    {
666
    this->CollapsingRendersCount++;
667
668
    return;
    }
669
670
671
672
673
674
675
676
677
678
679
680
681

  if (!this->RenderState)
    {
    return;
    }

  static int static_in_render = 0;
  if (static_in_render)
    {
    return;
    }
  static_in_render = 1;

682
  if (this->RenderMode != vtkKWRenderWidget::DISABLED_RENDER)
683
    {
684
    this->GetRenderer()->ResetCameraClippingRange();
685
686
687
688
    this->RenderWindow->Render();
    }
  
  static_in_render = 0;
689
690
}

691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//----------------------------------------------------------------------------
const char* vtkKWRenderWidget::GetRenderModeAsString()
{
  switch (this->RenderMode)
    {
    case vtkKWRenderWidget::INTERACTIVE_RENDER:
      return "Interactive";
    case vtkKWRenderWidget::STILL_RENDER:
      return "Still";
    case vtkKWRenderWidget::SINGLE_RENDER:
      return "Single";
    case vtkKWRenderWidget::DISABLED_RENDER:
      return "Disabled";
    default:
      return "Unknown (error)";
    }
}

709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//----------------------------------------------------------------------------
void vtkKWRenderWidget::SetParentWindow(vtkKWWindow *window)
{
  if (this->ParentWindow == window)
    {
    return;
    }

  // Remove old observers 
  // (some of them may use the parent window, to display progress for example)

  if (this->ParentWindow)
    {
    this->RemoveObservers();
    }

  this->ParentWindow = window;

  // Reinstall observers

  if (this->ParentWindow)
    {
    this->AddObservers();
    }
  
  this->Modified();
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::SetOffScreenRendering(int val)
{
  if (this->GetRenderWindow())
    {
    this->GetRenderWindow()->SetOffScreenRendering(val);
    }
  this->SetPrinting(val);
}

//----------------------------------------------------------------------------
void vtkKWRenderWidget::SetPrinting(int arg)
{
  if (arg == this->Printing)
    {
    return;
    }

  this->Printing = arg;
  this->Modified();

  if (this->Printing)
    {
    this->PreviousRenderMode = this->GetRenderMode();
    this->SetRenderModeToSingle();
    }
  else
    {
    this->SetRenderMode(this->PreviousRenderMode);

    // SetupPrint will call SetupMemoryRendering().
    // As convenience, let's call ResumeScreenRendering()
    this->ResumeScreenRendering();
    }
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
773
//----------------------------------------------------------------------------
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#ifdef _WIN32
void vtkKWRenderWidget::SetupPrint(RECT &rcDest, HDC ghdc,
                                   int printerPageSizeX, int printerPageSizeY,
                                   int printerDPIX, int printerDPIY,
                                   float scaleX, float scaleY,
                                   int screenSizeX, int screenSizeY)
{
  float scale;
  int cxDIB = screenSizeX;         // Size of DIB - x
  int cyDIB = screenSizeY;         // Size of DIB - y
  
  // target DPI specified here
  if (this->GetParentWindow())
    {
    scale = printerDPIX/this->GetParentWindow()->GetPrintTargetDPI();
    }
  else
    {
    scale = printerDPIX/100.0;
    }
  

  // Best Fit case -- create a rectangle which preserves
  // the DIB's aspect ratio, and fills the page horizontally.
  //
  // The formula in the "->bottom" field below calculates the Y
  // position of the printed bitmap, based on the size of the
  // bitmap, the width of the page, and the relative size of
  // a printed pixel (printerDPIY / printerDPIX).
  //
  rcDest.bottom = rcDest.left = 0;
  if (((float)cyDIB*(float)printerPageSizeX/(float)printerDPIX) > 
      ((float)cxDIB*(float)printerPageSizeY/(float)printerDPIY))
    {
    rcDest.top = printerPageSizeY;
    rcDest.right = (static_cast<float>(printerPageSizeY)*printerDPIX*cxDIB) /
      (static_cast<float>(printerDPIY)*cyDIB);
    }
  else
    {
    rcDest.right = printerPageSizeX;
    rcDest.top = (static_cast<float>(printerPageSizeX)*printerDPIY*cyDIB) /
      (static_cast<float>(printerDPIX)*cxDIB);
    } 
  
  this->SetupMemoryRendering(rcDest.right/scale*scaleX,
                             rcDest.top/scale*scaleY, ghdc);
}
#endif

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
824
//----------------------------------------------------------------------------
825
826
827
828
829
830
831
832
833
834
void* vtkKWRenderWidget::GetMemoryDC()
{
#ifdef _WIN32
  return (void *)vtkWin32OpenGLRenderWindow::
    SafeDownCast(this->RenderWindow)->GetMemoryDC();
#else
  return NULL;
#endif
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
835
//----------------------------------------------------------------------------
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
void vtkKWRenderWidget::SetupMemoryRendering(
#ifdef _WIN32
  int x, int y, void *cd
#else
  int, int, void*
#endif
  )
{
#ifdef _WIN32
  if (!cd)
    {
    cd = this->RenderWindow->GetGenericContext();
    }
  vtkWin32OpenGLRenderWindow::
    SafeDownCast(this->RenderWindow)->SetupMemoryRendering(x, y, (HDC)cd);
#endif
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
854
//----------------------------------------------------------------------------
855
856
857
858
859
860
861
862
void vtkKWRenderWidget::ResumeScreenRendering() 
{
#ifdef _WIN32
  vtkWin32OpenGLRenderWindow::
    SafeDownCast(this->RenderWindow)->ResumeScreenRendering();
#endif
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
863
//----------------------------------------------------------------------------
864
865
void vtkKWRenderWidget::AddProp(vtkProp *prop)
{
866
  this->GetRenderer()->AddProp(prop);
867
868
}

869
870
871
//----------------------------------------------------------------------------
void vtkKWRenderWidget::AddOverlayProp(vtkProp *prop)
{
872
  this->GetOverlayRenderer()->AddProp(prop);
873
874
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
875
//----------------------------------------------------------------------------
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
876
877
int vtkKWRenderWidget::HasProp(vtkProp *prop)
{
878
879
  if (this->GetRenderer()->GetProps()->IsItemPresent(prop) ||
      this->GetOverlayRenderer()->GetProps()->IsItemPresent(prop))
880
881
882
883
884
    {
    return 1;
    }
  
  return 0;
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
885
886
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
887
//----------------------------------------------------------------------------
888
889
void vtkKWRenderWidget::RemoveProp(vtkProp *prop)
{
890
  // safe to call both, vtkViewport does a check first
891
892
  this->GetRenderer()->RemoveProp(prop);
  this->GetOverlayRenderer()->RemoveProp(prop);
893
894
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
895
//----------------------------------------------------------------------------
896
897
void vtkKWRenderWidget::RemoveAllProps()
{
898
899
  this->GetRenderer()->RemoveAllProps();
  this->GetOverlayRenderer()->RemoveAllProps();
900
901
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
902
//----------------------------------------------------------------------------
Sebastien Barre's avatar
Sebastien Barre committed
903
void vtkKWRenderWidget::SetBackgroundColor(double r, double g, double b)
904
{
Sebastien Barre's avatar
Sebastien Barre committed
905
  double *color = this->GetBackgroundColor();
906
  if (!color || (color[0] == r && color[1] == g && color[2] == b))
907
908
909
    {
    return;
    }
910
911

  if (r < 0 || g < 0 || b < 0)
912
913
914
915
    {
    return;
    }
  
916
917
918
919
920
921
922
923
924
925
926
927
  vtkRendererCollection *renderers = this->RenderWindow->GetRenderers();
  if (renderers)
    {
    renderers->InitTraversal();
    vtkRenderer *renderer = renderers->GetNextItem();
    while (renderer)
      {
      renderer->SetBackground(r, g, b);
      renderer = renderers->GetNextItem();
      }
    }

928
929
930
  this->Render();
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
931
//----------------------------------------------------------------------------
Sebastien Barre's avatar
Sebastien Barre committed
932
double* vtkKWRenderWidget::GetBackgroundColor()
933
{
934
935
936
937
938
939
940
941
942
943
944
  vtkRendererCollection *renderers = this->RenderWindow->GetRenderers();
  if (renderers)
    {
    renderers->InitTraversal();
    vtkRenderer *renderer = renderers->GetNextItem();
    if (renderer)
      {
      return renderer->GetBackground();
      }
    }
  return NULL;
945
946
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
947
//----------------------------------------------------------------------------
948
949
950
void vtkKWRenderWidget::Close()
{
  this->RemoveBindings();
951

952
953
  // Clear all corner annotation texts

954
955
956
957
  if (this->GetCornerAnnotation())
    {
    this->GetCornerAnnotation()->ClearAllTexts();
    }
958
959
}

960
961
962
963
964
965
966
//----------------------------------------------------------------------------
void vtkKWRenderWidget::SetAnnotationsVisibility(int v)
{
  this->SetCornerAnnotationVisibility(v);
  this->SetHeaderAnnotationVisibility(v);
}

967
968
969
970
971
972
973
974
//----------------------------------------------------------------------------
int vtkKWRenderWidget::GetCornerAnnotationVisibility()
{
  return (this->CornerAnnotation &&
          this->HasProp(this->CornerAnnotation) && 
          this->CornerAnnotation->GetVisibility());
}

Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
975
//----------------------------------------------------------------------------
976
977
void vtkKWRenderWidget::SetCornerAnnotationVisibility(int v)
{
978
  if (this->GetCornerAnnotationVisibility() == v)
979
980
981
982
    {
    return;
    }

983
984
  if (v)
    {
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
985
986
987
    this->CornerAnnotation->VisibilityOn();
    if (!this->HasProp(this->CornerAnnotation))
      {
988
      this->AddOverlayProp(this->CornerAnnotation);
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
989
      }
990
991
992
    }
  else
    {
Sebastien Barre's avatar
ENH:    
Sebastien Barre committed
993
994
995
996
997
    this->CornerAnnotation->VisibilityOff();
    if (this->HasProp(this->CornerAnnotation))
      {
      this->RemoveProp(this->CornerAnnotation);
      }
998
    }
999
1000

  this->Render();