]>
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 /////////////////////////////////////////////////////////////////////////////
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"
51 #include "wx/clipbrd.h"
55 #include "wx/msw/private.h"
56 #include "wx/msw/dib.h"
58 // wxDataObject is tied to OLE/drag and drop implementation,
59 // therefore so is wxClipboard :-(
60 #if wxUSE_DRAG_AND_DROP
61 #include "wx/dataobj.h"
66 // ===========================================================================
68 // ===========================================================================
70 // ---------------------------------------------------------------------------
71 // old-style clipboard functions using Windows API
72 // ---------------------------------------------------------------------------
74 static bool gs_wxClipboardIsOpen
= FALSE
;
76 bool wxOpenClipboard()
78 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, "clipboard already opened." );
80 wxWindow
*win
= wxTheApp
->GetTopWindow();
83 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
85 if ( !gs_wxClipboardIsOpen
)
86 wxLogSysError(_("Failed to open the clipboard."));
88 return gs_wxClipboardIsOpen
;
92 wxLogDebug("Can not open clipboard without a main window,");
98 bool wxCloseClipboard()
100 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, "clipboard is not opened" );
102 gs_wxClipboardIsOpen
= FALSE
;
104 if ( ::CloseClipboard() == 0 )
106 wxLogSysError(_("Failed to close the clipboard."));
114 bool wxEmptyClipboard()
116 if ( ::EmptyClipboard() == 0 )
118 wxLogSysError(_("Failed to empty the clipboard."));
126 bool wxIsClipboardOpened()
128 return gs_wxClipboardIsOpen
;
131 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
133 return ::IsClipboardFormatAvailable(dataFormat
) != 0;
136 bool wxSetClipboardData(wxDataFormat dataFormat
,
138 int width
, int height
)
140 HANDLE handle
= 0; // return value of SetClipboardData
146 wxBitmap
*bitmap
= (wxBitmap
*)data
;
148 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
149 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
150 HBITMAP old
= (HBITMAP
)
151 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
152 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
154 bitmap
->GetHeight());
157 SelectObject(hdcSrc
, old
);
163 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
164 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
165 hdcSrc
, 0, 0, SRCCOPY
);
167 // Select new bitmap out of memory DC
168 SelectObject(hdcMem
, old1
);
171 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
174 SelectObject(hdcSrc
, old
);
182 #if wxUSE_IMAGE_LOADING_IN_MSW
183 wxBitmap
*bitmap
= (wxBitmap
*)data
;
184 HBITMAP hBitmap
= (HBITMAP
)bitmap
->GetHBITMAP();
185 // NULL palette means to use the system one
186 HANDLE hDIB
= BitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
187 handle
= SetClipboardData(CF_DIB
, hDIB
);
195 wxMetafile
*wxMF
= (wxMetafile
*)data
;
196 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
197 #ifdef __WINDOWS_386__
198 METAFILEPICT
*mf
= (METAFILEPICT
*)MK_FP32(GlobalLock(data
));
200 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
203 mf
->mm
= wxMF
->GetWindowsMappingMode();
206 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
208 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
210 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
220 wxLogError(_("Unsupported clipboard format."));
225 dataFormat
= wxDF_TEXT
;
230 char *s
= (char *)data
;
232 width
= strlen(s
) + 1;
234 DWORD l
= (width
* height
);
235 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
238 #ifdef __WINDOWS_386__
239 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
241 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
245 memcpy(lpGlobalMemory
, s
, l
);
246 #elif defined(__WATCOMC__) && defined(__WINDOWS_386__)
247 memcpy(lpGlobalMemory
, s
, l
);
249 hmemcpy(lpGlobalMemory
, s
, l
);
252 GlobalUnlock(hGlobalMemory
);
255 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
262 wxLogSysError(_("Failed to set clipboard data."));
270 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
274 switch ( dataFormat
)
279 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
283 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
284 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
286 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
287 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
289 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
293 SelectObject(hdcSrc
, old
);
299 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
300 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
301 hdcSrc
, 0, 0, SRCCOPY
);
303 // Select new bitmap out of memory DC
304 SelectObject(hdcMem
, old1
);
307 SelectObject(hdcSrc
, old
);
311 // Create and return a new wxBitmap
312 wxBitmap
*wxBM
= new wxBitmap
;
313 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
314 wxBM
->SetWidth(bm
.bmWidth
);
315 wxBM
->SetHeight(bm
.bmHeight
);
316 wxBM
->SetDepth(bm
.bmPlanes
);
330 wxLogError(_("Unsupported clipboard format."));
335 dataFormat
= wxDF_TEXT
;
340 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
344 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
348 char *s
= new char[hsize
];
352 #ifdef __WINDOWS_386__
353 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
355 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
359 memcpy(s
, lpGlobalMemory
, hsize
);
360 #elif __WATCOMC__ && defined(__WINDOWS_386__)
361 memcpy(s
, lpGlobalMemory
, hsize
);
363 hmemcpy(s
, lpGlobalMemory
, hsize
);
366 ::GlobalUnlock(hGlobalMemory
);
375 wxLogSysError(_("Failed to retrieve data from the clipboard."));
381 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
383 return (wxDataFormat
)::EnumClipboardFormats(dataFormat
);
386 int wxRegisterClipboardFormat(char *formatName
)
388 return ::RegisterClipboardFormat(formatName
);
391 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
395 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
398 // ---------------------------------------------------------------------------
400 // ---------------------------------------------------------------------------
402 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
404 wxClipboard
* wxTheClipboard
= (wxClipboard
*)NULL
;
406 wxClipboard::wxClipboard()
410 wxClipboard::~wxClipboard()
415 void wxClipboard::Clear()
419 bool wxClipboard::Open()
421 return wxOpenClipboard();
424 bool wxClipboard::SetData( wxDataObject
*data
)
426 (void)wxEmptyClipboard();
429 return AddData(data
);
434 bool wxClipboard::AddData( wxDataObject
*data
)
436 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
438 #if wxUSE_DRAG_AND_DROP
439 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, "clipboard not open" );
441 wxDataFormat format
= data
->GetFormat();
448 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
449 wxString
str(textDataObject
->GetText());
450 return wxSetClipboardData(format
, str
.c_str());
456 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
457 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
458 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
464 wxMetafileDataObject
* metaFileDataObject
=
465 (wxMetafileDataObject
*) data
;
466 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
467 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
468 metaFileDataObject
->GetWidth(),
469 metaFileDataObject
->GetHeight());
471 #endif // wxUSE_METAFILE
474 wxLogError(_("Can not put data in format '%s' on clipboard."),
475 wxDataObject::GetFormatName(format
));
486 void wxClipboard::Close()
491 bool wxClipboard::IsSupported( wxDataFormat format
)
493 return wxIsClipboardFormatAvailable(format
);
496 bool wxClipboard::GetData( wxDataObject
*data
)
498 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, "clipboard not open" );
500 #if wxUSE_DRAG_AND_DROP
501 wxDataFormat format
= data
->GetFormat();
507 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
508 char* s
= (char*) wxGetClipboardData(format
);
511 textDataObject
->SetText(s
);
522 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*)data
;
523 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
526 bitmapDataObject
->SetBitmap(* bitmap
);
536 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*)data
;
537 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
540 metaFileDataObject
->SetMetafile(*metaFile
);
549 wxLogError(_("Can not get data in format '%s' from clipboard."),
550 wxDataObject::GetFormatName(format
));
559 //-----------------------------------------------------------------------------
561 //-----------------------------------------------------------------------------
563 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
565 bool wxClipboardModule::OnInit()
567 wxTheClipboard
= new wxClipboard();
572 void wxClipboardModule::OnExit()
574 if (wxTheClipboard
) delete wxTheClipboard
;
575 wxTheClipboard
= (wxClipboard
*) NULL
;
579 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
580 #endif // wxUSE_CLIPBOARD