]>
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);
326 //-----------------------------------------------------------------------------
328 //-----------------------------------------------------------------------------
330 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
332 wxClipboard
* wxTheClipboard
= (wxClipboard
*) NULL
;
334 wxClipboard::wxClipboard()
339 wxClipboard::~wxClipboard()
344 void wxClipboard::Clear()
346 wxNode
* node
= m_data
.First();
349 wxDataObject
* data
= (wxDataObject
*) node
->Data();
356 bool wxClipboard::Open()
358 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
362 return wxOpenClipboard();
365 bool wxClipboard::SetData( wxDataObject
*data
)
367 #if wxUSE_DRAG_AND_DROP
368 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
369 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
371 switch (data
->GetFormat())
376 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
377 wxString
str(textDataObject
->GetText());
378 return wxSetClipboardData(data
->GetFormat(), (wxObject
*) (const char*) str
);
384 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
385 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
386 return wxSetClipboardData(data
->GetFormat(), & bitmap
);
392 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*) data
;
393 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
394 return wxSetClipboardData(wxDF_METAFILE
, & metaFile
, metaFileDataObject
->GetWidth(), metaFileDataObject
->GetHeight());
410 void wxClipboard::Close()
412 wxCHECK_RET( m_open
, "clipboard not open" );
418 bool wxClipboard::IsSupportedFormat( wxDataFormat format
, const wxString
& WXUNUSED(id
) )
420 return wxIsClipboardFormatAvailable(format
);
423 bool wxClipboard::GetData( wxDataObject
*data
)
425 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
427 #if wxUSE_DRAG_AND_DROP
428 switch (data
->GetFormat())
433 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
434 char* s
= (char*) wxGetClipboardData(data
->GetFormat());
437 textDataObject
->SetText(s
);
448 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
449 wxBitmap
* bitmap
= (wxBitmap
*) wxGetClipboardData(data
->GetFormat());
452 bitmapDataObject
->SetBitmap(* bitmap
);
463 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*) data
;
464 wxMetafile
* metaFile
= (wxMetafile
*) wxGetClipboardData(wxDF_METAFILE
);
467 metaFileDataObject
->SetMetafile(* metaFile
);
488 //-----------------------------------------------------------------------------
490 //-----------------------------------------------------------------------------
492 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
494 bool wxClipboardModule::OnInit()
496 wxTheClipboard
= new wxClipboard();
501 void wxClipboardModule::OnExit()
503 if (wxTheClipboard
) delete wxTheClipboard
;
504 wxTheClipboard
= (wxClipboard
*) NULL
;
507 #endif // wxUSE_CLIPBOARD