]>
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 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" );
278 switch (data
->GetFormat())
283 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
284 wxString
str(textDataObject
->GetText());
285 return wxSetClipboardData(data
->GetFormat(), (wxObject
*) (const char*) str
);
292 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
293 wxBitmap bitmap(bitmapDataObject->GetBitmap());
294 return wxSetClipboardData(data->GetFormat(), & bitmap);
307 void wxClipboard::Close()
309 wxCHECK_RET( m_open
, "clipboard not open" );
315 bool wxClipboard::IsSupported( wxDataFormat format
)
317 return wxIsClipboardFormatAvailable(format
);
320 bool wxClipboard::GetData( wxDataObject
*data
)
322 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
324 switch (data
->GetFormat())
329 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
330 char* s
= (char*) wxGetClipboardData(data
->GetFormat());
333 textDataObject
->SetText(s
);
345 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
346 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetFormat());
349 bitmapDataObject->SetBitmap(* bitmap);
369 * Old clipboard implementation by Matthew Flatt
372 wxClipboard
*wxTheClipboard
= NULL
;
374 void wxInitClipboard()
377 wxTheClipboard
= new wxClipboard
;
380 wxClipboard::wxClipboard()
386 wxClipboard::~wxClipboard()
389 clipOwner
->BeingReplaced();
394 static int FormatStringToID(char *str
)
396 if (!strcmp(str
, "TEXT"))
399 return wxRegisterClipboardFormat(str
);
402 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
407 clipOwner
->BeingReplaced();
414 if (wxOpenClipboard()) {
415 char **formats
, *data
;
420 formats
= clipOwner
->formats
.ListToArray(FALSE
);
421 for (i
= clipOwner
->formats
.Number(); i
--; ) {
422 ftype
= FormatStringToID(formats
[i
]);
423 data
= clipOwner
->GetData(formats
[i
], &size
);
424 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
425 got_selection
= FALSE
;
431 got_selection
= wxCloseClipboard();
433 got_selection
= FALSE
;
435 got_selection
= FALSE
; // Assume another process takes over
437 if (!got_selection
) {
438 clipOwner
->BeingReplaced();
443 wxClipboardClient
*wxClipboard::GetClipboardClient()
448 void wxClipboard::SetClipboardString(char *str
, long time
)
453 clipOwner
->BeingReplaced();
461 if (wxOpenClipboard()) {
462 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
463 got_selection
= FALSE
;
465 got_selection
= wxCloseClipboard();
467 got_selection
= FALSE
;
469 got_selection
= FALSE
; // Assume another process takes over
471 if (!got_selection
) {
477 char *wxClipboard::GetClipboardString(long time
)
482 str
= GetClipboardData("TEXT", &length
, time
);
491 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
494 if (clipOwner
->formats
.Member(format
))
495 return clipOwner
->GetData(format
, length
);
498 } else if (cbString
) {
499 if (!strcmp(format
, "TEXT"))
500 return copystring(cbString
);
504 if (wxOpenClipboard()) {
505 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
509 receivedString
= NULL
;
511 return receivedString
;
516 #endif // wxUSE_CLIPBOARD