VirtualBox

Changeset 71661 in vbox


Ignore:
Timestamp:
Apr 4, 2018 3:16:37 PM (7 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9049: Full and heavy cleanup for UICocoaApplication.

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  
    11/* $Id$ */
    22/** @file
    3  * VBox Qt GUI - UICocoaApplication - C++ interface to NSApplication for handling -sendEvent.
     3 * VBox Qt GUI - UICocoaApplication class declaration.
    44 */
    55
    66/*
    7  * Copyright (C) 2009-2017 Oracle Corporation
     7 * Copyright (C) 2009-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1616 */
    1717
    18 #ifndef ___darwin_VBoxCocoaApplication_h
    19 #define ___darwin_VBoxCocoaApplication_h
     18#ifndef ___VBoxCocoaApplication_h___
     19#define ___VBoxCocoaApplication_h___
    2020
    2121/* Qt includes: */
     
    2626#include "VBoxUtils-darwin.h"
    2727
     28/* Forward declarations: */
     29class QObject;
     30class QWidget;
     31
     32/* Cocoa declarations: */
    2833ADD_COCOA_NATIVE_REF(UICocoaApplicationPrivate);
    2934ADD_COCOA_NATIVE_REF(NSAutoreleasePool);
     
    3237ADD_COCOA_NATIVE_REF(NSButton);
    3338
    34 /* Forward declarations: */
    35 class QObject;
    36 class QWidget;
    3739
    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. */
    4441typedef bool (*PFNVBOXCACALLBACK)(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
    4542
     
    5148typedef void (*PfnStandardWindowButtonCallbackForQWidget)(StandardWindowButtonType emnButtonType, bool fWithOptionKey, QWidget *pWidget);
    5249
    53 /* C++ singleton for our private NSApplication object. */
     50
     51/** Singleton prototype for our private NSApplication object. */
    5452class UICocoaApplication
    5553{
    5654public:
    5755
    58     /** Returns singleton access instance. */
    59     static UICocoaApplication* instance();
     56    /** Returns singleton instance. */
     57    static UICocoaApplication *instance();
    6058
    61     /** Destructor. */
    62     ~UICocoaApplication();
     59    /** Destructs cocoa application. */
     60    virtual ~UICocoaApplication();
    6361
    6462    /** Returns whether application is currently active. */
     
    9795private:
    9896
    99     /** Constructor. */
     97    /** Constructs cocoa application. */
    10098    UICocoaApplication();
    10199
    102100    /** Holds the singleton access instance. */
    103     static UICocoaApplication *m_pInstance;
     101    static UICocoaApplication *s_pInstance;
    104102
    105103    /** Holds the private NSApplication instance. */
    106     NativeUICocoaApplicationPrivateRef m_pNative;
     104    NativeUICocoaApplicationPrivateRef  m_pNative;
    107105    /** Holds the private NSAutoreleasePool instance. */
    108     NativeNSAutoreleasePoolRef m_pPool;
     106    NativeNSAutoreleasePoolRef          m_pPool;
    109107
    110108    /** 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;
    112110    /** 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;
    114112
    115113    /** 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;
    117115};
    118116
    119 #endif /* ___darwin_VBoxCocoaApplication_h */
     117#endif /* !___VBoxCocoaApplication_h___ */
    120118
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UICocoaApplication.mm

    r69500 r71661  
    11/* $Id$ */
    22/** @file
    3  * VBox Qt GUI - UICocoaApplication - C++ interface to NSApplication for handling -sendEvent.
     3 * VBox Qt GUI - UICocoaApplication class implementation.
    44 */
    55
    66/*
    7  * Copyright (C) 2009-2017 Oracle Corporation
     7 * Copyright (C) 2009-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1919#include "UICocoaApplication.h"
    2020
    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>
    2227#import <AppKit/NSEvent.h>
    23 #import <AppKit/NSApplication.h>
     28#import <AppKit/NSWindow.h>
    2429#import <Foundation/NSArray.h>
    25 #import <AppKit/NSWindow.h>
    26 #import <AppKit/NSButton.h>
    27 
    28 #include <iprt/assert.h>
     30
    2931
    3032/** Class for tracking a callback. */
    31 @interface CallbackData: NSObject
     33@interface CallbackData : NSObject
    3234{
    3335@public
    34     /** Mask of events to send to this callback. */
    35     uint32_t            fMask;
    36     /** The callback. */
    37     PFNVBOXCACALLBACK   pfnCallback;
    38     /** The user argument. */
    39     void               *pvUser;
    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;
    4244@end /* @interface CallbackData  */
    4345
    4446@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
    4649{
    4750    self = [super init];
     
    5053        fMask = mask;
    5154        pfnCallback = callback;
    52         pvUser =  user;
     55        pvUser = user;
    5356    }
    5457    return self;
     
    5659@end /* @implementation CallbackData  */
    5760
    58 /** Class for event handling */
    59 @interface UICocoaApplicationPrivate: NSApplication
     61
     62/** Class for event handling. */
     63@interface UICocoaApplicationPrivate : NSApplication
    6064{
    6165    /** The event mask for which there currently are callbacks. */
     
    6569}
    6670- (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;
    8286@end /* @interface UICocoaApplicationPrivate */
    8387
    8488@implementation UICocoaApplicationPrivate
    85 -(id) init
     89/** Performs initialization. */
     90- (id) init
    8691{
    8792    self = [super init];
     
    8994        m_pCallbacks = [[NSMutableArray alloc] init];
    9095
    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.
    9399    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSFullScreenMenuItemEverywhere"];
    94100
     
    96102}
    97103
    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. */
    103109    uint32_t const fMask = m_fMask;
    104110#if 0 /* for debugging */
     
    111117        if (fMask & fEvtMask)
    112118        {
    113             /*
    114              * Do the callouts in LIFO order.
    115              */
     119            /* Do the callouts in LIFO order. */
    116120            for (CallbackData *pData in [m_pCallbacks reverseObjectEnumerator])
    117121            {
     
    126130    }
    127131
    128     /*
    129      * Get on with it.
    130      */
     132    /* Get on with it. */
    131133    [super sendEvent:pEvent];
    132134}
    133135
    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: */
    148147    m_fMask |= fMask;
    149148}
    150149
    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. */
    163157    for (CallbackData *pData in [m_pCallbacks reverseObjectEnumerator])
    164158    {
     
    167161            && pData->fMask       == fMask)
    168162        {
    169             [m_pCallbacks removeObject: pData];
     163            [m_pCallbacks removeObject:pData];
    170164            break;
    171165        }
     
    177171}
    178172
    179 /** Register to cocoa notification @a pstrNotificationName. */
    180 - (void) registerToNotificationOfWorkspace :(NSString*)pstrNotificationName
     173/** Registers to cocoa notification @a pstrNotificationName. */
     174- (void) registerToNotificationOfWorkspace :(NSString *)pstrNotificationName
    181175{
    182176    /* Register notification observer: */
     
    189183
    190184/** Unregister @a pWindow from cocoa notification @a pstrNotificationName. */
    191 - (void) unregisterFromNotificationOfWorkspace :(NSString*)pstrNotificationName
     185- (void) unregisterFromNotificationOfWorkspace :(NSString *)pstrNotificationName
    192186{
    193187    /* Uninstall notification observer: */
     
    199193
    200194/** Register @a pWindow to cocoa notification @a pstrNotificationName. */
    201 - (void) registerToNotificationOfWindow :(NSString*)pstrNotificationName :(NSWindow*)pWindow
     195- (void) registerToNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow *)pWindow
    202196{
    203197    /* Register notification observer: */
     
    209203
    210204/** Unregister @a pWindow from cocoa notification @a pstrNotificationName. */
    211 - (void) unregisterFromNotificationOfWindow :(NSString*)pstrNotificationName :(NSWindow*)pWindow
     205- (void) unregisterFromNotificationOfWindow :(NSString *)pstrNotificationName :(NSWindow *)pWindow
    212206{
    213207    /* Uninstall notification observer: */
     
    218212
    219213/** Redirects cocoa @a notification to UICocoaApplication instance. */
    220 - (void) notificationCallbackOfObject :(NSNotification*)notification
     214- (void) notificationCallbackOfObject :(NSNotification *)notification
    221215{
    222216    /* Get current notification name: */
     
    241235
    242236/** Redirects cocoa @a notification to UICocoaApplication instance. */
    243 - (void) notificationCallbackOfWindow :(NSNotification*)notification
     237- (void) notificationCallbackOfWindow :(NSNotification *)notification
    244238{
    245239    /* Get current notification name: */
     
    251245
    252246/** Registers selector for standard window @a enmButtonType of the passed @a pWindow. */
    253 - (void)registerSelectorForStandardWindowButton :(NSWindow*)pWindow :(StandardWindowButtonType)enmButtonType
     247- (void)registerSelectorForStandardWindowButton :(NSWindow *)pWindow :(StandardWindowButtonType)enmButtonType
    254248{
    255249    /* Retrieve corresponding button: */
     
    269263    if (pButton != Nil)
    270264    {
    271         [pButton setTarget :self];
    272         [pButton setAction :@selector(selectorForStandardWindowButton:)];
     265        [pButton setTarget:self];
     266        [pButton setAction:@selector(selectorForStandardWindowButton:)];
    273267    }
    274268}
    275269
    276270/** Redirects selector of the standard window @a pButton to UICocoaApplication instance callback. */
    277 - (void)selectorForStandardWindowButton :(NSButton*)pButton
     271- (void)selectorForStandardWindowButton :(NSButton *)pButton
    278272{
    279273    /* Check if Option key is currently held: */
     
    285279@end /* @implementation UICocoaApplicationPrivate */
    286280
    287 /* C++ singleton for our private NSApplication object */
    288 UICocoaApplication* UICocoaApplication::m_pInstance = 0;
     281
     282/*********************************************************************************************************************************
     283*   Class UICocoaApplication implementation.                                                                                     *
     284*********************************************************************************************************************************/
     285
     286/* static */
     287UICocoaApplication* UICocoaApplication::s_pInstance = 0;
    289288
    290289/* static */
    291290UICocoaApplication* UICocoaApplication::instance()
    292291{
    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;
    297296}
    298297
    299298UICocoaApplication::UICocoaApplication()
    300299{
    301     /* Make sure our private NSApplication object is created */
     300    /* Make sure our private NSApplication object is created: */
    302301    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).
    307307    m_pPool = [[NSAutoreleasePool alloc] init];
    308308}
     
    394394}
    395395
    396 void UICocoaApplication::nativeNotificationProxyForObject(NativeNSStringRef pstrNotificationName, const QMap<QString, QString> &userInfo)
     396void UICocoaApplication::nativeNotificationProxyForObject(NativeNSStringRef pstrNotificationName,
     397                                                          const QMap<QString,
     398                                                          QString> &userInfo)
    397399{
    398400    /* Get notification name: */
     
    429431{
    430432    /* 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));
    432435
    433436    /* Remember callback: */
     
    442445{
    443446    /* 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));
    445449
    446450    /* Forget callback: */
     
    452456void UICocoaApplication::nativeCallbackProxyForStandardWindowButton(NativeNSButtonRef pButton, bool fWithOptionKey)
    453457{
     458    // WORKAROUND:
    454459    // Why not using nested foreach, will you ask?
    455460    // It's because Qt 4.x has shadowing issue in Q_FOREACH macro.
     
    461466    {
    462467        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);
    464470        const QList<StandardWindowButtonType> buttonTypes = callbacks.keys();
    465471        for (int iButtonTypeIndex = 0; iButtonTypeIndex < buttonTypes.size(); ++iButtonTypeIndex)
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