Cursor.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2008 Novell, Inc.
  21. //
  22. // Authors:
  23. // Geoff Norton ([email protected])
  24. //
  25. //
  26. using System;
  27. using System.Drawing;
  28. using System.Windows.Forms;
  29. using System.Runtime.InteropServices;
  30. namespace System.Windows.Forms.CarbonInternal {
  31. internal class Cursor {
  32. internal static CarbonCursor defcur = new CarbonCursor (StdCursor.Default);
  33. internal static Bitmap DefineStdCursorBitmap (StdCursor id) {
  34. // FIXME
  35. return new Bitmap (16, 16);
  36. }
  37. internal static IntPtr DefineCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
  38. CarbonCursor cc = new CarbonCursor (bitmap, mask, cursor_pixel, mask_pixel, xHotSpot, yHotSpot);
  39. return (IntPtr) GCHandle.Alloc (cc);
  40. }
  41. internal static IntPtr DefineStdCursor (StdCursor id) {
  42. CarbonCursor cc = new CarbonCursor (id);
  43. return (IntPtr) GCHandle.Alloc (cc);
  44. }
  45. internal static void SetCursor (IntPtr cursor) {
  46. if (cursor == IntPtr.Zero) {
  47. defcur.SetCursor ();
  48. return;
  49. }
  50. CarbonCursor cc = (CarbonCursor) ((GCHandle) cursor).Target;
  51. cc.SetCursor ();
  52. }
  53. }
  54. internal struct CarbonCursor {
  55. private Bitmap bmp;
  56. private Bitmap mask;
  57. private Color cursor_color;
  58. private Color mask_color;
  59. private int hot_x;
  60. private int hot_y;
  61. private StdCursor id;
  62. private bool standard;
  63. public CarbonCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
  64. this.id = StdCursor.Default;
  65. this.bmp = bitmap;
  66. this.mask = mask;
  67. this.cursor_color = cursor_pixel;
  68. this.mask_color = mask_pixel;
  69. this.hot_x = xHotSpot;
  70. this.hot_y = yHotSpot;
  71. standard = true;
  72. }
  73. public CarbonCursor (StdCursor id) {
  74. this.id = id;
  75. this.bmp = null;
  76. this.mask = null;
  77. this.cursor_color = Color.Black;
  78. this.mask_color = Color.Black;
  79. this.hot_x = 0;
  80. this.hot_y = 0;
  81. standard = true;
  82. }
  83. public StdCursor StdCursor {
  84. get {
  85. return id;
  86. }
  87. }
  88. public Bitmap Bitmap {
  89. get {
  90. return bmp;
  91. }
  92. }
  93. public Bitmap Mask {
  94. get {
  95. return mask;
  96. }
  97. }
  98. public Color CursorColor {
  99. get {
  100. return cursor_color;
  101. }
  102. }
  103. public Color MaskColor {
  104. get {
  105. return mask_color;
  106. }
  107. }
  108. public int HotSpotX {
  109. get {
  110. return hot_x;
  111. }
  112. }
  113. public int HotSpotY {
  114. get {
  115. return hot_y;
  116. }
  117. }
  118. public void SetCursor () {
  119. if (standard)
  120. SetStandardCursor ();
  121. else
  122. SetCustomCursor ();
  123. }
  124. public void SetCustomCursor () {
  125. throw new NotImplementedException ("We dont support custom cursors yet");
  126. }
  127. public void SetStandardCursor () {
  128. switch (id) {
  129. case StdCursor.AppStarting:
  130. SetThemeCursor (ThemeCursor.kThemeSpinningCursor);
  131. break;
  132. case StdCursor.Arrow:
  133. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  134. break;
  135. case StdCursor.Cross:
  136. SetThemeCursor (ThemeCursor.kThemeCrossCursor);
  137. break;
  138. case StdCursor.Default:
  139. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  140. break;
  141. case StdCursor.Hand:
  142. SetThemeCursor (ThemeCursor.kThemeOpenHandCursor);
  143. break;
  144. case StdCursor.Help:
  145. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  146. break;
  147. case StdCursor.HSplit:
  148. SetThemeCursor (ThemeCursor.kThemeResizeLeftRightCursor);
  149. break;
  150. case StdCursor.IBeam:
  151. SetThemeCursor (ThemeCursor.kThemeIBeamCursor);
  152. break;
  153. case StdCursor.No:
  154. SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
  155. break;
  156. case StdCursor.NoMove2D:
  157. SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
  158. break;
  159. case StdCursor.NoMoveHoriz:
  160. SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
  161. break;
  162. case StdCursor.NoMoveVert:
  163. SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
  164. break;
  165. case StdCursor.PanEast:
  166. SetThemeCursor (ThemeCursor.kThemeResizeRightCursor);
  167. break;
  168. case StdCursor.PanNE:
  169. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  170. break;
  171. case StdCursor.PanNorth:
  172. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  173. break;
  174. case StdCursor.PanNW:
  175. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  176. break;
  177. case StdCursor.PanSE:
  178. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  179. break;
  180. case StdCursor.PanSouth:
  181. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  182. break;
  183. case StdCursor.PanSW:
  184. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  185. break;
  186. case StdCursor.PanWest:
  187. SetThemeCursor (ThemeCursor.kThemeResizeLeftCursor);
  188. break;
  189. case StdCursor.SizeAll:
  190. SetThemeCursor (ThemeCursor.kThemeResizeLeftRightCursor);
  191. break;
  192. case StdCursor.SizeNESW:
  193. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  194. break;
  195. case StdCursor.SizeNS:
  196. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  197. break;
  198. case StdCursor.SizeNWSE:
  199. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  200. break;
  201. case StdCursor.SizeWE:
  202. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  203. break;
  204. case StdCursor.UpArrow:
  205. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  206. break;
  207. case StdCursor.VSplit:
  208. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  209. break;
  210. case StdCursor.WaitCursor:
  211. SetThemeCursor (ThemeCursor.kThemeSpinningCursor);
  212. break;
  213. default:
  214. SetThemeCursor (ThemeCursor.kThemeArrowCursor);
  215. break;
  216. }
  217. return;
  218. }
  219. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  220. static extern int SetThemeCursor (ThemeCursor cursor);
  221. }
  222. }