]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "clipbrd.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
30 #include "wx/object.h"
34 #include "wx/bitmap.h"
39 #include "wx/metafile.h"
42 #include "wx/clipbrd.h"
48 #include "wx/msw/private.h"
49 #include "wx/msw/dib.h"
51 // wxDataObject is tied to OLE/drag and drop implementation,
52 // therefore so is wxClipboard :-(
53 #if wxUSE_DRAG_AND_DROP
54 #include "wx/dataobj.h"
59 bool wxClipboardIsOpen
= FALSE
;
61 bool wxOpenClipboard(void)
63 if (wxTheApp
->GetTopWindow() && !wxClipboardIsOpen
)
65 wxClipboardIsOpen
= (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()) != 0);
66 return wxClipboardIsOpen
;
71 bool wxCloseClipboard(void)
73 if (wxClipboardIsOpen
)
75 wxClipboardIsOpen
= FALSE
;
77 return (::CloseClipboard() != 0);
80 bool wxEmptyClipboard(void)
82 return (::EmptyClipboard() != 0);
85 bool wxClipboardOpen(void)
87 return wxClipboardIsOpen
;
90 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
92 return (::IsClipboardFormatAvailable(dataFormat
) != 0);
95 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int width
, int height
)
101 wxBitmap
*wxBM
= (wxBitmap
*)obj
;
103 HDC hdcMem
= CreateCompatibleDC(NULL
);
104 HDC hdcSrc
= CreateCompatibleDC(NULL
);
105 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, (HBITMAP
) wxBM
->GetHBITMAP());
106 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
107 wxBM
->GetWidth(), wxBM
->GetHeight());
110 SelectObject(hdcSrc
, old
);
115 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
116 BitBlt(hdcMem
, 0, 0, wxBM
->GetWidth(), wxBM
->GetHeight(),
117 hdcSrc
, 0, 0, SRCCOPY
);
119 // Select new bitmap out of memory DC
120 SelectObject(hdcMem
, old1
);
123 bool success
= (bool)(::SetClipboardData(CF_BITMAP
, hBitmap
) != 0);
126 SelectObject(hdcSrc
, old
);
134 #if wxUSE_IMAGE_LOADING_IN_MSW
135 HBITMAP hBitmap
=(HBITMAP
) ((wxBitmap
*)obj
)->GetHBITMAP();
136 HANDLE hDIB
=BitmapToDIB(hBitmap
,NULL
); // NULL==uses system palette
137 bool success
= (::SetClipboardData(CF_DIB
,hDIB
) != 0);
147 wxMetafile
*wxMF
= (wxMetafile
*)obj
;
148 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
149 #ifdef __WINDOWS_386__
150 METAFILEPICT
*mf
= (METAFILEPICT
*)MK_FP32(GlobalLock(data
));
152 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
155 mf
->mm
= wxMF
->GetWindowsMappingMode();
158 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
160 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
162 return (SetClipboardData(CF_METAFILEPICT
, data
) != 0);
175 dataFormat
= wxDF_TEXT
;
177 width
= strlen((char *)obj
) + 1;
181 char *s
= (char *)obj
;
184 l
= (width
* height
);
185 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
189 #ifdef __WINDOWS_386__
190 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
192 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
196 memcpy(lpGlobalMemory
, s
, l
);
197 #elif defined(__WATCOMC__) && defined(__WINDOWS_386__)
198 memcpy(lpGlobalMemory
, s
, l
);
200 hmemcpy(lpGlobalMemory
, s
, l
);
203 GlobalUnlock(hGlobalMemory
);
204 HANDLE success
= SetClipboardData(dataFormat
, hGlobalMemory
);
205 return (success
!= 0);
212 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
219 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
223 HDC hdcMem
= CreateCompatibleDC(NULL
);
224 HDC hdcSrc
= CreateCompatibleDC(NULL
);
226 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
227 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
229 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
233 SelectObject(hdcSrc
, old
);
239 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
240 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
241 hdcSrc
, 0, 0, SRCCOPY
);
243 // Select new bitmap out of memory DC
244 SelectObject(hdcMem
, old1
);
247 SelectObject(hdcSrc
, old
);
251 // Create and return a new wxBitmap
252 wxBitmap
*wxBM
= new wxBitmap
;
253 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
254 wxBM
->SetWidth(bm
.bmWidth
);
255 wxBM
->SetHeight(bm
.bmHeight
);
256 wxBM
->SetDepth(bm
.bmPlanes
);
258 return (wxObject
*)wxBM
;
272 dataFormat
= wxDF_TEXT
;
276 HANDLE hGlobalMemory
= GetClipboardData(dataFormat
);
280 int hsize
= (int)GlobalSize(hGlobalMemory
);
284 char *s
= new char[hsize
];
288 #ifdef __WINDOWS_386__
289 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
291 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
295 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
296 #elif __WATCOMC__ && defined(__WINDOWS_386__)
297 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
299 hmemcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
302 GlobalUnlock(hGlobalMemory
);
304 return (wxObject
*)s
;
311 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
313 return (wxDataFormat
) ::EnumClipboardFormats(dataFormat
);
316 int wxRegisterClipboardFormat(char *formatName
)
318 return ::RegisterClipboardFormat(formatName
);
321 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int maxCount
)
323 return (::GetClipboardFormatName((int) dataFormat
, formatName
, maxCount
) > 0);
330 //-----------------------------------------------------------------------------
332 //-----------------------------------------------------------------------------
334 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
336 wxClipboard::wxClipboard()
341 wxClipboard::~wxClipboard()
346 void wxClipboard::Clear()
350 bool wxClipboard::Open()
352 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
356 return wxOpenClipboard();
359 bool wxClipboard::SetData( wxDataObject
*data
)
361 #if wxUSE_DRAG_AND_DROP
362 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
363 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
365 switch (data
->GetFormat())
370 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
371 wxString
str(textDataObject
->GetText());
372 return wxSetClipboardData(data
->GetFormat(), (wxObject
*) (const char*) str
);
378 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
379 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
380 return wxSetClipboardData(data
->GetFormat(), & bitmap
);
386 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*) data
;
387 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
388 return wxSetClipboardData(wxDF_METAFILE
, & metaFile
, metaFileDataObject
->GetWidth(), metaFileDataObject
->GetHeight());
404 void wxClipboard::Close()
406 wxCHECK_RET( m_open
, "clipboard not open" );
412 bool wxClipboard::IsSupportedFormat( wxDataFormat format
, const wxString
& WXUNUSED(id
) )
414 return wxIsClipboardFormatAvailable(format
);
417 bool wxClipboard::GetData( wxDataObject
*data
)
419 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
421 #if wxUSE_DRAG_AND_DROP
422 switch (data
->GetFormat())
427 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
428 char* s
= (char*) wxGetClipboardData(data
->GetFormat());
431 textDataObject
->SetText(s
);
442 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
443 wxBitmap
* bitmap
= (wxBitmap
*) wxGetClipboardData(data
->GetFormat());
446 bitmapDataObject
->SetBitmap(* bitmap
);
457 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*) data
;
458 wxMetafile
* metaFile
= (wxMetafile
*) wxGetClipboardData(wxDF_METAFILE
);
461 metaFileDataObject
->SetMetafile(* metaFile
);
482 #endif // wxUSE_CLIPBOARD