Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_win32 / ecore_win32_cursor.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #define WIN32_LEAN_AND_MEAN
6 #include <windows.h>
7 #undef WIN32_LEAN_AND_MEAN
8
9 #include <Eina.h>
10
11 #include "Ecore_Win32.h"
12 #include "ecore_win32_private.h"
13
14 /*============================================================================*
15  *                                  Local                                     *
16  *============================================================================*/
17
18
19 /*============================================================================*
20  *                                 Global                                     *
21  *============================================================================*/
22
23
24 /*============================================================================*
25  *                                   API                                      *
26  *============================================================================*/
27
28 /**
29  * @addtogroup Ecore_Win32_Group Ecore_Win32 library
30  *
31  * @{
32  */
33
34 /**
35  * @brief Create a new cursor.
36  *
37  * @param pixels_and The array of bytes containing the bit values for
38  * the AND mask of the cursor.
39  * @param pixels_xor The array of bytes containing the bit values for
40  * the XOR mask of the cursor.
41  * @param width The width of the cursor.
42  * @param height The height of the cursor.
43  * @param hot_x The horizontal position of the cursor's hot spot.
44  * @param hot_y The vertical position of the cursor's hot spot.
45  * @return A newly user-defined cursor.
46  *
47  * This function creates a new cursor of size @p width and @p
48  * height. They must be valid size. To determine the valid size of a
49  * cursor, use ecore_win32_cursor_size_get(). @p pixels_and is an array
50  * of bytes (unsigned char) containing the bits of the cursor that
51  * will be visible. @p pixels_xor is similar but will allow the cursor
52  * to have a shape. Here is the truth table for the masks:
53  *
54  * <table border=1>
55  * <tr><td>AND mask</td><td>XOR mask</td><td>Display</td></tr>
56  * <tr><td>0</td>       <td>0</td>       <td>Black</td></tr>
57  * <tr><td>0</td>       <td>1</td>       <td>White</td></tr>
58  * <tr><td>1</td>       <td>0</td>       <td>Screen</td></tr>
59  * <tr><td>1</td>       <td>1</td>       <td>Reverse screen</td></tr>
60  * </table>
61  *
62  * @p hot_x and @p hot_y are the position of the hot spot of the
63  * cursor. If @p pixels_and or @p pixels_xor are @c NULL, the function
64  * returns NULL. If @p width or @p height does not match the valid
65  * size of a cursor, the function returns @c NULL. On success, the
66  * function creates a user-defined cursor, otherwise it returns
67  * @c NULL.
68  *
69  * Once the cursor is not used anymore, use ecore_win32_cursor_free()
70  * to free the ressources.
71  *
72  * Example of use:
73  *
74  * @code
75  * unsigned char pixels_and[] ={
76  * 0xFF, 0xFC, 0x3F, 0xFF,   // line 1
77  * 0xFF, 0xC0, 0x1F, 0xFF,   // line 2
78  * 0xFF, 0x00, 0x3F, 0xFF,   // line 3
79  * 0xFE, 0x00, 0xFF, 0xFF,   // line 4
80  *
81  * 0xF7, 0x01, 0xFF, 0xFF,   // line 5
82  * 0xF0, 0x03, 0xFF, 0xFF,   // line 6
83  * 0xF0, 0x03, 0xFF, 0xFF,   // line 7
84  * 0xE0, 0x07, 0xFF, 0xFF,   // line 8
85  *
86  * 0xC0, 0x07, 0xFF, 0xFF,   // line 9
87  * 0xC0, 0x0F, 0xFF, 0xFF,   // line 10
88  * 0x80, 0x0F, 0xFF, 0xFF,   // line 11
89  * 0x80, 0x0F, 0xFF, 0xFF,   // line 12
90  *
91  * 0x80, 0x07, 0xFF, 0xFF,   // line 13
92  * 0x00, 0x07, 0xFF, 0xFF,   // line 14
93  * 0x00, 0x03, 0xFF, 0xFF,   // line 15
94  * 0x00, 0x00, 0xFF, 0xFF,   // line 16
95  *
96  * 0x00, 0x00, 0x7F, 0xFF,   // line 17
97  * 0x00, 0x00, 0x1F, 0xFF,   // line 18
98  * 0x00, 0x00, 0x0F, 0xFF,   // line 19
99  * 0x80, 0x00, 0x0F, 0xFF,   // line 20
100  *
101  * 0x80, 0x00, 0x07, 0xFF,   // line 21
102  * 0x80, 0x00, 0x07, 0xFF,   // line 22
103  * 0xC0, 0x00, 0x07, 0xFF,   // line 23
104  * 0xC0, 0x00, 0x0F, 0xFF,   // line 24
105  *
106  * 0xE0, 0x00, 0x0F, 0xFF,   // line 25
107  * 0xF0, 0x00, 0x1F, 0xFF,   // line 26
108  * 0xF0, 0x00, 0x1F, 0xFF,   // line 27
109  * 0xF8, 0x00, 0x3F, 0xFF,   // line 28
110  *
111  * 0xFE, 0x00, 0x7F, 0xFF,   // line 29
112  * 0xFF, 0x00, 0xFF, 0xFF,   // line 30
113  * 0xFF, 0xC3, 0xFF, 0xFF,   // line 31
114  * 0xFF, 0xFF, 0xFF, 0xFF    // line 32
115  * };
116  *
117  * unsigned char pixels_xor[] = {
118  * 0x00, 0x00, 0x00, 0x00,   // line 1
119  * 0x00, 0x03, 0xC0, 0x00,   // line 2
120  * 0x00, 0x3F, 0x00, 0x00,   // line 3
121  * 0x00, 0xFE, 0x00, 0x00,   // line 4
122  *
123  * 0x0E, 0xFC, 0x00, 0x00,   // line 5
124  * 0x07, 0xF8, 0x00, 0x00,   // line 6
125  * 0x07, 0xF8, 0x00, 0x00,   // line 7
126  * 0x0F, 0xF0, 0x00, 0x00,   // line 8
127  *
128  * 0x1F, 0xF0, 0x00, 0x00,   // line 9
129  * 0x1F, 0xE0, 0x00, 0x00,   // line 10
130  * 0x3F, 0xE0, 0x00, 0x00,   // line 11
131  * 0x3F, 0xE0, 0x00, 0x00,   // line 12
132  *
133  * 0x3F, 0xF0, 0x00, 0x00,   // line 13
134  * 0x7F, 0xF0, 0x00, 0x00,   // line 14
135  * 0x7F, 0xF8, 0x00, 0x00,   // line 15
136  * 0x7F, 0xFC, 0x00, 0x00,   // line 16
137  *
138  * 0x7F, 0xFF, 0x00, 0x00,   // line 17
139  * 0x7F, 0xFF, 0x80, 0x00,   // line 18
140  * 0x7F, 0xFF, 0xE0, 0x00,   // line 19
141  * 0x3F, 0xFF, 0xE0, 0x00,   // line 20
142  *
143  * 0x3F, 0xC7, 0xF0, 0x00,   // line 21
144  * 0x3F, 0x83, 0xF0, 0x00,   // line 22
145  * 0x1F, 0x83, 0xF0, 0x00,   // line 23
146  * 0x1F, 0x83, 0xE0, 0x00,   // line 24
147  *
148  * 0x0F, 0xC7, 0xE0, 0x00,   // line 25
149  * 0x07, 0xFF, 0xC0, 0x00,   // line 26
150  * 0x07, 0xFF, 0xC0, 0x00,   // line 27
151  * 0x01, 0xFF, 0x80, 0x00,   // line 28
152  *
153  * 0x00, 0xFF, 0x00, 0x00,   // line 29
154  * 0x00, 0x3C, 0x00, 0x00,   // line 30
155  * 0x00, 0x00, 0x00, 0x00,   // line 31
156  * 0x00, 0x00, 0x00, 0x00    // line 32
157  * };
158  *
159  * Ecore_Win32_Cursor *cursor = ecore_win32_cursor_new(pixels_and, pixels_xor, 32, 32, 19, 2);
160  * @endcode
161  */
162 EAPI Ecore_Win32_Cursor *
163 ecore_win32_cursor_new(const void *pixels_and,
164                        const void *pixels_xor,
165                        int         width,
166                        int         height,
167                        int         hot_x,
168                        int         hot_y)
169 {
170    Ecore_Win32_Cursor *cursor = NULL;
171    int                 cursor_width;
172    int                 cursor_height;
173
174    INF("creating cursor");
175
176    if (!pixels_and || !pixels_xor)
177      return NULL;
178
179    cursor_width = GetSystemMetrics(SM_CXCURSOR);
180    cursor_height = GetSystemMetrics(SM_CYCURSOR);
181
182    if ((cursor_width != width) ||
183        (cursor_height != height))
184      return NULL;
185
186    if (!(cursor = CreateCursor(_ecore_win32_instance,
187                                hot_x, hot_y,
188                                width, height,
189                                pixels_and,
190                                pixels_xor)))
191      return NULL;
192
193    return cursor;
194 }
195
196 /**
197  * @brief Free the given cursor.
198  *
199  * @param cursor The cursor to free.
200  *
201  * This function free @p cursor. @p cursor must have been obtained
202  * with ecore_win32_cursor_new().
203  */
204 EAPI void
205 ecore_win32_cursor_free(Ecore_Win32_Cursor *cursor)
206 {
207    INF("destroying cursor");
208
209    DestroyCursor(cursor);
210 }
211
212 /**
213  * @brief Create a cursor from a Windows ressource.
214  *
215  * @param shape The pre-defined shape of the cursor.
216  * @return The new cursor.
217  *
218  * This function returns a pre-defined cursor with a specified
219  * @p shape. This cursor does not need to be freed, as it is loaded
220  * from an existing resource.
221  */
222 EAPI Ecore_Win32_Cursor *
223 ecore_win32_cursor_shaped_new(Ecore_Win32_Cursor_Shape shape)
224 {
225    Ecore_Win32_Cursor *cursor = NULL;
226    const char         *cursor_name;
227
228    INF("geting shape cursor");
229
230    switch (shape)
231      {
232        case ECORE_WIN32_CURSOR_SHAPE_APP_STARTING:
233          cursor_name = IDC_APPSTARTING;
234          break;
235        case ECORE_WIN32_CURSOR_SHAPE_ARROW:
236          cursor_name = IDC_ARROW;
237          break;
238        case ECORE_WIN32_CURSOR_SHAPE_CROSS:
239          cursor_name = IDC_CROSS;
240          break;
241        case ECORE_WIN32_CURSOR_SHAPE_HAND:
242          cursor_name = IDC_HAND;
243          break;
244        case ECORE_WIN32_CURSOR_SHAPE_HELP:
245          cursor_name = IDC_HELP;
246          break;
247        case ECORE_WIN32_CURSOR_SHAPE_I_BEAM:
248          cursor_name = IDC_IBEAM;
249          break;
250        case ECORE_WIN32_CURSOR_SHAPE_NO:
251          cursor_name = IDC_NO;
252          break;
253        case ECORE_WIN32_CURSOR_SHAPE_SIZE_ALL:
254          cursor_name = IDC_SIZEALL;
255          break;
256        case ECORE_WIN32_CURSOR_SHAPE_SIZE_NESW:
257          cursor_name = IDC_SIZENESW;
258          break;
259        case ECORE_WIN32_CURSOR_SHAPE_SIZE_NS:
260          cursor_name = IDC_SIZENS;
261          break;
262        case ECORE_WIN32_CURSOR_SHAPE_SIZE_NWSE:
263          cursor_name = IDC_SIZENWSE;
264          break;
265        case ECORE_WIN32_CURSOR_SHAPE_SIZE_WE:
266          cursor_name = IDC_SIZEWE;
267          break;
268        case ECORE_WIN32_CURSOR_SHAPE_UP_ARROW:
269          cursor_name = IDC_UPARROW;
270          break;
271        case ECORE_WIN32_CURSOR_SHAPE_WAIT:
272          cursor_name = IDC_WAIT;
273          break;
274      default:
275          return NULL;
276      }
277
278    if (!(cursor = LoadCursor(NULL, cursor_name)))
279      return NULL;
280
281    return cursor;
282 }
283
284 /**
285  * @brief Retrieve the size of a valid cursor.
286  *
287  * @param width The width of a valid cursor.
288  * @param height The height of a valid cursor.
289  *
290  * This function returns the size of a cursor that must be passed to
291  * ecore_win32_cursor_new(). @p width and @p height are buffers that
292  * will be filled with the correct size. They can be @c NULL.
293  */
294 EAPI void
295 ecore_win32_cursor_size_get(int *width, int *height)
296 {
297    INF("geting size cursor");
298
299    if (*width) *width = GetSystemMetrics(SM_CXCURSOR);
300    if (*height) *height = GetSystemMetrics(SM_CYCURSOR);
301 }
302
303 /**
304  * @}
305  */