]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/clipbrd.cpp
41afb324d308b393539aa8c0419f494b445858d6
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 // IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
41 // IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
43 static bool gs_clipboardIsOpen
= FALSE
;
45 bool wxOpenClipboard()
47 if (!gs_clipboardIsOpen
)
49 gs_clipboardIsOpen
= TRUE
;
56 bool wxCloseClipboard()
58 if (gs_clipboardIsOpen
)
60 gs_clipboardIsOpen
= FALSE
;
67 bool wxEmptyClipboard()
69 // No equivalent in Motif
73 bool wxClipboardOpen()
75 return gs_clipboardIsOpen
;
78 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
80 // Only text is supported.
81 if (dataFormat
!= wxDF_TEXT
)
84 unsigned long numBytes
= 0;
87 Window window
= (Window
) 0;
88 if (wxTheApp
->GetTopWindow())
89 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
91 int success
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
92 window
, "TEXT", (XtPointer
) 0, 0, & numBytes
, & privateId
) ;
94 // Assume only text is supported. If we have anything at all,
95 // or the clipboard is locked so we're not sure, we say we support it.
96 if (success
== ClipboardNoData
)
102 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int WXUNUSED(width
), int WXUNUSED(height
))
104 if (dataFormat
!= wxDF_TEXT
)
107 char* data
= (char*) obj
;
109 XmString text
= XmStringCreateSimple ("CLIPBOARD");
110 Window window
= (Window
) 0;
111 if (wxTheApp
->GetTopWindow())
112 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
118 XmClipboardStartCopy((Display
*) wxGetDisplay(),
121 XtLastTimestampProcessed((Display
*) wxGetDisplay()),
124 & itemId
)) != ClipboardSuccess
)
132 XmClipboardCopy((Display
*) wxGetDisplay(),
139 & dataId
)) != ClipboardSuccess
)
144 XmClipboardEndCopy((Display
*) wxGetDisplay(),
145 window
, itemId
) ) != ClipboardSuccess
)
152 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
154 if (dataFormat
!= wxDF_TEXT
)
155 return (wxObject
*) NULL
;
159 unsigned long numBytes
= 0;
161 Window window
= (Window
) 0;
162 if (wxTheApp
->GetTopWindow())
163 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
165 int currentDataSize
= 256;
166 char* data
= new char[currentDataSize
];
170 if (result
== ClipboardTruncate
)
173 currentDataSize
= 2*currentDataSize
;
174 data
= new char[currentDataSize
];
176 result
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
186 case ClipboardSuccess
:
189 *len
= strlen(data
) + 1;
190 return (wxObject
*) data
;
193 case ClipboardTruncate
:
194 case ClipboardLocked
:
199 case ClipboardNoData
:
201 return (wxObject
*) NULL
;
211 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
213 // Only wxDF_TEXT supported
214 if (dataFormat
== wxDF_TEXT
)
220 wxDataFormat
wxRegisterClipboardFormat(char *WXUNUSED(formatName
))
223 return (wxDataFormat
) wxDF_INVALID
;
226 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int WXUNUSED(maxCount
))
228 // Only wxDF_TEXT supported
229 if (dataFormat
== wxDF_TEXT
)
231 strcpy(formatName
, "TEXT");
238 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------
242 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
244 wxClipboard::wxClipboard()
249 wxClipboard::~wxClipboard()
254 void wxClipboard::Clear()
256 wxNode
* node
= m_data
.First();
259 wxDataObject
* data
= (wxDataObject
*) node
->Data();
266 bool wxClipboard::Open()
268 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
272 return wxOpenClipboard();
275 bool wxClipboard::SetData( wxDataObject
*data
)
277 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
278 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
282 return AddData( data
);
285 bool wxClipboard::AddData( wxDataObject
*data
)
287 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
288 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
290 wxDataFormat::NativeFormat format
= data
->GetPreferredFormat().GetType();
296 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
297 wxString
str(textDataObject
->GetText());
298 return wxSetClipboardData(format
, (wxObject
*) (const char*) str
);
304 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
305 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
306 return wxSetClipboardData(data
->GetType(), & bitmap
);
315 void wxClipboard::Close()
317 wxCHECK_RET( m_open
, "clipboard not open" );
323 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
325 return wxIsClipboardFormatAvailable(format
);
328 bool wxClipboard::GetData( wxDataObject
& data
)
330 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
332 wxDataFormat::NativeFormat format
= data
.GetPreferredFormat().GetType();
338 wxTextDataObject
& textDataObject
= (wxTextDataObject
&) data
;
339 char* s
= (char*) wxGetClipboardData(format
);
342 textDataObject
.SetText(s
);
354 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
355 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetType());
358 bitmapDataObject->SetBitmap(* bitmap);
378 * Old clipboard implementation by Matthew Flatt
381 wxClipboard
*wxTheClipboard
= NULL
;
383 void wxInitClipboard()
386 wxTheClipboard
= new wxClipboard
;
389 wxClipboard::wxClipboard()
395 wxClipboard::~wxClipboard()
398 clipOwner
->BeingReplaced();
403 static int FormatStringToID(char *str
)
405 if (!strcmp(str
, "TEXT"))
408 return wxRegisterClipboardFormat(str
);
411 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
416 clipOwner
->BeingReplaced();
423 if (wxOpenClipboard()) {
424 char **formats
, *data
;
429 formats
= clipOwner
->formats
.ListToArray(FALSE
);
430 for (i
= clipOwner
->formats
.Number(); i
--; ) {
431 ftype
= FormatStringToID(formats
[i
]);
432 data
= clipOwner
->GetData(formats
[i
], &size
);
433 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
434 got_selection
= FALSE
;
440 got_selection
= wxCloseClipboard();
442 got_selection
= FALSE
;
444 got_selection
= FALSE
; // Assume another process takes over
446 if (!got_selection
) {
447 clipOwner
->BeingReplaced();
452 wxClipboardClient
*wxClipboard::GetClipboardClient()
457 void wxClipboard::SetClipboardString(char *str
, long time
)
462 clipOwner
->BeingReplaced();
470 if (wxOpenClipboard()) {
471 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
472 got_selection
= FALSE
;
474 got_selection
= wxCloseClipboard();
476 got_selection
= FALSE
;
478 got_selection
= FALSE
; // Assume another process takes over
480 if (!got_selection
) {
486 char *wxClipboard::GetClipboardString(long time
)
491 str
= GetClipboardData("TEXT", &length
, time
);
500 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
503 if (clipOwner
->formats
.Member(format
))
504 return clipOwner
->GetData(format
, length
);
507 } else if (cbString
) {
508 if (!strcmp(format
, "TEXT"))
509 return copystring(cbString
);
513 if (wxOpenClipboard()) {
514 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
518 receivedString
= NULL
;
520 return receivedString
;
525 #endif // wxUSE_CLIPBOARD