vtkCarbonRenderWindow.cxx 34.4 KB
Newer Older
Yves Starreveld's avatar
Yves Starreveld committed
1 2
/*=========================================================================

3 4
Program:   Visualization Toolkit
Module:    vtkCarbonRenderWindow.cxx
Brad King's avatar
Brad King committed
5

6 7 8
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Yves Starreveld's avatar
Yves Starreveld committed
9

10 11 12
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.
Yves Starreveld's avatar
Yves Starreveld committed
13 14 15

=========================================================================*/
#include "vtkCarbonRenderWindow.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
16

Yves Starreveld's avatar
Yves Starreveld committed
17
#include "vtkCarbonRenderWindowInteractor.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
18
#include "vtkIdList.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
19
#include "vtkObjectFactory.h"
Yves Starreveld's avatar
Yves Starreveld committed
20
#include "vtkOpenGLActor.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
21
#include "vtkOpenGLCamera.h"
Yves Starreveld's avatar
Yves Starreveld committed
22 23
#include "vtkOpenGLLight.h"
#include "vtkOpenGLPolyDataMapper.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
24 25 26
#include "vtkOpenGLProperty.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLTexture.h"
27
#include "vtkRendererCollection.h"
Yves Starreveld's avatar
Yves Starreveld committed
28

Andy Cedilnik's avatar
Andy Cedilnik committed
29 30
#include <math.h>

31
//----------------------------------------------------------------------------
32
vtkCxxRevisionMacro(vtkCarbonRenderWindow, "1.79");
Yves Starreveld's avatar
Yves Starreveld committed
33 34
vtkStandardNewMacro(vtkCarbonRenderWindow);

35 36
//----------------------------------------------------------------------------
#include <AvailabilityMacros.h>
37
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
38 39 40 41 42 43
enum
{
  kEventControlVisibilityChanged = 157
};
#endif

44 45 46 47 48 49 50 51
//----------------------------------------------------------------------------
// At runtime, AGL_MULTISAMPLE is available on Mac OS X 10.3 and later,
// however the #define was not added until the 10.3.9 SDK, so we define it
// here if it isn't already defined
#ifndef AGL_MULTISAMPLE
  #define AGL_MULTISAMPLE 59
#endif

Yves Starreveld's avatar
Yves Starreveld committed
52 53
//----------------------------------------------------------------------------
// Dump agl errors to string, return error code
54
OSStatus aglReportError ()
Yves Starreveld's avatar
Yves Starreveld committed
55 56 57 58 59 60 61 62 63 64 65 66 67
{
  GLenum err = aglGetError();
  if (AGL_NO_ERROR != err)
    cout << ((char *)aglErrorString(err));
  // ensure we are returning an OSStatus noErr if no error condition
  if (err == AGL_NO_ERROR)
    return noErr;
  else
    return (OSStatus) err;
}

//----------------------------------------------------------------------------
// if error dump gl errors, return error
68
OSStatus glReportError ()
Yves Starreveld's avatar
Yves Starreveld committed
69 70 71
{
  GLenum err = glGetError();
  switch (err)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    {
    case GL_NO_ERROR:
      break;
    case GL_INVALID_ENUM:
      cout << "GL Error: Invalid enumeration\n";
      break;
    case GL_INVALID_VALUE:
      cout << "GL Error: Invalid value\n";
      break;
    case GL_INVALID_OPERATION:
      cout << "GL Error: Invalid operation\n";
      break;
    case GL_STACK_OVERFLOW:
      cout << "GL Error: Stack overflow\n";
      break;
    case GL_STACK_UNDERFLOW:
      cout << "GL Error: Stack underflow\n";
      break;
    case GL_OUT_OF_MEMORY:
      cout << "GL Error: Out of memory\n";
      break;
    }
Yves Starreveld's avatar
Yves Starreveld committed
94 95 96 97 98 99 100
  // ensure we are returning an OSStatus noErr if no error condition
  if (err == GL_NO_ERROR)
    return noErr;
  else
    return (OSStatus) err;
}

101
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
102 103 104 105 106
static void* vtkCreateOSWindow(int width, int height, int pixel_size)
{
  return malloc(width*height*pixel_size);
}

107
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
108 109 110 111 112 113 114 115 116
static void vtkDestroyOSWindow(void* win)
{
  free(win);
}

