Qt 4.8
qcocoaapplication_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 /****************************************************************************
43 **
44 ** Copyright (c) 2007-2008, Apple, Inc.
45 **
46 ** All rights reserved.
47 **
48 ** Redistribution and use in source and binary forms, with or without
49 ** modification, are permitted provided that the following conditions are met:
50 **
51 ** * Redistributions of source code must retain the above copyright notice,
52 ** this list of conditions and the following disclaimer.
53 **
54 ** * Redistributions in binary form must reproduce the above copyright notice,
55 ** this list of conditions and the following disclaimer in the documentation
56 ** and/or other materials provided with the distribution.
57 **
58 ** * Neither the name of Apple, Inc. nor the names of its contributors
59 ** may be used to endorse or promote products derived from this software
60 ** without specific prior written permission.
61 **
62 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
66 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 **
74 ****************************************************************************/
75 
76 #include <qglobal.h>
77 #ifdef QT_MAC_USE_COCOA
78 #include <private/qcocoaapplication_mac_p.h>
79 #include <private/qcocoaapplicationdelegate_mac_p.h>
80 #include <private/qt_cocoa_helpers_mac_p.h>
81 #include <private/qcocoaintrospection_p.h>
82 
84 
85 @implementation NSApplication (QT_MANGLE_NAMESPACE(QApplicationIntegration))
86 
87 - (void)QT_MANGLE_NAMESPACE(qt_setDockMenu):(NSMenu *)newMenu
88 {
89  [[QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) sharedDelegate] setDockMenu:newMenu];
90 }
91 
92 - (QApplicationPrivate *)QT_MANGLE_NAMESPACE(qt_qappPrivate)
93 {
94  return [[QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) sharedDelegate] qAppPrivate];
95 }
96 
97 - (QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *)QT_MANGLE_NAMESPACE(qt_qcocoamenuLoader)
98 {
99  return [[QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate) sharedDelegate] menuLoader];
100 }
101 
102 - (int)QT_MANGLE_NAMESPACE(qt_validModesForFontPanel):(NSFontPanel *)fontPanel
103 {
104  Q_UNUSED(fontPanel);
105  // only display those things that QFont can handle
106  return NSFontPanelFaceModeMask
107  | NSFontPanelSizeModeMask
108  | NSFontPanelCollectionModeMask
109  | NSFontPanelUnderlineEffectModeMask
110  | NSFontPanelStrikethroughEffectModeMask;
111 }
112 
113 - (void)QT_MANGLE_NAMESPACE(qt_sendPostedMessage):(NSEvent *)event
114 {
115  // WARNING: data1 and data2 is truncated to from 64-bit to 32-bit on OS 10.5!
116  // That is why we need to split the address in two parts:
117  quint64 lower = [event data1];
118  quint64 upper = [event data2];
119  QCocoaPostMessageArgs *args = reinterpret_cast<QCocoaPostMessageArgs *>(lower | (upper << 32));
120  // Special case for convenience: if the argument is an NSNumber, we unbox it directly.
121  // Use NSValue instead if this behaviour is unwanted.
122  id a1 = ([args->arg1 isKindOfClass:[NSNumber class]]) ? (id)[args->arg1 intValue] : args->arg1;
123  id a2 = ([args->arg2 isKindOfClass:[NSNumber class]]) ? (id)[args->arg2 intValue] : args->arg2;
124  switch (args->argCount) {
125  case 0:
126  [args->target performSelector:args->selector];
127  break;
128  case 1:
129  [args->target performSelector:args->selector withObject:a1];
130  break;
131  case 3:
132  [args->target performSelector:args->selector withObject:a1 withObject:a2];
133  break;
134  }
135 
136  delete args;
137 }
138 
139 - (BOOL)QT_MANGLE_NAMESPACE(qt_filterEvent):(NSEvent *)event
140 {
141  if (!qApp)
142  return false;
143 
144  if (qApp->macEventFilter(0, reinterpret_cast<EventRef>(event)))
145  return true;
146 
147  if ([event type] == NSApplicationDefined) {
148  switch ([event subtype]) {
150  [NSApp QT_MANGLE_NAMESPACE(qt_sendPostedMessage):event];
151  return true;
152  default:
153  break;
154  }
155  }
156  return false;
157 }
158 
159 @end
160 
161 @implementation QT_MANGLE_NAMESPACE(QNSApplication)
162 
163 - (void)QT_MANGLE_NAMESPACE(qt_sendEvent_original):(NSEvent *)event
164 {
165  Q_UNUSED(event);
166  // This method will only be used as a signature
167  // template for the method we add into NSApplication
168  // containing the original [NSApplication sendEvent:] implementation
169 }
170 
171 - (void)QT_MANGLE_NAMESPACE(qt_sendEvent_replacement):(NSEvent *)event
172 {
173  // This method (or its implementation to be precise) will
174  // be called instead of sendEvent if redirection occurs.
175  // 'self' will then be an instance of NSApplication
176  // (and not QNSApplication)
177  if (![NSApp QT_MANGLE_NAMESPACE(qt_filterEvent):event])
178  [self QT_MANGLE_NAMESPACE(qt_sendEvent_original):event];
179 }
180 
181 - (void)sendEvent:(NSEvent *)event
182 {
183  // This method will be called if
184  // no redirection occurs
185  if (![NSApp QT_MANGLE_NAMESPACE(qt_filterEvent):event])
186  [super sendEvent:event];
187 }
188 
189 - (void)qtDispatcherToQAction:(id)sender
190 {
191  // Forward actions sendt from the menu bar (e.g. quit) to the menu loader.
192  // Having this method here means that we are the last stop in the responder
193  // chain, and that we are able to handle menu actions even when no window is
194  // visible on screen. Note: If Qt is used as a plugin, Qt will not use a
195  // native menu bar. Hence, we will also not need to do any redirection etc. as
196  // we do with sendEvent.
197  [[NSApp QT_MANGLE_NAMESPACE(qt_qcocoamenuLoader)] qtDispatcherToQAction:sender];
198 }
199 
200 @end
201 
203 
204 void qt_redirectNSApplicationSendEvent()
205 {
206  if ([NSApp isMemberOfClass:[QT_MANGLE_NAMESPACE(QNSApplication) class]]) {
207  // No need to change implementation since Qt
208  // already controls a subclass of NSApplication
209  return;
210  }
211 
212  // Change the implementation of [NSApplication sendEvent] to the
213  // implementation of qt_sendEvent_replacement found in QNSApplication.
214  // And keep the old implementation that gets overwritten inside a new
215  // method 'qt_sendEvent_original' that we add to NSApplication
217  [NSApplication class],
218  @selector(sendEvent:),
219  [QT_MANGLE_NAMESPACE(QNSApplication) class],
220  @selector(QT_MANGLE_NAMESPACE(qt_sendEvent_replacement):),
221  @selector(QT_MANGLE_NAMESPACE(qt_sendEvent_original):));
222  }
223 
224 void qt_resetNSApplicationSendEvent()
225 {
226  qt_cocoa_change_back_implementation([NSApplication class],
227  @selector(sendEvent:),
228  @selector(QT_MANGLE_NAMESPACE(qt_sendEvent_original):));
229 }
230 
232 #endif
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
unsigned __int64 quint64
Definition: qglobal.h:943
#define qApp
void qt_cocoa_change_implementation(Class baseClass, SEL originalSel, Class proxyClass, SEL replacementSel, SEL backupSel)
void qt_cocoa_change_back_implementation(Class baseClass, SEL originalSel, SEL backupSel)
#define QT_MANGLE_NAMESPACE(name)
Definition: qglobal.h:106
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729