]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/clipbrd.cpp
f3eb66e6ede0ffcb81edb94423f6ca1725cfe491
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 #include <Xm/CutPaste.h>
34 #if !USE_SHARED_LIBRARY
35 // IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
36 // IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
39 static bool gs_clipboardIsOpen
= FALSE
;
41 bool wxOpenClipboard()
43 if (!gs_clipboardIsOpen
)
45 gs_clipboardIsOpen
= TRUE
;
52 bool wxCloseClipboard()
54 if (gs_clipboardIsOpen
)
56 gs_clipboardIsOpen
= FALSE
;
63 bool wxEmptyClipboard()
65 // No equivalent in Motif
69 bool wxClipboardOpen()
71 return gs_clipboardIsOpen
;
74 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
76 // Only text is supported.
77 if (dataFormat
!= wxDF_TEXT
)
80 unsigned long numBytes
= 0;
83 Window window
= (Window
) 0;
84 if (wxTheApp
->GetTopWindow())
85 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
87 int success
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
88 window
, "TEXT", (XtPointer
) 0, 0, & numBytes
, & privateId
) ;
90 // Assume only text is supported. If we have anything at all,
91 // or the clipboard is locked so we're not sure, we say we support it.
92 if (success
== ClipboardNoData
)
98 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int width
, int height
)
100 if (dataFormat
!= wxDF_TEXT
)
103 char* data
= (char*) obj
;
105 XmString text
= XmStringCreateSimple ("CLIPBOARD");
106 Window window
= (Window
) 0;
107 if (wxTheApp
->GetTopWindow())
108 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
114 XmClipboardStartCopy((Display
*) wxGetDisplay(),
117 XtLastTimestampProcessed((Display
*) wxGetDisplay()),
120 & itemId
)) != ClipboardSuccess
)
128 XmClipboardCopy((Display
*) wxGetDisplay(),
135 & dataId
)) != ClipboardSuccess
)
140 XmClipboardEndCopy((Display
*) wxGetDisplay(),
141 window
, itemId
) ) != ClipboardSuccess
)
148 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
150 if (dataFormat
!= wxDF_TEXT
)
151 return (wxObject
*) NULL
;
155 unsigned long numBytes
= 0;
157 Window window
= (Window
) 0;
158 if (wxTheApp
->GetTopWindow())
159 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
161 int currentDataSize
= 256;
162 char* data
= new char[currentDataSize
];
166 if (result
== ClipboardTruncate
)
169 currentDataSize
= 2*currentDataSize
;
170 data
= new char[currentDataSize
];
172 result
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
182 case ClipboardSuccess
:
185 *len
= strlen(data
) + 1;
186 return (wxObject
*) data
;
189 case ClipboardTruncate
:
190 case ClipboardLocked
:
195 case ClipboardNoData
:
197 return (wxObject
*) NULL
;
207 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
209 // Only wxDF_TEXT supported
210 if (dataFormat
== wxDF_TEXT
)
216 wxDataFormat
wxRegisterClipboardFormat(char *formatName
)
219 return (wxDataFormat
) wxDF_INVALID
;
222 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int maxCount
)
224 // Only wxDF_TEXT supported
225 if (dataFormat
== wxDF_TEXT
)
227 strcpy(formatName
, "TEXT");
234 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
238 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
240 wxClipboard
* wxTheClipboard
= (wxClipboard
*) NULL
;
242 wxClipboard::wxClipboard()
247 wxClipboard::~wxClipboard()
252 void wxClipboard::Clear()
254 wxNode
* node
= m_data
.First();
257 wxDataObject
* data
= (wxDataObject
*) node
->Data();
264 bool wxClipboard::Open()
266 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
270 return wxOpenClipboard();
273 bool wxClipboard::SetData( wxDataObject
*data
)
275 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
276 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
280 return AddData( data
);
283 bool wxClipboard::AddData( wxDataObject
*data
)
285 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
286 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
288 wxDataFormat::NativeFormat format
= data
->GetPreferredFormat().GetType();
294 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
295 wxString
str(textDataObject
->GetText());
296 return wxSetClipboardData(format
, (wxObject
*) (const char*) str
);
302 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
303 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
304 return wxSetClipboardData(data
->GetType(), & bitmap
);
313 void wxClipboard::Close()
315 wxCHECK_RET( m_open
, "clipboard not open" );
321 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
323 return wxIsClipboardFormatAvailable(format
);
326 bool wxClipboard::GetData( wxDataObject
& data
)
328 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
330 wxDataFormat::NativeFormat format
= data
.GetPreferredFormat().GetType();
336 wxTextDataObject
& textDataObject
= (wxTextDataObject
&) data
;
337 char* s
= (char*) wxGetClipboardData(format
);
340 textDataObject
.SetText(s
);
352 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
353 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetType());
356 bitmapDataObject->SetBitmap(* bitmap);
376 * Old clipboard implementation by Matthew Flatt
379 wxClipboard
*wxTheClipboard
= NULL
;
381 void wxInitClipboard()
384 wxTheClipboard
= new wxClipboard
;
387 wxClipboard::wxClipboard()
393 wxClipboard::~wxClipboard()
396 clipOwner
->BeingReplaced();
401 static int FormatStringToID(char *str
)
403 if (!strcmp(str
, "TEXT"))
406 return wxRegisterClipboardFormat(str
);
409 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
414 clipOwner
->BeingReplaced();
421 if (wxOpenClipboard()) {
422 char **formats
, *data
;
427 formats
= clipOwner
->formats
.ListToArray(FALSE
);
428 for (i
= clipOwner
->formats
.Number(); i
--; ) {
429 ftype
= FormatStringToID(formats
[i
]);
430 data
= clipOwner
->GetData(formats
[i
], &size
);
431 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
432 got_selection
= FALSE
;
438 got_selection
= wxCloseClipboard();
440 got_selection
= FALSE
;
442 got_selection
= FALSE
; // Assume another process takes over
444 if (!got_selection
) {
445 clipOwner
->BeingReplaced();
450 wxClipboardClient
*wxClipboard::GetClipboardClient()
455 void wxClipboard::SetClipboardString(char *str
, long time
)
460 clipOwner
->BeingReplaced();
468 if (wxOpenClipboard()) {
469 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
470 got_selection
= FALSE
;
472 got_selection
= wxCloseClipboard();
474 got_selection
= FALSE
;
476 got_selection
= FALSE
; // Assume another process takes over
478 if (!got_selection
) {
484 char *wxClipboard::GetClipboardString(long time
)
489 str
= GetClipboardData("TEXT", &length
, time
);
498 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
501 if (clipOwner
->formats
.Member(format
))
502 return clipOwner
->GetData(format
, length
);
505 } else if (cbString
) {
506 if (!strcmp(format
, "TEXT"))
507 return copystring(cbString
);
511 if (wxOpenClipboard()) {
512 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
516 receivedString
= NULL
;
518 return receivedString
;
523 #endif // wxUSE_CLIPBOARD