class vtkCarbonRenderWindowInternal
{
public:
  vtkCarbonRenderWindowInternal(vtkRenderWindow* win)
117 118 119 120 121 122
    {
      this->OffScreenWindow = NULL;
      this->OffScreenContextId = NULL;
      this->ScreenMapped = win->GetMapped();
      this->ScreenDoubleBuffer = win->GetDoubleBuffer();
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
123 124 125 126 127 128 129

  void* OffScreenWindow;
  AGLContext OffScreenContextId;
  AGLPixelFormat OffScreenPixelFmt;
  int ScreenMapped;
  int ScreenDoubleBuffer;
  
130
  AGLPixelFormat ChoosePixelFormat(int accel, int offscreen, int doublebuff, int stereo, 
131
    int multisamples, int alphaBitPlanes, int stencil);
132 133

  AGLContext CreateContext(int offscreen, int& doublebuff, int& stereo, 
134
    int& multisamples, int& alphaBitPlanes, int &stencil, const char*& error);
135
  
Clinton Stimpson's avatar
 
Clinton Stimpson committed
136 137
};

138
AGLPixelFormat vtkCarbonRenderWindowInternal::ChoosePixelFormat(int accel, int offscreen, int doublebuff, 
139
  int stereo, int multisamples, int alphaBitPlanes, int stencil)
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
{
  int i = 0;
  GLint attr[64];

  if(offscreen)
    {
    attr[i++] = AGL_OFFSCREEN;
    }
  if(doublebuff)
    {
    attr[i++] = AGL_DOUBLEBUFFER;
    }
  attr[i++] = AGL_RGBA;
  attr[i++] = AGL_DEPTH_SIZE;
  attr[i++] = 32;
  attr[i++] = AGL_PIXEL_SIZE;
  attr[i++] = 32;
  if(accel)
    {
    attr[i++] = AGL_ACCELERATED;
    }
  if(multisamples)
    {
    attr[i++] = AGL_SAMPLE_BUFFERS_ARB;
    attr[i++] = 1;
    attr[i++] = AGL_SAMPLES_ARB;
    attr[i++] = multisamples;
    attr[i++] = AGL_MULTISAMPLE;
    }
  if (alphaBitPlanes)
    {
    attr[i++] = AGL_ALPHA_SIZE;
    attr[i++] = 8;
    }
  if(stereo)
    {
    attr[i++] = AGL_STEREO;
    attr[i++] = GL_TRUE;
    }
179 180 181 182 183
  if(stencil)
    {
    attr[i++] = AGL_STENCIL_SIZE;
    attr[i++] = 8;    
    }
184 185 186 187 188 189 190
  attr[i++] = AGL_NO_RECOVERY;  // must choose the pixel format we want!
  attr[i++] = AGL_NONE;

  return aglChoosePixelFormat (NULL, 0, attr);
}

AGLContext vtkCarbonRenderWindowInternal::CreateContext(int offscreen, int& doublebuff, 
191
  int& stereo, int& multisamples, int& alphaBitPlanes, int& stencil, const char*& error)
192 193 194 195 196 197 198
{
  error = NULL;
  AGLContext ctx = 0;
  AGLPixelFormat fmt = 0;
  int noSoftwareRendering = 1;  // flip to zero if you're willing to do software
                                // rendering to get more features.

199
  int _db, _a, _s, _m, _stencil;
200

201
  for(_stencil = stencil; !fmt && _stencil >= 0; _stencil--)
202
    {
203
    for(_db = doublebuff; !fmt && _db >= 0; _db--)
204
      {
205
      for(_a = alphaBitPlanes; !fmt && _a >= 0; _a--)
206
        {
207
        for(_s = stereo; !fmt && _s >= 0; _s--)
208
          {
209
          for(_m = multisamples; !fmt && _m >= 0; _m--)
210
            {
211
            for(int accel = 1; !fmt && accel >= noSoftwareRendering; accel--)
212
              {
Dave Demarle's avatar
Dave Demarle committed
213
              fmt = this->ChoosePixelFormat(accel, offscreen, _db, _s, _m, _a, _stencil);
214 215 216 217 218 219 220 221
              if(fmt)
                {
                doublebuff = _db;
                stereo = _s;
                multisamples = _m;
                alphaBitPlanes = _a;
                stencil = _stencil;
                }
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
              }
            }
          }
        }
      }
    }
  aglReportError (); // cough up any errors encountered
  if (NULL == fmt)
    {
    error = "Could not find valid pixel format";
    return NULL;
    }
  
  ctx = aglCreateContext (fmt, 0); // create without sharing
  aglDestroyPixelFormat(fmt);
  aglReportError (); // cough up errors
  if (NULL == ctx)
    {
    error = "Could not create context";
    return NULL;
    }
  return ctx;
}


Yves Starreveld's avatar
Yves Starreveld committed
247
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
248 249
vtkCarbonRenderWindow::vtkCarbonRenderWindow()
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
250
  this->Internal = new vtkCarbonRenderWindowInternal(this);
Yves Starreveld's avatar
Yves Starreveld committed
251 252
  this->ApplicationInitialized = 0;
  this->ContextId = 0;
253
  this->MultiSamples = 0;
Yves Starreveld's avatar
Yves Starreveld committed
254
  this->WindowId = 0;
255
  this->ParentId = 0;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
256
  this->RootWindow = 0;
257
  this->OwnWindow = 0; // Keep before SetWindowName.
Yves Starreveld's avatar
Yves Starreveld committed
258 259
  this->SetWindowName("Visualization Toolkit - Carbon");
  this->CursorHidden = 0;
260
  this->ForceMakeCurrent = 0;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
261 262
  this->RegionEventHandlerUPP = 0;
  this->RegionEventHandler = 0;
Yves Starreveld's avatar
Yves Starreveld committed
263 264 265 266 267
}

// --------------------------------------------------------------------------
vtkCarbonRenderWindow::~vtkCarbonRenderWindow()
{
Yves Starreveld's avatar
 
Yves Starreveld committed
268
  this->Finalize();
269 270 271 272 273 274 275 276 277
  
  vtkRenderer *ren;
  vtkCollectionSimpleIterator rit;
  this->Renderers->InitTraversal(rit);
  while ( (ren = this->Renderers->GetNextRenderer(rit)) )
    {
    ren->SetRenderWindow(NULL);
    }

Clinton Stimpson's avatar
 
Clinton Stimpson committed
278
  delete this->Internal;
Yves Starreveld's avatar
Yves Starreveld committed
279 280
}

