1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "clipbrd.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/object.h"
42 #include "wx/bitmap.h"
47 #include "wx/metafile.h"
50 #include "wx/clipbrd.h"
54 #include "wx/msw/private.h"
55 #include "wx/msw/dib.h"
57 // wxDataObject is tied to OLE/drag and drop implementation,
58 // therefore so is wxClipboard :-(
59 #if wxUSE_DRAG_AND_DROP
60 #include "wx/dataobj.h"
65 // ===========================================================================
67 // ===========================================================================
69 // ---------------------------------------------------------------------------
70 // old-style clipboard functions using Windows API
71 // ---------------------------------------------------------------------------
73 static bool gs_wxClipboardIsOpen
= FALSE
;
75 bool wxOpenClipboard()
77 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, "clipboard already opened." );
79 wxWindow
*win
= wxTheApp
->GetTopWindow();
82 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
84 if ( !gs_wxClipboardIsOpen
)
85 wxLogSysError(_("Failed to open the clipboard."));
87 return gs_wxClipboardIsOpen
;
91 wxLogDebug("Can not open clipboard without a main window,");
97 bool wxCloseClipboard()
99 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, "clipboard is not opened" );
101 gs_wxClipboardIsOpen
= FALSE
;
103 if ( ::CloseClipboard() == 0 )
105 wxLogSysError(_("Failed to close the clipboard."));
113 bool wxEmptyClipboard()
115 if ( ::EmptyClipboard() == 0 )
117 wxLogSysError(_("Failed to empty the clipboard."));
125 bool wxIsClipboardOpened()
127 return gs_wxClipboardIsOpen
;
130 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
132 return ::IsClipboardFormatAvailable(dataFormat
) != 0;
135 bool wxSetClipboardData(wxDataFormat dataFormat
,
137 int width
, int height
)
139 HANDLE handle
= 0; // return value of SetClipboardData
145 wxBitmap
*bitmap
= (wxBitmap
*)data
;
147 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
148 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
149 HBITMAP old
= (HBITMAP
)
150 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
151 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
153 bitmap
->GetHeight());
156 SelectObject(hdcSrc
, old
);
162 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
163 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
164 hdcSrc
, 0, 0, SRCCOPY
);
166 // Select new bitmap out of memory DC
167 SelectObject(hdcMem
, old1
);
170 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
173 SelectObject(hdcSrc
, old
);
181 #if wxUSE_IMAGE_LOADING_IN_MSW
182 wxBitmap
*bitmap
= (wxBitmap
*)data
;
183 HBITMAP hBitmap
= (HBITMAP
)bitmap
->GetHBITMAP();
184 // NULL palette means to use the system one
185 HANDLE hDIB
= BitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
186 handle
= SetClipboardData(CF_DIB
, hDIB
);
194 wxMetafile
*wxMF
= (wxMetafile
*)data
;
195 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
196 #ifdef __WINDOWS_386__
197 METAFILEPICT
*mf
= (METAFILEPICT
*)MK_FP32(GlobalLock(data
));
199 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
202 mf
->mm
= wxMF
->GetWindowsMappingMode();
205 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
207 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
209 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
219 wxLogError(_("Unsupported clipboard format."));
224 dataFormat
= wxDF_TEXT
;
229 char *s
= (char *)data
;
231 width
= strlen(s
) + 1;
233 DWORD l
= (width
* height
);
234 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
237 #ifdef __WINDOWS_386__
238 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
240 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
244 memcpy(lpGlobalMemory
, s
, l
);
245 #elif defined(__WATCOMC__) && defined(__WINDOWS_386__)
246 memcpy(lpGlobalMemory
, s
, l
);
248 hmemcpy(lpGlobalMemory
, s
, l
);
251 GlobalUnlock(hGlobalMemory
);
254 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
261 wxLogSysError(_("Failed to set clipboard data."));
269 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
273 switch ( dataFormat
)
278 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
282 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
283 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
285 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
286 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
288 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
292 SelectObject(hdcSrc
, old
);
298 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
299 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
300 hdcSrc
, 0, 0, SRCCOPY
);
302 // Select new bitmap out of memory DC
303 SelectObject(hdcMem
, old1
);
306 SelectObject(hdcSrc
, old
);
310 // Create and return a new wxBitmap
311 wxBitmap
*wxBM
= new wxBitmap
;
312 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
313 wxBM
->SetWidth(bm
.bmWidth
);
314 wxBM
->SetHeight(bm
.bmHeight
);
315 wxBM
->SetDepth(bm
.bmPlanes
);
329 wxLogError(_("Unsupported clipboard format."));
334 dataFormat
= wxDF_TEXT
;
339 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
343 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
347 char *s
= new char[hsize
];
351 #ifdef __WINDOWS_386__
352 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
354 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
358 memcpy(s
, lpGlobalMemory
, hsize
);
359 #elif __WATCOMC__ && defined(__WINDOWS_386__)
360 memcpy(s
, lpGlobalMemory
, hsize
);
362 hmemcpy(s
, lpGlobalMemory
, hsize
);
365 ::GlobalUnlock(hGlobalMemory
);
374 wxLogSysError(_("Failed to retrieve data from the clipboard."));
380 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
382 return (wxDataFormat
)::EnumClipboardFormats(dataFormat
);
385 int wxRegisterClipboardFormat(char *formatName
)
387 return ::RegisterClipboardFormat(formatName
);
390 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
394 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
397 // ---------------------------------------------------------------------------
399 // ---------------------------------------------------------------------------
401 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
403 wxClipboard
* wxTheClipboard
= (wxClipboard
*)NULL
;
405 wxClipboard::wxClipboard()
409 wxClipboard::~wxClipboard()
414 void wxClipboard::Clear()
418 bool wxClipboard::Open()
420 return wxOpenClipboard();
423 bool wxClipboard::SetData( wxDataObject
*data
)
425 (void)wxEmptyClipboard();
428 return AddData(data
);
433 bool wxClipboard::AddData( wxDataObject
*data
)
435 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
437 #if wxUSE_DRAG_AND_DROP
438 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, "clipboard not open" );
440 wxDataFormat format
= data
->GetFormat();
447 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
448 wxString
str(textDataObject
->GetText());
449 return wxSetClipboardData(format
, str
.c_str());
455 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
456 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
457 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
463 wxMetafileDataObject
* metaFileDataObject
=
464 (wxMetafileDataObject
*) data
;
465 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
466 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
467 metaFileDataObject
->GetWidth(),
468 metaFileDataObject
->GetHeight());
470 #endif // wxUSE_METAFILE
473 wxLogError(_("Can not put data in format '%s' on clipboard."),
474 wxDataObject::GetFormatName(format
));
485 void wxClipboard::Close()
490 bool wxClipboard::IsSupported( wxDataFormat format
)
492 return wxIsClipboardFormatAvailable(format
);
495 bool wxClipboard::GetData( wxDataObject
*data
)
497 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, "clipboard not open" );
499 #if wxUSE_DRAG_AND_DROP
500 wxDataFormat format
= data
->GetFormat();
506 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
507 char* s
= (char*) wxGetClipboardData(format
);
510 textDataObject
->SetText(s
);
521 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*)data
;
522 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
525 bitmapDataObject
->SetBitmap(* bitmap
);
535 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*)data
;
536 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
539 metaFileDataObject
->SetMetafile(*metaFile
);
548 wxLogError(_("Can not get data in format '%s' from clipboard."),
549 wxDataObject::GetFormatName(format
));
558 //-----------------------------------------------------------------------------
560 //-----------------------------------------------------------------------------
562 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
564 bool wxClipboardModule::OnInit()
566 wxTheClipboard
= new wxClipboard();
571 void wxClipboardModule::OnExit()
573 if (wxTheClipboard
) delete wxTheClipboard
;
574 wxTheClipboard
= (wxClipboard
*) NULL
;
578 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
579 #endif // wxUSE_CLIPBOARD