| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: qcursor.cpp 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Implementation of QCursor class | 
|---|
| 5 | ** | 
|---|
| 6 | ** Created : 940220 | 
|---|
| 7 | ** | 
|---|
| 8 | ** Copyright (C) 1992-2003 Trolltech AS.  All rights reserved. | 
|---|
| 9 | ** | 
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit. | 
|---|
| 11 | ** | 
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License | 
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file | 
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file. | 
|---|
| 15 | ** | 
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the | 
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 19 | ** packaging of this file. | 
|---|
| 20 | ** | 
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | 
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License | 
|---|
| 23 | ** Agreement provided with the Software. | 
|---|
| 24 | ** | 
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 
|---|
| 27 | ** | 
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | 
|---|
| 29 | **   information about Qt Commercial License Agreements. | 
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | 
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 
|---|
| 32 | ** | 
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are | 
|---|
| 34 | ** not clear to you. | 
|---|
| 35 | ** | 
|---|
| 36 | **********************************************************************/ | 
|---|
| 37 |  | 
|---|
| 38 | #include "qcursor.h" | 
|---|
| 39 |  | 
|---|
| 40 | #ifndef QT_NO_CURSOR | 
|---|
| 41 |  | 
|---|
| 42 | #include "qbitmap.h" | 
|---|
| 43 | #include "qimage.h" | 
|---|
| 44 | #include "qdatastream.h" | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | /*! | 
|---|
| 48 | \class QCursor qcursor.h | 
|---|
| 49 |  | 
|---|
| 50 | \brief The QCursor class provides a mouse cursor with an arbitrary | 
|---|
| 51 | shape. | 
|---|
| 52 |  | 
|---|
| 53 | \ingroup appearance | 
|---|
| 54 | \ingroup shared | 
|---|
| 55 |  | 
|---|
| 56 | This class is mainly used to create mouse cursors that are | 
|---|
| 57 | associated with particular widgets and to get and set the position | 
|---|
| 58 | of the mouse cursor. | 
|---|
| 59 |  | 
|---|
| 60 | Qt has a number of standard cursor shapes, but you can also make | 
|---|
| 61 | custom cursor shapes based on a QBitmap, a mask and a hotspot. | 
|---|
| 62 |  | 
|---|
| 63 | To associate a cursor with a widget, use QWidget::setCursor(). To | 
|---|
| 64 | associate a cursor with all widgets (normally for a short period | 
|---|
| 65 | of time), use QApplication::setOverrideCursor(). | 
|---|
| 66 |  | 
|---|
| 67 | To set a cursor shape use QCursor::setShape() or use the QCursor | 
|---|
| 68 | constructor which takes the shape as argument, or you can use one | 
|---|
| 69 | of the predefined cursors defined in the \l CursorShape enum. | 
|---|
| 70 |  | 
|---|
| 71 | If you want to create a cursor with your own bitmap, either use | 
|---|
| 72 | the QCursor constructor which takes a bitmap and a mask or the | 
|---|
| 73 | constructor which takes a pixmap as arguments. | 
|---|
| 74 |  | 
|---|
| 75 | To set or get the position of the mouse cursor use the static | 
|---|
| 76 | methods QCursor::pos() and QCursor::setPos(). | 
|---|
| 77 |  | 
|---|
| 78 | \img cursors.png Cursor Shapes | 
|---|
| 79 |  | 
|---|
| 80 | \sa QWidget \link guibooks.html#fowler GUI Design Handbook: | 
|---|
| 81 | Cursors\endlink | 
|---|
| 82 |  | 
|---|
| 83 | On X11, Qt supports the \link | 
|---|
| 84 | http://www.xfree86.org/4.3.0/Xcursor.3.html Xcursor\endlink | 
|---|
| 85 | library, which allows for full color icon themes. The table below | 
|---|
| 86 | shows the cursor name used for each Qt::CursorShape value. If a | 
|---|
| 87 | cursor cannot be found using the name shown below, a standard X11 | 
|---|
| 88 | cursor will be used instead. Note: X11 does not provide | 
|---|
| 89 | appropriate cursors for all possible Qt::CursorShape values. It | 
|---|
| 90 | is possible that some cursors will be taken from the Xcursor | 
|---|
| 91 | theme, while others will use an internal bitmap cursor. | 
|---|
| 92 |  | 
|---|
| 93 | \table | 
|---|
| 94 | \header \i Qt::CursorShape Values   \i Cursor Names | 
|---|
| 95 | \row \i Qt::ArrowCursor             \i left_ptr | 
|---|
| 96 | \row \i Qt::UpArrowCursor           \i up_arrow | 
|---|
| 97 | \row \i Qt::CrossCursor             \i cross | 
|---|
| 98 | \row \i Qt::WaitCursor              \i wait | 
|---|
| 99 | \row \i Qt::BusyCursor              \i left_ptr_watch | 
|---|
| 100 | \row \i Qt::IbeamCursor             \i ibeam | 
|---|
| 101 | \row \i Qt::SizeVerCursor           \i size_ver | 
|---|
| 102 | \row \i Qt::SizeHorCursor           \i size_hor | 
|---|
| 103 | \row \i Qt::SizeBDiagCursor         \i size_bdiag | 
|---|
| 104 | \row \i Qt::SizeFDiagCursor         \i size_fdiag | 
|---|
| 105 | \row \i Qt::SizeAllCursor           \i size_all | 
|---|
| 106 | \row \i Qt::SplitVCursor            \i split_v | 
|---|
| 107 | \row \i Qt::SplitHCursor            \i split_h | 
|---|
| 108 | \row \i Qt::PointingHandCursor      \i pointing_hand | 
|---|
| 109 | \row \i Qt::ForbiddenCursor         \i forbidden | 
|---|
| 110 | \row \i Qt::WhatsThisCursor         \i whats_this | 
|---|
| 111 | \endtable | 
|---|
| 112 | */ | 
|---|
| 113 |  | 
|---|
| 114 | /*! | 
|---|
| 115 | \enum Qt::CursorShape | 
|---|
| 116 |  | 
|---|
| 117 | This enum type defines the various cursors that can be used. | 
|---|
| 118 |  | 
|---|
| 119 | \value ArrowCursor  standard arrow cursor | 
|---|
| 120 | \value UpArrowCursor  upwards arrow | 
|---|
| 121 | \value CrossCursor  crosshair | 
|---|
| 122 | \value WaitCursor  hourglass/watch | 
|---|
| 123 | \value BusyCursor  standard arrow with hourglass/watch | 
|---|
| 124 | \value IbeamCursor  ibeam/text entry | 
|---|
| 125 | \value SizeVerCursor  vertical resize | 
|---|
| 126 | \value SizeHorCursor  horizontal resize | 
|---|
| 127 | \value SizeFDiagCursor  diagonal resize (\) | 
|---|
| 128 | \value SizeBDiagCursor  diagonal resize (/) | 
|---|
| 129 | \value SizeAllCursor  all directions resize | 
|---|
| 130 | \value BlankCursor  blank/invisible cursor | 
|---|
| 131 | \value SplitVCursor  vertical splitting | 
|---|
| 132 | \value SplitHCursor  horizontal splitting | 
|---|
| 133 | \value PointingHandCursor  a pointing hand | 
|---|
| 134 | \value ForbiddenCursor  a slashed circle | 
|---|
| 135 | \value WhatsThisCursor  an arrow with a question mark | 
|---|
| 136 | \value BitmapCursor | 
|---|
| 137 |  | 
|---|
| 138 | ArrowCursor is the default for widgets in a normal state. | 
|---|
| 139 |  | 
|---|
| 140 | \img cursors.png Cursor Shapes | 
|---|
| 141 | */ | 
|---|
| 142 |  | 
|---|
| 143 | /***************************************************************************** | 
|---|
| 144 | QCursor stream functions | 
|---|
| 145 | *****************************************************************************/ | 
|---|
| 146 |  | 
|---|
| 147 | #ifndef QT_NO_DATASTREAM | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | /*! | 
|---|
| 151 | \relates QCursor | 
|---|
| 152 | Writes the cursor \a c to the stream \a s. | 
|---|
| 153 |  | 
|---|
| 154 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink | 
|---|
| 155 | */ | 
|---|
| 156 |  | 
|---|
| 157 | QDataStream &operator<<( QDataStream &s, const QCursor &c ) | 
|---|
| 158 | { | 
|---|
| 159 | s << (Q_INT16)c.shape();                    // write shape id to stream | 
|---|
| 160 | if ( c.shape() == Qt::BitmapCursor ) {              // bitmap cursor | 
|---|
| 161 | #if !defined(QT_NO_IMAGEIO) | 
|---|
| 162 | s << *c.bitmap() << *c.mask(); | 
|---|
| 163 | s << c.hotSpot(); | 
|---|
| 164 | #else | 
|---|
| 165 | qWarning("No Image Cursor I/O"); | 
|---|
| 166 | #endif | 
|---|
| 167 | } | 
|---|
| 168 | return s; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | /*! | 
|---|
| 172 | \relates QCursor | 
|---|
| 173 | Reads a cursor from the stream \a s and sets \a c to the read data. | 
|---|
| 174 |  | 
|---|
| 175 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink | 
|---|
| 176 | */ | 
|---|
| 177 |  | 
|---|
| 178 | QDataStream &operator>>( QDataStream &s, QCursor &c ) | 
|---|
| 179 | { | 
|---|
| 180 | Q_INT16 shape; | 
|---|
| 181 | s >> shape;                                 // read shape id from stream | 
|---|
| 182 | if ( shape == Qt::BitmapCursor ) {          // read bitmap cursor | 
|---|
| 183 | #if !defined(QT_NO_IMAGEIO) | 
|---|
| 184 | QBitmap bm, bmm; | 
|---|
| 185 | QPoint  hot; | 
|---|
| 186 | s >> bm >> bmm >> hot; | 
|---|
| 187 | c = QCursor( bm, bmm, hot.x(), hot.y() ); | 
|---|
| 188 | #else | 
|---|
| 189 | qWarning("No Image Cursor I/O"); | 
|---|
| 190 | #endif | 
|---|
| 191 | } else { | 
|---|
| 192 | c.setShape( (int)shape );               // create cursor with shape | 
|---|
| 193 | } | 
|---|
| 194 | return s; | 
|---|
| 195 | } | 
|---|
| 196 | #endif // QT_NO_DATASTREAM | 
|---|
| 197 |  | 
|---|
| 198 |  | 
|---|
| 199 | /*! | 
|---|
| 200 | Constructs a custom pixmap cursor. | 
|---|
| 201 |  | 
|---|
| 202 | \a pixmap is the image. It is usual to give it a mask (set using | 
|---|
| 203 | QPixmap::setMask()). \a hotX and \a hotY define the cursor's hot | 
|---|
| 204 | spot. | 
|---|
| 205 |  | 
|---|
| 206 | If \a hotX is negative, it is set to the \c{pixmap().width()/2}. | 
|---|
| 207 | If \a hotY is negative, it is set to the \c{pixmap().height()/2}. | 
|---|
| 208 |  | 
|---|
| 209 | Valid cursor sizes depend on the display hardware (or the | 
|---|
| 210 | underlying window system). We recommend using 32x32 cursors, | 
|---|
| 211 | because this size is supported on all platforms. Some platforms | 
|---|
| 212 | also support 16x16, 48x48 and 64x64 cursors. | 
|---|
| 213 |  | 
|---|
| 214 | Currently, only black-and-white pixmaps can be used. | 
|---|
| 215 |  | 
|---|
| 216 | \sa QPixmap::QPixmap(), QPixmap::setMask() | 
|---|
| 217 | */ | 
|---|
| 218 |  | 
|---|
| 219 | QCursor::QCursor( const QPixmap &pixmap, int hotX, int hotY ) | 
|---|
| 220 | { | 
|---|
| 221 | QImage img = pixmap.convertToImage(). | 
|---|
| 222 | convertDepth( 8, Qt::ThresholdDither|Qt::AvoidDither ); | 
|---|
| 223 | QBitmap bm; | 
|---|
| 224 | bm.convertFromImage( img, Qt::ThresholdDither|Qt::AvoidDither ); | 
|---|
| 225 | QBitmap bmm; | 
|---|
| 226 | if ( bm.mask() ) { | 
|---|
| 227 | bmm = *bm.mask(); | 
|---|
| 228 | QBitmap nullBm; | 
|---|
| 229 | bm.setMask( nullBm ); | 
|---|
| 230 | } | 
|---|
| 231 | else if ( pixmap.mask() ) { | 
|---|
| 232 | QImage mimg = pixmap.mask()->convertToImage(). | 
|---|
| 233 | convertDepth( 8, Qt::ThresholdDither|Qt::AvoidDither ); | 
|---|
| 234 | bmm.convertFromImage( mimg, Qt::ThresholdDither|Qt::AvoidDither ); | 
|---|
| 235 | } | 
|---|
| 236 | else { | 
|---|
| 237 | bmm.resize( bm.size() ); | 
|---|
| 238 | bmm.fill( Qt::color1 ); | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | setBitmap(bm,bmm,hotX,hotY); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 |  | 
|---|
| 245 |  | 
|---|
| 246 | /*! | 
|---|
| 247 | Constructs a custom bitmap cursor. | 
|---|
| 248 |  | 
|---|
| 249 | \a bitmap and | 
|---|
| 250 | \a mask make up the bitmap. | 
|---|
| 251 | \a hotX and | 
|---|
| 252 | \a hotY define the cursor's hot spot. | 
|---|
| 253 |  | 
|---|
| 254 | If \a hotX is negative, it is set to the \c{bitmap().width()/2}. | 
|---|
| 255 | If \a hotY is negative, it is set to the \c{bitmap().height()/2}. | 
|---|
| 256 |  | 
|---|
| 257 | The cursor \a bitmap (B) and \a mask (M) bits are combined like this: | 
|---|
| 258 | \list | 
|---|
| 259 | \i B=1 and M=1 gives black. | 
|---|
| 260 | \i B=0 and M=1 gives white. | 
|---|
| 261 | \i B=0 and M=0 gives transparent. | 
|---|
| 262 | \i B=1 and M=0 gives an undefined result. | 
|---|
| 263 | \endlist | 
|---|
| 264 |  | 
|---|
| 265 | Use the global Qt color \c color0 to draw 0-pixels and \c color1 to | 
|---|
| 266 | draw 1-pixels in the bitmaps. | 
|---|
| 267 |  | 
|---|
| 268 | Valid cursor sizes depend on the display hardware (or the | 
|---|
| 269 | underlying window system). We recommend using 32x32 cursors, | 
|---|
| 270 | because this size is supported on all platforms. Some platforms | 
|---|
| 271 | also support 16x16, 48x48 and 64x64 cursors. | 
|---|
| 272 |  | 
|---|
| 273 | \sa QBitmap::QBitmap(), QBitmap::setMask() | 
|---|
| 274 | */ | 
|---|
| 275 |  | 
|---|
| 276 | QCursor::QCursor( const QBitmap &bitmap, const QBitmap &mask, | 
|---|
| 277 | int hotX, int hotY ) | 
|---|
| 278 | { | 
|---|
| 279 | setBitmap(bitmap,mask,hotX,hotY); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | #endif // QT_NO_CURSOR | 
|---|
| 283 |  | 
|---|
| 284 |  | 
|---|