• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using the Pasteboard to Copy and Paste (C/C++)
2
3## When to Use
4
5The pasteboard allows you to copy and paste data of the plain text, hypertext, and URI.
6
7## Basic Concepts
8
9- **OH_PasteboardObserver**: pasteboard observer object, which is used to listen for data changes on the pasteboard.
10- **OH_Pasteboard**: pasteboard object, which is used to query and write.
11- **OH_UdmfData**: unified data object. For details, see [UDMF Development (C/C++)](../../database/native-unified-data-management-framework-guidelines.md).
12
13## Constraints
14
15- The size of data written to the pasteboard at a time cannot exceed 128 MB.
16- To ensure the accuracy of the pasteboard data, only one copy can be performed at a time.
17- Currently, supported data types include **OH_UdsPlainText** (plain text), **OH_UdsHtml** (hypertext markup language), **OH_UdsFileUri** (file URI). **OH_UdsPixelMap** (pixel map), **OH_UdsHyperlink** (hyperlink), **OH_UdsAppItem** (application icon), and custom type. The data types supported by JS APIs are different from those supported by NDK APIs. You need to match the data types with the corresponding APIs during usage. For details, see [Using the Pasteboard to Copy and Paste](../pasteboard/use_pasteboard_to_copy_and_paste.md).
18- When you copy and paste data of a custom type, the specified type name cannot be the same as an existing one.
19- Since API version 12, [permission control](get-pastedata-permission-guidelines.md) is added to the pasteboard reading API to enhance user privacy protection.
20- The copy and paste APIs, [setUnifiedData](../../reference/apis-basic-services-kit/js-apis-pasteboard.md#setunifieddata12) and [getUnifiedData](../../reference/apis-basic-services-kit/js-apis-pasteboard.md#getunifieddata12), added in API version 12 are independent of the **OH_Pasteboard_SetData** and **OH_Pasteboard_GetData** APIs mentioned in this topic. Use the corresponding APIs when writing and reading data.
21
22## Available APIs
23
24For details about more APIs and their usage, see [Pasteboard](../../reference/apis-basic-services-kit/_pasteboard.md).
25
26| API                                                    | Description                                                   |
27| ------------------------------------------------------------ | ------------------------------------------------------- |
28| OH_PasteboardObserver* OH_PasteboardObserver_Create()        | Creates a pasteboard observer object.                     |
29| OH_PasteboardObserver_Destroy(OH_PasteboardObserver* observer) | Destroys the pasteboard observer object.                         |
30| int OH_PasteboardObserver_SetData(OH_PasteboardObserver* observer, void* context, const Pasteboard_Notify callback, const Pasteboard_Finalize finalize) | Sets the callback used to return data changes to the pasteboard observer object. |
31| OH_Pasteboard* OH_Pasteboard_Create()                        | Creates a pasteboard instance.                                   |
32| void OH_Pasteboard_Destroy(OH_Pasteboard* pasteboard)        | Destroys the pasteboard instance.                                       |
33| int OH_Pasteboard_Subscribe(OH_Pasteboard* pasteboard, int type, const OH_PasteboardObserver* observer) | Subscribes to the pasteboard observer.                                 |
34| int OH_Pasteboard_Unsubscribe(OH_Pasteboard* pasteboard, int type, const OH_PasteboardObserver* observer) | Unsubscribes from the pasteboard observer.                           |
35| bool OH_Pasteboard_IsRemoteData(OH_Pasteboard* pasteboard)   | Checks whether the pasteboard data comes from remote devices.                   |
36| int OH_Pasteboard_GetDataSource(OH_Pasteboard* pasteboard, char* source, unsigned int len) | Obtains the pasteboard data source.                             |
37| bool OH_Pasteboard_HasType(OH_Pasteboard* pasteboard, const char* type) | Checks whether the pasteboard contains data of the specified type.                     |
38| bool OH_Pasteboard_HasData(OH_Pasteboard* pasteboard)        | Checks whether the pasteboard contains data.                               |
39| OH_UdmfData* OH_Pasteboard_GetData(OH_Pasteboard* pasteboard, int* status) | Obtains data from the pasteboard.                                   |
40| int OH_Pasteboard_SetData(OH_Pasteboard* pasteboard, OH_UdmfData* data) | Writes data to the pasteboard.                                   |
41| int OH_Pasteboard_ClearData(OH_Pasteboard* pasteboard)       | Clears data from the pasteboard.                                   |
42| void (\*Pasteboard_Notify)(void\* context, Pasteboard_NotifyType type) | Notifies data changes in the pasteboard.                             |
43| void (\*Pasteboard_Finalize)(void\* context)                 | Releases context resources when the pasteboard observer object is destroyed.|
44
45## How to Develop
46
471. Add dynamic link libraries.
48
49   ```CMake
50   # Add the following libraries to **CMakeLists.txt**.
51   libudmf.so
52   libpasteboard.so
53   ```
54
552. Include header files.
56
57   ```c
58   #include <cstdio>
59   #include <database/pasteboard/oh_pasteboard.h>
60   #include <database/udmf/udmf.h>
61   #include <database/udmf/uds.h>
62   ```
63
643. Define the callback for listening for pasteboard data changes.
65
66   ```c
67   // Define the callback for notifying data changes in the pasteboard.
68   static void Pasteboard_Notify_impl2(void* context, Pasteboard_NotifyType type)
69   {
70     printf("Pasteboard_NotifyType, type: %d", type);
71   }
72   // Define the callback for notifying when the pasteboard observer object is destroyed.
73   static void Pasteboard_Finalize_impl2(void* context)
74   {
75     printf("callback: Pasteboard_Finalize");
76   }
77   ```
78
794. Subscribe to the pasteboard observer.
80
81   ```c
82   // 1. Create a pasteboard instance.
83   OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
84   // 2. Create a pasteboard observer instance.
85   OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
86   // 3. Set two callbacks to the pasteboard observer instance.
87   OH_PasteboardObserver_SetData(observer, (void* )pasteboard, Pasteboard_Notify_impl2, Pasteboard_Finalize_impl2);
88   // 4. Subscribe to local data changes on the pasteboard.
89   OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
90   ```
91
925. Write data to the pasteboard.
93
94   ```c
95   // 1. Create a pasteboard instance.
96   OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
97
98   // 2. Create an OH_UdmfRecord object and add text data to it.
99   OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
100   OH_UdsPlainText_SetContent(plainText, "Hello world!");
101   OH_UdmfRecord* record = OH_UdmfRecord_Create();
102   OH_UdmfRecord_AddPlainText(record, plainText);
103
104   // 3. Create an OH_UdmfData object and add OH_UdmfRecord to it.
105   OH_UdmfData* data = OH_UdmfData_Create();
106   OH_UdmfData_AddRecord(data, record);
107
108   // 4. Write data to the pasteboard.
109   OH_Pasteboard_SetData(pasteboard, data);
110
111   // 5. Destroy the pointer when the object is no longer required.
112   OH_UdsPlainText_Destroy(plainText);
113   OH_UdmfRecord_Destroy(record);
114   OH_UdmfData_Destroy(data);
115   OH_Pasteboard_Destroy(pasteboard);
116   ```
117
1186. Read data from the pasteboard.
119
120   ```c
121   // 1. Create a pasteboard instance.
122   OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
123   // 2. Check whether the pasteboard contains text data.
124   bool hasPlainTextData = OH_Pasteboard_HasType(pasteboard, "text/plain");
125   if (hasPlainTextData) {
126     // 3. Obtain the unified data OH_UdmfData from the pasteboard.
127     int ret = 0;
128     OH_UdmfData* udmfData = OH_Pasteboard_GetData(pasteboard, &ret);
129     // 4. Obtain the first data record from OH_UdmfData.
130     OH_UdmfRecord* record = OH_UdmfData_GetRecord(udmfData, 0);
131     // 5. Obtain the text data from the data record.
132     OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
133     OH_UdmfRecord_GetPlainText(record, plainText);
134     const char* content = OH_UdsPlainText_GetContent(plainText);
135     printf("Get plain text success. content: %s", content);
136     // 5. Destroy the pointer when the object is no longer required.
137     OH_UdsPlainText_Destroy(plainText);
138     OH_UdmfData_Destroy(udmfData);
139   }
140   OH_Pasteboard_Destroy(pasteboard);
141   ```
142