| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <iostream> |
|---|
| 15 | #include <osgViewer/api/Cocoa/PixelBufferCocoa> |
|---|
| 16 | #include <osgViewer/api/Cocoa/GraphicsWindowCocoa> |
|---|
| 17 | |
|---|
| 18 | #include <Cocoa/Cocoa.h> |
|---|
| 19 | |
|---|
| 20 | #include "DarwinUtils.h" |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #define DEBUG_OUT(s) ; |
|---|
| 25 | |
|---|
| 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 |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | class CocoaKeyboardMap { |
|---|
| 41 | |
|---|
| 42 | public: |
|---|
| 43 | CocoaKeyboardMap() |
|---|
| 44 | { |
|---|
| 45 | _keymap[27] = osgGA::GUIEventAdapter::KEY_Escape; |
|---|
| 46 | _keymap[13] = osgGA::GUIEventAdapter::KEY_Return; |
|---|
| 47 | _keymap[3] = osgGA::GUIEventAdapter::KEY_KP_Enter; |
|---|
| 48 | _keymap[9] = osgGA::GUIEventAdapter::KEY_Tab; |
|---|
| 49 | _keymap[32] = osgGA::GUIEventAdapter::KEY_Space; |
|---|
| 50 | _keymap[127] = osgGA::GUIEventAdapter::KEY_BackSpace; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | _keymap[NSHomeFunctionKey] = osgGA::GUIEventAdapter::KEY_Home; |
|---|
| 54 | _keymap[NSEndFunctionKey] = osgGA::GUIEventAdapter::KEY_End; |
|---|
| 55 | _keymap[NSPageUpFunctionKey] = osgGA::GUIEventAdapter::KEY_Page_Up; |
|---|
| 56 | _keymap[NSPageDownFunctionKey] = osgGA::GUIEventAdapter::KEY_Page_Down; |
|---|
| 57 | _keymap[NSLeftArrowFunctionKey] = osgGA::GUIEventAdapter::KEY_Left; |
|---|
| 58 | _keymap[NSRightArrowFunctionKey] = osgGA::GUIEventAdapter::KEY_Right; |
|---|
| 59 | _keymap[NSUpArrowFunctionKey] = osgGA::GUIEventAdapter::KEY_Up; |
|---|
| 60 | _keymap[NSDownArrowFunctionKey] = osgGA::GUIEventAdapter::KEY_Down; |
|---|
| 61 | |
|---|
| 62 | _keymap[NSDeleteFunctionKey] = osgGA::GUIEventAdapter::KEY_Delete; |
|---|
| 63 | |
|---|
| 64 | _keymap[NSF1FunctionKey] = osgGA::GUIEventAdapter::KEY_F1; |
|---|
| 65 | _keymap[NSF2FunctionKey] = osgGA::GUIEventAdapter::KEY_F2; |
|---|
| 66 | _keymap[NSF3FunctionKey] = osgGA::GUIEventAdapter::KEY_F3; |
|---|
| 67 | _keymap[NSF4FunctionKey] = osgGA::GUIEventAdapter::KEY_F4; |
|---|
| 68 | _keymap[NSF5FunctionKey] = osgGA::GUIEventAdapter::KEY_F5; |
|---|
| 69 | _keymap[NSF6FunctionKey] = osgGA::GUIEventAdapter::KEY_F6; |
|---|
| 70 | _keymap[NSF7FunctionKey] = osgGA::GUIEventAdapter::KEY_F7; |
|---|
| 71 | _keymap[NSF8FunctionKey] = osgGA::GUIEventAdapter::KEY_F8; |
|---|
| 72 | _keymap[NSF9FunctionKey] = osgGA::GUIEventAdapter::KEY_F9; |
|---|
| 73 | |
|---|
| 74 | _keymap[NSF10FunctionKey] = osgGA::GUIEventAdapter::KEY_F10; |
|---|
| 75 | _keymap[NSF11FunctionKey] = osgGA::GUIEventAdapter::KEY_F11; |
|---|
| 76 | _keymap[NSF12FunctionKey] = osgGA::GUIEventAdapter::KEY_F12; |
|---|
| 77 | _keymap[NSF13FunctionKey] = osgGA::GUIEventAdapter::KEY_F13; |
|---|
| 78 | _keymap[NSF14FunctionKey] = osgGA::GUIEventAdapter::KEY_F14; |
|---|
| 79 | _keymap[NSF15FunctionKey] = osgGA::GUIEventAdapter::KEY_F15; |
|---|
| 80 | _keymap[NSF16FunctionKey] = osgGA::GUIEventAdapter::KEY_F16; |
|---|
| 81 | _keymap[NSF17FunctionKey] = osgGA::GUIEventAdapter::KEY_F17; |
|---|
| 82 | _keymap[NSF18FunctionKey] = osgGA::GUIEventAdapter::KEY_F18; |
|---|
| 83 | _keymap[NSF19FunctionKey] = osgGA::GUIEventAdapter::KEY_F19; |
|---|
| 84 | |
|---|
| 85 | _keymap[NSF20FunctionKey] = osgGA::GUIEventAdapter::KEY_F20; |
|---|
| 86 | _keymap[NSF21FunctionKey] = osgGA::GUIEventAdapter::KEY_F21; |
|---|
| 87 | _keymap[NSF22FunctionKey] = osgGA::GUIEventAdapter::KEY_F22; |
|---|
| 88 | _keymap[NSF23FunctionKey] = osgGA::GUIEventAdapter::KEY_F23; |
|---|
| 89 | _keymap[NSF24FunctionKey] = osgGA::GUIEventAdapter::KEY_F24; |
|---|
| 90 | _keymap[NSF25FunctionKey] = osgGA::GUIEventAdapter::KEY_F25; |
|---|
| 91 | _keymap[NSF26FunctionKey] = osgGA::GUIEventAdapter::KEY_F26; |
|---|
| 92 | _keymap[NSF27FunctionKey] = osgGA::GUIEventAdapter::KEY_F27; |
|---|
| 93 | _keymap[NSF28FunctionKey] = osgGA::GUIEventAdapter::KEY_F28; |
|---|
| 94 | _keymap[NSF29FunctionKey] = osgGA::GUIEventAdapter::KEY_F29; |
|---|
| 95 | |
|---|
| 96 | _keymap[NSF30FunctionKey] = osgGA::GUIEventAdapter::KEY_F30; |
|---|
| 97 | _keymap[NSF31FunctionKey] = osgGA::GUIEventAdapter::KEY_F31; |
|---|
| 98 | _keymap[NSF32FunctionKey] = osgGA::GUIEventAdapter::KEY_F32; |
|---|
| 99 | _keymap[NSF33FunctionKey] = osgGA::GUIEventAdapter::KEY_F33; |
|---|
| 100 | _keymap[NSF34FunctionKey] = osgGA::GUIEventAdapter::KEY_F34; |
|---|
| 101 | _keymap[NSF35FunctionKey] = osgGA::GUIEventAdapter::KEY_F35; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | _keypadmap['='] = osgGA::GUIEventAdapter::KEY_KP_Equal; |
|---|
| 105 | _keypadmap['*'] = osgGA::GUIEventAdapter::KEY_KP_Multiply; |
|---|
| 106 | _keypadmap['+'] = osgGA::GUIEventAdapter::KEY_KP_Add; |
|---|
| 107 | _keypadmap['-'] = osgGA::GUIEventAdapter::KEY_KP_Subtract; |
|---|
| 108 | _keypadmap['.'] = osgGA::GUIEventAdapter::KEY_KP_Decimal; |
|---|
| 109 | _keypadmap['/'] = osgGA::GUIEventAdapter::KEY_KP_Divide; |
|---|
| 110 | |
|---|
| 111 | _keypadmap['0'] = osgGA::GUIEventAdapter::KEY_KP_0; |
|---|
| 112 | _keypadmap['1'] = osgGA::GUIEventAdapter::KEY_KP_1; |
|---|
| 113 | _keypadmap['2'] = osgGA::GUIEventAdapter::KEY_KP_2; |
|---|
| 114 | _keypadmap['3'] = osgGA::GUIEventAdapter::KEY_KP_3; |
|---|
| 115 | _keypadmap['4'] = osgGA::GUIEventAdapter::KEY_KP_4; |
|---|
| 116 | _keypadmap['5'] = osgGA::GUIEventAdapter::KEY_KP_5; |
|---|
| 117 | _keypadmap['6'] = osgGA::GUIEventAdapter::KEY_KP_6; |
|---|
| 118 | _keypadmap['7'] = osgGA::GUIEventAdapter::KEY_KP_7; |
|---|
| 119 | _keypadmap['8'] = osgGA::GUIEventAdapter::KEY_KP_8; |
|---|
| 120 | _keypadmap['9'] = osgGA::GUIEventAdapter::KEY_KP_9; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | ~CocoaKeyboardMap() { |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | unsigned int remapKey(unsigned int key, bool pressedOnKeypad = false) |
|---|
| 127 | { |
|---|
| 128 | if (pressedOnKeypad) { |
|---|
| 129 | KeyMap::iterator itr = _keypadmap.find(key); |
|---|
| 130 | if (itr == _keypadmap.end()) return key; |
|---|
| 131 | else return itr->second; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | KeyMap::iterator itr = _keymap.find(key); |
|---|
| 135 | if (itr == _keymap.end()) return key; |
|---|
| 136 | else return itr->second; |
|---|
| 137 | } |
|---|
| 138 | private: |
|---|
| 139 | typedef std::map<unsigned int, osgGA::GUIEventAdapter::KeySymbol> KeyMap; |
|---|
| 140 | KeyMap _keymap, _keypadmap; |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | static unsigned int remapCocoaKey(unsigned int key, bool pressedOnKeypad = false) |
|---|
| 148 | { |
|---|
| 149 | static CocoaKeyboardMap s_CocoaKeyboardMap; |
|---|
| 150 | return s_CocoaKeyboardMap.remapKey(key, pressedOnKeypad); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | std::ostream& operator<<(std::ostream& os, const NSRect& rect) |
|---|
| 155 | { |
|---|
| 156 | os << rect.origin.x << "/" << rect.origin.y << " " << rect.size.width << "x" << rect.size.height; |
|---|
| 157 | return os; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | static NSRect convertFromQuartzCoordinates(const NSRect& rect) |
|---|
| 168 | { |
|---|
| 169 | NSRect frame = [[[NSScreen screens] objectAtIndex: 0] frame]; |
|---|
| 170 | float y = frame.size.height - rect.origin.y - rect.size.height; |
|---|
| 171 | NSRect converted = NSMakeRect(rect.origin.x, y, rect.size.width, rect.size.height); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | return converted; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | static NSRect convertToQuartzCoordinates(const NSRect& rect) |
|---|
| 179 | { |
|---|
| 180 | NSRect frame = [[[NSScreen screens] objectAtIndex: 0] frame]; |
|---|
| 181 | |
|---|
| 182 | float y = frame.size.height - (rect.origin.y + rect.size.height); |
|---|
| 183 | NSRect converted = NSMakeRect(rect.origin.x, y, rect.size.width, rect.size.height); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | return converted; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | #pragma mark CocoaAppDelegate |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | @interface CocoaAppDelegate : NSObject |
|---|
| 197 | { |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; |
|---|
| 201 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification; |
|---|
| 202 | @end |
|---|
| 203 | |
|---|
| 204 | @implementation CocoaAppDelegate |
|---|
| 205 | - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender |
|---|
| 206 | { |
|---|
| 207 | s_quit_requested = true; |
|---|
| 208 | DEBUG_OUT("quit requested "); |
|---|
| 209 | return NSTerminateCancel; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
|---|
| 213 | { |
|---|
| 214 | DEBUG_OUT("applicationDidFinishLaunching"); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | @end |
|---|
| 218 | |
|---|
| 219 | #pragma mark GraphicsWindowCocoaWindow |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | @interface GraphicsWindowCocoaWindow : NSWindow |
|---|
| 226 | { |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | - (BOOL) canBecomeKeyWindow; |
|---|
| 230 | - (BOOL) canBecomeMainWindow; |
|---|
| 231 | |
|---|
| 232 | @end |
|---|
| 233 | |
|---|
| 234 | @implementation GraphicsWindowCocoaWindow |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | - (BOOL) canBecomeKeyWindow |
|---|
| 238 | { |
|---|
| 239 | return YES; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | - (BOOL) canBecomeMainWindow |
|---|
| 243 | { |
|---|
| 244 | return YES; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | @end |
|---|
| 248 | |
|---|
| 249 | #pragma mark GraphicsWindowCocoaGLView |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | @interface GraphicsWindowCocoaGLView : NSOpenGLView |
|---|
| 259 | { |
|---|
| 260 | @private |
|---|
| 261 | osgViewer::GraphicsWindowCocoa* _win; |
|---|
| 262 | BOOL _isUsingCtrlClick, _isUsingOptionClick; |
|---|
| 263 | unsigned int _cachedModifierFlags; |
|---|
| 264 | BOOL _handleTabletEvents; |
|---|
| 265 | |
|---|
| 266 | } |
|---|
| 267 | - (void)setGraphicsWindowCocoa: (osgViewer::GraphicsWindowCocoa*) win; |
|---|
| 268 | |
|---|
| 269 | - (void)keyDown:(NSEvent *)theEvent; |
|---|
| 270 | - (void)keyUp:(NSEvent *)theEvent; |
|---|
| 271 | - (void)flagsChanged:(NSEvent *)theEvent; |
|---|
| 272 | - (void) mouseMoved:(NSEvent*)theEvent; |
|---|
| 273 | - (void) mouseDown:(NSEvent*)theEvent; |
|---|
| 274 | - (void) mouseDragged:(NSEvent*)theEvent; |
|---|
| 275 | - (void) mouseUp:(NSEvent*)theEvent; |
|---|
| 276 | - (void) rightMouseDown:(NSEvent*)theEvent; |
|---|
| 277 | - (void) rightMouseDragged:(NSEvent*)theEvent; |
|---|
| 278 | - (void) rightMouseUp:(NSEvent*)theEvent; |
|---|
| 279 | - (void) otherMouseDown:(NSEvent*)theEvent; |
|---|
| 280 | - (void) otherMouseDragged:(NSEvent*)theEvent; |
|---|
| 281 | - (void) otherMouseUp:(NSEvent*)theEvent; |
|---|
| 282 | |
|---|
| 283 | - (NSPoint) getLocalPoint: (NSEvent*)theEvent; |
|---|
| 284 | - (void) handleModifiers: (NSEvent*)theEvent; |
|---|
| 285 | - (void) setIsUsingCtrlClick:(BOOL)is_using_ctrl_click; |
|---|
| 286 | - (BOOL) isUsingCtrlClick; |
|---|
| 287 | - (void) setIsUsingOptionClick:(BOOL)is_using_option_click; |
|---|
| 288 | - (BOOL) isUsingOptionClick; |
|---|
| 289 | |
|---|
| 290 | - (void) doLeftMouseButtonDown:(NSEvent*)theEvent; |
|---|
| 291 | - (void) doLeftMouseButtonUp:(NSEvent*)theEvent; |
|---|
| 292 | - (void) doRightMouseButtonDown:(NSEvent*)theEvent; |
|---|
| 293 | - (void) doRightMouseButtonUp:(NSEvent*)theEvent; |
|---|
| 294 | - (void) doMiddleMouseButtonDown:(NSEvent*)theEvent; |
|---|
| 295 | - (void) doExtraMouseButtonDown:(NSEvent*)theEvent buttonNumber:(int)button_number; |
|---|
| 296 | - (void) doMiddleMouseButtonUp:(NSEvent*)theEvent; |
|---|
| 297 | - (void) doExtraMouseButtonUp:(NSEvent*)theEvent buttonNumber:(int)button_number; |
|---|
| 298 | - (void) scrollWheel:(NSEvent*)theEvent; |
|---|
| 299 | |
|---|
| 300 | - (void)tabletPoint:(NSEvent *)theEvent; |
|---|
| 301 | - (void)tabletProximity:(NSEvent *)theEvent; |
|---|
| 302 | - (void)handleTabletEvents:(NSEvent*)theEvent; |
|---|
| 303 | |
|---|
| 304 | - (BOOL)acceptsFirstResponder; |
|---|
| 305 | - (BOOL)becomeFirstResponder; |
|---|
| 306 | - (BOOL)resignFirstResponder; |
|---|
| 307 | |
|---|
| 308 | @end |
|---|
| 309 | |
|---|
| 310 | @implementation GraphicsWindowCocoaGLView |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | -(void) setGraphicsWindowCocoa: (osgViewer::GraphicsWindowCocoa*) win |
|---|
| 314 | { |
|---|
| 315 | _win = win; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | - (BOOL)acceptsFirstResponder |
|---|
| 319 | { |
|---|
| 320 | return YES; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | - (BOOL)becomeFirstResponder |
|---|
| 324 | { |
|---|
| 325 | return YES; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | - (BOOL)resignFirstResponder |
|---|
| 329 | { |
|---|
| 330 | return YES; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | - (NSPoint) getLocalPoint: (NSEvent*)theEvent |
|---|
| 335 | { |
|---|
| 336 | return [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | - (void) handleModifiers: (NSEvent*)theEvent |
|---|
| 341 | { |
|---|
| 342 | DEBUG_OUT("handling modifiers"); |
|---|
| 343 | |
|---|
| 344 | if ((!_win) || (!_win->getEventQueue())) |
|---|
| 345 | return; |
|---|
| 346 | |
|---|
| 347 | unsigned int flags = [theEvent modifierFlags]; |
|---|
| 348 | |
|---|
| 349 | if (flags == _cachedModifierFlags) |
|---|
| 350 | return; |
|---|
| 351 | |
|---|
| 352 | const unsigned int masks[] = { |
|---|
| 353 | NSShiftKeyMask, |
|---|
| 354 | NSControlKeyMask, |
|---|
| 355 | NSAlternateKeyMask, |
|---|
| 356 | NSCommandKeyMask, |
|---|
| 357 | NSAlphaShiftKeyMask |
|---|
| 358 | }; |
|---|
| 359 | |
|---|
| 360 | const unsigned int keys[] = { |
|---|
| 361 | osgGA::GUIEventAdapter::KEY_Shift_L, |
|---|
| 362 | osgGA::GUIEventAdapter::KEY_Control_L, |
|---|
| 363 | osgGA::GUIEventAdapter::KEY_Alt_L, |
|---|
| 364 | osgGA::GUIEventAdapter::KEY_Super_L, |
|---|
| 365 | osgGA::GUIEventAdapter::KEY_Caps_Lock |
|---|
| 366 | }; |
|---|
| 367 | |
|---|
| 368 | for(unsigned int i = 0; i < 5; ++i) { |
|---|
| 369 | |
|---|
| 370 | if ((flags & masks[i]) && !(_cachedModifierFlags & masks[i])) |
|---|
| 371 | { |
|---|
| 372 | _win->getEventQueue()->keyPress(keys[i]); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | if (!(flags & masks[i]) && (_cachedModifierFlags & masks[i])) |
|---|
| 376 | { |
|---|
| 377 | _win->getEventQueue()->keyRelease(keys[i]); |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | _cachedModifierFlags = flags; |
|---|
| 382 | |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | - (void)flagsChanged:(NSEvent *)theEvent { |
|---|
| 386 | [self handleModifiers: theEvent]; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | - (void) mouseMoved:(NSEvent*)theEvent |
|---|
| 390 | { |
|---|
| 391 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 392 | DEBUG_OUT("Mouse moved" << converted_point.x << "/" << converted_point.y); |
|---|
| 393 | _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | - (void) mouseDown:(NSEvent*)theEvent |
|---|
| 399 | { |
|---|
| 400 | DEBUG_OUT("Mouse down"); |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | if([theEvent modifierFlags] & NSControlKeyMask) |
|---|
| 406 | { |
|---|
| 407 | [self setIsUsingCtrlClick:YES]; |
|---|
| 408 | [self doRightMouseButtonDown:theEvent]; |
|---|
| 409 | } |
|---|
| 410 | else if([theEvent modifierFlags] & NSAlternateKeyMask) |
|---|
| 411 | { |
|---|
| 412 | [self setIsUsingOptionClick:YES]; |
|---|
| 413 | [self doMiddleMouseButtonDown:theEvent]; |
|---|
| 414 | } |
|---|
| 415 | else |
|---|
| 416 | { |
|---|
| 417 | [self doLeftMouseButtonDown:theEvent]; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | if ([theEvent subtype] == NSTabletPointEventSubtype) { |
|---|
| 421 | _handleTabletEvents = true; |
|---|
| 422 | [self handleTabletEvents:theEvent]; |
|---|
| 423 | } |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | - (void) mouseDragged:(NSEvent*)theEvent |
|---|
| 428 | { |
|---|
| 429 | if (!_win) return; |
|---|
| 430 | |
|---|
| 431 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 432 | _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); |
|---|
| 433 | |
|---|
| 434 | if (_handleTabletEvents) |
|---|
| 435 | [self handleTabletEvents:theEvent]; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | - (void) mouseUp:(NSEvent*)theEvent |
|---|
| 440 | { |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | if([self isUsingCtrlClick] == YES) |
|---|
| 446 | { |
|---|
| 447 | [self setIsUsingCtrlClick:NO]; |
|---|
| 448 | [self doRightMouseButtonUp:theEvent]; |
|---|
| 449 | } |
|---|
| 450 | else if([self isUsingOptionClick] == YES) |
|---|
| 451 | { |
|---|
| 452 | [self setIsUsingOptionClick:NO]; |
|---|
| 453 | [self doMiddleMouseButtonUp:theEvent]; |
|---|
| 454 | } |
|---|
| 455 | else |
|---|
| 456 | { |
|---|
| 457 | [self doLeftMouseButtonUp:theEvent]; |
|---|
| 458 | } |
|---|
| 459 | _handleTabletEvents = false; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | - (void) rightMouseDown:(NSEvent*)theEvent |
|---|
| 463 | { |
|---|
| 464 | [self doRightMouseButtonDown:theEvent]; |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | - (void) rightMouseDragged:(NSEvent*)theEvent |
|---|
| 468 | { |
|---|
| 469 | if (!_win) return; |
|---|
| 470 | |
|---|
| 471 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 472 | _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | - (void) rightMouseUp:(NSEvent*)theEvent |
|---|
| 476 | { |
|---|
| 477 | [self doRightMouseButtonUp:theEvent]; |
|---|
| 478 | _handleTabletEvents = false; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | - (void) otherMouseDown:(NSEvent*)theEvent |
|---|
| 483 | { |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | if([theEvent buttonNumber] == 2) |
|---|
| 491 | { |
|---|
| 492 | [self doMiddleMouseButtonDown:theEvent]; |
|---|
| 493 | } |
|---|
| 494 | else |
|---|
| 495 | { |
|---|
| 496 | [self doExtraMouseButtonDown:theEvent buttonNumber:[theEvent buttonNumber]]; |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | - (void) otherMouseDragged:(NSEvent*)theEvent |
|---|
| 501 | { |
|---|
| 502 | if (!_win) return; |
|---|
| 503 | |
|---|
| 504 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 505 | _win->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); |
|---|
| 506 | |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | - (void) otherMouseUp:(NSEvent*)theEvent |
|---|
| 511 | { |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | if([theEvent buttonNumber] == 2) |
|---|
| 520 | { |
|---|
| 521 | [self doMiddleMouseButtonUp:theEvent]; |
|---|
| 522 | } |
|---|
| 523 | else |
|---|
| 524 | { |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | [self doExtraMouseButtonUp:theEvent buttonNumber:[theEvent buttonNumber]]; |
|---|
| 529 | } |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | - (void) setIsUsingCtrlClick:(BOOL)is_using_ctrl_click |
|---|
| 533 | { |
|---|
| 534 | _isUsingCtrlClick = is_using_ctrl_click; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | - (BOOL) isUsingCtrlClick |
|---|
| 538 | { |
|---|
| 539 | return _isUsingCtrlClick; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | - (void) setIsUsingOptionClick:(BOOL)is_using_option_click |
|---|
| 543 | { |
|---|
| 544 | _isUsingOptionClick = is_using_option_click; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | - (BOOL) isUsingOptionClick |
|---|
| 548 | { |
|---|
| 549 | return _isUsingOptionClick; |
|---|
| 550 | } |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | - (void) doLeftMouseButtonDown:(NSEvent*)theEvent |
|---|
| 554 | { |
|---|
| 555 | if (!_win) return; |
|---|
| 556 | |
|---|
| 557 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 558 | |
|---|
| 559 | if([theEvent clickCount] == 1) |
|---|
| 560 | { |
|---|
| 561 | _win->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1); |
|---|
| 562 | } |
|---|
| 563 | else |
|---|
| 564 | { |
|---|
| 565 | _win->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 1); |
|---|
| 566 | } |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | - (void) doLeftMouseButtonUp:(NSEvent*)theEvent |
|---|
| 570 | { |
|---|
| 571 | if (!_win) return; |
|---|
| 572 | |
|---|
| 573 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 574 | |
|---|
| 575 | _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 1); |
|---|
| 576 | |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | - (void) doRightMouseButtonDown:(NSEvent*)theEvent |
|---|
| 580 | { |
|---|
| 581 | if (!_win) return; |
|---|
| 582 | |
|---|
| 583 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 584 | if([theEvent clickCount] == 1) |
|---|
| 585 | { |
|---|
| 586 | _win->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 3); |
|---|
| 587 | } |
|---|
| 588 | else |
|---|
| 589 | { |
|---|
| 590 | _win->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 3); |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | - (void) doRightMouseButtonUp:(NSEvent*)theEvent |
|---|
| 597 | { |
|---|
| 598 | if (!_win) return; |
|---|
| 599 | |
|---|
| 600 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 601 | _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 3); |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | - (void) doMiddleMouseButtonDown:(NSEvent*)theEvent |
|---|
| 605 | { |
|---|
| 606 | if (!_win) return; |
|---|
| 607 | |
|---|
| 608 | DEBUG_OUT("middleMouseDown "); |
|---|
| 609 | |
|---|
| 610 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 611 | |
|---|
| 612 | if([theEvent clickCount] == 1) |
|---|
| 613 | { |
|---|
| 614 | _win->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 2); |
|---|
| 615 | } |
|---|
| 616 | else |
|---|
| 617 | { |
|---|
| 618 | _win->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 2); |
|---|
| 619 | } |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | - (void) doExtraMouseButtonDown:(NSEvent*)theEvent buttonNumber:(int)button_number |
|---|
| 623 | { |
|---|
| 624 | if (!_win) return; |
|---|
| 625 | |
|---|
| 626 | DEBUG_OUT("extraMouseDown btn: " << button_number); |
|---|
| 627 | |
|---|
| 628 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 629 | if([theEvent clickCount] == 1) |
|---|
| 630 | { |
|---|
| 631 | _win->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, button_number+1); |
|---|
| 632 | } |
|---|
| 633 | else |
|---|
| 634 | { |
|---|
| 635 | _win->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, button_number+1); |
|---|
| 636 | } |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | - (void) doMiddleMouseButtonUp:(NSEvent*)theEvent |
|---|
| 641 | { |
|---|
| 642 | if (!_win) return; |
|---|
| 643 | |
|---|
| 644 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 645 | _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 2); |
|---|
| 646 | |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | - (void) doExtraMouseButtonUp:(NSEvent*)theEvent buttonNumber:(int)button_number |
|---|
| 650 | { |
|---|
| 651 | if (!_win) return; |
|---|
| 652 | |
|---|
| 653 | NSPoint converted_point = [self getLocalPoint: theEvent]; |
|---|
| 654 | _win->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, button_number+1); |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | - (void) scrollWheel:(NSEvent*)theEvent |
|---|
| 660 | { |
|---|
| 661 | if (!_win) return; |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | _win->getEventQueue()->mouseScroll2D([theEvent deltaX], [theEvent deltaY]); |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | - (void)keyDown:(NSEvent *)theEvent |
|---|
| 672 | { |
|---|
| 673 | if (!_win) return; |
|---|
| 674 | |
|---|
| 675 | NSString* chars = [theEvent charactersIgnoringModifiers]; |
|---|
| 676 | if ((chars) && ([chars length] > 0)) { |
|---|
| 677 | unsigned int keyCode = remapCocoaKey([chars characterAtIndex:0], ([theEvent modifierFlags] & NSFunctionKeyMask) ); |
|---|
| 678 | |
|---|
| 679 | _win->getEventQueue()->keyPress( remapCocoaKey(keyCode), [theEvent timestamp]); |
|---|
| 680 | } |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | - (void)keyUp:(NSEvent *)theEvent |
|---|
| 685 | { |
|---|
| 686 | if (!_win) return; |
|---|
| 687 | |
|---|
| 688 | NSString* chars = [theEvent charactersIgnoringModifiers]; |
|---|
| 689 | if ((chars) && ([chars length] > 0)) { |
|---|
| 690 | unsigned int keyCode = remapCocoaKey([chars characterAtIndex:0], ([theEvent modifierFlags] & NSFunctionKeyMask)); |
|---|
| 691 | |
|---|
| 692 | _win->getEventQueue()->keyRelease( remapCocoaKey(keyCode), [theEvent timestamp]); |
|---|
| 693 | } |
|---|
| 694 | } |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | - (void)tabletPoint:(NSEvent *)theEvent |
|---|
| 698 | { |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | -(void)handleTabletEvents:(NSEvent *)theEvent |
|---|
| 704 | { |
|---|
| 705 | if (!_win) return; |
|---|
| 706 | |
|---|
| 707 | float pressure = [theEvent pressure]; |
|---|
| 708 | _win->getEventQueue()->penPressure(pressure); |
|---|
| 709 | NSPoint tilt = [theEvent tilt]; |
|---|
| 710 | |
|---|
| 711 | _win->getEventQueue()->penOrientation (tilt.x, tilt.y, [theEvent rotation]); |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | - (void)tabletProximity:(NSEvent *)theEvent |
|---|
| 716 | { |
|---|
| 717 | if (!_win) return; |
|---|
| 718 | |
|---|
| 719 | osgGA::GUIEventAdapter::TabletPointerType pt(osgGA::GUIEventAdapter::UNKNOWN); |
|---|
| 720 | switch ([theEvent pointingDeviceType]) { |
|---|
| 721 | case NSPenPointingDevice: |
|---|
| 722 | pt = osgGA::GUIEventAdapter::PEN; |
|---|
| 723 | break; |
|---|
| 724 | case NSCursorPointingDevice: |
|---|
| 725 | pt = osgGA::GUIEventAdapter::PUCK; |
|---|
| 726 | break; |
|---|
| 727 | case NSEraserPointingDevice: |
|---|
| 728 | pt = osgGA::GUIEventAdapter::ERASER; |
|---|
| 729 | break; |
|---|
| 730 | default: |
|---|
| 731 | break; |
|---|
| 732 | } |
|---|
| 733 | _win->getEventQueue()->penProximity(pt, [theEvent isEnteringProximity]); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | @end |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | #pragma mark GraphicsWindowCocoaDelegate |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | @interface GraphicsWindowCocoaDelegate : NSObject |
|---|
| 748 | { |
|---|
| 749 | @private |
|---|
| 750 | osgViewer::GraphicsWindowCocoa* _win; |
|---|
| 751 | BOOL _inDidMove; |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | - (id)initWith: (osgViewer::GraphicsWindowCocoa*) win; |
|---|
| 755 | - (void)windowDidMove:(NSNotification *)notification; |
|---|
| 756 | - (void)windowDidResize:(NSNotification *)notification; |
|---|
| 757 | - (BOOL)windowShouldClose:(id)window; |
|---|
| 758 | - (void)updateWindowBounds; |
|---|
| 759 | |
|---|
| 760 | @end |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | @implementation GraphicsWindowCocoaDelegate |
|---|
| 764 | |
|---|
| 765 | - (id)initWith: (osgViewer::GraphicsWindowCocoa*) win |
|---|
| 766 | { |
|---|
| 767 | _inDidMove = false; |
|---|
| 768 | _win = win; |
|---|
| 769 | return [super init]; |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | - (void)windowDidMove:(NSNotification *)notification |
|---|
| 774 | { |
|---|
| 775 | [self updateWindowBounds]; |
|---|
| 776 | } |
|---|
| 777 | |
|---|
| 778 | - (void)windowDidResize:(NSNotification *)notification |
|---|
| 779 | { |
|---|
| 780 | [self updateWindowBounds]; |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | -(void)updateWindowBounds |
|---|
| 784 | { |
|---|
| 785 | if (_inDidMove) return; |
|---|
| 786 | _inDidMove = true; |
|---|
| 787 | |
|---|
| 788 | GraphicsWindowCocoaWindow* nswin = _win->getWindow(); |
|---|
| 789 | NSRect bounds = [nswin contentRectForFrameRect: [nswin frame] ]; |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | bounds = convertToQuartzCoordinates(bounds); |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | _win->adaptResize(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); |
|---|
| 797 | |
|---|
| 798 | _win->requestRedraw(); |
|---|
| 799 | _inDidMove = false; |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | - (BOOL)windowShouldClose:(id)window |
|---|
| 803 | { |
|---|
| 804 | return _win->requestClose(); |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | @end |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | #pragma mark CocoaWindowAdapter |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | using namespace osgDarwin; |
|---|
| 815 | namespace osgViewer { |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | |
|---|
| 820 | |
|---|
| 821 | |
|---|
| 822 | class CocoaWindowAdapter : public MenubarController::WindowAdapter { |
|---|
| 823 | public: |
|---|
| 824 | CocoaWindowAdapter(GraphicsWindowCocoa* win) : MenubarController::WindowAdapter(), _win(win) {} |
|---|
| 825 | |
|---|
| 826 | virtual bool valid() { return (_win.valid() && _win->valid()); } |
|---|
| 827 | |
|---|
| 828 | virtual void getWindowBounds(CGRect& rect) |
|---|
| 829 | { |
|---|
| 830 | NSRect nsrect = [_win->getWindow() frame]; |
|---|
| 831 | nsrect = convertToQuartzCoordinates(nsrect); |
|---|
| 832 | |
|---|
| 833 | rect.origin.x = nsrect.origin.x; |
|---|
| 834 | rect.origin.y = nsrect.origin.y; |
|---|
| 835 | rect.size.width = nsrect.size.width; |
|---|
| 836 | rect.size.height = nsrect.size.height; |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | virtual osgViewer::GraphicsWindow* getWindow() {return _win.get(); } |
|---|
| 840 | private: |
|---|
| 841 | osg::observer_ptr<GraphicsWindowCocoa> _win; |
|---|
| 842 | }; |
|---|
| 843 | |
|---|
| 844 | #pragma mark GraphicsWindowCocoa |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | void GraphicsWindowCocoa::init() |
|---|
| 853 | { |
|---|
| 854 | if (_initialized) return; |
|---|
| 855 | |
|---|
| 856 | _closeRequested = false; |
|---|
| 857 | _ownsWindow = false; |
|---|
| 858 | _context = NULL; |
|---|
| 859 | _window = NULL; |
|---|
| 860 | _pixelformat = NULL; |
|---|
| 861 | |
|---|
| 862 | _updateContext = false; |
|---|
| 863 | _valid = _initialized = true; |
|---|
| 864 | } |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | |
|---|
| 868 | |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | void GraphicsWindowCocoa::setupNSWindow(NSWindow* win) |
|---|
| 873 | { |
|---|
| 874 | |
|---|
| 875 | [win setReleasedWhenClosed:NO]; |
|---|
| 876 | [win setDisplaysWhenScreenProfileChanges:YES]; |
|---|
| 877 | GraphicsWindowCocoaDelegate* delegate = [[GraphicsWindowCocoaDelegate alloc] initWith: this]; |
|---|
| 878 | [win setDelegate: delegate ]; |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | [win makeKeyAndOrderFront:nil]; |
|---|
| 882 | [win setAcceptsMouseMovedEvents: YES]; |
|---|
| 883 | |
|---|
| 884 | } |
|---|
| 885 | |
|---|
| 886 | |
|---|
| 887 | |
|---|
| 888 | |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | bool GraphicsWindowCocoa::realizeImplementation() |
|---|
| 892 | { |
|---|
| 893 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 894 | |
|---|
| 895 | unsigned int style(NSBorderlessWindowMask); |
|---|
| 896 | |
|---|
| 897 | if (_traits->windowDecoration) { |
|---|
| 898 | style = NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask; |
|---|
| 899 | |
|---|
| 900 | |
|---|
| 901 | if (_traits->supportsResize) |
|---|
| 902 | style |= NSResizableWindowMask; |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 906 | int screenLeft(0), screenTop(0); |
|---|
| 907 | if (wsi) { |
|---|
| 908 | wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); |
|---|
| 909 | } |
|---|
| 910 | |
|---|
| 911 | NSRect rect = NSMakeRect(_traits->x + screenLeft, _traits->y + screenTop, _traits->width, _traits->height); |
|---|
| 912 | |
|---|
| 913 | _ownsWindow = true; |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | WindowData* windowData = _traits->inheritedWindowData ? dynamic_cast<WindowData*>(_traits->inheritedWindowData.get()) : NULL; |
|---|
| 917 | if (windowData) |
|---|
| 918 | { |
|---|
| 919 | if (windowData->createOnlyView()) |
|---|
| 920 | _ownsWindow = false; |
|---|
| 921 | _checkForEvents = windowData->checkForEvents(); |
|---|
| 922 | |
|---|
| 923 | } |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | osg::notify(osg::DEBUG_INFO) << "GraphicsWindowCocoa::realizeImplementation / ownsWindow: " << _ownsWindow << " checkForEvents: " << _checkForEvents << std::endl; |
|---|
| 927 | |
|---|
| 928 | if (_ownsWindow) |
|---|
| 929 | { |
|---|
| 930 | _window = [[GraphicsWindowCocoaWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: NO]; |
|---|
| 931 | |
|---|
| 932 | if (!_window) { |
|---|
| 933 | osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create window" << std::endl; |
|---|
| 934 | return false; |
|---|
| 935 | } |
|---|
| 936 | |
|---|
| 937 | rect = convertFromQuartzCoordinates(rect); |
|---|
| 938 | [_window setFrameOrigin: rect.origin]; |
|---|
| 939 | } |
|---|
| 940 | |
|---|
| 941 | NSOpenGLPixelFormatAttribute attr[32]; |
|---|
| 942 | int i = 0; |
|---|
| 943 | |
|---|
| 944 | attr[i++] = NSOpenGLPFADepthSize; |
|---|
| 945 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->depth); |
|---|
| 946 | |
|---|
| 947 | if (_traits->doubleBuffer) { |
|---|
| 948 | attr[i++] = NSOpenGLPFADoubleBuffer; |
|---|
| 949 | } |
|---|
| 950 | |
|---|
| 951 | if (_traits->alpha) { |
|---|
| 952 | attr[i++] = NSOpenGLPFAAlphaSize; |
|---|
| 953 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->alpha); |
|---|
| 954 | } |
|---|
| 955 | |
|---|
| 956 | if (_traits->stencil) { |
|---|
| 957 | attr[i++] = NSOpenGLPFAStencilSize; |
|---|
| 958 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->stencil); |
|---|
| 959 | } |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | if (_traits->sampleBuffers) { |
|---|
| 963 | attr[i++] = NSOpenGLPFASampleBuffers; |
|---|
| 964 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->sampleBuffers); |
|---|
| 965 | attr[i++] = NSOpenGLPFASamples; |
|---|
| 966 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->samples); |
|---|
| 967 | } |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | attr[i++] = NSOpenGLPFAAccelerated; |
|---|
| 971 | attr[i] = static_cast<NSOpenGLPixelFormatAttribute>(0); |
|---|
| 972 | |
|---|
| 973 | |
|---|
| 974 | NSOpenGLContext* sharedContext = NULL; |
|---|
| 975 | |
|---|
| 976 | GraphicsHandleCocoa* graphicsHandleCocoa = dynamic_cast<GraphicsHandleCocoa*>(_traits->sharedContext); |
|---|
| 977 | if (graphicsHandleCocoa) |
|---|
| 978 | { |
|---|
| 979 | sharedContext = graphicsHandleCocoa->getNSOpenGLContext(); |
|---|
| 980 | } |
|---|
| 981 | |
|---|
| 982 | _pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr ]; |
|---|
| 983 | _context = [[NSOpenGLContext alloc] initWithFormat: _pixelformat shareContext: sharedContext]; |
|---|
| 984 | |
|---|
| 985 | if (!_context) { |
|---|
| 986 | osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create context" << std::endl; |
|---|
| 987 | return false; |
|---|
| 988 | } |
|---|
| 989 | GraphicsWindowCocoaGLView* theView = [[ GraphicsWindowCocoaGLView alloc ] initWithFrame:[ _window frame ] ]; |
|---|
| 990 | [theView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable) ]; |
|---|
| 991 | [theView setGraphicsWindowCocoa: this]; |
|---|
| 992 | [theView setOpenGLContext:_context]; |
|---|
| 993 | _view = theView; |
|---|
| 994 | osg::notify(osg::DEBUG_INFO) << "GraphicsWindowCocoa::realizeImplementation / view: " << theView << std::endl; |
|---|
| 995 | |
|---|
| 996 | if (_ownsWindow) { |
|---|
| 997 | [_window setContentView: theView]; |
|---|
| 998 | setupNSWindow(_window); |
|---|
| 999 | [theView release]; |
|---|
| 1000 | |
|---|
| 1001 | MenubarController::instance()->attachWindow( new CocoaWindowAdapter(this) ); |
|---|
| 1002 | } |
|---|
| 1003 | else |
|---|
| 1004 | { |
|---|
| 1005 | windowData->setCreatedNSView(theView); |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | [pool release]; |
|---|
| 1009 | |
|---|
| 1010 | |
|---|
| 1011 | useCursor(_traits->useCursor); |
|---|
| 1012 | setWindowName(_traits->windowName); |
|---|
| 1013 | setVSync(_traits->vsync); |
|---|
| 1014 | |
|---|
| 1015 | MenubarController::instance()->update(); |
|---|
| 1016 | |
|---|
| 1017 | |
|---|
| 1018 | getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); |
|---|
| 1019 | |
|---|
| 1020 | _valid = _initialized = _realized = true; |
|---|
| 1021 | return _valid; |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | |
|---|
| 1030 | void GraphicsWindowCocoa::closeImplementation() |
|---|
| 1031 | { |
|---|
| 1032 | _valid = false; |
|---|
| 1033 | _realized = false; |
|---|
| 1034 | |
|---|
| 1035 | |
|---|
| 1036 | MenubarController* mbc = MenubarController::instance(); |
|---|
| 1037 | if (mbc) mbc->detachWindow(this); |
|---|
| 1038 | |
|---|
| 1039 | if (_view) { |
|---|
| 1040 | [_view setGraphicsWindowCocoa: NULL]; |
|---|
| 1041 | } |
|---|
| 1042 | |
|---|
| 1043 | if (_window) { |
|---|
| 1044 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1045 | |
|---|
| 1046 | |
|---|
| 1047 | |
|---|
| 1048 | [_window performSelectorOnMainThread: @selector(close) withObject:NULL waitUntilDone: YES]; |
|---|
| 1049 | [_window performSelectorOnMainThread: @selector(release) withObject:NULL waitUntilDone: YES]; |
|---|
| 1050 | [pool release]; |
|---|
| 1051 | } |
|---|
| 1052 | |
|---|
| 1053 | _window = NULL; |
|---|
| 1054 | _view = NULL; |
|---|
| 1055 | } |
|---|
| 1056 | |
|---|
| 1057 | |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | |
|---|
| 1062 | bool GraphicsWindowCocoa:: makeCurrentImplementation() |
|---|
| 1063 | { |
|---|
| 1064 | if (_updateContext) |
|---|
| 1065 | { |
|---|
| 1066 | [_context update]; |
|---|
| 1067 | _updateContext = false; |
|---|
| 1068 | } |
|---|
| 1069 | |
|---|
| 1070 | [_context makeCurrentContext]; |
|---|
| 1071 | return true; |
|---|
| 1072 | } |
|---|
| 1073 | |
|---|
| 1074 | |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | |
|---|
| 1078 | |
|---|
| 1079 | bool GraphicsWindowCocoa::releaseContextImplementation() |
|---|
| 1080 | { |
|---|
| 1081 | [NSOpenGLContext clearCurrentContext]; |
|---|
| 1082 | return true; |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | |
|---|
| 1086 | |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | void GraphicsWindowCocoa::swapBuffersImplementation() |
|---|
| 1091 | { |
|---|
| 1092 | [_context flushBuffer]; |
|---|
| 1093 | } |
|---|
| 1094 | |
|---|
| 1095 | |
|---|
| 1096 | |
|---|
| 1097 | |
|---|
| 1098 | |
|---|
| 1099 | |
|---|
| 1100 | void GraphicsWindowCocoa::checkEvents() |
|---|
| 1101 | { |
|---|
| 1102 | if (!_checkForEvents) |
|---|
| 1103 | return; |
|---|
| 1104 | |
|---|
| 1105 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1106 | |
|---|
| 1107 | while(1) |
|---|
| 1108 | { |
|---|
| 1109 | |
|---|
| 1110 | |
|---|
| 1111 | |
|---|
| 1112 | |
|---|
| 1113 | |
|---|
| 1114 | |
|---|
| 1115 | |
|---|
| 1116 | NSEvent *event = [ NSApp |
|---|
| 1117 | nextEventMatchingMask:NSAnyEventMask |
|---|
| 1118 | untilDate:[NSDate distantPast] |
|---|
| 1119 | inMode:NSDefaultRunLoopMode |
|---|
| 1120 | dequeue: YES]; |
|---|
| 1121 | if(!event) |
|---|
| 1122 | break; |
|---|
| 1123 | [NSApp sendEvent: event]; |
|---|
| 1124 | } |
|---|
| 1125 | |
|---|
| 1126 | if (_closeRequested) |
|---|
| 1127 | getEventQueue()->closeWindow(); |
|---|
| 1128 | |
|---|
| 1129 | if (s_quit_requested) { |
|---|
| 1130 | getEventQueue()->quitApplication(); |
|---|
| 1131 | s_quit_requested = false; |
|---|
| 1132 | } |
|---|
| 1133 | |
|---|
| 1134 | [pool release]; |
|---|
| 1135 | } |
|---|
| 1136 | |
|---|
| 1137 | |
|---|
| 1138 | |
|---|
| 1139 | |
|---|
| 1140 | |
|---|
| 1141 | |
|---|
| 1142 | |
|---|
| 1143 | |
|---|
| 1144 | |
|---|
| 1145 | |
|---|
| 1146 | bool GraphicsWindowCocoa::setWindowDecorationImplementation(bool flag) |
|---|
| 1147 | { |
|---|
| 1148 | if (!_realized || !_ownsWindow) return false; |
|---|
| 1149 | |
|---|
| 1150 | NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1151 | |
|---|
| 1152 | unsigned int style(NSBorderlessWindowMask); |
|---|
| 1153 | |
|---|
| 1154 | if (flag) { |
|---|
| 1155 | style = NSClosableWindowMask | NSMiniaturizableWindowMask | NSTitledWindowMask; |
|---|
| 1156 | |
|---|
| 1157 | |
|---|
| 1158 | if (_traits->supportsResize) |
|---|
| 1159 | style |= NSResizableWindowMask; |
|---|
| 1160 | } |
|---|
| 1161 | NSRect rect = [_window contentRectForFrameRect: [_window frame] ]; |
|---|
| 1162 | GraphicsWindowCocoaWindow* new_win = [[GraphicsWindowCocoaWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: NO]; |
|---|
| 1163 | |
|---|
| 1164 | if (new_win) { |
|---|
| 1165 | [new_win setContentView: [_window contentView]]; |
|---|
| 1166 | setupNSWindow(new_win); |
|---|
| 1167 | NSString* title = (_traits.valid()) ? [NSString stringWithUTF8String: _traits->windowName.c_str()] : @""; |
|---|
| 1168 | [new_win setTitle: title ]; |
|---|
| 1169 | [_window close]; |
|---|
| 1170 | [_window release]; |
|---|
| 1171 | |
|---|
| 1172 | _window = new_win; |
|---|
| 1173 | [_window makeKeyAndOrderFront: nil]; |
|---|
| 1174 | } |
|---|
| 1175 | |
|---|
| 1176 | [localPool release]; |
|---|
| 1177 | |
|---|
| 1178 | return true; |
|---|
| 1179 | } |
|---|
| 1180 | |
|---|
| 1181 | |
|---|
| 1182 | |
|---|
| 1183 | |
|---|
| 1184 | |
|---|
| 1185 | void GraphicsWindowCocoa::grabFocus() |
|---|
| 1186 | { |
|---|
| 1187 | if (_ownsWindow) |
|---|
| 1188 | [_window makeKeyAndOrderFront: nil]; |
|---|
| 1189 | } |
|---|
| 1190 | |
|---|
| 1191 | |
|---|
| 1192 | |
|---|
| 1193 | |
|---|
| 1194 | |
|---|
| 1195 | void GraphicsWindowCocoa::grabFocusIfPointerInWindow() |
|---|
| 1196 | { |
|---|
| 1197 | osg::notify(osg::INFO) << "GraphicsWindowCocoa :: grabFocusIfPointerInWindow not implemented yet " << std::endl; |
|---|
| 1198 | } |
|---|
| 1199 | |
|---|
| 1200 | |
|---|
| 1201 | |
|---|
| 1202 | |
|---|
| 1203 | |
|---|
| 1204 | |
|---|
| 1205 | void GraphicsWindowCocoa::resizedImplementation(int x, int y, int width, int height) |
|---|
| 1206 | { |
|---|
| 1207 | DEBUG_OUT("resized implementation" << x << " " << y << " " << width << " " << height); |
|---|
| 1208 | GraphicsContext::resizedImplementation(x, y, width, height); |
|---|
| 1209 | |
|---|
| 1210 | _updateContext = true; |
|---|
| 1211 | |
|---|
| 1212 | MenubarController::instance()->update(); |
|---|
| 1213 | getEventQueue()->windowResize(x,y,width, height, getEventQueue()->getTime()); |
|---|
| 1214 | } |
|---|
| 1215 | |
|---|
| 1216 | |
|---|
| 1217 | |
|---|
| 1218 | |
|---|
| 1219 | |
|---|
| 1220 | |
|---|
| 1221 | |
|---|
| 1222 | bool GraphicsWindowCocoa::setWindowRectangleImplementation(int x, int y, int width, int height) |
|---|
| 1223 | { |
|---|
| 1224 | if (!_ownsWindow) |
|---|
| 1225 | return false; |
|---|
| 1226 | |
|---|
| 1227 | NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1228 | |
|---|
| 1229 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 1230 | int screenLeft(0), screenTop(0); |
|---|
| 1231 | if (wsi) { |
|---|
| 1232 | wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); |
|---|
| 1233 | } |
|---|
| 1234 | |
|---|
| 1235 | |
|---|
| 1236 | NSRect rect = NSMakeRect(x+screenLeft,y+screenTop,width, height); |
|---|
| 1237 | rect = convertFromQuartzCoordinates(rect); |
|---|
| 1238 | |
|---|
| 1239 | [_window setFrame: [NSWindow frameRectForContentRect: rect styleMask: [_window styleMask]] display: YES]; |
|---|
| 1240 | [_context update]; |
|---|
| 1241 | MenubarController::instance()->update(); |
|---|
| 1242 | |
|---|
| 1243 | [localPool release]; |
|---|
| 1244 | |
|---|
| 1245 | return true; |
|---|
| 1246 | } |
|---|
| 1247 | |
|---|
| 1248 | |
|---|
| 1249 | |
|---|
| 1250 | |
|---|
| 1251 | |
|---|
| 1252 | |
|---|
| 1253 | void GraphicsWindowCocoa::adaptResize(int x, int y, int w, int h) |
|---|
| 1254 | { |
|---|
| 1255 | |
|---|
| 1256 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 1257 | int screenLeft(0), screenTop(0); |
|---|
| 1258 | if (wsi) { |
|---|
| 1259 | |
|---|
| 1260 | |
|---|
| 1261 | unsigned int screenNdx = wsi->getScreenContaining(x,y,w,h); |
|---|
| 1262 | |
|---|
| 1263 | |
|---|
| 1264 | _traits->screenNum = screenNdx; |
|---|
| 1265 | |
|---|
| 1266 | |
|---|
| 1267 | wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); |
|---|
| 1268 | } |
|---|
| 1269 | |
|---|
| 1270 | resized(x-screenLeft,y-screenTop,w,h); |
|---|
| 1271 | } |
|---|
| 1272 | |
|---|
| 1273 | |
|---|
| 1274 | |
|---|
| 1275 | |
|---|
| 1276 | |
|---|
| 1277 | |
|---|
| 1278 | void GraphicsWindowCocoa::setWindowName (const std::string & name) |
|---|
| 1279 | { |
|---|
| 1280 | if (_traits.valid()) _traits->windowName = name; |
|---|
| 1281 | |
|---|
| 1282 | if (!_ownsWindow) |
|---|
| 1283 | return; |
|---|
| 1284 | |
|---|
| 1285 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1286 | |
|---|
| 1287 | NSString* title = [NSString stringWithUTF8String: name.c_str()]; |
|---|
| 1288 | [_window setTitle: title]; |
|---|
| 1289 | [pool release]; |
|---|
| 1290 | } |
|---|
| 1291 | |
|---|
| 1292 | |
|---|
| 1293 | |
|---|
| 1294 | |
|---|
| 1295 | |
|---|
| 1296 | |
|---|
| 1297 | void GraphicsWindowCocoa::useCursor(bool cursorOn) |
|---|
| 1298 | { |
|---|
| 1299 | if (_traits.valid()) |
|---|
| 1300 | _traits->useCursor = cursorOn; |
|---|
| 1301 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 1302 | if (wsi == NULL) { |
|---|
| 1303 | osg::notify(osg::WARN) << "GraphicsWindowCarbon::useCursor :: could not get OSXCarbonWindowingSystemInterface" << std::endl; |
|---|
| 1304 | return; |
|---|
| 1305 | } |
|---|
| 1306 | |
|---|
| 1307 | CGDirectDisplayID displayId = wsi->getDisplayID((*_traits)); |
|---|
| 1308 | CGDisplayErr err = kCGErrorSuccess; |
|---|
| 1309 | switch (cursorOn) |
|---|
| 1310 | { |
|---|
| 1311 | case true: |
|---|
| 1312 | err = CGDisplayShowCursor(displayId); |
|---|
| 1313 | break; |
|---|
| 1314 | case false: |
|---|
| 1315 | err = CGDisplayHideCursor(displayId); |
|---|
| 1316 | break; |
|---|
| 1317 | } |
|---|
| 1318 | if (err != kCGErrorSuccess) { |
|---|
| 1319 | osg::notify(osg::WARN) << "GraphicsWindowCocoa::useCursor failed with " << err << std::endl; |
|---|
| 1320 | } |
|---|
| 1321 | } |
|---|
| 1322 | |
|---|
| 1323 | |
|---|
| 1324 | |
|---|
| 1325 | |
|---|
| 1326 | |
|---|
| 1327 | |
|---|
| 1328 | void GraphicsWindowCocoa::setCursor(MouseCursor mouseCursor) |
|---|
| 1329 | { |
|---|
| 1330 | NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1331 | |
|---|
| 1332 | switch (mouseCursor) |
|---|
| 1333 | { |
|---|
| 1334 | |
|---|
| 1335 | case NoCursor: |
|---|
| 1336 | [NSCursor hide]; |
|---|
| 1337 | break; |
|---|
| 1338 | |
|---|
| 1339 | case LeftArrowCursor: |
|---|
| 1340 | [[NSCursor arrowCursor] set]; |
|---|
| 1341 | break; |
|---|
| 1342 | |
|---|
| 1343 | case TextCursor: |
|---|
| 1344 | [[NSCursor IBeamCursor] set]; |
|---|
| 1345 | break; |
|---|
| 1346 | |
|---|
| 1347 | case CrosshairCursor: |
|---|
| 1348 | [[NSCursor crosshairCursor] set]; |
|---|
| 1349 | break; |
|---|
| 1350 | |
|---|
| 1351 | default: |
|---|
| 1352 | osg::notify(osg::INFO) << "GraphicsWindowCocoa::setCursor :: unsupported MouseCursor: " << mouseCursor << std::endl; |
|---|
| 1353 | } |
|---|
| 1354 | |
|---|
| 1355 | [localPool release]; |
|---|
| 1356 | } |
|---|
| 1357 | |
|---|
| 1358 | |
|---|
| 1359 | |
|---|
| 1360 | |
|---|
| 1361 | |
|---|
| 1362 | |
|---|
| 1363 | void GraphicsWindowCocoa::setVSync(bool f) |
|---|
| 1364 | { |
|---|
| 1365 | GLint VBL(f?1:0); |
|---|
| 1366 | [_context setValues:&VBL forParameter:NSOpenGLCPSwapInterval]; |
|---|
| 1367 | } |
|---|
| 1368 | |
|---|
| 1369 | |
|---|
| 1370 | |
|---|
| 1371 | |
|---|
| 1372 | |
|---|
| 1373 | |
|---|
| 1374 | GraphicsWindowCocoa::~GraphicsWindowCocoa() |
|---|
| 1375 | { |
|---|
| 1376 | close(); |
|---|
| 1377 | } |
|---|
| 1378 | |
|---|
| 1379 | |
|---|
| 1380 | |
|---|
| 1381 | #pragma mark CocoaWindowingSystemInterface |
|---|
| 1382 | |
|---|
| 1383 | |
|---|
| 1384 | |
|---|
| 1385 | |
|---|
| 1386 | |
|---|
| 1387 | struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface |
|---|
| 1388 | { |
|---|
| 1389 | |
|---|
| 1390 | CocoaWindowingSystemInterface() : DarwinWindowingSystemInterface() |
|---|
| 1391 | { |
|---|
| 1392 | } |
|---|
| 1393 | |
|---|
| 1394 | void initAsStandaloneApplication() |
|---|
| 1395 | { |
|---|
| 1396 | _init(); |
|---|
| 1397 | |
|---|
| 1398 | static bool s_inited = false; |
|---|
| 1399 | if (s_inited) return; |
|---|
| 1400 | s_inited = true; |
|---|
| 1401 | |
|---|
| 1402 | osg::notify(osg::INFO) << "CocoaWindowingSystemInterface::initAsStandaloneApplication " << std::endl; |
|---|
| 1403 | |
|---|
| 1404 | ProcessSerialNumber psn; |
|---|
| 1405 | if (!GetCurrentProcess(&psn)) { |
|---|
| 1406 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
|---|
| 1407 | SetFrontProcess(&psn); |
|---|
| 1408 | } |
|---|
| 1409 | |
|---|
| 1410 | NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1411 | |
|---|
| 1412 | if (NSApp == nil) { |
|---|
| 1413 | [NSApplication sharedApplication]; |
|---|
| 1414 | } |
|---|
| 1415 | |
|---|
| 1416 | [NSApp setDelegate: [[CocoaAppDelegate alloc] init] ]; |
|---|
| 1417 | |
|---|
| 1418 | createApplicationMenus(); |
|---|
| 1419 | |
|---|
| 1420 | [NSApp finishLaunching]; |
|---|
| 1421 | |
|---|
| 1422 | [localPool release]; |
|---|
| 1423 | } |
|---|
| 1424 | |
|---|
| 1425 | virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) |
|---|
| 1426 | { |
|---|
| 1427 | _init(); |
|---|
| 1428 | |
|---|
| 1429 | if (!traits->pbuffer) |
|---|
| 1430 | { |
|---|
| 1431 | GraphicsWindowCocoa::WindowData* windowData = traits->inheritedWindowData ? dynamic_cast<GraphicsWindowCocoa::WindowData*>(traits->inheritedWindowData.get()) : NULL; |
|---|
| 1432 | |
|---|
| 1433 | if (!windowData || (windowData && windowData->poseAsStandaloneApp())) |
|---|
| 1434 | { |
|---|
| 1435 | initAsStandaloneApplication(); |
|---|
| 1436 | } |
|---|
| 1437 | } |
|---|
| 1438 | |
|---|
| 1439 | return createGraphicsContextImplementation<PixelBufferCocoa, GraphicsWindowCocoa>(traits); |
|---|
| 1440 | } |
|---|
| 1441 | |
|---|
| 1442 | virtual ~CocoaWindowingSystemInterface() |
|---|
| 1443 | { |
|---|
| 1444 | } |
|---|
| 1445 | |
|---|
| 1446 | private: |
|---|
| 1447 | NSString *getApplicationName(void) |
|---|
| 1448 | { |
|---|
| 1449 | NSDictionary *dict; |
|---|
| 1450 | NSString *appName = 0; |
|---|
| 1451 | |
|---|
| 1452 | |
|---|
| 1453 | dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); |
|---|
| 1454 | if (dict) |
|---|
| 1455 | appName = [dict objectForKey: @"CFBundleName"]; |
|---|
| 1456 | |
|---|
| 1457 | if (![appName length]) |
|---|
| 1458 | appName = [[NSProcessInfo processInfo] processName]; |
|---|
| 1459 | |
|---|
| 1460 | return appName; |
|---|
| 1461 | } |
|---|
| 1462 | |
|---|
| 1463 | void createApplicationMenus(void) |
|---|
| 1464 | { |
|---|
| 1465 | NSString *appName; |
|---|
| 1466 | NSString *title; |
|---|
| 1467 | NSMenu *appleMenu; |
|---|
| 1468 | NSMenuItem *menuItem; |
|---|
| 1469 | |
|---|
| 1470 | |
|---|
| 1471 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
|---|
| 1472 | |
|---|
| 1473 | |
|---|
| 1474 | appName = getApplicationName(); |
|---|
| 1475 | appleMenu = [[NSMenu alloc] initWithTitle:@""]; |
|---|
| 1476 | |
|---|
| 1477 | |
|---|
| 1478 | title = [@"About " stringByAppendingString:appName]; |
|---|
| 1479 | [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; |
|---|
| 1480 | |
|---|
| 1481 | [appleMenu addItem:[NSMenuItem separatorItem]]; |
|---|
| 1482 | |
|---|
| 1483 | NSMenu* service_menu = [[NSMenu alloc] init]; |
|---|
| 1484 | NSMenuItem* service_menu_item = [[NSMenuItem alloc] initWithTitle:@"Services" action:nil keyEquivalent:@""]; |
|---|
| 1485 | [service_menu_item setSubmenu: service_menu]; |
|---|
| 1486 | [appleMenu addItem: service_menu_item]; |
|---|
| 1487 | [NSApp setServicesMenu: service_menu]; |
|---|
| 1488 | |
|---|
| 1489 | [appleMenu addItem:[NSMenuItem separatorItem]]; |
|---|
| 1490 | |
|---|
| 1491 | title = [@"Hide " stringByAppendingString:appName]; |
|---|
| 1492 | [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; |
|---|
| 1493 | |
|---|
| 1494 | menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""]; |
|---|
| 1495 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; |
|---|
| 1496 | |
|---|
| 1497 | [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; |
|---|
| 1498 | |
|---|
| 1499 | [appleMenu addItem:[NSMenuItem separatorItem]]; |
|---|
| 1500 | |
|---|
| 1501 | title = [@"Quit " stringByAppendingString:appName]; |
|---|
| 1502 | [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; |
|---|
| 1503 | |
|---|
| 1504 | |
|---|
| 1505 | menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; |
|---|
| 1506 | [menuItem setSubmenu:appleMenu]; |
|---|
| 1507 | [[NSApp mainMenu] addItem:menuItem]; |
|---|
| 1508 | [menuItem release]; |
|---|
| 1509 | |
|---|
| 1510 | |
|---|
| 1511 | [NSApp setAppleMenu:appleMenu]; |
|---|
| 1512 | [appleMenu release]; |
|---|
| 1513 | |
|---|
| 1514 | |
|---|
| 1515 | } |
|---|
| 1516 | |
|---|
| 1517 | }; |
|---|
| 1518 | |
|---|
| 1519 | } |
|---|
| 1520 | |
|---|
| 1521 | #ifdef USE_DARWIN_COCOA_IMPLEMENTATION |
|---|
| 1522 | RegisterWindowingSystemInterfaceProxy<osgViewer::CocoaWindowingSystemInterface> createWindowingSystemInterfaceProxy; |
|---|
| 1523 | #endif |
|---|
| 1524 | |
|---|
| 1525 | |
|---|
| 1526 | extern "C" void graphicswindow_Cocoa(void) |
|---|
| 1527 | { |
|---|
| 1528 | osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::CocoaWindowingSystemInterface()); |
|---|
| 1529 | } |
|---|