]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/clipbrd.cpp
8156b69bc78fde38e1823e2031c84c3b13a469e7
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
== (wxDataFormat
) 0)
209 return (wxDataFormat
) 0;
212 wxDataFormat
wxRegisterClipboardFormat(char *formatName
)
215 return (wxDataFormat
) 0;
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
);
287 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
288 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
289 return wxSetClipboardData(data
->GetFormat(), & bitmap
);
301 void wxClipboard::Close()
303 wxCHECK_RET( m_open
, "clipboard not open" );
309 bool wxClipboard::IsSupportedFormat( wxDataFormat format
, const wxString
& WXUNUSED(id
) )
311 return wxIsClipboardFormatAvailable(format
);
314 bool wxClipboard::GetData( wxDataObject
*data
)
316 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
318 switch (data
->GetFormat())
323 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
324 char* s
= (char*) wxGetClipboardData(data
->GetFormat());
327 textDataObject
->SetText(s
);
338 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
339 wxBitmap
* bitmap
= (wxBitmap
*) wxGetClipboardData(data
->GetFormat());
342 bitmapDataObject
->SetBitmap(* bitmap
);
358 //-----------------------------------------------------------------------------
360 //-----------------------------------------------------------------------------
362 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
364 bool wxClipboardModule::OnInit()
366 wxTheClipboard
= new wxClipboard();
371 void wxClipboardModule::OnExit()
373 if (wxTheClipboard
) delete wxTheClipboard
;
374 wxTheClipboard
= (wxClipboard
*) NULL
;
381 * Old clipboard implementation by Matthew Flatt
384 wxClipboard
*wxTheClipboard
= NULL
;
386 void wxInitClipboard()
389 wxTheClipboard
= new wxClipboard
;
392 wxClipboard::wxClipboard()
398 wxClipboard::~wxClipboard()
401 clipOwner
->BeingReplaced();
406 static int FormatStringToID(char *str
)
408 if (!strcmp(str
, "TEXT"))
411 return wxRegisterClipboardFormat(str
);
414 void wxClipboard::SetClipboardClient(wxClipboardClient
*client
, long time
)
419 clipOwner
->BeingReplaced();
426 if (wxOpenClipboard()) {
427 char **formats
, *data
;
432 formats
= clipOwner
->formats
.ListToArray(FALSE
);
433 for (i
= clipOwner
->formats
.Number(); i
--; ) {
434 ftype
= FormatStringToID(formats
[i
]);
435 data
= clipOwner
->GetData(formats
[i
], &size
);
436 if (!wxSetClipboardData(ftype
, (wxObject
*)data
, size
, 1)) {
437 got_selection
= FALSE
;
443 got_selection
= wxCloseClipboard();
445 got_selection
= FALSE
;
447 got_selection
= FALSE
; // Assume another process takes over
449 if (!got_selection
) {
450 clipOwner
->BeingReplaced();
455 wxClipboardClient
*wxClipboard::GetClipboardClient()
460 void wxClipboard::SetClipboardString(char *str
, long time
)
465 clipOwner
->BeingReplaced();
473 if (wxOpenClipboard()) {
474 if (!wxSetClipboardData(wxDF_TEXT
, (wxObject
*)str
))
475 got_selection
= FALSE
;
477 got_selection
= wxCloseClipboard();
479 got_selection
= FALSE
;
481 got_selection
= FALSE
; // Assume another process takes over
483 if (!got_selection
) {
489 char *wxClipboard::GetClipboardString(long time
)
494 str
= GetClipboardData("TEXT", &length
, time
);
503 char *wxClipboard::GetClipboardData(char *format
, long *length
, long time
)
506 if (clipOwner
->formats
.Member(format
))
507 return clipOwner
->GetData(format
, length
);
510 } else if (cbString
) {
511 if (!strcmp(format
, "TEXT"))
512 return copystring(cbString
);
516 if (wxOpenClipboard()) {
517 receivedString
= (char *)wxGetClipboardData(FormatStringToID(format
),
521 receivedString
= NULL
;
523 return receivedString
;