| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: cursor.h |
| 3 | // Purpose: interface of wxCursor |
| 4 | // Author: wxWidgets team |
| 5 | // RCS-ID: $Id$ |
| 6 | // Licence: wxWindows license |
| 7 | ///////////////////////////////////////////////////////////////////////////// |
| 8 | |
| 9 | /** |
| 10 | @class wxCursor |
| 11 | |
| 12 | A cursor is a small bitmap usually used for denoting where the mouse |
| 13 | pointer is, with a picture that might indicate the interpretation of a |
| 14 | mouse click. As with icons, cursors in X and MS Windows are created in a |
| 15 | different manner. Therefore, separate cursors will be created for the |
| 16 | different environments. Platform-specific methods for creating a wxCursor |
| 17 | object are catered for, and this is an occasion where conditional |
| 18 | compilation will probably be required (see wxIcon for an example). |
| 19 | |
| 20 | A single cursor object may be used in many windows (any subwindow type). |
| 21 | The wxWidgets convention is to set the cursor for a window, as in X, rather |
| 22 | than to set it globally as in MS Windows, although a global wxSetCursor() |
| 23 | function is also available for MS Windows use. |
| 24 | |
| 25 | @section cursor_custom Creating a Custom Cursor |
| 26 | |
| 27 | The following is an example of creating a cursor from 32x32 bitmap data |
| 28 | (down_bits) and a mask (down_mask) where 1 is black and 0 is white for the |
| 29 | bits, and 1 is opaque and 0 is transparent for the mask. |
| 30 | It works on Windows and GTK+. |
| 31 | |
| 32 | @code |
| 33 | static char down_bits[] = { 255, 255, 255, 255, 31, |
| 34 | 255, 255, 255, 31, 255, 255, 255, 31, 255, 255, 255, |
| 35 | 31, 255, 255, 255, 31, 255, 255, 255, 31, 255, 255, |
| 36 | 255, 31, 255, 255, 255, 31, 255, 255, 255, 25, 243, |
| 37 | 255, 255, 19, 249, 255, 255, 7, 252, 255, 255, 15, 254, |
| 38 | 255, 255, 31, 255, 255, 255, 191, 255, 255, 255, 255, |
| 39 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 40 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 41 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 42 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 43 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 44 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 45 | 255 }; |
| 46 | |
| 47 | static char down_mask[] = { 240, 1, 0, 0, 240, 1, |
| 48 | 0, 0, 240, 1, 0, 0, 240, 1, 0, 0, 240, 1, 0, 0, 240, 1, |
| 49 | 0, 0, 240, 1, 0, 0, 240, 1, 0, 0, 255, 31, 0, 0, 255, |
| 50 | 31, 0, 0, 254, 15, 0, 0, 252, 7, 0, 0, 248, 3, 0, 0, |
| 51 | 240, 1, 0, 0, 224, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, |
| 52 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 53 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 54 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 55 | 0, 0, 0, 0, 0 }; |
| 56 | |
| 57 | #ifdef __WXMSW__ |
| 58 | wxBitmap down_bitmap(down_bits, 32, 32); |
| 59 | wxBitmap down_mask_bitmap(down_mask, 32, 32); |
| 60 | |
| 61 | down_bitmap.SetMask(new wxMask(down_mask_bitmap)); |
| 62 | wxImage down_image = down_bitmap.ConvertToImage(); |
| 63 | down_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 6); |
| 64 | down_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 14); |
| 65 | wxCursor down_cursor = wxCursor(down_image); |
| 66 | #elif defined(__WXGTK__) or defined(__WXMOTIF__) |
| 67 | wxCursor down_cursor = wxCursor(down_bits, 32, 32, 6, 14, |
| 68 | down_mask, wxWHITE, wxBLACK); |
| 69 | #endif |
| 70 | @endcode |
| 71 | |
| 72 | @library{wxcore} |
| 73 | @category{gdi} |
| 74 | |
| 75 | @stdobjects |
| 76 | - ::wxNullCursor |
| 77 | - ::wxSTANDARD_CURSOR |
| 78 | - ::wxHOURGLASS_CURSOR |
| 79 | - ::wxCROSS_CURSOR |
| 80 | |
| 81 | @see wxBitmap, wxIcon, wxWindow::SetCursor(), wxSetCursor(), ::wxStockCursor |
| 82 | */ |
| 83 | class wxCursor : public wxGDIObject |
| 84 | { |
| 85 | public: |
| 86 | /** |
| 87 | Default constructor. |
| 88 | */ |
| 89 | wxCursor(); |
| 90 | |
| 91 | /** |
| 92 | Constructs a cursor by passing an array of bits (XBM data). |
| 93 | |
| 94 | The parameters @a fg and @a bg have an effect only on GTK+, and force |
| 95 | the cursor to use particular background and foreground colours. |
| 96 | |
| 97 | If either @a hotSpotX or @a hotSpotY is -1, the hotspot will be the |
| 98 | centre of the cursor image (Motif only). |
| 99 | |
| 100 | @param bits |
| 101 | An array of XBM data bits. |
| 102 | @param width |
| 103 | Cursor width. |
| 104 | @param height |
| 105 | Cursor height. |
| 106 | @param hotSpotX |
| 107 | Hotspot x coordinate. |
| 108 | @param hotSpotY |
| 109 | Hotspot y coordinate. |
| 110 | @param maskBits |
| 111 | Bits for a mask bitmap. |
| 112 | |
| 113 | @onlyfor{wxgtk,wxmotif} |
| 114 | */ |
| 115 | wxCursor(const char bits[], int width, int height, |
| 116 | int hotSpotX = -1, int hotSpotY = -1, |
| 117 | const char maskBits[] = NULL); |
| 118 | |
| 119 | /** |
| 120 | Constructs a cursor by passing a string resource name or filename. |
| 121 | |
| 122 | @note |
| 123 | On MacOS when specifying a string resource name, first the color |
| 124 | cursors 'crsr' and then the black/white cursors 'CURS' in the resource |
| 125 | chain are scanned through. |
| 126 | |
| 127 | @a hotSpotX and @a hotSpotY are currently only used under Windows when |
| 128 | loading from an icon file, to specify the cursor hotspot relative to |
| 129 | the top left of the image. |
| 130 | |
| 131 | @param cursorName |
| 132 | The name of the resource or the image file to load. |
| 133 | @param type |
| 134 | Icon type to load. It defaults to wxCURSOR_DEFAULT_TYPE, |
| 135 | which is a @#define associated to different values on different |
| 136 | platforms: |
| 137 | - under Windows, it defaults to wxBITMAP_TYPE_CUR_RESOURCE. |
| 138 | - under MacOS, it defaults to wxBITMAP_TYPE_MACCURSOR_RESOURCE. |
| 139 | - under GTK, it defaults to wxBITMAP_TYPE_XPM. |
| 140 | - under X11, it defaults to wxBITMAP_TYPE_XPM. |
| 141 | - under Motif, type defaults to wxBITMAP_TYPE_XBM. |
| 142 | Under Windows, the permitted types are: |
| 143 | - wxBITMAP_TYPE_CUR - Load a cursor from a .cur cursor file (only |
| 144 | if USE_RESOURCE_LOADING_IN_MSW is enabled in |
| 145 | setup.h). |
| 146 | - wxBITMAP_TYPE_CUR_RESOURCE - Load a Windows resource |
| 147 | (as specified in the .rc file). |
| 148 | - wxBITMAP_TYPE_ICO - Load a cursor from a .ico icon file (only if |
| 149 | USE_RESOURCE_LOADING_IN_MSW is enabled in |
| 150 | setup.h). Specify @a hotSpotX and @a hotSpotY. |
| 151 | @param hotSpotX |
| 152 | Hotspot x coordinate. |
| 153 | @param hotSpotY |
| 154 | Hotspot y coordinate. |
| 155 | */ |
| 156 | wxCursor(const wxString& cursorName, |
| 157 | wxBitmapType type = wxCURSOR_DEFAULT_TYPE, |
| 158 | int hotSpotX = 0, int hotSpotY = 0); |
| 159 | |
| 160 | /** |
| 161 | Constructs a cursor using a cursor identifier. |
| 162 | |
| 163 | @param cursorId |
| 164 | A stock cursor identifier. See ::wxStockCursor. |
| 165 | */ |
| 166 | wxCursor(wxStockCursor cursorId); |
| 167 | |
| 168 | /** |
| 169 | Constructs a cursor from a wxImage. If cursor are monochrome on the |
| 170 | current platform, colors with the RGB elements all greater than 127 |
| 171 | will be foreground, colors less than this background. The mask (if any) |
| 172 | will be used to specify the transparent area. |
| 173 | |
| 174 | In wxMSW the foreground will be white and the background black. |
| 175 | If the cursor is larger than 32x32 it is resized. |
| 176 | |
| 177 | In wxGTK, colour cursors and alpha channel are supported (starting from |
| 178 | GTK+ 2.2). Otherwise the two most frequent colors will be used for |
| 179 | foreground and background. In any case, the cursor will be displayed |
| 180 | at the size of the image. |
| 181 | |
| 182 | In wxMac, if the cursor is larger than 16x16 it is resized and |
| 183 | currently only shown as black/white (mask respected). |
| 184 | */ |
| 185 | wxCursor(const wxImage& image); |
| 186 | |
| 187 | /** |
| 188 | Copy constructor, uses @ref overview_refcount "reference counting". |
| 189 | |
| 190 | @param cursor |
| 191 | Pointer or reference to a cursor to copy. |
| 192 | */ |
| 193 | wxCursor(const wxCursor& cursor); |
| 194 | |
| 195 | /** |
| 196 | Destroys the cursor. See |
| 197 | @ref overview_refcount_destruct "reference-counted object destruction" |
| 198 | for more info. |
| 199 | |
| 200 | A cursor can be reused for more than one window, and does not get |
| 201 | destroyed when the window is destroyed. wxWidgets destroys all cursors |
| 202 | on application exit, although it is best to clean them up explicitly. |
| 203 | */ |
| 204 | virtual ~wxCursor(); |
| 205 | |
| 206 | /** |
| 207 | Returns @true if cursor data is present. |
| 208 | */ |
| 209 | virtual bool IsOk() const; |
| 210 | |
| 211 | /** |
| 212 | Assignment operator, using @ref overview_refcount "reference counting". |
| 213 | */ |
| 214 | wxCursor& operator =(const wxCursor& cursor); |
| 215 | }; |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | @name Predefined cursors. |
| 220 | |
| 221 | @see wxStockCursor |
| 222 | */ |
| 223 | //@{ |
| 224 | wxCursor wxNullCursor; |
| 225 | wxCursor* wxSTANDARD_CURSOR; |
| 226 | wxCursor* wxHOURGLASS_CURSOR; |
| 227 | wxCursor* wxCROSS_CURSOR; |
| 228 | //@} |
| 229 | |