Yves Starreveld's avatar
Yves Starreveld committed
281
//--------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
282
void vtkCarbonRenderWindow::DestroyWindow()
Yves Starreveld's avatar
Yves Starreveld committed
283
{
284
  GLuint txId;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
285 286 287 288 289 290 291 292 293
  
  this->MakeCurrent();

  // tell each of the renderers that this render window/graphics context
  // is being removed (the RendererCollection is removed by vtkRenderWindow's
  // destructor)
  vtkCollectionSimpleIterator rsit;
  vtkRenderer *ren;
  for ( this->Renderers->InitTraversal(rsit);
294
        (ren = this->Renderers->GetNextRenderer(rsit));)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
295 296
    {
    ren->SetRenderWindow(NULL);
297
    ren->SetRenderWindow(this);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
298
    }
Yves Starreveld's avatar
Yves Starreveld committed
299 300

  /* finish OpenGL rendering */
Clinton Stimpson's avatar
 
Clinton Stimpson committed
301
  if (this->ContextId)
Yves Starreveld's avatar
Yves Starreveld committed
302 303 304 305 306 307
    {

    /* now delete all textures */
    glDisable(GL_TEXTURE_2D);
    for (int i = 1; i < this->TextureResourceIds->GetNumberOfIds(); i++)
      {
308
      txId = (GLuint) this->TextureResourceIds->GetId(i);
Yves Starreveld's avatar
Yves Starreveld committed
309
#ifdef GL_VERSION_1_1
310
      if (glIsTexture(txId))
Yves Starreveld's avatar
Yves Starreveld committed
311
        {
312
        glDeleteTextures(1, &txId);
Yves Starreveld's avatar
Yves Starreveld committed
313
        }
Yves Starreveld's avatar
Yves Starreveld committed
314
#else
315
      if (glIsList(txId))
Yves Starreveld's avatar
Yves Starreveld committed
316
        {
317
        glDeleteLists(txId,1);
Yves Starreveld's avatar
Yves Starreveld committed
318
        }
Yves Starreveld's avatar
Yves Starreveld committed
319 320 321
#endif
      }

322 323 324
    aglSetCurrentContext(this->ContextId);
    aglDestroyContext(this->ContextId);
    this->ContextId = NULL;
Yves Starreveld's avatar
Yves Starreveld committed
325
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  
  // remove event filters if we have them
  if(this->RegionEventHandler)
    {
    RemoveEventHandler(this->RegionEventHandler);
    DisposeEventHandlerUPP(this->RegionEventHandlerUPP);
    this->RegionEventHandler = 0;
    this->RegionEventHandlerUPP = 0;
    }

  if (this->RootWindow && this->OwnWindow)
    {
    DisposeWindow(this->RootWindow);
    this->RootWindow = 0;
    this->WindowId = 0;
    }

  this->Mapped = 0;
Yves Starreveld's avatar
Yves Starreveld committed
344 345
}

Yves Starreveld's avatar
Yves Starreveld committed
346
//--------------------------------------------------------------------------
347
void vtkCarbonRenderWindow::SetWindowName( const char * _arg )
Yves Starreveld's avatar
Yves Starreveld committed
348 349 350
{
  vtkWindow::SetWindowName(_arg);

351
  if(this->OwnWindow)
Yves Starreveld's avatar
Yves Starreveld committed
352
    {
353 354 355 356 357
    CFStringRef newTitle =
      CFStringCreateWithCString(kCFAllocatorDefault, _arg,
                                kCFStringEncodingASCII);
    SetWindowTitleWithCFString(this->RootWindow, newTitle);
    CFRelease(newTitle);
Yves Starreveld's avatar
Yves Starreveld committed
358 359 360
    }
}

Yves Starreveld's avatar
Yves Starreveld committed
361
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
362 363 364 365 366
int vtkCarbonRenderWindow::GetEventPending()
{
  return 0;
}

367 368
//--------------------------------------------------------------------------
// Set the window id to a pre-existing window.
Clinton Stimpson's avatar
 
Clinton Stimpson committed
369
void vtkCarbonRenderWindow::SetParentId(HIViewRef arg)
370 371 372 373 374 375
{
  vtkDebugMacro(<< "Setting ParentId to " << arg << "\n");

  this->ParentId = arg;
}

Yves Starreveld's avatar
Yves Starreveld committed
376
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
377
// Begin the rendering process.
378
void vtkCarbonRenderWindow::Start()
Yves Starreveld's avatar
Yves Starreveld committed
379 380
{
  // if the renderer has not been initialized, do so now
Clinton Stimpson's avatar
 
Clinton Stimpson committed
381
  this->Initialize();
Yves Starreveld's avatar
Yves Starreveld committed
382 383 384 385 386 387 388 389

  // set the current window
  this->MakeCurrent();
}

// --------------------------------------------------------------------------
void vtkCarbonRenderWindow::MakeCurrent()
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
390
  if(this->OffScreenRendering && this->Internal->OffScreenContextId)
Yves Starreveld's avatar
Yves Starreveld committed
391
    {
392 393
    if((this->Internal->OffScreenContextId != aglGetCurrentContext())
       || this->ForceMakeCurrent)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
394 395 396 397 398 399 400 401 402 403 404 405
      {
      aglSetCurrentContext(this->Internal->OffScreenContextId);
      this->ForceMakeCurrent = 0;
      }
    }
  else if (this->ContextId || this->ForceMakeCurrent)
    {
    if((this->ContextId != aglGetCurrentContext()) || this->ForceMakeCurrent)
      {
      aglSetCurrentContext(this->ContextId);
      this->ForceMakeCurrent = 0;
      }
Yves Starreveld's avatar
Yves Starreveld committed
406
    }
Yves Starreveld's avatar
Yves Starreveld committed
407 408
}

