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