1 #include <Xm/CutPaste.h>
2 #include <Xm/RowColumn.h>
3 #include <Xm/PushB.h>
4 #include <stdio.h>
5 
6 static void to_clipbd(), from_clipbd();
7 
8 int
main(int argc,char ** argv)9 main(int argc, char **argv)
10 {
11     Widget toplevel, rowcol, button;
12     XtAppContext app;
13 
14     XtSetLanguageProc(NULL, NULL, NULL);
15 
16     toplevel = XtVaAppInitialize(&app, "Demo", NULL, 0,
17 				 &argc, argv, NULL, NULL);
18 
19     rowcol = XtVaCreateWidget("rowcol", xmRowColumnWidgetClass,
20 			      toplevel, NULL);
21 
22     button = XtVaCreateManagedWidget("button1", xmPushButtonWidgetClass,
23 				     rowcol, XtVaTypedArg, XmNlabelString,
24 				     XmRString, "Copy To Clipboard", 18,
25 				     NULL);
26 
27     XtAddCallback(button, XmNactivateCallback, to_clipbd, "text");
28 
29     button = XtVaCreateManagedWidget("button2", xmPushButtonWidgetClass,
30 				     rowcol, XtVaTypedArg, XmNlabelString,
31 				     XmRString, "Retrieve From Clipboard", 24,
32 				     NULL);
33 
34     XtAddCallback(button, XmNactivateCallback, from_clipbd, NULL);
35 
36     XtManageChild(rowcol);
37     XtRealizeWidget(toplevel);
38 
39 {
40     static XtWidgetGeometry Expected[] = {
41    CWWidth | CWHeight            ,   50,   50,  156,   59, 0,0,0, /* rowcol */
42    CWWidth | CWHeight | CWX | CWY,    3,    3,  150,   25, 0,0,0, /* button1 */
43    CWWidth | CWHeight | CWX | CWY,    3,   31,  150,   25, 0,0,0, /* button2 */
44     };
45     PrintDetails(    toplevel ,Expected);
46 };
47    LessTifTestMainLoop(    toplevel );
48    exit(0);
49 }
50 
51 static void
to_clipbd(Widget w,XtPointer client_data,XtPointer call_data)52 to_clipbd(Widget w, XtPointer client_data, XtPointer call_data)
53 {
54     long item_id = 0;
55     int status;
56     XmString clip_label;
57     char buf[40];
58     static int cnt;
59     Display *dpy = XtDisplayOfObject(w);
60     Window window = XtWindowOfObject(w);
61     char *data = (char *)client_data;
62 
63     sprintf(buf, "%s-%30d", data, ++cnt);
64 
65     clip_label = XmStringCreateLocalized("to_clipbd");
66 
67     do {
68 	status = XmClipboardStartCopy(dpy, window, clip_label, CurrentTime,
69 				      NULL, NULL, &item_id);
70     } while (status == ClipboardLocked);
71 
72     XmStringFree(clip_label);
73 
74     do {
75 	status = XmClipboardCopy(dpy, window, item_id, "STRING",
76 				buf, (long)strlen(buf)+1, cnt, NULL);
77     } while (status == ClipboardLocked);
78 
79 
80     do {
81 	status = XmClipboardEndCopy(dpy, window, item_id);
82     } while (status == ClipboardLocked);
83 
84     printf("copied: '%s' to clipboard\n", buf);
85 }
86 
87 static void
from_clipbd(Widget w,XtPointer client_data,XtPointer call_data)88 from_clipbd(Widget w, XtPointer client_data, XtPointer call_data)
89 {
90     int status;
91     unsigned total_bytes;
92     unsigned long received;
93     char buf[16], *data = NULL;
94     Display *dpy = XtDisplayOfObject(w);
95     Window window = XtWindowOfObject(w);
96 
97     do {
98 	status = XmClipboardStartRetrieve(dpy, window, CurrentTime);
99     } while (status == ClipboardLocked);
100 
101     data = XtMalloc(1);
102     total_bytes = 1;
103 
104     do {
105 	status = XmClipboardRetrieve(dpy, window, "STRING",
106 				     buf, sizeof(buf), &received, NULL);
107 
108 	if (!(data = XtRealloc(data, total_bytes + received))) {
109 	    XtError("Can't allocate space for data");
110 	    break;
111 	}
112 
113 	strncpy(&data[total_bytes-1], buf, received);
114 	total_bytes += received;
115 
116     } while (status == ClipboardTruncate);
117 
118     if (data)
119 	data[total_bytes] = 0;
120 
121     if (status == ClipboardSuccess)
122 	printf("Retrieved '%s' from clipboard.\n", data);
123 
124     status = XmClipboardEndRetrieve(dpy, window);
125 }
126 
127