| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #if defined (__APPLE__) && (!__LP64__) |
|---|
| 15 | |
|---|
| 16 | #include <osg/observer_ptr> |
|---|
| 17 | |
|---|
| 18 | #include <osgViewer/api/Carbon/PixelBufferCarbon> |
|---|
| 19 | #include <osgViewer/api/Carbon/GraphicsWindowCarbon> |
|---|
| 20 | |
|---|
| 21 | #include <osg/DeleteHandler> |
|---|
| 22 | |
|---|
| 23 | #include <Carbon/Carbon.h> |
|---|
| 24 | #include <OpenGL/OpenGL.h> |
|---|
| 25 | |
|---|
| 26 | #include <iostream> |
|---|
| 27 | |
|---|
| 28 | #include "DarwinUtils.h" |
|---|
| 29 | |
|---|
| 30 | using namespace osgViewer; |
|---|
| 31 | using namespace osgDarwin; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | static pascal OSStatus GraphicsWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* userData) |
|---|
| 37 | { |
|---|
| 38 | WindowRef window; |
|---|
| 39 | Rect bounds; |
|---|
| 40 | OSStatus result = eventNotHandledErr; |
|---|
| 41 | |
|---|
| 42 | OSG_NOTIFY(osg::INFO) << "GraphicsWindowEventHandler" << std::endl; |
|---|
| 43 | |
|---|
| 44 | GraphicsWindowCarbon* w = (GraphicsWindowCarbon*)userData; |
|---|
| 45 | if (!w) |
|---|
| 46 | return result; |
|---|
| 47 | |
|---|
| 48 | GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, |
|---|
| 49 | sizeof(window), NULL, &window); |
|---|
| 50 | |
|---|
| 51 | switch(GetEventClass(event)) |
|---|
| 52 | { |
|---|
| 53 | case kEventClassTablet: |
|---|
| 54 | case kEventClassMouse: |
|---|
| 55 | if (w->handleMouseEvent(event)) |
|---|
| 56 | result = noErr; |
|---|
| 57 | break; |
|---|
| 58 | |
|---|
| 59 | case kEventClassKeyboard: |
|---|
| 60 | if (w->handleKeyboardEvent(event)) |
|---|
| 61 | result = noErr; |
|---|
| 62 | break; |
|---|
| 63 | |
|---|
| 64 | case kEventClassWindow: { |
|---|
| 65 | |
|---|
| 66 | switch (GetEventKind(event)) |
|---|
| 67 | { |
|---|
| 68 | case kEventWindowBoundsChanging: |
|---|
| 69 | |
|---|
| 70 | GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL, sizeof(Rect), NULL, &bounds ); |
|---|
| 71 | |
|---|
| 72 | w->adaptResize(bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top); |
|---|
| 73 | w->requestRedraw(); |
|---|
| 74 | result = noErr; |
|---|
| 75 | break; |
|---|
| 76 | |
|---|
| 77 | case kEventWindowBoundsChanged: |
|---|
| 78 | InvalWindowRect(window, GetWindowPortBounds(window, &bounds)); |
|---|
| 79 | GetWindowBounds(window, kWindowContentRgn, &bounds); |
|---|
| 80 | w->adaptResize(bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top); |
|---|
| 81 | result = noErr; |
|---|
| 82 | break; |
|---|
| 83 | |
|---|
| 84 | case kEventWindowClose: |
|---|
| 85 | w->requestClose(); |
|---|
| 86 | result = noErr; |
|---|
| 87 | break; |
|---|
| 88 | |
|---|
| 89 | default: |
|---|
| 90 | break; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | default: |
|---|
| 94 | |
|---|
| 95 | break; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | return result; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | static bool s_quit_requested = false; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | static pascal OSStatus ApplicationEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) |
|---|
| 109 | { |
|---|
| 110 | |
|---|
| 111 | HICommand commandStruct; |
|---|
| 112 | |
|---|
| 113 | OSErr err = eventNotHandledErr; |
|---|
| 114 | |
|---|
| 115 | GetEventParameter (inEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(HICommand), NULL, &commandStruct); |
|---|
| 116 | |
|---|
| 117 | switch(commandStruct.commandID) { |
|---|
| 118 | case kHICommandQuit: |
|---|
| 119 | s_quit_requested = true; |
|---|
| 120 | err = noErr; |
|---|
| 121 | break; |
|---|
| 122 | |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return err; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | static pascal OSErr QuitAppleEventHandler(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) { |
|---|
| 130 | s_quit_requested = true; |
|---|
| 131 | return (noErr); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | namespace osgViewer |
|---|
| 136 | { |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | class CarbonKeyboardMap { |
|---|
| 141 | |
|---|
| 142 | public: |
|---|
| 143 | CarbonKeyboardMap() |
|---|
| 144 | { |
|---|
| 145 | _keymap[53 ] = osgGA::GUIEventAdapter::KEY_Escape; |
|---|
| 146 | _keymap[115 ] = osgGA::GUIEventAdapter::KEY_Home; |
|---|
| 147 | _keymap[76 ] = osgGA::GUIEventAdapter::KEY_KP_Enter; |
|---|
| 148 | _keymap[119 ] = osgGA::GUIEventAdapter::KEY_End; |
|---|
| 149 | _keymap[36 ] = osgGA::GUIEventAdapter::KEY_Return; |
|---|
| 150 | _keymap[116 ] = osgGA::GUIEventAdapter::KEY_Page_Up; |
|---|
| 151 | _keymap[121 ] = osgGA::GUIEventAdapter::KEY_Page_Down; |
|---|
| 152 | _keymap[123 ] = osgGA::GUIEventAdapter::KEY_Left; |
|---|
| 153 | _keymap[124 ] = osgGA::GUIEventAdapter::KEY_Right; |
|---|
| 154 | _keymap[126 ] = osgGA::GUIEventAdapter::KEY_Up; |
|---|
| 155 | _keymap[125 ] = osgGA::GUIEventAdapter::KEY_Down; |
|---|
| 156 | _keymap[51 ] = osgGA::GUIEventAdapter::KEY_BackSpace; |
|---|
| 157 | _keymap[48 ] = osgGA::GUIEventAdapter::KEY_Tab; |
|---|
| 158 | _keymap[49 ] = osgGA::GUIEventAdapter::KEY_Space; |
|---|
| 159 | _keymap[117 ] = osgGA::GUIEventAdapter::KEY_Delete; |
|---|
| 160 | |
|---|
| 161 | _keymap[122 ] = osgGA::GUIEventAdapter::KEY_F1; |
|---|
| 162 | _keymap[120 ] = osgGA::GUIEventAdapter::KEY_F2; |
|---|
| 163 | _keymap[99 ] = osgGA::GUIEventAdapter::KEY_F3; |
|---|
| 164 | _keymap[118 ] = osgGA::GUIEventAdapter::KEY_F4; |
|---|
| 165 | _keymap[96 ] = osgGA::GUIEventAdapter::KEY_F5; |
|---|
| 166 | _keymap[97 ] = osgGA::GUIEventAdapter::KEY_F6; |
|---|
| 167 | _keymap[98 ] = osgGA::GUIEventAdapter::KEY_F7; |
|---|
| 168 | _keymap[100 ] = osgGA::GUIEventAdapter::KEY_F8; |
|---|
| 169 | _keymap[101 ] = osgGA::GUIEventAdapter::KEY_F9; |
|---|
| 170 | _keymap[109 ] = osgGA::GUIEventAdapter::KEY_F10; |
|---|
| 171 | _keymap[103 ] = osgGA::GUIEventAdapter::KEY_F11; |
|---|
| 172 | _keymap[111 ] = osgGA::GUIEventAdapter::KEY_F12; |
|---|
| 173 | |
|---|
| 174 | _keymap[75 ] = osgGA::GUIEventAdapter::KEY_KP_Divide; |
|---|
| 175 | _keymap[67 ] = osgGA::GUIEventAdapter::KEY_KP_Multiply; |
|---|
| 176 | _keymap[78 ] = osgGA::GUIEventAdapter::KEY_KP_Subtract; |
|---|
| 177 | _keymap[69 ] = osgGA::GUIEventAdapter::KEY_KP_Add; |
|---|
| 178 | _keymap[89 ] = osgGA::GUIEventAdapter::KEY_KP_Home; |
|---|
| 179 | _keymap[91 ] = osgGA::GUIEventAdapter::KEY_KP_Up; |
|---|
| 180 | _keymap[92 ] = osgGA::GUIEventAdapter::KEY_KP_Page_Up; |
|---|
| 181 | _keymap[86 ] = osgGA::GUIEventAdapter::KEY_KP_Left; |
|---|
| 182 | _keymap[87 ] = osgGA::GUIEventAdapter::KEY_KP_Begin; |
|---|
| 183 | _keymap[88 ] = osgGA::GUIEventAdapter::KEY_KP_Right; |
|---|
| 184 | _keymap[83 ] = osgGA::GUIEventAdapter::KEY_KP_End; |
|---|
| 185 | _keymap[84 ] = osgGA::GUIEventAdapter::KEY_KP_Down; |
|---|
| 186 | _keymap[85 ] = osgGA::GUIEventAdapter::KEY_KP_Page_Down; |
|---|
| 187 | _keymap[82 ] = osgGA::GUIEventAdapter::KEY_KP_Insert; |
|---|
| 188 | _keymap[65 ] = osgGA::GUIEventAdapter::KEY_KP_Delete; |
|---|
| 189 | |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | ~CarbonKeyboardMap() { |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | unsigned int remapKey(unsigned int key, unsigned int rawkey) |
|---|
| 196 | { |
|---|
| 197 | KeyMap::iterator itr = _keymap.find(rawkey); |
|---|
| 198 | if (itr == _keymap.end()) return key; |
|---|
| 199 | else return itr->second; |
|---|
| 200 | } |
|---|
| 201 | private: |
|---|
| 202 | typedef std::map<unsigned int, osgGA::GUIEventAdapter::KeySymbol> KeyMap; |
|---|
| 203 | KeyMap _keymap; |
|---|
| 204 | }; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | static unsigned int remapCarbonKey(unsigned int key, unsigned int rawkey) |
|---|
| 208 | { |
|---|
| 209 | static CarbonKeyboardMap s_CarbonKeyboardMap; |
|---|
| 210 | return s_CarbonKeyboardMap.remapKey(key,rawkey); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | class CarbonWindowAdapter : public MenubarController::WindowAdapter { |
|---|
| 215 | public: |
|---|
| 216 | CarbonWindowAdapter(GraphicsWindowCarbon* win) : MenubarController::WindowAdapter(), _win(win) {} |
|---|
| 217 | virtual bool valid() {return (_win.valid() && _win->valid()); } |
|---|
| 218 | virtual void getWindowBounds(CGRect& rect) |
|---|
| 219 | { |
|---|
| 220 | Rect windowBounds; |
|---|
| 221 | OSErr error = GetWindowBounds(_win->getNativeWindowRef(), kWindowStructureRgn, &windowBounds); |
|---|
| 222 | rect.origin.x = windowBounds.left; |
|---|
| 223 | rect.origin.y = windowBounds.top; |
|---|
| 224 | rect.size.width = windowBounds.right - windowBounds.left; |
|---|
| 225 | rect.size.height = windowBounds.bottom - windowBounds.top; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | osgViewer::GraphicsWindow* getWindow() { return _win.get(); } |
|---|
| 229 | private: |
|---|
| 230 | osg::observer_ptr<GraphicsWindowCarbon> _win; |
|---|
| 231 | }; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | void GraphicsWindowCarbon::init() |
|---|
| 236 | { |
|---|
| 237 | if (_initialized) return; |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | _lastModifierKeys = 0; |
|---|
| 242 | _windowTitleHeight = 0; |
|---|
| 243 | _closeRequested = false; |
|---|
| 244 | _ownsWindow = false; |
|---|
| 245 | _context = NULL; |
|---|
| 246 | _window = NULL; |
|---|
| 247 | _pixelFormat = PixelBufferCarbon::createPixelFormat(_traits.get()); |
|---|
| 248 | if (!_pixelFormat) |
|---|
| 249 | { |
|---|
| 250 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::init could not create a valid pixelformat" << std::endl; |
|---|
| 251 | } |
|---|
| 252 | _valid = (_pixelFormat != NULL); |
|---|
| 253 | _initialized = true; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | bool GraphicsWindowCarbon::setWindowDecorationImplementation(bool flag) |
|---|
| 257 | { |
|---|
| 258 | _useWindowDecoration = flag; |
|---|
| 259 | |
|---|
| 260 | if (_realized) |
|---|
| 261 | { |
|---|
| 262 | OSErr err = noErr; |
|---|
| 263 | Rect bounds; |
|---|
| 264 | GetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); |
|---|
| 265 | |
|---|
| 266 | if (_useWindowDecoration) |
|---|
| 267 | { |
|---|
| 268 | err = ChangeWindowAttributes(getNativeWindowRef(), kWindowStandardDocumentAttributes, kWindowNoTitleBarAttribute | kWindowNoShadowAttribute); |
|---|
| 269 | SetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); |
|---|
| 270 | } |
|---|
| 271 | else |
|---|
| 272 | { |
|---|
| 273 | err = ChangeWindowAttributes(getNativeWindowRef(), kWindowNoTitleBarAttribute | kWindowNoShadowAttribute, kWindowStandardDocumentAttributes); |
|---|
| 274 | SetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if (err != noErr) |
|---|
| 278 | { |
|---|
| 279 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::setWindowDecoration failed with " << err << std::endl; |
|---|
| 280 | return false; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | Rect titleRect; |
|---|
| 285 | GetWindowBounds(_window, kWindowTitleBarRgn, &titleRect); |
|---|
| 286 | _windowTitleHeight = abs(titleRect.bottom - titleRect.top); |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | aglSetDrawable(_context, 0); |
|---|
| 292 | aglSetDrawable(_context, GetWindowPort(_window)); |
|---|
| 293 | |
|---|
| 294 | MenubarController::instance()->update(); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | return true; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | WindowAttributes GraphicsWindowCarbon::computeWindowAttributes(bool useWindowDecoration, bool supportsResize) { |
|---|
| 302 | WindowAttributes attr; |
|---|
| 303 | |
|---|
| 304 | if (useWindowDecoration) |
|---|
| 305 | { |
|---|
| 306 | if (supportsResize) |
|---|
| 307 | attr = (kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute); |
|---|
| 308 | else |
|---|
| 309 | attr = (kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute) & ~kWindowResizableAttribute; |
|---|
| 310 | } |
|---|
| 311 | else |
|---|
| 312 | { |
|---|
| 313 | attr = kWindowNoTitleBarAttribute | kWindowNoShadowAttribute | kWindowStandardHandlerAttribute; |
|---|
| 314 | if (supportsResize) |
|---|
| 315 | attr |= kWindowResizableAttribute; |
|---|
| 316 | } |
|---|
| 317 | return attr; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | void GraphicsWindowCarbon::installEventHandler() { |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | EventTypeSpec windEventList[] = { |
|---|
| 324 | { kEventClassWindow, kEventWindowBoundsChanged}, |
|---|
| 325 | { kEventClassWindow, kEventWindowClose}, |
|---|
| 326 | |
|---|
| 327 | {kEventClassMouse, kEventMouseDown}, |
|---|
| 328 | {kEventClassMouse, kEventMouseUp}, |
|---|
| 329 | {kEventClassMouse, kEventMouseMoved}, |
|---|
| 330 | {kEventClassMouse, kEventMouseDragged}, |
|---|
| 331 | {kEventClassMouse, kEventMouseWheelMoved}, |
|---|
| 332 | {kEventClassMouse, 11 }, |
|---|
| 333 | |
|---|
| 334 | {kEventClassKeyboard, kEventRawKeyDown}, |
|---|
| 335 | {kEventClassKeyboard, kEventRawKeyRepeat}, |
|---|
| 336 | {kEventClassKeyboard, kEventRawKeyUp}, |
|---|
| 337 | {kEventClassKeyboard, kEventRawKeyModifiersChanged}, |
|---|
| 338 | {kEventClassKeyboard, kEventHotKeyPressed}, |
|---|
| 339 | {kEventClassKeyboard, kEventHotKeyReleased}, |
|---|
| 340 | }; |
|---|
| 341 | |
|---|
| 342 | InstallWindowEventHandler(_window, NewEventHandlerUPP(GraphicsWindowEventHandler), GetEventTypeCount(windEventList), windEventList, this, NULL); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | bool GraphicsWindowCarbon::realizeImplementation() |
|---|
| 347 | { |
|---|
| 348 | |
|---|
| 349 | if (!_initialized) init(); |
|---|
| 350 | if (!_initialized) return false; |
|---|
| 351 | if (!_traits) return false; |
|---|
| 352 | |
|---|
| 353 | OSG_NOTIFY(osg::INFO) << "GraphicsWindowCarbon:: realizeIMplementation" << std::endl; |
|---|
| 354 | |
|---|
| 355 | setWindowDecoration(_traits->windowDecoration); |
|---|
| 356 | useCursor(_traits->useCursor); |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 360 | int screenLeft(0), screenTop(0); |
|---|
| 361 | if (wsi) |
|---|
| 362 | { |
|---|
| 363 | wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | WindowData *windowData = ( _traits.get() && _traits->inheritedWindowData.get() ) ? static_cast<osgViewer::GraphicsWindowCarbon::WindowData*>(_traits->inheritedWindowData.get()) : 0; |
|---|
| 367 | |
|---|
| 368 | _ownsWindow = (windowData) ? (windowData->getNativeWindowRef() == NULL) : true; |
|---|
| 369 | |
|---|
| 370 | if (_ownsWindow) { |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | Rect bounds = {_traits->y + screenTop, _traits->x + screenLeft, _traits->y + _traits->height + screenTop, _traits->x + _traits->width + screenLeft}; |
|---|
| 374 | OSStatus err = 0; |
|---|
| 375 | WindowAttributes attr = computeWindowAttributes(_useWindowDecoration, _traits->supportsResize); |
|---|
| 376 | |
|---|
| 377 | err = CreateNewWindow(kDocumentWindowClass, attr, &bounds, &_window); |
|---|
| 378 | |
|---|
| 379 | if (err) { |
|---|
| 380 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::realizeImplementation() failed creating a window: " << err << std::endl; |
|---|
| 381 | return false; |
|---|
| 382 | } else { |
|---|
| 383 | OSG_NOTIFY(osg::INFO) << "GraphicsWindowCarbon::realizeImplementation() - window created with bounds(" << bounds.top << ", " << bounds.left << ", " << bounds.bottom << ", " << bounds.right << ")" << std::endl; |
|---|
| 384 | } |
|---|
| 385 | } |
|---|
| 386 | else { |
|---|
| 387 | _window = windowData->getNativeWindowRef(); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | Rect titleRect; |
|---|
| 391 | GetWindowBounds(_window, kWindowTitleBarRgn, &titleRect); |
|---|
| 392 | _windowTitleHeight = abs(titleRect.bottom - titleRect.top); |
|---|
| 393 | |
|---|
| 394 | if ((_ownsWindow) || (windowData && windowData->installEventHandler())) |
|---|
| 395 | installEventHandler(); |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | setWindowName(_traits->windowName); |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | AGLContext sharedContextCarbon = NULL; |
|---|
| 402 | |
|---|
| 403 | GraphicsHandleCarbon* graphicsHandleCarbon = dynamic_cast<GraphicsHandleCarbon*>(_traits->sharedContext); |
|---|
| 404 | if (graphicsHandleCarbon) |
|---|
| 405 | { |
|---|
| 406 | sharedContextCarbon = graphicsHandleCarbon->getAGLContext(); |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | _context = aglCreateContext (_pixelFormat, sharedContextCarbon); |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | if (!_context) { |
|---|
| 413 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::realizeImplementation failed creating a context: " << aglGetError() << std::endl; |
|---|
| 414 | return false; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | if ( windowData && windowData->getAGLDrawable() ) { |
|---|
| 419 | aglSetDrawable(_context, (AGLDrawable)*(windowData->getAGLDrawable()) ); |
|---|
| 420 | |
|---|
| 421 | } else { |
|---|
| 422 | aglSetDrawable(_context, GetWindowPort(_window)); |
|---|
| 423 | ShowWindow(_window); |
|---|
| 424 | MenubarController::instance()->attachWindow( new CarbonWindowAdapter(this) ); |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | makeCurrent(); |
|---|
| 428 | |
|---|
| 429 | if ((_traits->useMultiThreadedOpenGLEngine) && (OpenThreads::GetNumberOfProcessors() > 1)) { |
|---|
| 430 | |
|---|
| 431 | CGLError cgerr = kCGLNoError; |
|---|
| 432 | CGLContextObj ctx = CGLGetCurrentContext(); |
|---|
| 433 | |
|---|
| 434 | #if 0 |
|---|
| 435 | cgerr = CGLEnable( ctx, kCGLCEMPEngine); |
|---|
| 436 | #else |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | cgerr = CGLEnable( ctx, static_cast <CGLContextEnable>(313) ); |
|---|
| 441 | #endif |
|---|
| 442 | if (cgerr != kCGLNoError ) |
|---|
| 443 | { |
|---|
| 444 | OSG_NOTIFY(osg::INFO) << "GraphicsWindowCarbon:: Multi-threaded OpenGL Execution not available" << std::endl; |
|---|
| 445 | } |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | InitCursor(); |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | if (_traits->vsync) { |
|---|
| 452 | GLint swap = 1; |
|---|
| 453 | aglSetInteger (_context, AGL_SWAP_INTERVAL, &swap); |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | _realized = true; |
|---|
| 457 | return _realized; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | bool GraphicsWindowCarbon::makeCurrentImplementation() |
|---|
| 463 | { |
|---|
| 464 | |
|---|
| 465 | return (aglSetCurrentContext(_context) == GL_TRUE); |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | bool GraphicsWindowCarbon::releaseContextImplementation() |
|---|
| 469 | { |
|---|
| 470 | if (!_realized) |
|---|
| 471 | { |
|---|
| 472 | OSG_NOTIFY(osg::NOTICE)<<"Warning: GraphicsWindow not realized, cannot do makeCurrent."<<std::endl; |
|---|
| 473 | return false; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | return (aglSetCurrentContext(NULL) == GL_TRUE); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | void GraphicsWindowCarbon::closeImplementation() |
|---|
| 484 | { |
|---|
| 485 | |
|---|
| 486 | _valid = false; |
|---|
| 487 | _realized = false; |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | MenubarController* mbc = MenubarController::instance(); |
|---|
| 491 | if (mbc) mbc->detachWindow(this); |
|---|
| 492 | |
|---|
| 493 | if (_pixelFormat) |
|---|
| 494 | { |
|---|
| 495 | aglDestroyPixelFormat(_pixelFormat); |
|---|
| 496 | _pixelFormat = NULL; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | if (_context) |
|---|
| 500 | { |
|---|
| 501 | aglSetDrawable(_context, NULL); |
|---|
| 502 | aglSetCurrentContext(NULL); |
|---|
| 503 | aglDestroyContext(_context); |
|---|
| 504 | _context = NULL; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | if (_ownsWindow && _window) DisposeWindow(_window); |
|---|
| 508 | _window = NULL; |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | void GraphicsWindowCarbon::swapBuffersImplementation() |
|---|
| 514 | { |
|---|
| 515 | aglSwapBuffers(_context); |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | void GraphicsWindowCarbon::resizedImplementation(int x, int y, int width, int height) |
|---|
| 521 | { |
|---|
| 522 | GraphicsContext::resizedImplementation(x, y, width, height); |
|---|
| 523 | |
|---|
| 524 | aglUpdateContext(_context); |
|---|
| 525 | MenubarController::instance()->update(); |
|---|
| 526 | |
|---|
| 527 | getEventQueue()->windowResize(x,y,width, height, getEventQueue()->getTime()); |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | bool GraphicsWindowCarbon::handleMouseEvent(EventRef theEvent) |
|---|
| 533 | { |
|---|
| 534 | |
|---|
| 535 | static unsigned int lastEmulatedMouseButton = 0; |
|---|
| 536 | |
|---|
| 537 | Point wheresMyMouse; |
|---|
| 538 | GetEventParameter (theEvent, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(wheresMyMouse), NULL, &wheresMyMouse); |
|---|
| 539 | |
|---|
| 540 | wheresMyMouse.v -= _windowTitleHeight; |
|---|
| 541 | if (_useWindowDecoration && (wheresMyMouse.v < 0)) |
|---|
| 542 | return false; |
|---|
| 543 | |
|---|
| 544 | Point wheresMyMouseGlobal; |
|---|
| 545 | GetEventParameter (theEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(wheresMyMouse), NULL, &wheresMyMouseGlobal); |
|---|
| 546 | |
|---|
| 547 | EventMouseButton mouseButton = 0; |
|---|
| 548 | GetEventParameter (theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(mouseButton), NULL, &mouseButton); |
|---|
| 549 | |
|---|
| 550 | UInt32 modifierKeys; |
|---|
| 551 | GetEventParameter (theEvent,kEventParamKeyModifiers,typeUInt32, NULL,sizeof(modifierKeys), NULL,&modifierKeys); |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | WindowRef win; |
|---|
| 555 | int fwres = FindWindow(wheresMyMouseGlobal, &win); |
|---|
| 556 | |
|---|
| 557 | if (((fwres != inContent) && (fwres > 0) && (mouseButton >= 1)) || !IsWindowActive(win)) |
|---|
| 558 | { |
|---|
| 559 | return false; |
|---|
| 560 | } |
|---|
| 561 | else |
|---|
| 562 | { |
|---|
| 563 | UInt32 clickCount; |
|---|
| 564 | GetEventParameter(theEvent, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount); |
|---|
| 565 | |
|---|
| 566 | if (mouseButton==3) mouseButton = 2; |
|---|
| 567 | else if (mouseButton==2) mouseButton = 3; |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | TabletProximityRec theTabletRecord; |
|---|
| 571 | |
|---|
| 572 | if(noErr == GetEventParameter(theEvent, kEventParamTabletProximityRec, |
|---|
| 573 | typeTabletProximityRec, NULL, |
|---|
| 574 | sizeof(TabletProximityRec), |
|---|
| 575 | NULL, (void *)&theTabletRecord)) |
|---|
| 576 | { |
|---|
| 577 | osgGA::GUIEventAdapter::TabletPointerType pointerType; |
|---|
| 578 | switch(theTabletRecord.pointerType) |
|---|
| 579 | { |
|---|
| 580 | case 1: |
|---|
| 581 | pointerType = osgGA::GUIEventAdapter::PEN; |
|---|
| 582 | break; |
|---|
| 583 | |
|---|
| 584 | case 2: |
|---|
| 585 | pointerType = osgGA::GUIEventAdapter::PUCK; |
|---|
| 586 | break; |
|---|
| 587 | |
|---|
| 588 | case 3: |
|---|
| 589 | pointerType = osgGA::GUIEventAdapter::ERASER; |
|---|
| 590 | break; |
|---|
| 591 | |
|---|
| 592 | default: |
|---|
| 593 | pointerType = osgGA::GUIEventAdapter::UNKNOWN; |
|---|
| 594 | break; |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | getEventQueue()->penProximity(pointerType, (theTabletRecord.enterProximity != 0)); |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | TabletPointRec theTabletPointRecord; |
|---|
| 602 | if(noErr == GetEventParameter(theEvent, kEventParamTabletPointRec, typeTabletPointRec, NULL, |
|---|
| 603 | sizeof(TabletPointRec), NULL, (void *)&theTabletPointRecord)) |
|---|
| 604 | { |
|---|
| 605 | int penRotation = (int)theTabletPointRecord.rotation * 9 / 575; |
|---|
| 606 | penRotation = -(((penRotation + 180) % 360) - 180) ; |
|---|
| 607 | getEventQueue()->penOrientation ( |
|---|
| 608 | theTabletPointRecord.tiltX * 60 / 32767.0f, |
|---|
| 609 | -theTabletPointRecord.tiltY * 60 / 32767.0f, |
|---|
| 610 | penRotation |
|---|
| 611 | ); |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | switch(GetEventKind(theEvent)) |
|---|
| 615 | { |
|---|
| 616 | case kEventMouseDown: |
|---|
| 617 | { |
|---|
| 618 | float mx =wheresMyMouse.h; |
|---|
| 619 | float my =wheresMyMouse.v; |
|---|
| 620 | transformMouseXY(mx, my); |
|---|
| 621 | |
|---|
| 622 | lastEmulatedMouseButton = 0; |
|---|
| 623 | |
|---|
| 624 | if (mouseButton == 1) |
|---|
| 625 | { |
|---|
| 626 | if( modifierKeys & cmdKey ) |
|---|
| 627 | { |
|---|
| 628 | mouseButton = lastEmulatedMouseButton = 3; |
|---|
| 629 | } |
|---|
| 630 | else if( modifierKeys & optionKey ) |
|---|
| 631 | { |
|---|
| 632 | mouseButton = lastEmulatedMouseButton = 2; |
|---|
| 633 | } |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | if (clickCount > 1) |
|---|
| 637 | getEventQueue()->mouseDoubleButtonPress(mx,my, mouseButton); |
|---|
| 638 | else |
|---|
| 639 | getEventQueue()->mouseButtonPress(mx, my, mouseButton); |
|---|
| 640 | } |
|---|
| 641 | break; |
|---|
| 642 | case kEventMouseUp: |
|---|
| 643 | { |
|---|
| 644 | float mx =wheresMyMouse.h; |
|---|
| 645 | float my =wheresMyMouse.v; |
|---|
| 646 | transformMouseXY(mx, my); |
|---|
| 647 | if (lastEmulatedMouseButton > 0) { |
|---|
| 648 | getEventQueue()->mouseButtonRelease(mx, my, lastEmulatedMouseButton); |
|---|
| 649 | lastEmulatedMouseButton = 0; |
|---|
| 650 | } |
|---|
| 651 | else { |
|---|
| 652 | getEventQueue()->mouseButtonRelease(mx, my, mouseButton); |
|---|
| 653 | } |
|---|
| 654 | } |
|---|
| 655 | break; |
|---|
| 656 | |
|---|
| 657 | case kEventMouseDragged: |
|---|
| 658 | { |
|---|
| 659 | |
|---|
| 660 | TabletPointRec theTabletRecord; |
|---|
| 661 | if(noErr == GetEventParameter(theEvent, kEventParamTabletPointRec, typeTabletPointRec, NULL, |
|---|
| 662 | sizeof(TabletPointRec), NULL, (void *)&theTabletRecord)) { |
|---|
| 663 | |
|---|
| 664 | getEventQueue()->penPressure(theTabletRecord.pressure / 65535.0f); |
|---|
| 665 | |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | float mx =wheresMyMouse.h; |
|---|
| 669 | float my =wheresMyMouse.v; |
|---|
| 670 | transformMouseXY(mx, my); |
|---|
| 671 | getEventQueue()->mouseMotion(mx, my); |
|---|
| 672 | } |
|---|
| 673 | break; |
|---|
| 674 | |
|---|
| 675 | case kEventMouseMoved: |
|---|
| 676 | { |
|---|
| 677 | float mx = wheresMyMouse.h; |
|---|
| 678 | float my = wheresMyMouse.v; |
|---|
| 679 | transformMouseXY(mx, my); |
|---|
| 680 | getEventQueue()->mouseMotion(mx, my); |
|---|
| 681 | } |
|---|
| 682 | break; |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | case kEventMouseWheelMoved: |
|---|
| 686 | { |
|---|
| 687 | EventMouseWheelAxis axis; |
|---|
| 688 | SInt32 delta; |
|---|
| 689 | if (noErr == GetEventParameter( theEvent, kEventParamMouseWheelAxis, typeMouseWheelAxis, NULL, sizeof(axis), NULL, &axis )) { |
|---|
| 690 | if (noErr == GetEventParameter( theEvent, kEventParamMouseWheelDelta, typeLongInteger, NULL, sizeof(delta), NULL, &delta )) { |
|---|
| 691 | switch (axis) { |
|---|
| 692 | case kEventMouseWheelAxisX: |
|---|
| 693 | getEventQueue()->mouseScroll( (delta > 0) ? osgGA::GUIEventAdapter::SCROLL_RIGHT : osgGA::GUIEventAdapter::SCROLL_LEFT); |
|---|
| 694 | break; |
|---|
| 695 | case kEventMouseWheelAxisY: |
|---|
| 696 | getEventQueue()->mouseScroll( (delta < 0) ? osgGA::GUIEventAdapter::SCROLL_DOWN : osgGA::GUIEventAdapter::SCROLL_UP); |
|---|
| 697 | break; |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | } |
|---|
| 702 | break; |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | case 11: |
|---|
| 706 | { |
|---|
| 707 | enum |
|---|
| 708 | { |
|---|
| 709 | kEventParamMouseWheelSmoothVerticalDelta = 'saxy', |
|---|
| 710 | kEventParamMouseWheelSmoothHorizontalDelta = 'saxx' |
|---|
| 711 | }; |
|---|
| 712 | |
|---|
| 713 | SInt32 scroll_delta_x = 0; |
|---|
| 714 | SInt32 scroll_delta_y = 0; |
|---|
| 715 | OSErr err = noErr; |
|---|
| 716 | err = GetEventParameter( theEvent, kEventParamMouseWheelSmoothVerticalDelta, typeLongInteger, NULL, sizeof(scroll_delta_y), NULL, &scroll_delta_y ); |
|---|
| 717 | err = GetEventParameter( theEvent, kEventParamMouseWheelSmoothHorizontalDelta, typeLongInteger, NULL, sizeof(scroll_delta_x), NULL, &scroll_delta_x ); |
|---|
| 718 | |
|---|
| 719 | if ((scroll_delta_x != 0) || (scroll_delta_y != 0)) { |
|---|
| 720 | getEventQueue()->mouseScroll2D( scroll_delta_x, scroll_delta_y); |
|---|
| 721 | } |
|---|
| 722 | } |
|---|
| 723 | break; |
|---|
| 724 | |
|---|
| 725 | default: |
|---|
| 726 | return false; |
|---|
| 727 | } |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | return true; |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | bool GraphicsWindowCarbon::handleKeyboardEvent(EventRef theEvent) |
|---|
| 736 | { |
|---|
| 737 | handleModifierKeys(theEvent); |
|---|
| 738 | |
|---|
| 739 | OSStatus status; |
|---|
| 740 | |
|---|
| 741 | UInt32 rawkey; |
|---|
| 742 | GetEventParameter (theEvent,kEventParamKeyCode,typeUInt32, NULL,sizeof(rawkey), NULL,&rawkey); |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | UInt32 dataSize; |
|---|
| 747 | |
|---|
| 748 | status = GetEventParameter( theEvent, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0, &dataSize, NULL ); |
|---|
| 749 | if (status != noErr) return false; |
|---|
| 750 | if (dataSize<=1) return false; |
|---|
| 751 | |
|---|
| 752 | UniChar* uniChars = new UniChar[dataSize+1]; |
|---|
| 753 | GetEventParameter( theEvent, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize, NULL, (void*)uniChars ); |
|---|
| 754 | |
|---|
| 755 | unsigned int keychar = remapCarbonKey(static_cast<unsigned long>(uniChars[0]), rawkey); |
|---|
| 756 | |
|---|
| 757 | switch(GetEventKind(theEvent)) |
|---|
| 758 | { |
|---|
| 759 | case kEventRawKeyDown: |
|---|
| 760 | case kEventRawKeyRepeat: |
|---|
| 761 | { |
|---|
| 762 | |
|---|
| 763 | OSG_NOTIFY(osg::INFO) << "GraphicsWindowCarbon::keyPress" << std::endl; |
|---|
| 764 | getEventQueue()->keyPress(keychar); |
|---|
| 765 | break; |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | case kEventRawKeyUp: |
|---|
| 769 | { |
|---|
| 770 | OSG_NOTIFY(osg::INFO) << "GraphicsWindowCarbon::keyPress" << std::endl; |
|---|
| 771 | |
|---|
| 772 | getEventQueue()->keyRelease(keychar); |
|---|
| 773 | break; |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | default: |
|---|
| 777 | break; |
|---|
| 778 | |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | delete[] uniChars; |
|---|
| 782 | |
|---|
| 783 | return true; |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | void GraphicsWindowCarbon::handleModifierKey(UInt32 modifierKey, UInt32 modifierMask, osgGA::GUIEventAdapter::KeySymbol keySymbol) { |
|---|
| 787 | |
|---|
| 788 | if ((modifierKey & modifierMask) && !(_lastModifierKeys & modifierMask)) |
|---|
| 789 | { |
|---|
| 790 | getEventQueue()->keyPress(keySymbol); |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | if (!(modifierKey & modifierMask) && (_lastModifierKeys & modifierMask)) |
|---|
| 794 | { |
|---|
| 795 | getEventQueue()->keyRelease(keySymbol); |
|---|
| 796 | } |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | bool GraphicsWindowCarbon::handleModifierKeys(EventRef theEvent) |
|---|
| 800 | { |
|---|
| 801 | UInt32 modifierKeys; |
|---|
| 802 | GetEventParameter (theEvent,kEventParamKeyModifiers,typeUInt32, NULL,sizeof(modifierKeys), NULL,&modifierKeys); |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | if (_lastModifierKeys == modifierKeys) |
|---|
| 806 | return false; |
|---|
| 807 | |
|---|
| 808 | handleModifierKey(modifierKeys, shiftKey, osgGA::GUIEventAdapter::KEY_Shift_L); |
|---|
| 809 | handleModifierKey(modifierKeys, controlKey, osgGA::GUIEventAdapter::KEY_Control_L); |
|---|
| 810 | handleModifierKey(modifierKeys, optionKey, osgGA::GUIEventAdapter::KEY_Alt_L); |
|---|
| 811 | handleModifierKey(modifierKeys, cmdKey, osgGA::GUIEventAdapter::KEY_Super_L); |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | if ((modifierKeys & alphaLock) && !(_lastModifierKeys & alphaLock)) |
|---|
| 815 | { |
|---|
| 816 | getEventQueue()->keyPress(osgGA::GUIEventAdapter::KEY_Caps_Lock); |
|---|
| 817 | getEventQueue()->keyRelease(osgGA::GUIEventAdapter::KEY_Caps_Lock); |
|---|
| 818 | } |
|---|
| 819 | |
|---|
| 820 | if (!(modifierKeys & alphaLock) && (_lastModifierKeys & alphaLock)) |
|---|
| 821 | { |
|---|
| 822 | getEventQueue()->keyPress(osgGA::GUIEventAdapter::KEY_Caps_Lock); |
|---|
| 823 | getEventQueue()->keyRelease(osgGA::GUIEventAdapter::KEY_Caps_Lock); |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | _lastModifierKeys = modifierKeys; |
|---|
| 827 | return true; |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | void GraphicsWindowCarbon::checkEvents() |
|---|
| 833 | { |
|---|
| 834 | if (!_realized) return; |
|---|
| 835 | |
|---|
| 836 | EventRef theEvent; |
|---|
| 837 | EventTargetRef theTarget = GetEventDispatcherTarget(); |
|---|
| 838 | while (ReceiveNextEvent(0, NULL, 0,true, &theEvent)== noErr) |
|---|
| 839 | { |
|---|
| 840 | switch(GetEventClass(theEvent)) |
|---|
| 841 | { |
|---|
| 842 | case kEventClassMouse: |
|---|
| 843 | { |
|---|
| 844 | |
|---|
| 845 | Point wheresMyMouse; |
|---|
| 846 | GetEventParameter (theEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(wheresMyMouse), NULL, &wheresMyMouse); |
|---|
| 847 | |
|---|
| 848 | EventMouseButton mouseButton = 0; |
|---|
| 849 | GetEventParameter (theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(mouseButton), NULL, &mouseButton); |
|---|
| 850 | |
|---|
| 851 | WindowRef win; |
|---|
| 852 | int fwres = FindWindow(wheresMyMouse, &win); |
|---|
| 853 | |
|---|
| 854 | if ((fwres == inMenuBar) && (mouseButton >= 1)) { |
|---|
| 855 | MenuSelect(wheresMyMouse); |
|---|
| 856 | HiliteMenu(0); |
|---|
| 857 | return; |
|---|
| 858 | } |
|---|
| 859 | break; |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | case kEventClassApplication: |
|---|
| 863 | switch (GetEventKind(theEvent)) { |
|---|
| 864 | case kEventAppQuit: |
|---|
| 865 | getEventQueue()->quitApplication(); |
|---|
| 866 | break; |
|---|
| 867 | } |
|---|
| 868 | break; |
|---|
| 869 | |
|---|
| 870 | case kEventClassAppleEvent: |
|---|
| 871 | { |
|---|
| 872 | EventRecord eventRecord; |
|---|
| 873 | ConvertEventRefToEventRecord(theEvent, &eventRecord); |
|---|
| 874 | AEProcessAppleEvent(&eventRecord); |
|---|
| 875 | return; |
|---|
| 876 | } |
|---|
| 877 | break; |
|---|
| 878 | |
|---|
| 879 | } |
|---|
| 880 | SendEventToEventTarget (theEvent, theTarget); |
|---|
| 881 | ReleaseEvent(theEvent); |
|---|
| 882 | } |
|---|
| 883 | if (_closeRequested) |
|---|
| 884 | getEventQueue()->closeWindow(); |
|---|
| 885 | |
|---|
| 886 | if (s_quit_requested) { |
|---|
| 887 | getEventQueue()->quitApplication(); |
|---|
| 888 | s_quit_requested = false; |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | } |
|---|
| 892 | |
|---|
| 893 | |
|---|
| 894 | bool GraphicsWindowCarbon::setWindowRectangleImplementation(int x, int y, int width, int height) |
|---|
| 895 | { |
|---|
| 896 | int screenLeft(0), screenTop(0); |
|---|
| 897 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 898 | if (wsi) |
|---|
| 899 | { |
|---|
| 900 | wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); |
|---|
| 901 | } |
|---|
| 902 | |
|---|
| 903 | Rect bounds = {y + screenTop, x + screenLeft, y + height + screenTop, x + width + screenLeft}; |
|---|
| 904 | SetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); |
|---|
| 905 | aglUpdateContext(_context); |
|---|
| 906 | MenubarController::instance()->update(); |
|---|
| 907 | return true; |
|---|
| 908 | } |
|---|
| 909 | |
|---|
| 910 | |
|---|
| 911 | |
|---|
| 912 | void GraphicsWindowCarbon::adaptResize(int x, int y, int w, int h) |
|---|
| 913 | { |
|---|
| 914 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 915 | int screenLeft(0), screenTop(0); |
|---|
| 916 | if (wsi) { |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | unsigned int screenNdx = wsi->getScreenContaining(x,y,w,h); |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | _traits->screenNum = screenNdx; |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); |
|---|
| 926 | } |
|---|
| 927 | |
|---|
| 928 | resized(x-screenLeft,y-screenTop,w,h); |
|---|
| 929 | } |
|---|
| 930 | |
|---|
| 931 | |
|---|
| 932 | |
|---|
| 933 | void GraphicsWindowCarbon::grabFocus() |
|---|
| 934 | { |
|---|
| 935 | SelectWindow(_window); |
|---|
| 936 | } |
|---|
| 937 | |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | void GraphicsWindowCarbon::grabFocusIfPointerInWindow() |
|---|
| 941 | { |
|---|
| 942 | |
|---|
| 943 | OSG_NOTIFY(osg::ALWAYS) << "GraphicsWindowCarbon::grabFocusIfPointerInWindow" << std::endl; |
|---|
| 944 | } |
|---|
| 945 | |
|---|
| 946 | |
|---|
| 947 | void GraphicsWindowCarbon::useCursor(bool cursorOn) |
|---|
| 948 | { |
|---|
| 949 | |
|---|
| 950 | if (_traits.valid()) |
|---|
| 951 | _traits->useCursor = cursorOn; |
|---|
| 952 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 953 | if (wsi == NULL) { |
|---|
| 954 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::useCursor :: could not get OSXCarbonWindowingSystemInterface" << std::endl; |
|---|
| 955 | return; |
|---|
| 956 | } |
|---|
| 957 | |
|---|
| 958 | CGDirectDisplayID displayId = wsi->getDisplayID((*_traits)); |
|---|
| 959 | CGDisplayErr err = kCGErrorSuccess; |
|---|
| 960 | switch (cursorOn) |
|---|
| 961 | { |
|---|
| 962 | case true: |
|---|
| 963 | err = CGDisplayShowCursor(displayId); |
|---|
| 964 | break; |
|---|
| 965 | case false: |
|---|
| 966 | err = CGDisplayHideCursor(displayId); |
|---|
| 967 | break; |
|---|
| 968 | } |
|---|
| 969 | if (err != kCGErrorSuccess) { |
|---|
| 970 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::useCursor failed with " << err << std::endl; |
|---|
| 971 | } |
|---|
| 972 | } |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | |
|---|
| 976 | void GraphicsWindowCarbon::setCursor(MouseCursor mouseCursor) |
|---|
| 977 | { |
|---|
| 978 | UInt32 cursor; |
|---|
| 979 | if (_currentCursor == mouseCursor) |
|---|
| 980 | return; |
|---|
| 981 | switch (mouseCursor) |
|---|
| 982 | { |
|---|
| 983 | case NoCursor: |
|---|
| 984 | HideCursor(); |
|---|
| 985 | _currentCursor = mouseCursor; |
|---|
| 986 | return; |
|---|
| 987 | case RightArrowCursor: |
|---|
| 988 | cursor = kThemeArrowCursor; |
|---|
| 989 | break; |
|---|
| 990 | case CrosshairCursor: |
|---|
| 991 | cursor = kThemeCrossCursor; |
|---|
| 992 | break; |
|---|
| 993 | case TextCursor: |
|---|
| 994 | cursor = kThemeIBeamCursor; |
|---|
| 995 | break; |
|---|
| 996 | case UpDownCursor: |
|---|
| 997 | cursor = kThemeResizeUpDownCursor; |
|---|
| 998 | break; |
|---|
| 999 | case LeftRightCursor: |
|---|
| 1000 | cursor = kThemeResizeLeftRightCursor; |
|---|
| 1001 | break; |
|---|
| 1002 | default: |
|---|
| 1003 | cursor = kThemeArrowCursor; |
|---|
| 1004 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::setCursor doesn't implement cursor: type = " << mouseCursor << std::endl; |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| 1007 | _currentCursor = mouseCursor; |
|---|
| 1008 | SetThemeCursor(cursor); |
|---|
| 1009 | ShowCursor(); |
|---|
| 1010 | } |
|---|
| 1011 | |
|---|
| 1012 | |
|---|
| 1013 | void GraphicsWindowCarbon::setWindowName (const std::string& name) |
|---|
| 1014 | { |
|---|
| 1015 | _traits->windowName = name; |
|---|
| 1016 | if (!_traits->windowName.empty()) |
|---|
| 1017 | { |
|---|
| 1018 | CFStringRef windowtitle = CFStringCreateWithBytes( kCFAllocatorDefault, (const UInt8*)(_traits->windowName.c_str()), _traits->windowName.length(),kCFStringEncodingUTF8, false ); |
|---|
| 1019 | SetWindowTitleWithCFString( _window, windowtitle ); |
|---|
| 1020 | CFRelease(windowtitle); |
|---|
| 1021 | } |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | void GraphicsWindowCarbon::requestWarpPointer(float x,float y) |
|---|
| 1025 | { |
|---|
| 1026 | |
|---|
| 1027 | DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); |
|---|
| 1028 | if (wsi == NULL) { |
|---|
| 1029 | OSG_NOTIFY(osg::WARN) << "GraphicsWindowCarbon::useCursor :: could not get OSXCarbonWindowingSystemInterface" << std::endl; |
|---|
| 1030 | return; |
|---|
| 1031 | } |
|---|
| 1032 | |
|---|
| 1033 | CGDirectDisplayID displayId = wsi->getDisplayID((*_traits)); |
|---|
| 1034 | |
|---|
| 1035 | CGPoint point; |
|---|
| 1036 | point.x = x + _traits->x; |
|---|
| 1037 | point.y = y + _traits->y; |
|---|
| 1038 | CGDisplayMoveCursorToPoint(displayId, point); |
|---|
| 1039 | |
|---|
| 1040 | getEventQueue()->mouseWarped(x,y); |
|---|
| 1041 | } |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | void GraphicsWindowCarbon::transformMouseXY(float& x, float& y) |
|---|
| 1045 | { |
|---|
| 1046 | if (getEventQueue()->getUseFixedMouseInputRange()) |
|---|
| 1047 | { |
|---|
| 1048 | osgGA::GUIEventAdapter* eventState = getEventQueue()->getCurrentEventState(); |
|---|
| 1049 | x = eventState->getXmin() + (eventState->getXmax()-eventState->getXmin())*x/float(_traits->width); |
|---|
| 1050 | y = eventState->getYmin() + (eventState->getYmax()-eventState->getYmin())*y/float(_traits->height); |
|---|
| 1051 | } |
|---|
| 1052 | } |
|---|
| 1053 | |
|---|
| 1054 | class CarbonWindowingSystemInterface : public DarwinWindowingSystemInterface { |
|---|
| 1055 | public: |
|---|
| 1056 | CarbonWindowingSystemInterface() : DarwinWindowingSystemInterface() |
|---|
| 1057 | { |
|---|
| 1058 | } |
|---|
| 1059 | |
|---|
| 1060 | virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) |
|---|
| 1061 | { |
|---|
| 1062 | _init(); |
|---|
| 1063 | |
|---|
| 1064 | return createGraphicsContextImplementation<PixelBufferCarbon, GraphicsWindowCarbon>(traits); |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | virtual void _init() |
|---|
| 1068 | { |
|---|
| 1069 | if (_initialized) return; |
|---|
| 1070 | |
|---|
| 1071 | DarwinWindowingSystemInterface::init(); |
|---|
| 1072 | |
|---|
| 1073 | |
|---|
| 1074 | static const EventTypeSpec menueventSpec = {kEventClassCommand, kEventCommandProcess}; |
|---|
| 1075 | OSErr status = InstallEventHandler(GetApplicationEventTarget(), NewEventHandlerUPP(ApplicationEventHandler), 1, &menueventSpec, 0, NULL); |
|---|
| 1076 | status = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(QuitAppleEventHandler), 0, false); |
|---|
| 1077 | } |
|---|
| 1078 | |
|---|
| 1079 | }; |
|---|
| 1080 | |
|---|
| 1081 | |
|---|
| 1082 | } |
|---|
| 1083 | |
|---|
| 1084 | #ifdef USE_DARWIN_CARBON_IMPLEMENTATION |
|---|
| 1085 | RegisterWindowingSystemInterfaceProxy<CarbonWindowingSystemInterface> createWindowingSystemInterfaceProxy; |
|---|
| 1086 | #endif |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | extern "C" void graphicswindow_Carbon(void) |
|---|
| 1091 | { |
|---|
| 1092 | osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::CarbonWindowingSystemInterface()); |
|---|
| 1093 | } |
|---|
| 1094 | |
|---|
| 1095 | #endif |
|---|