Debian Codegraph @

src/win/wclipboard.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_//_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows clipboard handling.
12 *
13 * By Beoran.
14 *
15 * See readme.txt for copyright information.
16 */
17
18
19 #define _WIN32_WINNT 0x0501
20 #ifndef WINVER
21 #define WINVER 0x0501
22 #endif
23
24 #include <windows.h>
25 #include <windowsx.h>
26
27
28 #include <allegro5/allegro.h>
29
30 #include "allegro5/allegro_windows.h"
31 #include "allegro5/internal/aintern.h"
32 #include "allegro5/internal/aintern_wclipboard.h"
33 #include "allegro5/internal/aintern_display.h"
34 #include "allegro5/internal/aintern_wunicode.h"
35 #include "allegro5/platform/aintwin.h"
36
37
38 ALLEGRO_DEBUG_CHANNEL("clipboard")
39
40 #ifdef UNICODE
41 #define TEXT_FORMAT CF_UNICODETEXT
42 #else
43 #define TEXT_FORMAT CF_TEXT
44 #endif
45
46
47 /* Get any application owned window handle for clipboard association */
48 static HWND get_window_handle(ALLEGRO_DISPLAY *display)
49 {
50 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
51 if (!win_display)
52 return NULL;
53
54 return win_display->window;
55 }
56
57 static bool win_set_clipboard_text(ALLEGRO_DISPLAY *display, const char *text)
58 {
59 HWND handle = get_window_handle(display);
60 HANDLE hMem = NULL;
61 TCHAR *tstr = NULL;
62 size_t size;
63 size_t len;
64 LPTSTR dst;
65
66 if (!OpenClipboard(handle)) {
67 ALLEGRO_DEBUG("Could not open clipboard for handle %p", handle);
68 return false;
69 }
70
71 /* Convert the text from UTF-8 to Windows Unicode */
72 tstr = _twin_utf8_to_tchar(text);
73 len = _twin_tchar_strlen(tstr);
74 size = (len+1) * sizeof(TCHAR);
75 /* Save the data to the clipboard */
76 hMem = GlobalAlloc(GMEM_MOVEABLE, size);
77
78 if (!hMem) {
79 al_free(tstr);
80 ALLEGRO_DEBUG("GlobalAlloc failed to allocate memory for the clipboard data");
81 return false;
82 }
83
84 dst = (LPTSTR)GlobalLock(hMem);
85 /* Copy the text over. Unlike SDL, do NOT convert newlines, that's for the
86 * use to decide. */
87 memmove(dst, tstr, size);
88 dst[len] = 0;
89 GlobalUnlock(hMem);
90 EmptyClipboard();
91 if (!SetClipboardData(TEXT_FORMAT, hMem)) {
92 al_free(tstr);
93 ALLEGRO_DEBUG("Couldn't set clipboard data");
94 return false;
95 }
96 al_free(tstr);
97 CloseClipboard();
98 return true;
99 }
100
101 static char *win_get_clipboard_text(ALLEGRO_DISPLAY *display)
102 {
103 char *text;
104
105 text = NULL;
106 if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
107 OpenClipboard(get_window_handle(display))) {
108 HANDLE hMem;
109 LPTSTR tstr;
110
111 hMem = GetClipboardData(TEXT_FORMAT);
112 if (hMem) {
113 tstr = (LPTSTR)GlobalLock(hMem);
114 text = _twin_tchar_to_utf8(tstr);
115 GlobalUnlock(hMem);
116 } else {
117 ALLEGRO_DEBUG("Couldn't get clipboard data");
118 }
119 CloseClipboard();
120 }
121 return text;
122 }
123
124 static bool win_has_clipboard_text(ALLEGRO_DISPLAY *display)
125 {
126 if (!IsClipboardFormatAvailable(TEXT_FORMAT))
127 return false;
128 if (!OpenClipboard(get_window_handle(display)))
129 return false;
130
131 CloseClipboard();
132 return true;
133 }
134
135
136 void _al_win_add_clipboard_functions(ALLEGRO_DISPLAY_INTERFACE *vt)
137 {
138 vt->set_clipboard_text = win_set_clipboard_text;
139 vt->get_clipboard_text = win_get_clipboard_text;
140 vt->has_clipboard_text = win_has_clipboard_text;
141 }
142
143 /* vi: set ts=8 sts=3 sw=3 et: */