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