]>
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 #pragma message disable nosimpint
33 #include <Xm/CutPaste.h>
35 #pragma message enable nosimpint
40 #if !USE_SHARED_LIBRARY
41 // IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
42 // IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
45 static bool gs_clipboardIsOpen
= FALSE
;
47 bool wxOpenClipboard()
49 if (!gs_clipboardIsOpen
)
51 gs_clipboardIsOpen
= TRUE
;
58 bool wxCloseClipboard()
60 if (gs_clipboardIsOpen
)
62 gs_clipboardIsOpen
= FALSE
;
69 bool wxEmptyClipboard()
71 // No equivalent in Motif
75 bool wxClipboardOpen()
77 return gs_clipboardIsOpen
;
80 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
82 // Only text is supported.
83 if (dataFormat
!= wxDF_TEXT
)
86 unsigned long numBytes
= 0;
89 Window window
= (Window
) 0;
90 if (wxTheApp
->GetTopWindow())
91 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
93 int success
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
94 window
, "TEXT", (XtPointer
) 0, 0, & numBytes
, & privateId
) ;
96 // Assume only text is supported. If we have anything at all,
97 // or the clipboard is locked so we're not sure, we say we support it.
98 if (success
== ClipboardNoData
)
104 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int WXUNUSED(width
), int WXUNUSED(height
))
106 if (dataFormat
!= wxDF_TEXT
)
109 char* data
= (char*) obj
;
111 XmString text
= XmStringCreateSimple ("CLIPBOARD");
112 Window window
= (Window
) 0;
113 if (wxTheApp
->GetTopWindow())
114 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
120 XmClipboardStartCopy((Display
*) wxGetDisplay(),
123 XtLastTimestampProcessed((Display
*) wxGetDisplay()),
126 & itemId
)) != ClipboardSuccess
)
134 XmClipboardCopy((Display
*) wxGetDisplay(),
141 & dataId
)) != ClipboardSuccess
)
146 XmClipboardEndCopy((Display
*) wxGetDisplay(),
147 window
, itemId
) ) != ClipboardSuccess
)
154 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
156 if (dataFormat
!= wxDF_TEXT
)
157 return (wxObject
*) NULL
;
161 unsigned long numBytes
= 0;
163 Window window
= (Window
) 0;
164 if (wxTheApp
->GetTopWindow())
165 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
167 int currentDataSize
= 256;
168 char* data
= new char[currentDataSize
];
172 if (result
== ClipboardTruncate
)
175 currentDataSize
= 2*currentDataSize
;
176 data
= new char[currentDataSize
];
178 result
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
188 case ClipboardSuccess
:
191 *len
= strlen(data
) + 1;
192 return (wxObject
*) data
;
195 case ClipboardTruncate
:
196 case ClipboardLocked
:
201 case ClipboardNoData
:
203 return (wxObject
*) NULL
;
213 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
215 // Only wxDF_TEXT supported
216 if (dataFormat
== wxDF_TEXT
)
222 wxDataFormat
wxRegisterClipboardFormat(char *WXUNUSED(formatName
))
225 return (wxDataFormat
) wxDF_INVALID
;
228 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int WXUNUSED(maxCount
))
230 // Only wxDF_TEXT supported
231 if (dataFormat
== wxDF_TEXT
)
233 strcpy(formatName
, "TEXT");
240 //-----------------------------------------------------------------------------
242 //-----------------------------------------------------------------------------
244 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
246 wxClipboard::wxClipboard()
251 wxClipboard::~wxClipboard()
256 void wxClipboard::Clear()
258 wxNode
* node
= m_data
.First();
261 wxDataObject
* data
= (wxDataObject
*) node
->Data();
268 bool wxClipboard::Open()
270 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
274 return wxOpenClipboard();
277 bool wxClipboard::SetData( wxDataObject
*data
)
279 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
280 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
284 return AddData( data
);
287 bool wxClipboard::AddData( wxDataObject
*data
)
289 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
290 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
292 wxDataFormat::NativeFormat format
= data
->GetPreferredFormat().GetType();
298 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
299 wxString
str(textDataObject
->GetText());
300 return wxSetClipboardData(format
, (wxObject
*) (const char*) str
);
306 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
307 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
308 return wxSetClipboardData(data
->GetType(), & bitmap
);
317 void wxClipboard::Close()
319 wxCHECK_RET( m_open
, "clipboard not open" );
325 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
327 return wxIsClipboardFormatAvailable(format
);
330 bool wxClipboard::GetData( wxDataObject
& data
)
332 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
334 wxDataFormat::NativeFormat format
= data
.GetPreferredFormat().GetType();
340 wxTextDataObject
& textDataObject
= (wxTextDataObject
&) data
;
341 char* s
= (char*) wxGetClipboardData(format
);
344 textDataObject
.SetText(s
);
356 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
357 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetType());
360 bitmapDataObject->SetBitmap(* bitmap);
380 * Old clipboard implementation by Matthew Flatt
383 wxClipboard
*wxTheClipboard
= NULL
;
385 void wxInitClipboard()
388 wxTheClipboard
= new wxClipboard
;
391 wxClipboard::wxClipboard()
397 wxClipboard::~wxClipboard()
400 clipOwner
->BeingReplaced();
405 static int FormatStringToID(char *str
)
407 if (!strcmp(str
, "TEXT"))
410 return wxRegisterClipboardFormat(str
);
413 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
418 clipOwner
->BeingReplaced();
425 if (wxOpenClipboard()) {
426 char **formats
, *data
;
431 formats
= clipOwner
->formats
.ListToArray(FALSE
);
432 for (i
= clipOwner
->formats
.Number(); i
--; ) {
433 ftype
= FormatStringToID(formats
[i
]);
434 data
= clipOwner
->GetData(formats
[i
], &size
);
435 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
436 got_selection
= FALSE
;
442 got_selection
= wxCloseClipboard();
444 got_selection
= FALSE
;
446 got_selection
= FALSE
; // Assume another process takes over
448 if (!got_selection
) {
449 clipOwner
->BeingReplaced();
454 wxClipboardClient
*wxClipboard::GetClipboardClient()
459 void wxClipboard::SetClipboardString(char *str
, long time
)
464 clipOwner
->BeingReplaced();
472 if (wxOpenClipboard()) {
473 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
474 got_selection
= FALSE
;
476 got_selection
= wxCloseClipboard();
478 got_selection
= FALSE
;
480 got_selection
= FALSE
; // Assume another process takes over
482 if (!got_selection
) {
488 char *wxClipboard::GetClipboardString(long time
)
493 str
= GetClipboardData("TEXT", &length
, time
);
502 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
505 if (clipOwner
->formats
.Member(format
))
506 return clipOwner
->GetData(format
, length
);
509 } else if (cbString
) {
510 if (!strcmp(format
, "TEXT"))
511 return copystring(cbString
);
515 if (wxOpenClipboard()) {
516 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
520 receivedString
= NULL
;
522 return receivedString
;
527 #endif // wxUSE_CLIPBOARD