2 * Copyright 2011-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Gabe Yoder, gyoder@stny.rr.com
7 * John Scipione, jscipione@gmail.com
10 * headers/os/app/Clipboard.h hrev47355
11 * src/kits/app/Clipboard.cpp hrev47355
19 \brief Provides the BClipboard class.
25 \brief Global system clipboard object.
35 \brief Used for short-term data storage between documents and
36 applications via copy and paste operations.
38 Clipboards are differentiated by their name. In order for two
39 applications to share a clipboard they simply have to create a
40 BClipboard object with the same name. However, it is rarely necessary
41 to create your own clipboard, instead you can use the \c be_clipboard
42 system clipboard object.
44 \remark To access the system clipboard without a BApplication object,
45 create a BClipboard object with the name "system". You should avoid
46 creating a custom clipboard with the name "system" for your own use.
48 To access the clipboard data call the Data() method. The BMessage object
49 returned by the Data() method has the following properties:
50 - The \c what value is unused.
51 - The clipboard data is stored in a message field typed as
53 - The MIME type of the data is used as the name of the field that
55 - Each field in the data message contains the same data with a
58 To read and write to the clipboard you must first lock the BClipboard
59 object. If you fail to lock the BClipboard object then the Data() method
60 will return \c NULL instead of a pointer to a BMessage object.
62 Below is an example of reading a string from the system clipboard.
66 if (be_clipboard->Lock()) {
67 // Get the clipboard BMessage
68 BMessage *clip = be_clipboard->Data();
70 // Read the string from the clipboard data message
71 clip->FindData("text/plain", B_MIME_TYPE, (const void **)&string,
74 be_clipboard->Unlock();
76 fprintf(stderr, "could not lock clipboard.\n");
79 Below is an example of writing a string to the system clipboard.
81 const char* string = "Some clipboard data";
83 if (be_clipboard->Lock()) {
84 // Clear the clipboard data
85 be_clipboard->Clear();
87 // Get the clipboard data message
88 BMessage *clip = be_clipboard->Data();
90 // Write string data to the clipboard data message
91 clip->AddData("text/plain", B_MIME_TYPE, string, strlen(string));
93 // Commit the data to the clipboard
94 status = be_clipboard->Commit();
96 fprintf(stderr, "could not commit data to clipboard.\n");
98 be_clipboard->Unlock();
100 fprintf(stderr, "could not lock clipboard.\n");
108 \fn BClipboard::BClipboard(const char* name, bool transient = false)
109 \brief Create a BClipboard object with the given \a name.
111 If the \a name parameter is \c NULL then the "system" BClipboard object
112 is constructed instead.
114 \param name The \a name of the clipboard.
115 \param transient If \c true, lose data after a reboot (currently unused).
122 \fn BClipboard::~BClipboard()
123 \brief Destroys the BClipboard object. The clipboard data is not destroyed.
130 \fn const char* BClipboard::Name() const
131 \brief Returns the name of the BClipboard object.
133 \returns The name of the clipboard.
148 \fn uint32 BClipboard::LocalCount() const
149 \brief Returns the (locally cached) number of commits to the clipboard.
151 The returned value is the number of successful Commit() invocations for
152 the clipboard represented by this object, either invoked on this object
153 or another (even from another application). This method returns a locally
154 cached value, which might already be obsolete. For an up-to-date value
157 \return The number of commits to the clipboard.
166 \fn uint32 BClipboard::SystemCount() const
167 \brief Returns the number of commits to the clipboard.
169 The returned value is the number of successful Commit() invocations for
170 the clipboard represented by this object, either invoked on this object
171 or another (even from another application). This method retrieves the
172 value directly from the system service managing the clipboards, so it is
173 more expensive, but more up-to-date than LocalCount(), which returns a
174 locally cached value.
176 \return The number of commits to the clipboard.
196 \fn status_t BClipboard::StartWatching(BMessenger target)
197 \brief Start watching the BClipboard object for changes.
199 When a change in the clipboard occurs, most like as the result of a cut
200 or copy action, a \a B_CLIPBOARD_CHANGED message is sent to \a target.
202 \retval B_OK Everything went fine.
203 \retval B_BAD_VALUE \a target is invalid.
204 \retval B_ERROR An error occured.
213 \fn status_t BClipboard::StopWatching(BMessenger target)
214 \brief Stop watching the BClipboard object for changes.
216 \retval B_OK Everything went fine.
217 \retval B_BAD_VALUE \a target is invalid.
218 \retval B_ERROR An error occurred.
238 \fn bool BClipboard::Lock()
239 \brief Locks the clipboard so that no other tread can read from it or
242 You should call Lock() before reading or writing to the clipboard.
244 \returns \c true if the clipboard was locked, \c false otherwise.
253 \fn void BClipboard::Unlock()
254 \brief Unlocks the clipboard.
263 \fn bool BClipboard::IsLocked() const
264 \brief Returns whether or not the clipboard is locked.
266 \returns \c true if the clipboard is locked, \c false if it is unlocked.
276 \name Clipboard Data Transaction
284 \fn status_t BClipboard::Clear()
285 \brief Clears out all data from the clipboard.
287 You should call Clear() before adding new data to the BClipboard object.
289 \return A status code.
290 \retval B_OK Everything went find.
291 \retval B_NOT_ALLOWED The clipboard is not locked.
292 \retval B_NO_MEMORY Ran out of memory initializing the data message.
293 \retval B_ERROR Another error occurred.
300 \fn status_t BClipboard::Commit()
301 \brief Commits the clipboard data to the BClipboard object.
303 \return A status code.
304 \retval B_OK Everything went find.
305 \retval B_NOT_ALLOWED The clipboard is not locked.
306 \retval B_ERROR Another error occurred.
313 \fn status_t BClipboard::Commit(bool failIfChanged)
314 \brief Commits the clipboard data to the BClipboard object with the
315 option to fail if there is a change to the clipboard data.
317 \param failIfChanged Whether or not to fail to commit the changes
318 if there is a change in the clipboard data.
320 \return A status code.
321 \retval B_OK Everything went find.
322 \retval B_NOT_ALLOWED The clipboard is not locked.
323 \retval B_ERROR Another error occurred.
330 \fn status_t BClipboard::Revert()
331 \brief Reverts the clipboard data.
333 The method should be used in the case that you have made a change to the
334 clipboard data message and then decide to revert the change instead of
337 \return A status code.
338 \retval B_OK Everything went find.
339 \retval B_NOT_ALLOWED The clipboard is not locked.
340 \retval B_NO_MEMORY Ran out of memory initializing the data message.
341 \retval B_ERROR Another error occurred.
351 \name Clipboard Data Message
359 \fn BMessenger BClipboard::DataSource() const
360 \brief Gets a BMessenger object targeting the application that last
361 modified the clipboard.
363 The clipboard object does not need to be locked to call this method.
365 \returns A BMessenger object that targets the application that last
366 modified the clipboard.
373 \fn BMessage* BClipboard::Data() const
374 \brief Gets a pointer to the BMessage object that holds the clipboard
377 If the BClipboard object is not locked this method returns \c NULL.
379 \returns A pointer to the BMessage object that holds the clipboard
380 data or \c NULL if the clipboard is not locked.