]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
14 #pragma implementation "clipbrd.h"
23 #include "wx/bitmap.h"
25 #include "wx/metafile.h"
26 #include "wx/clipbrd.h"
27 #include "wx/dataobj.h"
30 #pragma message disable nosimpint
33 #include <Xm/CutPaste.h>
35 #pragma message enable nosimpint
40 // IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
41 // IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
43 static bool gs_clipboardIsOpen
= FALSE
;
45 bool wxOpenClipboard()
47 if (!gs_clipboardIsOpen
)
49 gs_clipboardIsOpen
= TRUE
;
56 bool wxCloseClipboard()
58 if (gs_clipboardIsOpen
)
60 gs_clipboardIsOpen
= FALSE
;
67 bool wxEmptyClipboard()
69 // No equivalent in Motif
73 bool wxClipboardOpen()
75 return gs_clipboardIsOpen
;
78 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
83 // Only text is supported.
84 if (dataFormat
!= wxDF_TEXT
)
87 unsigned long numBytes
= 0;
90 Window window
= (Window
) 0;
91 if (wxTheApp
->GetTopWindow())
92 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWindow() );
94 int success
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
95 window
, "TEXT", (XtPointer
) 0, 0, & numBytes
, & privateId
) ;
97 // Assume only text is supported. If we have anything at all,
98 // or the clipboard is locked so we're not sure, we say we support it.
99 if (success
== ClipboardNoData
)
106 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int WXUNUSED(width
), int WXUNUSED(height
))
111 if (dataFormat
!= wxDF_TEXT
)
114 char* data
= (char*) obj
;
116 XmString text
= XmStringCreateSimple ("CLIPBOARD");
117 Window window
= (Window
) 0;
118 if (wxTheApp
->GetTopWindow())
119 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
125 XmClipboardStartCopy((Display
*) wxGetDisplay(),
128 XtLastTimestampProcessed((Display
*) wxGetDisplay()),
131 & itemId
)) != ClipboardSuccess
)
139 XmClipboardCopy((Display
*) wxGetDisplay(),
146 & dataId
)) != ClipboardSuccess
)
151 XmClipboardEndCopy((Display
*) wxGetDisplay(),
152 window
, itemId
) ) != ClipboardSuccess
)
160 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
165 if (dataFormat
!= wxDF_TEXT
)
166 return (wxObject
*) NULL
;
170 unsigned long numBytes
= 0;
172 Window window
= (Window
) 0;
173 if (wxTheApp
->GetTopWindow())
174 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
176 int currentDataSize
= 256;
177 char* data
= new char[currentDataSize
];
181 if (result
== ClipboardTruncate
)
184 currentDataSize
= 2*currentDataSize
;
185 data
= new char[currentDataSize
];
187 result
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
197 case ClipboardSuccess
:
200 *len
= strlen(data
) + 1;
201 return (wxObject
*) data
;
204 case ClipboardTruncate
:
205 case ClipboardLocked
:
210 case ClipboardNoData
:
212 return (wxObject
*) NULL
;
223 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
225 // Only wxDF_TEXT supported
226 if (dataFormat
== wxDF_TEXT
)
232 wxDataFormat
wxRegisterClipboardFormat(char *WXUNUSED(formatName
))
235 return (wxDataFormat
) wxDF_INVALID
;
238 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int WXUNUSED(maxCount
))
240 // Only wxDF_TEXT supported
241 if (dataFormat
== wxDF_TEXT
)
243 strcpy(formatName
, "TEXT");
250 //-----------------------------------------------------------------------------
252 //-----------------------------------------------------------------------------
254 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
256 wxClipboard::wxClipboard()
261 wxClipboard::~wxClipboard()
266 void wxClipboard::Clear()
268 wxNode
* node
= m_data
.First();
271 wxDataObject
* data
= (wxDataObject
*) node
->Data();
278 bool wxClipboard::Open()
280 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
284 return wxOpenClipboard();
287 bool wxClipboard::SetData( wxDataObject
*data
)
289 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
290 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
294 return AddData( data
);
297 bool wxClipboard::AddData( wxDataObject
*data
)
299 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
300 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
302 wxDataFormat::NativeFormat format
= data
->GetPreferredFormat().GetType();
308 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
309 wxString
str(textDataObject
->GetText());
310 return wxSetClipboardData(format
, (wxObject
*) (const char*) str
);
316 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
317 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
318 return wxSetClipboardData(data
->GetType(), & bitmap
);
327 void wxClipboard::Close()
329 wxCHECK_RET( m_open
, "clipboard not open" );
335 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
337 return wxIsClipboardFormatAvailable(format
);
340 bool wxClipboard::GetData( wxDataObject
& data
)
342 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
344 wxDataFormat::NativeFormat format
= data
.GetPreferredFormat().GetType();
350 wxTextDataObject
& textDataObject
= (wxTextDataObject
&) data
;
351 char* s
= (char*) wxGetClipboardData(format
);
354 textDataObject
.SetText(s
);
366 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
367 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetType());
370 bitmapDataObject->SetBitmap(* bitmap);
390 * Old clipboard implementation by Matthew Flatt
393 wxClipboard
*wxTheClipboard
= NULL
;
395 void wxInitClipboard()
398 wxTheClipboard
= new wxClipboard
;
401 wxClipboard::wxClipboard()
407 wxClipboard::~wxClipboard()
410 clipOwner
->BeingReplaced();
415 static int FormatStringToID(char *str
)
417 if (!strcmp(str
, "TEXT"))
420 return wxRegisterClipboardFormat(str
);
423 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
428 clipOwner
->BeingReplaced();
435 if (wxOpenClipboard()) {
436 char **formats
, *data
;
441 formats
= clipOwner
->formats
.ListToArray(FALSE
);
442 for (i
= clipOwner
->formats
.Number(); i
--; ) {
443 ftype
= FormatStringToID(formats
[i
]);
444 data
= clipOwner
->GetData(formats
[i
], &size
);
445 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
446 got_selection
= FALSE
;
452 got_selection
= wxCloseClipboard();
454 got_selection
= FALSE
;
456 got_selection
= FALSE
; // Assume another process takes over
458 if (!got_selection
) {
459 clipOwner
->BeingReplaced();
464 wxClipboardClient
*wxClipboard::GetClipboardClient()
469 void wxClipboard::SetClipboardString(char *str
, long time
)
474 clipOwner
->BeingReplaced();
482 if (wxOpenClipboard()) {
483 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
484 got_selection
= FALSE
;
486 got_selection
= wxCloseClipboard();
488 got_selection
= FALSE
;
490 got_selection
= FALSE
; // Assume another process takes over
492 if (!got_selection
) {
498 char *wxClipboard::GetClipboardString(long time
)
503 str
= GetClipboardData("TEXT", &length
, time
);
512 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
515 if (clipOwner
->formats
.Member(format
))
516 return clipOwner
->GetData(format
, length
);
519 } else if (cbString
) {
520 if (!strcmp(format
, "TEXT"))
521 return copystring(cbString
);
525 if (wxOpenClipboard()) {
526 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
530 receivedString
= NULL
;
532 return receivedString
;
537 #endif // wxUSE_CLIPBOARD