]>
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 // 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);
370 // VMS complains that this statement is/causes unreachability
382 * Old clipboard implementation by Matthew Flatt
385 wxClipboard
*wxTheClipboard
= NULL
;
387 void wxInitClipboard()
390 wxTheClipboard
= new wxClipboard
;
393 wxClipboard::wxClipboard()
399 wxClipboard::~wxClipboard()
402 clipOwner
->BeingReplaced();
407 static int FormatStringToID(char *str
)
409 if (!strcmp(str
, "TEXT"))
412 return wxRegisterClipboardFormat(str
);
415 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
420 clipOwner
->BeingReplaced();
427 if (wxOpenClipboard()) {
428 char **formats
, *data
;
433 formats
= clipOwner
->formats
.ListToArray(FALSE
);
434 for (i
= clipOwner
->formats
.Number(); i
--; ) {
435 ftype
= FormatStringToID(formats
[i
]);
436 data
= clipOwner
->GetData(formats
[i
], &size
);
437 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
438 got_selection
= FALSE
;
444 got_selection
= wxCloseClipboard();
446 got_selection
= FALSE
;
448 got_selection
= FALSE
; // Assume another process takes over
450 if (!got_selection
) {
451 clipOwner
->BeingReplaced();
456 wxClipboardClient
*wxClipboard::GetClipboardClient()
461 void wxClipboard::SetClipboardString(char *str
, long time
)
466 clipOwner
->BeingReplaced();
474 if (wxOpenClipboard()) {
475 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
476 got_selection
= FALSE
;
478 got_selection
= wxCloseClipboard();
480 got_selection
= FALSE
;
482 got_selection
= FALSE
; // Assume another process takes over
484 if (!got_selection
) {
490 char *wxClipboard::GetClipboardString(long time
)
495 str
= GetClipboardData("TEXT", &length
, time
);
504 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
507 if (clipOwner
->formats
.Member(format
))
508 return clipOwner
->GetData(format
, length
);
511 } else if (cbString
) {
512 if (!strcmp(format
, "TEXT"))
513 return copystring(cbString
);
517 if (wxOpenClipboard()) {
518 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
522 receivedString
= NULL
;
524 return receivedString
;
529 #endif // wxUSE_CLIPBOARD