409 410 411 412 413 414 415 416 417 418 419 420 421
// ----------------------------------------------------------------------------
// Description:
// Tells if this window is the current OpenGL context for the calling thread.
bool vtkCarbonRenderWindow::IsCurrent()
{
  bool result;
  
  if(this->OffScreenRendering && this->Internal->OffScreenContextId)
    {
    result=this->Internal->OffScreenContextId==aglGetCurrentContext();
    }
  else
    {
422
    result=this->ContextId!=0 && this->ContextId==aglGetCurrentContext();
423 424 425 426
    }
  return result;
}

427
// --------------------------------------------------------------------------
428 429
void vtkCarbonRenderWindow::SetForceMakeCurrent()
{
430
  this->ForceMakeCurrent = 1;
431 432
}

433 434 435 436 437 438
// --------------------------------------------------------------------------
int vtkCarbonRenderWindow::IsDirect()
{
  return 1;
}

439 440 441 442 443 444
// --------------------------------------------------------------------------
void vtkCarbonRenderWindow::SetSize(int a[2])
{
  this->SetSize(a[0], a[1]);
}

Clinton Stimpson's avatar
 
Clinton Stimpson committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
void vtkCarbonRenderWindow::UpdateGLRegion()
{
  // if WindowId is a child window
  if(this->WindowId)
    {
    // Determine the AGL_BUFFER_RECT for the view. The coordinate
    // system for this rectangle is relative to the owning window, with
    // the origin at the bottom left corner and the y-axis inverted.
    HIRect viewBounds, winBounds;
    HIViewGetBounds(this->WindowId, &viewBounds);
    HIViewRef root = HIViewGetRoot(this->GetRootWindow());
    HIViewRef content_root;
    HIViewFindByID(root, kHIViewWindowContentID, &content_root);

    HIViewGetBounds(content_root, &winBounds);
    HIViewConvertRect(&viewBounds, this->WindowId, content_root);
    GLint bufferRect[4] = { GLint(viewBounds.origin.x), 
                            GLint((winBounds.size.height) - (viewBounds.origin.y + viewBounds.size.height)), 
                            GLint(viewBounds.size.width), 
                            GLint(viewBounds.size.height) };
Clinton Stimpson's avatar
 
Clinton Stimpson committed
465 466 467 468 469 470 471
    if(!HIViewIsVisible(this->WindowId))
      {
      bufferRect[0] = 0;
      bufferRect[1] = 0;
      bufferRect[2] = 0;
      bufferRect[3] = 0;
      }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
472 473
    
    // Associate the OpenGL context with the control's window, and establish the buffer rect.
474
#if 0
475 476
    aglSetWindowRef(this->ContextId, this->GetRootWindow());
#else
Clinton Stimpson's avatar
 
Clinton Stimpson committed
477
    aglSetDrawable(this->ContextId, GetWindowPort(this->GetRootWindow()));
478
#endif
Clinton Stimpson's avatar
 
Clinton Stimpson committed
479 480 481 482 483 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
    aglSetInteger(this->ContextId, AGL_BUFFER_RECT, bufferRect);
    aglEnable(this->ContextId, AGL_BUFFER_RECT);
  
    // Establish the clipping region for the OpenGL context. To properly handle clipping
    // within the view hierarchy, walk the hierarchy to determine the intersection
    // of this view's bounds with its children, siblings, and parents also taking into 
    // account z-ordering of the views
    RgnHandle rgn = NewRgn();
    RgnHandle tmp_rgn = NewRgn();

    GetControlRegion(this->WindowId, kControlStructureMetaPart, rgn);
    HIViewConvertRegion(rgn, this->WindowId, content_root);

    HIViewRef current_view = NULL;
    HIViewRef child = NULL;
    HIViewRef last = NULL;

    for(current_view = this->WindowId; 
        (current_view != NULL); 
        current_view = HIViewGetSuperview(current_view))
      {
      if(last)
        {
        // clip view within parent bounds
        GetControlRegion(current_view, kControlStructureMetaPart, tmp_rgn);
        HIViewConvertRegion(tmp_rgn, current_view, content_root);
        DiffRgn(rgn, tmp_rgn, tmp_rgn);
        DiffRgn(rgn, tmp_rgn, rgn);
        }
      for(child = HIViewGetFirstSubview(current_view); 
          (child != last) && (child != NULL); 
          child = HIViewGetNextView(child))
        {
        if(child != last && HIViewIsVisible(child))
          {
          GetControlRegion(child, kControlStructureMetaPart, tmp_rgn);
          HIViewConvertRegion(tmp_rgn, child, content_root);
          DiffRgn(rgn, tmp_rgn, rgn);
          }
        }
519
      last = current_view;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
520 521 522 523 524 525 526
      }
    
    GetControlRegion(this->WindowId, kControlStructureMetaPart, tmp_rgn);
    
    if(EqualRgn(rgn,tmp_rgn))
      {
      if(aglIsEnabled(this->ContextId, AGL_CLIP_REGION))
527
        {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
528
        aglDisable(this->ContextId, AGL_CLIP_REGION);
529
        }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
530 531 532 533
      }
    else
      {
      if(!aglIsEnabled(this->ContextId, AGL_CLIP_REGION))
534
        {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
535
        aglEnable(this->ContextId, AGL_CLIP_REGION);
536
        }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
      aglSetInteger(this->ContextId, AGL_CLIP_REGION, reinterpret_cast<const GLint*>(rgn));
      }
      
    DisposeRgn(rgn);
    DisposeRgn(tmp_rgn);

    }
  // this is provided for backwards compatibility
  else if(this->WindowId == 0 && this->RootWindow && this->ParentId)
    {
    GLint bufRect[4];
    Rect windowRect;
    GetWindowBounds(this->RootWindow, kWindowContentRgn, &windowRect);
    bufRect[0] = this->Position[0];
    bufRect[1] = (int) (windowRect.bottom-windowRect.top)
552
      - (this->Position[1]+this->Size[1]);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
553 554 555 556 557 558 559 560 561 562
    bufRect[2] = this->Size[0];
    bufRect[3] = this->Size[1];
    aglEnable(this->ContextId, AGL_BUFFER_RECT);
    aglSetInteger(this->ContextId, AGL_BUFFER_RECT, bufRect);
    }

  aglUpdateContext(this->ContextId);

}

