summaryrefslogtreecommitdiff
path: root/src/lib/ecore_win32/ecore_win32_cursor.c
blob: 490e4f4ec9ff8f657bbb49c1e24bfd5fececc3fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include <Eina.h>

#include "Ecore_Win32.h"
#include "ecore_win32_private.h"
#include "ecore_win32_cursor_x11.h"

/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/


Ecore_Win32_Cursor *
_ecore_win32_cursor_x11_shaped_new(Ecore_Win32_Cursor_X11_Shape shape)
{
   INF("creating X11 shaped cursor");

   if ((shape < ECORE_WIN32_CURSOR_X11_SHAPE_X) ||
       (shape > ECORE_WIN32_CURSOR_X11_SHAPE_XTERM))
     return NULL;

   return ecore_win32_cursor_new(_ecore_win32_cursors_x11[shape].mask_and,
                                 _ecore_win32_cursors_x11[shape].mask_xor,
                                 32, 32,
                                 _ecore_win32_cursors_x11[shape].hotspot_x,
                                 _ecore_win32_cursors_x11[shape].hotspot_y);
}


/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/


/*============================================================================*
 *                                   API                                      *
 *============================================================================*/

/**
 * @addtogroup Ecore_Win32_Group Ecore_Win32 library
 *
 * @{
 */

