/src/gui/kernel/qcursor_s60.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 533 lines · 361 code · 34 blank · 138 comment · 69 complexity · bb11dc94379f617ca3bb6159900cf6f6 MD5 · raw file

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