Yves Starreveld's avatar
Yves Starreveld committed
563 564 565 566 567 568 569 570 571
// --------------------------------------------------------------------------
void vtkCarbonRenderWindow::SetSize(int x, int y)
{
  static int resizing = 0;

  if ((this->Size[0] != x) || (this->Size[1] != y))
    {
    this->Size[0] = x;
    this->Size[1] = y;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
572

573 574 575
    if(this->OffScreenRendering && 
       (this->Internal->OffScreenWindow 
        || this->OffScreenUseFrameBuffer))
Yves Starreveld's avatar
Yves Starreveld committed
576
      {
577 578 579 580 581
      this->ResizeOffScreenWindow(x,y);
      }
    else
      {
      if (this->Mapped)
582
        {
583
        if (!resizing)
584
          {
585 586 587 588 589 590 591 592 593 594 595 596
          resizing = 1;

          if(this->ParentId && this->RootWindow && !this->WindowId)
            {
            // backwards compatiblity with Tk and who else?
            UpdateGLRegion();
            }
          else if(this->OwnWindow || !this->WindowId)
            {
            SizeWindow(this->RootWindow, x, y, TRUE);
            }
          resizing = 0;
597
          }
598
        }
Yves Starreveld's avatar
Yves Starreveld committed
599
      }
600 601

    this->Modified();
Yves Starreveld's avatar
Yves Starreveld committed
602 603 604
    }
}

605 606 607 608 609 610
// --------------------------------------------------------------------------
void vtkCarbonRenderWindow::SetPosition(int a[2])
{
  this->SetPosition(a[0], a[1]);
}

Yves Starreveld's avatar
Yves Starreveld committed
611 612 613 614 615 616 617 618 619 620 621 622 623
// --------------------------------------------------------------------------
void vtkCarbonRenderWindow::SetPosition(int x, int y)
{
  static int resizing = 0;

  if ((this->Position[0] != x) || (this->Position[1] != y))
    {
    this->Modified();
    this->Position[0] = x;
    this->Position[1] = y;
    if (this->Mapped)
      {
      if (!resizing)
624 625
        {
        resizing = 1;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
626 627
        
        if(this->ParentId && this->RootWindow && !this->WindowId)
628
          {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
629 630
          // backwards compatiblity with Tk and who else?
          UpdateGLRegion();
631
          }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
632
        else if(this->OwnWindow || !this->WindowId)
633
          {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
634
          MoveWindow(this->RootWindow, x, y, FALSE);
635 636 637 638
          }

        resizing = 0;
        }
Yves Starreveld's avatar
Yves Starreveld committed
639 640 641 642 643
      }
    }
}


Yves Starreveld's avatar
Yves Starreveld committed
644
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
645
// End the rendering process and display the image.
646
void vtkCarbonRenderWindow::Frame()
Yves Starreveld's avatar
Yves Starreveld committed
647
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
648 649
  this->MakeCurrent();
  if (!this->AbortRender && this->DoubleBuffer && this->SwapBuffers)
Yves Starreveld's avatar
Yves Starreveld committed
650
    {
651
    glFinish();
Yves Starreveld's avatar
Yves Starreveld committed
652
    aglSwapBuffers(this->ContextId);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
653
    vtkDebugMacro(<< " aglSwapBuffers\n");
Yves Starreveld's avatar
Yves Starreveld committed
654
    }
655 656 657 658
  else
    {
    glFlush();
    }
Yves Starreveld's avatar
Yves Starreveld committed
659
}
Mathieu Malaterre's avatar
Mathieu Malaterre committed
660 661

//--------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
662 663 664 665 666 667 668 669
AGLContext vtkCarbonRenderWindow::GetContextId()
{
  if(this->OffScreenRendering)
    {
    return this->Internal->OffScreenContextId;
    }
  return this->ContextId;
}
Yves Starreveld's avatar
Yves Starreveld committed
670

Yves Starreveld's avatar
Yves Starreveld committed
671
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
672 673 674 675 676 677
// Specify various window parameters.
void vtkCarbonRenderWindow::WindowConfigure()
{
  // this is all handled by the desiredVisualInfo method
}

Yves Starreveld's avatar
Yves Starreveld committed
678 679 680 681 682
//--------------------------------------------------------------------------
void vtkCarbonRenderWindow::InitializeApplication()
{
  if (!this->ApplicationInitialized)
    {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
683
    if (this->OwnWindow)
684 685
      {
      // Initialize the Toolbox managers if we are running the show
686 687 688
      DrawMenuBar();
      this->ApplicationInitialized=1;
      }
Yves Starreveld's avatar
Yves Starreveld committed
689 690 691
    }
}

