VirtualBox

Ignore:
Timestamp:
Feb 18, 2014 12:11:28 PM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
92332
Message:

FE/Qt: Mac OS X: 7016: UICocoaApplication: Native notification handling framework.

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

    r49595 r50491  
    1818#define ___darwin_VBoxCocoaApplication_h
    1919
     20/* Qt includes: */
     21#include <QMap>
     22
     23/* GUI includes: */
    2024#include "VBoxCocoaHelper.h"
     25
    2126ADD_COCOA_NATIVE_REF(UICocoaApplicationPrivate);
    2227ADD_COCOA_NATIVE_REF(NSAutoreleasePool);
     28ADD_COCOA_NATIVE_REF(NSString);
     29ADD_COCOA_NATIVE_REF(NSWindow);
     30
     31/* Forward declarations: */
     32class QWidget;
    2333
    2434/** Event handler callback.
     
    2939 */
    3040typedef bool (*PFNVBOXCACALLBACK)(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
     41
     42/** Native notification callback type for QWidget. */
     43typedef void (*PfnNativeNotificationCallbackForQWidget)(const QString &strNativeNotificationName, QWidget *pWidget);
    3144
    3245/* C++ singleton for our private NSApplication object */
     
    4154    void unregisterForNativeEvents(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
    4255
     56    /** Register passed @a pWidget to native notification @a strNativeNotificationName, using @a pCallback as handler. */
     57    void registerToNativeNotification(const QString &strNativeNotificationName, QWidget *pWidget, PfnNativeNotificationCallbackForQWidget pCallback);
     58    /** Unregister passed @a pWidget from native notification @a strNativeNotificationName. */
     59    void unregisterFromNativeNotification(const QString &strNativeNotificationName, QWidget *pWidget);
     60    /** Redirects native notification @a pstrNativeNotificationName for window @a pWindow to registered listener. */
     61    void nativeNotificationProxy(NativeNSStringRef pstrNativeNotificationName, NativeNSWindowRef pWindow);
     62
    4363private:
    4464    UICocoaApplication();
     
    4666    NativeUICocoaApplicationPrivateRef m_pNative;
    4767    NativeNSAutoreleasePoolRef m_pPool;
     68
     69    /** Map of notification callbacks registered for corresponding QWidget(s). */
     70    QMap<QWidget*, QMap<QString, PfnNativeNotificationCallbackForQWidget> > m_callbacks;
    4871};
    4972
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UICocoaApplication.mm

    r49595 r50491  
    1616 */
    1717
    18 /* Local includes */
     18/* GUI includes: */
    1919#include "UICocoaApplication.h"
    2020#include "VBoxUtils-darwin.h"
     
    6868- (void)setCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
    6969- (void)unsetCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
     70
     71- (void)registerToNotification :(NSString*)pstrNotificationName :(NSWindow*)pWindow;
     72- (void)unregisterFromNotification :(NSString*)pstrNotificationName :(NSWindow*)pWindow;
     73- (void)notificationCallback :(NSNotification*)notification;
    7074@end /* @interface UICocoaApplicationPrivate */
    7175
     
    160164    m_fMask = fNewMask;
    161165}
     166
     167/** Register @a pWindow to cocoa notification @a pstrNotificationName. */
     168- (void) registerToNotification :(NSString*)pstrNotificationName :(NSWindow*)pWindow
     169{
     170    /* Register notification observer: */
     171    [[NSNotificationCenter defaultCenter] addObserver:self
     172                                             selector:@selector(notificationCallback:)
     173                                                 name:pstrNotificationName
     174                                               object:pWindow];
     175}
     176
     177/** Unregister @a pWindow from cocoa notification @a pstrNotificationName. */
     178- (void) unregisterFromNotification :(NSString*)pstrNotificationName :(NSWindow*)pWindow
     179{
     180    /* Uninstall notification observer: */
     181    [[NSNotificationCenter defaultCenter] removeObserver:self
     182                                                    name:pstrNotificationName
     183                                                  object:pWindow];
     184}
     185
     186/** Redirects cocoa @a notification to UICocoaApplication instance. */
     187- (void) notificationCallback :(NSNotification*)notification
     188{
     189    /* Get current notification name: */
     190    NSString *pstrName = [notification name];
     191
     192    /* Define known notification names: */
     193    NSString *spstrWillEnterFullscreenNotification = @"NSWindowWillEnterFullScreenNotification";
     194    NSString *spstrDidEnterFullscreenNotification  = @"NSWindowDidEnterFullScreenNotification";
     195    NSString *spstrWillExitFullscreenNotification  = @"NSWindowWillExitFullScreenNotification";
     196    NSString *spstrDidExitFullscreenNotification   = @"NSWindowDidExitFullScreenNotification";
     197
     198    /* Redirect known notifications to UICocoaApplication instance: */
     199    if (   [pstrName isEqualToString :spstrWillEnterFullscreenNotification]
     200        || [pstrName isEqualToString :spstrDidEnterFullscreenNotification]
     201        || [pstrName isEqualToString :spstrWillExitFullscreenNotification]
     202        || [pstrName isEqualToString :spstrDidExitFullscreenNotification])
     203        UICocoaApplication::instance()->nativeNotificationProxy(pstrName, [notification object]);
     204}
    162205@end /* @implementation UICocoaApplicationPrivate */
    163206
     
    206249}
    207250
     251void UICocoaApplication::registerToNativeNotification(const QString &strNativeNotificationName, QWidget *pWidget, PfnNativeNotificationCallbackForQWidget pCallback)
     252{
     253    /* Make sure it is not registered yet: */
     254    AssertReturnVoid(!m_callbacks.contains(pWidget) || !m_callbacks[pWidget].contains(strNativeNotificationName));
     255
     256    /* Remember callback: */
     257    m_callbacks[pWidget][strNativeNotificationName] = pCallback;
     258
     259    /* Register observer: */
     260    NativeNSStringRef pstrNativeNotificationName = darwinToNativeString(strNativeNotificationName.toLatin1().constData());
     261    NativeNSWindowRef pWindow = darwinToNativeWindow(pWidget);
     262    [m_pNative registerToNotification :pstrNativeNotificationName :pWindow];
     263}
     264
     265void UICocoaApplication::unregisterFromNativeNotification(const QString &strNativeNotificationName, QWidget *pWidget)
     266{
     267    /* Make sure it is registered yet: */
     268    AssertReturnVoid(m_callbacks.contains(pWidget) && m_callbacks[pWidget].contains(strNativeNotificationName));
     269
     270    /* Forget callback: */
     271    m_callbacks[pWidget].remove(strNativeNotificationName);
     272    if (m_callbacks[pWidget].isEmpty())
     273        m_callbacks.remove(pWidget);
     274
     275    /* Unregister observer: */
     276    NativeNSStringRef pstrNativeNotificationName = darwinToNativeString(strNativeNotificationName.toLatin1().constData());
     277    NativeNSWindowRef pWindow = darwinToNativeWindow(pWidget);
     278    [m_pNative unregisterFromNotification :pstrNativeNotificationName :pWindow];
     279}
     280
     281void UICocoaApplication::nativeNotificationProxy(NativeNSStringRef pstrNotificationName, NativeNSWindowRef pWindow)
     282{
     283    /* Get notification name: */
     284    QString strNotificationName = darwinFromNativeString(pstrNotificationName);
     285
     286    /* Check if existing widget(s) have corresponding notification handler: */
     287    const QList<QWidget*> &keys1 = m_callbacks.keys();
     288    for (int i = 0; i < keys1.size(); ++i)
     289    {
     290        QWidget *pWidget = keys1[i];
     291        if (darwinToNativeWindow(pWidget) == pWindow)
     292        {
     293            const QMap<QString, PfnNativeNotificationCallbackForQWidget> &callbacks = m_callbacks[pWidget];
     294            if (callbacks.contains(strNotificationName))
     295                callbacks[strNotificationName](strNotificationName, pWidget);
     296        }
     297    }
     298}
     299
Note: See TracChangeset for help on using the changeset viewer.

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