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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 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 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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 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 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 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 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 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 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCocoaRenderWindow.mm
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#import <Cocoa/Cocoa.h>
#import "vtkCocoaMacOSXSDKCompatibility.h" // Needed to support old SDKs
#import "vtkCocoaRenderWindow.h"
#import "vtkRenderWindowInteractor.h"
#import "vtkCommand.h"
#import "vtkIdList.h"
#import "vtkObjectFactory.h"
#import "vtkRendererCollection.h"
#import "vtkCocoaGLView.h"
#import <vtksys/ios/sstream>
vtkStandardNewMacro(vtkCocoaRenderWindow);
//----------------------------------------------------------------------------
// This is a private class and an implementation detail, do not use it.
// For fullscreen, an NSWindow that captures key events even when borderless
@interface vtkCocoaFullScreenWindow : NSWindow
{
}
@end
@implementation vtkCocoaFullScreenWindow
- (BOOL)canBecomeKeyWindow
{
return YES;
}
@end
//----------------------------------------------------------------------------
// This is a private class and an implementation detail, do not use it.
// It manages the NSWindow of a "pure VTK application",
// as opposed to a regular Mac app that happens to use VTK.
//----------------------------------------------------------------------------
@interface vtkCocoaServer : NSObject
{
@private
vtkCocoaRenderWindow *_renWin;
}
// Designated initializer
- (id)initWithRenderWindow:(vtkCocoaRenderWindow *)inRenderWindow;
- (void)startObservations;
- (void)stopObservations;
@end
//----------------------------------------------------------------------------
@implementation vtkCocoaServer
//----------------------------------------------------------------------------
- (id)initWithRenderWindow:(vtkCocoaRenderWindow *)inRenderWindow
{
self = [super init];
if (self)
{
_renWin = inRenderWindow;
}
return self;
}
//----------------------------------------------------------------------------
- (void)startObservations
{
assert(_renWin);
int windowCreated = _renWin->GetWindowCreated();
NSWindow *win = reinterpret_cast<NSWindow *>(_renWin->GetRootWindow());
if (windowCreated && win)
{
// Receive notifications of this, and only this, window's closing.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification
object:win];
}
NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
int viewCreated = _renWin->GetViewCreated();
if (viewCreated && view)
{
// Receive notifications of this, and only this, view's frame changing.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(viewFrameDidChange:)
name:NSViewFrameDidChangeNotification
object:view];
}
}
//----------------------------------------------------------------------------
- (void)stopObservations
{
assert(_renWin);
int windowCreated = _renWin->GetWindowCreated();
NSWindow *win = reinterpret_cast<NSWindow *>(_renWin->GetRootWindow());
if (windowCreated && win)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:NSWindowWillCloseNotification
object:win];
}
NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
int viewCreated = _renWin->GetViewCreated();
if (viewCreated && view)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:NSViewFrameDidChangeNotification
object:view];
}
}
//----------------------------------------------------------------------------
- (void)windowWillClose:(NSNotification *)aNotification
{
// We should only get here if it was us that created the NSWindow.
assert(_renWin);
assert(_renWin->GetWindowCreated());
// We should only have observed our own NSWindow.
assert([aNotification object] == _renWin->GetRootWindow());
(void)aNotification;
// Stop observing because the window is closing.
[self stopObservations];
// The NSWindow is closing, so prevent anyone from accidentally using it.
_renWin->SetRootWindow(NULL);
// Tell interactor to stop the NSApplication's run loop
vtkRenderWindowInteractor *interactor = _renWin->GetInteractor();
if (interactor)
{
interactor->TerminateApp();
}
}
//----------------------------------------------------------------------------
- (void)viewFrameDidChange:(NSNotification *)aNotification
{
// We should only get here if it was us that created the NSView.
assert(_renWin);
assert(_renWin->GetViewCreated());
// We should only have observed our own NSView.
assert([aNotification object] == _renWin->GetWindowId());
(void)aNotification;
// Retrieve the Interactor.
vtkRenderWindowInteractor *interactor = _renWin->GetInteractor();
if (!interactor || !interactor->GetEnabled())
{
return;
}
// Get the NSView's new frame size.
NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
assert(view);
NSRect frameRect = [view frame];
int width = (int)round(NSWidth(frameRect));
int height = (int)round(NSHeight(frameRect));
// Get the interactor's current cache of the size.
int size[2];
interactor->GetSize(size);
if (width != size[0] || height != size[1])
{
// Send ConfigureEvent from the Interactor.
interactor->UpdateSize(width, height);
interactor->InvokeEvent(vtkCommand::ConfigureEvent, NULL);
}
}
@end
//----------------------------------------------------------------------------
vtkCocoaRenderWindow::vtkCocoaRenderWindow()
{
// First, create the cocoa objects manager. The dictionary is empty so
// essentially all objects are initialized to NULL.
NSMutableDictionary *cocoaManager = [NSMutableDictionary dictionary];
// SetCocoaManager works like an Obj-C setter, so do like Obj-C and
// init the ivar to null first.
this->CocoaManager = NULL;
this->SetCocoaManager(reinterpret_cast<void *>(cocoaManager));
[cocoaManager self]; // prevent premature collection under GC.
this->WindowCreated = 0;
this->ViewCreated = 0;
this->SetWindowName("Visualization Toolkit - Cocoa");
this->CursorHidden = 0;
this->ForceMakeCurrent = 0;
this->Capabilities = 0;
this->OnScreenInitialized = 0;
this->OffScreenInitialized = 0;
}
//----------------------------------------------------------------------------
vtkCocoaRenderWindow::~vtkCocoaRenderWindow()
{
if (this->CursorHidden)
{
this->ShowCursor();
}
this->Finalize();
vtkRenderer *ren;
vtkCollectionSimpleIterator rit;
this->Renderers->InitTraversal(rit);
while ( (ren = this->Renderers->GetNextRenderer(rit)) )
{
ren->SetRenderWindow(NULL);
}
delete[] this->Capabilities;
this->Capabilities = 0;
this->SetContextId(NULL);
this->SetPixelFormat(NULL);
this->SetCocoaServer(NULL);
this->SetRootWindow(NULL);
this->SetWindowId(NULL);
this->SetParentId(NULL);
// Release the cocoa object manager.
this->SetCocoaManager(NULL);
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::Finalize()
{
if(this->OffScreenInitialized)
{
this->OffScreenInitialized = 0;
this->DestroyOffScreenWindow();
}
if(this->OnScreenInitialized)
{
this->OnScreenInitialized = 0;
this->DestroyWindow();
}
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::DestroyWindow()
{
// finish OpenGL rendering
if (this->OwnContext && this->GetContextId())
{
this->MakeCurrent();
// now delete all textures
glDisable(GL_TEXTURE_2D);
for (vtkIdType i = 1; i < this->TextureResourceIds->GetNumberOfIds(); i++)
{
GLuint txId = (GLuint) this->TextureResourceIds->GetId(i);
#ifdef GL_VERSION_1_1
if (glIsTexture(txId))
{
glDeleteTextures(1, &txId);
}
#else
if (glIsList(txId))
{
glDeleteLists(txId,1);
}
#endif
}
// 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);
(ren = this->Renderers->GetNextRenderer(rsit));)
{
ren->SetRenderWindow(NULL);
ren->SetRenderWindow(this);
}
}
this->SetContextId(NULL);
this->SetPixelFormat(NULL);
vtkCocoaServer *server = (vtkCocoaServer *)this->GetCocoaServer();
[server stopObservations];
this->SetCocoaServer(NULL);
// If we created it, close the NSWindow.
if (this->WindowCreated)
{
NSWindow *window = (NSWindow*)this->GetRootWindow();
[window close];
}
this->SetWindowId(NULL);
this->SetParentId(NULL);
this->SetRootWindow(NULL);
this->WindowCreated = 0;
this->ViewCreated = 0;
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetWindowName( const char * _arg )
{
vtkWindow::SetWindowName(_arg);
if (this->GetRootWindow())
{
NSString *winTitleStr = [NSString stringWithUTF8String:_arg];
[(NSWindow*)this->GetRootWindow() setTitle:winTitleStr];
}
}
//----------------------------------------------------------------------------
bool vtkCocoaRenderWindow::InitializeFromCurrentContext()
{
NSOpenGLContext *currentContext = [NSOpenGLContext currentContext];
if (currentContext != NULL)
{
NSView *currentView = [currentContext view];
if (currentView != NULL)
{
NSWindow *window = [currentView window];
this->SetWindowId(currentView);
this->SetRootWindow(window);
this->SetContextId((void*)currentContext);
this->OpenGLInit();
this->OwnContext = 0;
return true;
}
}
return false;
}
//----------------------------------------------------------------------------
int vtkCocoaRenderWindow::GetEventPending()
{
return 0;
}
//----------------------------------------------------------------------------
// Initialize the rendering process.
void vtkCocoaRenderWindow::Start()
{
this->Initialize();
// set the current window
this->MakeCurrent();
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::MakeCurrent()
{
if (this->GetContextId())
{
[(NSOpenGLContext*)this->GetContextId() makeCurrentContext];
}
}
// ----------------------------------------------------------------------------
// Description:
// Tells if this window is the current OpenGL context for the calling thread.
bool vtkCocoaRenderWindow::IsCurrent()
{
bool result=false;
if(this->GetContextId()!=0)
{
result=static_cast<NSOpenGLContext *>(this->GetContextId())==
[NSOpenGLContext currentContext];
}
return result;
}
//----------------------------------------------------------------------------
bool vtkCocoaRenderWindow::IsDrawable()
{
// you must initialize it first
// else it always evaluates false
this->Initialize();
// first check that window is valid
NSView *theView = (NSView*)this->GetWindowId();
bool win =[[theView window] windowNumber]>0;
// then check that the drawable is valid
NSOpenGLContext *context = (NSOpenGLContext *)this->GetContextId();
bool ok = [context view] != nil;
return win && ok;
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::UpdateContext()
{
if (this->GetContextId())
{
[(NSOpenGLContext*)this->GetContextId() update];
}
}
//----------------------------------------------------------------------------
const char* vtkCocoaRenderWindow::ReportCapabilities()
{
this->MakeCurrent();
const char* glVendor = (const char*) glGetString(GL_VENDOR);
const char* glRenderer = (const char*) glGetString(GL_RENDERER);
const char* glVersion = (const char*) glGetString(GL_VERSION);
const char* glExtensions = (const char*) glGetString(GL_EXTENSIONS);
vtksys_ios::ostringstream strm;
strm << "OpenGL vendor string: " << glVendor
<< "\nOpenGL renderer string: " << glRenderer
<< "\nOpenGL version string: " << glVersion
<< "\nOpenGL extensions: " << glExtensions << endl;
// Obtain the OpenGL context in order to keep track of the current screen.
NSOpenGLContext* context = (NSOpenGLContext*)this->GetContextId();
GLint currentScreen = [context currentVirtualScreen];
// The NSOpenGLPixelFormat can only be queried for one particular
// attribute at a time. Just make repeated queries to get the
// pertinent settings.
NSOpenGLPixelFormat* pixelFormat = (NSOpenGLPixelFormat*)this->GetPixelFormat();
strm << "PixelFormat Descriptor:" << endl;
GLint pfd = 0;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAColorSize forVirtualScreen: currentScreen];
strm << " colorSize: " << pfd << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAAlphaSize forVirtualScreen: currentScreen];
strm << " alphaSize: " << pfd << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAStencilSize forVirtualScreen: currentScreen];
strm << " stencilSize: " << pfd << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFADepthSize forVirtualScreen: currentScreen];
strm << " depthSize: " << pfd << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAAccumSize forVirtualScreen: currentScreen];
strm << " accumSize: " << pfd << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFADoubleBuffer forVirtualScreen: currentScreen];
strm << " double buffer: " << (pfd == 0 ? "No" : "Yes") << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAStereo forVirtualScreen: currentScreen];
strm << " stereo: " << (pfd == 0 ? "No" : "Yes") << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAStencilSize forVirtualScreen: currentScreen];
strm << " stencil: " << pfd << endl;
[pixelFormat getValues: &pfd forAttribute: NSOpenGLPFAAccelerated forVirtualScreen: currentScreen];
strm << " hardware acceleration:: " << (pfd == 0 ? "No" : "Yes") << endl;
delete[] this->Capabilities;
size_t len = strm.str().length() + 1;
this->Capabilities = new char[len];
strlcpy(this->Capabilities, strm.str().c_str(), len);
return this->Capabilities;
}
//----------------------------------------------------------------------------
int vtkCocoaRenderWindow::SupportsOpenGL()
{
this->MakeCurrent();
if (!this->GetContextId() || !this->GetPixelFormat())
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
int vtkCocoaRenderWindow::IsDirect()
{
this->MakeCurrent();
if (!this->GetContextId() || !this->GetPixelFormat())
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetSize(int* a)
{
this->SetSize( a[0], a[1] );
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetSize(int x, int y)
{
static int resizing = 0;
if ((this->Size[0] != x) || (this->Size[1] != y) || (this->GetParentId()))
{
this->Modified();
this->Size[0] = x;
this->Size[1] = y;
if (this->GetParentId() && this->GetWindowId() && this->Mapped)
{
// Set the NSView size, not the window size.
if (!resizing)
{
resizing = 1;
NSView *theView = (NSView*)this->GetWindowId();
NSRect viewRect = [theView frame];
CGFloat oldHeight = NSHeight(viewRect);
CGFloat height = (CGFloat)y;
CGFloat width = (CGFloat)x;
CGFloat xpos = NSMinX(viewRect);
CGFloat ypos = NSMinY(viewRect) - (height - oldHeight);
NSRect theRect = NSMakeRect(xpos, ypos, width, height);
[theView setFrame:theRect];
[theView setNeedsDisplay:YES];
resizing = 0;
}
}
else if (this->GetRootWindow() && this->Mapped)
{
if (!resizing)
{
resizing = 1;
NSSize theSize = NSMakeSize((CGFloat)x, (CGFloat)y);
[(NSWindow*)this->GetRootWindow() setContentSize:theSize];
resizing = 0;
}
}
}
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetForceMakeCurrent()
{
this->ForceMakeCurrent = 1;
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetPosition(int* a)
{
this->SetPosition( a[0], a[1] );
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetPosition(int x, int y)
{
static int resizing = 0;
if ((this->Position[0] != x) || (this->Position[1] != y)
|| (this->GetParentId()))
{
this->Modified();
this->Position[0] = x;
this->Position[1] = y;
if (this->GetParentId() && this->GetWindowId() && this->Mapped)
{
// Set the NSView position relative to the parent
if (!resizing)
{
resizing = 1;
NSRect parentRect = [(NSView*)this->GetParentId() frame];
NSView *theView = (NSView*)this->GetWindowId();
NSRect viewRect = [theView frame];
CGFloat parentHeight = NSHeight(parentRect);
CGFloat height = NSHeight(viewRect);
CGFloat xpos = (CGFloat)x;
CGFloat ypos = parentHeight - height - (CGFloat)y;
NSPoint origin = NSMakePoint(xpos,ypos);
[theView setFrameOrigin:origin];
[theView setNeedsDisplay:YES];
resizing = 0;
}
}
else if (this->GetRootWindow() && this->Mapped)
{
if (!resizing)
{
resizing = 1;
NSPoint origin = NSMakePoint((CGFloat)x, (CGFloat)y);
[(NSWindow*)this->GetRootWindow() setFrameOrigin:origin];
resizing = 0;
}
}
}
}
//----------------------------------------------------------------------------
// End the rendering process and display the image.
void vtkCocoaRenderWindow::Frame()
{
this->MakeCurrent();
if (!this->AbortRender && this->DoubleBuffer && this->SwapBuffers)
{
[(NSOpenGLContext*)this->GetContextId() flushBuffer];
}
else
{
glFlush();
}
}
//----------------------------------------------------------------------------
// Update system if needed due to stereo rendering.
void vtkCocoaRenderWindow::StereoUpdate()
{
// if stereo is on and it wasn't before
if (this->StereoRender && (!this->StereoStatus))
{
switch (this->StereoType)
{
case VTK_STEREO_CRYSTAL_EYES:
this->StereoStatus = 1;
break;
case VTK_STEREO_RED_BLUE:
this->StereoStatus = 1;
break;
case VTK_STEREO_ANAGLYPH:
this->StereoStatus = 1;
break;
case VTK_STEREO_DRESDEN:
this->StereoStatus = 1;
break;
case VTK_STEREO_INTERLACED:
this->StereoStatus = 1;
break;
case VTK_STEREO_CHECKERBOARD:
this->StereoStatus = 1;
break;
case VTK_STEREO_SPLITVIEWPORT_HORIZONTAL:
this->StereoStatus = 1;
break;
}
}
else if ((!this->StereoRender) && this->StereoStatus)
{
switch (this->StereoType)
{
case VTK_STEREO_CRYSTAL_EYES:
this->StereoStatus = 0;
break;
case VTK_STEREO_RED_BLUE:
this->StereoStatus = 0;
break;
case VTK_STEREO_ANAGLYPH:
this->StereoStatus = 0;
break;
case VTK_STEREO_DRESDEN:
this->StereoStatus = 0;
break;
case VTK_STEREO_INTERLACED:
this->StereoStatus = 0;
break;
case VTK_STEREO_CHECKERBOARD:
this->StereoStatus = 0;
break;
case VTK_STEREO_SPLITVIEWPORT_HORIZONTAL:
this->StereoStatus = 0;
break;
}
}
}
//----------------------------------------------------------------------------
// Specify various window parameters.
void vtkCocoaRenderWindow::WindowConfigure()
{
// this is all handled by the desiredVisualInfo method
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetupPixelFormat(void*, void*, int, int, int)
{
vtkErrorMacro(<< "vtkCocoaRenderWindow::SetupPixelFormat - IMPLEMENT");
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetupPalette(void*)
{
vtkErrorMacro(<< "vtkCocoaRenderWindow::SetupPalette - IMPLEMENT");
}
//----------------------------------------------------------------------------
// Initialize the window for rendering.
void vtkCocoaRenderWindow::CreateAWindow()
{
static unsigned count = 1;
// As vtk is both crossplatform and a library, we don't know if it is being
// used in a 'regular Cocoa application' or as a 'pure vtk application'.
// By the former I mean a regular Cocoa application that happens to have
// a vtkCocoaGLView, by the latter I mean an application that only uses
// vtk APIs (which happen to use Cocoa as an implementation detail).
// Specifically, we can't know if NSApplicationMain() was ever called
// (which is usually done in main()), nor whether the NSApplication exists.
//
// So here we call +sharedApplication which will create the NSApplication
// if it does not exist. If it does exist, this does nothing.
// We are not actually interested in the return value.
// This call is intentionally delayed until this CreateAWindow call
// to prevent Cocoa-window related stuff from happening in scenarios
// where vtkRenderWindows are created but never shown.
(void)[NSApplication sharedApplication];
// create an NSWindow only if neither an NSView nor an NSWindow have
// been specified already. This is the case for a 'pure vtk application'.
// If you are using vtk in a 'regular Mac application' you should call
// SetRootWindow() and SetWindowId() so that a window is not created here.
if (!this->GetRootWindow() && !this->GetWindowId() && !this->GetParentId())
{
NSWindow* theWindow = nil;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
NSScreen *screen = [NSScreen mainScreen];
if (this->FullScreen && screen)
{
NSRect ctRect = [screen frame];
this->Size[0] = (int)round(NSWidth(ctRect));
this->Size[1] = (int)round(NSHeight(ctRect));
theWindow = [[vtkCocoaFullScreenWindow alloc]
initWithContentRect:ctRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
// This will hide the menu and the dock
[theWindow setLevel:NSMainMenuWindowLevel+1];
// This will show the menu and the dock
//[theWindow setLevel:NSFloatingWindowLevel];
}
else
#endif
{
if ((this->Size[0]+this->Size[1]) == 0)
{
this->Size[0] = 300;
this->Size[1] = 300;
}
if ((this->Position[0]+this->Position[1]) == 0)
{
this->Position[0] = 50;
this->Position[1] = 50;
}
NSRect ctRect = NSMakeRect((CGFloat)this->Position[0],
(CGFloat)this->Position[1],
(CGFloat)this->Size[0],
(CGFloat)this->Size[1]);
theWindow = [[NSWindow alloc]
initWithContentRect:ctRect
styleMask:NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
}
if (!theWindow)
{
vtkErrorMacro("Could not create window, serious error!");
return;
}
this->SetRootWindow(theWindow);
this->WindowCreated = 1;
// makeKeyAndOrderFront: will show the window
// we don't want this if offscreen was requested
if(!this->OffScreenRendering)
{
[theWindow makeKeyAndOrderFront:nil];
[theWindow setAcceptsMouseMovedEvents:YES];
}
}
// create a view if one has not been specified
if (!this->GetWindowId())
{
if (this->GetParentId())
{
NSView *parent = (NSView*)this->GetParentId();
NSRect parentRect = [parent frame];
CGFloat parentHeight = NSHeight(parentRect);
CGFloat parentWidth = NSWidth(parentRect);
CGFloat width = (CGFloat)this->Size[0];
CGFloat height = (CGFloat)this->Size[1];
CGFloat x = (CGFloat)this->Position[0];
CGFloat y = parentHeight - height - (CGFloat)this->Position[1];
// A whole bunch of sanity checks: frame must be inside parent
if (x > parentWidth - 1) { x = parentWidth - 1; };
if (y > parentHeight - 1) { y = parentHeight - 1; };
if (x < 0.0) { x = 0.0; }
if (y < 0.0) { y = 0.0; }
if (x + width > parentWidth) { width = parentWidth - x; }
if (y + height > parentWidth) { height = parentHeight - y; }
// Don't use vtkCocoaGLView, because if we are in Tk (which is what
// SetParentId() was added for) then the Tk superview handles the events.
NSRect glRect = NSMakeRect(x, y, width, height);
NSView *glView = [[NSView alloc] initWithFrame:glRect];
[parent addSubview:glView];
this->SetWindowId(glView);
this->ViewCreated = 1;
#if !VTK_OBJC_IS_ARC
[glView release];
#endif
}
else
{
NSRect glRect = NSMakeRect(0.0, 0.0,
(CGFloat)this->Size[0],
(CGFloat)this->Size[1]);
// Create a vtkCocoaGLView.
vtkCocoaGLView *glView = [[vtkCocoaGLView alloc] initWithFrame:glRect];
[(NSWindow*)this->GetRootWindow() setContentView:glView];
this->SetWindowId(glView);
this->ViewCreated = 1;
[glView setVTKRenderWindow:this];
#if !VTK_OBJC_IS_ARC
[glView release];
#endif
}
}
this->CreateGLContext();
// Change the window title, but only if it was created by vtk
if (this->WindowCreated)
{
NSString *winName = [NSString stringWithFormat:@"Visualization Toolkit - Cocoa #%u", count++];
this->SetWindowName([winName cStringUsingEncoding:NSASCIIStringEncoding]);
}
// the error "invalid drawable" in the console from this call can appear
// but only early in the app's lifetime (ie sometime during launch)
// IMPORTANT: this is necessary to update the context here in case of
// hardware offscreen rendering
NSOpenGLContext* context = (NSOpenGLContext*)this->GetContextId();
[context setView:(NSView*)this->GetWindowId()];
[context update];
this->MakeCurrent();
// wipe out any existing display lists
vtkRenderer *renderer = NULL;
vtkCollectionSimpleIterator rsit;
for ( this->Renderers->InitTraversal(rsit);
(renderer = this->Renderers->GetNextRenderer(rsit));)
{
renderer->SetRenderWindow(0);
renderer->SetRenderWindow(this);
}
this->OpenGLInit();
this->Mapped = 1;
// Now that the NSView and NSWindow exist, the vtkCocoaServer can start its observations.
vtkCocoaServer *server = [[vtkCocoaServer alloc] initWithRenderWindow:this];
this->SetCocoaServer(reinterpret_cast<void *>(server));
[server startObservations];
#if !VTK_OBJC_IS_ARC
[server release];
#endif
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::CreateGLContext()
{
// keep trying to get different pixelFormats until successful
NSOpenGLPixelFormat *pixelFormat = nil;
while (pixelFormat == nil)
{
int i = 0;
NSOpenGLPixelFormatAttribute attribs[20];
attribs[i++] = NSOpenGLPFAAccelerated;
attribs[i++] = NSOpenGLPFADepthSize;
attribs[i++] = (NSOpenGLPixelFormatAttribute)32;
if (this->MultiSamples != 0)
{
attribs[i++] = NSOpenGLPFASampleBuffers;
attribs[i++] = (NSOpenGLPixelFormatAttribute)1;
attribs[i++] = NSOpenGLPFASamples;
attribs[i++] = (NSOpenGLPixelFormatAttribute)(this->MultiSamples);
attribs[i++] = NSOpenGLPFAMultisample;
}
if (this->DoubleBuffer != 0)
{
attribs[i++] = NSOpenGLPFADoubleBuffer;
}
attribs[i++] = (NSOpenGLPixelFormatAttribute)0;
// make sure that size of array was not exceeded
assert(sizeof(NSOpenGLPixelFormatAttribute)*i < sizeof(attribs));
pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (pixelFormat == nil)
{
if (this->MultiSamples == 0)
{
// after trying with no multisamples, we are done
break;
}
else if (this->MultiSamples < 4)
{
// next time try with no multisamples
this->MultiSamples = 0;
}
else
{
this->MultiSamples /= 2;
}
}
}
NSOpenGLContext *context = [[NSOpenGLContext alloc]
initWithFormat:pixelFormat
shareContext:nil];
// This syncs the OpenGL context to the VBL to prevent tearing
GLint one = 1;
[context setValues:&one forParameter:NSOpenGLCPSwapInterval];
this->SetPixelFormat((void*)pixelFormat);
this->SetContextId((void*)context);
[pixelFormat self]; // prevent premature collection under GC.
[context self]; // prevent premature collection under GC.
#if !VTK_OBJC_IS_ARC
[pixelFormat release];
[context release];
#endif
}
//----------------------------------------------------------------------------
// Initialize the rendering window.
void vtkCocoaRenderWindow::Initialize ()
{
if(this->OffScreenRendering)
{
// destroy on screen
if(this->OnScreenInitialized)
{
this->DestroyWindow();
this->OnScreenInitialized = 0;
}
// create off screen
if(!this->OffScreenInitialized)
{
int width=((this->Size[0]>0) ? this->Size[0] : 300);
int height=((this->Size[1]>0) ? this->Size[1] : 300);
if(!this->CreateHardwareOffScreenWindow(width,height))
{
// no other offscreen mode available, do on screen rendering
this->CreateAWindow();
}
this->OffScreenInitialized = 1;
}
}
else
{
// destroy off screen
if(this->OffScreenInitialized)
{
this->DestroyOffScreenWindow();
}
// create on screen
if(!this->OnScreenInitialized)
{
this->OnScreenInitialized = 1;
this->CreateAWindow();
}
}
if((this->OnScreenInitialized) && (this->Mapped))
{
// the error "invalid drawable" in the console from this call can appear
// but only early in the app's lifetime (ie sometime during launch)
// IMPORTANT: this is necessary to update the context here in case of
// onscreen rendering
NSOpenGLContext* context = (NSOpenGLContext*)this->GetContextId();
[context setView:(NSView*)this->GetWindowId()];
[context update];
}
}
//-----------------------------------------------------------------------------
void vtkCocoaRenderWindow::DestroyOffScreenWindow()
{
if(this->OffScreenUseFrameBuffer)
{
this->DestroyHardwareOffScreenWindow();
}
else
{
// on screen
this->DestroyWindow();
}
}
//----------------------------------------------------------------------------
// Get the current size of the window.
int *vtkCocoaRenderWindow::GetSize()
{
// if we aren't mapped then just return the ivar
if (!this->Mapped)
{
return this->Superclass::GetSize();
}
// We want to return the size of 'the window'. But the term 'window'
// is overloaded. It's really the NSView that vtk draws into, so we
// return its size.
NSView* view = (NSView*)this->GetWindowId();
if (view)
{
NSRect frameRect = [view frame];
this->Size[0] = (int)round(NSWidth(frameRect));
this->Size[1] = (int)round(NSHeight(frameRect));
}
return this->Superclass::GetSize();
}
//----------------------------------------------------------------------------
// Get the current size of the screen in pixels.
int *vtkCocoaRenderWindow::GetScreenSize()
{
NSOpenGLContext* context = (NSOpenGLContext*)this->GetContextId();
GLint currentScreen = [context currentVirtualScreen];
NSScreen* screen = [[NSScreen screens] objectAtIndex:currentScreen];
NSRect screenRect = [screen frame];
this->Size[0] = (int)round(NSWidth(screenRect));
this->Size[1] = (int)round(NSHeight(screenRect));
return this->Size;
}
//----------------------------------------------------------------------------
// Get the position in screen coordinates of the window.
int *vtkCocoaRenderWindow::GetPosition()
{
// if we aren't mapped then just return the ivar
if (!this->Mapped)
{
return this->Position;
}
if (this->GetParentId() && this->GetWindowId())
{
// Get display position of the NSView within its parent
NSRect parentRect = [(NSView*)this->GetParentId() frame];
NSRect viewFrameRect = [(NSView*)this->GetWindowId() frame];
this->Position[0] = int(round(NSMinX(viewFrameRect)));
this->Position[1] = int(round((NSHeight(parentRect)
- NSHeight(viewFrameRect)
- NSMinY(viewFrameRect))));
}
else
{
// We want to return the position of 'the window'. But the term 'window'
// is overloaded. In this case, it's the position of the NSWindow itself
// on the screen that we return. We don't much care where the NSView is
// within the NSWindow.
NSWindow *window = (NSWindow*)this->GetRootWindow();
if (window)
{
NSRect winFrameRect = [window frame];
this->Position[0] = (int)NSMinX(winFrameRect);
this->Position[1] = (int)NSMinY(winFrameRect);
}
}
return this->Position;
}
//----------------------------------------------------------------------------
// Change the window to fill the entire screen.
void vtkCocoaRenderWindow::SetFullScreen(int arg)
{
if (this->FullScreen == arg)
{
return;
}
if (!this->Mapped)
{
this->FullScreen = arg;
return;
}
// set the mode
this->FullScreen = arg;
if (this->FullScreen <= 0)
{
this->Position[0] = this->OldScreen[0];
this->Position[1] = this->OldScreen[1];
this->Size[0] = this->OldScreen[2];
this->Size[1] = this->OldScreen[3];
this->Borders = this->OldScreen[4];
}
else
{
// if window already up get its values
if (this->GetRootWindow())
{
int* pos = this->GetPosition();
this->OldScreen[0] = pos[0];
this->OldScreen[1] = pos[1];
this->OldScreen[4] = this->Borders;
this->PrefFullScreen();
}
}
// remap the window
this->WindowRemap();
this->Modified();
}
//----------------------------------------------------------------------------
//
// Set the variable that indicates that we want a stereo capable window
// be created. This method can only be called before a window is realized.
//
void vtkCocoaRenderWindow::SetStereoCapableWindow(int capable)
{
if (this->GetContextId() == 0)
{
vtkRenderWindow::SetStereoCapableWindow(capable);
}
else
{
vtkWarningMacro(<< "Requesting a StereoCapableWindow must be performed "
<< "before the window is realized, i.e. before a render.");
}
}
//----------------------------------------------------------------------------
// Set the preferred window size to full screen.
void vtkCocoaRenderWindow::PrefFullScreen()
{
int *size = this->GetScreenSize();
vtkWarningMacro(<< "Can only set FullScreen before showing window: "
<< size[0] << 'x' << size[1] << ".");
}
//----------------------------------------------------------------------------
// Remap the window.
void vtkCocoaRenderWindow::WindowRemap()
{
vtkWarningMacro(<< "Can't remap the window.");
// Acquire the display and capture the screen.
// Create the full-screen window.
// Add the context.
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "MultiSamples: " << this->MultiSamples << endl;
os << indent << "CocoaManager: " << this->GetCocoaManager() << endl;
os << indent << "RootWindow (NSWindow): " << this->GetRootWindow() << endl;
os << indent << "WindowId (NSView): " << this->GetWindowId() << endl;
os << indent << "ParentId: " << this->GetParentId() << endl;
os << indent << "ContextId: " << this->GetContextId() << endl;
os << indent << "PixelFormat: " << this->GetPixelFormat() << endl;
os << indent << "WindowCreated: " << (this->GetWindowCreated() ? "Yes" : "No") << endl;
os << indent << "ViewCreated: " << (this->GetViewCreated() ? "Yes" : "No") << endl;
}
//----------------------------------------------------------------------------
int vtkCocoaRenderWindow::GetDepthBufferSize()
{
if ( this->Mapped )
{
GLint size = 0;
glGetIntegerv( GL_DEPTH_BITS, &size );
return (int) size;
}
else
{
vtkDebugMacro(<< "Window is not mapped yet!" );
return 24;
}
}
//----------------------------------------------------------------------------
// Returns the NSWindow* associated with this vtkRenderWindow.
void *vtkCocoaRenderWindow::GetRootWindow()
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
return reinterpret_cast<void *>([manager objectForKey:@"RootWindow"]);
}
//----------------------------------------------------------------------------
// Sets the NSWindow* associated with this vtkRenderWindow.
void vtkCocoaRenderWindow::SetRootWindow(void *arg)
{
if (arg != NULL)
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager setObject:reinterpret_cast<id>(arg)
forKey:@"RootWindow"];
}
else
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager removeObjectForKey:@"RootWindow"];
}
}
//----------------------------------------------------------------------------
// Returns the NSView* associated with this vtkRenderWindow.
void *vtkCocoaRenderWindow::GetWindowId()
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
return reinterpret_cast<void *>([manager objectForKey:@"WindowId"]);
}
//----------------------------------------------------------------------------
// Sets the NSView* associated with this vtkRenderWindow.
void vtkCocoaRenderWindow::SetWindowId(void *arg)
{
if (arg != NULL)
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager setObject:reinterpret_cast<id>(arg)
forKey:@"WindowId"];
}
else
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager removeObjectForKey:@"WindowId"];
}
}
//----------------------------------------------------------------------------
// Returns the NSView* that is the parent of this vtkRenderWindow.
void *vtkCocoaRenderWindow::GetParentId()
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
return reinterpret_cast<void *>([manager objectForKey:@"ParentId"]);
}
//----------------------------------------------------------------------------
// Sets the NSView* that this vtkRenderWindow should use as a parent.
void vtkCocoaRenderWindow::SetParentId(void *arg)
{
if (arg != NULL)
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager setObject:reinterpret_cast<id>(arg)
forKey:@"ParentId"];
}
else
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager removeObjectForKey:@"ParentId"];
}
}
//----------------------------------------------------------------------------
// Sets the NSOpenGLContext* associated with this vtkRenderWindow.
void vtkCocoaRenderWindow::SetContextId(void *contextId)
{
if (contextId != NULL)
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager setObject:reinterpret_cast<id>(contextId)
forKey:@"ContextId"];
}
else
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager removeObjectForKey:@"ContextId"];
}
}
//----------------------------------------------------------------------------
// Returns the NSOpenGLContext* associated with this vtkRenderWindow.
void *vtkCocoaRenderWindow::GetContextId()
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
return reinterpret_cast<void *>([manager objectForKey:@"ContextId"]);
}
//----------------------------------------------------------------------------
// Sets the NSOpenGLPixelFormat* associated with this vtkRenderWindow.
void vtkCocoaRenderWindow::SetPixelFormat(void *pixelFormat)
{
if (pixelFormat != NULL)
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager setObject:reinterpret_cast<id>(pixelFormat)
forKey:@"PixelFormat"];
}
else
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager removeObjectForKey:@"PixelFormat"];
}
}
//----------------------------------------------------------------------------
// Returns the NSOpenGLPixelFormat* associated with this vtkRenderWindow.
void *vtkCocoaRenderWindow::GetPixelFormat()
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
return reinterpret_cast<void *>([manager objectForKey:@"PixelFormat"]);
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetCocoaServer(void *server)
{
if (server != NULL)
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager setObject:reinterpret_cast<vtkCocoaServer *>(server)
forKey:@"CocoaServer"];
}
else
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
[manager removeObjectForKey:@"CocoaServer"];
}
}
//----------------------------------------------------------------------------
void *vtkCocoaRenderWindow::GetCocoaServer()
{
NSMutableDictionary *manager =
reinterpret_cast<NSMutableDictionary *>(this->GetCocoaManager());
return reinterpret_cast<void *>([manager objectForKey:@"CocoaServer"]);
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetCocoaManager(void *manager)
{
NSMutableDictionary *currentCocoaManager =
reinterpret_cast<NSMutableDictionary *>(this->CocoaManager);
NSMutableDictionary *newCocoaManager =
reinterpret_cast<NSMutableDictionary *>(manager);
if (currentCocoaManager != newCocoaManager)
{
// Why not use Cocoa's retain and release? Without garbage collection
// (GC), the two are equivalent anyway because of 'toll free bridging',
// so no problem there. With GC, retain and release do nothing, but
// CFRetain and CFRelease still manipulate the internal reference count.
// We need that, since we are not using strong references (we don't want
// it collected out from under us!).
if (currentCocoaManager)
{
CFRelease(currentCocoaManager);
}
if (newCocoaManager)
{
this->CocoaManager = const_cast<void *>(CFRetain (newCocoaManager));
}
else
{
this->CocoaManager = NULL;
}
}
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetWindowInfo(char *info)
{
// The paramater is an ASCII string of a decimal number representing
// a pointer to the window. Convert it back to a pointer.
ptrdiff_t tmp = 0;
if (info)
{
(void)sscanf(info, "%tu", &tmp);
}
this->SetWindowId (reinterpret_cast<void *>(tmp));
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetParentInfo(char *info)
{
// The paramater is an ASCII string of a decimal number representing
// a pointer to the window. Convert it back to a pointer.
ptrdiff_t tmp = 0;
if (info)
{
(void)sscanf(info, "%tu", &tmp);
}
this->SetParentId (reinterpret_cast<void *>(tmp));
}
//----------------------------------------------------------------------------
void *vtkCocoaRenderWindow::GetCocoaManager()
{
return this->CocoaManager;
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::HideCursor()
{
if (this->CursorHidden)
{
return;
}
this->CursorHidden = 1;
[NSCursor hide];
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::ShowCursor()
{
if (!this->CursorHidden)
{
return;
}
this->CursorHidden = 0;
[NSCursor unhide];
}
// ---------------------------------------------------------------------------
int vtkCocoaRenderWindow::GetViewCreated()
{
return this->ViewCreated;
}
// ---------------------------------------------------------------------------
int vtkCocoaRenderWindow::GetWindowCreated()
{
return this->WindowCreated;
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetCursorPosition(int x, int y)
{
// The given coordinates are from the bottom left of the view.
NSPoint newViewPoint = NSMakePoint (x, y);
// Convert to screen coordinates.
NSView *view = (NSView *)this->GetWindowId();
if (view)
{
NSPoint screenPoint = [view convertPoint:newViewPoint toView:nil];
CGPoint newCursorPosition = NSPointToCGPoint(screenPoint);
// Move the cursor there.
(void)CGWarpMouseCursorPosition (newCursorPosition);
}
}
//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetCurrentCursor(int shape)
{
if (this->InvokeEvent(vtkCommand::CursorChangedEvent, &shape))
{
return;
}
this->Superclass::SetCurrentCursor(shape);
NSCursor* cursor = nil;
switch (shape)
{
case VTK_CURSOR_DEFAULT:
case VTK_CURSOR_ARROW:
cursor = [NSCursor arrowCursor];
break;
case VTK_CURSOR_SIZENS:
cursor = [NSCursor resizeUpDownCursor];
break;
case VTK_CURSOR_SIZEWE:
cursor = [NSCursor resizeLeftRightCursor];
break;
case VTK_CURSOR_HAND:
cursor = [NSCursor pointingHandCursor];
break;
case VTK_CURSOR_CROSSHAIR:
cursor = [NSCursor crosshairCursor];
break;
// NSCursor does not have cursors for these.
case VTK_CURSOR_SIZENE:
case VTK_CURSOR_SIZESW:
case VTK_CURSOR_SIZENW:
case VTK_CURSOR_SIZESE:
case VTK_CURSOR_SIZEALL:
cursor = [NSCursor arrowCursor];
break;
}
[cursor set];
}
|