Yves Starreveld's avatar
Yves Starreveld committed
692
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
693
// Initialize the window for rendering.
694
void vtkCarbonRenderWindow::CreateAWindow()
Yves Starreveld's avatar
Yves Starreveld committed
695 696 697 698
{
  static int count = 1;
  char *windowName;
  
Clinton Stimpson's avatar
 
Clinton Stimpson committed
699 700
  // if a Window and HIView wasn't given, make a Window and HIView
  if (!this->WindowId && !this->RootWindow)
Yves Starreveld's avatar
Yves Starreveld committed
701
    {
702 703 704 705 706 707 708 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
    this->Position[0] = ((this->Position[0] >= 0) ? this->Position[0] : 5);
    this->Position[1] = ((this->Position[1] >= 0) ? this->Position[1] : 5);
    this->Size[0] = ((this->Size[0] > 0) ? this->Size[0] : 300);
    this->Size[1] = ((this->Size[1] > 0) ? this->Size[1] : 300);
    
    // Rect is defined as {top, left, bottom, right} (really)
    Rect rectWin = {this->Position[1], this->Position[0],
                    this->Position[1]+this->Size[1],
                    this->Position[0]+this->Size[0]};
    
    WindowAttributes windowAttrs = (kWindowStandardDocumentAttributes | 
                                    kWindowLiveResizeAttribute |
                                    kWindowStandardHandlerAttribute |
                                    kWindowCompositingAttribute);
    
    if (noErr != CreateNewWindow (kDocumentWindowClass,
                                  windowAttrs,
                                  &rectWin, &(this->RootWindow)))
      {
      vtkErrorMacro("Could not create window, serious error!");
      return;
      }
    
    // get the content view
    HIViewFindByID(HIViewGetRoot(this->RootWindow),
                   kHIViewWindowContentID,
                   &this->WindowId);
    
    int len = (strlen("Visualization Toolkit - Carbon #")
               + (int) ceil((double)log10((double)(count+1)))
               + 1);
    windowName = new char [ len ];
    sprintf(windowName,"Visualization Toolkit - Carbon #%i",count++);
    this->OwnWindow = 1;
    this->SetWindowName(windowName);
    delete [] windowName;
    
    ShowWindow(this->RootWindow);
Yves Starreveld's avatar
Yves Starreveld committed
740
    }
741 742
  
  
Clinton Stimpson's avatar
 
Clinton Stimpson committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  // install event handler for updating gl region
  // this works for a supplied HIView and an HIView made here
  if(this->WindowId && !this->RegionEventHandler)
    {
    EventTypeSpec region_events [] = 
      {
        { kEventClassControl, kEventControlOwningWindowChanged},
        { kEventClassControl, kEventControlVisibilityChanged },
        { kEventClassControl, kEventControlBoundsChanged }
      };
    this->RegionEventHandlerUPP = NewEventHandlerUPP(vtkCarbonRenderWindow::RegionEventProcessor);
    InstallControlEventHandler(this->WindowId, this->RegionEventHandlerUPP, 
                               GetEventTypeCount(region_events), region_events,
                               reinterpret_cast<void*>(this), &this->RegionEventHandler);
    }
  
  SetPortWindowPort(this->GetRootWindow());
760

Clinton Stimpson's avatar
Clinton Stimpson committed
761
  const char* error = NULL;
762 763
  this->ContextId = this->Internal->CreateContext(0, this->DoubleBuffer,
                                this->StereoCapableWindow, this->MultiSamples, 
Dave Demarle's avatar
Dave Demarle committed
764 765
                                this->AlphaBitPlanes, this->StencilCapable, 
                                error);
Yves Starreveld's avatar
Yves Starreveld committed
766 767
  if (NULL == this->ContextId)
    {
768 769 770 771
    if(error)
      {
      vtkErrorMacro(<<error);
      }
Yves Starreveld's avatar
Yves Starreveld committed
772 773
    return;
    }
774 775 776 777 778 779 780 781 782 783

  // This syncs the OpenGL context to the VBL to prevent tearing
  GLint one = 1;
  GLboolean res = aglSetInteger (this->ContextId, AGL_SWAP_INTERVAL, &one);
  if (GL_FALSE == res)
    {
    vtkErrorMacro ("Could not set context option");
    return;
    }

784
#if 0
785 786 787
  // attach the WindowRef to the context
  if (!aglSetWindowRef (this->ContextId, this->GetRootWindow()))
#else
Yves Starreveld's avatar
Yves Starreveld committed
788
  // attach the CGrafPtr to the context
Clinton Stimpson's avatar
 
Clinton Stimpson committed
789
  if (!aglSetDrawable (this->ContextId, GetWindowPort (this->GetRootWindow())))
790
#endif
Yves Starreveld's avatar
Yves Starreveld committed
791 792 793 794 795 796 797 798 799 800 801 802
    {
    aglReportError();
    return;
    }

  if(!aglSetCurrentContext (this->ContextId))
    // make the context the current context
    {
    aglReportError();
    return;
    }

Yves Starreveld's avatar
Yves Starreveld committed
803 804
  this->OpenGLInit();
  this->Mapped = 1;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
805
  UpdateGLRegion();
Yves Starreveld's avatar
Yves Starreveld committed
806 807
}

