]>
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"
38 #include "wx/metafile.h"
39 #include "wx/clipbrd.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/dib.h"
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
47 IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient
, wxObject
)
50 bool wxClipboardIsOpen
= FALSE
;
52 bool wxOpenClipboard(void)
54 if (wxTheApp
->GetTopWindow() && !wxClipboardIsOpen
)
56 wxClipboardIsOpen
= (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()) != 0);
57 return wxClipboardIsOpen
;
62 bool wxCloseClipboard(void)
64 if (wxClipboardIsOpen
)
66 wxClipboardIsOpen
= FALSE
;
68 return (::CloseClipboard() != 0);
71 bool wxEmptyClipboard(void)
73 return (::EmptyClipboard() != 0);
76 bool wxClipboardOpen(void)
78 return wxClipboardIsOpen
;
81 bool wxIsClipboardFormatAvailable(int dataFormat
)
83 return (::IsClipboardFormatAvailable(dataFormat
) != 0);
86 bool wxSetClipboardData(int dataFormat
, wxObject
*obj
, int width
, int height
)
92 wxBitmap
*wxBM
= (wxBitmap
*)obj
;
94 HDC hdcMem
= CreateCompatibleDC(NULL
);
95 HDC hdcSrc
= CreateCompatibleDC(NULL
);
96 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, (HBITMAP
) wxBM
->GetHBITMAP());
97 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
98 wxBM
->GetWidth(), wxBM
->GetHeight());
101 SelectObject(hdcSrc
, old
);
106 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
107 BitBlt(hdcMem
, 0, 0, wxBM
->GetWidth(), wxBM
->GetHeight(),
108 hdcSrc
, 0, 0, SRCCOPY
);
110 // Select new bitmap out of memory DC
111 SelectObject(hdcMem
, old1
);
114 bool success
= (bool)(::SetClipboardData(CF_BITMAP
, hBitmap
) != 0);
117 SelectObject(hdcSrc
, old
);
125 #if wxUSE_IMAGE_LOADING_IN_MSW
126 HBITMAP hBitmap
=(HBITMAP
) ((wxBitmap
*)obj
)->GetHBITMAP();
127 HANDLE hDIB
=BitmapToDIB(hBitmap
,NULL
); // NULL==uses system palette
128 bool success
= (::SetClipboardData(CF_DIB
,hDIB
) != 0);
138 wxMetaFile
*wxMF
= (wxMetaFile
*)obj
;
139 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
140 #ifdef __WINDOWS_386__
141 METAFILEPICT
*mf
= (METAFILEPICT
*)MK_FP32(GlobalLock(data
));
143 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
146 mf
->mm
= wxMF
->GetWindowsMappingMode();
149 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
151 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
153 return (SetClipboardData(CF_METAFILEPICT
, data
) != 0);
166 dataFormat
= wxDF_TEXT
;
168 width
= strlen((char *)obj
) + 1;
172 char *s
= (char *)obj
;
175 l
= (width
* height
);
176 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
180 #ifdef __WINDOWS_386__
181 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
183 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
187 memcpy(lpGlobalMemory
, s
, l
);
188 #elif defined(__WATCOMC__) && defined(__WINDOWS_386__)
189 memcpy(lpGlobalMemory
, s
, l
);
191 hmemcpy(lpGlobalMemory
, s
, l
);
194 GlobalUnlock(hGlobalMemory
);
195 HANDLE success
= SetClipboardData(dataFormat
, hGlobalMemory
);
196 return (success
!= 0);
203 wxObject
*wxGetClipboardData(int dataFormat
, long *len
)
210 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
214 HDC hdcMem
= CreateCompatibleDC(NULL
);
215 HDC hdcSrc
= CreateCompatibleDC(NULL
);
217 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
218 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
220 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
224 SelectObject(hdcSrc
, old
);
230 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
231 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
232 hdcSrc
, 0, 0, SRCCOPY
);
234 // Select new bitmap out of memory DC
235 SelectObject(hdcMem
, old1
);
238 SelectObject(hdcSrc
, old
);
242 // Create and return a new wxBitmap
243 wxBitmap
*wxBM
= new wxBitmap
;
244 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
245 wxBM
->SetWidth(bm
.bmWidth
);
246 wxBM
->SetHeight(bm
.bmHeight
);
247 wxBM
->SetDepth(bm
.bmPlanes
);
249 return (wxObject
*)wxBM
;
263 dataFormat
= wxDF_TEXT
;
267 HANDLE hGlobalMemory
= GetClipboardData(dataFormat
);
271 int hsize
= (int)GlobalSize(hGlobalMemory
);
275 char *s
= new char[hsize
];
279 #ifdef __WINDOWS_386__
280 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
282 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
286 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
287 #elif __WATCOMC__ && defined(__WINDOWS_386__)
288 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
290 hmemcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
293 GlobalUnlock(hGlobalMemory
);
295 return (wxObject
*)s
;
302 int wxEnumClipboardFormats(int dataFormat
)
304 return ::EnumClipboardFormats(dataFormat
);
307 int wxRegisterClipboardFormat(char *formatName
)
309 return ::RegisterClipboardFormat(formatName
);
312 bool wxGetClipboardFormatName(int dataFormat
, char *formatName
, int maxCount
)
314 return (::GetClipboardFormatName(dataFormat
, formatName
, maxCount
) > 0);
318 * Generalized clipboard implementation by Matthew Flatt
321 wxClipboard
*wxTheClipboard
= NULL
;
323 void wxInitClipboard(void)
326 wxTheClipboard
= new wxClipboard
;
329 wxClipboard::wxClipboard()
335 wxClipboard::~wxClipboard()
338 clipOwner
->BeingReplaced();
343 static int FormatStringToID(char *str
)
345 if (!strcmp(str
, "TEXT"))
348 return wxRegisterClipboardFormat(str
);
351 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
356 clipOwner
->BeingReplaced();
363 if (wxOpenClipboard()) {
364 char **formats
, *data
;
369 formats
= clipOwner
->formats
.ListToArray(FALSE
);
370 for (i
= clipOwner
->formats
.Number(); i
--; ) {
371 ftype
= FormatStringToID(formats
[i
]);
372 data
= clipOwner
->GetData(formats
[i
], &size
);
373 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
374 got_selection
= FALSE
;
380 got_selection
= wxCloseClipboard();
382 got_selection
= FALSE
;
384 got_selection
= FALSE
; // Assume another process takes over
386 if (!got_selection
) {
387 clipOwner
->BeingReplaced();
392 wxClipboardClient
*wxClipboard::GetClipboardClient()
397 void wxClipboard::SetClipboardString(char *str
, long time
)
402 clipOwner
->BeingReplaced();
410 if (wxOpenClipboard()) {
411 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
412 got_selection
= FALSE
;
414 got_selection
= wxCloseClipboard();
416 got_selection
= FALSE
;
418 got_selection
= FALSE
; // Assume another process takes over
420 if (!got_selection
) {
426 char *wxClipboard::GetClipboardString(long time
)
431 str
= GetClipboardData("TEXT", &length
, time
);
440 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
443 if (clipOwner
->formats
.Member(format
))
444 return clipOwner
->GetData(format
, length
);
447 } else if (cbString
) {
448 if (!strcmp(format
, "TEXT"))
449 return copystring(cbString
);
453 if (wxOpenClipboard()) {
454 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
458 receivedString
= NULL
;
460 return receivedString
;
465 #endif // wxUSE_CLIPBOARD