]>
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
14 #pragma implementation "clipbrd.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
33 #include "wx/bitmap.h"
37 #include "wx/metafile.h"
38 #include "wx/clipbrd.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/dib.h"
44 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
46 IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient
, wxObject
)
49 bool wxClipboardIsOpen
= FALSE
;
51 bool wxOpenClipboard(void)
53 if (wxTheApp
->GetTopWindow() && !wxClipboardIsOpen
)
55 wxClipboardIsOpen
= (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()) != 0);
56 return wxClipboardIsOpen
;
61 bool wxCloseClipboard(void)
63 if (wxClipboardIsOpen
)
65 wxClipboardIsOpen
= FALSE
;
67 return (::CloseClipboard() != 0);
70 bool wxEmptyClipboard(void)
72 return (::EmptyClipboard() != 0);
75 bool wxClipboardOpen(void)
77 return wxClipboardIsOpen
;
80 bool wxIsClipboardFormatAvailable(int dataFormat
)
82 return (::IsClipboardFormatAvailable(dataFormat
) != 0);
85 bool wxSetClipboardData(int dataFormat
, wxObject
*obj
, int width
, int height
)
91 wxBitmap
*wxBM
= (wxBitmap
*)obj
;
93 HDC hdcMem
= CreateCompatibleDC(NULL
);
94 HDC hdcSrc
= CreateCompatibleDC(NULL
);
95 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, (HBITMAP
) wxBM
->GetHBITMAP());
96 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
97 wxBM
->GetWidth(), wxBM
->GetHeight());
100 SelectObject(hdcSrc
, old
);
105 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
106 BitBlt(hdcMem
, 0, 0, wxBM
->GetWidth(), wxBM
->GetHeight(),
107 hdcSrc
, 0, 0, SRCCOPY
);
109 // Select new bitmap out of memory DC
110 SelectObject(hdcMem
, old1
);
113 bool success
= (bool)(::SetClipboardData(CF_BITMAP
, hBitmap
) != 0);
116 SelectObject(hdcSrc
, old
);
124 #if wxUSE_IMAGE_LOADING_IN_MSW
125 HBITMAP hBitmap
=(HBITMAP
) ((wxBitmap
*)obj
)->GetHBITMAP();
126 HANDLE hDIB
=BitmapToDIB(hBitmap
,NULL
); // NULL==uses system palette
127 bool success
= (::SetClipboardData(CF_DIB
,hDIB
) != 0);
137 wxMetaFile
*wxMF
= (wxMetaFile
*)obj
;
138 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
139 #ifdef __WINDOWS_386__
140 METAFILEPICT
*mf
= (METAFILEPICT
*)MK_FP32(GlobalLock(data
));
142 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
145 mf
->mm
= wxMF
->GetWindowsMappingMode();
148 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
150 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
152 return (SetClipboardData(CF_METAFILEPICT
, data
) != 0);
165 dataFormat
= wxDF_TEXT
;
167 width
= strlen((char *)obj
) + 1;
171 char *s
= (char *)obj
;
174 l
= (width
* height
);
175 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
179 #ifdef __WINDOWS_386__
180 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
182 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
186 memcpy(lpGlobalMemory
, s
, l
);
187 #elif defined(__WATCOMC__) && defined(__WINDOWS_386__)
188 memcpy(lpGlobalMemory
, s
, l
);
190 hmemcpy(lpGlobalMemory
, s
, l
);
193 GlobalUnlock(hGlobalMemory
);
194 HANDLE success
= SetClipboardData(dataFormat
, hGlobalMemory
);
195 return (success
!= 0);
202 wxObject
*wxGetClipboardData(int dataFormat
, long *len
)
209 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
213 HDC hdcMem
= CreateCompatibleDC(NULL
);
214 HDC hdcSrc
= CreateCompatibleDC(NULL
);
216 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
217 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
219 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
223 SelectObject(hdcSrc
, old
);
229 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
230 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
231 hdcSrc
, 0, 0, SRCCOPY
);
233 // Select new bitmap out of memory DC
234 SelectObject(hdcMem
, old1
);
237 SelectObject(hdcSrc
, old
);
241 // Create and return a new wxBitmap
242 wxBitmap
*wxBM
= new wxBitmap
;
243 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
244 wxBM
->SetWidth(bm
.bmWidth
);
245 wxBM
->SetHeight(bm
.bmHeight
);
246 wxBM
->SetDepth(bm
.bmPlanes
);
248 return (wxObject
*)wxBM
;
262 dataFormat
= wxDF_TEXT
;
266 HANDLE hGlobalMemory
= GetClipboardData(dataFormat
);
270 int hsize
= (int)GlobalSize(hGlobalMemory
);
274 char *s
= new char[hsize
];
278 #ifdef __WINDOWS_386__
279 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
281 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
285 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
286 #elif __WATCOMC__ && defined(__WINDOWS_386__)
287 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
289 hmemcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
292 GlobalUnlock(hGlobalMemory
);
294 return (wxObject
*)s
;
301 int wxEnumClipboardFormats(int dataFormat
)
303 return ::EnumClipboardFormats(dataFormat
);
306 int wxRegisterClipboardFormat(char *formatName
)
308 return ::RegisterClipboardFormat(formatName
);
311 bool wxGetClipboardFormatName(int dataFormat
, char *formatName
, int maxCount
)
313 return (::GetClipboardFormatName(dataFormat
, formatName
, maxCount
) > 0);
317 * Generalized clipboard implementation by Matthew Flatt
320 wxClipboard
*wxTheClipboard
= NULL
;
322 void wxInitClipboard(void)
325 wxTheClipboard
= new wxClipboard
;
328 wxClipboard::wxClipboard()
334 wxClipboard::~wxClipboard()
337 clipOwner
->BeingReplaced();
342 static int FormatStringToID(char *str
)
344 if (!strcmp(str
, "TEXT"))
347 return wxRegisterClipboardFormat(str
);
350 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
355 clipOwner
->BeingReplaced();
362 if (wxOpenClipboard()) {
363 char **formats
, *data
;
368 formats
= clipOwner
->formats
.ListToArray(FALSE
);
369 for (i
= clipOwner
->formats
.Number(); i
--; ) {
370 ftype
= FormatStringToID(formats
[i
]);
371 data
= clipOwner
->GetData(formats
[i
], &size
);
372 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
373 got_selection
= FALSE
;
379 got_selection
= wxCloseClipboard();
381 got_selection
= FALSE
;
383 got_selection
= FALSE
; // Assume another process takes over
385 if (!got_selection
) {
386 clipOwner
->BeingReplaced();
391 wxClipboardClient
*wxClipboard::GetClipboardClient()
396 void wxClipboard::SetClipboardString(char *str
, long time
)
401 clipOwner
->BeingReplaced();
409 if (wxOpenClipboard()) {
410 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
411 got_selection
= FALSE
;
413 got_selection
= wxCloseClipboard();
415 got_selection
= FALSE
;
417 got_selection
= FALSE
; // Assume another process takes over
419 if (!got_selection
) {
425 char *wxClipboard::GetClipboardString(long time
)
430 str
= GetClipboardData("TEXT", &length
, time
);
439 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
442 if (clipOwner
->formats
.Member(format
))
443 return clipOwner
->GetData(format
, length
);
446 } else if (cbString
) {
447 if (!strcmp(format
, "TEXT"))
448 return copystring(cbString
);
452 if (wxOpenClipboard()) {
453 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
457 receivedString
= NULL
;
459 return receivedString
;
464 #endif // wxUSE_CLIPBOARD