src/gui/kernel/qcocoawindow_mac.mm
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qmacdefines_mac.h"
       
    43 #ifdef QT_MAC_USE_COCOA
       
    44 #import <private/qcocoawindow_mac_p.h>
       
    45 #import <private/qcocoawindowdelegate_mac_p.h>
       
    46 #import <private/qcocoaview_mac_p.h>
       
    47 #import <private/qt_cocoa_helpers_mac_p.h>
       
    48 #import <private/qcocoawindowcustomthemeframe_mac_p.h>
       
    49 
       
    50 #include <QtGui/QWidget>
       
    51 
       
    52 QT_FORWARD_DECLARE_CLASS(QWidget);
       
    53 QT_BEGIN_NAMESPACE
       
    54 extern Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum); // qcocoaview.mm
       
    55 QT_END_NAMESPACE
       
    56 QT_USE_NAMESPACE
       
    57 
       
    58 @interface NSWindow (QtCoverForHackWithCategory)
       
    59 + (Class)frameViewClassForStyleMask:(NSUInteger)styleMask;
       
    60 @end
       
    61 
       
    62 @implementation NSWindow (QT_MANGLE_NAMESPACE(QWidgetIntegration))
       
    63 
       
    64 - (id)QT_MANGLE_NAMESPACE(qt_initWithQWidget):(QWidget*)widget contentRect:(NSRect)rect styleMask:(NSUInteger)mask;
       
    65 {
       
    66     self = [self initWithContentRect:rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
       
    67     if (self) {
       
    68         [[QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate] becomeDelegteForWindow:self widget:widget];
       
    69         [self setReleasedWhenClosed:NO];
       
    70     }
       
    71     return self;
       
    72 }
       
    73 
       
    74 - (QWidget *)QT_MANGLE_NAMESPACE(qt_qwidget)
       
    75 {
       
    76     QWidget *widget = 0;
       
    77     if ([self delegate] == [QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate])
       
    78         widget = [[QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate] qt_qwidgetForWindow:self];
       
    79     return widget;
       
    80 }
       
    81 
       
    82 @end
       
    83 
       
    84 @implementation QT_MANGLE_NAMESPACE(QCocoaWindow)
       
    85 
       
    86 - (BOOL)canBecomeKeyWindow
       
    87 {
       
    88     return YES;
       
    89 }
       
    90 
       
    91 /***********************************************************************
       
    92   BEGIN Copy and Paste between QCocoaWindow and QCocoaPanel
       
    93   This is a bit unfortunate, but thanks to the dynamic dispatch we
       
    94   have to duplicate this code or resort to really silly forwarding methods
       
    95 **************************************************************************/
       
    96 
       
    97 /*
       
    98     The methods keyDown, keyUp, and flagsChanged... These really shouldn't ever
       
    99     get hit. We automatically say we can be first responder if we are a window.
       
   100     So, the handling should get handled by the view. This is here more as a
       
   101     last resort (i.e., this is code that can potentially be removed).
       
   102  */
       
   103 
       
   104 - (void)toggleToolbarShown:(id)sender
       
   105 {
       
   106     macSendToolbarChangeEvent([self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
   107     [super toggleToolbarShown:sender];
       
   108 }
       
   109 
       
   110 - (void)keyDown:(NSEvent *)theEvent
       
   111 {
       
   112     bool keyOK = qt_dispatchKeyEvent(theEvent, [self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
   113     if (!keyOK)
       
   114         [super keyDown:theEvent];
       
   115 }
       
   116 
       
   117 - (void)keyUp:(NSEvent *)theEvent
       
   118 {
       
   119     bool keyOK = qt_dispatchKeyEvent(theEvent, [self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
   120     if (!keyOK)
       
   121         [super keyUp:theEvent];
       
   122 }
       
   123 
       
   124 - (void)flagsChanged:(NSEvent *)theEvent
       
   125 {
       
   126     qt_dispatchModifiersChanged(theEvent, [self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
   127     [super flagsChanged:theEvent];
       
   128 }
       
   129 
       
   130 
       
   131 - (void)tabletProximity:(NSEvent *)tabletEvent
       
   132 {
       
   133     qt_dispatchTabletProximityEvent(tabletEvent);
       
   134 }
       
   135 
       
   136 - (void)sendEvent:(NSEvent *)event
       
   137 {
       
   138     QWidget *widget = [[QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate] qt_qwidgetForWindow:self];
       
   139 
       
   140     // Cocoa can hold onto the window after we've disavowed its knowledge. So,
       
   141     // if we get sent an event afterwards just have it go through the super's
       
   142     // version and don't do any stuff with Qt.
       
   143     if (!widget) {
       
   144         [super sendEvent:event];
       
   145         return;
       
   146     }
       
   147 
       
   148     [self retain];
       
   149     QT_MANGLE_NAMESPACE(QCocoaView) *view = static_cast<QT_MANGLE_NAMESPACE(QCocoaView) *>(qt_mac_nativeview_for(widget));
       
   150     Qt::MouseButton mouseButton = cocoaButton2QtButton([event buttonNumber]);
       
   151     // sometimes need to redirect mouse events to the popup.
       
   152     QWidget *popup = qAppInstance()->activePopupWidget();
       
   153     if (popup && popup != widget) {
       
   154         switch([event type])
       
   155         {
       
   156         case NSLeftMouseDown:
       
   157             qt_mac_handleMouseEvent(view, event, QEvent::MouseButtonPress, mouseButton);
       
   158             // Don't call super here. This prevents us from getting the mouseUp event,
       
   159             // which we need to send even if the mouseDown event was not accepted. 
       
   160             // (this is standard Qt behavior.)
       
   161             break;
       
   162         case NSRightMouseDown:
       
   163         case NSOtherMouseDown:
       
   164             if (!qt_mac_handleMouseEvent(view, event, QEvent::MouseButtonPress, mouseButton))
       
   165                 [super sendEvent:event];
       
   166             break;
       
   167         case NSLeftMouseUp:
       
   168         case NSRightMouseUp:
       
   169         case NSOtherMouseUp:
       
   170             if (!qt_mac_handleMouseEvent(view, event, QEvent::MouseButtonRelease, mouseButton))
       
   171                 [super sendEvent:event];
       
   172             break;
       
   173         case NSMouseMoved:
       
   174             qt_mac_handleMouseEvent(view, event, QEvent::MouseMove, Qt::NoButton);
       
   175             break;
       
   176         case NSLeftMouseDragged:
       
   177         case NSRightMouseDragged:
       
   178         case NSOtherMouseDragged:
       
   179             [QT_MANGLE_NAMESPACE(QCocoaView) currentMouseEvent]->view = view;
       
   180             [QT_MANGLE_NAMESPACE(QCocoaView) currentMouseEvent]->theEvent = event;
       
   181             if (!qt_mac_handleMouseEvent(view, event, QEvent::MouseMove, mouseButton))
       
   182                 [super sendEvent:event];
       
   183             break;
       
   184         default:
       
   185             [super sendEvent:event];
       
   186             break;
       
   187         }
       
   188     } else {
       
   189         [super sendEvent:event];
       
   190     }
       
   191     qt_mac_dispatchNCMouseMessage(self, event, [self QT_MANGLE_NAMESPACE(qt_qwidget)], leftButtonIsRightButton);
       
   192 
       
   193 
       
   194     [self release];
       
   195 }
       
   196 
       
   197 
       
   198 - (BOOL)makeFirstResponder:(NSResponder *)responder
       
   199 {
       
   200     // For some reason Cocoa wants to flip the first responder
       
   201     // when Qt doesn't want to, sorry, but "No" :-)
       
   202     if (responder == nil && qApp->focusWidget())
       
   203         return NO;
       
   204     return [super makeFirstResponder:responder];
       
   205 }
       
   206 
       
   207 /***********************************************************************
       
   208   END Copy and Paste between QCocoaWindow and QCocoaPanel
       
   209 ***********************************************************************/
       
   210 
       
   211 + (Class)frameViewClassForStyleMask:(NSUInteger)styleMask
       
   212 {
       
   213     if (styleMask & QtMacCustomizeWindow)
       
   214         return [QT_MANGLE_NAMESPACE(QCocoaWindowCustomThemeFrame) class];
       
   215     return [super frameViewClassForStyleMask:styleMask];
       
   216 }
       
   217 
       
   218 @end
       
   219 
       
   220 #endif