/**
 * @brief Create a new cursor.
 *
 * @param pixels_and The array of bytes containing the bit values for
 * the AND mask of the cursor.
 * @param pixels_xor The array of bytes containing the bit values for
 * the XOR mask of the cursor.
 * @param width The width of the cursor.
 * @param height The height of the cursor.
 * @param hot_x The horizontal position of the cursor's hot spot.
 * @param hot_y The vertical position of the cursor's hot spot.
 * @return A newly user-defined cursor.
 *
 * This function creates a new cursor of size @p width and @p
 * height. They must be valid size. To determine the valid size of a
 * cursor, use ecore_win32_cursor_size_get(). @p pixels_and is an array
 * of bytes (unsigned char) containing the bits of the cursor that
 * will be visible. @p pixels_xor is similar but will allow the cursor
 * to have a shape. Here is the truth table for the masks:
 *
 * <table border=1>
 * <tr><td>AND mask</td><td>XOR mask</td><td>Display</td></tr>
 * <tr><td>0</td>       <td>0</td>       <td>Black</td></tr>
 * <tr><td>0</td>       <td>1</td>       <td>White</td></tr>
 * <tr><td>1</td>       <td>0</td>       <td>Screen</td></tr>
 * <tr><td>1</td>       <td>1</td>       <td>Reverse screen</td></tr>
 * </table>
 *
 * @p hot_x and @p hot_y are the position of the hot spot of the
 * cursor. If @p pixels_and or @p pixels_xor are @c NULL, the function
 * returns NULL. If @p width or @p height does not match the valid
 * size of a cursor, the function returns @c NULL. On success, the
 * function creates a user-defined cursor, otherwise it returns
 * @c NULL.
 *
 * Once the cursor is not used anymore, use ecore_win32_cursor_free()
 * to free the ressources.
 *
 * Example of use:
 *
 * @code
 * unsigned char pixels_and[] ={
 * 0xFF, 0xFC, 0x3F, 0xFF,   // line 1
 * 0xFF, 0xC0, 0x1F, 0xFF,   // line 2
 * 0xFF, 0x00, 0x3F, 0xFF,   // line 3
 * 0xFE, 0x00, 0xFF, 0xFF,   // line 4
 *
 * 0xF7, 0x01, 0xFF, 0xFF,   // line 5
 * 0xF0, 0x03, 0xFF, 0xFF,   // line 6
 * 0xF0, 0x03, 0xFF, 0xFF,   // line 7
 * 0xE0, 0x07, 0xFF, 0xFF,   // line 8
 *
 * 0xC0, 0x07, 0xFF, 0xFF,   // line 9
 * 0xC0, 0x0F, 0xFF, 0xFF,   // line 10
 * 0x80, 0x0F, 0xFF, 0xFF,   // line 11
 * 0x80, 0x0F, 0xFF, 0xFF,   // line 12
 *
 * 0x80, 0x07, 0xFF, 0xFF,   // line 13
 * 0x00, 0x07, 0xFF, 0xFF,   // line 14
 * 0x00, 0x03, 0xFF, 0xFF,   // line 15
 * 0x00, 0x00, 0xFF, 0xFF,   // line 16
 *
 * 0x00, 0x00, 0x7F, 0xFF,   // line 17
 * 0x00, 0x00, 0x1F, 0xFF,   // line 18
 * 0x00, 0x00, 0x0F, 0xFF,   // line 19
 * 0x80, 0x00, 0x0F, 0xFF,   // line 20
 *
 * 0x80, 0x00, 0x07, 0xFF,   // line 21
 * 0x80, 0x00, 0x07, 0xFF,   // line 22
 * 0xC0, 0x00, 0x07, 0xFF,   // line 23
 * 0xC0, 0x00, 0x0F, 0xFF,   // line 24
 *
 * 0xE0, 0x00, 0x0F, 0xFF,   // line 25
 * 0xF0, 0x00, 0x1F, 0xFF,   // line 26
 * 0xF0, 0x00, 0x1F, 0xFF,   // line 27
 * 0xF8, 0x00, 0x3F, 0xFF,   // line 28
 *
 * 0xFE, 0x00, 0x7F, 0xFF,   // line 29
 * 0xFF, 0x00, 0xFF, 0xFF,   // line 30
 * 0xFF, 0xC3, 0xFF, 0xFF,   // line 31
 * 0xFF, 0xFF, 0xFF, 0xFF    // line 32
 * };
 *
 * unsigned char pixels_xor[] = {
 * 0x00, 0x00, 0x00, 0x00,   // line 1
 * 0x00, 0x03, 0xC0, 0x00,   // line 2
 * 0x00, 0x3F, 0x00, 0x00,   // line 3
 * 0x00, 0xFE, 0x00, 0x00,   // line 4
 *
 * 0x0E, 0xFC, 0x00, 0x00,   // line 5
 * 0x07, 0xF8, 0x00, 0x00,   // line 6
 * 0x07, 0xF8, 0x00, 0x00,   // line 7
 * 0x0F, 0xF0, 0x00, 0x00,   // line 8
 *
 * 0x1F, 0xF0, 0x00, 0x00,   // line 9
 * 0x1F, 0xE0, 0x00, 0x00,   // line 10
 * 0x3F, 0xE0, 0x00, 0x00,   // line 11
 * 0x3F, 0xE0, 0x00, 0x00,   // line 12
 *
 * 0x3F, 0xF0, 0x00, 0x00,   // line 13
 * 0x7F, 0xF0, 0x00, 0x00,   // line 14
 * 0x7F, 0xF8, 0x00, 0x00,   // line 15
 * 0x7F, 0xFC, 0x00, 0x00,   // line 16
 *
 * 0x7F, 0xFF, 0x00, 0x00,   // line 17
 * 0x7F, 0xFF, 0x80, 0x00,   // line 18
 * 0x7F, 0xFF, 0xE0, 0x00,   // line 19
 * 0x3F, 0xFF, 0xE0, 0x00,   // line 20
 *
 * 0x3F, 0xC7, 0xF0, 0x00,   // line 21
 * 0x3F, 0x83, 0xF0, 0x00,   // line 22
 * 0x1F, 0x83, 0xF0, 0x00,   // line 23
 * 0x1F, 0x83, 0xE0, 0x00,   // line 24
 *
 * 0x0F, 0xC7, 0xE0, 0x00,   // line 25
 * 0x07, 0xFF, 0xC0, 0x00,   // line 26
 * 0x07, 0xFF, 0xC0, 0x00,   // line 27
 * 0x01, 0xFF, 0x80, 0x00,   // line 28
 *
 * 0x00, 0xFF, 0x00, 0x00,   // line 29
 * 0x00, 0x3C, 0x00, 0x00,   // line 30
 * 0x00, 0x00, 0x00, 0x00,   // line 31
 * 0x00, 0x00, 0x00, 0x00    // line 32
 * };
 *
 * Ecore_Win32_Cursor *cursor = ecore_win32_cursor_new(pixels_and, pixels_xor, 32, 32, 19, 2);
 * ecore_win32_window_cursor_set(window, cursor);
 * @endcode
 *
 * @see ecore_win32_cursor_free()
 * @see ecore_win32_window_cursor_set()
 */
EAPI Ecore_Win32_Cursor *
ecore_win32_cursor_new(const void *pixels_and,
                       const void *pixels_xor,
                       int         width,
                       int         height,
                       int         hot_x,
                       int         hot_y)
{
   Ecore_Win32_Cursor *cursor = NULL;
   int                 cursor_width;
   int                 cursor_height;

   INF("creating cursor");

   if (!pixels_and || !pixels_xor)
     return NULL;

   cursor_width = GetSystemMetrics(SM_CXCURSOR);
   cursor_height = GetSystemMetrics(SM_CYCURSOR);

   if ((cursor_width != width) ||
       (cursor_height != height))
     return NULL;

   if (!(cursor = CreateCursor(_ecore_win32_instance,
                               hot_x, hot_y,
                               width, height,
                               pixels_and,
                               pixels_xor)))
     return NULL;

   return cursor;
}

