1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <private/qcursor_p.h>
43 #include <private/qwidget_p.h>
44 #include <private/qapplication_p.h>
45 #include <coecntrl.h>
46 #include <qcursor.h>
47 #include <private/qt_s60_p.h>
48 #include <qbitmap.h>
49 #include <w32std.h>
50 #include <qapplication.h>
51 #include <qwidget.h>
52 
53 QT_BEGIN_NAMESPACE
54 
55 #ifndef QT_NO_CURSOR
56 static QCursor cursorSprite;
57 static int cursorSpriteVisible;
58 #endif
59 
60 //pos and setpos are required whether cursors are configured or not.
pos()61 QPoint QCursor::pos()
62 {
63     return S60->lastCursorPos;
64 }
65 
setPos(int x,int y)66 void QCursor::setPos(int x, int y)
67 {
68     //clip to screen size (window server allows a sprite hotspot to be outside the screen)
69     if (x < 0)
70         x=0;
71     else if (x >= S60->screenWidthInPixels)
72         x = S60->screenWidthInPixels - 1;
73     if (y < 0)
74         y = 0;
75     else if (y >= S60->screenHeightInPixels)
76         y = S60->screenHeightInPixels - 1;
77 
78 #ifndef QT_NO_CURSOR
79 #ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
80     if (S60->brokenPointerCursors && cursorSpriteVisible)
81         cursorSprite.d->scurs.SetPosition(TPoint(x,y));
82     else
83 #endif
84         S60->wsSession().SetPointerCursorPosition(TPoint(x, y));
85 #endif
86     S60->lastCursorPos = QPoint(x, y);
87     //send a fake mouse move event, so that enter/leave events go to the widget hierarchy
88     QWidget *w = QApplication::topLevelAt(S60->lastCursorPos);
89     if (w) {
90         CCoeControl* ctrl = w->effectiveWinId();
91         TPoint epos(x, y);
92         TPoint cpos = epos - ctrl->PositionRelativeToScreen();
93         TPointerEvent fakeEvent;
94         fakeEvent.iType = TPointerEvent::EMove;
95         fakeEvent.iModifiers = 0U;
96         fakeEvent.iPosition = cpos;
97         fakeEvent.iParentPosition = epos;
98         ctrl->HandlePointerEventL(fakeEvent);
99     }
100 }
101 
102 #ifndef QT_NO_CURSOR
103 /*
104  * Request cursor to be turned on or off.
105  * Reference counted, so 2 on + 1 off = on, for example
106  */
qt_symbian_set_cursor_visible(bool visible)107 void qt_symbian_set_cursor_visible(bool visible) {
108     if (visible)
109         cursorSpriteVisible++;
110     else
111         cursorSpriteVisible--;
112     Q_ASSERT(cursorSpriteVisible >=0);
113 
114     if (cursorSpriteVisible && !S60->mouseInteractionEnabled) {
115 #ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
116         if (S60->brokenPointerCursors)
117             qt_symbian_show_pointer_sprite();
118         else
119 #endif
120             S60->wsSession().SetPointerCursorMode(EPointerCursorNormal);
121     } else if (!cursorSpriteVisible && S60->mouseInteractionEnabled) {
122 #ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
123         if (S60->brokenPointerCursors)
124             qt_symbian_hide_pointer_sprite();
125         else
126 #endif
127             S60->wsSession().SetPointerCursorMode(EPointerCursorNone);
128     }
129     S60->mouseInteractionEnabled = ((cursorSpriteVisible > 0) ? true : false);
130 }
131 
132 /*
133  * Check if the cursor is on or off
134  */
qt_symbian_is_cursor_visible()135 bool qt_symbian_is_cursor_visible() {
136     return S60->mouseInteractionEnabled;
137 }
138 
QCursorData(Qt::CursorShape s)139 QCursorData::QCursorData(Qt::CursorShape s) :
140     cshape(s), bm(0), bmm(0), hx(0), hy(0), pcurs()
141 {
142     ref = 1;
143 }
144 
~QCursorData()145 QCursorData::~QCursorData()
146 {
147     for(int i=0;i<nativeSpriteMembers.Count();i++) {
148         delete nativeSpriteMembers[i]->iBitmap;
149         delete nativeSpriteMembers[i]->iMaskBitmap;
150     }
151     nativeSpriteMembers.ResetAndDestroy();
152     pcurs.Close();
153     delete bm;
154     delete bmm;
155 }
156 
157 /* Create a bitmap cursor, this is called by public constructors in the
158  * generic QCursor code.
159  */
setBitmap(const QBitmap & bitmap,const QBitmap & mask,int hotX,int hotY)160 QCursorData *QCursorData::setBitmap(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY)
161 {
162     if (!QCursorData::initialized)
163         QCursorData::initialize();
164     if (bitmap.depth() != 1 || mask.depth() != 1 || bitmap.size() != mask.size()) {
165         qWarning("QCursor: Cannot create bitmap cursor; invalid bitmap(s)");
166         QCursorData *c = qt_cursorTable[0];
167         c->ref.ref();
168         return c;
169     }
170     QCursorData *d = new QCursorData;
171     d->bm = new QBitmap(bitmap);
172     d->bmm = new QBitmap(mask);
173     d->cshape = Qt::BitmapCursor;
174     d->hx = hotX >= 0 ? hotX : bitmap.width() / 2;
175     d->hy = hotY >= 0 ? hotY : bitmap.height() / 2;
176     return d;
177 }
178 
179 /*
180  * returns an opaque native handle to a cursor.
181  * It happens to be the address of the native handle, as window server handles
182  * are not POD types. Note there is no QCursor(HANDLE) constructor on Symbian,
183  * Mac or QWS.
184  */
handle() const185 Qt::HANDLE QCursor::handle() const
186 {
187     if (d->pcurs.WsHandle())
188         return reinterpret_cast<Qt::HANDLE> (&(d->pcurs));
189 
190 #ifdef Q_SYMBIAN_HAS_SYSTEM_CURSORS
191     // don't construct shape cursors, QApplication_s60 will use the system cursor instead
192     if (!(d->bm))
193         return 0;
194 #endif
195 
196     d->pcurs = RWsPointerCursor(S60->wsSession());
197     d->pcurs.Construct(0);
198     d->constructCursorSprite(d->pcurs);
199     d->pcurs.Activate();
200 
201     return reinterpret_cast<Qt::HANDLE> (&(d->pcurs));
202 }
203 
204 #ifndef Q_SYMBIAN_HAS_SYSTEM_CURSORS
205 /*
206  * Loads a single cursor shape from resources and appends it to a native sprite.
207  * Animated cursors (e.g. the busy cursor) have multiple members.
208  */
loadShapeFromResource(RWsSpriteBase & target,QString resource,int hx,int hy,int interval)209 void QCursorData::loadShapeFromResource(RWsSpriteBase& target, QString resource, int hx, int hy, int interval)
210 {
211     QPixmap pix;
212     CFbsBitmap* native;
213     QScopedPointer<TSpriteMember> member(new TSpriteMember);
214     member->iInterval = interval;
215     member->iInvertMask = false;
216     member->iMaskBitmap = 0; // all shapes are RGBA
217     member->iDrawMode = CGraphicsContext::EDrawModePEN;
218     member->iOffset = TPoint(-hx, -hy);
219     QString res(QLatin1String(":/trolltech/symbian/cursors/images/%1.png"));
220     pix.load(res.arg(resource));
221     native = pix.toSymbianCFbsBitmap();
222     member->iBitmap = native;
223     qt_symbian_throwIfError(nativeSpriteMembers.Append(member.data()));
224     target.AppendMember(*(member.take()));
225 }
226 
227 //TODO: after 4.6, connect with style & skins?
228 /*
229  * Constructs the native cursor from resources compiled into QtGui
230  * This is needed only when the platform doesn't have system cursors.
231  *
232  * System cursors are higher performance, since they are constructed once
233  * and shared by all applications by specifying the shape number.
234  * Due to symbian platform security considerations, and the fact most
235  * existing phones have a broken RWsPointerCursor, system cursors are not
236  * being used.
237  */
constructShapeSprite(RWsSpriteBase & target)238 void QCursorData::constructShapeSprite(RWsSpriteBase& target)
239 {
240     int i;
241     switch (cshape) {
242     default:
243         qWarning("QCursorData::constructShapeSprite unknown shape %d", cshape);
244         //fall through and give arrow cursor
245     case Qt::ArrowCursor:
246         loadShapeFromResource(target, QLatin1String("pointer"), 1, 1);
247         break;
248     case Qt::UpArrowCursor:
249         loadShapeFromResource(target, QLatin1String("uparrow"), 4, 0);
250         break;
251     case Qt::CrossCursor:
252         loadShapeFromResource(target, QLatin1String("cross"), 7, 7);
253         break;
254     case Qt::WaitCursor:
255         for (i = 1; i <= 12; i++) {
256             loadShapeFromResource(target, QString(QLatin1String("wait%1")).arg(i), 7, 7, 1000000);
257         }
258         break;
259     case Qt::IBeamCursor:
260         loadShapeFromResource(target, QLatin1String("ibeam"), 3, 10);
261         break;
262     case Qt::SizeVerCursor:
263         loadShapeFromResource(target, QLatin1String("sizever"), 4, 8);
264         break;
265     case Qt::SizeHorCursor:
266         loadShapeFromResource(target, QLatin1String("sizehor"), 8, 4);
267         break;
268     case Qt::SizeBDiagCursor:
269         loadShapeFromResource(target, QLatin1String("sizebdiag"), 8, 8);
270         break;
271     case Qt::SizeFDiagCursor:
272         loadShapeFromResource(target, QLatin1String("sizefdiag"), 8, 8);
273         break;
274     case Qt::SizeAllCursor:
275         loadShapeFromResource(target, QLatin1String("sizeall"), 7, 7);
276         break;
277     case Qt::BlankCursor:
278         loadShapeFromResource(target, QLatin1String("blank"), 0, 0);
279         break;
280     case Qt::SplitVCursor:
281         loadShapeFromResource(target, QLatin1String("splitv"), 7, 7);
282         break;
283     case Qt::SplitHCursor:
284         loadShapeFromResource(target, QLatin1String("splith"), 7, 7);
285         break;
286     case Qt::PointingHandCursor:
287         loadShapeFromResource(target, QLatin1String("handpoint"), 5, 0);
288         break;
289     case Qt::ForbiddenCursor:
290         loadShapeFromResource(target, QLatin1String("forbidden"), 7, 7);
291         break;
292     case Qt::WhatsThisCursor:
293         loadShapeFromResource(target, QLatin1String("whatsthis"), 1, 1);
294         break;
295     case Qt::BusyCursor:
296         loadShapeFromResource(target, QLatin1String("busy3"), 1, 1, 1000000);
297         loadShapeFromResource(target, QLatin1String("busy6"), 1, 1, 1000000);
298         loadShapeFromResource(target, QLatin1String("busy9"), 1, 1, 1000000);
299         loadShapeFromResource(target, QLatin1String("busy12"), 1, 1, 1000000);
300         break;
301     case Qt::OpenHandCursor:
302         loadShapeFromResource(target, QLatin1String("openhand"), 7, 7);
303         break;
304     case Qt::ClosedHandCursor:
305         loadShapeFromResource(target, QLatin1String("closehand"), 7, 7);
306         break;
307     }
308 }
309 #endif
310 
311 /*
312  * Common code between the sprite workaround and standard modes of operation.
313  * RWsSpriteBase is the base class for both RWsSprite and RWsPointerCursor.
314  * It is called from both handle() and qt_s60_show_pointer_sprite()
315  */
constructCursorSprite(RWsSpriteBase & target)316 void QCursorData::constructCursorSprite(RWsSpriteBase& target)
317 {
318     int count = nativeSpriteMembers.Count();
319     if (count) {
320         // already constructed
321         for (int i = 0; i < count; i++)
322             target.AppendMember(*(nativeSpriteMembers[i]));
323 
324         return;
325     }
326     if (pixmap.isNull() && !bm) {
327 #ifndef Q_SYMBIAN_HAS_SYSTEM_CURSORS
328         //shape cursor
329         constructShapeSprite(target);
330 #endif
331         return;
332     }
333     QScopedPointer<TSpriteMember> member(new TSpriteMember);
334     if (pixmap.isNull()) {
335         //construct mono cursor
336         member->iBitmap = bm->toSymbianCFbsBitmap();
337         member->iMaskBitmap = bmm->toSymbianCFbsBitmap();
338     }
339     else {
340         //construct normal cursor
341         member->iBitmap = pixmap.toSymbianCFbsBitmap();
342         if (pixmap.hasAlphaChannel()) {
343             member->iMaskBitmap = 0; //use alpha blending
344         }
345         else if (pixmap.hasAlpha()) {
346             member->iMaskBitmap = pixmap.mask().toSymbianCFbsBitmap();
347         }
348         else {
349             member->iMaskBitmap = 0; //opaque rectangle cursor (due to EDrawModePEN)
350         }
351     }
352 
353     member->iDrawMode = CGraphicsContext::EDrawModePEN;
354     member->iInvertMask = EFalse;
355     member->iInterval = 0;
356     member->iOffset = TPoint(-(hx), -(hy)); //Symbian hotspot coordinates are negative
357     qt_symbian_throwIfError(nativeSpriteMembers.Append(member.data()));
358     target.AppendMember(*(member.take()));
359 }
360 
361 /*
362  * shows the pointer sprite by constructing a native handle, and registering
363  * it with the window server.
364  * Only used when the sprite workaround is in use.
365  */
qt_symbian_show_pointer_sprite()366 void qt_symbian_show_pointer_sprite()
367 {
368     if (cursorSprite.d) {
369         if (cursorSprite.d->scurs.WsHandle())
370             cursorSprite.d->scurs.Close();
371     } else {
372         cursorSprite = QCursor(Qt::ArrowCursor);
373     }
374 
375     cursorSprite.d->scurs = RWsSprite(S60->wsSession());
376     QPoint pos = QCursor::pos();
377     cursorSprite.d->scurs.Construct(S60->windowGroup(), TPoint(pos.x(), pos.y()), ESpriteNoChildClip | ESpriteNoShadows);
378 
379     cursorSprite.d->constructCursorSprite(cursorSprite.d->scurs);
380     cursorSprite.d->scurs.Activate();
381 }
382 
383 /*
384  * hides the pointer sprite by closing the native handle.
385  * Only used when the sprite workaround is in use.
386  */
qt_symbian_hide_pointer_sprite()387 void qt_symbian_hide_pointer_sprite()
388 {
389     if (cursorSprite.d) {
390         cursorSprite.d->scurs.Close();
391     }
392 }
393 
394 /*
395  * Changes the cursor sprite to the cursor specified.
396  * Only used when the sprite workaround is in use.
397  */
qt_symbian_set_pointer_sprite(const QCursor & cursor)398 void qt_symbian_set_pointer_sprite(const QCursor& cursor)
399 {
400     if (S60->mouseInteractionEnabled)
401         qt_symbian_hide_pointer_sprite();
402     cursorSprite = cursor;
403     if (S60->mouseInteractionEnabled)
404         qt_symbian_show_pointer_sprite();
405 }
406 
407 /*
408  * When using sprites as a workaround on phones that have a broken
409  * RWsPointerCursor, this function is called in response to pointer events
410  * and when QCursor::setPos() is called.
411  * Performance is worse than a real pointer cursor, due to extra context
412  * switches vs. the window server moving the cursor by itself.
413  */
qt_symbian_move_cursor_sprite()414 void qt_symbian_move_cursor_sprite()
415 {
416     if (S60->mouseInteractionEnabled) {
417         cursorSprite.d->scurs.SetPosition(TPoint(S60->lastCursorPos.x(), S60->lastCursorPos.y()));
418     }
419 }
420 
421 /*
422  * Translate from Qt::CursorShape to OS system pointer cursor list index.
423  * Currently we control the implementation of the system pointer cursor list,
424  * so this function is trivial. That may not always be the case.
425  */
qt_symbian_translate_cursor_shape(Qt::CursorShape shape)426 TInt qt_symbian_translate_cursor_shape(Qt::CursorShape shape)
427 {
428     return (TInt) shape;
429 }
430 
431 /*
432   Internal function called from QWidget::setCursor()
433    force is true if this function is called from dispatchEnterLeave, it means that the
434    mouse is actually directly under this widget.
435 */
qt_symbian_set_cursor(QWidget * w,bool force)436 void qt_symbian_set_cursor(QWidget *w, bool force)
437 {
438     static QPointer<QWidget> lastUnderMouse = 0;
439     if (force) {
440         lastUnderMouse = w;
441     }
442     else if (w->testAttribute(Qt::WA_WState_Created) && lastUnderMouse
443         && lastUnderMouse->effectiveWinId() == w->effectiveWinId()) {
444         w = lastUnderMouse;
445     }
446 
447     if (!S60->curWin && w && w->internalWinId())
448         return;
449     QWidget* cW = w && !w->internalWinId() ? w : QWidget::find(S60->curWin);
450     if (!cW || cW->window() != w->window() || !cW->isVisible() || !cW->underMouse()
451         || QApplication::overrideCursor())
452         return;
453 
454 #ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
455     if (S60->brokenPointerCursors)
456         qt_symbian_set_pointer_sprite(cW->cursor());
457     else
458 #endif
459         qt_symbian_setWindowCursor(cW->cursor(), w->effectiveWinId());
460 }
461 
462 /*
463  * Makes the specified cursor appear above a specific native window group
464  * Called from QSymbianControl and QApplication::restoreOverrideCursor
465  *
466  * Window server is needed for this, so there is no equivalent when using
467  * the sprite workaround.
468  */
qt_symbian_setWindowGroupCursor(const QCursor & cursor,RWindowTreeNode & node)469 void qt_symbian_setWindowGroupCursor(const QCursor &cursor, RWindowTreeNode &node)
470 {
471     Qt::HANDLE handle = cursor.handle();
472     if (handle) {
473         RWsPointerCursor *pcurs = reinterpret_cast<RWsPointerCursor *> (handle);
474         node.SetCustomPointerCursor(*pcurs);
475     } else
476 #ifdef Q_SYMBIAN_HAS_SYSTEM_CURSORS
477     {
478         TInt shape = qt_symbian_translate_cursor_shape(cursor.shape());
479         node.SetPointerCursor(shape);
480     }
481 #else
482     qWarning("qt_s60_setWindowGroupCursor - null handle");
483 #endif
484 }
485 
486 /*
487  * Makes the specified cursor appear above a specific native window
488  * Called from QSymbianControl and QApplication::restoreOverrideCursor
489  *
490  * Window server is needed for this, so there is no equivalent when using
491  * the sprite workaround.
492  */
qt_symbian_setWindowCursor(const QCursor & cursor,const CCoeControl * wid)493 void qt_symbian_setWindowCursor(const QCursor &cursor, const CCoeControl* wid)
494 {
495     //find the window for this control
496     while (!wid->OwnsWindow()) {
497         wid = wid->Parent();
498         if (!wid)
499             return;
500     }
501     RWindowTreeNode *node = wid->DrawableWindow();
502     qt_symbian_setWindowGroupCursor(cursor, *node);
503 }
504 
505 /*
506  * Makes the specified cursor appear everywhere.
507  * Called from QApplication::setOverrideCursor
508  */
qt_symbian_setGlobalCursor(const QCursor & cursor)509 void qt_symbian_setGlobalCursor(const QCursor &cursor)
510 {
511 #ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
512     if (S60->brokenPointerCursors) {
513         qt_symbian_set_pointer_sprite(cursor);
514     } else
515 #endif
516     {
517         //because of the internals of window server, we need to force the cursor
518         //to be set in all child windows too, otherwise when the cursor is over
519         //the child window it may show a widget cursor or arrow cursor instead,
520         //depending on construction order.
521         QListIterator<WId> iter(QWidgetPrivate::mapper->uniqueKeys());
522         while(iter.hasNext())
523         {
524             CCoeControl *ctrl = iter.next();
525             if(ctrl->OwnsWindow()) {
526                 RWindowTreeNode *node = ctrl->DrawableWindow();
527                 qt_symbian_setWindowGroupCursor(cursor, *node);
528             }
529         }
530     }
531 }
532 QT_END_NAMESPACE
533 #endif // QT_NO_CURSOR
534