]>
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((HDC
) NULL
);
104 HDC hdcSrc
= CreateCompatibleDC((HDC
) 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
,(HPALETTE
) 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((HDC
) NULL
);
224 HDC hdcSrc
= CreateCompatibleDC((HDC
) 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);
326 //-----------------------------------------------------------------------------
328 //-----------------------------------------------------------------------------
330 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
332 wxClipboard
* wxTheClipboard
= (wxClipboard
*) NULL
;
334 wxClipboard::wxClipboard()
339 wxClipboard::~wxClipboard()
344 void wxClipboard::Clear()
346 #if wxUSE_DRAG_AND_DROP
347 wxNode
* node
= m_data
.First();
350 wxDataObject
* data
= (wxDataObject
*) node
->Data();
358 bool wxClipboard::Open()
360 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
364 return wxOpenClipboard();
367 bool wxClipboard::SetData( wxDataObject
*data
)
369 #if wxUSE_DRAG_AND_DROP
370 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
371 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
373 switch (data
->GetFormat())
378 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
379 wxString
str(textDataObject
->GetText());
380 return wxSetClipboardData(data
->GetFormat(), (wxObject
*) (const char*) str
);
386 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
387 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
388 return wxSetClipboardData(data
->GetFormat(), & bitmap
);
394 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*) data
;
395 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
396 return wxSetClipboardData(wxDF_METAFILE
, & metaFile
, metaFileDataObject
->GetWidth(), metaFileDataObject
->GetHeight());
412 void wxClipboard::Close()
414 wxCHECK_RET( m_open
, "clipboard not open" );
420 bool wxClipboard::IsSupportedFormat( wxDataFormat format
, const wxString
& WXUNUSED(id
) )
422 return wxIsClipboardFormatAvailable(format
);
425 bool wxClipboard::GetData( wxDataObject
*data
)
427 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
429 #if wxUSE_DRAG_AND_DROP
430 switch (data
->GetFormat())
435 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
436 char* s
= (char*) wxGetClipboardData(data
->GetFormat());
439 textDataObject
->SetText(s
);
450 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
451 wxBitmap
* bitmap
= (wxBitmap
*) wxGetClipboardData(data
->GetFormat());
454 bitmapDataObject
->SetBitmap(* bitmap
);
465 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*) data
;
466 wxMetafile
* metaFile
= (wxMetafile
*) wxGetClipboardData(wxDF_METAFILE
);
469 metaFileDataObject
->SetMetafile(* metaFile
);
490 //-----------------------------------------------------------------------------
492 //-----------------------------------------------------------------------------
494 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
496 bool wxClipboardModule::OnInit()
498 wxTheClipboard
= new wxClipboard();
503 void wxClipboardModule::OnExit()
505 if (wxTheClipboard
) delete wxTheClipboard
;
506 wxTheClipboard
= (wxClipboard
*) NULL
;
509 #endif // wxUSE_CLIPBOARD