Yves Starreveld's avatar
Yves Starreveld committed
808 809 810
//--------------------------------------------------------------------------
// Initialize the window for rendering.
void vtkCarbonRenderWindow::WindowInitialize()
811
{ 
Yves Starreveld's avatar
Yves Starreveld committed
812
  // create our own window if not already set
Clinton Stimpson's avatar
 
Clinton Stimpson committed
813 814
  this->InitializeApplication();
  this->OwnWindow = 0;
815
  this->CreateAWindow();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
816
  
Clinton Stimpson's avatar
 
Clinton Stimpson committed
817 818 819 820 821 822 823 824
  // tell our renderers about us
  vtkRenderer* ren;
  for (this->Renderers->InitTraversal(); 
       (ren = this->Renderers->GetNextItem());)
    {
    ren->SetRenderWindow(0);
    ren->SetRenderWindow(this);
    }
Yves Starreveld's avatar
Yves Starreveld committed
825 826 827 828 829
    
  // set the DPI
  this->SetDPI(72); // this may need to be more clever some day
}

830
//-----------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
831
// Initialize the rendering window.
832
void vtkCarbonRenderWindow::Initialize ()
Yves Starreveld's avatar
Yves Starreveld committed
833 834
{
  // make sure we havent already been initialized
835

Clinton Stimpson's avatar
 
Clinton Stimpson committed
836
  if(!this->OffScreenRendering && !this->ContextId)
Yves Starreveld's avatar
Yves Starreveld committed
837
    {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
838 839
    this->WindowInitialize();
    }
840 841 842
  else if(this->OffScreenRendering &&
          !(this->Internal->OffScreenContextId
            || this->OffScreenUseFrameBuffer))
Clinton Stimpson's avatar
 
Clinton Stimpson committed
843 844 845 846
    {
    // initialize offscreen window
    int width = ((this->Size[0] > 0) ? this->Size[0] : 300);
    int height = ((this->Size[1] > 0) ? this->Size[1] : 300);
847
    this->CreateOffScreenWindow(width, height);
Yves Starreveld's avatar
Yves Starreveld committed
848
    }
Yves Starreveld's avatar
Yves Starreveld committed
849 850
}

851
//-----------------------------------------------------------------------------
Yves Starreveld's avatar
 
Yves Starreveld committed
852 853 854 855
void vtkCarbonRenderWindow::Finalize(void)
{
  if (this->CursorHidden)
    {
856
    this->ShowCursor();
Yves Starreveld's avatar
 
Yves Starreveld committed
857 858
    }

Clinton Stimpson's avatar
 
Clinton Stimpson committed
859 860 861
  this->SetOffScreenRendering(0);

  this->DestroyWindow();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
862

Yves Starreveld's avatar
 
Yves Starreveld committed
863 864
}

865
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
866 867 868 869 870 871
void vtkCarbonRenderWindow::SetOffScreenRendering(int i)
{
  if (this->OffScreenRendering == i)
    {
    return;
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
872
  
Clinton Stimpson's avatar
 
Clinton Stimpson committed
873 874 875 876 877 878 879 880 881 882 883 884
  Superclass::SetOffScreenRendering(i);
  
  // setup the offscreen area
  if(i)
    {
    this->Internal->ScreenDoubleBuffer = this->DoubleBuffer;
    this->DoubleBuffer = 0;
    this->Internal->ScreenMapped = this->Mapped;
    this->Mapped = 0;
    }
  else
    {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
885 886
    this->DestroyOffScreenWindow();
    
Clinton Stimpson's avatar
 
Clinton Stimpson committed
887 888 889 890 891 892 893
    this->DoubleBuffer = this->Internal->ScreenDoubleBuffer;
    this->Mapped = this->Internal->ScreenMapped;
    // reset the size based on the screen window
    this->GetSize();
    }
}

894
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
895 896
void vtkCarbonRenderWindow::CreateOffScreenWindow(int width, int height)
{
897
  if(!this->CreateHardwareOffScreenWindow(width,height))
Clinton Stimpson's avatar
 
Clinton Stimpson committed
898
    {
Clinton Stimpson's avatar
Clinton Stimpson committed
899
    const char* error = NULL;
900 901 902 903
    int doubleBuf = 0;
    this->Internal->OffScreenContextId = 
      this->Internal->CreateContext(1, doubleBuf,
         this->StereoCapableWindow, this->MultiSamples, 
Dave Demarle's avatar
Dave Demarle committed
904
         this->AlphaBitPlanes, this->StencilCapable, error);
905
    
906 907 908 909 910 911 912 913 914 915
    this->Internal->OffScreenWindow = vtkCreateOSWindow(width, height, 4);
    this->Size[0] = width;
    this->Size[1] = height;
      
    aglSetOffScreen(this->Internal->OffScreenContextId, 
                    width, height, width*4,
                    this->Internal->OffScreenWindow);
      
    aglSetCurrentContext(this->Internal->OffScreenContextId);
    } // if not hardware
916
  this->Mapped = 0;
917
  
Clinton Stimpson's avatar
 
Clinton Stimpson committed
918 919 920 921 922 923 924 925 926
  vtkRenderer *ren;
  vtkCollectionSimpleIterator rit;
  this->Renderers->InitTraversal(rit);
  while ( (ren = this->Renderers->GetNextRenderer(rit)) )
    {
    ren->SetRenderWindow(NULL);
    ren->SetRenderWindow(this);
    }

Clinton Stimpson's avatar
 
Clinton Stimpson committed
927 928 929
  this->OpenGLInit();
}

930
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
931 932 933 934 935 936 937 938 939 940 941 942
void vtkCarbonRenderWindow::DestroyOffScreenWindow()
{
  // release graphic resources.
  vtkRenderer *ren;
  vtkCollectionSimpleIterator rit;
  this->Renderers->InitTraversal(rit);
  while ( (ren = this->Renderers->GetNextRenderer(rit)) )
    {
    ren->SetRenderWindow(NULL);
    ren->SetRenderWindow(this);
    }

943
  if(this->OffScreenUseFrameBuffer)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
944
    {
945 946 947 948 949 950 951 952 953 954 955
    this->DestroyHardwareOffScreenWindow();
    }
  else
    {
    if(this->Internal->OffScreenContextId)
      {
      aglDestroyContext(this->Internal->OffScreenContextId);
      this->Internal->OffScreenContextId = NULL;
      vtkDestroyOSWindow(this->Internal->OffScreenWindow);
      this->Internal->OffScreenWindow = NULL;
      }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
956 957 958
    }
}

959
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
960 961 962 963 964 965 966
void vtkCarbonRenderWindow::ResizeOffScreenWindow(int width, int height)
{
  if(!this->OffScreenRendering)
    {
    return;
    }

967
  if(this->OffScreenUseFrameBuffer || this->Internal->OffScreenContextId)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
968 969 970 971 972 973 974
    {
    this->DestroyOffScreenWindow();
    this->CreateOffScreenWindow(width, height);
    }
}


Yves Starreveld's avatar
Yves Starreveld committed
975 976
//--------------------------------------------------------------------------
void vtkCarbonRenderWindow::UpdateSizeAndPosition(int xPos, int yPos, 
977
                                                  int xSize, int ySize)
Yves Starreveld's avatar
Yves Starreveld committed
978 979 980 981 982 983 984 985
{
  this->Size[0]=xSize;
  this->Size[1]=ySize;
  this->Position[0]=xPos;
  this->Position[1]=yPos;
  this->Modified();
}

Yves Starreveld's avatar
Yves Starreveld committed
986
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
987
// Get the current size of the window.
988
int *vtkCarbonRenderWindow::GetSize()
Yves Starreveld's avatar
Yves Starreveld committed
989
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
990
  if(this->WindowId && this->Mapped)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
991 992 993 994 995
    {
    HIRect viewBounds;
    HIViewGetBounds(this->WindowId, &viewBounds);
    this->Size[0] = (int)viewBounds.size.width;
    this->Size[1] = (int)viewBounds.size.height;
996 997
    }

Andy Cedilnik's avatar
Andy Cedilnik committed
998
  return this->Superclass::GetSize();
Yves Starreveld's avatar
Yves Starreveld committed
999 1000
}

