- Timestamp:
- 05/14/09 17:34:15 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm
r9895 r10208 8 8 * Some code borrowed from the implementation of CocoaViewer, 9 9 * Created by Eric Wing on 11/12/06. and ported by Martin Lavery 7/06/07 10 * 11 * Other snippets are borrowed from the Cocoa-implementation of the SDL-lib 10 12 */ 11 13 … … 18 20 #include "DarwinUtils.h" 19 21 22 //#define DEBUG_OUT(s) std::cout << "GraphicsWindowCocoa :: " << s << std::endl; 23 24 #define DEBUG_OUT(s) ; 20 25 21 26 static bool s_quit_requested = false; 27 28 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 29 @interface NSApplication(NSAppleMenu) 30 - (void)setAppleMenu:(NSMenu *)menu; 31 @end 32 #endif 22 33 23 34 … … 188 199 189 200 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; 201 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification; 190 202 @end 191 203 … … 194 206 { 195 207 s_quit_requested = true; 196 // std::cout << "quit requested " << std::endl; 197 return NSTerminateNow; 208 DEBUG_OUT("quit requested "); 209 return NSTerminateCancel; 210 } 211 212 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 213 { 214 DEBUG_OUT("applicationDidFinishLaunching"); 198 215 } 199 216 … … 323 340 - (void) handleModifiers: (NSEvent*)theEvent 324 341 { 342 DEBUG_OUT("handling modifiers"); 343 344 if ((!_win) || (!_win->getEventQueue())) 345 return; // no event queue in place 346 325 347 unsigned int flags = [theEvent modifierFlags]; 326 348 … … 367 389 - (void) mouseMoved:(NSEvent*)theEvent 368 390 { 391 DEBUG_OUT("Mouse moved"); 369 392 NSPoint converted_point = [self getLocalPoint: theEvent]; 370 393 _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); … … 375 398 - (void) mouseDown:(NSEvent*)theEvent 376 399 { 400 DEBUG_OUT("Mouse down"); 377 401 // Because many Mac users have only a 1-button mouse, we should provide ways 378 402 // to access the button 2 and 3 actions of osgViewer. … … 403 427 - (void) mouseDragged:(NSEvent*)theEvent 404 428 { 429 if (!_win) return; 430 405 431 NSPoint converted_point = [self getLocalPoint: theEvent]; 406 432 _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); … … 441 467 - (void) rightMouseDragged:(NSEvent*)theEvent 442 468 { 443 469 if (!_win) return; 470 444 471 NSPoint converted_point = [self getLocalPoint: theEvent]; 445 472 _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); … … 473 500 - (void) otherMouseDragged:(NSEvent*)theEvent 474 501 { 502 if (!_win) return; 503 475 504 NSPoint converted_point = [self getLocalPoint: theEvent]; 476 505 _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); … … 524 553 - (void) doLeftMouseButtonDown:(NSEvent*)theEvent 525 554 { 555 if (!_win) return; 556 526 557 NSPoint converted_point = [self getLocalPoint: theEvent]; 527 558 … … 538 569 - (void) doLeftMouseButtonUp:(NSEvent*)theEvent 539 570 { 571 if (!_win) return; 572 540 573 NSPoint converted_point = [self getLocalPoint: theEvent]; 541 574 … … 546 579 - (void) doRightMouseButtonDown:(NSEvent*)theEvent 547 580 { 581 if (!_win) return; 582 548 583 NSPoint converted_point = [self getLocalPoint: theEvent]; 549 584 if([theEvent clickCount] == 1) … … 561 596 - (void) doRightMouseButtonUp:(NSEvent*)theEvent 562 597 { 598 if (!_win) return; 599 563 600 NSPoint converted_point = [self getLocalPoint: theEvent]; 564 601 _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 3); … … 567 604 - (void) doMiddleMouseButtonDown:(NSEvent*)theEvent 568 605 { 606 if (!_win) return; 607 569 608 NSPoint converted_point = [self getLocalPoint: theEvent]; 570 609 … … 581 620 - (void) doExtraMouseButtonDown:(NSEvent*)theEvent buttonNumber:(int)button_number 582 621 { 622 if (!_win) return; 623 583 624 NSPoint converted_point = [self getLocalPoint: theEvent]; 584 625 if([theEvent clickCount] == 1) … … 595 636 - (void) doMiddleMouseButtonUp:(NSEvent*)theEvent 596 637 { 638 if (!_win) return; 639 597 640 NSPoint converted_point = [self getLocalPoint: theEvent]; 598 641 _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 2); … … 602 645 - (void) doExtraMouseButtonUp:(NSEvent*)theEvent buttonNumber:(int)button_number 603 646 { 647 if (!_win) return; 648 604 649 NSPoint converted_point = [self getLocalPoint: theEvent]; 605 650 _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, button_number+1); … … 610 655 - (void) scrollWheel:(NSEvent*)theEvent 611 656 { 657 if (!_win) return; 658 612 659 // Unfortunately, it turns out mouseScroll2D doesn't actually do anything. 613 660 // The camera manipulators don't seem to implement any code that utilize the scroll values. … … 620 667 - (void)keyDown:(NSEvent *)theEvent 621 668 { 669 if (!_win) return; 670 622 671 NSString* chars = [theEvent charactersIgnoringModifiers]; 623 672 unsigned int keyCode = remapCocoaKey([chars characterAtIndex:0], ([theEvent modifierFlags] & NSFunctionKeyMask) ); … … 629 678 - (void)keyUp:(NSEvent *)theEvent 630 679 { 680 if (!_win) return; 681 631 682 NSString* chars = [theEvent charactersIgnoringModifiers]; 632 683 unsigned int keyCode = remapCocoaKey([chars characterAtIndex:0], ([theEvent modifierFlags] & NSFunctionKeyMask)); … … 644 695 -(void)handleTabletEvents:(NSEvent *)theEvent 645 696 { 697 if (!_win) return; 698 646 699 float pressure = [theEvent pressure]; 647 700 _win->getEventQueue()->penPressure(pressure); … … 654 707 - (void)tabletProximity:(NSEvent *)theEvent 655 708 { 709 if (!_win) return; 710 656 711 osgGA::GUIEventAdapter::TabletPointerType pt(osgGA::GUIEventAdapter::UNKNOWN); 657 712 switch ([theEvent pointingDeviceType]) { … … 845 900 NSRect rect = NSMakeRect(_traits->x + screenLeft, _traits->y + screenTop, _traits->width, _traits->height); 846 901 847 _window = [[GraphicsWindowCocoaWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: NO]; 848 849 if (!_window) { 850 osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create window" << std::endl; 851 return false; 852 } 853 854 rect = convertFromQuartzCoordinates(rect); 855 [_window setFrameOrigin: rect.origin]; 856 902 _ownsWindow = true; 903 904 // should we create a NSView only?? 905 WindowData* windowData = _traits->inheritedWindowData ? dynamic_cast<WindowData*>(_traits->inheritedWindowData.get()) : NULL; 906 if (windowData) 907 { 908 if (windowData->createOnlyView()) 909 _ownsWindow = false; 910 _checkForEvents = windowData->checkForEvents(); 911 912 } 913 914 915 osg::notify(osg::DEBUG_INFO) << "GraphicsWindowCocoa::realizeImplementation / ownsWindow: " << _ownsWindow << " checkForEvents: " << _checkForEvents << std::endl; 916 917 if (_ownsWindow) 918 { 919 _window = [[GraphicsWindowCocoaWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: NO]; 920 921 if (!_window) { 922 osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create window" << std::endl; 923 return false; 924 } 925 926 rect = convertFromQuartzCoordinates(rect); 927 [_window setFrameOrigin: rect.origin]; 928 } 929 857 930 NSOpenGLPixelFormatAttribute attr[32]; 858 931 int i = 0; … … 914 987 [theView setGraphicsWindowCocoa: this]; 915 988 [theView setOpenGLContext:_context]; 916 [_window setContentView: theView]; 917 918 setupNSWindow(_window); 919 920 [theView release]; 989 _view = theView; 990 osg::notify(osg::DEBUG_INFO) << "GraphicsWindowCocoa::realizeImplementation / view: " << theView << std::endl; 991 992 if (_ownsWindow) { 993 [_window setContentView: theView]; 994 setupNSWindow(_window); 995 [theView release]; 996 997 MenubarController::instance()->attachWindow( new CocoaWindowAdapter(this) ); 998 } 999 else 1000 { 1001 windowData->setCreatedNSView(theView); 1002 } 1003 921 1004 [pool release]; 922 1005 923 MenubarController::instance()->attachWindow( new CocoaWindowAdapter(this) );924 1006 925 1007 useCursor(_traits->useCursor); … … 951 1033 if (mbc) mbc->detachWindow(this); 952 1034 953 [_window close]; 954 [_window release]; 1035 if (_window) { 1036 [_window close]; 1037 [_window release]; 1038 } 1039 1040 if (_view) { 1041 [_view setGraphicsWindowCocoa: NULL]; 1042 } 1043 1044 _window = NULL; 1045 _view = NULL; 1046 955 1047 } 956 1048 … … 994 1086 void GraphicsWindowCocoa::checkEvents() 995 1087 { 1088 if (!_checkForEvents) 1089 return; 1090 996 1091 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 997 1092 … … 1005 1100 so it may not effectively make much of a difference. 1006 1101 */ 1007 NSEvent *event = [ [NSApplication sharedApplication]1102 NSEvent *event = [ NSApp 1008 1103 nextEventMatchingMask:NSAnyEventMask 1009 1104 untilDate:[NSDate distantPast] … … 1012 1107 if(!event) 1013 1108 break; 1014 [ [NSApplication sharedApplication]sendEvent: event];1109 [NSApp sendEvent: event]; 1015 1110 } 1016 1111 … … 1037 1132 bool GraphicsWindowCocoa::setWindowDecorationImplementation(bool flag) 1038 1133 { 1039 if (!_realized) return false; 1134 if (!_realized || !_ownsWindow) return false; 1135 1136 NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; 1040 1137 1041 1138 unsigned int style(NSBorderlessWindowMask); … … 1062 1159 } 1063 1160 1161 [localPool release]; 1162 1064 1163 return true; 1065 1164 } … … 1071 1170 void GraphicsWindowCocoa::grabFocus() 1072 1171 { 1073 [_window makeKeyAndOrderFront: nil]; 1172 if (_ownsWindow) 1173 [_window makeKeyAndOrderFront: nil]; 1074 1174 } 1075 1175 … … 1090 1190 void GraphicsWindowCocoa::resizedImplementation(int x, int y, int width, int height) 1091 1191 { 1092 std::cout << "resized implementation" << x << " " << y << " " << width << " " << height << std::endl;1192 DEBUG_OUT("resized implementation" << x << " " << y << " " << width << " " << height); 1093 1193 GraphicsContext::resizedImplementation(x, y, width, height); 1194 1195 NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; 1094 1196 1095 [_context update]; 1197 if (_context) 1198 [_context update]; 1096 1199 MenubarController::instance()->update(); 1097 1200 getEventQueue()->windowResize(x,y,width, height, getEventQueue()->getTime()); 1201 [localPool release]; 1098 1202 } 1099 1203 … … 1106 1210 bool GraphicsWindowCocoa::setWindowRectangleImplementation(int x, int y, int width, int height) 1107 1211 { 1212 if (!_ownsWindow) 1213 return false; 1214 1215 NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; 1216 1108 1217 DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); 1109 1218 int screenLeft(0), screenTop(0); … … 1120 1229 MenubarController::instance()->update(); 1121 1230 1231 [localPool release]; 1232 1122 1233 return true; 1123 1234 } … … 1155 1266 void GraphicsWindowCocoa::setWindowName (const std::string & name) 1156 1267 { 1268 if (_traits.valid()) _traits->windowName = name; 1269 1270 if (!_ownsWindow) 1271 return; 1272 1157 1273 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 1158 1274 … … 1201 1317 void GraphicsWindowCocoa::setCursor(MouseCursor mouseCursor) 1202 1318 { 1319 NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; 1320 1203 1321 switch (mouseCursor) 1204 1322 { … … 1223 1341 osg::notify(osg::INFO) << "GraphicsWindowCocoa::setCursor :: unsupported MouseCursor: " << mouseCursor << std::endl; 1224 1342 } 1343 1344 [localPool release]; 1225 1345 } 1226 1346 … … 1243 1363 GraphicsWindowCocoa::~GraphicsWindowCocoa() 1244 1364 { 1365 close(); 1245 1366 } 1246 1367 … … 1256 1377 1257 1378 CocoaWindowingSystemInterface() 1258 : DarwinWindowingSystemInterface() 1259 { 1260 localPool = [[NSAutoreleasePool alloc] init]; 1261 [[NSApplication sharedApplication] setDelegate: [[CocoaAppDelegate alloc] init] ]; 1379 : DarwinWindowingSystemInterface() 1380 1381 { 1382 } 1383 1384 void initAsStandaloneApplication() 1385 { 1386 static bool s_inited = false; 1387 if (s_inited) return; 1388 s_inited = true; 1389 1390 osg::notify(osg::INFO) << "CocoaWindowingSystemInterface::initAsStandaloneApplication " << std::endl; 1391 1392 ProcessSerialNumber psn; 1393 if (!GetCurrentProcess(&psn)) { 1394 TransformProcessType(&psn, kProcessTransformToForegroundApplication); 1395 SetFrontProcess(&psn); 1396 } 1397 1398 NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; 1399 1400 if (NSApp == nil) { 1401 [NSApplication sharedApplication]; 1402 } 1403 1404 [NSApp setDelegate: [[CocoaAppDelegate alloc] init] ]; 1405 1406 createApplicationMenus(); 1407 1408 [NSApp finishLaunching]; 1409 1410 [localPool release]; 1262 1411 } 1263 1412 1264 1413 virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) 1265 1414 { 1415 if (!traits->pbuffer) 1416 { 1417 GraphicsWindowCocoa::WindowData* windowData = traits->inheritedWindowData ? dynamic_cast<GraphicsWindowCocoa::WindowData*>(traits->inheritedWindowData.get()) : NULL; 1418 1419 if (!windowData || (windowData && windowData->poseAsStandaloneApp())) 1420 { 1421 initAsStandaloneApplication(); 1422 } 1423 } 1424 1266 1425 return createGraphicsContextImplementation<PixelBufferCocoa, GraphicsWindowCocoa>(traits); 1267 1426 } … … 1269 1428 virtual ~CocoaWindowingSystemInterface() 1270 1429 { 1271 [localPool release]; 1272 } 1273 1274 NSAutoreleasePool *localPool; 1430 } 1431 1432 private: 1433 NSString *getApplicationName(void) 1434 { 1435 NSDictionary *dict; 1436 NSString *appName = 0; 1437 1438 /* Determine the application name */ 1439 dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); 1440 if (dict) 1441 appName = [dict objectForKey: @"CFBundleName"]; 1442 1443 if (![appName length]) 1444 appName = [[NSProcessInfo processInfo] processName]; 1445 1446 return appName; 1447 } 1448 1449 void createApplicationMenus(void) 1450 { 1451 NSString *appName; 1452 NSString *title; 1453 NSMenu *appleMenu; 1454 NSMenuItem *menuItem; 1455 1456 /* Create the main menu bar */ 1457 [NSApp setMainMenu:[[NSMenu alloc] init]]; 1458 1459 /* Create the application menu */ 1460 appName = getApplicationName(); 1461 appleMenu = [[NSMenu alloc] initWithTitle:@""]; 1462 1463 /* Add menu items */ 1464 title = [@"About " stringByAppendingString:appName]; 1465 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; 1466 1467 [appleMenu addItem:[NSMenuItem separatorItem]]; 1468 1469 NSMenu* service_menu = [[NSMenu alloc] init]; 1470 NSMenuItem* service_menu_item = [[NSMenuItem alloc] initWithTitle:@"Services" action:nil keyEquivalent:@""]; 1471 [service_menu_item setSubmenu: service_menu]; 1472 [appleMenu addItem: service_menu_item]; 1473 [NSApp setServicesMenu: service_menu]; 1474 1475 [appleMenu addItem:[NSMenuItem separatorItem]]; 1476 1477 title = [@"Hide " stringByAppendingString:appName]; 1478 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/"h"]; 1479 1480 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""]; 1481 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; 1482 1483 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; 1484 1485 [appleMenu addItem:[NSMenuItem separatorItem]]; 1486 1487 title = [@"Quit " stringByAppendingString:appName]; 1488 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/"q"]; 1489 1490 /* Put menu into the menubar */ 1491 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; 1492 [menuItem setSubmenu:appleMenu]; 1493 [[NSApp mainMenu] addItem:menuItem]; 1494 [menuItem release]; 1495 1496 /* Tell the application object that this is now the application menu */ 1497 [NSApp setAppleMenu:appleMenu]; 1498 [appleMenu release]; 1499 1500 1501 } 1275 1502 1276 1503 }; 1277 1278 1504 1279 1505 }
