Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowscursor.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QT_NO_CURSOR
10
11#include <QtGui/qbitmap.h>
12#include <QtGui/qimage.h>
13#include <QtGui/qbitmap.h>
14#include <QtGui/qguiapplication.h>
15#include <QtGui/qscreen.h>
16#include <QtGui/private/qguiapplication_p.h> // getPixmapCursor()
17#include <QtGui/private/qhighdpiscaling_p.h>
18#include <QtGui/private/qpixmap_win_p.h>
19#include <QtCore/private/qwinregistry_p.h>
20
21#include <QtCore/qdebug.h>
22#include <QtCore/qscopedpointer.h>
23
24static bool initResources()
25{
26#if QT_CONFIG(imageformat_png)
27 Q_INIT_RESOURCE(cursors);
28#endif
29 return true;
30}
31
32QT_BEGIN_NAMESPACE
33
34using namespace Qt::StringLiterals;
35
36/*!
37 \class QWindowsCursorCacheKey
38 \brief Cache key for storing values in a QHash with a QCursor as key.
39
40 \internal
41*/
42
45{
46 if (!bitmapCacheKey) {
47 Q_ASSERT(!c.bitmap().isNull());
48 Q_ASSERT(!c.mask().isNull());
49 bitmapCacheKey = c.bitmap().cacheKey();
50 maskCacheKey = c.mask().cacheKey();
51 }
52 hotspotCacheKey.x = c.hotSpot().x();
53 hotspotCacheKey.y = c.hotSpot().y();
54}
55
56/*!
57 \class QWindowsCursor
58 \brief Platform cursor implementation
59
60 Note that whereas under X11, a cursor can be set as a property of
61 a window, there is only a global SetCursor() function on Windows.
62 Each Window sets on the global cursor on receiving a Enter-event
63 as do the Window manager frames (resize/move handles).
64
65 \internal
66 \sa QWindowsWindowCursor
67*/
68
69HCURSOR QWindowsCursor::createPixmapCursor(QPixmap pixmap, const QPoint &hotSpot, qreal scaleFactor)
70{
71 HCURSOR cur = nullptr;
72 const qreal pixmapScaleFactor = scaleFactor / pixmap.devicePixelRatio();
73 if (!qFuzzyCompare(pixmapScaleFactor, 1)) {
74 pixmap = pixmap.scaled((pixmapScaleFactor * QSizeF(pixmap.size())).toSize(),
75 Qt::KeepAspectRatio, Qt::SmoothTransformation);
76 }
77 QBitmap mask = pixmap.mask();
78 if (mask.isNull()) {
79 mask = QBitmap(pixmap.size());
80 mask.fill(Qt::color1);
81 }
82
83 HBITMAP ic = qt_pixmapToWinHBITMAP(pixmap, /* HBitmapAlpha */ 2);
84 const HBITMAP im = qt_createIconMask(mask);
85
86 ICONINFO ii;
87 ii.fIcon = 0;
88 ii.xHotspot = DWORD(qRound(hotSpot.x() * scaleFactor));
89 ii.yHotspot = DWORD(qRound(hotSpot.y() * scaleFactor));
90 ii.hbmMask = im;
91 ii.hbmColor = ic;
92
93 cur = CreateIconIndirect(&ii);
94
95 DeleteObject(ic);
96 DeleteObject(im);
97 return cur;
98}
99
100// Create a cursor from image and mask of the format QImage::Format_Mono.
101static HCURSOR createBitmapCursor(const QImage &bbits, const QImage &mbits,
102 QPoint hotSpot = QPoint(-1, -1),
103 bool invb = false, bool invm = false)
104{
105 const int width = bbits.width();
106 const int height = bbits.height();
107 if (hotSpot.x() < 0)
108 hotSpot.setX(width / 2);
109 if (hotSpot.y() < 0)
110 hotSpot.setY(height / 2);
111 // a ddb is word aligned, QImage depends on bow it was created
112 const auto bplDdb = qMax(1, ((width + 15) >> 4) << 1);
113 const auto bplImg = int(bbits.bytesPerLine());
114 QScopedArrayPointer<uchar> xBits(new uchar[height * bplDdb]);
115 QScopedArrayPointer<uchar> xMask(new uchar[height * bplDdb]);
116 int x = 0;
117 for (int i = 0; i < height; ++i) {
118 const uchar *bits = bbits.constScanLine(i);
119 const uchar *mask = mbits.constScanLine(i);
120 for (int j = 0; j < bplImg && j < bplDdb; ++j) {
121 uchar b = bits[j];
122 uchar m = mask[j];
123 if (invb)
124 b ^= 0xff;
125 if (invm)
126 m ^= 0xff;
127 xBits[x] = ~m;
128 xMask[x] = b ^ m;
129 ++x;
130 }
131 for (int i = bplImg; i < bplDdb; ++i) {
132 xBits[x] = 0;
133 xMask[x] = 0;
134 ++x;
135 }
136 }
137 return CreateCursor(GetModuleHandle(nullptr), hotSpot.x(), hotSpot.y(), width, height,
138 xBits.data(), xMask.data());
139}
140
141// Create a cursor from image and mask of the format QImage::Format_Mono.
142static HCURSOR createBitmapCursor(const QCursor &cursor, qreal scaleFactor = 1)
143{
144 Q_ASSERT(cursor.shape() == Qt::BitmapCursor && !cursor.bitmap().isNull());
145 QImage bbits = cursor.bitmap().toImage();
146 QImage mbits = cursor.mask().toImage();
147 scaleFactor /= bbits.devicePixelRatio();
148 if (!qFuzzyCompare(scaleFactor, 1)) {
149 const QSize scaledSize = (QSizeF(bbits.size()) * scaleFactor).toSize();
150 bbits = bbits.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
151 mbits = mbits.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
152 }
153 bbits = std::move(bbits).convertToFormat(QImage::Format_Mono);
154 mbits = std::move(mbits).convertToFormat(QImage::Format_Mono);
155 const bool invb = bbits.colorCount() > 1 && qGray(bbits.color(0)) < qGray(bbits.color(1));
156 const bool invm = mbits.colorCount() > 1 && qGray(mbits.color(0)) < qGray(mbits.color(1));
157 return createBitmapCursor(bbits, mbits, cursor.hotSpot(), invb, invm);
158}
159
160static QSize systemCursorSize() { return QSize(GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR)); }
161
162static QSize screenCursorSize(const QPlatformScreen *screen = nullptr)
163{
164 const QSize primaryScreenCursorSize = systemCursorSize();
165 if (screen) {
166 // Correct the size if the DPI value of the screen differs from
167 // that of the primary screen.
168 if (const QScreen *primaryQScreen = QGuiApplication::primaryScreen()) {
169 const QPlatformScreen *primaryScreen = primaryQScreen->handle();
170 if (screen != primaryScreen) {
171 const qreal logicalDpi = screen->logicalDpi().first;
172 const qreal primaryScreenLogicalDpi = primaryScreen->logicalDpi().first;
173 if (!qFuzzyCompare(logicalDpi, primaryScreenLogicalDpi))
174 return (QSizeF(primaryScreenCursorSize) * logicalDpi / primaryScreenLogicalDpi).toSize();
175 }
176 }
177 }
178 return primaryScreenCursorSize;
179}
180
181#if !QT_CONFIG(imageformat_png)
182
183static inline QSize standardCursorSize() { return QSize(32, 32); }
184
185// Create pixmap cursors from data and scale the image if the cursor size is
186// higher than the standard 32. Note that bitmap cursors as produced by
187// createBitmapCursor() only work for standard sizes (32,48,64...), which does
188// not work when scaling the 16x16 openhand cursor bitmaps to 150% (resulting
189// in a non-standard 24x24 size).
190static QWindowsCursor::PixmapCursor createPixmapCursorFromData(const QSize &screenCursorSize,
191 // The cursor size the bitmap is targeted for
192 const QSize &bitmapTargetCursorSize,
193 // The actual size of the bitmap data
194 int bitmapSize, const uchar *bits,
195 const uchar *maskBits)
196{
197 QPixmap rawImage = QPixmap::fromImage(QBitmap::fromData(QSize(bitmapSize, bitmapSize), bits).toImage());
198 rawImage.setMask(QBitmap::fromData(QSize(bitmapSize, bitmapSize), maskBits));
199
200 const qreal factor = qreal(screenCursorSize.width()) / qreal(bitmapTargetCursorSize.width());
201 // Scale images if the cursor size is significantly different, starting with 150% where the system cursor
202 // size is 48.
203 if (qAbs(factor - 1.0) > 0.4) {
204 const QTransform transform = QTransform::fromScale(factor, factor);
205 rawImage = rawImage.transformed(transform, Qt::SmoothTransformation);
206 }
207 const QPoint hotSpot(rawImage.width() / 2, rawImage.height() / 2);
208 return QWindowsCursor::PixmapCursor(rawImage, hotSpot);
209}
210
211QWindowsCursor::PixmapCursor QWindowsCursor::customCursor(Qt::CursorShape cursorShape,
212 const QPlatformScreen *screen)
213{
214 // Non-standard Windows cursors are created from bitmaps
215 static const uchar vsplit_bits[] = {
216 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 0x00, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xe0, 0x03, 0x00,
219 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
220 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00,
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00,
222 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
223 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00,
224 0x00, 0xc0, 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
227 static const uchar vsplitm_bits[] = {
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
230 0x00, 0xc0, 0x01, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0xf0, 0x07, 0x00,
231 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00,
232 0x00, 0xc0, 0x01, 0x00, 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00,
233 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00,
234 0x80, 0xff, 0xff, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00,
235 0x00, 0xc0, 0x01, 0x00, 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xf0, 0x07, 0x00,
236 0x00, 0xe0, 0x03, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x80, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
239 static const uchar hsplit_bits[] = {
240 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00,
243 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00,
244 0x00, 0x41, 0x82, 0x00, 0x80, 0x41, 0x82, 0x01, 0xc0, 0x7f, 0xfe, 0x03,
245 0x80, 0x41, 0x82, 0x01, 0x00, 0x41, 0x82, 0x00, 0x00, 0x40, 0x02, 0x00,
246 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00,
247 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
249 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
250 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
251 static const uchar hsplitm_bits[] = {
252 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00,
255 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe2, 0x47, 0x00, 0x00, 0xe3, 0xc7, 0x00,
256 0x80, 0xe3, 0xc7, 0x01, 0xc0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x07,
257 0xc0, 0xff, 0xff, 0x03, 0x80, 0xe3, 0xc7, 0x01, 0x00, 0xe3, 0xc7, 0x00,
258 0x00, 0xe2, 0x47, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00,
259 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
263 static const uchar openhand_bits[] = {
264 0x80,0x01,0x58,0x0e,0x64,0x12,0x64,0x52,0x48,0xb2,0x48,0x92,
265 0x16,0x90,0x19,0x80,0x11,0x40,0x02,0x40,0x04,0x40,0x04,0x20,
266 0x08,0x20,0x10,0x10,0x20,0x10,0x00,0x00};
267 static const uchar openhandm_bits[] = {
268 0x80,0x01,0xd8,0x0f,0xfc,0x1f,0xfc,0x5f,0xf8,0xff,0xf8,0xff,
269 0xf6,0xff,0xff,0xff,0xff,0x7f,0xfe,0x7f,0xfc,0x7f,0xfc,0x3f,
270 0xf8,0x3f,0xf0,0x1f,0xe0,0x1f,0x00,0x00};
271 static const uchar closedhand_bits[] = {
272 0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x0d,0x48,0x32,0x08,0x50,
273 0x10,0x40,0x18,0x40,0x04,0x40,0x04,0x20,0x08,0x20,0x10,0x10,
274 0x20,0x10,0x20,0x10,0x00,0x00,0x00,0x00};
275 static const uchar closedhandm_bits[] = {
276 0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x0d,0xf8,0x3f,0xf8,0x7f,
277 0xf0,0x7f,0xf8,0x7f,0xfc,0x7f,0xfc,0x3f,0xf8,0x3f,0xf0,0x1f,
278 0xe0,0x1f,0xe0,0x1f,0x00,0x00,0x00,0x00};
279
280#if QT_CONFIG(imageformat_xpm)
281 static const char * const moveDragCursorXpmC[] = {
282 "11 20 3 1",
283 ". c None",
284 "a c #FFFFFF",
285 "X c #000000", // X11 cursor is traditionally black
286 "aa.........",
287 "aXa........",
288 "aXXa.......",
289 "aXXXa......",
290 "aXXXXa.....",
291 "aXXXXXa....",
292 "aXXXXXXa...",
293 "aXXXXXXXa..",
294 "aXXXXXXXXa.",
295 "aXXXXXXXXXa",
296 "aXXXXXXaaaa",
297 "aXXXaXXa...",
298 "aXXaaXXa...",
299 "aXa..aXXa..",
300 "aa...aXXa..",
301 "a.....aXXa.",
302 "......aXXa.",
303 ".......aXXa",
304 ".......aXXa",
305 "........aa."};
306
307 static const char * const copyDragCursorXpmC[] = {
308 "24 30 3 1",
309 ". c None",
310 "a c #000000",
311 "X c #FFFFFF",
312 "XX......................",
313 "XaX.....................",
314 "XaaX....................",
315 "XaaaX...................",
316 "XaaaaX..................",
317 "XaaaaaX.................",
318 "XaaaaaaX................",
319 "XaaaaaaaX...............",
320 "XaaaaaaaaX..............",
321 "XaaaaaaaaaX.............",
322 "XaaaaaaXXXX.............",
323 "XaaaXaaX................",
324 "XaaXXaaX................",
325 "XaX..XaaX...............",
326 "XX...XaaX...............",
327 "X.....XaaX..............",
328 "......XaaX..............",
329 ".......XaaX.............",
330 ".......XaaX.............",
331 "........XX...aaaaaaaaaaa",
332 ".............aXXXXXXXXXa",
333 ".............aXXXXXXXXXa",
334 ".............aXXXXaXXXXa",
335 ".............aXXXXaXXXXa",
336 ".............aXXaaaaaXXa",
337 ".............aXXXXaXXXXa",
338 ".............aXXXXaXXXXa",
339 ".............aXXXXXXXXXa",
340 ".............aXXXXXXXXXa",
341 ".............aaaaaaaaaaa"};
342
343 static const char * const linkDragCursorXpmC[] = {
344 "24 30 3 1",
345 ". c None",
346 "a c #000000",
347 "X c #FFFFFF",
348 "XX......................",
349 "XaX.....................",
350 "XaaX....................",
351 "XaaaX...................",
352 "XaaaaX..................",
353 "XaaaaaX.................",
354 "XaaaaaaX................",
355 "XaaaaaaaX...............",
356 "XaaaaaaaaX..............",
357 "XaaaaaaaaaX.............",
358 "XaaaaaaXXXX.............",
359 "XaaaXaaX................",
360 "XaaXXaaX................",
361 "XaX..XaaX...............",
362 "XX...XaaX...............",
363 "X.....XaaX..............",
364 "......XaaX..............",
365 ".......XaaX.............",
366 ".......XaaX.............",
367 "........XX...aaaaaaaaaaa",
368 ".............aXXXXXXXXXa",
369 ".............aXXXaaaaXXa",
370 ".............aXXXXaaaXXa",
371 ".............aXXXaaaaXXa",
372 ".............aXXaaaXaXXa",
373 ".............aXXaaXXXXXa",
374 ".............aXXaXXXXXXa",
375 ".............aXXXaXXXXXa",
376 ".............aXXXXXXXXXa",
377 ".............aaaaaaaaaaa"};
378#endif
379
380 switch (cursorShape) {
381 case Qt::SplitVCursor:
382 return createPixmapCursorFromData(screenCursorSize(screen), standardCursorSize(), 32, vsplit_bits, vsplitm_bits);
383 case Qt::SplitHCursor:
384 return createPixmapCursorFromData(screenCursorSize(screen), standardCursorSize(), 32, hsplit_bits, hsplitm_bits);
385 case Qt::OpenHandCursor:
386 return createPixmapCursorFromData(screenCursorSize(screen), standardCursorSize(), 16, openhand_bits, openhandm_bits);
387 case Qt::ClosedHandCursor:
388 return createPixmapCursorFromData(screenCursorSize(screen), standardCursorSize(), 16, closedhand_bits, closedhandm_bits);
389#if QT_CONFIG(imageformat_xpm)
390 case Qt::DragCopyCursor:
391 return QWindowsCursor::PixmapCursor(QPixmap(copyDragCursorXpmC), QPoint(0, 0));
392 case Qt::DragMoveCursor:
393 return QWindowsCursor::PixmapCursor(QPixmap(moveDragCursorXpmC), QPoint(0, 0));
394 case Qt::DragLinkCursor:
395 return QWindowsCursor::PixmapCursor(QPixmap(linkDragCursorXpmC), QPoint(0, 0));
396#endif
397 }
398
399 return QWindowsCursor::PixmapCursor();
400}
401#else // QT_NO_IMAGEFORMAT_PNG
402struct QWindowsCustomPngCursor {
403 Qt::CursorShape shape;
404 int size;
405 const char *fileName;
406 int hotSpotX;
407 int hotSpotY;
408};
409
410QWindowsCursor::PixmapCursor QWindowsCursor::customCursor(Qt::CursorShape cursorShape, const QPlatformScreen *screen)
411{
412 static const QWindowsCustomPngCursor pngCursors[] = {
413 { Qt::SplitVCursor, 32, "splitvcursor_32.png", 11, 11 },
414 { Qt::SplitVCursor, 48, "splitvcursor_48.png", 16, 17 },
415 { Qt::SplitVCursor, 64, "splitvcursor_64.png", 22, 22 },
416 { Qt::SplitHCursor, 32, "splithcursor_32.png", 11, 11 },
417 { Qt::SplitHCursor, 48, "splithcursor_48.png", 16, 17 },
418 { Qt::SplitHCursor, 64, "splithcursor_64.png", 22, 22 },
419 { Qt::OpenHandCursor, 32, "openhandcursor_32.png", 10, 12 },
420 { Qt::OpenHandCursor, 48, "openhandcursor_48.png", 15, 16 },
421 { Qt::OpenHandCursor, 64, "openhandcursor_64.png", 20, 24 },
422 { Qt::ClosedHandCursor, 32, "closedhandcursor_32.png", 10, 12 },
423 { Qt::ClosedHandCursor, 48, "closedhandcursor_48.png", 15, 16 },
424 { Qt::ClosedHandCursor, 64, "closedhandcursor_64.png", 20, 24 },
425 { Qt::DragCopyCursor, 32, "dragcopycursor_32.png", 0, 0 },
426 { Qt::DragCopyCursor, 48, "dragcopycursor_48.png", 0, 0 },
427 { Qt::DragCopyCursor, 64, "dragcopycursor_64.png", 0, 0 },
428 { Qt::DragMoveCursor, 32, "dragmovecursor_32.png", 0, 0 },
429 { Qt::DragMoveCursor, 48, "dragmovecursor_48.png", 0, 0 },
430 { Qt::DragMoveCursor, 64, "dragmovecursor_64.png", 0, 0 },
431 { Qt::DragLinkCursor, 32, "draglinkcursor_32.png", 0, 0 },
432 { Qt::DragLinkCursor, 48, "draglinkcursor_48.png", 0, 0 },
433 { Qt::DragLinkCursor, 64, "draglinkcursor_64.png", 0, 0 }
434 };
435
436 const QSize cursorSize = screenCursorSize(screen);
437 const QWindowsCustomPngCursor *sEnd = pngCursors + sizeof(pngCursors) / sizeof(pngCursors[0]);
438 const QWindowsCustomPngCursor *bestFit = nullptr;
439 int sizeDelta = INT_MAX;
440 for (const QWindowsCustomPngCursor *s = pngCursors; s < sEnd; ++s) {
441 if (s->shape != cursorShape)
442 continue;
443 const int currentSizeDelta = qMax(s->size, cursorSize.width()) - qMin(s->size, cursorSize.width());
444 if (currentSizeDelta < sizeDelta) {
445 bestFit = s;
446 if (currentSizeDelta == 0)
447 break; // Perfect match found
448 sizeDelta = currentSizeDelta;
449 }
450 }
451
452 if (!bestFit)
453 return PixmapCursor();
454
455 const QPixmap rawImage(":/qt-project.org/windows/cursors/images/"_L1 +
456 QLatin1StringView(bestFit->fileName));
457 return PixmapCursor(rawImage, QPoint(bestFit->hotSpotX, bestFit->hotSpotY));
458}
459#endif // !QT_NO_IMAGEFORMAT_PNG
460
465
466HCURSOR QWindowsCursor::createCursorFromShape(Qt::CursorShape cursorShape, const QPlatformScreen *screen)
467{
468 Q_ASSERT(cursorShape != Qt::BitmapCursor);
469
470 static const QWindowsStandardCursorMapping standardCursors[] = {
471 { Qt::ArrowCursor, IDC_ARROW},
472 { Qt::UpArrowCursor, IDC_UPARROW },
473 { Qt::CrossCursor, IDC_CROSS },
474 { Qt::WaitCursor, IDC_WAIT },
475 { Qt::IBeamCursor, IDC_IBEAM },
476 { Qt::SizeVerCursor, IDC_SIZENS },
477 { Qt::SizeHorCursor, IDC_SIZEWE },
478 { Qt::SizeBDiagCursor, IDC_SIZENESW },
479 { Qt::SizeFDiagCursor, IDC_SIZENWSE },
480 { Qt::SizeAllCursor, IDC_SIZEALL },
481 { Qt::ForbiddenCursor, IDC_NO },
482 { Qt::WhatsThisCursor, IDC_HELP },
483 { Qt::BusyCursor, IDC_APPSTARTING },
484 { Qt::PointingHandCursor, IDC_HAND }
485 };
486
487 switch (cursorShape) {
488 case Qt::BlankCursor: {
489 QImage blank = QImage(systemCursorSize(), QImage::Format_Mono);
490 blank.fill(0); // ignore color table
491 return createBitmapCursor(blank, blank);
492 }
493 case Qt::SplitVCursor:
494 case Qt::SplitHCursor:
495 case Qt::OpenHandCursor:
496 case Qt::ClosedHandCursor:
497 case Qt::DragCopyCursor:
498 case Qt::DragMoveCursor:
499 case Qt::DragLinkCursor:
500 return QWindowsCursor::createPixmapCursor(customCursor(cursorShape, screen));
501 default:
502 break;
503 }
504
505 // Load available standard cursors from resources
506 for (const QWindowsStandardCursorMapping &s : standardCursors) {
507 if (s.shape == cursorShape) {
508 return static_cast<HCURSOR>(LoadImage(nullptr, s.resource, IMAGE_CURSOR,
509 0, 0, LR_DEFAULTSIZE | LR_SHARED));
510 }
511 }
512
513 qWarning("%s: Invalid cursor shape %d", __FUNCTION__, cursorShape);
514 return nullptr;
515}
516
517/*!
518 \brief Return cached standard cursor resources or create new ones.
519*/
520
522{
523 StandardCursorCache::Iterator it = m_standardCursorCache.find(shape);
524 if (it == m_standardCursorCache.end()) {
525 if (const HCURSOR hc = QWindowsCursor::createCursorFromShape(shape, m_screen))
526 it = m_standardCursorCache.insert(shape, CursorHandlePtr(new CursorHandle(hc)));
527 }
528 return it != m_standardCursorCache.end() ? it.value() : CursorHandlePtr(new CursorHandle);
529}
530
531HCURSOR QWindowsCursor::m_overriddenCursor = nullptr;
532QCursor QWindowsCursor::m_overrideCursor = QCursor();
533POINT QWindowsCursor::m_cursorPositionCache = {0,0};
534
535/*!
536 \brief Return cached pixmap cursor or create new one.
537*/
538
540{
541 const qreal scaleFactor = QHighDpiScaling::factor(m_screen);
542 const QWindowsPixmapCursorCacheKey cacheKey(c, scaleFactor);
543 PixmapCursorCache::iterator it = m_pixmapCursorCache.find(cacheKey);
544 if (it == m_pixmapCursorCache.end()) {
545 if (m_pixmapCursorCache.size() > 50) {
546 // Prevent the cursor cache from growing indefinitely hitting GDI resource
547 // limits if new pixmap cursors are created repetitively by purging out
548 // all-noncurrent pixmap cursors (QTBUG-43515)
549 const HCURSOR currentCursor = GetCursor();
550 for (it = m_pixmapCursorCache.begin(); it != m_pixmapCursorCache.end() ; ) {
551 if (it.value()->handle() != currentCursor)
552 it = m_pixmapCursorCache.erase(it);
553 else
554 ++it;
555 }
556 }
557 const QPixmap pixmap = c.pixmap();
558 const HCURSOR hc = pixmap.isNull()
559 ? createBitmapCursor(c, scaleFactor)
560 : QWindowsCursor::createPixmapCursor(pixmap, c.hotSpot(), scaleFactor);
561 it = m_pixmapCursorCache.insert(cacheKey, CursorHandlePtr(new CursorHandle(hc)));
562 }
563 return it.value();
564}
565
566QWindowsCursor::QWindowsCursor(const QPlatformScreen *screen)
567 : m_screen(screen)
568{
569 static const bool dummy = initResources();
570 Q_UNUSED(dummy);
571}
572
573inline CursorHandlePtr QWindowsCursor::cursorHandle(const QCursor &cursor)
574{
575 return cursor.shape() == Qt::BitmapCursor
576 ? pixmapWindowCursor(cursor)
577 : standardWindowCursor(cursor.shape());
578}
579
580/*!
581 \brief Set a cursor on a window.
582
583 This is called frequently as the mouse moves over widgets in the window
584 (QLineEdits, etc).
585*/
586
587void QWindowsCursor::changeCursor(QCursor *cursorIn, QWindow *window)
588{
589 QWindowsWindow *platformWindow = QWindowsWindow::windowsWindowOf(window);
590 if (!platformWindow) // Desktop/Foreign window.
591 return;
592
593 if (!cursorIn) {
594 platformWindow->setCursor(CursorHandlePtr(new CursorHandle));
595 return;
596 }
597 const CursorHandlePtr wcursor = cursorHandle(*cursorIn);
598 if (wcursor->handle()) {
599 platformWindow->setCursor(wcursor);
600 } else {
601 qWarning("%s: Unable to obtain system cursor for %d",
602 __FUNCTION__, cursorIn->shape());
603 }
604}
605
607{
608 QScreen *qscreen = QGuiApplication::screenAt(QCursor::pos());
609 if (!qscreen)
610 qscreen = QGuiApplication::primaryScreen();
611 if (!qscreen || !qscreen->handle())
612 return nullptr;
613 return static_cast<QWindowsCursor *>(qscreen->handle()->cursor());
614}
615
616// QTBUG-69637: Override cursors can get reset externally when moving across
617// window borders. Enforce the cursor again (to be called from enter event).
619{
621 return;
623 if (!cursor)
624 return;
625 const CursorHandlePtr wcursor = cursor->cursorHandle(m_overrideCursor);
626 if (wcursor->handle() && wcursor->handle() != GetCursor())
627 SetCursor(wcursor->handle());
628}
629
630void QWindowsCursor::setOverrideCursor(const QCursor &cursor)
631{
633 if (!screenCursor)
634 screenCursor = this;
635 const CursorHandlePtr wcursor = screenCursor->cursorHandle(cursor);
636 if (const auto overrideCursor = wcursor->handle()) {
637 m_overrideCursor = cursor;
638 const HCURSOR previousCursor = SetCursor(overrideCursor);
639 if (m_overriddenCursor == nullptr)
640 m_overriddenCursor = previousCursor;
641 } else {
642 qWarning("%s: Unable to obtain system cursor for %d",
643 __FUNCTION__, cursor.shape());
644 }
645}
646
648{
649 if (m_overriddenCursor) {
650 SetCursor(m_overriddenCursor);
651 m_overriddenCursor = nullptr;
652 }
653 auto &windows = QWindowsContext::instance()->windows();
654 for (auto it = windows.cbegin(), end = windows.cend(); it != end; ++it) {
655 if (it.value()->screen() == m_screen)
656 it.value()->setFlag(QWindowsWindow::RestoreOverrideCursor);
657 }
658}
659
660QPoint QWindowsCursor::mousePosition()
661{
662 POINT p;
663 if (GetCursorPos(&p))
664 m_cursorPositionCache = p;
665 return QPoint(m_cursorPositionCache.x, m_cursorPositionCache.y);
666}
667
669{
670 enum { cursorShowing = 0x1, cursorSuppressed = 0x2 }; // Windows 8: CURSOR_SUPPRESSED
671 CURSORINFO cursorInfo;
672 cursorInfo.cbSize = sizeof(CURSORINFO);
673 if (GetCursorInfo(&cursorInfo)) {
674 if (cursorInfo.flags & cursorShowing)
675 return State::Showing;
676 if (cursorInfo.flags & cursorSuppressed)
677 return State::Suppressed;
678 }
679 return State::Hidden;
680}
681
683{
684 return mousePosition();
685}
686
687void QWindowsCursor::setPos(const QPoint &pos)
688{
689 m_cursorPositionCache = {pos.x(), pos.y()};
690 SetCursorPos(pos.x() , pos.y());
691}
692
693/*
694 The standard size is 32x32, even though the cursor is actually just
695 16 pixels large. If a large cursor is set in the accessibility settings,
696 then the cursor increases with 8 pixels for each step.
697*/
699{
700 const auto cursorSizeSetting =
701 QWinRegistryKey(HKEY_CURRENT_USER, LR"(Control Panel\Cursors)")
702 .value<DWORD>(L"CursorBaseSize");
703 const int baseSize = screenCursorSize(m_screen).width() / 2;
704 if (!cursorSizeSetting)
705 return QSize(baseSize / 2, baseSize / 2);
706
707 // The registry values are dpi-independent, so we need to scale the result.
708 int cursorSizeValue = *cursorSizeSetting * m_screen->logicalDpi().first
709 / m_screen->logicalBaseDpi().first;
710
711 // map from registry value 32-256 to 0-14, and from there to pixels
712 cursorSizeValue = (cursorSizeValue - 2 * baseSize) / baseSize;
713 const int cursorSize = baseSize + cursorSizeValue * (baseSize / 2);
714 return QSize(cursorSize, cursorSize);
715}
716
717QPixmap QWindowsCursor::dragDefaultCursor(Qt::DropAction action) const
718{
719 switch (action) {
720 case Qt::CopyAction:
721 if (m_copyDragCursor.isNull())
722 m_copyDragCursor = QWindowsCursor::customCursor(Qt::DragCopyCursor, m_screen).pixmap;
723 return m_copyDragCursor;
724 case Qt::TargetMoveAction:
725 case Qt::MoveAction:
726 if (m_moveDragCursor.isNull())
727 m_moveDragCursor = QWindowsCursor::customCursor(Qt::DragMoveCursor, m_screen).pixmap;
728 return m_moveDragCursor;
729 case Qt::LinkAction:
730 if (m_linkDragCursor.isNull())
731 m_linkDragCursor = QWindowsCursor::customCursor(Qt::DragLinkCursor, m_screen).pixmap;
732 return m_linkDragCursor;
733 default:
734 break;
735 }
736
737#if QT_CONFIG(imageformat_xpm)
738 static const char * const ignoreDragCursorXpmC[] = {
739 "24 30 3 1",
740 ". c None",
741 "a c #000000",
742 "X c #FFFFFF",
743 "aa......................",
744 "aXa.....................",
745 "aXXa....................",
746 "aXXXa...................",
747 "aXXXXa..................",
748 "aXXXXXa.................",
749 "aXXXXXXa................",
750 "aXXXXXXXa...............",
751 "aXXXXXXXXa..............",
752 "aXXXXXXXXXa.............",
753 "aXXXXXXaaaa.............",
754 "aXXXaXXa................",
755 "aXXaaXXa................",
756 "aXa..aXXa...............",
757 "aa...aXXa...............",
758 "a.....aXXa..............",
759 "......aXXa.....XXXX.....",
760 ".......aXXa..XXaaaaXX...",
761 ".......aXXa.XaaaaaaaaX..",
762 "........aa.XaaaXXXXaaaX.",
763 "...........XaaaaX..XaaX.",
764 "..........XaaXaaaX..XaaX",
765 "..........XaaXXaaaX.XaaX",
766 "..........XaaX.XaaaXXaaX",
767 "..........XaaX..XaaaXaaX",
768 "...........XaaX..XaaaaX.",
769 "...........XaaaXXXXaaaX.",
770 "............XaaaaaaaaX..",
771 ".............XXaaaaXX...",
772 "...............XXXX....."};
773#endif
774
775 if (m_ignoreDragCursor.isNull()) {
776 HCURSOR cursor = LoadCursor(nullptr, IDC_NO);
777 ICONINFO iconInfo = {0, 0, 0, nullptr, nullptr};
778 GetIconInfo(cursor, &iconInfo);
779 BITMAP bmColor = {0, 0, 0, 0, 0, 0, nullptr};
780
781 if (iconInfo.hbmColor
782 && GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bmColor)
783 && bmColor.bmWidth == bmColor.bmWidthBytes / 4) {
784 const int colorBitsLength = bmColor.bmHeight * bmColor.bmWidthBytes;
785 auto *colorBits = new uchar[colorBitsLength];
786 GetBitmapBits(iconInfo.hbmColor, colorBitsLength, colorBits);
787 const QImage colorImage(colorBits, bmColor.bmWidth, bmColor.bmHeight,
788 bmColor.bmWidthBytes, QImage::Format_ARGB32);
789
790 m_ignoreDragCursor = QPixmap::fromImage(colorImage);
791 delete [] colorBits;
792#if QT_CONFIG(imageformat_xpm)
793 } else {
794 m_ignoreDragCursor = QPixmap(ignoreDragCursorXpmC);
795#endif
796 }
797
798 DeleteObject(iconInfo.hbmMask);
799 DeleteObject(iconInfo.hbmColor);
800 DestroyCursor(cursor);
801 }
802 return m_ignoreDragCursor;
803}
804
805HCURSOR QWindowsCursor::hCursor(const QCursor &c) const
806{
807 const Qt::CursorShape shape = c.shape();
808 if (shape == Qt::BitmapCursor) {
809 const qreal scaleFactor = QHighDpiScaling::factor(m_screen);
810 const auto pit = m_pixmapCursorCache.constFind(QWindowsPixmapCursorCacheKey(c, scaleFactor));
811 if (pit != m_pixmapCursorCache.constEnd())
812 return pit.value()->handle();
813 } else {
814 const auto sit = m_standardCursorCache.constFind(shape);
815 if (sit != m_standardCursorCache.constEnd())
816 return sit.value()->handle();
817 }
818 return HCURSOR(nullptr);
819}
820
821/*!
822 \class QWindowsWindowCursor
823 \brief Per-Window cursor. Contains a QCursor and manages its associated system
824 cursor handle resource.
825
826 \internal
827 \sa QWindowsCursor
828*/
829
830QT_END_NAMESPACE
831
832#endif // !QT_NO_CURSOR
\inmodule QtCore\reentrant
Definition qpoint.h:30
Singleton container for all relevant information.
static QWindowsContext * instance()
Platform cursor implementation.
static bool hasOverrideCursor()
void setOverrideCursor(const QCursor &cursor) override
Reimplement this function in subclass to set an override cursor on the associated screen and return t...
void changeCursor(QCursor *widgetCursor, QWindow *widget) override
Set a cursor on a window.
HCURSOR hCursor(const QCursor &c) const
void clearOverrideCursor() override
Reimplement this function in subclass to clear the override cursor.
CursorHandlePtr standardWindowCursor(Qt::CursorShape s=Qt::ArrowCursor)
Return cached standard cursor resources or create new ones.
static State cursorState()
static void enforceOverrideCursor()
QPixmap dragDefaultCursor(Qt::DropAction action) const
CursorHandlePtr pixmapWindowCursor(const QCursor &c)
Return cached pixmap cursor or create new one.
QSize size() const override
Returns the size of the cursor, in native pixels.
QPoint pos() const override
Raster or OpenGL Window.
static bool initResources()
static QSize screenCursorSize(const QPlatformScreen *screen=nullptr)
static HCURSOR createBitmapCursor(const QImage &bbits, const QImage &mbits, QPoint hotSpot=QPoint(-1, -1), bool invb=false, bool invm=false)
static QSize systemCursorSize()
static QWindowsCursor * cursorForPointerScreen()
static HCURSOR createBitmapCursor(const QCursor &cursor, qreal scaleFactor=1)
QWindowsPixmapCursorCacheKey(const QCursor &c, qreal scaleFactor)