2009-11-19 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.CarbonInternal / Cursor.cs
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 (gnorton@novell.com)
24 //
25 //
26
27
28 using System;
29 using System.Drawing;
30 using System.Windows.Forms;
31 using System.Runtime.InteropServices;
32
33 namespace System.Windows.Forms.CarbonInternal {
34         internal class Cursor {
35                 internal static CarbonCursor defcur = new CarbonCursor (StdCursor.Default);
36
37                 internal static Bitmap DefineStdCursorBitmap (StdCursor id) {
38                         // FIXME
39                         return new Bitmap (16, 16);
40                 }
41                 internal static IntPtr DefineCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
42                         CarbonCursor cc = new CarbonCursor (bitmap, mask, cursor_pixel, mask_pixel, xHotSpot, yHotSpot);
43
44                         return (IntPtr) GCHandle.Alloc (cc);
45                 }
46                 internal static IntPtr DefineStdCursor (StdCursor id) {
47                         CarbonCursor cc = new CarbonCursor (id);
48                 
49                         return (IntPtr) GCHandle.Alloc (cc);
50                 }
51                 internal static void SetCursor (IntPtr cursor) {
52                         if (cursor == IntPtr.Zero) {
53                                 defcur.SetCursor ();
54                                 return;
55                         }
56
57                         CarbonCursor cc = (CarbonCursor) ((GCHandle) cursor).Target;
58
59                         cc.SetCursor ();
60                 }
61         }
62
63         internal struct CarbonCursor {
64                 private Bitmap bmp;
65                 private Bitmap mask;
66                 private Color cursor_color;
67                 private Color mask_color;
68                 private int hot_x;
69                 private int hot_y;
70                 private StdCursor id;
71                 private bool standard;
72
73                 public CarbonCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
74                         this.id = StdCursor.Default;
75                         this.bmp = bitmap;
76                         this.mask = mask;
77                         this.cursor_color = cursor_pixel;
78                         this.mask_color = mask_pixel;
79                         this.hot_x = xHotSpot;
80                         this.hot_y = yHotSpot;
81                         standard = true;
82                 }
83
84                 public CarbonCursor (StdCursor id) {
85                         this.id = id;
86                         this.bmp = null;
87                         this.mask = null;
88                         this.cursor_color = Color.Black;
89                         this.mask_color = Color.Black;
90                         this.hot_x = 0;
91                         this.hot_y = 0;
92                         standard = true;
93                 }
94
95                 public StdCursor StdCursor {
96                         get {
97                                 return id;
98                         }
99                 }
100
101                 public Bitmap Bitmap {
102                         get { 
103                                 return bmp;
104                         }
105                 }
106
107                 public Bitmap Mask {
108                         get { 
109                                 return mask;
110                         }
111                 }
112
113                 public Color CursorColor {
114                         get {
115                                 return cursor_color;
116                         }
117                 }
118
119                 public Color MaskColor {
120                         get {
121                                 return mask_color;
122                         }
123                 }
124
125                 public int HotSpotX {
126                         get { 
127                                 return hot_x;
128                         }
129                 }
130
131                 public int HotSpotY {
132                         get { 
133                                 return hot_y;
134                         }
135                 }
136
137                 public void SetCursor () {
138                         if (standard)
139                                 SetStandardCursor ();
140                         else    
141                                 SetCustomCursor ();
142                 }
143
144                 public void SetCustomCursor () {
145                         throw new NotImplementedException ("We dont support custom cursors yet");
146                 }
147
148                 public void SetStandardCursor () {
149                         switch (id) {
150                                 case StdCursor.AppStarting:
151                                         SetThemeCursor (ThemeCursor.kThemeSpinningCursor);
152                                         break;
153                                 case StdCursor.Arrow:
154                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
155                                         break;
156                                 case StdCursor.Cross:
157                                         SetThemeCursor (ThemeCursor.kThemeCrossCursor);
158                                         break;
159                                 case StdCursor.Default:
160                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
161                                         break;
162                                 case StdCursor.Hand:
163                                         SetThemeCursor (ThemeCursor.kThemeOpenHandCursor);
164                                         break;
165                                 case StdCursor.Help:
166                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
167                                         break;
168                                 case StdCursor.HSplit:
169                                         SetThemeCursor (ThemeCursor.kThemeResizeLeftRightCursor);
170                                         break;
171                                 case StdCursor.IBeam:
172                                         SetThemeCursor (ThemeCursor.kThemeIBeamCursor);
173                                         break;
174                                 case StdCursor.No:
175                                         SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
176                                         break;
177                                 case StdCursor.NoMove2D:
178                                         SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
179                                         break;
180                                 case StdCursor.NoMoveHoriz:
181                                         SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
182                                         break;
183                                 case StdCursor.NoMoveVert:
184                                         SetThemeCursor (ThemeCursor.kThemeNotAllowedCursor);
185                                         break;
186                                 case StdCursor.PanEast:
187                                         SetThemeCursor (ThemeCursor.kThemeResizeRightCursor);
188                                         break;
189                                 case StdCursor.PanNE:
190                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
191                                         break;
192                                 case StdCursor.PanNorth:
193                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
194                                         break;
195                                 case StdCursor.PanNW:
196                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
197                                         break;
198                                 case StdCursor.PanSE:
199                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
200                                         break;
201                                 case StdCursor.PanSouth:
202                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
203                                         break;
204                                 case StdCursor.PanSW:
205                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
206                                         break;
207                                 case StdCursor.PanWest:
208                                         SetThemeCursor (ThemeCursor.kThemeResizeLeftCursor);
209                                         break;
210                                 case StdCursor.SizeAll:
211                                         SetThemeCursor (ThemeCursor.kThemeResizeLeftRightCursor);
212                                         break;
213                                 case StdCursor.SizeNESW:
214                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
215                                         break;
216                                 case StdCursor.SizeNS:
217                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
218                                         break;
219                                 case StdCursor.SizeNWSE:
220                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
221                                         break;
222                                 case StdCursor.SizeWE:
223                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
224                                         break;
225                                 case StdCursor.UpArrow:
226                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
227                                         break;
228                                 case StdCursor.VSplit:
229                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
230                                         break;
231                                 case StdCursor.WaitCursor:
232                                         SetThemeCursor (ThemeCursor.kThemeSpinningCursor);
233                                         break;
234                                 default:
235                                         SetThemeCursor (ThemeCursor.kThemeArrowCursor);
236                                         break;
237                         }
238                         return;
239                 }
240
241                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
242                 static extern int SetThemeCursor (ThemeCursor cursor);
243
244         }
245 }