src/gui/kernel/qcocoasharedwindowmethods_mac_p.h
changeset 3 41300fa6a67c
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     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 /****************************************************************************
       
    43  NB: This is not a header file, dispite the file name suffix. This file is
       
    44  included directly into the source code of qcocoawindow_mac.mm and
       
    45  qcocoapanel_mac.mm to avoid manually doing copy and paste of the exact
       
    46  same code needed at both places. This solution makes it more difficult
       
    47  to e.g fix a bug in qcocoawindow_mac.mm, but forget to do the same in
       
    48  qcocoapanel_mac.mm.
       
    49  The reason we need to do copy and paste in the first place, rather than
       
    50  resolve to method overriding, is that QCocoaPanel needs to inherit from
       
    51  NSPanel, while QCocoaWindow needs to inherit NSWindow rather than NSPanel).
       
    52 ****************************************************************************/
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 extern Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum); // qcocoaview.mm
       
    56 extern QPointer<QWidget> qt_button_down; //qapplication_mac.cpp
       
    57 QT_END_NAMESPACE
       
    58 
       
    59 - (BOOL)canBecomeKeyWindow
       
    60 {
       
    61     QWidget *widget = [self QT_MANGLE_NAMESPACE(qt_qwidget)];
       
    62 
       
    63     bool isToolTip = (widget->windowType() == Qt::ToolTip);
       
    64     bool isPopup = (widget->windowType() == Qt::Popup);
       
    65     return !(isPopup || isToolTip);
       
    66 }
       
    67 
       
    68 - (void)toggleToolbarShown:(id)sender
       
    69 {
       
    70     macSendToolbarChangeEvent([self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
    71     [super toggleToolbarShown:sender];
       
    72 }
       
    73 
       
    74 /*
       
    75     The methods keyDown, keyUp, and flagsChanged... These really shouldn't ever
       
    76     get hit. We automatically say we can be first responder if we are a window.
       
    77     So, the handling should get handled by the view. This is here more as a
       
    78     last resort (i.e., this is code that can potentially be removed).
       
    79  */
       
    80 - (void)keyDown:(NSEvent *)theEvent
       
    81 {
       
    82     bool keyOK = qt_dispatchKeyEvent(theEvent, [self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
    83     if (!keyOK)
       
    84         [super keyDown:theEvent];
       
    85 }
       
    86 
       
    87 - (void)keyUp:(NSEvent *)theEvent
       
    88 {
       
    89     bool keyOK = qt_dispatchKeyEvent(theEvent, [self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
    90     if (!keyOK)
       
    91         [super keyUp:theEvent];
       
    92 }
       
    93 
       
    94 - (void)flagsChanged:(NSEvent *)theEvent
       
    95 {
       
    96     qt_dispatchModifiersChanged(theEvent, [self QT_MANGLE_NAMESPACE(qt_qwidget)]);
       
    97     [super flagsChanged:theEvent];
       
    98 }
       
    99 
       
   100 
       
   101 - (void)tabletProximity:(NSEvent *)tabletEvent
       
   102 {
       
   103     qt_dispatchTabletProximityEvent(tabletEvent);
       
   104 }
       
   105 
       
   106 - (void)sendEvent:(NSEvent *)event
       
   107 {
       
   108     QWidget *widget = [[QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate] qt_qwidgetForWindow:self];
       
   109 
       
   110     // Cocoa can hold onto the window after we've disavowed its knowledge. So,
       
   111     // if we get sent an event afterwards just have it go through the super's
       
   112     // version and don't do any stuff with Qt.
       
   113     if (!widget) {
       
   114         [super sendEvent:event];
       
   115         return;
       
   116     }
       
   117 
       
   118     [self retain];
       
   119     QT_MANGLE_NAMESPACE(QCocoaView) *view = static_cast<QT_MANGLE_NAMESPACE(QCocoaView) *>(qt_mac_nativeview_for(widget));
       
   120     Qt::MouseButton mouseButton = cocoaButton2QtButton([event buttonNumber]);
       
   121 
       
   122     bool handled = false;
       
   123     // sometimes need to redirect mouse events to the popup.
       
   124     QWidget *popup = qAppInstance()->activePopupWidget();
       
   125     if (popup) {
       
   126         switch([event type])
       
   127         {
       
   128         case NSLeftMouseDown:
       
   129             if (!qt_button_down)
       
   130                 qt_button_down = widget;
       
   131             handled = qt_mac_handleMouseEvent(view, event, QEvent::MouseButtonPress, mouseButton);
       
   132             // Don't call super here. This prevents us from getting the mouseUp event,
       
   133             // which we need to send even if the mouseDown event was not accepted. 
       
   134             // (this is standard Qt behavior.)
       
   135             break;
       
   136         case NSRightMouseDown:
       
   137         case NSOtherMouseDown:
       
   138             if (!qt_button_down)
       
   139                 qt_button_down = widget;
       
   140             handled = qt_mac_handleMouseEvent(view, event, QEvent::MouseButtonPress, mouseButton);
       
   141             break;
       
   142         case NSLeftMouseUp:
       
   143         case NSRightMouseUp:
       
   144         case NSOtherMouseUp:
       
   145             handled = qt_mac_handleMouseEvent(view, event, QEvent::MouseButtonRelease, mouseButton);
       
   146             qt_button_down = 0;
       
   147             break;
       
   148         case NSMouseMoved:
       
   149             handled = qt_mac_handleMouseEvent(view, event, QEvent::MouseMove, Qt::NoButton);
       
   150             break;
       
   151         case NSLeftMouseDragged:
       
   152         case NSRightMouseDragged:
       
   153         case NSOtherMouseDragged:
       
   154             [QT_MANGLE_NAMESPACE(QCocoaView) currentMouseEvent]->view = view;
       
   155             [QT_MANGLE_NAMESPACE(QCocoaView) currentMouseEvent]->theEvent = event;
       
   156             handled = qt_mac_handleMouseEvent(view, event, QEvent::MouseMove, mouseButton);
       
   157             break;
       
   158         default:
       
   159             [super sendEvent:event];
       
   160             break;
       
   161         }
       
   162     } else {
       
   163         [super sendEvent:event];
       
   164     }
       
   165 
       
   166     if (!handled)
       
   167         qt_mac_dispatchNCMouseMessage(self, event, [self QT_MANGLE_NAMESPACE(qt_qwidget)], leftButtonIsRightButton);
       
   168 
       
   169     [self release];
       
   170 }
       
   171 
       
   172 - (BOOL)makeFirstResponder:(NSResponder *)responder
       
   173 {
       
   174     // For some reason Cocoa wants to flip the first responder
       
   175     // when Qt doesn't want to, sorry, but "No" :-)
       
   176     if (responder == nil && qApp->focusWidget())
       
   177         return NO;
       
   178     return [super makeFirstResponder:responder];
       
   179 }
       
   180 
       
   181 + (Class)frameViewClassForStyleMask:(NSUInteger)styleMask
       
   182 {
       
   183     if (styleMask & QtMacCustomizeWindow)
       
   184         return [QT_MANGLE_NAMESPACE(QCocoaWindowCustomThemeFrame) class];
       
   185     return [super frameViewClassForStyleMask:styleMask];
       
   186 }
       
   187