]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/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 #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 WXUNUSED(width
), int WXUNUSED(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 *WXUNUSED(formatName
))
219 return (wxDataFormat
) wxDF_INVALID
;
222 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int WXUNUSED(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::wxClipboard()
245 wxClipboard::~wxClipboard()
250 void wxClipboard::Clear()
252 wxNode
* node
= m_data
.First();
255 wxDataObject
* data
= (wxDataObject
*) node
->Data();
262 bool wxClipboard::Open()
264 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
268 return wxOpenClipboard();
271 bool wxClipboard::SetData( wxDataObject
*data
)
273 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
274 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
278 return AddData( data
);
281 bool wxClipboard::AddData( wxDataObject
*data
)
283 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
284 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
286 wxDataFormat::NativeFormat format
= data
->GetPreferredFormat().GetType();
292 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
293 wxString
str(textDataObject
->GetText());
294 return wxSetClipboardData(format
, (wxObject
*) (const char*) str
);
300 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
301 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
302 return wxSetClipboardData(data
->GetType(), & bitmap
);
311 void wxClipboard::Close()
313 wxCHECK_RET( m_open
, "clipboard not open" );
319 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
321 return wxIsClipboardFormatAvailable(format
);
324 bool wxClipboard::GetData( wxDataObject
& data
)
326 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
328 wxDataFormat::NativeFormat format
= data
.GetPreferredFormat().GetType();
334 wxTextDataObject
& textDataObject
= (wxTextDataObject
&) data
;
335 char* s
= (char*) wxGetClipboardData(format
);
338 textDataObject
.SetText(s
);
350 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
351 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetType());
354 bitmapDataObject->SetBitmap(* bitmap);
374 * Old clipboard implementation by Matthew Flatt
377 wxClipboard
*wxTheClipboard
= NULL
;
379 void wxInitClipboard()
382 wxTheClipboard
= new wxClipboard
;
385 wxClipboard::wxClipboard()
391 wxClipboard::~wxClipboard()
394 clipOwner
->BeingReplaced();
399 static int FormatStringToID(char *str
)
401 if (!strcmp(str
, "TEXT"))
404 return wxRegisterClipboardFormat(str
);
407 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
412 clipOwner
->BeingReplaced();
419 if (wxOpenClipboard()) {
420 char **formats
, *data
;
425 formats
= clipOwner
->formats
.ListToArray(FALSE
);
426 for (i
= clipOwner
->formats
.Number(); i
--; ) {
427 ftype
= FormatStringToID(formats
[i
]);
428 data
= clipOwner
->GetData(formats
[i
], &size
);
429 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
430 got_selection
= FALSE
;
436 got_selection
= wxCloseClipboard();
438 got_selection
= FALSE
;
440 got_selection
= FALSE
; // Assume another process takes over
442 if (!got_selection
) {
443 clipOwner
->BeingReplaced();
448 wxClipboardClient
*wxClipboard::GetClipboardClient()
453 void wxClipboard::SetClipboardString(char *str
, long time
)
458 clipOwner
->BeingReplaced();
466 if (wxOpenClipboard()) {
467 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
468 got_selection
= FALSE
;
470 got_selection
= wxCloseClipboard();
472 got_selection
= FALSE
;
474 got_selection
= FALSE
; // Assume another process takes over
476 if (!got_selection
) {
482 char *wxClipboard::GetClipboardString(long time
)
487 str
= GetClipboardData("TEXT", &length
, time
);
496 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
499 if (clipOwner
->formats
.Member(format
))
500 return clipOwner
->GetData(format
, length
);
503 } else if (cbString
) {
504 if (!strcmp(format
, "TEXT"))
505 return copystring(cbString
);
509 if (wxOpenClipboard()) {
510 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
514 receivedString
= NULL
;
516 return receivedString
;
521 #endif // wxUSE_CLIPBOARD