]> git.saurik.com Git - wxWidgets.git/blob - src/os2/clipbrd.cpp
document On{Open,Save}Document()
[wxWidgets.git] / src / os2 / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/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 #if wxUSE_CLIPBOARD
16
17 #include "wx/clipbrd.h"
18
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"
27 #include "wx/log.h"
28 #include "wx/dataobj.h"
29 #endif
30
31 #if wxUSE_METAFILE
32 #include "wx/metafile.h"
33 #endif
34
35 #include <string.h>
36
37 #include "wx/os2/private.h"
38
39 // wxDataObject is tied to OLE/drag and drop implementation,
40 // therefore so is wxClipboard :-(
41
42 // ===========================================================================
43 // implementation
44 // ===========================================================================
45
46 // ---------------------------------------------------------------------------
47 // old-style clipboard functions using Windows API
48 // ---------------------------------------------------------------------------
49
50 static bool gs_wxClipboardIsOpen = false;
51
52 bool wxOpenClipboard()
53 {
54 wxCHECK_MSG( !gs_wxClipboardIsOpen, true, wxT("clipboard already opened.") );
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
71 return false;
72 }
73 */
74 return false;
75 }
76
77 bool wxCloseClipboard()
78 {
79 wxCHECK_MSG( gs_wxClipboardIsOpen, false, wxT("clipboard is not opened") );
80 // TODO:
81 /*
82 gs_wxClipboardIsOpen = false;
83
84 if ( ::CloseClipboard() == 0 )
85 {
86 wxLogSysError(_("Failed to close the clipboard."));
87
88 return false;
89 }
90 */
91 return true;
92 }
93
94 bool wxEmptyClipboard()
95 {
96 // TODO:
97 /*
98 if ( ::EmptyClipboard() == 0 )
99 {
100 wxLogSysError(_("Failed to empty the clipboard."));
101
102 return false;
103 }
104 */
105 return true;
106 }
107
108 bool wxIsClipboardOpened()
109 {
110 return gs_wxClipboardIsOpen;
111 }
112
113 bool wxIsClipboardFormatAvailable(wxDataFormat WXUNUSED(dataFormat))
114 {
115 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
116 return false;
117 }
118
119 #if 0
120 #if wxUSE_DRAG_AND_DROP
121 static bool wxSetClipboardData(wxDataObject *data)
122 {
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
132 return false;
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
147 return false;
148 }
149 */
150 return true;
151 }
152 #endif // wxUSE_DRAG_AND_DROP
153 #endif
154
155 bool wxSetClipboardData(wxDataFormat WXUNUSED(dataFormat),
156 const void *WXUNUSED(data),
157 int WXUNUSED(width), int WXUNUSED(height))
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);
180 return false;
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."));
237 return false;
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
270 return false;
271 }
272 */
273 return true;
274 }
275
276 void *wxGetClipboardData(wxDataFormat WXUNUSED(dataFormat), long *WXUNUSED(len))
277 {
278 // void *retval = NULL;
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);
324 wxBM->SetOk(true);
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."));
337 return NULL;
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 */
400 return NULL;
401 }
402
403 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
404 {
405 // TODO: return ::EnumClipboardFormats(dataFormat);
406 return dataFormat;
407 }
408
409 int wxRegisterClipboardFormat(wxChar *WXUNUSED(formatName))
410 {
411 // TODO: return ::RegisterClipboardFormat(formatName);
412 return 0;
413 }
414
415 bool wxGetClipboardFormatName(wxDataFormat WXUNUSED(dataFormat),
416 wxChar *WXUNUSED(formatName),
417 int WXUNUSED(maxCount))
418 {
419 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
420 return 0;
421 }
422
423 // ---------------------------------------------------------------------------
424 // wxClipboard
425 // ---------------------------------------------------------------------------
426
427 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
428
429 wxClipboard::wxClipboard()
430 {
431 }
432
433 wxClipboard::~wxClipboard()
434 {
435 Clear();
436 }
437
438 void wxClipboard::Clear()
439 {
440 }
441
442 bool wxClipboard::Flush()
443 {
444 // TODO:
445 return false;
446 }
447
448 bool wxClipboard::Open()
449 {
450 return wxOpenClipboard();
451 }
452
453 bool wxClipboard::IsOpened() const
454 {
455 return wxIsClipboardOpened();
456 }
457
458 bool wxClipboard::SetData( wxDataObject *WXUNUSED(data) )
459 {
460 (void)wxEmptyClipboard();
461 // TODO:
462 /*
463 if ( data )
464 return AddData(data);
465 else
466 return true;
467 */
468 return true;
469 }
470
471 bool wxClipboard::AddData( wxDataObject *data )
472 {
473 wxCHECK_MSG( data, false, wxT("data is invalid") );
474
475 #if wxUSE_DRAG_AND_DROP
476 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
477
478 // wxDataFormat format = data->GetPreferredFormat();
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());
496 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
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);
513 }
514 #else // !wxUSE_DRAG_AND_DROP
515 */
516 return false;
517 #else
518 return false;
519 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
520 }
521
522 void wxClipboard::Close()
523 {
524 wxCloseClipboard();
525 }
526
527 bool wxClipboard::IsSupported( const wxDataFormat& format )
528 {
529 return wxIsClipboardFormatAvailable(format);
530 }
531
532 bool wxClipboard::GetData( wxDataObject& WXUNUSED(data) )
533 {
534 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
535
536 #if wxUSE_DRAG_AND_DROP
537 // wxDataFormat format = data.GetPreferredFormat();
538 // TODO:
539 /*
540 switch ( format )
541 {
542 case wxDF_TEXT:
543 case wxDF_OEMTEXT:
544 {
545 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
546 char* s = (char*) wxGetClipboardData(format);
547 if ( s )
548 {
549 textDataObject.SetText(s);
550 delete[] s;
551 return true;
552 }
553 else
554 return false;
555 }
556
557 case wxDF_BITMAP:
558 case wxDF_DIB:
559 {
560 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
561 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
562 if (bitmap)
563 {
564 bitmapDataObject.SetBitmap(* bitmap);
565 delete bitmap;
566 return true;
567 }
568 else
569 return false;
570 }
571 #if wxUSE_METAFILE
572 case wxDF_METAFILE:
573 {
574 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
575 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
576 if (metaFile)
577 {
578 metaFileDataObject.SetMetafile(*metaFile);
579 delete metaFile;
580 return true;
581 }
582 else
583 return false;
584 }
585 #endif
586 default:
587 {
588 long len;
589 void *buf = wxGetClipboardData(format, &len);
590 if ( buf )
591 {
592 // FIXME this is for testing only!
593 ((wxPrivateDataObject &)data).SetData(buf, len);
594 free(buf);
595
596 return true;
597 }
598 }
599
600 return false;
601 }
602 #else
603 */
604 return false;
605 #else
606 return false;
607 #endif
608 }
609
610 #endif // wxUSE_CLIPBOARD