summaryrefslogtreecommitdiff
path: root/src/lib/ecore_win32/ecore_win32_clipboard.c
blob: da4960370aa2b0cd893065eeba8bfee72b3a3cee (plain)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

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

#include <evil_private.h> /* utf-8 and utf-16 conversion */
#include <Eina.h>

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

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

/**
 * @cond LOCAL
 */



/**
 * @endcond
 */


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


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


EAPI Eina_Bool
ecore_win32_clipboard_set(const Ecore_Win32_Window *window,
                          const void *data,
                          size_t size,
                          const char *mime_type)
{
   HGLOBAL global;
   char *d;
   Eina_Bool supported_mime_text;
   Eina_Bool res = EINA_FALSE;

   /*
    * See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms649016%28v=vs.85%29.aspx#_win32_Copying_Information_to_the_Clipboard
    * 1. Open the clipboard
    * 2. Empty the clipboard
    * 3. Set the data
    * 4. Close the clipboard
    */

   INF("setting data to the clipboard");

   supported_mime_text = eina_str_has_prefix(mime_type, "text/");
   if (!supported_mime_text)
     {
        ERR("Mimetype %s is not handled yet", mime_type);
        return EINA_FALSE;
     }

   if (!window || !data || (size <= 0) || !OpenClipboard(window->window))
     return EINA_FALSE;

   if (!EmptyClipboard())
     goto close_clipboard;

   if (supported_mime_text)
     {
        wchar_t *text16;
        size_t size16;

        /* CF_TEXT (UTF-8) */

        global = GlobalAlloc(GMEM_MOVEABLE, size);
        if (global)
          {
             d = (char *)GlobalLock(global);
             if (d)
               {
                  memcpy(d, data, size);
                  SetClipboardData(CF_TEXT, global);
                  res = EINA_TRUE;
                  GlobalUnlock(global);
               }
          }

        /* CF_UNICODETEXT (UTF-16) */

        text16 = evil_utf8_to_utf16(data);
        if (text16)
          {
             size16 = (wcslen(text16) + 1) * sizeof(wchar_t);

             global = GlobalAlloc(GMEM_MOVEABLE, size16);
             if (global)
               {
                  d = (char *)GlobalLock(global);
                  if (d)
                    {
                       memcpy(d, text16, size16);
                       SetClipboardData(CF_UNICODETEXT, global);
                       res = EINA_TRUE;
                       GlobalUnlock(global);
                    }
               }

             free(text16);
          }
     }

 close_clipboard:
   CloseClipboard();

   return res;
}

EAPI void *
ecore_win32_clipboard_get(const Ecore_Win32_Window *window,
                          size_t *size,
                          const char *mime_type)
{
   HGLOBAL global;
   void *data;
   void *d;

   /*
    * See https://msdn.microsoft.com/en-us/library/windows/desktop/ms649016%28v=vs.85%29.aspx#_win32_Pasting_Information_from_the_Clipboard
    * 1. Open Clipboard
    * 2. Determine format
    * 3. Retrieve data
    * 4. Manage data
    * 5. Close clipboard
    */

   INF("getting data from the clipboard");

   if (!eina_str_has_prefix(mime_type, "text/"))
     {
        ERR("Mimetype %s is not handled yet", mime_type);
        return NULL;
     }

   if (!window || !size || !OpenClipboard(window->window))
     return NULL;

   *size = 0;

#if 0
   {
     UINT fmt = 0;

     while (1)
       {
         char name[4096];
         int res;
         fmt = EnumClipboardFormats(fmt);
         res = GetClipboardFormatName(fmt, name, sizeof(name));
         fprintf(stderr, " $ Format2 : %x %d\n", fmt, res);
         if (res)
           fprintf(stderr, " $ Format2 : %s\n", name);
         else
           fprintf(stderr, " $ Format2 : error %ld\n", GetLastError());
         fflush(stderr);
         if (!fmt)
           break;
       }
   }
#endif

   if (eina_str_has_prefix(mime_type, "text/"))
     {
        /* first check if UTF-16 text is available */
        global = GetClipboardData(CF_UNICODETEXT);
        if (global)
          {
             d = GlobalLock(global);
             if (d)
               {
                  data = evil_utf16_to_utf8(d);
                  if (data)
                    {
                       *size = strlen(data);
                       GlobalUnlock(global);
                       CloseClipboard();
                       return data;
                    }
                  GlobalUnlock(global);
               }
             /* otherwise, we try CF_TEXT (UTF-8/ANSI) */
          }

        /* second, check if UTF-8/ANSI text is available */
        global = GetClipboardData(CF_TEXT);
        if (global)
          {
             d = GlobalLock(global);
             if (d)
               {
                  *size = strlen(d) + 1;
                  data = malloc(*size);
                  if (data)
                    {
                       memcpy(data, d, *size);
                       GlobalUnlock(global);
                       CloseClipboard();
                       return data;
                    }
                  else
                    *size = 0;

                  GlobalUnlock(global);
               }
          }
     }

   CloseClipboard();

   return NULL;
}

EAPI void
ecore_win32_clipboard_clear(const Ecore_Win32_Window *window)
{
   INF("clearing the clipboard");

   if (!window || !OpenClipboard(window->window))
     return;

   EmptyClipboard();
   CloseClipboard();
}

/**
 * @}
 */