source: trunk/src/kernel/qclipboard.cpp@ 109

Last change on this file since 109 was 8, checked in by dmik, 20 years ago

Transferred Qt for OS/2 version 3.3.1-rc5 sources from the CVS

  • Property svn:keywords set to Id
File size: 18.9 KB
Line 
1/****************************************************************************
2** $Id: qclipboard.cpp 8 2005-11-16 19:36:46Z dmik $
3**
4** Implementation of QClipboard class
5**
6** Created : 960430
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the kernel module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qclipboard.h"
39
40#ifndef QT_NO_CLIPBOARD
41
42#include "qapplication.h"
43#include "qapplication_p.h"
44#include "qdragobject.h"
45#include "qpixmap.h"
46
47/*!
48 \class QClipboard qclipboard.h
49 \brief The QClipboard class provides access to the window system clipboard.
50
51 \ingroup io
52 \ingroup environment
53 \mainclass
54
55 The clipboard offers a simple mechanism to copy and paste data
56 between applications.
57
58 QClipboard supports the same data types that QDragObject does, and
59 uses similar mechanisms. For advanced clipboard usage
60 read \link dnd.html the drag-and-drop documentation\endlink.
61
62 There is a single QClipboard object in an application, and you can
63 access it using QApplication::clipboard().
64
65 Example:
66 \code
67 QClipboard *cb = QApplication::clipboard();
68
69 // Copy text from the clipboard (paste)
70 QString text = cb->text(QClipboard::Clipboard);
71 if ( !text.isNull() )
72 qDebug( "The clipboard contains: " + text );
73
74 // Copy text into the clipboard
75 cb->setText( "This text can be pasted by other programs",
76 QClipboard::Clipboard );
77 \endcode
78
79 QClipboard features some convenience functions to access common data
80 types: setText() allows the exchange of Unicode text and
81 setPixmap() and setImage() allows the exchange of QPixmaps
82 and QImages between applications. The setData() function is the
83 ultimate in flexibility: it allows you to add any QMimeSource into the
84 clipboard. There are corresponding getters for each of these, e.g.
85 text(), image() and pixmap().
86
87 You can clear the clipboard by calling clear().
88
89
90 \section1 Platform Specific Information
91
92 \section2 X11
93
94 \list
95
96 \i The X11 Window System has the concept of a separate selection
97 and clipboard. When text is selected, it is immediately available
98 as the global mouse selection. The global mouse selection may
99 later be copied to the clipboard. By convention, the middle mouse
100 button is used to paste the global mouse selection.
101
102 \i X11 also has the concept of ownership; if you change the
103 selection within a window, X11 will only notify the owner and the
104 previous owner of the change, i.e. it will not notify all
105 applications that the selection or clipboard data changed.
106
107 \i Lastly, the X11 clipboard is event driven, i.e. the clipboard
108 will not function properly if the event loop is not running.
109 Similarly, it is recommended that the contents of the clipboard
110 are stored or retrieved in direct response to user-input events,
111 e.g. mouse button or key presses and releases. You should not
112 store or retrieve the clipboard contents in response to timer or
113 non-user-input events.
114
115 \endlist
116
117 \section2 Windows
118
119 \list
120
121 \i Microsoft Windows does not support the global mouse selection;
122 it only supports the global clipboard, e.g. Windows only adds text
123 to the clipboard when an explicit copy or cut is made.
124
125 \i Windows does not have the concept of ownership; the clipboard
126 is a fully global resource so all applications are notified of
127 changes.
128
129 \endlist
130
131 See the multiclip example in the \e{Qt Designer} examples
132 directory for an example of a multiplatform clipboard application
133 that also demonstrates selection handling.
134*/
135
136
137/*!
138 \internal
139
140 Constructs a clipboard object.
141
142 Do not call this function.
143
144 Call QApplication::clipboard() instead to get a pointer to the
145 application's global clipboard object.
146
147 There is only one clipboard in the window system, and creating
148 more than one object to represent it is almost certainly an error.
149*/
150
151QClipboard::QClipboard( QObject *parent, const char *name )
152 : QObject( parent, name )
153{
154 // nothing
155}
156
157#if !defined(Q_WS_WIN32) && !defined(Q_WS_PM)
158/*!
159 \internal
160
161 Destroys the clipboard.
162
163 You should never delete the clipboard. QApplication will do this
164 when the application terminates.
165*/
166QClipboard::~QClipboard()
167{
168}
169#endif
170
171/*!
172 \fn void QClipboard::dataChanged()
173
174 This signal is emitted when the clipboard data is changed.
175
176 \note On OS/2, this signal is guaranteed to be emitted only if no other
177 non-Qt applications (for example, the standard Clipboard Viewer shipped
178 with OS/2) register themselves as clipboard viewers -- this is the
179 limitation of the Presentation Manager, not Qt. Reconnecting this signal
180 to a slot will restore its functionality (until another non-Qt application
181 will again register itself as the clipboard viewer).
182*/
183
184/*!
185 \fn void QClipboard::selectionChanged()
186
187 This signal is emitted when the selection is changed. This only
188 applies to windowing systems that support selections, e.g. X11.
189 Windows doesn't support selections.
190*/
191
192/*! \enum QClipboard::Mode
193 \keyword clipboard mode
194
195 This enum type is used to control which part of the system clipboard is
196 used by QClipboard::data(), QClipboard::setData() and related functions.
197
198 \value Clipboard indicates that data should be stored and retrieved from
199 the global clipboard.
200
201 \value Selection indicates that data should be stored and retrieved from
202 the global mouse selection.
203
204 \e Note: Support for \c Selection is provided only on systems with a
205 global mouse selection (e.g. X11).
206
207 \sa QClipboard::supportsSelection()
208*/
209
210
211/*****************************************************************************
212 QApplication member functions related to QClipboard.
213 *****************************************************************************/
214
215#ifndef QT_NO_MIMECLIPBOARD
216// text handling is done directly in qclipboard_qws, for now
217
218/*!
219 \overload
220
221 Returns the clipboard text in subtype \a subtype, or a null string
222 if the clipboard does not contain any text. If \a subtype is null,
223 any subtype is acceptable, and \a subtype is set to the chosen
224 subtype.
225
226 The \a mode argument is used to control which part of the system
227 clipboard is used. If \a mode is QClipboard::Clipboard, the
228 text is retrieved from the global clipboard. If \a mode is
229 QClipboard::Selection, the text is retrieved from the global
230 mouse selection.
231
232 Common values for \a subtype are "plain" and "html".
233
234 \sa setText(), data(), QString::operator!()
235*/
236QString QClipboard::text( QCString &subtype, Mode mode ) const
237{
238 QString r;
239 QTextDrag::decode( data( mode ) ,r, subtype );
240 return r;
241}
242
243/*!
244 \overload
245
246 Returns the clipboard text in subtype \a subtype, or a null string
247 if the clipboard does not contain any text. This function uses the
248 QClipboard::text() function which takes a QClipboard::Mode
249 argument. The value of the mode argument is determined by the
250 return value of selectionModeEnabled(). If selectionModeEnabled()
251 returns TRUE, the mode argument is QClipboard::Selection,
252 otherwise the mode argument is QClipboard::Clipboard.
253*/
254// ### remove 4.0
255QString QClipboard::text( QCString& subtype ) const
256{
257 return text( subtype, selectionModeEnabled() ? Selection : Clipboard );
258}
259
260/*!
261 Returns the clipboard text as plain text, or a null string if the
262 clipboard does not contain any text.
263
264 The \a mode argument is used to control which part of the system
265 clipboard is used. If \a mode is QClipboard::Clipboard, the
266 text is retrieved from the global clipboard. If \a mode is
267 QClipboard::Selection, the text is retrieved from the global
268 mouse selection.
269
270 \sa setText(), data(), QString::operator!()
271*/
272QString QClipboard::text( Mode mode ) const
273{
274 QCString subtype = "plain";
275 return text( subtype, mode );
276}
277
278/*!
279 \overload
280
281 This function uses the QClipboard::text() function which takes
282 a QClipboard::Mode argument. The value of the mode argument is
283 determined by the return value of selectionModeEnabled().
284 If selectionModeEnabled() returns TRUE, the mode argument is
285 QClipboard::Selection, otherwise the mode argument is
286 QClipboard::Clipboard.
287*/
288
289QString QClipboard::text() const
290{
291 return text( selectionModeEnabled() ? Selection : Clipboard );
292}
293
294 /*!
295 Copies \a text into the clipboard as plain text.
296
297 The \a mode argument is used to control which part of the system
298 clipboard is used. If \a mode is QClipboard::Clipboard, the
299 text is stored in the global clipboard. If \a mode is
300 QClipboard::Selection, the text is stored in the global
301 mouse selection.
302
303 \sa text(), setData()
304*/
305
306void QClipboard::setText( const QString &text, Mode mode )
307{
308 setData( new QTextDrag(text), mode );
309}
310
311/*!
312 \overload
313
314 This function uses the QClipboard::setText() function which takes
315 a QClipboard::Mode argument. The value of the mode argument is
316 determined by the return value of selectionModeEnabled().
317 If selectionModeEnabled() returns TRUE, the mode argument is
318 QClipboard::Selection, otherwise the mode argument is
319 QClipboard::Clipboard.
320*/
321// ### remove 4.0
322void QClipboard::setText( const QString &text )
323{
324 setText( text, selectionModeEnabled() ? Selection : Clipboard );
325}
326
327/*!
328 Returns the clipboard image, or returns a null image if the
329 clipboard does not contain an image or if it contains an image in
330 an unsupported image format.
331
332 The \a mode argument is used to control which part of the system
333 clipboard is used. If \a mode is QClipboard::Clipboard, the
334 image is retrieved from the global clipboard. If \a mode is
335 QClipboard::Selection, the image is retrieved from the global
336 mouse selection.
337
338 \sa setImage() pixmap() data(), QImage::isNull()
339*/
340QImage QClipboard::image( Mode mode ) const
341{
342 QImage r;
343 QImageDrag::decode( data( mode ), r );
344 return r;
345}
346
347/*!
348 \overload
349
350 This function uses the QClipboard::image() function which takes
351 a QClipboard::Mode argument. The value of the mode argument is
352 determined by the return value of selectionModeEnabled().
353 If selectionModeEnabled() returns TRUE, the mode argument is
354 QClipboard::Selection, otherwise the mode argument is
355 QClipboard::Clipboard.
356*/
357// ### remove 4.0
358QImage QClipboard::image() const
359{
360 return image( selectionModeEnabled() ? Selection : Clipboard );
361}
362
363/*!
364 Copies \a image into the clipboard.
365
366 The \a mode argument is used to control which part of the system
367 clipboard is used. If \a mode is QClipboard::Clipboard, the
368 image is stored in the global clipboard. If \a mode is
369 QClipboard::Selection, the data is stored in the global
370 mouse selection.
371
372 This is shorthand for:
373 \code
374 setData( new QImageDrag(image), mode )
375 \endcode
376
377 \sa image(), setPixmap() setData()
378*/
379void QClipboard::setImage( const QImage &image, Mode mode )
380{
381 setData( new QImageDrag( image ), mode );
382}
383
384/*!
385 \overload
386
387 This function uses the QClipboard::setImage() function which takes
388 a QClipboard::Mode argument. The value of the mode argument is
389 determined by the return value of selectionModeEnabled().
390 If selectionModeEnabled() returns TRUE, the mode argument is
391 QClipboard::Selection, otherwise the mode argument is
392 QClipboard::Clipboard.
393*/
394// ### remove 4.0
395void QClipboard::setImage( const QImage &image )
396{
397 setImage( image, selectionModeEnabled() ? Selection : Clipboard );
398}
399
400/*!
401 Returns the clipboard pixmap, or null if the clipboard does not
402 contain a pixmap. Note that this can lose information. For
403 example, if the image is 24-bit and the display is 8-bit, the
404 result is converted to 8 bits, and if the image has an alpha
405 channel, the result just has a mask.
406
407 The \a mode argument is used to control which part of the system
408 clipboard is used. If \a mode is QClipboard::Clipboard, the
409 pixmap is retrieved from the global clipboard. If \a mode is
410 QClipboard::Selection, the pixmap is retrieved from the global
411 mouse selection.
412
413 \sa setPixmap() image() data() QPixmap::convertFromImage().
414*/
415QPixmap QClipboard::pixmap( Mode mode ) const
416{
417 QPixmap r;
418 QImageDrag::decode( data( mode ), r );
419 return r;
420}
421
422/*!
423 \overload
424
425 This function uses the QClipboard::pixmap() function which takes
426 a QClipboard::Mode argument. The value of the mode argument is
427 determined by the return value of selectionModeEnabled().
428 If selectionModeEnabled() returns TRUE, the mode argument is
429 QClipboard::Selection, otherwise the mode argument is
430 QClipboard::Clipboard.
431*/
432// ### remove 4.0
433QPixmap QClipboard::pixmap() const
434{
435 return pixmap( selectionModeEnabled() ? Selection : Clipboard );
436}
437
438/*!
439 Copies \a pixmap into the clipboard. Note that this is slower
440 than setImage() because it needs to convert the QPixmap to a
441 QImage first.
442
443 The \a mode argument is used to control which part of the system
444 clipboard is used. If \a mode is QClipboard::Clipboard, the
445 pixmap is stored in the global clipboard. If \a mode is
446 QClipboard::Selection, the pixmap is stored in the global
447 mouse selection.
448
449 \sa pixmap() setImage() setData()
450*/
451void QClipboard::setPixmap( const QPixmap &pixmap, Mode mode )
452{
453 // *could* just use the handle, but that is X hackery, MIME is better.
454 setData( new QImageDrag( pixmap.convertToImage() ), mode );
455}
456
457/*!
458 \overload
459
460 This function uses the QClipboard::setPixmap() function which takes
461 a QClipboard::Mode argument. The value of the mode argument is
462 determined by the return value of selectionModeEnabled().
463 If selectionModeEnabled() returns TRUE, the mode argument is
464 QClipboard::Selection, otherwise the mode argument is
465 QClipboard::Clipboard.
466*/
467// ### remove 4.0
468void QClipboard::setPixmap( const QPixmap &pixmap )
469{
470 setPixmap( pixmap, selectionModeEnabled() ? Selection : Clipboard );
471}
472
473
474/*! \fn QMimeSource *QClipboard::data( Mode mode ) const
475 Returns a reference to a QMimeSource representation of the current
476 clipboard data.
477
478 The \a mode argument is used to control which part of the system
479 clipboard is used. If \a mode is QClipboard::Clipboard, the
480 data is retrieved from the global clipboard. If \a mode is
481 QClipboard::Selection, the data is retrieved from the global
482 mouse selection.
483
484 \sa setData()
485*/
486
487/*!
488 \overload
489
490 This function uses the QClipboard::data() function which takes
491 a QClipboard::Mode argument. The value of the mode argument is
492 determined by the return value of selectionModeEnabled().
493 If selectionModeEnabled() returns TRUE, the mode argument is
494 QClipboard::Selection, otherwise the mode argument is
495 QClipboard::Clipboard.
496*/
497// ### remove 4.0
498QMimeSource *QClipboard::data() const
499{
500 return data( selectionModeEnabled() ? Selection : Clipboard );
501}
502
503/*! \fn void QClipboard::setData( QMimeSource *src, Mode mode )
504 Sets the clipboard data to \a src. Ownership of the data is
505 transferred to the clipboard. If you want to remove the data
506 either call clear() or call setData() again with new data.
507
508 The \a mode argument is used to control which part of the system
509 clipboard is used. If \a mode is QClipboard::Clipboard, the
510 data is retrieved from the global clipboard. If \a mode is
511 QClipboard::Selection, the data is retrieved from the global
512 mouse selection.
513
514 The QDragObject subclasses are reasonable objects to put into the
515 clipboard (but do not try to call QDragObject::drag() on the same
516 object). Any QDragObject placed in the clipboard should have a
517 parent of 0. Do not put QDragMoveEvent or QDropEvent subclasses in
518 the clipboard, as they do not belong to the event handler which
519 receives them.
520
521 The setText(), setImage() and setPixmap() functions are simpler
522 wrappers for setting text, image and pixmap data respectively.
523
524 \sa data()
525*/
526
527/*!
528 \overload
529
530 This function uses the QClipboard::setData() function which takes
531 a QClipboard::Mode argument. The value of the mode argument is
532 determined by the return value of selectionModeEnabled().
533 If selectionModeEnabled() returns TRUE, the mode argument is
534 QClipboard::Selection, otherwise the mode argument is
535 QClipboard::Clipboard.
536*/
537// ### remove 4.0
538void QClipboard::setData( QMimeSource *src )
539{
540 setData( src, selectionModeEnabled() ? Selection : Clipboard );
541}
542
543/*! \fn void QClipboard::clear( Mode mode )
544 Clear the clipboard contents.
545
546 The \a mode argument is used to control which part of the system
547 clipboard is used. If \a mode is QClipboard::Clipboard, this
548 function clears the the global clipboard contents. If \a mode is
549 QClipboard::Selection, this function clears the global mouse
550 selection contents.
551
552 \sa QClipboard::Mode, supportsSelection()
553*/
554
555/*!
556 \overload
557
558 This function uses the QClipboard::clear() function which takes
559 a QClipboard::Mode argument. The value of the mode argument is
560 determined by the return value of selectionModeEnabled().
561 If selectionModeEnabled() returns TRUE, the mode argument is
562 QClipboard::Selection, otherwise the mode argument is QClipboard::Clipboard.
563*/
564// ### remove 4.0
565void QClipboard::clear()
566{
567 clear( selectionModeEnabled() ? Selection : Clipboard );
568}
569
570#endif // QT_NO_MIMECLIPBOARD
571#endif // QT_NO_CLIPBOARD
Note: See TracBrowser for help on using the repository browser.