source: vendor/trolltech/current/src/kernel/qclipboard.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 18.5 KB
Line 
1/****************************************************************************
2** $Id: qclipboard.cpp 2 2005-11-16 15:49:26Z 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#ifndef Q_WS_WIN32
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
177/*!
178 \fn void QClipboard::selectionChanged()
179
180 This signal is emitted when the selection is changed. This only
181 applies to windowing systems that support selections, e.g. X11.
182 Windows doesn't support selections.
183*/
184
185/*! \enum QClipboard::Mode
186 \keyword clipboard mode
187
188 This enum type is used to control which part of the system clipboard is
189 used by QClipboard::data(), QClipboard::setData() and related functions.
190
191 \value Clipboard indicates that data should be stored and retrieved from
192 the global clipboard.
193
194 \value Selection indicates that data should be stored and retrieved from
195 the global mouse selection.
196
197 \e Note: Support for \c Selection is provided only on systems with a
198 global mouse selection (e.g. X11).
199
200 \sa QClipboard::supportsSelection()
201*/
202
203
204/*****************************************************************************
205 QApplication member functions related to QClipboard.
206 *****************************************************************************/
207
208#ifndef QT_NO_MIMECLIPBOARD
209// text handling is done directly in qclipboard_qws, for now
210
211/*!
212 \overload
213
214 Returns the clipboard text in subtype \a subtype, or a null string
215 if the clipboard does not contain any text. If \a subtype is null,
216 any subtype is acceptable, and \a subtype is set to the chosen
217 subtype.
218
219 The \a mode argument is used to control which part of the system
220 clipboard is used. If \a mode is QClipboard::Clipboard, the
221 text is retrieved from the global clipboard. If \a mode is
222 QClipboard::Selection, the text is retrieved from the global
223 mouse selection.
224
225 Common values for \a subtype are "plain" and "html".
226
227 \sa setText(), data(), QString::operator!()
228*/
229QString QClipboard::text( QCString &subtype, Mode mode ) const
230{
231 QString r;
232 QTextDrag::decode( data( mode ) ,r, subtype );
233 return r;
234}
235
236/*!
237 \overload
238
239 Returns the clipboard text in subtype \a subtype, or a null string
240 if the clipboard does not contain any text. This function uses the
241 QClipboard::text() function which takes a QClipboard::Mode
242 argument. The value of the mode argument is determined by the
243 return value of selectionModeEnabled(). If selectionModeEnabled()
244 returns TRUE, the mode argument is QClipboard::Selection,
245 otherwise the mode argument is QClipboard::Clipboard.
246*/
247// ### remove 4.0
248QString QClipboard::text( QCString& subtype ) const
249{
250 return text( subtype, selectionModeEnabled() ? Selection : Clipboard );
251}
252
253/*!
254 Returns the clipboard text as plain text, or a null string if the
255 clipboard does not contain any text.
256
257 The \a mode argument is used to control which part of the system
258 clipboard is used. If \a mode is QClipboard::Clipboard, the
259 text is retrieved from the global clipboard. If \a mode is
260 QClipboard::Selection, the text is retrieved from the global
261 mouse selection.
262
263 \sa setText(), data(), QString::operator!()
264*/
265QString QClipboard::text( Mode mode ) const
266{
267 QCString subtype = "plain";
268 return text( subtype, mode );
269}
270
271/*!
272 \overload
273
274 This function uses the QClipboard::text() function which takes
275 a QClipboard::Mode argument. The value of the mode argument is
276 determined by the return value of selectionModeEnabled().
277 If selectionModeEnabled() returns TRUE, the mode argument is
278 QClipboard::Selection, otherwise the mode argument is
279 QClipboard::Clipboard.
280*/
281
282QString QClipboard::text() const
283{
284 return text( selectionModeEnabled() ? Selection : Clipboard );
285}
286
287 /*!
288 Copies \a text into the clipboard as plain text.
289
290 The \a mode argument is used to control which part of the system
291 clipboard is used. If \a mode is QClipboard::Clipboard, the
292 text is stored in the global clipboard. If \a mode is
293 QClipboard::Selection, the text is stored in the global
294 mouse selection.
295
296 \sa text(), setData()
297*/
298
299void QClipboard::setText( const QString &text, Mode mode )
300{
301 setData( new QTextDrag(text), mode );
302}
303
304/*!
305 \overload
306
307 This function uses the QClipboard::setText() function which takes
308 a QClipboard::Mode argument. The value of the mode argument is
309 determined by the return value of selectionModeEnabled().
310 If selectionModeEnabled() returns TRUE, the mode argument is
311 QClipboard::Selection, otherwise the mode argument is
312 QClipboard::Clipboard.
313*/
314// ### remove 4.0
315void QClipboard::setText( const QString &text )
316{
317 setText( text, selectionModeEnabled() ? Selection : Clipboard );
318}
319
320/*!
321 Returns the clipboard image, or returns a null image if the
322 clipboard does not contain an image or if it contains an image in
323 an unsupported image format.
324
325 The \a mode argument is used to control which part of the system
326 clipboard is used. If \a mode is QClipboard::Clipboard, the
327 image is retrieved from the global clipboard. If \a mode is
328 QClipboard::Selection, the image is retrieved from the global
329 mouse selection.
330
331 \sa setImage() pixmap() data(), QImage::isNull()
332*/
333QImage QClipboard::image( Mode mode ) const
334{
335 QImage r;
336 QImageDrag::decode( data( mode ), r );
337 return r;
338}
339
340/*!
341 \overload
342
343 This function uses the QClipboard::image() function which takes
344 a QClipboard::Mode argument. The value of the mode argument is
345 determined by the return value of selectionModeEnabled().
346 If selectionModeEnabled() returns TRUE, the mode argument is
347 QClipboard::Selection, otherwise the mode argument is
348 QClipboard::Clipboard.
349*/
350// ### remove 4.0
351QImage QClipboard::image() const
352{
353 return image( selectionModeEnabled() ? Selection : Clipboard );
354}
355
356/*!
357 Copies \a image into the clipboard.
358
359 The \a mode argument is used to control which part of the system
360 clipboard is used. If \a mode is QClipboard::Clipboard, the
361 image is stored in the global clipboard. If \a mode is
362 QClipboard::Selection, the data is stored in the global
363 mouse selection.
364
365 This is shorthand for:
366 \code
367 setData( new QImageDrag(image), mode )
368 \endcode
369
370 \sa image(), setPixmap() setData()
371*/
372void QClipboard::setImage( const QImage &image, Mode mode )
373{
374 setData( new QImageDrag( image ), mode );
375}
376
377/*!
378 \overload
379
380 This function uses the QClipboard::setImage() function which takes
381 a QClipboard::Mode argument. The value of the mode argument is
382 determined by the return value of selectionModeEnabled().
383 If selectionModeEnabled() returns TRUE, the mode argument is
384 QClipboard::Selection, otherwise the mode argument is
385 QClipboard::Clipboard.
386*/
387// ### remove 4.0
388void QClipboard::setImage( const QImage &image )
389{
390 setImage( image, selectionModeEnabled() ? Selection : Clipboard );
391}
392
393/*!
394 Returns the clipboard pixmap, or null if the clipboard does not
395 contain a pixmap. Note that this can lose information. For
396 example, if the image is 24-bit and the display is 8-bit, the
397 result is converted to 8 bits, and if the image has an alpha
398 channel, the result just has a mask.
399
400 The \a mode argument is used to control which part of the system
401 clipboard is used. If \a mode is QClipboard::Clipboard, the
402 pixmap is retrieved from the global clipboard. If \a mode is
403 QClipboard::Selection, the pixmap is retrieved from the global
404 mouse selection.
405
406 \sa setPixmap() image() data() QPixmap::convertFromImage().
407*/
408QPixmap QClipboard::pixmap( Mode mode ) const
409{
410 QPixmap r;
411 QImageDrag::decode( data( mode ), r );
412 return r;
413}
414
415/*!
416 \overload
417
418 This function uses the QClipboard::pixmap() function which takes
419 a QClipboard::Mode argument. The value of the mode argument is
420 determined by the return value of selectionModeEnabled().
421 If selectionModeEnabled() returns TRUE, the mode argument is
422 QClipboard::Selection, otherwise the mode argument is
423 QClipboard::Clipboard.
424*/
425// ### remove 4.0
426QPixmap QClipboard::pixmap() const
427{
428 return pixmap( selectionModeEnabled() ? Selection : Clipboard );
429}
430
431/*!
432 Copies \a pixmap into the clipboard. Note that this is slower
433 than setImage() because it needs to convert the QPixmap to a
434 QImage first.
435
436 The \a mode argument is used to control which part of the system
437 clipboard is used. If \a mode is QClipboard::Clipboard, the
438 pixmap is stored in the global clipboard. If \a mode is
439 QClipboard::Selection, the pixmap is stored in the global
440 mouse selection.
441
442 \sa pixmap() setImage() setData()
443*/
444void QClipboard::setPixmap( const QPixmap &pixmap, Mode mode )
445{
446 // *could* just use the handle, but that is X hackery, MIME is better.
447 setData( new QImageDrag( pixmap.convertToImage() ), mode );
448}
449
450/*!
451 \overload
452
453 This function uses the QClipboard::setPixmap() function which takes
454 a QClipboard::Mode argument. The value of the mode argument is
455 determined by the return value of selectionModeEnabled().
456 If selectionModeEnabled() returns TRUE, the mode argument is
457 QClipboard::Selection, otherwise the mode argument is
458 QClipboard::Clipboard.
459*/
460// ### remove 4.0
461void QClipboard::setPixmap( const QPixmap &pixmap )
462{
463 setPixmap( pixmap, selectionModeEnabled() ? Selection : Clipboard );
464}
465
466
467/*! \fn QMimeSource *QClipboard::data( Mode mode ) const
468 Returns a reference to a QMimeSource representation of the current
469 clipboard data.
470
471 The \a mode argument is used to control which part of the system
472 clipboard is used. If \a mode is QClipboard::Clipboard, the
473 data is retrieved from the global clipboard. If \a mode is
474 QClipboard::Selection, the data is retrieved from the global
475 mouse selection.
476
477 \sa setData()
478*/
479
480/*!
481 \overload
482
483 This function uses the QClipboard::data() function which takes
484 a QClipboard::Mode argument. The value of the mode argument is
485 determined by the return value of selectionModeEnabled().
486 If selectionModeEnabled() returns TRUE, the mode argument is
487 QClipboard::Selection, otherwise the mode argument is
488 QClipboard::Clipboard.
489*/
490// ### remove 4.0
491QMimeSource *QClipboard::data() const
492{
493 return data( selectionModeEnabled() ? Selection : Clipboard );
494}
495
496/*! \fn void QClipboard::setData( QMimeSource *src, Mode mode )
497 Sets the clipboard data to \a src. Ownership of the data is
498 transferred to the clipboard. If you want to remove the data
499 either call clear() or call setData() again with new data.
500
501 The \a mode argument is used to control which part of the system
502 clipboard is used. If \a mode is QClipboard::Clipboard, the
503 data is retrieved from the global clipboard. If \a mode is
504 QClipboard::Selection, the data is retrieved from the global
505 mouse selection.
506
507 The QDragObject subclasses are reasonable objects to put into the
508 clipboard (but do not try to call QDragObject::drag() on the same
509 object). Any QDragObject placed in the clipboard should have a
510 parent of 0. Do not put QDragMoveEvent or QDropEvent subclasses in
511 the clipboard, as they do not belong to the event handler which
512 receives them.
513
514 The setText(), setImage() and setPixmap() functions are simpler
515 wrappers for setting text, image and pixmap data respectively.
516
517 \sa data()
518*/
519
520/*!
521 \overload
522
523 This function uses the QClipboard::setData() function which takes
524 a QClipboard::Mode argument. The value of the mode argument is
525 determined by the return value of selectionModeEnabled().
526 If selectionModeEnabled() returns TRUE, the mode argument is
527 QClipboard::Selection, otherwise the mode argument is
528 QClipboard::Clipboard.
529*/
530// ### remove 4.0
531void QClipboard::setData( QMimeSource *src )
532{
533 setData( src, selectionModeEnabled() ? Selection : Clipboard );
534}
535
536/*! \fn void QClipboard::clear( Mode mode )
537 Clear the clipboard contents.
538
539 The \a mode argument is used to control which part of the system
540 clipboard is used. If \a mode is QClipboard::Clipboard, this
541 function clears the the global clipboard contents. If \a mode is
542 QClipboard::Selection, this function clears the global mouse
543 selection contents.
544
545 \sa QClipboard::Mode, supportsSelection()
546*/
547
548/*!
549 \overload
550
551 This function uses the QClipboard::clear() function which takes
552 a QClipboard::Mode argument. The value of the mode argument is
553 determined by the return value of selectionModeEnabled().
554 If selectionModeEnabled() returns TRUE, the mode argument is
555 QClipboard::Selection, otherwise the mode argument is QClipboard::Clipboard.
556*/
557// ### remove 4.0
558void QClipboard::clear()
559{
560 clear( selectionModeEnabled() ? Selection : Clipboard );
561}
562
563#endif // QT_NO_MIMECLIPBOARD
564#endif // QT_NO_CLIPBOARD
Note: See TracBrowser for help on using the repository browser.