]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/clipbrd.h"
20 #include "wx/object.h"
24 #include "wx/bitmap.h"
28 #include "wx/dataobj.h"
32 #include "wx/metafile.h"
37 #include "wx/os2/private.h"
39 // wxDataObject is tied to OLE/drag and drop implementation,
40 // therefore so is wxClipboard :-(
42 // ===========================================================================
44 // ===========================================================================
46 // ---------------------------------------------------------------------------
47 // old-style clipboard functions using Windows API
48 // ---------------------------------------------------------------------------
50 static bool gs_wxClipboardIsOpen
= false;
52 bool wxOpenClipboard()
54 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
57 wxWindow *win = wxTheApp->GetTopWindow();
60 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
62 if ( !gs_wxClipboardIsOpen )
64 wxLogSysError(_("Failed to open the clipboard."));
67 return gs_wxClipboardIsOpen;
71 wxLogDebug(wxT("Can not open clipboard without a main window."));
79 bool wxCloseClipboard()
81 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
84 gs_wxClipboardIsOpen = false;
86 if ( ::CloseClipboard() == 0 )
88 wxLogSysError(_("Failed to close the clipboard."));
96 bool wxEmptyClipboard()
100 if ( ::EmptyClipboard() == 0 )
102 wxLogSysError(_("Failed to empty the clipboard."));
110 bool wxIsClipboardOpened()
112 return gs_wxClipboardIsOpen
;
115 bool wxIsClipboardFormatAvailable(wxDataFormat
WXUNUSED(dataFormat
))
117 // TODO: return ::IsClipboardFormatAvailable(dataFormat) != 0;
122 #if wxUSE_DRAG_AND_DROP
123 static bool wxSetClipboardData(wxDataObject
*data
)
127 size_t size = data->GetDataSize();
128 HANDLE hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
131 wxLogSysError(_("Failed to allocate %dKb of memory for clipboard "
132 "transfer."), size / 1024);
137 LPVOID lpGlobalMemory = ::GlobalLock(hGlobal);
139 data->GetDataHere(lpGlobalMemory);
141 GlobalUnlock(hGlobal);
143 wxDataFormat format = data->GetPreferredFormat();
144 if ( !::SetClipboardData(format, hGlobal) )
146 wxLogSysError(_("Failed to set clipboard data in format %s"),
147 wxDataObject::GetFormatName(format));
154 #endif // wxUSE_DRAG_AND_DROP
157 bool wxSetClipboardData(wxDataFormat
WXUNUSED(dataFormat
),
158 const void *WXUNUSED(data
),
159 int WXUNUSED(width
), int WXUNUSED(height
))
163 HANDLE handle = 0; // return value of SetClipboardData
168 wxBitmap *bitmap = (wxBitmap *)data;
170 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
171 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
172 HBITMAP old = (HBITMAP)
173 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
174 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
176 bitmap->GetHeight());
179 SelectObject(hdcSrc, old);
185 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
186 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
187 hdcSrc, 0, 0, SRCCOPY);
189 // Select new bitmap out of memory DC
190 SelectObject(hdcMem, old1);
193 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
196 SelectObject(hdcSrc, old);
204 #if wxUSE_IMAGE_LOADING_IN_MSW
205 wxBitmap *bitmap = (wxBitmap *)data;
206 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
207 // NULL palette means to use the system one
208 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
209 handle = SetClipboardData(CF_DIB, hDIB);
217 wxMetafile *wxMF = (wxMetafile *)data;
218 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
219 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
221 mf->mm = wxMF->GetWindowsMappingMode();
224 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
226 wxMF->SetHMETAFILE((WXHANDLE) NULL);
228 handle = SetClipboardData(CF_METAFILEPICT, data);
238 wxLogError(_("Unsupported clipboard format."));
243 dataFormat = wxDF_TEXT;
248 char *s = (char *)data;
250 width = strlen(s) + 1;
252 DWORD l = (width * height);
253 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
256 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
258 memcpy(lpGlobalMemory, s, l);
260 GlobalUnlock(hGlobalMemory);
263 handle = SetClipboardData(dataFormat, hGlobalMemory);
270 wxLogSysError(_("Failed to set clipboard data."));
278 void *wxGetClipboardData(wxDataFormat
WXUNUSED(dataFormat
), long *WXUNUSED(len
))
280 // void *retval = NULL;
283 switch ( dataFormat )
288 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
292 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
293 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
295 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
296 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
298 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
302 SelectObject(hdcSrc, old);
308 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
309 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
310 hdcSrc, 0, 0, SRCCOPY);
312 // Select new bitmap out of memory DC
313 SelectObject(hdcMem, old1);
316 SelectObject(hdcSrc, old);
320 // Create and return a new wxBitmap
321 wxBitmap *wxBM = new wxBitmap;
322 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
323 wxBM->SetWidth(bm.bmWidth);
324 wxBM->SetHeight(bm.bmHeight);
325 wxBM->SetDepth(bm.bmPlanes);
338 wxLogError(_("Unsupported clipboard format."));
343 dataFormat = wxDF_TEXT;
348 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
352 DWORD hsize = ::GlobalSize(hGlobalMemory);
356 char *s = new char[hsize];
360 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
362 memcpy(s, lpGlobalMemory, hsize);
364 ::GlobalUnlock(hGlobalMemory);
372 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
373 if ( !hGlobalMemory )
376 DWORD size = ::GlobalSize(hGlobalMemory);
380 void *buf = malloc(size);
384 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
386 memcpy(buf, lpGlobalMemory, size);
388 ::GlobalUnlock(hGlobalMemory);
397 wxLogSysError(_("Failed to retrieve data from the clipboard."));
405 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
407 // TODO: return ::EnumClipboardFormats(dataFormat);
411 int wxRegisterClipboardFormat(wxChar
*WXUNUSED(formatName
))
413 // TODO: return ::RegisterClipboardFormat(formatName);
417 bool wxGetClipboardFormatName(wxDataFormat
WXUNUSED(dataFormat
),
418 wxChar
*WXUNUSED(formatName
),
419 int WXUNUSED(maxCount
))
421 // TODO: return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
425 // ---------------------------------------------------------------------------
427 // ---------------------------------------------------------------------------
429 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
431 wxClipboard::wxClipboard()
435 wxClipboard::~wxClipboard()
440 void wxClipboard::Clear()
444 bool wxClipboard::Flush()
450 bool wxClipboard::Open()
452 return wxOpenClipboard();
455 bool wxClipboard::IsOpened() const
457 return wxIsClipboardOpened();
460 bool wxClipboard::SetData( wxDataObject
*WXUNUSED(data
) )
462 (void)wxEmptyClipboard();
466 return AddData(data);
473 bool wxClipboard::AddData( wxDataObject
*data
)
475 wxCHECK_MSG( data
, false, wxT("data is invalid") );
477 #if wxUSE_DRAG_AND_DROP
478 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
480 // wxDataFormat format = data->GetPreferredFormat();
488 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
489 wxString str(textDataObject->GetText());
490 return wxSetClipboardData(format, str.c_str());
496 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
497 wxBitmap bitmap(bitmapDataObject->GetBitmap());
498 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
504 wxMetafileDataObject* metaFileDataObject =
505 (wxMetafileDataObject*) data;
506 wxMetafile metaFile = metaFileDataObject->GetMetafile();
507 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
508 metaFileDataObject->GetWidth(),
509 metaFileDataObject->GetHeight());
511 #endif // wxUSE_METAFILE
514 return wxSetClipboardData(data);
516 #else // !wxUSE_DRAG_AND_DROP
521 #endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
524 void wxClipboard::Close()
529 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
531 return wxIsClipboardFormatAvailable(format
);
534 bool wxClipboard::GetData( wxDataObject
& WXUNUSED(data
) )
536 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
538 #if wxUSE_DRAG_AND_DROP
539 // wxDataFormat format = data.GetPreferredFormat();
547 wxTextDataObject& textDataObject = (wxTextDataObject&) data;
548 char* s = (char*) wxGetClipboardData(format);
551 textDataObject.SetText(s);
562 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
563 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetPreferredFormat());
566 bitmapDataObject.SetBitmap(* bitmap);
576 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
577 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
580 metaFileDataObject.SetMetafile(*metaFile);
591 void *buf = wxGetClipboardData(format, &len);
594 // FIXME this is for testing only!
595 ((wxPrivateDataObject &)data).SetData(buf, len);
612 #endif // wxUSE_CLIPBOARD