]>
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);
366 //-----------------------------------------------------------------------------
368 //-----------------------------------------------------------------------------
370 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
372 bool wxClipboardModule::OnInit()
374 wxTheClipboard
= new wxClipboard();
379 void wxClipboardModule::OnExit()
381 if (wxTheClipboard
) delete wxTheClipboard
;
382 wxTheClipboard
= (wxClipboard
*) NULL
;
389 * Old clipboard implementation by Matthew Flatt
392 wxClipboard
*wxTheClipboard
= NULL
;
394 void wxInitClipboard()
397 wxTheClipboard
= new wxClipboard
;
400 wxClipboard::wxClipboard()
406 wxClipboard::~wxClipboard()
409 clipOwner
->BeingReplaced();
414 static int FormatStringToID(char *str
)
416 if (!strcmp(str
, "TEXT"))
419 return wxRegisterClipboardFormat(str
);
422 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
427 clipOwner
->BeingReplaced();
434 if (wxOpenClipboard()) {
435 char **formats
, *data
;
440 formats
= clipOwner
->formats
.ListToArray(FALSE
);
441 for (i
= clipOwner
->formats
.Number(); i
--; ) {
442 ftype
= FormatStringToID(formats
[i
]);
443 data
= clipOwner
->GetData(formats
[i
], &size
);
444 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
445 got_selection
= FALSE
;
451 got_selection
= wxCloseClipboard();
453 got_selection
= FALSE
;
455 got_selection
= FALSE
; // Assume another process takes over
457 if (!got_selection
) {
458 clipOwner
->BeingReplaced();
463 wxClipboardClient
*wxClipboard::GetClipboardClient()
468 void wxClipboard::SetClipboardString(char *str
, long time
)
473 clipOwner
->BeingReplaced();
481 if (wxOpenClipboard()) {
482 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
483 got_selection
= FALSE
;
485 got_selection
= wxCloseClipboard();
487 got_selection
= FALSE
;
489 got_selection
= FALSE
; // Assume another process takes over
491 if (!got_selection
) {
497 char *wxClipboard::GetClipboardString(long time
)
502 str
= GetClipboardData("TEXT", &length
, time
);
511 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
514 if (clipOwner
->formats
.Member(format
))
515 return clipOwner
->GetData(format
, length
);
518 } else if (cbString
) {
519 if (!strcmp(format
, "TEXT"))
520 return copystring(cbString
);
524 if (wxOpenClipboard()) {
525 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
529 receivedString
= NULL
;
531 return receivedString
;
536 #endif // wxUSE_CLIPBOARD