Qt 4.8
qt_cocoa_helpers_mac_p.h
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 #ifndef QT_COCOA_HELPERS_MAC_P_H
77 #define QT_COCOA_HELPERS_MAC_P_H
78 
79 //
80 // W A R N I N G
81 // -------------
82 //
83 // This file is not part of the Qt API. It exists for the convenience
84 // of qapplication_*.cpp, qwidget*.cpp, qcolor_x11.cpp, qfiledialog.cpp
85 // and many other. This header file may change from version to version
86 // without notice, or even be removed.
87 //
88 // We mean it.
89 //
90 
91 #include <private/qt_mac_p.h>
92 
93 #include <qapplication.h>
94 #include <qdesktopwidget.h>
95 #include <qwidget.h>
96 #include <qevent.h>
97 #include <qhash.h>
98 #include <qlabel.h>
99 #include <qpointer.h>
100 #include <qstyle.h>
101 #include <qstyleoption.h>
102 #include <qstylepainter.h>
103 #include <qtimer.h>
104 #include <qtooltip.h>
105 #include <private/qeffects_p.h>
106 #include <private/qwidget_p.h>
107 #include <qtextdocument.h>
108 #include <qdebug.h>
109 #include <qpoint.h>
110 #include "private/qt_mac_p.h"
111 
112 struct HIContentBorderMetrics;
113 struct TabletProximityRec;
114 
115 #ifdef Q_WS_MAC32
116 typedef struct _NSPoint NSPoint; // Just redefine here so I don't have to pull in all of Cocoa.
117 #else
118 typedef struct CGPoint NSPoint;
119 #endif
120 
122 
123 enum {
126 };
127 
128 Qt::MouseButtons qt_mac_get_buttons(int buttons);
129 Qt::MouseButton qt_mac_get_button(EventMouseButton button);
130 void macWindowFade(void * /*OSWindowRef*/ window, float durationSeconds = 0.15);
131 bool macWindowIsTextured(void * /*OSWindowRef*/ window);
132 void macWindowToolbarShow(const QWidget *widget, bool show );
133 void macWindowToolbarSet( void * /*OSWindowRef*/ window, void* toolbarRef );
134 bool macWindowToolbarIsVisible( void * /*OSWindowRef*/ window );
135 void macWindowSetHasShadow( void * /*OSWindowRef*/ window, bool hasShadow );
136 void macWindowFlush(void * /*OSWindowRef*/ window);
138 void qt_mac_updateContentBorderMetricts(void * /*OSWindowRef */window, const ::HIContentBorderMetrics &metrics);
139 void qt_mac_replaceDrawRect(void * /*OSWindowRef */window, QWidgetPrivate *widget);
140 void qt_mac_replaceDrawRectOriginal(void * /*OSWindowRef */window, QWidgetPrivate *widget);
141 void qt_mac_showBaseLineSeparator(void * /*OSWindowRef */window, bool show);
142 void * /*NSImage */qt_mac_create_nsimage(const QPixmap &pm);
146 void qt_dispatchTabletProximityEvent(void * /*NSEvent * */ tabletEvent);
147 #ifdef QT_MAC_USE_COCOA
148 bool qt_dispatchKeyEventWithCocoa(void * /*NSEvent * */ keyEvent, QWidget *widgetToGetEvent);
149 // These methods exists only for supporting unified mode.
150 void macDrawRectOnTop(void * /*OSWindowRef */ window);
151 void macSyncDrawingOnFirstInvocation(void * /*OSWindowRef */window);
152 void qt_cocoaStackChildWindowOnTopOfOtherChildren(QWidget *widget);
153 void qt_mac_menu_collapseSeparators(void * /*NSMenu */ menu, bool collapse);
154 #endif
155 bool qt_dispatchKeyEvent(void * /*NSEvent * */ keyEvent, QWidget *widgetToGetEvent);
156 void qt_dispatchModifiersChanged(void * /*NSEvent * */flagsChangedEvent, QWidget *widgetToGetEvent);
157 bool qt_mac_handleTabletEvent(void * /*QCocoaView * */view, void * /*NSEvent * */event);
158 inline QApplication *qAppInstance() { return static_cast<QApplication *>(QCoreApplication::instance()); }
159 void qt_dispatchTabletProximityEvent(const ::TabletProximityRec &proxRec);
160 Qt::KeyboardModifiers qt_cocoaModifiers2QtModifiers(ulong modifierFlags);
161 Qt::KeyboardModifiers qt_cocoaDragOperation2QtModifiers(uint dragOperations);
162 QPixmap qt_mac_convert_iconref(const IconRef icon, int width, int height);
163 void qt_mac_constructQIconFromIconRef(const IconRef icon, const IconRef overlayIcon, QIcon *retIcon,
165 
166 #if QT_MAC_USE_COCOA && __OBJC__
167 struct DnDParams
168 {
169  NSView *view;
170  NSEvent *theEvent;
171  QPoint globalPoint;
172  NSDragOperation performedAction;
173 };
174 
175 DnDParams *macCurrentDnDParameters();
176 NSDragOperation qt_mac_mapDropAction(Qt::DropAction action);
177 NSDragOperation qt_mac_mapDropActions(Qt::DropActions actions);
178 Qt::DropAction qt_mac_mapNSDragOperation(NSDragOperation nsActions);
179 Qt::DropActions qt_mac_mapNSDragOperations(NSDragOperation nsActions);
180 
181 QWidget *qt_mac_getTargetForKeyEvent(QWidget *widgetThatReceivedEvent);
183  QPoint &returnLocalPoint, QPoint &returnGlobalPoint, QWidget *nativeWidget, QWidget **returnWidgetUnderMouse);
184 bool qt_mac_handleMouseEvent(NSEvent *event, QEvent::Type eventType, Qt::MouseButton button, QWidget *nativeWidget, bool fakeEvent = false);
185 void qt_mac_handleNonClientAreaMouseEvent(NSWindow *window, NSEvent *event);
186 #endif
187 
188 inline int flipYCoordinate(int y)
189 {
190  return QApplication::desktop()->screenGeometry(0).height() - y;
191 }
192 
194 {
195  return QApplication::desktop()->screenGeometry(0).height() - y;
196 }
197 
198 QPointF flipPoint(const NSPoint &p);
199 NSPoint flipPoint(const QPoint &p);
200 NSPoint flipPoint(const QPointF &p);
201 
204 
205 void qt_syncCocoaTitleBarButtons(OSWindowRef window, QWidget *widgetForWindow);
206 
209 
210 #ifdef __OBJC__
211 inline NSMutableArray *qt_mac_QStringListToNSMutableArray(const QStringList &qstrlist)
212 { return reinterpret_cast<NSMutableArray *>(qt_mac_QStringListToNSMutableArrayVoid(qstrlist)); }
213 
214 inline QString qt_mac_NSStringToQString(const NSString *nsstr)
215 { return QCFString::toQString(reinterpret_cast<const CFStringRef>(nsstr)); }
216 
217 inline NSString *qt_mac_QStringToNSString(const QString &qstr)
218 { return [const_cast<NSString *>(reinterpret_cast<const NSString *>(QCFString::toCFStringRef(qstr))) autorelease]; }
219 
220 #ifdef QT_MAC_USE_COCOA
221 class QCocoaPostMessageArgs {
222 public:
223  id target;
224  SEL selector;
225  int argCount;
226  id arg1;
227  id arg2;
228  QCocoaPostMessageArgs(id target, SEL selector, int argCount=0, id arg1=0, id arg2=0)
229  : target(target), selector(selector), argCount(argCount), arg1(arg1), arg2(arg2)
230  {
231  [target retain];
232  [arg1 retain];
233  [arg2 retain];
234  }
235 
236  ~QCocoaPostMessageArgs()
237  {
238  [arg2 release];
239  [arg1 release];
240  [target release];
241  }
242 };
243 void qt_cocoaPostMessage(id target, SEL selector, int argCount=0, id arg1=0, id arg2=0);
244 void qt_cocoaPostMessageAfterEventLoopExit(id target, SEL selector, int argCount=0, id arg1=0, id arg2=0);
245 #endif
246 
247 #endif
248 
250  // This class is made to optimize for the case when the user
251  // scrolls both horizontally and vertically at the same
252  // time. This will result in two QWheelEvents (one for each
253  // direction), which will typically result in two calls to
254  // QWidget::_scroll_sys. Rather than copying pixels twize on
255  // screen because of this, we add this helper class to try to
256  // get away with only one blit.
258  static bool _inWheelEvent;
259  static int _dx;
260  static int _dy;
262 
263 public:
264  static void initDelayedScroll()
265  {
266  _inWheelEvent = true;
267  }
268 
269  static bool delayScroll(QWidgetPrivate *target, int dx, int dy, const QRect &scrollRect)
270  {
271  if (!_inWheelEvent)
272  return false;
273  if (_target && _target != target)
274  return false;
275  if (_scrollRect.width() != -1 && _scrollRect != scrollRect)
276  return false;
277 
278  _target = target;
279  _dx += dx;
280  _dy += dy;
281  _scrollRect = scrollRect;
282  return true;
283  }
284 
285  static void performDelayedScroll()
286  {
287  if (!_inWheelEvent)
288  return;
289  _inWheelEvent = false;
290  if (!_target)
291  return;
292 
293  _target->scroll_sys(_dx, _dy, _scrollRect);
294 
295  _target = 0;
296  _dx = 0;
297  _dy = 0;
298  _scrollRect = QRect(0, 0, -1, -1);
299  }
300 };
301 
303 
304 #ifdef QT_MAC_USE_COCOA
305 void qt_mac_display(QWidget *widget);
306 void qt_mac_setNeedsDisplay(QWidget *widget);
307 void qt_mac_setNeedsDisplayInRect(QWidget *widget, QRegion region);
308 #endif // QT_MAC_USE_COCOA
309 
310 
311 // Utility functions to ease the use of Core Graphics contexts.
312 
314 {
315  CGContextRetain(context);
316  CGContextSaveGState(context);
317 }
318 
320 {
321  CGContextRestoreGState(context);
322  CGContextRelease(context);
323 }
324 
325 inline void qt_mac_draw_image(CGContextRef context, CGContextRef imageContext, CGRect area, CGRect drawingArea)
326 {
327  CGImageRef image = CGBitmapContextCreateImage(imageContext);
328  CGImageRef subImage = CGImageCreateWithImageInRect(image, area);
329 
330  CGContextTranslateCTM (context, 0, drawingArea.origin.y + CGRectGetMaxY(drawingArea));
331  CGContextScaleCTM(context, 1, -1);
332  CGContextDrawImage(context, drawingArea, subImage);
333 
334  CGImageRelease(subImage);
335  CGImageRelease(image);
336 }
337 
339 
340 #endif // QT_COCOA_HELPERS_MAC_P_H
void qt_mac_updateContentBorderMetricts(void *window, const ::HIContentBorderMetrics &metrics)
OSStatus qt_mac_drawCGImage(CGContextRef cg, const CGRect *inbounds, CGImageRef)
The QApplication class manages the GUI application&#39;s control flow and main settings.
Definition: qapplication.h:99
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void qt_mac_release_graphics_context(CGContextRef context)
EventRef event
QPointer< QWidget > widget
static void keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
bool qt_mac_handleTabletEvent(void *view, void *event)
int flipYCoordinate(int y)
PasteboardRef OSPasteboardRef
bool qt_dispatchKeyEvent(void *keyEvent, QWidget *widgetToGetEvent)
static bool delayScroll(QWidgetPrivate *target, int dx, int dy, const QRect &scrollRect)
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
static Qt::MouseButtons buttons
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static CFStringRef toCFStringRef(const QString &str)
Definition: qcore_mac.cpp:69
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
struct CGImage * CGImageRef
bool macWindowIsTextured(void *window)
void qt_mac_update_mouseTracking(QWidget *widget)
void macWindowFlush(void *window)
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
The QString class provides a Unicode character string.
Definition: qstring.h:83
void * qt_mac_create_nsimage(const QPixmap &pm)
Qt::MouseButton qt_mac_get_button(EventMouseButton button)
void qt_mac_constructQIconFromIconRef(const IconRef icon, const IconRef overlayIcon, QIcon *retIcon, QStyle::StandardPixmap standardIcon=QStyle::SP_CustomBase)
StandardPixmap
This enum describes the available standard pixmaps.
Definition: qstyle.h:755
void macWindowFade(void *window, float durationSeconds=0.15)
Qt::KeyboardModifiers qt_cocoaModifiers2QtModifiers(ulong modifierFlags)
void macSendToolbarChangeEvent(QWidget *widget)
static QWidgetPrivate * _target
NSWindow * window
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void qt_mac_replaceDrawRectOriginal(void *window, QWidgetPrivate *widget)
void macWindowToolbarSet(void *window, void *toolbarRef)
WindowRef OSWindowRef
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
bool qt_mac_checkForNativeSizeGrip(const QWidget *widget)
unsigned int uint
Definition: qglobal.h:996
QPointF flipPoint(const NSPoint &p)
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
QPixmap qt_mac_convert_iconref(const IconRef icon, int width, int height)
DropAction
Definition: qnamespace.h:1597
unsigned long ulong
Definition: qglobal.h:997
Qt::MouseButtons qt_mac_get_buttons(int buttons)
signed long OSStatus
void * qt_mac_QStringListToNSMutableArrayVoid(const QStringList &list)
QApplication * qAppInstance()
Qt::KeyboardModifiers qt_cocoaDragOperation2QtModifiers(uint dragOperations)
static QDesktopWidget * desktop()
Returns the desktop widget (also called the root window).
void qt_mac_replaceDrawRect(void *window, QWidgetPrivate *widget)
struct CGRect CGRect
const QRect screenGeometry(int screen=-1) const
void qt_mac_draw_image(CGContextRef context, CGContextRef imageContext, CGRect area, CGRect drawingArea)
void qt_mac_post_retranslateAppMenu()
CGFloat qt_mac_get_scalefactor()
Type
This enum type defines the valid event types in Qt.
Definition: qcoreevent.h:62
static QCoreApplication * instance()
Returns a pointer to the application&#39;s QCoreApplication (or QApplication) instance.
struct CGPoint NSPoint
QStringList qt_mac_NSArrayToQStringList(void *nsarray)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void scroll_sys(int dx, int dy)
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void macWindowSetHasShadow(void *window, bool hasShadow)
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void qt_mac_showBaseLineSeparator(void *window, bool show)
void qt_syncCocoaTitleBarButtons(OSWindowRef window, QWidget *widgetForWindow)
bool macWindowToolbarIsVisible(void *window)
void qt_dispatchModifiersChanged(void *flagsChangedEvent, QWidget *widgetToGetEvent)
void macWindowToolbarShow(const QWidget *widget, bool show)
struct CGContext * CGContextRef
QString qt_mac_get_pasteboardString(OSPasteboardRef paste)
void qt_mac_retain_graphics_context(CGContextRef context)
qt_mac_getTargetForMouseEvent(event, QEvent::Gesture, qlocal, qglobal, 0, &widgetToGetTouch)
struct OpaqueIconRef * IconRef
static int area(const QSize &s)
Definition: qicon.cpp:155
float CGFloat
MouseButton
Definition: qnamespace.h:150
void qt_dispatchTabletProximityEvent(void *tabletEvent)
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60