]> git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
8a18cf4e5cc75efa9aad46b20e3a2bc01a73d00d
[wxWidgets.git] / src / msw / clipbrd.cpp
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 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
331
332 wxClipboard* wxTheClipboard = (wxClipboard*) NULL;
333
334 wxClipboard::wxClipboard()
335 {
336 m_open = FALSE;
337 }
338
339 wxClipboard::~wxClipboard()
340 {
341 Clear();
342 }
343
344 void wxClipboard::Clear()
345 {
346 wxNode* node = m_data.First();
347 while (node)
348 {
349 wxDataObject* data = (wxDataObject*) node->Data();
350 delete data;
351 node = node->Next();
352 }
353 m_data.Clear();
354 }
355
356 bool wxClipboard::Open()
357 {
358 wxCHECK_MSG( !m_open, FALSE, "clipboard already open" );
359
360 m_open = TRUE;
361
362 return wxOpenClipboard();
363 }
364
365 bool wxClipboard::SetData( wxDataObject *data )
366 {
367 #if wxUSE_DRAG_AND_DROP
368 wxCHECK_MSG( data, FALSE, "data is invalid" );
369 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
370
371 switch (data->GetFormat())
372 {
373 case wxDF_TEXT:
374 case wxDF_OEMTEXT:
375 {
376 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
377 wxString str(textDataObject->GetText());
378 return wxSetClipboardData(data->GetFormat(), (wxObject*) (const char*) str);
379 break;
380 }
381 case wxDF_BITMAP:
382 case wxDF_DIB:
383 {
384 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
385 wxBitmap bitmap(bitmapDataObject->GetBitmap());
386 return wxSetClipboardData(data->GetFormat(), & bitmap);
387 break;
388 }
389 #if wxUSE_METAFILE
390 case wxDF_METAFILE:
391 {
392 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject*) data;
393 wxMetafile metaFile = metaFileDataObject->GetMetafile();
394 return wxSetClipboardData(wxDF_METAFILE, & metaFile, metaFileDataObject->GetWidth(), metaFileDataObject->GetHeight());
395 break;
396 }
397 #endif
398 default:
399 {
400 return FALSE;
401 }
402 }
403
404 return FALSE;
405 #else
406 return FALSE;
407 #endif
408 }
409
410 void wxClipboard::Close()
411 {
412 wxCHECK_RET( m_open, "clipboard not open" );
413
414 m_open = FALSE;
415 wxCloseClipboard();
416 }
417
418 bool wxClipboard::IsSupportedFormat( wxDataFormat format, const wxString& WXUNUSED(id) )
419 {
420 return wxIsClipboardFormatAvailable(format);
421 }
422
423 bool wxClipboard::GetData( wxDataObject *data )
424 {
425 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
426
427 #if wxUSE_DRAG_AND_DROP
428 switch (data->GetFormat())
429 {
430 case wxDF_TEXT:
431 case wxDF_OEMTEXT:
432 {
433 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
434 char* s = (char*) wxGetClipboardData(data->GetFormat());
435 if (s)
436 {
437 textDataObject->SetText(s);
438 delete[] s;
439 return TRUE;
440 }
441 else
442 return FALSE;
443 break;
444 }
445 case wxDF_BITMAP:
446 case wxDF_DIB:
447 {
448 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
449 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetFormat());
450 if (bitmap)
451 {
452 bitmapDataObject->SetBitmap(* bitmap);
453 delete bitmap;
454 return TRUE;
455 }
456 else
457 return FALSE;
458 break;
459 }
460 #if wxUSE_METAFILE
461 case wxDF_METAFILE:
462 {
463 wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject*) data;
464 wxMetafile* metaFile = (wxMetafile*) wxGetClipboardData(wxDF_METAFILE);
465 if (metaFile)
466 {
467 metaFileDataObject->SetMetafile(* metaFile);
468 delete metaFile;
469 return TRUE;
470 }
471 else
472 return FALSE;
473
474 break;
475 }
476 #endif
477 default:
478 {
479 return FALSE;
480 }
481 }
482 return FALSE;
483 #else
484 return FALSE;
485 #endif
486 }
487
488 //-----------------------------------------------------------------------------
489 // wxClipboardModule
490 //-----------------------------------------------------------------------------
491
492 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
493
494 bool wxClipboardModule::OnInit()
495 {
496 wxTheClipboard = new wxClipboard();
497
498 return TRUE;
499 }
500
501 void wxClipboardModule::OnExit()
502 {
503 if (wxTheClipboard) delete wxTheClipboard;
504 wxTheClipboard = (wxClipboard*) NULL;
505 }
506
507 #endif // wxUSE_CLIPBOARD
508