/**
 * @brief Free the given cursor.
 *
 * @param cursor The cursor to free.
 *
 * This function free @p cursor. @p cursor must have been obtained
 * with ecore_win32_cursor_new() or ecore_win32_cursor_x11_shaped_new().
 *
 * @see ecore_win32_cursor_new()
 * @see ecore_win32_cursor_x11_shaped_new()
 */
EAPI void
ecore_win32_cursor_free(Ecore_Win32_Cursor *cursor)
{
   INF("destroying cursor");

   if (!cursor)
     return;

   DestroyCursor(cursor);
}

/**
 * @brief Create a cursor from a Windows ressource.
 *
 * @param shape The pre-defined shape of the cursor.
 * @return The new cursor.
 *
 * This function returns a pre-defined cursor with a specified
 * @p shape. This cursor does not need to be freed, as it is loaded
 * from an existing resource. On error @c NULL is returned.
 */
EAPI Ecore_Win32_Cursor *
ecore_win32_cursor_shaped_new(Ecore_Win32_Cursor_Shape shape)
{
   Ecore_Win32_Cursor *cursor = NULL;
   const char         *cursor_name;

   INF("geting shape cursor");

   switch (shape)
     {
       case ECORE_WIN32_CURSOR_SHAPE_APP_STARTING:
         cursor_name = IDC_APPSTARTING;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_ARROW:
         cursor_name = IDC_ARROW;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_CROSS:
         cursor_name = IDC_CROSS;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_HAND:
         cursor_name = IDC_HAND;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_HELP:
         cursor_name = IDC_HELP;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_I_BEAM:
         cursor_name = IDC_IBEAM;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_NO:
         cursor_name = IDC_NO;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_SIZE_ALL:
         cursor_name = IDC_SIZEALL;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_SIZE_NESW:
         cursor_name = IDC_SIZENESW;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_SIZE_NS:
         cursor_name = IDC_SIZENS;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_SIZE_NWSE:
         cursor_name = IDC_SIZENWSE;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_SIZE_WE:
         cursor_name = IDC_SIZEWE;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_UP_ARROW:
         cursor_name = IDC_UPARROW;
         break;
       case ECORE_WIN32_CURSOR_SHAPE_WAIT:
         cursor_name = IDC_WAIT;
         break;
     default:
         return NULL;
     }

   if (!(cursor = LoadCursor(NULL, cursor_name)))
     return NULL;

   return cursor;
}

/**
 * @brief Retrieve a X11 cursor from a X Id.
 *
 * @param[in] shape The defined X11 shape of the cursor.
 * @return The new cursor.
 *
 * This function returns a defined cursor with a specified X11
 * @p shape. Do not use ecore_win32_cursor_free() to free the
 * ressources as it is created once the libray is initialized and
 * detroyed when it is shut down.
 *
 * @see ecore_win32_cursor_new()
 *
 * @since 1.16
 */
EAPI const Ecore_Win32_Cursor *
ecore_win32_cursor_x11_shaped_get(Ecore_Win32_Cursor_X11_Shape shape)
{
   INF("getting X11 shaped cursor");

   if ((shape < ECORE_WIN32_CURSOR_X11_SHAPE_X) ||
       (shape > ECORE_WIN32_CURSOR_X11_SHAPE_XTERM))
     return NULL;

   return _ecore_win32_cursor_x[shape];
}

/**
 * @brief Retrieve the size of a valid cursor.
 *
 * @param width The width of a valid cursor.
 * @param height The height of a valid cursor.
 *
 * This function returns the size of a cursor that must be passed to
 * ecore_win32_cursor_new(). @p width and @p height are buffers that
 * will be filled with the correct size. They can be @c NULL.
 */
EAPI void
ecore_win32_cursor_size_get(int *width, int *height)
{
   INF("geting size cursor");

   if (*width) *width = GetSystemMetrics(SM_CXCURSOR);
   if (*height) *height = GetSystemMetrics(SM_CYCURSOR);
}

EAPI void
ecore_win32_cursor_show(Eina_Bool show)
{
   INF("show cursor");

   ShowCursor(show ? TRUE : FALSE);
}

/**
 * @}
 */