]> git.saurik.com Git - wxWidgets.git/blame - src/motif/clipbrd.cpp
and the compilation fix which really compiles, finally
[wxWidgets.git] / src / motif / clipbrd.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: clipbrd.cpp
3// Purpose: Clipboard functionality
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#pragma implementation "clipbrd.h"
15#endif
16
dfe1eee3
VZ
17#include "wx/defs.h"
18
19#if wxUSE_CLIPBOARD
20
4bb6408c
JS
21#include "wx/app.h"
22#include "wx/frame.h"
23#include "wx/bitmap.h"
24#include "wx/utils.h"
25#include "wx/metafile.h"
26#include "wx/clipbrd.h"
2d120f83
JS
27#include "wx/dataobj.h"
28
29#include <Xm/Xm.h>
30#include <Xm/CutPaste.h>
4bb6408c
JS
31
32#include <string.h>
33
34#if !USE_SHARED_LIBRARY
2d120f83
JS
35// IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
36// IMPLEMENT_ABSTRACT_CLASS(wxClipboardClient, wxObject)
4bb6408c
JS
37#endif
38
2d120f83
JS
39static bool gs_clipboardIsOpen = FALSE;
40
4bb6408c
JS
41bool wxOpenClipboard()
42{
2d120f83
JS
43 if (!gs_clipboardIsOpen)
44 {
45 gs_clipboardIsOpen = TRUE;
46 return TRUE;
47 }
48 else
49 return FALSE;
4bb6408c
JS
50}
51
52bool wxCloseClipboard()
53{
2d120f83
JS
54 if (gs_clipboardIsOpen)
55 {
56 gs_clipboardIsOpen = FALSE;
57 return TRUE;
58 }
59 else
60 return FALSE;
4bb6408c
JS
61}
62
63bool wxEmptyClipboard()
64{
2d120f83
JS
65 // No equivalent in Motif
66 return TRUE;
4bb6408c
JS
67}
68
69bool wxClipboardOpen()
70{
2d120f83 71 return gs_clipboardIsOpen;
4bb6408c
JS
72}
73
2d120f83 74bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
4bb6408c 75{
2d120f83
JS
76 // Only text is supported.
77 if (dataFormat != wxDF_TEXT)
78 return FALSE;
79
80 unsigned long numBytes = 0;
81 long privateId = 0;
82
83 Window window = (Window) 0;
84 if (wxTheApp->GetTopWindow())
85 window = XtWindow( (Widget) wxTheApp->GetTopWindow()->GetTopWidget() );
86
87 int success = XmClipboardRetrieve((Display*) wxGetDisplay(),
9b64e798 88 window, "TEXT", (XtPointer) 0, 0, & numBytes, & privateId) ;
2d120f83
JS
89
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)
93 return FALSE;
94 else
95 return TRUE;
4bb6408c
JS
96}
97
2d120f83 98bool wxSetClipboardData(wxDataFormat dataFormat, wxObject *obj, int width, int height)
4bb6408c 99{
2d120f83
JS
100 if (dataFormat != wxDF_TEXT)
101 return FALSE;
102
103 char* data = (char*) obj;
104
105 XmString text = XmStringCreateSimple ("CLIPBOARD");
106 Window window = (Window) 0;
107 if (wxTheApp->GetTopWindow())
108 window = XtWindow( (Widget) wxTheApp->GetTopWindow()->GetTopWidget() );
109
110 long itemId = 0;
111 int result = 0;
112
113 while ((result =
114 XmClipboardStartCopy((Display*) wxGetDisplay(),
115 window,
116 text,
117 XtLastTimestampProcessed((Display*) wxGetDisplay()),
118 (Widget) 0,
119 (XmCutPasteProc) 0,
120 & itemId)) != ClipboardSuccess)
121
122 ;
123
124 XmStringFree (text);
125
126 long dataId = 0;
127 while ((result =
128 XmClipboardCopy((Display*) wxGetDisplay(),
129 window,
130 itemId,
9b64e798 131 "TEXT",
2d120f83
JS
132 (XtPointer) data,
133 strlen(data) + 1,
134 0,
135 & dataId)) != ClipboardSuccess)
136
137 ;
138
139 while (( result =
140 XmClipboardEndCopy((Display*) wxGetDisplay(),
141 window, itemId) ) != ClipboardSuccess)
142
143 ;
144
145 return TRUE;
4bb6408c
JS
146}
147
2d120f83 148wxObject *wxGetClipboardData(wxDataFormat dataFormat, long *len)
4bb6408c 149{
2d120f83
JS
150 if (dataFormat != wxDF_TEXT)
151 return (wxObject*) NULL;
152
153 bool done = FALSE;
154 long id = 0;
155 unsigned long numBytes = 0;
156 int result = 0;
157 Window window = (Window) 0;
158 if (wxTheApp->GetTopWindow())
159 window = XtWindow( (Widget) wxTheApp->GetTopWindow()->GetTopWidget() );
160
161 int currentDataSize = 256;
162 char* data = new char[currentDataSize];
163
164 while (!done)
165 {
166 if (result == ClipboardTruncate)
167 {
168 delete[] data;
169 currentDataSize = 2*currentDataSize;
170 data = new char[currentDataSize];
171 }
172 result = XmClipboardRetrieve((Display*) wxGetDisplay(),
173 window,
9b64e798 174 "TEXT",
2d120f83
JS
175 (XtPointer) data,
176 currentDataSize,
177 &numBytes,
178 &id);
179
180 switch (result)
181 {
182 case ClipboardSuccess:
183 {
184 if (len)
185 *len = strlen(data) + 1;
186 return (wxObject*) data;
187 break;
188 }
189 case ClipboardTruncate:
190 case ClipboardLocked:
191 {
192 break;
193 }
194 default:
195 case ClipboardNoData:
196 {
197 return (wxObject*) NULL;
198 break;
199 }
200 }
201
202 }
203
4bb6408c
JS
204 return NULL;
205}
206
2d120f83 207wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
4bb6408c 208{
2d120f83 209 // Only wxDF_TEXT supported
da175b2c 210 if (dataFormat == wxDF_TEXT)
2d120f83
JS
211 return wxDF_TEXT;
212 else
da175b2c 213 return wxDF_INVALID;
4bb6408c
JS
214}
215
2d120f83 216wxDataFormat wxRegisterClipboardFormat(char *formatName)
4bb6408c 217{
2d120f83 218 // Not supported
da175b2c 219 return (wxDataFormat) wxDF_INVALID;
4bb6408c
JS
220}
221
2d120f83 222bool wxGetClipboardFormatName(wxDataFormat dataFormat, char *formatName, int maxCount)
4bb6408c 223{
2d120f83
JS
224 // Only wxDF_TEXT supported
225 if (dataFormat == wxDF_TEXT)
226 {
9b64e798 227 strcpy(formatName, "TEXT");
2d120f83
JS
228 return TRUE;
229 }
230 else
231 return FALSE;
4bb6408c
JS
232}
233
2d120f83
JS
234//-----------------------------------------------------------------------------
235// wxClipboard
236//-----------------------------------------------------------------------------
4bb6408c 237
2d120f83 238IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
4bb6408c 239
2d120f83 240wxClipboard* wxTheClipboard = (wxClipboard*) NULL;
4bb6408c
JS
241
242wxClipboard::wxClipboard()
243{
2d120f83 244 m_open = FALSE;
4bb6408c
JS
245}
246
247wxClipboard::~wxClipboard()
248{
2d120f83 249 Clear();
4bb6408c
JS
250}
251
2d120f83 252void wxClipboard::Clear()
4bb6408c 253{
2d120f83
JS
254 wxNode* node = m_data.First();
255 while (node)
256 {
257 wxDataObject* data = (wxDataObject*) node->Data();
258 delete data;
259 node = node->Next();
260 }
261 m_data.Clear();
4bb6408c
JS
262}
263
2d120f83 264bool wxClipboard::Open()
4bb6408c 265{
2d120f83
JS
266 wxCHECK_MSG( !m_open, FALSE, "clipboard already open" );
267
268 m_open = TRUE;
4bb6408c 269
2d120f83
JS
270 return wxOpenClipboard();
271}
4bb6408c 272
2d120f83
JS
273bool wxClipboard::SetData( wxDataObject *data )
274{
275 wxCHECK_MSG( data, FALSE, "data is invalid" );
276 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
277
278 switch (data->GetFormat())
279 {
280 case wxDF_TEXT:
281 case wxDF_OEMTEXT:
282 {
283 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
284 wxString str(textDataObject->GetText());
285 return wxSetClipboardData(data->GetFormat(), (wxObject*) (const char*) str);
286 break;
287 }
da175b2c 288/*
2d120f83
JS
289 case wxDF_BITMAP:
290 case wxDF_DIB:
291 {
292 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
293 wxBitmap bitmap(bitmapDataObject->GetBitmap());
294 return wxSetClipboardData(data->GetFormat(), & bitmap);
295 break;
296 }
da175b2c 297*/
2d120f83
JS
298 default:
299 {
300 return FALSE;
301 }
302 }
4bb6408c 303
2d120f83
JS
304 return FALSE;
305}
4bb6408c 306
2d120f83
JS
307void wxClipboard::Close()
308{
309 wxCHECK_RET( m_open, "clipboard not open" );
310
311 m_open = FALSE;
312 wxCloseClipboard();
4bb6408c
JS
313}
314
5b6aa0ff 315bool wxClipboard::IsSupported( wxDataFormat format)
4bb6408c 316{
2d120f83 317 return wxIsClipboardFormatAvailable(format);
4bb6408c
JS
318}
319
2d120f83 320bool wxClipboard::GetData( wxDataObject *data )
4bb6408c 321{
2d120f83
JS
322 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
323
324 switch (data->GetFormat())
325 {
326 case wxDF_TEXT:
327 case wxDF_OEMTEXT:
328 {
329 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
330 char* s = (char*) wxGetClipboardData(data->GetFormat());
331 if (s)
332 {
333 textDataObject->SetText(s);
334 delete[] s;
335 return TRUE;
336 }
337 else
338 return FALSE;
339 break;
340 }
da175b2c 341/*
2d120f83
JS
342 case wxDF_BITMAP:
343 case wxDF_DIB:
344 {
345 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
346 wxBitmap* bitmap = (wxBitmap*) wxGetClipboardData(data->GetFormat());
347 if (bitmap)
348 {
349 bitmapDataObject->SetBitmap(* bitmap);
350 delete bitmap;
351 return TRUE;
352 }
353 else
354 return FALSE;
355 break;
356 }
da175b2c 357*/
2d120f83
JS
358 default:
359 {
360 return FALSE;
361 }
362 }
363 return FALSE;
364}
4bb6408c 365
2d120f83
JS
366#if 0
367
368/*
369* Old clipboard implementation by Matthew Flatt
370*/
371
372wxClipboard *wxTheClipboard = NULL;
373
374void wxInitClipboard()
375{
376 if (!wxTheClipboard)
377 wxTheClipboard = new wxClipboard;
378}
4bb6408c 379
2d120f83
JS
380wxClipboard::wxClipboard()
381{
382 clipOwner = NULL;
4bb6408c 383 cbString = NULL;
4bb6408c
JS
384}
385
2d120f83
JS
386wxClipboard::~wxClipboard()
387{
388 if (clipOwner)
389 clipOwner->BeingReplaced();
390 if (cbString)
391 delete[] cbString;
392}
393
394static int FormatStringToID(char *str)
4bb6408c 395{
9b64e798 396 if (!strcmp(str, "TEXT"))
2d120f83
JS
397 return wxDF_TEXT;
398
399 return wxRegisterClipboardFormat(str);
400}
4bb6408c 401
2d120f83
JS
402void wxClipboard::SetClipboardClient(wxClipboardClient *client, long time)
403{
404 bool got_selection;
405
406 if (clipOwner)
407 clipOwner->BeingReplaced();
408 clipOwner = client;
409 if (cbString) {
410 delete[] cbString;
411 cbString = NULL;
412 }
413
414 if (wxOpenClipboard()) {
415 char **formats, *data;
416 int i;
417 int ftype;
418 long size;
419
420 formats = clipOwner->formats.ListToArray(FALSE);
421 for (i = clipOwner->formats.Number(); i--; ) {
422 ftype = FormatStringToID(formats[i]);
423 data = clipOwner->GetData(formats[i], &size);
424 if (!wxSetClipboardData(ftype, (wxObject *)data, size, 1)) {
425 got_selection = FALSE;
426 break;
427 }
428 }
429
430 if (i < 0)
431 got_selection = wxCloseClipboard();
432 } else
433 got_selection = FALSE;
434
435 got_selection = FALSE; // Assume another process takes over
436
437 if (!got_selection) {
438 clipOwner->BeingReplaced();
439 clipOwner = NULL;
440 }
441}
4bb6408c 442
2d120f83
JS
443wxClipboardClient *wxClipboard::GetClipboardClient()
444{
445 return clipOwner;
4bb6408c
JS
446}
447
2d120f83 448void wxClipboard::SetClipboardString(char *str, long time)
4bb6408c 449{
2d120f83
JS
450 bool got_selection;
451
452 if (clipOwner) {
453 clipOwner->BeingReplaced();
454 clipOwner = NULL;
455 }
456 if (cbString)
457 delete[] cbString;
458
459 cbString = str;
460
4bb6408c 461 if (wxOpenClipboard()) {
2d120f83
JS
462 if (!wxSetClipboardData(wxDF_TEXT, (wxObject *)str))
463 got_selection = FALSE;
464 else
465 got_selection = wxCloseClipboard();
4bb6408c 466 } else
2d120f83
JS
467 got_selection = FALSE;
468
469 got_selection = FALSE; // Assume another process takes over
470
471 if (!got_selection) {
472 delete[] cbString;
473 cbString = NULL;
474 }
475}
476
477char *wxClipboard::GetClipboardString(long time)
478{
479 char *str;
480 long length;
481
9b64e798 482 str = GetClipboardData("TEXT", &length, time);
2d120f83
JS
483 if (!str) {
484 str = new char[1];
485 *str = 0;
486 }
487
488 return str;
489}
4bb6408c 490
2d120f83
JS
491char *wxClipboard::GetClipboardData(char *format, long *length, long time)
492{
493 if (clipOwner) {
494 if (clipOwner->formats.Member(format))
495 return clipOwner->GetData(format, length);
496 else
497 return NULL;
498 } else if (cbString) {
9b64e798 499 if (!strcmp(format, "TEXT"))
2d120f83
JS
500 return copystring(cbString);
501 else
502 return NULL;
503 } else {
504 if (wxOpenClipboard()) {
505 receivedString = (char *)wxGetClipboardData(FormatStringToID(format),
506 length);
507 wxCloseClipboard();
508 } else
509 receivedString = NULL;
510
511 return receivedString;
512 }
4bb6408c 513}
2d120f83 514#endif
4bb6408c 515
dfe1eee3 516#endif // wxUSE_CLIPBOARD