cursor.h 9.0 KB

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