]> git.saurik.com Git - wxWidgets.git/blame - src/os2/clipbrd.cpp
some cleanup, and plug a few small holes
[wxWidgets.git] / src / os2 / clipbrd.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
cb7d7375 2// Name: src/os2/clipbrd.cpp
0e320a79 3// Purpose: Clipboard functionality
37f214d5 4// Author: David Webster
0e320a79 5// Modified by:
37f214d5 6// Created: 10/13/99
0e320a79 7// RCS-ID: $Id$
37f214d5 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
37f214d5
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
37f214d5
DW
15#if wxUSE_CLIPBOARD
16
e4db172a
WS
17#include "wx/clipbrd.h"
18
37f214d5
DW
19#ifndef WX_PRECOMP
20 #include "wx/object.h"
21 #include "wx/event.h"
22 #include "wx/app.h"
23 #include "wx/frame.h"
24 #include "wx/bitmap.h"
25 #include "wx/utils.h"
26 #include "wx/intl.h"
e4db172a 27 #include "wx/log.h"
28f92d74 28 #include "wx/dataobj.h"
37f214d5
DW
29#endif
30
31#if wxUSE_METAFILE
32 #include "wx/metafile.h"
0e320a79
DW
33#endif
34
0e320a79
DW
35#include <string.h>
36
37f214d5
DW
37#include "wx/os2/private.h"
38
39// wxDataObject is tied to OLE/drag and drop implementation,
40// therefore so is wxClipboard :-(
0e320a79 41
37f214d5
DW
42// ===========================================================================
43// implementation
44// ===========================================================================
45
46// ---------------------------------------------------------------------------
47// old-style clipboard functions using Windows API
48// ---------------------------------------------------------------------------
49
cb7d7375 50static bool gs_wxClipboardIsOpen = false;
37f214d5 51
0e320a79
DW
52bool wxOpenClipboard()
53{
6670f564 54 wxCHECK_MSG( !gs_wxClipboardIsOpen, true, wxT("clipboard already opened.") );
37f214d5
DW
55// TODO:
56/*
57 wxWindow *win = wxTheApp->GetTopWindow();
58 if ( win )
59 {
60 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
61
62 if ( !gs_wxClipboardIsOpen )
63 wxLogSysError(_("Failed to open the clipboard."));
64
65 return gs_wxClipboardIsOpen;
66 }
67 else
68 {
69 wxLogDebug(wxT("Can not open clipboard without a main window."));
70
cb7d7375 71 return false;
37f214d5
DW
72 }
73*/
cb7d7375 74 return false;
0e320a79
DW
75}
76
77bool wxCloseClipboard()
78{
cb7d7375 79 wxCHECK_MSG( gs_wxClipboardIsOpen, false, wxT("clipboard is not opened") );
37f214d5
DW
80// TODO:
81/*
cb7d7375 82 gs_wxClipboardIsOpen = false;
37f214d5
DW
83
84 if ( ::CloseClipboard() == 0 )
85 {
86 wxLogSysError(_("Failed to close the clipboard."));
87
cb7d7375 88 return false;
37f214d5
DW
89 }
90*/
6670f564 91 return true;
0e320a79
DW
92}
93
94bool wxEmptyClipboard()
95{
37f214d5
DW
96// TODO:
97/*
98 if ( ::EmptyClipboard() == 0 )
99 {
100 wxLogSysError(_("Failed to empty the clipboard."));
101
cb7d7375 102 return false;
37f214d5
DW
103 }
104*/
6670f564 105 return true;
0e320a79
DW
106}
107
37f214d5 108bool wxIsClipboardOpened()
0e320a79 109{
37f214d5 110 return gs_wxClipboardIsOpen;
0e320a79
DW
111}
112
6670f564 113bool wxIsClipboardFormatAvailable(wxDataFormat WXUNUSED(dataFormat))
0e320a79 114{
37f214d5 115 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
6670f564 116 return false;
0e320a79
DW
117}
118
9923c37d 119#if 0
37f214d5
DW
120#if wxUSE_DRAG_AND_DROP
121static bool wxSetClipboardData(wxDataObject *data)
0e320a79 122{
37f214d5
DW
123 // TODO:
124/*
125 size_t size = data->GetDataSize();
126 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
127 if ( !hGlobal )
128 {
129 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
130 "transfer."), size / 1024);
131
cb7d7375 132 return false;
37f214d5
DW
133 }
134
135 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
136
137 data->GetDataHere(lpGlobalMemory);
138
139 GlobalUnlock(hGlobal);
140
141 wxDataFormat format = data->GetPreferredFormat();
142 if ( !::SetClipboardData(format, hGlobal) )
143 {
144 wxLogSysError(_("Failed to set clipboard data in format %s"),
145 wxDataObject::GetFormatName(format));
146
cb7d7375 147 return false;
37f214d5
DW
148 }
149*/
6670f564 150 return true;
37f214d5
DW
151}
152#endif // wxUSE_DRAG_AND_DROP
9923c37d 153#endif
37f214d5 154
cb7d7375
WS
155bool wxSetClipboardData(wxDataFormat WXUNUSED(dataFormat),
156 const void *WXUNUSED(data),
157 int WXUNUSED(width), int WXUNUSED(height))
37f214d5
DW
158{
159// TODO:
160/*
161 HANDLE handle = 0; // return value of SetClipboardData
162 switch (dataFormat)
163 {
164 case wxDF_BITMAP:
165 {
166 wxBitmap *bitmap = (wxBitmap *)data;
167
168 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
169 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
170 HBITMAP old = (HBITMAP)
171 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
172 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
173 bitmap->GetWidth(),
174 bitmap->GetHeight());
175 if (!hBitmap)
176 {
177 SelectObject(hdcSrc, old);
178 DeleteDC(hdcMem);
179 DeleteDC(hdcSrc);
cb7d7375 180 return false;
37f214d5
DW
181 }
182
183 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
184 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
185 hdcSrc, 0, 0, SRCCOPY);
186
187 // Select new bitmap out of memory DC
188 SelectObject(hdcMem, old1);
189
190 // Set the data
191 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
192
193 // Clean up
194 SelectObject(hdcSrc, old);
195 DeleteDC(hdcSrc);
196 DeleteDC(hdcMem);
197 break;
198 }
199
200 case wxDF_DIB:
201 {
202#if wxUSE_IMAGE_LOADING_IN_MSW
203 wxBitmap *bitmap = (wxBitmap *)data;
204 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
205 // NULL palette means to use the system one
206 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
207 handle = SetClipboardData(CF_DIB, hDIB);
208#endif
209 break;
210 }
211
212#if wxUSE_METAFILE
213 case wxDF_METAFILE:
214 {
215 wxMetafile *wxMF = (wxMetafile *)data;
216 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
217 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
218
219 mf->mm = wxMF->GetWindowsMappingMode();
220 mf->xExt = width;
221 mf->yExt = height;
222 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
223 GlobalUnlock(data);
224 wxMF->SetHMETAFILE((WXHANDLE) NULL);
225
226 handle = SetClipboardData(CF_METAFILEPICT, data);
227 break;
228 }
229#endif
230 case CF_SYLK:
231 case CF_DIF:
232 case CF_TIFF:
233 case CF_PALETTE:
234 default:
235 {
236 wxLogError(_("Unsupported clipboard format."));
cb7d7375 237 return false;
37f214d5
DW
238 }
239
240 case wxDF_OEMTEXT:
241 dataFormat = wxDF_TEXT;
242 // fall through
243
244 case wxDF_TEXT:
245 {
246 char *s = (char *)data;
247
248 width = strlen(s) + 1;
249 height = 1;
250 DWORD l = (width * height);
251 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
252 if ( hGlobalMemory )
253 {
254 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
255
256 memcpy(lpGlobalMemory, s, l);
257
258 GlobalUnlock(hGlobalMemory);
259 }
260
261 handle = SetClipboardData(dataFormat, hGlobalMemory);
262 break;
263 }
264 }
265
266 if ( handle == 0 )
267 {
268 wxLogSysError(_("Failed to set clipboard data."));
269
cb7d7375 270 return false;
37f214d5
DW
271 }
272*/
6670f564 273 return true;
0e320a79
DW
274}
275
cb7d7375 276void *wxGetClipboardData(wxDataFormat WXUNUSED(dataFormat), long *WXUNUSED(len))
0e320a79 277{
9923c37d 278// void *retval = NULL;
37f214d5
DW
279// TODO:
280/*
281 switch ( dataFormat )
282 {
283 case wxDF_BITMAP:
284 {
285 BITMAP bm;
286 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
287 if (!hBitmap)
288 break;
289
290 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
291 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
292
293 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
294 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
295
296 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
297
298 if (!hNewBitmap)
299 {
300 SelectObject(hdcSrc, old);
301 DeleteDC(hdcMem);
302 DeleteDC(hdcSrc);
303 break;
304 }
305
306 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
307 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
308 hdcSrc, 0, 0, SRCCOPY);
309
310 // Select new bitmap out of memory DC
311 SelectObject(hdcMem, old1);
312
313 // Clean up
314 SelectObject(hdcSrc, old);
315 DeleteDC(hdcSrc);
316 DeleteDC(hdcMem);
317
318 // Create and return a new wxBitmap
319 wxBitmap *wxBM = new wxBitmap;
320 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
321 wxBM->SetWidth(bm.bmWidth);
322 wxBM->SetHeight(bm.bmHeight);
323 wxBM->SetDepth(bm.bmPlanes);
6670f564 324 wxBM->SetOk(true);
37f214d5
DW
325 retval = wxBM;
326 break;
327 }
328
329 case wxDF_METAFILE:
330 case CF_SYLK:
331 case CF_DIF:
332 case CF_TIFF:
333 case CF_PALETTE:
334 case wxDF_DIB:
335 {
336 wxLogError(_("Unsupported clipboard format."));
cb7d7375 337 return NULL;
37f214d5
DW
338 }
339
340 case wxDF_OEMTEXT:
341 dataFormat = wxDF_TEXT;
342 // fall through
343
344 case wxDF_TEXT:
345 {
346 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
347 if (!hGlobalMemory)
348 break;
349
350 DWORD hsize = ::GlobalSize(hGlobalMemory);
351 if (len)
352 *len = hsize;
353
354 char *s = new char[hsize];
355 if (!s)
356 break;
357
358 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
359
360 memcpy(s, lpGlobalMemory, hsize);
361
362 ::GlobalUnlock(hGlobalMemory);
363
364 retval = s;
365 break;
366 }
367
368 default:
369 {
370 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
371 if ( !hGlobalMemory )
372 break;
373
374 DWORD size = ::GlobalSize(hGlobalMemory);
375 if ( len )
376 *len = size;
377
378 void *buf = malloc(size);
379 if ( !buf )
380 break;
381
382 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
383
384 memcpy(buf, lpGlobalMemory, size);
385
386 ::GlobalUnlock(hGlobalMemory);
387
388 retval = buf;
389 break;
390 }
391 }
392
393 if ( !retval )
394 {
395 wxLogSysError(_("Failed to retrieve data from the clipboard."));
396 }
397
398 return retval;
399*/
0e320a79
DW
400 return NULL;
401}
402
37f214d5 403wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
0e320a79 404{
37f214d5
DW
405 // TODO: return ::EnumClipboardFormats(dataFormat);
406 return dataFormat;
0e320a79
DW
407}
408
cb7d7375 409int wxRegisterClipboardFormat(wxChar *WXUNUSED(formatName))
0e320a79 410{
37f214d5
DW
411 // TODO: return ::RegisterClipboardFormat(formatName);
412 return 0;
0e320a79
DW
413}
414
cb7d7375
WS
415bool wxGetClipboardFormatName(wxDataFormat WXUNUSED(dataFormat),
416 wxChar *WXUNUSED(formatName),
417 int WXUNUSED(maxCount))
0e320a79 418{
37f214d5
DW
419 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
420 return 0;
0e320a79
DW
421}
422
37f214d5
DW
423// ---------------------------------------------------------------------------
424// wxClipboard
425// ---------------------------------------------------------------------------
0e320a79 426
37f214d5 427IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
0e320a79 428
0e320a79
DW
429wxClipboard::wxClipboard()
430{
0e320a79
DW
431}
432
433wxClipboard::~wxClipboard()
434{
37f214d5 435 Clear();
0e320a79
DW
436}
437
37f214d5 438void wxClipboard::Clear()
0e320a79 439{
37f214d5 440}
0e320a79 441
6dddc146
DW
442bool wxClipboard::Flush()
443{
444 // TODO:
cb7d7375 445 return false;
6dddc146
DW
446}
447
37f214d5
DW
448bool wxClipboard::Open()
449{
450 return wxOpenClipboard();
0e320a79
DW
451}
452
6dddc146
DW
453bool wxClipboard::IsOpened() const
454{
455 return wxIsClipboardOpened();
456}
457
cb7d7375 458bool wxClipboard::SetData( wxDataObject *WXUNUSED(data) )
0e320a79 459{
37f214d5
DW
460 (void)wxEmptyClipboard();
461 // TODO:
462 /*
463 if ( data )
464 return AddData(data);
465 else
6670f564 466 return true;
37f214d5 467 */
6670f564 468 return true;
37f214d5 469}
0e320a79 470
37f214d5
DW
471bool wxClipboard::AddData( wxDataObject *data )
472{
cb7d7375 473 wxCHECK_MSG( data, false, wxT("data is invalid") );
0e320a79 474
37f214d5 475#if wxUSE_DRAG_AND_DROP
cb7d7375 476 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
0e320a79 477
2dab12be 478// wxDataFormat format = data->GetPreferredFormat();
37f214d5
DW
479// TODO:
480/*
481 switch ( format )
482 {
483 case wxDF_TEXT:
484 case wxDF_OEMTEXT:
485 {
486 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
487 wxString str(textDataObject->GetText());
488 return wxSetClipboardData(format, str.c_str());
489 }
490
491 case wxDF_BITMAP:
492 case wxDF_DIB:
493 {
494 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
495 wxBitmap bitmap(bitmapDataObject->GetBitmap());
2dab12be 496 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
37f214d5
DW
497 }
498
499#if wxUSE_METAFILE
500 case wxDF_METAFILE:
501 {
502 wxMetafileDataObject* metaFileDataObject =
503 (wxMetafileDataObject*) data;
504 wxMetafile metaFile = metaFileDataObject->GetMetafile();
505 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
506 metaFileDataObject->GetWidth(),
507 metaFileDataObject->GetHeight());
508 }
509#endif // wxUSE_METAFILE
510
511 default:
512 return wxSetClipboardData(data);
0e320a79 513 }
37f214d5
DW
514#else // !wxUSE_DRAG_AND_DROP
515*/
cb7d7375 516 return false;
19193a2c 517#else
cb7d7375 518 return false;
37f214d5 519#endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
0e320a79
DW
520}
521
37f214d5 522void wxClipboard::Close()
0e320a79 523{
37f214d5 524 wxCloseClipboard();
0e320a79
DW
525}
526
2af18715 527bool wxClipboard::IsSupported( const wxDataFormat& format )
0e320a79 528{
37f214d5
DW
529 return wxIsClipboardFormatAvailable(format);
530}
0e320a79 531
cb7d7375 532bool wxClipboard::GetData( wxDataObject& WXUNUSED(data) )
37f214d5 533{
cb7d7375 534 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
0e320a79 535
37f214d5 536#if wxUSE_DRAG_AND_DROP
2dab12be 537// wxDataFormat format = data.GetPreferredFormat();
37f214d5
DW
538 // TODO:
539/*
540 switch ( format )
541 {
542 case wxDF_TEXT:
543 case wxDF_OEMTEXT:
544 {
efe7d6ff 545 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
37f214d5
DW
546 char* s = (char*) wxGetClipboardData(format);
547 if ( s )
548 {
efe7d6ff 549 textDataObject.SetText(s);
37f214d5 550 delete[] s;
6670f564 551 return true;
37f214d5
DW
552 }
553 else
cb7d7375 554 return false;
37f214d5
DW
555 }
556
557 case wxDF_BITMAP:
558 case wxDF_DIB:
559 {
efe7d6ff 560 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
2dab12be 561 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
37f214d5
DW
562 if (bitmap)
563 {
efe7d6ff 564 bitmapDataObject.SetBitmap(* bitmap);
37f214d5 565 delete bitmap;
6670f564 566 return true;
37f214d5
DW
567 }
568 else
cb7d7375 569 return false;
37f214d5
DW
570 }
571#if wxUSE_METAFILE
572 case wxDF_METAFILE:
573 {
efe7d6ff 574 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
37f214d5
DW
575 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
576 if (metaFile)
577 {
efe7d6ff 578 metaFileDataObject.SetMetafile(*metaFile);
37f214d5 579 delete metaFile;
6670f564 580 return true;
37f214d5
DW
581 }
582 else
cb7d7375 583 return false;
37f214d5
DW
584 }
585#endif
586 default:
587 {
588 long len;
589 void *buf = wxGetClipboardData(format, &len);
590 if ( buf )
591 {
cb7d7375 592 // FIXME this is for testing only!
efe7d6ff 593 ((wxPrivateDataObject &)data).SetData(buf, len);
37f214d5
DW
594 free(buf);
595
6670f564 596 return true;
37f214d5
DW
597 }
598 }
599
cb7d7375 600 return false;
37f214d5
DW
601 }
602#else
603*/
cb7d7375 604 return false;
19193a2c 605#else
cb7d7375 606 return false;
37f214d5
DW
607#endif
608}
0e320a79 609
37f214d5 610#endif // wxUSE_CLIPBOARD