]>
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"
32 #include "wx/bitmap.h"
36 #include "wx/metafile.h"
37 #include "wx/clipbrd.h"
38 #include "wx/msw/private.h"
39 #include "wx/msw/dib.h"
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
45 IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient
, wxObject
)
48 bool wxClipboardIsOpen
= FALSE
;
50 bool wxOpenClipboard(void)
52 if (wxTheApp
->GetTopWindow() && !wxClipboardIsOpen
)
54 wxClipboardIsOpen
= (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()) != 0);
55 return wxClipboardIsOpen
;
60 bool wxCloseClipboard(void)
62 if (wxClipboardIsOpen
)
64 wxClipboardIsOpen
= FALSE
;
66 return (::CloseClipboard() != 0);
69 bool wxEmptyClipboard(void)
71 return (::EmptyClipboard() != 0);
74 bool wxClipboardOpen(void)
76 return wxClipboardIsOpen
;
79 bool wxIsClipboardFormatAvailable(int dataFormat
)
81 return (::IsClipboardFormatAvailable(dataFormat
) != 0);
84 bool wxSetClipboardData(int dataFormat
, wxObject
*obj
, int width
, int height
)
90 wxBitmap
*wxBM
= (wxBitmap
*)obj
;
92 HDC hdcMem
= CreateCompatibleDC(NULL
);
93 HDC hdcSrc
= CreateCompatibleDC(NULL
);
94 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, (HBITMAP
) wxBM
->GetHBITMAP());
95 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
96 wxBM
->GetWidth(), wxBM
->GetHeight());
99 SelectObject(hdcSrc
, old
);
104 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
105 BitBlt(hdcMem
, 0, 0, wxBM
->GetWidth(), wxBM
->GetHeight(),
106 hdcSrc
, 0, 0, SRCCOPY
);
108 // Select new bitmap out of memory DC
109 SelectObject(hdcMem
, old1
);
112 bool success
= (bool)(::SetClipboardData(CF_BITMAP
, hBitmap
) != 0);
115 SelectObject(hdcSrc
, old
);
123 #if wxUSE_IMAGE_LOADING_IN_MSW
124 HBITMAP hBitmap
=(HBITMAP
) ((wxBitmap
*)obj
)->GetHBITMAP();
125 HANDLE hDIB
=BitmapToDIB(hBitmap
,NULL
); // NULL==uses system palette
126 bool success
= (::SetClipboardData(CF_DIB
,hDIB
) != 0);
136 wxMetaFile
*wxMF
= (wxMetaFile
*)obj
;
137 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
138 #ifdef __WINDOWS_386__
139 METAFILEPICT
*mf
= (METAFILEPICT
*)MK_FP32(GlobalLock(data
));
141 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
144 mf
->mm
= wxMF
->GetWindowsMappingMode();
147 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
149 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
151 return (SetClipboardData(CF_METAFILEPICT
, data
) != 0);
164 dataFormat
= wxDF_TEXT
;
166 width
= strlen((char *)obj
) + 1;
170 char *s
= (char *)obj
;
173 l
= (width
* height
);
174 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
178 #ifdef __WINDOWS_386__
179 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
181 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
185 memcpy(lpGlobalMemory
, s
, l
);
186 #elif defined(__WATCOMC__) && defined(__WINDOWS_386__)
187 memcpy(lpGlobalMemory
, s
, l
);
189 hmemcpy(lpGlobalMemory
, s
, l
);
192 GlobalUnlock(hGlobalMemory
);
193 HANDLE success
= SetClipboardData(dataFormat
, hGlobalMemory
);
194 return (success
!= 0);
201 wxObject
*wxGetClipboardData(int dataFormat
, long *len
)
208 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
212 HDC hdcMem
= CreateCompatibleDC(NULL
);
213 HDC hdcSrc
= CreateCompatibleDC(NULL
);
215 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
216 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
218 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
222 SelectObject(hdcSrc
, old
);
228 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
229 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
230 hdcSrc
, 0, 0, SRCCOPY
);
232 // Select new bitmap out of memory DC
233 SelectObject(hdcMem
, old1
);
236 SelectObject(hdcSrc
, old
);
240 // Create and return a new wxBitmap
241 wxBitmap
*wxBM
= new wxBitmap
;
242 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
243 wxBM
->SetWidth(bm
.bmWidth
);
244 wxBM
->SetHeight(bm
.bmHeight
);
245 wxBM
->SetDepth(bm
.bmPlanes
);
247 return (wxObject
*)wxBM
;
261 dataFormat
= wxDF_TEXT
;
265 HANDLE hGlobalMemory
= GetClipboardData(dataFormat
);
269 int hsize
= (int)GlobalSize(hGlobalMemory
);
273 char *s
= new char[hsize
];
277 #ifdef __WINDOWS_386__
278 LPSTR lpGlobalMemory
= (LPSTR
)MK_FP32(GlobalLock(hGlobalMemory
));
280 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
284 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
285 #elif __WATCOMC__ && defined(__WINDOWS_386__)
286 memcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
288 hmemcpy(s
, lpGlobalMemory
, GlobalSize(hGlobalMemory
));
291 GlobalUnlock(hGlobalMemory
);
293 return (wxObject
*)s
;
300 int wxEnumClipboardFormats(int dataFormat
)
302 return ::EnumClipboardFormats(dataFormat
);
305 int wxRegisterClipboardFormat(char *formatName
)
307 return ::RegisterClipboardFormat(formatName
);
310 bool wxGetClipboardFormatName(int dataFormat
, char *formatName
, int maxCount
)
312 return (::GetClipboardFormatName(dataFormat
, formatName
, maxCount
) > 0);
316 * Generalized clipboard implementation by Matthew Flatt
319 wxClipboard
*wxTheClipboard
= NULL
;
321 void wxInitClipboard(void)
324 wxTheClipboard
= new wxClipboard
;
327 wxClipboard::wxClipboard()
333 wxClipboard::~wxClipboard()
336 clipOwner
->BeingReplaced();
341 static int FormatStringToID(char *str
)
343 if (!strcmp(str
, "TEXT"))
346 return wxRegisterClipboardFormat(str
);
349 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
354 clipOwner
->BeingReplaced();
361 if (wxOpenClipboard()) {
362 char **formats
, *data
;
367 formats
= clipOwner
->formats
.ListToArray(FALSE
);
368 for (i
= clipOwner
->formats
.Number(); i
--; ) {
369 ftype
= FormatStringToID(formats
[i
]);
370 data
= clipOwner
->GetData(formats
[i
], &size
);
371 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
372 got_selection
= FALSE
;
378 got_selection
= wxCloseClipboard();
380 got_selection
= FALSE
;
382 got_selection
= FALSE
; // Assume another process takes over
384 if (!got_selection
) {
385 clipOwner
->BeingReplaced();
390 wxClipboardClient
*wxClipboard::GetClipboardClient()
395 void wxClipboard::SetClipboardString(char *str
, long time
)
400 clipOwner
->BeingReplaced();
408 if (wxOpenClipboard()) {
409 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
410 got_selection
= FALSE
;
412 got_selection
= wxCloseClipboard();
414 got_selection
= FALSE
;
416 got_selection
= FALSE
; // Assume another process takes over
418 if (!got_selection
) {
424 char *wxClipboard::GetClipboardString(long time
)
429 str
= GetClipboardData("TEXT", &length
, time
);
438 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
441 if (clipOwner
->formats
.Member(format
))
442 return clipOwner
->GetData(format
, length
);
445 } else if (cbString
) {
446 if (!strcmp(format
, "TEXT"))
447 return copystring(cbString
);
451 if (wxOpenClipboard()) {
452 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
456 receivedString
= NULL
;
458 return receivedString
;
463 #endif // wxUSE_CLIPBOARD