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