]>
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"
19 #include "wx/bitmap.h"
21 #include "wx/metafile.h"
22 #include "wx/clipbrd.h"
23 #include "wx/dataobj.h"
26 #include <Xm/CutPaste.h>
30 #if !USE_SHARED_LIBRARY
31 // IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
32 // IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
35 static bool gs_clipboardIsOpen
= FALSE
;
37 bool wxOpenClipboard()
39 if (!gs_clipboardIsOpen
)
41 gs_clipboardIsOpen
= TRUE
;
48 bool wxCloseClipboard()
50 if (gs_clipboardIsOpen
)
52 gs_clipboardIsOpen
= FALSE
;
59 bool wxEmptyClipboard()
61 // No equivalent in Motif
65 bool wxClipboardOpen()
67 return gs_clipboardIsOpen
;
70 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
72 // Only text is supported.
73 if (dataFormat
!= wxDF_TEXT
)
76 unsigned long numBytes
= 0;
79 Window window
= (Window
) 0;
80 if (wxTheApp
->GetTopWindow())
81 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
83 int success
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
84 window
, "TEXT", (XtPointer
) 0, 0, & numBytes
, & privateId
) ;
86 // Assume only text is supported. If we have anything at all,
87 // or the clipboard is locked so we're not sure, we say we support it.
88 if (success
== ClipboardNoData
)
94 bool wxSetClipboardData(wxDataFormat dataFormat
, wxObject
*obj
, int width
, int height
)
96 if (dataFormat
!= wxDF_TEXT
)
99 char* data
= (char*) obj
;
101 XmString text
= XmStringCreateSimple ("CLIPBOARD");
102 Window window
= (Window
) 0;
103 if (wxTheApp
->GetTopWindow())
104 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
110 XmClipboardStartCopy((Display
*) wxGetDisplay(),
113 XtLastTimestampProcessed((Display
*) wxGetDisplay()),
116 & itemId
)) != ClipboardSuccess
)
124 XmClipboardCopy((Display
*) wxGetDisplay(),
131 & dataId
)) != ClipboardSuccess
)
136 XmClipboardEndCopy((Display
*) wxGetDisplay(),
137 window
, itemId
) ) != ClipboardSuccess
)
144 wxObject
*wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
146 if (dataFormat
!= wxDF_TEXT
)
147 return (wxObject
*) NULL
;
151 unsigned long numBytes
= 0;
153 Window window
= (Window
) 0;
154 if (wxTheApp
->GetTopWindow())
155 window
= XtWindow( (Widget
) wxTheApp
->GetTopWindow()->GetTopWidget() );
157 int currentDataSize
= 256;
158 char* data
= new char[currentDataSize
];
162 if (result
== ClipboardTruncate
)
165 currentDataSize
= 2*currentDataSize
;
166 data
= new char[currentDataSize
];
168 result
= XmClipboardRetrieve((Display
*) wxGetDisplay(),
178 case ClipboardSuccess
:
181 *len
= strlen(data
) + 1;
182 return (wxObject
*) data
;
185 case ClipboardTruncate
:
186 case ClipboardLocked
:
191 case ClipboardNoData
:
193 return (wxObject
*) NULL
;
203 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
205 // Only wxDF_TEXT supported
206 if (dataFormat
== wxDF_TEXT
)
212 wxDataFormat
wxRegisterClipboardFormat(char *formatName
)
215 return (wxDataFormat
) wxDF_INVALID
;
218 bool wxGetClipboardFormatName(wxDataFormat dataFormat
, char *formatName
, int maxCount
)
220 // Only wxDF_TEXT supported
221 if (dataFormat
== wxDF_TEXT
)
223 strcpy(formatName
, "TEXT");
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
236 wxClipboard
* wxTheClipboard
= (wxClipboard
*) NULL
;
238 wxClipboard::wxClipboard()
243 wxClipboard::~wxClipboard()
248 void wxClipboard::Clear()
250 wxNode
* node
= m_data
.First();
253 wxDataObject
* data
= (wxDataObject
*) node
->Data();
260 bool wxClipboard::Open()
262 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
266 return wxOpenClipboard();
269 bool wxClipboard::SetData( wxDataObject
*data
)
271 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
272 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
274 switch (data
->GetFormat())
279 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
280 wxString
str(textDataObject
->GetText());
281 return wxSetClipboardData(data
->GetFormat(), (wxObject
*) (const char*) str
);
288 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
289 wxBitmap bitmap(bitmapDataObject->GetBitmap());
290 return wxSetClipboardData(data->GetFormat(), & bitmap);
303 void wxClipboard::Close()
305 wxCHECK_RET( m_open
, "clipboard not open" );
311 bool wxClipboard::IsSupported( wxDataFormat format
)
313 return wxIsClipboardFormatAvailable(format
);
316 bool wxClipboard::GetData( wxDataObject
*data
)
318 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
320 switch (data
->GetFormat())
325 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
326 char* s
= (char*) wxGetClipboardData(data
->GetFormat());
329 textDataObject
->SetText(s
);
341 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
342 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetFormat());
345 bitmapDataObject->SetBitmap(* bitmap);
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
366 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
368 bool wxClipboardModule::OnInit()
370 wxTheClipboard
= new wxClipboard();
375 void wxClipboardModule::OnExit()
377 if (wxTheClipboard
) delete wxTheClipboard
;
378 wxTheClipboard
= (wxClipboard
*) NULL
;
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
;