VirtualBox

Changeset 52511 in vbox


Ignore:
Timestamp:
Aug 28, 2014 11:59:52 AM (10 years ago)
Author:
vboxsync
Message:

FE/Qt: Mac OS X: Runtime UI: UICocoaApplication API extension: Allow to check whether NSApplication is active; Allow to register NSWorkspace notification listeners.

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

    r52510 r52511  
    3030
    3131/* Forward declarations: */
     32class QObject;
    3233class QWidget;
    3334
     
    4041typedef bool (*PFNVBOXCACALLBACK)(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
    4142
     43/** Native notification callback type for QObject. */
     44typedef void (*PfnNativeNotificationCallbackForQObject)(QObject *pObject, const QMap<QString, QString> &userInfo);
    4245/** Native notification callback type for QWidget. */
    4346typedef void (*PfnNativeNotificationCallbackForQWidget)(const QString &strNativeNotificationName, QWidget *pWidget);
     
    5457    ~UICocoaApplication();
    5558
     59    /** Returns whether application is currently active. */
     60    bool isActive() const;
     61
    5662    /** Hides the application. */
    5763    void hide();
     
    6268    void unregisterForNativeEvents(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
    6369
     70    /** Register passed @a pObject to native notification @a strNativeNotificationName, using @a pCallback as handler. */
     71    void registerToNotificationOfWorkspace(const QString &strNativeNotificationName, QObject *pObject, PfnNativeNotificationCallbackForQObject pCallback);
     72    /** Unregister passed @a pWidget from native notification @a strNativeNotificationName. */
     73    void unregisterFromNotificationOfWorkspace(const QString &strNativeNotificationName, QObject *pObject);
     74
    6475    /** Register passed @a pWidget to native notification @a strNativeNotificationName, using @a pCallback as handler. */
    6576    void registerToNotificationOfWindow(const QString &strNativeNotificationName, QWidget *pWidget, PfnNativeNotificationCallbackForQWidget pCallback);
     
    6778    void unregisterFromNotificationOfWindow(const QString &strNativeNotificationName, QWidget *pWidget);
    6879
     80    /** Redirects native notification @a pstrNativeNotificationName for application to registered listener. */
     81    void nativeNotificationProxyForObject(NativeNSStringRef pstrNativeNotificationName, const QMap<QString, QString> &userInfo);
    6982    /** Redirects native notification @a pstrNativeNotificationName for window @a pWindow to registered listener. */
    7083    void nativeNotificationProxyForWidget(NativeNSStringRef pstrNativeNotificationName, NativeNSWindowRef pWindow);
     
    8396    NativeNSAutoreleasePoolRef m_pPool;
    8497
     98    /** Map of notification callbacks registered for corresponding QObject(s). */
     99    QMap<QObject*, QMap<QString, PfnNativeNotificationCallbackForQObject> > m_objectCallbacks;
    85100    /** Map of notification callbacks registered for corresponding QWidget(s). */
    86101    QMap<QWidget*, QMap<QString, PfnNativeNotificationCallbackForQWidget> > m_widgetCallbacks;
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UICocoaApplication.mm

    r52510 r52511  
    6969- (void)unsetCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
    7070
     71- (void)registerToNotificationOfWorkspace :(NSString*)pstrNotificationName;
     72- (void)unregisterFromNotificationOfWorkspace :(NSString*)pstrNotificationName;
     73
    7174- (void)registerToNotificationOfWindow :(NSString*)pstrNotificationName :(NSWindow*)pWindow;
    7275- (void)unregisterFromNotificationOfWindow :(NSString*)pstrNotificationName :(NSWindow*)pWindow;
    7376
     77- (void)notificationCallbackOfObject :(NSNotification*)notification;
    7478- (void)notificationCallbackOfWindow :(NSNotification*)notification;
    7579@end /* @interface UICocoaApplicationPrivate */
     
    166170}
    167171
     172/** Register to cocoa notification @a pstrNotificationName. */
     173- (void) registerToNotificationOfWorkspace :(NSString*)pstrNotificationName
     174{
     175    /* Register notification observer: */
     176    NSNotificationCenter *pNotificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
     177    [pNotificationCenter addObserver:self
     178                            selector:@selector(notificationCallbackOfObject:)
     179                                name:pstrNotificationName
     180                              object:nil];
     181}
     182
     183/** Unregister @a pWindow from cocoa notification @a pstrNotificationName. */
     184- (void) unregisterFromNotificationOfWorkspace :(NSString*)pstrNotificationName
     185{
     186    /* Uninstall notification observer: */
     187    NSNotificationCenter *pNotificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
     188    [pNotificationCenter removeObserver:self
     189                                   name:pstrNotificationName
     190                                 object:nil];
     191}
     192
    168193/** Register @a pWindow to cocoa notification @a pstrNotificationName. */
    169194- (void) registerToNotificationOfWindow :(NSString*)pstrNotificationName :(NSWindow*)pWindow
     
    183208                                                    name:pstrNotificationName
    184209                                                  object:pWindow];
     210}
     211
     212/** Redirects cocoa @a notification to UICocoaApplication instance. */
     213- (void) notificationCallbackOfObject :(NSNotification*)notification
     214{
     215    /* Get current notification name: */
     216    NSString *pstrName = [notification name];
     217
     218    /* Prepare user-info: */
     219    QMap<QString, QString> userInfo;
     220
     221    /* Process known notifications: */
     222    if (   [pstrName isEqualToString :@"NSWorkspaceDidActivateApplicationNotification"]
     223        || [pstrName isEqualToString :@"NSWorkspaceDidDeactivateApplicationNotification"])
     224    {
     225        NSDictionary *pUserInfo = [notification userInfo];
     226        NSRunningApplication *pApplication = [pUserInfo valueForKey :@"NSWorkspaceApplicationKey"];
     227        NSString *pstrBundleIndentifier = [pApplication bundleIdentifier];
     228        userInfo.insert("BundleIdentifier", darwinFromNativeString((NativeNSStringRef)pstrBundleIndentifier));
     229    }
     230
     231    /* Redirect known notifications to objects: */
     232    UICocoaApplication::instance()->nativeNotificationProxyForObject(pstrName, userInfo);
    185233}
    186234
     
    225273}
    226274
     275bool UICocoaApplication::isActive() const
     276{
     277    return [m_pNative isActive];
     278}
     279
    227280void UICocoaApplication::hide()
    228281{
     
    238291{
    239292    [m_pNative unsetCallback:fMask :pfnCallback :pvUser];
     293}
     294
     295void UICocoaApplication::registerToNotificationOfWorkspace(const QString &strNativeNotificationName, QObject *pObject,
     296                                                           PfnNativeNotificationCallbackForQObject pCallback)
     297{
     298    /* Make sure it is not registered yet: */
     299    AssertReturnVoid(!m_objectCallbacks.contains(pObject) || !m_objectCallbacks[pObject].contains(strNativeNotificationName));
     300
     301    /* Remember callback: */
     302    m_objectCallbacks[pObject][strNativeNotificationName] = pCallback;
     303
     304    /* Register observer: */
     305    NativeNSStringRef pstrNativeNotificationName = darwinToNativeString(strNativeNotificationName.toLatin1().constData());
     306    [m_pNative registerToNotificationOfWorkspace :pstrNativeNotificationName];
     307}
     308
     309void UICocoaApplication::unregisterFromNotificationOfWorkspace(const QString &strNativeNotificationName, QObject *pObject)
     310{
     311    /* Make sure it is registered yet: */
     312    AssertReturnVoid(m_objectCallbacks.contains(pObject) && m_objectCallbacks[pObject].contains(strNativeNotificationName));
     313
     314    /* Forget callback: */
     315    m_objectCallbacks[pObject].remove(strNativeNotificationName);
     316    if (m_objectCallbacks[pObject].isEmpty())
     317        m_objectCallbacks.remove(pObject);
     318
     319    /* Unregister observer: */
     320    NativeNSStringRef pstrNativeNotificationName = darwinToNativeString(strNativeNotificationName.toLatin1().constData());
     321    [m_pNative unregisterFromNotificationOfWorkspace :pstrNativeNotificationName];
    240322}
    241323
     
    269351    NativeNSWindowRef pWindow = darwinToNativeWindow(pWidget);
    270352    [m_pNative unregisterFromNotificationOfWindow :pstrNativeNotificationName :pWindow];
     353}
     354
     355void UICocoaApplication::nativeNotificationProxyForObject(NativeNSStringRef pstrNotificationName, const QMap<QString, QString> &userInfo)
     356{
     357    /* Get notification name: */
     358    QString strNotificationName = darwinFromNativeString(pstrNotificationName);
     359
     360    /* Check if existing object(s) have corresponding notification handler: */
     361    foreach (QObject *pObject, m_objectCallbacks.keys())
     362    {
     363        const QMap<QString, PfnNativeNotificationCallbackForQObject> &callbacks = m_objectCallbacks[pObject];
     364        if (callbacks.contains(strNotificationName))
     365            callbacks[strNotificationName](pObject, userInfo);
     366    }
    271367}
    272368
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette