]>
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"
29 #include "wx/listimpl.cpp"
30 WX_DEFINE_LIST(wxDataObjectList
);
33 #pragma message disable nosimpint
36 #include <Xm/CutPaste.h>
38 #pragma message enable nosimpint
43 // IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
44 // IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
46 static bool gs_clipboardIsOpen
= FALSE
;
48 bool wxOpenClipboard()
50 if (!gs_clipboardIsOpen
)
52 gs_clipboardIsOpen
= TRUE
;
59 bool wxCloseClipboard()
61 if (gs_clipboardIsOpen
)
63 gs_clipboardIsOpen
= FALSE
;
70 bool wxEmptyClipboard()
72 // No equivalent in Motif
76 bool wxClipboardOpen()
78 return gs_clipboardIsOpen
;
81 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()->GetTopWidget() );
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
)
105 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int WXUNUSED(width
), int WXUNUSED(height
))
107 if (dataFormat
!= wxDF_TEXT
)
110 char* data
= (char*) obj
;
112 XmString text
= XmStringCreateSimple ("CLIPBOARD");
113 Window window
= (Window
) 0;
114 if (wxTheApp
->GetTopWindow())
115 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
121 XmClipboardStartCopy((Display
*) wxGetDisplay(),
124 XtLastTimestampProcessed((Display
*) wxGetDisplay()),
127 & itemId
)) != ClipboardSuccess
)
135 XmClipboardCopy((Display
*) wxGetDisplay(),
142 & dataId
)) != ClipboardSuccess
)
147 XmClipboardEndCopy((Display
*) wxGetDisplay(),
148 window
, itemId
) ) != ClipboardSuccess
)
155 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
157 if (dataFormat
!= wxDF_TEXT
)
158 return (wxObject
*) NULL
;
162 unsigned long numBytes
= 0;
164 Window window
= (Window
) 0;
165 if (wxTheApp
->GetTopWindow())
166 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
168 int currentDataSize
= 256;
169 char* data
= new char[currentDataSize
];
173 if (result
== ClipboardTruncate
)
176 currentDataSize
= 2*currentDataSize
;
177 data
= new char[currentDataSize
];
179 result
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
189 case ClipboardSuccess
:
192 *len
= strlen(data
) + 1;
193 return (wxObject
*) data
;
196 case ClipboardTruncate
:
197 case ClipboardLocked
:
202 case ClipboardNoData
:
204 return (wxObject
*) NULL
;
214 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
216 // Only wxDF_TEXT supported
217 if (dataFormat
== wxDF_TEXT
)
223 wxDataFormat
wxRegisterClipboardFormat(char *WXUNUSED(formatName
))
226 return (wxDataFormat
) wxDF_INVALID
;
229 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int WXUNUSED(maxCount
))
231 // Only wxDF_TEXT supported
232 if (dataFormat
== wxDF_TEXT
)
234 strcpy(formatName
, "TEXT");
241 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
245 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
247 wxClipboard::wxClipboard()
252 wxClipboard::~wxClipboard()
257 void wxClipboard::Clear()
259 wxDataObjectList::Node
* node
= m_data
.GetFirst();
262 wxDataObject
* data
= node
->GetData();
264 node
= node
->GetNext();
269 bool wxClipboard::Open()
271 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
275 return wxOpenClipboard();
278 bool wxClipboard::SetData( wxDataObject
*data
)
280 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
281 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
285 return AddData( data
);
288 bool wxClipboard::AddData( wxDataObject
*data
)
290 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
291 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
293 wxDataFormat::NativeFormat format
= data
->GetPreferredFormat().GetType();
299 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
300 wxString
str(textDataObject
->GetText());
301 return wxSetClipboardData(format
, (wxObject
*) (const char*) str
);
307 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
308 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
309 return wxSetClipboardData(data
->GetType(), & bitmap
);
318 void wxClipboard::Close()
320 wxCHECK_RET( m_open
, "clipboard not open" );
326 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
328 return wxIsClipboardFormatAvailable(format
);
331 bool wxClipboard::GetData( wxDataObject
& data
)
333 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
335 wxDataFormat::NativeFormat format
= data
.GetPreferredFormat().GetType();
341 wxTextDataObject
& textDataObject
= (wxTextDataObject
&) data
;
342 char* s
= (char*) wxGetClipboardData(format
);
345 textDataObject
.SetText(s
);
357 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
358 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetType());
361 bitmapDataObject->SetBitmap(* bitmap);
373 // VMS complains that this statement is/causes unreachability
385 * Old clipboard implementation by Matthew Flatt
388 wxClipboard
*wxTheClipboard
= NULL
;
390 void wxInitClipboard()
393 wxTheClipboard
= new wxClipboard
;
396 wxClipboard::wxClipboard()
402 wxClipboard::~wxClipboard()
405 clipOwner
->BeingReplaced();
410 static int FormatStringToID(char *str
)
412 if (!strcmp(str
, "TEXT"))
415 return wxRegisterClipboardFormat(str
);
418 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
423 clipOwner
->BeingReplaced();
430 if (wxOpenClipboard()) {
431 char **formats
, *data
;
436 formats
= clipOwner
->formats
.ListToArray(FALSE
);
437 for (i
= clipOwner
->formats
.Number(); i
--; ) {
438 ftype
= FormatStringToID(formats
[i
]);
439 data
= clipOwner
->GetData(formats
[i
], &size
);
440 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
441 got_selection
= FALSE
;
447 got_selection
= wxCloseClipboard();
449 got_selection
= FALSE
;
451 got_selection
= FALSE
; // Assume another process takes over
453 if (!got_selection
) {
454 clipOwner
->BeingReplaced();
459 wxClipboardClient
*wxClipboard::GetClipboardClient()
464 void wxClipboard::SetClipboardString(char *str
, long time
)
469 clipOwner
->BeingReplaced();
477 if (wxOpenClipboard()) {
478 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
479 got_selection
= FALSE
;
481 got_selection
= wxCloseClipboard();
483 got_selection
= FALSE
;
485 got_selection
= FALSE
; // Assume another process takes over
487 if (!got_selection
) {
493 char *wxClipboard::GetClipboardString(long time
)
498 str
= GetClipboardData("TEXT", &length
, time
);
507 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
510 if (clipOwner
->formats
.Member(format
))
511 return clipOwner
->GetData(format
, length
);
514 } else if (cbString
) {
515 if (!strcmp(format
, "TEXT"))
516 return copystring(cbString
);
520 if (wxOpenClipboard()) {
521 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
525 receivedString
= NULL
;
527 return receivedString
;
532 #endif // wxUSE_CLIPBOARD