source: trunk/src/gui/kernel/qcursor_mac.mm@ 452

Last change on this file since 452 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <private/qcursor_p.h>
43#include <private/qpixmap_mac_p.h>
44#include <qapplication.h>
45#include <qbitmap.h>
46#include <qcursor.h>
47#include <qevent.h>
48#include <string.h>
49#include <unistd.h>
50#include <AppKit/NSCursor.h>
51#include <qpainter.h>
52#include <private/qt_cocoa_helpers_mac_p.h>
53
54QT_BEGIN_NAMESPACE
55
56/*****************************************************************************
57 Externals
58 *****************************************************************************/
59extern QCursorData *qt_cursorTable[Qt::LastCursor + 1];
60extern OSWindowRef qt_mac_window_for(const QWidget *); //qwidget_mac.cpp
61extern GrafPtr qt_mac_qd_context(const QPaintDevice *); //qpaintdevice_mac.cpp
62extern bool qt_sendSpontaneousEvent(QObject *, QEvent *); //qapplication_mac.cpp
63
64/*****************************************************************************
65 Internal QCursorData class
66 *****************************************************************************/
67
68class QMacAnimateCursor : public QObject
69{
70 int timerId, step;
71 ThemeCursor curs;
72public:
73 QMacAnimateCursor() : QObject(), timerId(-1) { }
74 void start(ThemeCursor c) {
75 step = 1;
76 if(timerId != -1)
77 killTimer(timerId);
78 timerId = startTimer(300);
79 curs = c;
80 }
81 void stop() {
82 if(timerId != -1) {
83 killTimer(timerId);
84 timerId = -1;
85 }
86 }
87protected:
88 void timerEvent(QTimerEvent *e) {
89 if(e->timerId() == timerId) {
90 /*
91 if(SetAnimatedThemeCursor(curs, step++) == themeBadCursorIndexErr)
92 stop();
93 */
94 }
95 }
96};
97
98static QCursorData *currentCursor = 0; //current cursor
99void qt_mac_set_cursor(const QCursor *c, const QPoint &)
100{
101 if (!c) {
102 currentCursor = 0;
103 return;
104 }
105 c->handle(); //force the cursor to get loaded, if it's not
106
107 if(1 || currentCursor != c->d) {
108 if(currentCursor && currentCursor->type == QCursorData::TYPE_ThemeCursor
109 && currentCursor->curs.tc.anim)
110 currentCursor->curs.tc.anim->stop();
111 QMacCocoaAutoReleasePool pool;
112 if(c->d->type == QCursorData::TYPE_ImageCursor) {
113 [static_cast<NSCursor *>(c->d->curs.cp.nscursor) set];
114 } else if(c->d->type == QCursorData::TYPE_ThemeCursor) {
115#ifdef QT_MAC_USE_COCOA
116 if (c->d->curs.cp.nscursor == 0)
117 [[NSCursor arrowCursor] set];
118 [static_cast<NSCursor *>(c->d->curs.cp.nscursor) set];
119#else
120 if(SetAnimatedThemeCursor(c->d->curs.tc.curs, 0) == themeBadCursorIndexErr) {
121 SetThemeCursor(c->d->curs.tc.curs);
122 } else {
123 if(!c->d->curs.tc.anim)
124 c->d->curs.tc.anim = new QMacAnimateCursor;
125 c->d->curs.tc.anim->start(c->d->curs.tc.curs);
126 }
127#endif
128 }
129 }
130 currentCursor = c->d;
131}
132
133void qt_mac_update_cursor_at_global_pos(const QPoint &globalPos)
134{
135 QCursor cursor(Qt::ArrowCursor);
136 if (QApplication::overrideCursor()) {
137 cursor = *QApplication::overrideCursor();
138 } else {
139 for(QWidget *w = QApplication::widgetAt(globalPos); w; w = w->parentWidget()) {
140 if(w->testAttribute(Qt::WA_SetCursor)) {
141 cursor = w->cursor();
142 break;
143 }
144 }
145 }
146 qt_mac_set_cursor(&cursor, globalPos);
147}
148
149void qt_mac_update_cursor()