]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: clipbrd.cpp | |
3 | // Purpose: Clipboard functionality | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "clipbrd.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/setup.h" | |
25 | #endif | |
26 | ||
27 | #if wxUSE_CLIPBOARD | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/object.h" | |
31 | #include "wx/event.h" | |
32 | #include "wx/app.h" | |
33 | #include "wx/frame.h" | |
34 | #include "wx/bitmap.h" | |
35 | #include "wx/utils.h" | |
36 | #endif | |
37 | ||
38 | #if wxUSE_METAFILE | |
39 | #include "wx/metafile.h" | |
40 | #endif | |
41 | ||
42 | #include "wx/clipbrd.h" | |
43 | ||
44 | #include <windows.h> | |
45 | ||
46 | HICON myIcon; | |
47 | ||
48 | #include "wx/msw/private.h" | |
49 | #include "wx/msw/dib.h" | |
50 | ||
51 | // wxDataObject is tied to OLE/drag and drop implementation, | |
52 | // therefore so is wxClipboard :-( | |
53 | #if wxUSE_DRAG_AND_DROP | |
54 | #include "wx/dataobj.h" | |
55 | #endif | |
56 | ||
57 | #include <string.h> | |
58 | ||
59 | bool wxClipboardIsOpen = FALSE; | |
60 | ||
61 | bool wxOpenClipboard(void) | |
62 | { | |
63 | if (wxTheApp->GetTopWindow() && !wxClipboardIsOpen) | |
64 | { | |
65 | wxClipboardIsOpen = (::OpenClipboard((HWND) wxTheApp->GetTopWindow()->GetHWND()) != 0); | |
66 | return wxClipboardIsOpen; | |
67 | } | |
68 | else return FALSE; | |
69 | } | |
70 | ||
71 | bool wxCloseClipboard(void) | |
72 | { | |
73 | if (wxClipboardIsOpen) | |
74 | { | |
75 | wxClipboardIsOpen = FALSE; | |
76 | } | |
77 | return (::CloseClipboard() != 0); | |
78 | } | |
79 | ||
80 | bool wxEmptyClipboard(void) | |
81 | { | |
82 | return (::EmptyClipboard() != 0); | |
83 | } | |
84 | ||
85 | bool wxClipboardOpen(void) | |
86 | { | |
87 | return wxClipboardIsOpen; | |
88 | } | |
89 | ||
90 | bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat) | |
91 | { | |
92 | return (::IsClipboardFormatAvailable(dataFormat) != 0); | |
93 | } | |
94 | ||
95 | bool wxSetClipboardData(wxDataFormat dataFormat, wxObject *obj, int width, int height) | |
96 | { | |
97 | switch (dataFormat) | |
98 | { | |
99 | case wxDF_BITMAP: | |
100 | { | |
101 | wxBitmap *wxBM = (wxBitmap *)obj; | |
102 | ||
103 | HDC hdcMem = CreateCompatibleDC(NULL); | |
104 | HDC hdcSrc = CreateCompatibleDC(NULL); | |
105 | HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, (HBITMAP) wxBM->GetHBITMAP()); | |
106 | HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, | |
107 | wxBM->GetWidth(), wxBM->GetHeight()); | |
108 | if (!hBitmap) | |
109 | { | |
110 | SelectObject(hdcSrc, old); | |
111 | DeleteDC(hdcMem); | |
112 | DeleteDC(hdcSrc); | |
113 | return FALSE; | |
114 | } | |
115 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap); | |
116 | BitBlt(hdcMem, 0, 0, wxBM->GetWidth(), wxBM->GetHeight(), | |
117 | hdcSrc, 0, 0, SRCCOPY); | |
118 | ||
119 | // Select new bitmap out of memory DC | |
120 | SelectObject(hdcMem, old1); | |
121 | ||
122 | // Set the data | |
123 | bool success = (bool)(::SetClipboardData(CF_BITMAP, hBitmap) != 0); | |
124 | ||
125 | // Clean up | |
126 | SelectObject(hdcSrc, old); | |
127 | DeleteDC(hdcSrc); | |
128 | DeleteDC(hdcMem); | |
129 | return success; | |
130 | break; | |
131 | } | |
132 | case wxDF_DIB: | |
133 | { | |
134 | #if wxUSE_IMAGE_LOADING_IN_MSW | |
135 | HBITMAP hBitmap=(HBITMAP) ((wxBitmap *)obj)->GetHBITMAP(); | |
136 | HANDLE hDIB=BitmapToDIB(hBitmap,NULL); // NULL==uses system palette | |
137 | bool success = (::SetClipboardData(CF_DIB,hDIB) != 0); | |
138 | #else | |
139 | bool success=FALSE; | |
140 | #endif | |
141 | return success; | |
142 | break; | |
143 | } | |
144 | #if wxUSE_METAFILE | |
145 | case wxDF_METAFILE: | |
146 | { | |
147 | wxMetafile *wxMF = (wxMetafile *)obj; | |
148 | HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1); | |
149 | #ifdef __WINDOWS_386__ | |
150 | METAFILEPICT *mf = (METAFILEPICT *)MK_FP32(GlobalLock(data)); | |
151 | #else | |
152 | METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data); | |
153 | #endif | |
154 | ||
155 | mf->mm = wxMF->GetWindowsMappingMode(); | |
156 | mf->xExt = width; | |
157 | mf->yExt = height; | |
158 | mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE(); | |
159 | GlobalUnlock(data); | |
160 | wxMF->SetHMETAFILE((WXHANDLE) NULL); | |
161 | ||
162 | return (SetClipboardData(CF_METAFILEPICT, data) != 0); | |
163 | break; | |
164 | } | |
165 | #endif | |
166 | case CF_SYLK: | |
167 | case CF_DIF: | |
168 | case CF_TIFF: | |
169 | case CF_PALETTE: | |
170 | { | |
171 | return FALSE; | |
172 | break; | |
173 | } | |
174 | case wxDF_OEMTEXT: | |
175 | dataFormat = wxDF_TEXT; | |
176 | case wxDF_TEXT: | |
177 | width = strlen((char *)obj) + 1; | |
178 | height = 1; | |
179 | default: | |
180 | { | |
181 | char *s = (char *)obj; | |
182 | DWORD l; | |
183 | ||
184 | l = (width * height); | |
185 | HANDLE hGlobalMemory = GlobalAlloc(GHND, l); | |
186 | if (!hGlobalMemory) | |
187 | return FALSE; | |
188 | ||
189 | #ifdef __WINDOWS_386__ | |
190 | LPSTR lpGlobalMemory = (LPSTR)MK_FP32(GlobalLock(hGlobalMemory)); | |
191 | #else | |
192 | LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); | |
193 | #endif | |
194 | ||
195 | #ifdef __WIN32__ | |
196 | memcpy(lpGlobalMemory, s, l); | |
197 | #elif defined(__WATCOMC__) && defined(__WINDOWS_386__) | |
198 | memcpy(lpGlobalMemory, s, l); | |
199 | #else | |
200 | hmemcpy(lpGlobalMemory, s, l); | |
201 | #endif | |
202 | ||
203 | GlobalUnlock(hGlobalMemory); | |
204 | HANDLE success = SetClipboardData(dataFormat, hGlobalMemory); | |
205 | return (success != 0); | |
206 | break; | |
207 | } | |
208 | } | |
209 | return FALSE; | |
210 | } | |
211 | ||
212 | wxObject *wxGetClipboardData(wxDataFormat dataFormat, long *len) | |
213 | { | |
214 | switch (dataFormat) | |
215 | { | |
216 | case wxDF_BITMAP: | |
217 | { | |
218 | BITMAP bm; | |
219 | HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP); | |
220 | if (!hBitmap) | |
221 | return NULL; | |
222 | ||
223 | HDC hdcMem = CreateCompatibleDC(NULL); | |
224 | HDC hdcSrc = CreateCompatibleDC(NULL); | |
225 | ||
226 | HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap); | |
227 | GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm); | |
228 | ||
229 | HBITMAP hNewBitmap = CreateBitmapIndirect(&bm); | |
230 | ||
231 | if (!hNewBitmap) | |
232 | { | |
233 | SelectObject(hdcSrc, old); | |
234 | DeleteDC(hdcMem); | |
235 | DeleteDC(hdcSrc); | |
236 | return NULL; | |
237 | } | |
238 | ||
239 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap); | |
240 | BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, | |
241 | hdcSrc, 0, 0, SRCCOPY); | |
242 | ||
243 | // Select new bitmap out of memory DC | |
244 | SelectObject(hdcMem, old1); | |
245 | ||
246 | // Clean up | |
247 | SelectObject(hdcSrc, old); | |
248 | DeleteDC(hdcSrc); | |
249 | DeleteDC(hdcMem); | |
250 | ||
251 | // Create and return a new wxBitmap | |
252 | wxBitmap *wxBM = new wxBitmap; | |
253 | wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap); | |
254 | wxBM->SetWidth(bm.bmWidth); | |
255 | wxBM->SetHeight(bm.bmHeight); | |
256 | wxBM->SetDepth(bm.bmPlanes); | |
257 | wxBM->SetOk(TRUE); | |
258 | return (wxObject *)wxBM; | |
259 | break; | |
260 | } | |
261 | case wxDF_METAFILE: | |
262 | case CF_SYLK: | |
263 | case CF_DIF: | |
264 | case CF_TIFF: | |
265 | case CF_PALETTE: | |
266 | case wxDF_DIB: | |
267 | { | |
268 | return FALSE; | |
269 | break; | |
270 | } | |
271 | case wxDF_OEMTEXT: | |
272 | dataFormat = wxDF_TEXT; | |
273 | case wxDF_TEXT: | |
274 | default: | |
275 | { | |
276 | HANDLE hGlobalMemory = GetClipboardData(dataFormat); | |
277 | if (!hGlobalMemory) | |
278 | return NULL; | |
279 | ||
280 | int hsize = (int)GlobalSize(hGlobalMemory); | |
281 | if (len) | |
282 | *len = hsize; | |
283 | ||
284 | char *s = new char[hsize]; | |
285 | if (!s) | |
286 | return NULL; | |
287 | ||
288 | #ifdef __WINDOWS_386__ | |
289 | LPSTR lpGlobalMemory = (LPSTR)MK_FP32(GlobalLock(hGlobalMemory)); | |
290 | #else | |
291 | LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); | |
292 | #endif | |
293 | ||
294 | #ifdef __WIN32__ | |
295 | memcpy(s, lpGlobalMemory, GlobalSize(hGlobalMemory)); | |
296 | #elif __WATCOMC__ && defined(__WINDOWS_386__) | |
297 | memcpy(s, lpGlobalMemory, GlobalSize(hGlobalMemory)); | |
298 | #else | |
299 | hmemcpy(s, lpGlobalMemory, GlobalSize(hGlobalMemory)); | |
300 | #endif | |
301 | ||
302 | GlobalUnlock(hGlobalMemory); | |
303 | ||
304 | return (wxObject *)s; | |
305 | break; | |
306 | } | |
307 | } | |
308 | return NULL; | |
309 | } | |
310 | ||
311 | wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat) | |
312 | { | |
313 | return (wxDataFormat) ::EnumClipboardFormats(dataFormat); | |
314 | } | |
315 | ||
316 | int wxRegisterClipboardFormat(char *formatName) | |
317 | { | |
318 | return ::RegisterClipboardFormat(formatName); | |
319 | } | |
320 | ||
321 | bool wxGetClipboardFormatName(wxDataFormat dataFormat, char *formatName, int maxCount) | |
322 | { | |
323 | return (::GetClipboardFormatName((int) dataFormat, formatName, maxCount) > 0); | |
324 | } | |
325 | ||
326 | /* | |
327 | * wxClipboard | |
328 | */ | |
329 | ||
330 | //----------------------------------------------------------------------------- | |
331 | // wxClipboard | |
332 | //----------------------------------------------------------------------------- | |
333 | ||
334 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject) | |
335 | ||
336 | wxClipboard::wxClipboard() | |
337 | { | |
338 | m_open = FALSE; | |
339 | } | |
340 | ||
341 | wxClipboard::~wxClipboard() | |
342 | { | |
343 | Clear(); | |
344 | } | |
345 | ||
346 | void wxClipboard::Clear() | |
347 | { | |
348 | } | |
349 | ||
350 | bool wxClipboard::Open() | |
351 | { | |
352 | wxCHECK_MSG( !m_open, FALSE, "clipboard already open" ); | |
353 | ||
354 | m_open = TRUE; | |
355 | ||
356 | return wxOpenClipboard(); | |
357 | } | |
358 | ||
359 | bool wxClipboard::SetData( wxDataObject *data ) | |
360 | { | |
361 | #if wxUSE_DRAG_AND_DROP | |
362 | wxCHECK_MSG( data, FALSE, "data is invalid" ); | |
363 | wxCHECK_MSG( m_open, FALSE, "clipboard not open" ); | |
364 | ||
365 | switch (data->GetFormat()) | |
366 | { | |
367 | case wxDF_TEXT: | |
368 | case wxDF_OEMTEXT: | |
369 | { | |
370 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
371 | wxString str(textDataObject->GetText()); | |
372 | return wxSetClipboardData(data->GetFormat(), (wxObject*) (const char*) str); | |
373 | break; | |
374 | } | |
375 | case wxDF_BITMAP: | |
376 | case wxDF_DIB: | |
377 | { | |
378 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data; | |
379 | wxBitmap bitmap(bitmapDataObject->GetBitmap()); | |
380 | return wxSetClipboardData(data->GetFormat(), & bitmap); | |
381 | break; | |
382 | } | |
383 | #if wxUSE_METAFILE | |
384 | case wxDF_METAFILE: | |
385 | { | |
386 | wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject*) data; | |
387 | wxMetafile metaFile = metaFileDataObject->GetMetafile(); | |
388 | return wxSetClipboardData(wxDF_METAFILE, & metaFile, metaFileDataObject->GetWidth(), metaFileDataObject->GetHeight()); | |
389 | break; | |
390 | } | |
391 | #endif | |
392 | default: | |
393 | { | |
394 | return FALSE; | |
395 | } | |
396 | } | |
397 | ||
398 | return FALSE; | |
399 | #else | |
400 | return FALSE; | |
401 | #endif | |
402 | } | |
403 | ||
404 | void wxClipboard::Close() | |
405 | { | |
406 | wxCHECK_RET( m_open, "clipboard not open" ); | |
407 | ||
408 | m_open = FALSE; | |
409 | wxCloseClipboard(); | |
410 | } | |
411 | ||
412 | bool wxClipboard::IsSupportedFormat( wxDataFormat format, const wxString& WXUNUSED(id) ) | |
413 | { | |
414 | return wxIsClipboardFormatAvailable(format); | |
415 | } | |
416 | ||
417 | bool wxClipboard::GetData( wxDataObject *data ) | |
418 | { | |
419 | wxCHECK_MSG( m_open, FALSE, "clipboard not open" ); | |
420 | ||
421 | #if wxUSE_DRAG_AND_DROP | |
422 | switch (data->GetFormat()) | |
423 | { | |
424 | case wxDF_TEXT: | |
425 | case wxDF_OEMTEXT: | |
426 | { | |
427 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
428 | char* s = (char*) wxGetClipboardData(data->GetFormat()); | |
429 | if (s) | |
430 | { | |
431 | textDataObject->SetText(s); | |
432 | delete[] s; | |
433 | return TRUE; | |
434 | } | |
435 | else | |
436 | return FALSE; | |
437 | break; | |
438 | } | |
439 | case wxDF_BITMAP: | |
440 | case wxDF_DIB: | |
441 | { | |
442 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data; | |
443 | wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetFormat()); | |
444 | if (bitmap) | |
445 | { | |
446 | bitmapDataObject->SetBitmap(* bitmap); | |
447 | delete bitmap; | |
448 | return TRUE; | |
449 | } | |
450 | else | |
451 | return FALSE; | |
452 | break; | |
453 | } | |
454 | #if wxUSE_METAFILE | |
455 | case wxDF_METAFILE: | |
456 | { | |
457 | wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject*) data; | |
458 | wxMetafile* metaFile = (wxMetafile*) wxGetClipboardData(wxDF_METAFILE); | |
459 | if (metaFile) | |
460 | { | |
461 | metaFileDataObject->SetMetafile(* metaFile); | |
462 | delete metaFile; | |
463 | return TRUE; | |
464 | } | |
465 | else | |
466 | return FALSE; | |
467 | ||
468 | break; | |
469 | } | |
470 | #endif | |
471 | default: | |
472 | { | |
473 | return FALSE; | |
474 | } | |
475 | } | |
476 | return FALSE; | |
477 | #else | |
478 | return FALSE; | |
479 | #endif | |
480 | } | |
481 | ||
482 | #endif // wxUSE_CLIPBOARD |