Changeset 71661 in vbox for trunk/src/VBox/Frontends/VirtualBox
- Timestamp:
- Apr 4, 2018 3:16:37 PM (7 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UICocoaApplication.h
r69500 r71661 1 1 /* $Id$ */ 2 2 /** @file 3 * VBox Qt GUI - UICocoaApplication - C++ interface to NSApplication for handling -sendEvent.3 * VBox Qt GUI - UICocoaApplication class declaration. 4 4 */ 5 5 6 6 /* 7 * Copyright (C) 2009-201 7Oracle Corporation7 * Copyright (C) 2009-2018 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 16 16 */ 17 17 18 #ifndef ___ darwin_VBoxCocoaApplication_h19 #define ___ darwin_VBoxCocoaApplication_h18 #ifndef ___VBoxCocoaApplication_h___ 19 #define ___VBoxCocoaApplication_h___ 20 20 21 21 /* Qt includes: */ … … 26 26 #include "VBoxUtils-darwin.h" 27 27 28 /* Forward declarations: */ 29 class QObject; 30 class QWidget; 31 32 /* Cocoa declarations: */ 28 33 ADD_COCOA_NATIVE_REF(UICocoaApplicationPrivate); 29 34 ADD_COCOA_NATIVE_REF(NSAutoreleasePool); … … 32 37 ADD_COCOA_NATIVE_REF(NSButton); 33 38 34 /* Forward declarations: */35 class QObject;36 class QWidget;37 39 38 /** Event handler callback. 39 * @returns true if handled, false if not. 40 * @param pvCocoaEvent The Cocoa event. 41 * @param pvCarbonEvent The Carbon event. 42 * @param pvUser The user argument. 43 */ 40 /** Event handler callback. */ 44 41 typedef bool (*PFNVBOXCACALLBACK)(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser); 45 42 … … 51 48 typedef void (*PfnStandardWindowButtonCallbackForQWidget)(StandardWindowButtonType emnButtonType, bool fWithOptionKey, QWidget *pWidget); 52 49 53 /* C++ singleton for our private NSApplication object. */ 50 51 /** Singleton prototype for our private NSApplication object. */ 54 52 class UICocoaApplication 55 53 { 56 54 public: 57 55 58 /** Returns singleton accessinstance. */59 static UICocoaApplication *instance();56 /** Returns singleton instance. */ 57 static UICocoaApplication *instance(); 60 58 61 /** Destruct or. */62 ~UICocoaApplication();59 /** Destructs cocoa application. */ 60 virtual ~UICocoaApplication(); 63 61 64 62 /** Returns whether application is currently active. */ … … 97 95 private: 98 96 99 /** Construct or. */97 /** Constructs cocoa application. */ 100 98 UICocoaApplication(); 101 99 102 100 /** Holds the singleton access instance. */ 103 static UICocoaApplication * m_pInstance;101 static UICocoaApplication *s_pInstance; 104 102 105 103 /** Holds the private NSApplication instance. */ 106 NativeUICocoaApplicationPrivateRef m_pNative;104 NativeUICocoaApplicationPrivateRef m_pNative; 107 105 /** Holds the private NSAutoreleasePool instance. */ 108 NativeNSAutoreleasePoolRef m_pPool;106 NativeNSAutoreleasePoolRef m_pPool; 109 107 110 108 /** Map of notification callbacks registered for corresponding QObject(s). */ 111 QMap<QObject*, QMap<QString, PfnNativeNotificationCallbackForQObject> > m_objectCallbacks;109 QMap<QObject*, QMap<QString, PfnNativeNotificationCallbackForQObject> > m_objectCallbacks; 112 110 /** Map of notification callbacks registered for corresponding QWidget(s). */ 113 QMap<QWidget*, QMap<QString, PfnNativeNotificationCallbackForQWidget> > m_widgetCallbacks;111 QMap<QWidget*, QMap<QString, PfnNativeNotificationCallbackForQWidget> > m_widgetCallbacks; 114 112 115 113 /** Map of callbacks registered for standard window button(s) of corresponding QWidget(s). */ 116 QMap<QWidget*, QMap<StandardWindowButtonType, PfnStandardWindowButtonCallbackForQWidget> > m_stdWindowButtonCallbacks;114 QMap<QWidget*, QMap<StandardWindowButtonType, PfnStandardWindowButtonCallbackForQWidget> > m_stdWindowButtonCallbacks; 117 115 }; 118 116 119 #endif /* ___darwin_VBoxCocoaApplication_h*/117 #endif /* !___VBoxCocoaApplication_h___ */ 120 118 -
trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UICocoaApplication.mm
r69500 r71661 1 1 /* $Id$ */ 2 2 /** @file 3 * VBox Qt GUI - UICocoaApplication - C++ interface to NSApplication for handling -sendEvent.3 * VBox Qt GUI - UICocoaApplication class implementation. 4 4 */ 5 5 6 6 /* 7 * Copyright (C) 2009-201 7Oracle Corporation7 * Copyright (C) 2009-2018 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 19 19 #include "UICocoaApplication.h" 20 20 21 /* Global includes */ 21 /* Other VBox includes: */ 22 #include <iprt/assert.h> 23 24 /* External includes: */ 25 #import <AppKit/NSApplication.h> 26 #import <AppKit/NSButton.h> 22 27 #import <AppKit/NSEvent.h> 23 #import <AppKit/NS Application.h>28 #import <AppKit/NSWindow.h> 24 29 #import <Foundation/NSArray.h> 25 #import <AppKit/NSWindow.h> 26 #import <AppKit/NSButton.h> 27 28 #include <iprt/assert.h> 30 29 31 30 32 /** Class for tracking a callback. */ 31 @interface CallbackData : NSObject33 @interface CallbackData : NSObject 32 34 { 33 35 @public 34 /** Mask of events to send to this callback. */35 uint32_t 36 /** The callback. */37 PFNVBOXCACALLBACK 38 /** The user argument. */39 void 40 } 41 - (id) initWithMask:(uint32)mask callback:(PFNVBOXCACALLBACK)callback user:(void*)user;36 /** Holds the mask of events to send to this callback. */ 37 uint32_t fMask; 38 /** Holds the callback. */ 39 PFNVBOXCACALLBACK pfnCallback; 40 /** Holds the user argument. */ 41 void *pvUser; 42 } 43 - (id)initWithMask :(uint32)mask callback :(PFNVBOXCACALLBACK)callback user :(void *)user; 42 44 @end /* @interface CallbackData */ 43 45 44 46 @implementation CallbackData 45 - (id) initWithMask:(uint32)mask callback:(PFNVBOXCACALLBACK)callback user:(void*)user 47 /** Performs initialization. */ 48 - (id)initWithMask :(uint32)mask callback :(PFNVBOXCACALLBACK)callback user :(void *)user 46 49 { 47 50 self = [super init]; … … 50 53 fMask = mask; 51 54 pfnCallback = callback; 52 pvUser = 55 pvUser = user; 53 56 } 54 57 return self; … … 56 59 @end /* @implementation CallbackData */ 57 60 58 /** Class for event handling */ 59 @interface UICocoaApplicationPrivate: NSApplication 61 62 /** Class for event handling. */ 63 @interface UICocoaApplicationPrivate : NSApplication 60 64 { 61 65 /** The event mask for which there currently are callbacks. */ … … 65 69 } 66 70 - (id)init; 67 - (void)sendEvent :(NSEvent *)theEvent;68 - (void)setCallback :(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;69 - (void)unsetCallback :(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;70 71 - (void)registerToNotificationOfWorkspace :(NSString *)pstrNotificationName;72 - (void)unregisterFromNotificationOfWorkspace :(NSString *)pstrNotificationName;73 74 - (void)registerToNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow*)pWindow;75 - (void)unregisterFromNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow*)pWindow;76 77 - (void)notificationCallbackOfObject :(NSNotification *)notification;78 - (void)notificationCallbackOfWindow :(NSNotification *)notification;79 80 - (void)registerSelectorForStandardWindowButton :(NSWindow *)pWindow :(StandardWindowButtonType)enmButtonType;81 - (void)selectorForStandardWindowButton :(NSButton *)pButton;71 - (void)sendEvent :(NSEvent *)theEvent; 72 - (void)setCallback :(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser; 73 - (void)unsetCallback :(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser; 74 75 - (void)registerToNotificationOfWorkspace :(NSString *)pstrNotificationName; 76 - (void)unregisterFromNotificationOfWorkspace :(NSString *)pstrNotificationName; 77 78 - (void)registerToNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow *)pWindow; 79 - (void)unregisterFromNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow *)pWindow; 80 81 - (void)notificationCallbackOfObject :(NSNotification *)notification; 82 - (void)notificationCallbackOfWindow :(NSNotification *)notification; 83 84 - (void)registerSelectorForStandardWindowButton :(NSWindow *)pWindow :(StandardWindowButtonType)enmButtonType; 85 - (void)selectorForStandardWindowButton :(NSButton *)pButton; 82 86 @end /* @interface UICocoaApplicationPrivate */ 83 87 84 88 @implementation UICocoaApplicationPrivate 85 -(id) init 89 /** Performs initialization. */ 90 - (id) init 86 91 { 87 92 self = [super init]; … … 89 94 m_pCallbacks = [[NSMutableArray alloc] init]; 90 95 91 /* Gently disable El Capitan tries to break everything with the Enter Full Screen action. 92 * S.a. https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/ for reference. */ 96 // WORKAROUND: 97 // Gently disable El Capitan tries to break everything with the Enter Full Screen action. 98 // S.a. https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/ for reference. 93 99 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSFullScreenMenuItemEverywhere"]; 94 100 … … 96 102 } 97 103 98 -(void) sendEvent:(NSEvent *)pEvent 99 { 100 /* 101 * Check if the type matches any of the registered callbacks. 102 */104 /** Sends an event. 105 * @param pEvent Brings the event to be sent. */ 106 - (void) sendEvent :(NSEvent *)pEvent 107 { 108 /* Check if the type matches any of the registered callbacks. */ 103 109 uint32_t const fMask = m_fMask; 104 110 #if 0 /* for debugging */ … … 111 117 if (fMask & fEvtMask) 112 118 { 113 /* 114 * Do the callouts in LIFO order. 115 */ 119 /* Do the callouts in LIFO order. */ 116 120 for (CallbackData *pData in [m_pCallbacks reverseObjectEnumerator]) 117 121 { … … 126 130 } 127 131 128 /* 129 * Get on with it. 130 */ 132 /* Get on with it. */ 131 133 [super sendEvent:pEvent]; 132 134 } 133 135 134 /** 135 * Register an event callback. 136 * 137 * @param fMask The event mask for which the callback is to be invoked. 138 * @param pfnCallback The callback function. 139 * @param pvUser The user argument. 140 */ 141 -(void) setCallback: (uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser 142 { 143 /* Add the callback data to the array */ 144 CallbackData *pData = [[[CallbackData alloc] initWithMask: fMask callback: pfnCallback user: pvUser] autorelease]; 145 [m_pCallbacks addObject: pData]; 146 147 /* Update the global mask */ 136 /** Registers an event callback. 137 * @param fMask Brings the event mask for which the callback is to be invoked. 138 * @param pfnCallback Brings the callback function. 139 * @param pvUser Brings the user argument. */ 140 - (void) setCallback :(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser 141 { 142 /* Add the callback data to the array: */ 143 CallbackData *pData = [[[CallbackData alloc] initWithMask:fMask callback:pfnCallback user:pvUser] autorelease]; 144 [m_pCallbacks addObject:pData]; 145 146 /* Update the global mask: */ 148 147 m_fMask |= fMask; 149 148 } 150 149 151 /** 152 * Deregister an event callback. 153 * 154 * @param fMask Same as setCallback. 155 * @param pfnCallback Same as setCallback. 156 * @param pvUser Same as setCallback. 157 */ 158 -(void) unsetCallback: (uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser 159 { 160 /* 161 * Loop the event array LIFO fashion searching for a matching callback. 162 */ 150 /** Deregisters an event callback. 151 * @param fMask Brings the event mask for which the callback is to be invoked. 152 * @param pfnCallback Brings the callback function. 153 * @param pvUser Brings the user argument. */ 154 - (void) unsetCallback: (uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser 155 { 156 /* Loop the event array LIFO fashion searching for a matching callback. */ 163 157 for (CallbackData *pData in [m_pCallbacks reverseObjectEnumerator]) 164 158 { … … 167 161 && pData->fMask == fMask) 168 162 { 169 [m_pCallbacks removeObject: 163 [m_pCallbacks removeObject:pData]; 170 164 break; 171 165 } … … 177 171 } 178 172 179 /** Register to cocoa notification @a pstrNotificationName. */180 - (void) registerToNotificationOfWorkspace :(NSString *)pstrNotificationName173 /** Registers to cocoa notification @a pstrNotificationName. */ 174 - (void) registerToNotificationOfWorkspace :(NSString *)pstrNotificationName 181 175 { 182 176 /* Register notification observer: */ … … 189 183 190 184 /** Unregister @a pWindow from cocoa notification @a pstrNotificationName. */ 191 - (void) unregisterFromNotificationOfWorkspace :(NSString *)pstrNotificationName185 - (void) unregisterFromNotificationOfWorkspace :(NSString *)pstrNotificationName 192 186 { 193 187 /* Uninstall notification observer: */ … … 199 193 200 194 /** Register @a pWindow to cocoa notification @a pstrNotificationName. */ 201 - (void) registerToNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow*)pWindow195 - (void) registerToNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow *)pWindow 202 196 { 203 197 /* Register notification observer: */ … … 209 203 210 204 /** Unregister @a pWindow from cocoa notification @a pstrNotificationName. */ 211 - (void) unregisterFromNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow*)pWindow205 - (void) unregisterFromNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow *)pWindow 212 206 { 213 207 /* Uninstall notification observer: */ … … 218 212 219 213 /** Redirects cocoa @a notification to UICocoaApplication instance. */ 220 - (void) notificationCallbackOfObject :(NSNotification *)notification214 - (void) notificationCallbackOfObject :(NSNotification *)notification 221 215 { 222 216 /* Get current notification name: */ … … 241 235 242 236 /** Redirects cocoa @a notification to UICocoaApplication instance. */ 243 - (void) notificationCallbackOfWindow :(NSNotification *)notification237 - (void) notificationCallbackOfWindow :(NSNotification *)notification 244 238 { 245 239 /* Get current notification name: */ … … 251 245 252 246 /** Registers selector for standard window @a enmButtonType of the passed @a pWindow. */ 253 - (void)registerSelectorForStandardWindowButton :(NSWindow *)pWindow :(StandardWindowButtonType)enmButtonType247 - (void)registerSelectorForStandardWindowButton :(NSWindow *)pWindow :(StandardWindowButtonType)enmButtonType 254 248 { 255 249 /* Retrieve corresponding button: */ … … 269 263 if (pButton != Nil) 270 264 { 271 [pButton setTarget 272 [pButton setAction 265 [pButton setTarget:self]; 266 [pButton setAction:@selector(selectorForStandardWindowButton:)]; 273 267 } 274 268 } 275 269 276 270 /** Redirects selector of the standard window @a pButton to UICocoaApplication instance callback. */ 277 - (void)selectorForStandardWindowButton :(NSButton *)pButton271 - (void)selectorForStandardWindowButton :(NSButton *)pButton 278 272 { 279 273 /* Check if Option key is currently held: */ … … 285 279 @end /* @implementation UICocoaApplicationPrivate */ 286 280 287 /* C++ singleton for our private NSApplication object */ 288 UICocoaApplication* UICocoaApplication::m_pInstance = 0; 281 282 /********************************************************************************************************************************* 283 * Class UICocoaApplication implementation. * 284 *********************************************************************************************************************************/ 285 286 /* static */ 287 UICocoaApplication* UICocoaApplication::s_pInstance = 0; 289 288 290 289 /* static */ 291 290 UICocoaApplication* UICocoaApplication::instance() 292 291 { 293 if (! m_pInstance)294 m_pInstance = new UICocoaApplication();295 296 return m_pInstance;292 if (!s_pInstance) 293 s_pInstance = new UICocoaApplication; 294 295 return s_pInstance; 297 296 } 298 297 299 298 UICocoaApplication::UICocoaApplication() 300 299 { 301 /* Make sure our private NSApplication object is created */300 /* Make sure our private NSApplication object is created: */ 302 301 m_pNative = (UICocoaApplicationPrivate*)[UICocoaApplicationPrivate sharedApplication]; 303 /* Create one auto release pool which is in place for all the 304 initialization and deinitialization stuff. That is when the 305 NSApplication is not running the run loop (there is a separate auto 306 release pool defined). */ 302 // WORKAROUND": 303 // Create one auto release pool which is in place for all the 304 // initialization and deinitialization stuff. That is when the 305 // NSApplication is not running the run loop (there is a separate 306 // auto release pool defined). 307 307 m_pPool = [[NSAutoreleasePool alloc] init]; 308 308 } … … 394 394 } 395 395 396 void UICocoaApplication::nativeNotificationProxyForObject(NativeNSStringRef pstrNotificationName, const QMap<QString, QString> &userInfo) 396 void UICocoaApplication::nativeNotificationProxyForObject(NativeNSStringRef pstrNotificationName, 397 const QMap<QString, 398 QString> &userInfo) 397 399 { 398 400 /* Get notification name: */ … … 429 431 { 430 432 /* Make sure it is not registered yet: */ 431 AssertReturnVoid(!m_stdWindowButtonCallbacks.contains(pWidget) || !m_stdWindowButtonCallbacks.value(pWidget).contains(enmButtonType)); 433 AssertReturnVoid( !m_stdWindowButtonCallbacks.contains(pWidget) 434 || !m_stdWindowButtonCallbacks.value(pWidget).contains(enmButtonType)); 432 435 433 436 /* Remember callback: */ … … 442 445 { 443 446 /* Make sure it is registered yet: */ 444 AssertReturnVoid(m_stdWindowButtonCallbacks.contains(pWidget) && m_stdWindowButtonCallbacks.value(pWidget).contains(enmButtonType)); 447 AssertReturnVoid( m_stdWindowButtonCallbacks.contains(pWidget) 448 && m_stdWindowButtonCallbacks.value(pWidget).contains(enmButtonType)); 445 449 446 450 /* Forget callback: */ … … 452 456 void UICocoaApplication::nativeCallbackProxyForStandardWindowButton(NativeNSButtonRef pButton, bool fWithOptionKey) 453 457 { 458 // WORKAROUND: 454 459 // Why not using nested foreach, will you ask? 455 460 // It's because Qt 4.x has shadowing issue in Q_FOREACH macro. … … 461 466 { 462 467 QWidget *pWidget = widgets.at(iWidgetIndex); 463 const QMap<StandardWindowButtonType, PfnStandardWindowButtonCallbackForQWidget> callbacks = m_stdWindowButtonCallbacks.value(pWidget); 468 const QMap<StandardWindowButtonType, PfnStandardWindowButtonCallbackForQWidget> callbacks 469 = m_stdWindowButtonCallbacks.value(pWidget); 464 470 const QList<StandardWindowButtonType> buttonTypes = callbacks.keys(); 465 471 for (int iButtonTypeIndex = 0; iButtonTypeIndex < buttonTypes.size(); ++iButtonTypeIndex)
Note:
See TracChangeset
for help on using the changeset viewer.