Qt 4.8
qxlibcursor.cpp
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 plugins 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 #include "qxlibcursor.h"
43 
44 #include "qxlibintegration.h"
45 #include "qxlibscreen.h"
46 #include "qxlibwindow.h"
47 #include "qxlibdisplay.h"
48 
49 #include <QtGui/QBitmap>
50 
51 #include <X11/cursorfont.h>
52 
54 
56  : QPlatformCursor(screen)
57 {
58 }
59 
61 {
62  QXlibWindow *w = 0;
63  if (widget) {
64  QWidget *window = widget->window();
65  w = static_cast<QXlibWindow*>(window->platformWindow());
66  } else {
67  // No X11 cursor control when there is no widget under the cursor
68  return;
69  }
70 
71  if (!w)
72  return;
73 
74  int id = cursor->handle();
75 
76  Cursor c;
77  if (!cursorMap.contains(id)) {
78  if (cursor->shape() == Qt::BitmapCursor)
79  c = createCursorBitmap(cursor);
80  else
81  c = createCursorShape(cursor->shape());
82  if (!c) {
83  return;
84  }
85  cursorMap.insert(id, c);
86  } else {
87  c = cursorMap.value(id);
88  }
89 
90  w->setCursor(c);
91 }
92 
94 {
95  XColor bg, fg;
96  bg.red = 255 << 8;
97  bg.green = 255 << 8;
98  bg.blue = 255 << 8;
99  fg.red = 0;
100  fg.green = 0;
101  fg.blue = 0;
102  QPoint spot = cursor->hotSpot();
103  Window rootwin = testLiteScreen()->rootWindow();
104 
106  QImage maskImage = cursor->mask()->toImage().convertToFormat(QImage::Format_MonoLSB);
107 
108  int width = cursor->bitmap()->width();
109  int height = cursor->bitmap()->height();
110  int bytesPerLine = mapImage.bytesPerLine();
111  int destLineSize = width / 8;
112  if (width % 8)
113  destLineSize++;
114 
115  const uchar * map = mapImage.bits();
116  const uchar * mask = maskImage.bits();
117 
118  char * mapBits = new char[height * destLineSize];
119  char * maskBits = new char[height * destLineSize];
120  for (int i = 0; i < height; i++) {
121  memcpy(mapBits + (destLineSize * i),map + (bytesPerLine * i), destLineSize);
122  memcpy(maskBits + (destLineSize * i),mask + (bytesPerLine * i), destLineSize);
123  }
124 
125  Pixmap cp = XCreateBitmapFromData(testLiteScreen()->display()->nativeDisplay(), rootwin, mapBits, width, height);
126  Pixmap mp = XCreateBitmapFromData(testLiteScreen()->display()->nativeDisplay(), rootwin, maskBits, width, height);
127  Cursor c = XCreatePixmapCursor(testLiteScreen()->display()->nativeDisplay(), cp, mp, &fg, &bg, spot.x(), spot.y());
128  XFreePixmap(testLiteScreen()->display()->nativeDisplay(), cp);
129  XFreePixmap(testLiteScreen()->display()->nativeDisplay(), mp);
130  delete[] mapBits;
131  delete[] maskBits;
132 
133  return c;
134 }
135 
137 {
138  Cursor cursor = 0;
139 
140  if (cshape < 0 || cshape > Qt::LastCursor)
141  return 0;
142 
143  switch (cshape) {
144  case Qt::ArrowCursor:
145  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_left_ptr);
146  break;
147  case Qt::UpArrowCursor:
148  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_center_ptr);
149  break;
150  case Qt::CrossCursor:
151  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_crosshair);
152  break;
153  case Qt::WaitCursor:
154  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_watch);
155  break;
156  case Qt::IBeamCursor:
157  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_xterm);
158  break;
159  case Qt::SizeAllCursor:
160  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_fleur);
161  break;
163  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_hand2);
164  break;
165  case Qt::SizeBDiagCursor:
166  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_top_right_corner);
167  break;
168  case Qt::SizeFDiagCursor:
169  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_bottom_right_corner);
170  break;
171  case Qt::SizeVerCursor:
172  case Qt::SplitVCursor:
173  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_sb_v_double_arrow);
174  break;
175  case Qt::SizeHorCursor:
176  case Qt::SplitHCursor:
177  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_sb_h_double_arrow);
178  break;
179  case Qt::WhatsThisCursor:
180  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_question_arrow);
181  break;
182  case Qt::ForbiddenCursor:
183  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_circle);
184  break;
185  case Qt::BusyCursor:
186  cursor = XCreateFontCursor(testLiteScreen()->display()->nativeDisplay(), XC_watch);
187  break;
188 
189  default: //default cursor for all the rest
190  break;
191  }
192  return cursor;
193 }
194 
196 {
197  return static_cast<QXlibScreen *>(screen);
198 }
199 
HCURSOR_or_HANDLE handle() const
Returns a platform-specific cursor handle.
Definition: qcursor_mac.mm:301
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition: qcursor.h:89
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
QPointer< QWidget > widget
Window rootWindow()
The QPlatformCursor class provides information about pointer device events (movement, buttons), and requests to change the currently displayed cursor.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
QXlibCursor(QXlibScreen *screen)
Definition: qxlibcursor.cpp:55
unsigned char uchar
Definition: qglobal.h:994
NSWindow * window
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
QMap< int, Cursor > cursorMap
Definition: qxlibcursor.h:63
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
void changeCursor(QCursor *cursor, QWidget *widget)
This method is called by Qt whenever the cursor graphic should be changed.
Definition: qxlibcursor.cpp:60
QPoint hotSpot() const
Returns the cursor hot spot, or (0, 0) if it is one of the standard cursors.
Definition: qcursor.cpp:540
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
Cursor createCursorShape(int cshape)
Q_GUI_EXPORT EGLNativeDisplayType nativeDisplay()
Definition: qegl_qpa.cpp:55
const QBitmap * bitmap() const
Returns the cursor bitmap, or 0 if it is one of the standard cursors.
Definition: qcursor.cpp:504
QXlibScreen * testLiteScreen() const
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QPlatformScreen * screen
Cursor createCursorBitmap(QCursor *cursor)
Definition: qxlibcursor.cpp:93
bool contains(const Key &key) const
Returns true if the map contains an item with key key; otherwise returns false.
Definition: qmap.h:553
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
const QBitmap * mask() const
Returns the cursor bitmap mask, or 0 if it is one of the standard cursors.
Definition: qcursor.cpp:516
Qt::CursorShape shape() const
Returns the cursor shape identifier.
Definition: qcursor.cpp:469
void setCursor(const Cursor &cursor)