xref: /haiku/docs/user/app/Clipboard.dox (revision f5821a1aee77d3b9a979b42c68a79e50b5ebaefe)
1/*
2 * Copyright 2011 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Gabe Yoder, gyoder@stny.rr.com
7 *		John Scipione, jscipione@gmail.com
8 *
9 * Corresponds to:
10 *		headers/os/app/Clipboard.h	 rev 42274
11 *		src/kits/app/Clipboard.cpp	 rev 42274
12 */
13
14
15/*!
16	\file Clipboard.h
17	\ingroup app
18	\ingroup libbe
19	\brief Provides the BClipboard class.
20*/
21
22
23/*!
24	\var be_clipboard
25	\brief Global system clipboard object.
26*/
27
28
29/*!
30	\class BClipboard
31	\ingroup app
32	\ingroup libbe
33	\brief Used for short-term data storage between documents and
34		applications via copy and paste operations.
35
36	Clipboards are differentiated by their name. In order for two
37	applications to share a clipboard they simply have to create a
38	BClipboard object with the same name. However, it is rarely necessary
39	to create your own clipboard, instead you can use the \c be_clipboard
40	system clipboard object.
41
42	\remark To access the system clipboard without a BApplication object,
43	create a BClipboard object with the name "system". You should avoid
44	creating a custom clipboard with the name "system" for your own use.
45
46	To access the clipboard data call the Data() method. The BMessage object
47	returned by the Data() method has the following properties:
48		- The \c what value is unused.
49		- The clipboard data is stored in a message field typed as
50			\c B_MIME_TYPE.
51		- The MIME type of the data is used as the name of the field that
52			holds the data.
53		- Each field in the data message contains the same data with a
54			different format.
55
56	To read and write to the clipboard you must first lock the BClipboard
57	object. If you fail to lock the BClipboard object then the Data() method
58	will return \c NULL instead of a pointer to a BMessage object.
59
60	Below is an example of reading a string from the system clipboard.
61\code
62const char *string;
63int32 stringLen;
64if (be_clipboard->Lock()) {
65	// Get the clipboard BMessage
66	BMessage *clip = be_clipboard->Data();
67
68	// Read the string from the clipboard data message
69	clip->FindData("text/plain", B_MIME_TYPE, (const void **)&string,
70		&stringLen);
71
72	be_clipboard->Unlock();
73} else
74	fprintf(stderr, "could not lock clipboard.\n");
75\endcode
76
77	Below is an example of writing a string to the system clipboard.
78\code
79const char* string = "Some clipboard data";
80
81if (be_clipboard->Lock()) {
82	// Clear the clipboard data
83	be_clipboard->Clear();
84
85	// Get the clipboard data message
86	BMessage *clip = be_clipboard->Data();
87
88	// Write string data to the clipboard data message
89	clip->AddData("text/plain", B_MIME_TYPE, string, strlen(string));
90
91	// Commit the data to the clipboard
92	status = be_clipboard->Commit();
93	if (status != B_OK)
94		fprintf(stderr, "could not commit data to clipboard.\n");
95
96	be_clipboard->Unlock();
97} else
98	fprintf(stderr, "could not lock clipboard.\n");
99\endcode
100*/
101
102
103/*!
104	\fn BClipboard::BClipboard(const char *name, bool transient = false)
105	\brief Create a BClipboard object with the given \a name.
106
107	If the \a name parameter is \c NULL then the "system" BClipboard object
108	is constructed instead.
109
110	\param name The \a name of the clipboard.
111	\param transient If \c true, lose data after a reboot (currently unused).
112*/
113
114
115/*!
116	\fn BClipboard::~BClipboard()
117	\brief Destroys the BClipboard object. The clipboard data is not destroyed.
118*/
119
120
121/*!
122	\fn const char* BClipboard::Name() const
123	\brief Returns the name of the BClipboard object.
124
125	\returns The name of the clipboard.
126*/
127
128
129/*!
130	\name Commit Count Methods
131*/
132
133
134//! @{
135
136
137/*!
138	\fn uint32 BClipboard::LocalCount() const
139	\brief Returns the (locally cached) number of commits to the clipboard.
140
141	The returned value is the number of successful Commit() invocations for
142	the clipboard represented by this object, either invoked on this object
143	or another (even from another application). This method returns a locally
144	cached value, which might already be obsolete. For an up-to-date value
145	use SystemCount().
146
147	\return The number of commits to the clipboard.
148
149	\sa SystemCount()
150*/
151
152
153/*!
154	\fn uint32 BClipboard::SystemCount() const
155	\brief Returns the number of commits to the clipboard.
156
157	The returned value is the number of successful Commit() invocations for
158	the clipboard represented by this object, either invoked on this object
159	or another (even from another application). This method retrieves the
160	value directly from the system service managing the clipboards, so it is
161	more expensive, but more up-to-date than LocalCount(), which returns a
162	locally cached value.
163
164	\return The number of commits to the clipboard.
165
166	\sa LocalCount()
167*/
168
169
170//! @}
171
172
173/*!
174	\name Monitoring Methods
175*/
176
177
178//! @{
179
180
181/*!
182	\fn status_t BClipboard::StartWatching(BMessenger target)
183	\brief Start watching the BClipboard object for changes.
184
185	When a change in the clipboard occurs, most like as the result of a cut
186	or copy action, a \a B_CLIPBOARD_CHANGED message is sent to \a target.
187
188	\retval B_OK Everything went fine.
189	\retval B_BAD_VALUE \a target is invalid.
190	\retval B_ERROR An error occured.
191
192	\sa StopWatching()
193*/
194
195
196/*!
197	\fn status_t BClipboard::StopWatching(BMessenger target)
198	\brief Stop watching the BClipboard object for changes.
199
200	\retval B_OK Everything went fine.
201	\retval B_BAD_VALUE \a target is invalid.
202	\retval B_ERROR An error occurred.
203
204	\sa StartWatching()
205*/
206
207
208//! @}
209
210
211/*!
212	\name Locking Methods
213*/
214
215
216//! @{
217
218
219/*!
220	\fn bool BClipboard::Lock()
221	\brief Locks the clipboard so that no other tread can read from it or
222		write to it.
223
224	You should call Lock() before reading or writing to the clipboard.
225
226	\returns \c true if the clipboard was locked, \c false otherwise.
227
228	\sa Unlock()
229*/
230
231
232/*!
233	\fn void BClipboard::Unlock()
234	\brief Unlocks the clipboard.
235
236	\sa Lock()
237*/
238
239
240/*!
241	\fn bool BClipboard::IsLocked() const
242	\brief Returns whether or not the clipboard is locked.
243
244	\returns \c true if the clipboard is locked, \c false if it is unlocked.
245*/
246
247
248//! @}
249
250
251/*!
252	\name Clipboard Data Transaction Methods
253*/
254
255
256//! @{
257
258
259/*!
260	\fn status_t BClipboard::Clear()
261	\brief Clears out all data from the clipboard.
262
263	You should call Clear() before adding new data to the BClipboard object.
264
265	\retval B_OK Everything went find.
266	\retval B_NOT_ALLOWED The clipboard is not locked.
267	\retval B_NO_MEMORY Ran out of memory initializing the data message.
268	\retval B_ERROR Another error occurred.
269*/
270
271
272/*!
273	\fn status_t BClipboard::Commit()
274	\brief Commits the clipboard data to the BClipboard object.
275
276	\retval B_OK Everything went find.
277	\retval B_NOT_ALLOWED The clipboard is not locked.
278	\retval B_ERROR Another error occurred.
279*/
280
281
282/*!
283	\fn status_t BClipboard::Commit(bool failIfChanged)
284	\brief Commits the clipboard data to the BClipboard object with the
285		option to fail if there is a change to the clipboard data.
286
287	\param failIfChanged Whether or not to fail to commit the changes
288		if there is a change in the clipboard data.
289
290	\retval B_OK Everything went find.
291	\retval B_NOT_ALLOWED The clipboard is not locked.
292	\retval B_ERROR Another error occurred.
293*/
294
295
296/*!
297	\fn status_t BClipboard::Revert()
298	\brief Reverts the clipboard data.
299
300	The method should be used in the case that you have made a change to the
301	clipboard data message and then decide to revert the change instead of
302	committing it.
303
304	\retval B_OK Everything went find.
305	\retval B_NOT_ALLOWED The clipboard is not locked.
306	\retval B_NO_MEMORY Ran out of memory initializing the data message.
307	\retval B_ERROR Another error occurred.
308*/
309
310
311//! @}
312
313
314/*!
315	\name Clipboard Data Message Methods
316*/
317
318
319//! @{
320
321
322/*!
323	\fn BMessenger BClipboard::DataSource() const
324	\brief Gets a BMessenger object targeting the application that last
325		modified the clipboard.
326
327	The clipboard object does not need to be locked to call this method.
328
329	\returns A BMessenger object that targets the application that last
330		modified the clipboard.
331*/
332
333
334/*!
335	\fn BMessage* BClipboard::Data() const
336	\brief Gets a pointer to the BMessage object that holds the clipboard
337		data.
338
339	If the BClipboard object is not locked this method returns \c NULL.
340
341	\returns A pointer to the BMessage object that holds the clipboard
342		data or \c NULL if the clipboard is not locked.
343*/
344
345
346//! @}
347