]> git.saurik.com Git - wxWidgets.git/blame - src/msw/clipbrd.cpp
VC++ compilation correction; doc file corrections
[wxWidgets.git] / src / msw / clipbrd.cpp
CommitLineData
2bda0e17
KB
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__
2bda0e17
KB
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
47d67540 27#if wxUSE_CLIPBOARD
2bda0e17
KB
28
29#ifndef WX_PRECOMP
2432b92d
JS
30#include "wx/object.h"
31#include "wx/event.h"
2bda0e17
KB
32#include "wx/app.h"
33#include "wx/frame.h"
34#include "wx/bitmap.h"
35#include "wx/utils.h"
36#endif
37
06e43511 38#if wxUSE_METAFILE
2bda0e17 39#include "wx/metafile.h"
06e43511
JS
40#endif
41
2bda0e17 42#include "wx/clipbrd.h"
06e43511
JS
43
44#include <windows.h>
45
46HICON myIcon;
47
2bda0e17
KB
48#include "wx/msw/private.h"
49#include "wx/msw/dib.h"
50
06e43511
JS
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"
2bda0e17
KB
55#endif
56
06e43511
JS
57#include <string.h>
58
2bda0e17
KB
59bool wxClipboardIsOpen = FALSE;
60
61bool 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
71bool wxCloseClipboard(void)
72{
73 if (wxClipboardIsOpen)
74 {
75 wxClipboardIsOpen = FALSE;
76 }
77 return (::CloseClipboard() != 0);
78}
79
80bool wxEmptyClipboard(void)
81{
82 return (::EmptyClipboard() != 0);
83}
84
85bool wxClipboardOpen(void)
86{
87 return wxClipboardIsOpen;
88}
89
06e43511 90bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
2bda0e17
KB
91{
92 return (::IsClipboardFormatAvailable(dataFormat) != 0);
93}
94
06e43511 95bool wxSetClipboardData(wxDataFormat dataFormat, wxObject *obj, int width, int height)
2bda0e17
KB
96{
97 switch (dataFormat)
98 {
b3324be2 99 case wxDF_BITMAP:
2bda0e17
KB
100 {
101 wxBitmap *wxBM = (wxBitmap *)obj;
102
103 HDC hdcMem = CreateCompatibleDC(NULL);
104 HDC hdcSrc = CreateCompatibleDC(NULL);
c4e7c2aa 105 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, (HBITMAP) wxBM->GetHBITMAP());
2bda0e17
KB
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 }
c4e7c2aa 115 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
2bda0e17
KB
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 }
b3324be2 132 case wxDF_DIB:
2bda0e17 133 {
47d67540 134#if wxUSE_IMAGE_LOADING_IN_MSW
2bda0e17
KB
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 }
47d67540 144#if wxUSE_METAFILE
b3324be2 145 case wxDF_METAFILE:
2bda0e17 146 {
06e43511 147 wxMetafile *wxMF = (wxMetafile *)obj;
2bda0e17
KB
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;
c4e7c2aa 158 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
2bda0e17
KB
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 }
b3324be2
JS
174 case wxDF_OEMTEXT:
175 dataFormat = wxDF_TEXT;
176 case wxDF_TEXT:
2bda0e17
KB
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
06e43511 212wxObject *wxGetClipboardData(wxDataFormat dataFormat, long *len)
2bda0e17
KB
213{
214 switch (dataFormat)
215 {
b3324be2 216 case wxDF_BITMAP:
2bda0e17
KB
217 {
218 BITMAP bm;
c4e7c2aa 219 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
2bda0e17
KB
220 if (!hBitmap)
221 return NULL;
222
223 HDC hdcMem = CreateCompatibleDC(NULL);
224 HDC hdcSrc = CreateCompatibleDC(NULL);
225
c4e7c2aa 226 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
2bda0e17
KB
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
c4e7c2aa 239 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
2bda0e17
KB
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 }
b3324be2 261 case wxDF_METAFILE:
2bda0e17
KB
262 case CF_SYLK:
263 case CF_DIF:
264 case CF_TIFF:
265 case CF_PALETTE:
b3324be2 266 case wxDF_DIB:
2bda0e17
KB
267 {
268 return FALSE;
269 break;
270 }
b3324be2
JS
271 case wxDF_OEMTEXT:
272 dataFormat = wxDF_TEXT;
273 case wxDF_TEXT:
2bda0e17
KB
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
06e43511 311wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
2bda0e17 312{
06e43511 313 return (wxDataFormat) ::EnumClipboardFormats(dataFormat);
2bda0e17
KB
314}
315
316int wxRegisterClipboardFormat(char *formatName)
317{
318 return ::RegisterClipboardFormat(formatName);
319}
320
06e43511 321bool wxGetClipboardFormatName(wxDataFormat dataFormat, char *formatName, int maxCount)
2bda0e17 322{
06e43511 323 return (::GetClipboardFormatName((int) dataFormat, formatName, maxCount) > 0);
2bda0e17
KB
324}
325
06e43511
JS
326//-----------------------------------------------------------------------------
327// wxClipboard
328//-----------------------------------------------------------------------------
2bda0e17 329
06e43511 330IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
2bda0e17 331
4ce81a75
JS
332wxClipboard* wxTheClipboard = (wxClipboard*) NULL;
333
2bda0e17
KB
334wxClipboard::wxClipboard()
335{
06e43511 336 m_open = FALSE;
2bda0e17
KB
337}
338
339wxClipboard::~wxClipboard()
340{
06e43511 341 Clear();
2bda0e17
KB
342}
343
06e43511 344void wxClipboard::Clear()
2bda0e17 345{
4ce81a75
JS
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();
2bda0e17
KB
354}
355
06e43511 356bool wxClipboard::Open()
2bda0e17 357{
06e43511 358 wxCHECK_MSG( !m_open, FALSE, "clipboard already open" );
2bda0e17 359
06e43511 360 m_open = TRUE;
2bda0e17 361
06e43511 362 return wxOpenClipboard();
2bda0e17
KB
363}
364
06e43511 365bool wxClipboard::SetData( wxDataObject *data )
2bda0e17 366{
06e43511
JS
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
2bda0e17
KB
408}
409
06e43511 410void wxClipboard::Close()
2bda0e17 411{
06e43511
JS
412 wxCHECK_RET( m_open, "clipboard not open" );
413
414 m_open = FALSE;
415 wxCloseClipboard();
2bda0e17
KB
416}
417
06e43511 418bool wxClipboard::IsSupportedFormat( wxDataFormat format, const wxString& WXUNUSED(id) )
2bda0e17 419{
06e43511 420 return wxIsClipboardFormatAvailable(format);
2bda0e17
KB
421}
422
06e43511 423bool wxClipboard::GetData( wxDataObject *data )
2bda0e17 424{
06e43511
JS
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
2bda0e17
KB
486}
487
4ce81a75
JS
488//-----------------------------------------------------------------------------
489// wxClipboardModule
490//-----------------------------------------------------------------------------
491
492IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
493
494bool wxClipboardModule::OnInit()
495{
496 wxTheClipboard = new wxClipboard();
497
498 return TRUE;
499}
500
501void wxClipboardModule::OnExit()
502{
503 if (wxTheClipboard) delete wxTheClipboard;
504 wxTheClipboard = (wxClipboard*) NULL;
505}
506
47d67540 507#endif // wxUSE_CLIPBOARD
4ce81a75 508