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