Yves Starreveld's avatar
Yves Starreveld committed
1001
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
1002
// Get the current size of the screen.
1003
int *vtkCarbonRenderWindow::GetScreenSize()
Yves Starreveld's avatar
Yves Starreveld committed
1004
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
1005 1006 1007
  CGRect r = CGDisplayBounds(CGMainDisplayID());
  this->Size[0] = (int)r.size.width;
  this->Size[1] = (int)r.size.height;
Yves Starreveld's avatar
Yves Starreveld committed
1008 1009 1010
  return this->Size;
}

Yves Starreveld's avatar
Yves Starreveld committed
1011
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
1012
// Get the position in screen coordinates of the window.
1013
int *vtkCarbonRenderWindow::GetPosition()
Yves Starreveld's avatar
Yves Starreveld committed
1014 1015 1016
{
  // if we aren't mapped then just return the ivar
  if (!this->Mapped)
Yves Starreveld's avatar
Yves Starreveld committed
1017
    {
1018
    return this->Position;
Yves Starreveld's avatar
Yves Starreveld committed
1019
    }
Yves Starreveld's avatar
Yves Starreveld committed
1020

Clinton Stimpson's avatar
 
Clinton Stimpson committed
1021
  if(!this->WindowId && !this->ParentId)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    {
    //  Find the current window position
    Rect windowRect;
    GetWindowBounds(this->GetRootWindow(), kWindowContentRgn, &windowRect);
    this->Position[0] = windowRect.left;
    this->Position[1] = windowRect.top;
    }
  else
    {
    HIRect viewBounds;
    HIViewGetBounds(this->WindowId, &viewBounds);
    Rect windowRect;
    GetWindowBounds(this->GetRootWindow(), kWindowContentRgn, &windowRect);
    this->Position[0] = ((int)viewBounds.origin.x) + windowRect.left;
    this->Position[1] = ((int)viewBounds.origin.y) + windowRect.top;
    }
  
Yves Starreveld's avatar
Yves Starreveld committed
1039 1040 1041
  return this->Position;
}

Yves Starreveld's avatar
Yves Starreveld committed
1042
//--------------------------------------------------------------------------
Yves Starreveld's avatar
Yves Starreveld committed
1043 1044 1045 1046 1047 1048
// Change the window to fill the entire screen.
void vtkCarbonRenderWindow::SetFullScreen(int arg)
{
  int *temp;

  if (this->FullScreen == arg)
Yves Starreveld's avatar
Yves Starreveld committed
1049
    {
Yves Starreveld's avatar
Yves Starreveld committed
1050
    return;
Yves Starreveld's avatar
Yves Starreveld committed
1051
    }
Yves Starreveld's avatar
Yves Starreveld committed
1052 1053

  if (!this->Mapped)
Yves Starreveld's avatar
Yves Starreveld committed
1054
    {
Yves Starreveld's avatar
Yves Starreveld committed
1055 1056
    this->PrefFullScreen();
    return;
Yves Starreveld's avatar
Yves Starreveld committed
1057
    }
Yves Starreveld's avatar
Yves Starreveld committed
1058 1059 1060 1061

  // set the mode
  this->FullScreen = arg;
  if (this->FullScreen <= 0)