Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / examples / motif / clipboard / cutpaste.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: cutpaste.c /main/4 1995/10/27 10:41:39 rswiston $ */
24 /*
25  * (c) Copyright 1989, 1990, 1991, 1992 OPEN SOFTWARE FOUNDATION, INC. 
26  * ALL RIGHTS RESERVED 
27  */
28 /*
29  * Motif Release 1.2
30  */
31
32 /******************************************************************************
33  * cutpaste.c
34  *
35  *
36  *****************************************************************************/
37
38 #include <stdio.h>
39 #include <Xm/Xm.h>
40 #include <Xm/CutPaste.h>
41 #include <Mrm/MrmPublic.h>
42
43
44 static Boolean CopyToClipboard(
45         Time time);
46 static Boolean PasteClipboard(
47         Time time );
48 static void PopupHandler(
49         Widget w,
50         Widget pw,
51         XEvent *event,
52         Boolean *ctd );
53 static void ManageCb(
54         Widget w,
55         String id,
56         XtPointer cb );
57 static void InitPopupCb(
58         Widget w,
59         String id,
60         XtPointer cb );
61 static void CutCb(
62         Widget w,
63         XtPointer cd,
64         XmPushButtonCallbackStruct *cb );
65 static void CopyCb(
66         Widget w,
67         XtPointer cd,
68         XmPushButtonCallbackStruct *cb );
69 static void PasteCb(
70         Widget w,
71         XtPointer cd,
72         XmPushButtonCallbackStruct *cb );
73 static void ExitCb(
74         Widget w,
75         XtPointer cd,
76         XtPointer cb );
77
78
79 static MrmHierarchy mrm_id;
80 static char *mrm_vec[]={"cutpaste.uid"};
81 static MrmCode mrm_class;
82 static MRMRegisterArg mrm_names[] = {
83         {"InitPopupCb", (XtPointer)InitPopupCb },
84         {"ManageCb", (XtPointer)ManageCb },
85         {"CutCb", (XtPointer)CutCb },
86         {"CopyCb", (XtPointer)CopyCb },
87         {"PasteCb", (XtPointer)PasteCb },
88         {"ExitCb", (XtPointer)ExitCb }
89 };
90
91 #define GraphFormat "MY_GRAPH_DATA"
92
93 #define MIN(x,y)  ((x) > (y) ? (y) : (x))
94
95 static XtAppContext  appContext;
96 static Widget shell;
97 static Widget *bars;
98 static Cardinal nbars;
99
100 int
101 main(
102     int argc,
103     char *argv[] )
104 {
105     Widget app_main;
106     Display *display;
107     Arg args[2];
108     Widget *children;
109
110     XtToolkitInitialize();
111     MrmInitialize ();
112     appContext = XtCreateApplicationContext();
113     display = XtOpenDisplay(appContext, NULL, argv[0], "Cutpaste",
114                         NULL, 0, &argc, argv);
115     if (display == NULL) {
116         fprintf(stderr, "%s:  Can't open display\n", argv[0]);
117         exit(1);
118     }
119     shell = XtAppCreateShell(argv[0], NULL, applicationShellWidgetClass,
120                           display, NULL, 0);
121     if (MrmOpenHierarchy (1, mrm_vec, NULL, &mrm_id) != MrmSUCCESS) exit(0);
122     MrmRegisterNames(mrm_names, XtNumber(mrm_names));
123     MrmFetchWidget (mrm_id, "appMain", shell, &app_main, &mrm_class);
124     XtManageChild(app_main);
125
126     /* define graph data format */
127
128     while (XmClipboardRegisterFormat (display, GraphFormat, 32) ==
129                                         XmClipboardLocked)  { }
130
131     XtSetArg (args[0], XmNchildren, &children);
132     XtSetArg (args[1], XmNnumChildren, &nbars);
133     XtGetValues (XtNameToWidget (shell, "*graphForm"), args, 2);
134     bars = (Widget *) XtMalloc (nbars * sizeof(Widget));
135     memcpy (bars, children, nbars * sizeof(Widget));
136
137     XtRealizeWidget(shell);
138
139     XtAppMainLoop(appContext);
140 }
141
142 static void
143 ManageCb(
144     Widget w,
145     String id,
146     XtPointer cb )
147
148 {
149     XtManageChild (XtNameToWidget (shell, id));
150 }
151
152 static void
153 ExitCb(
154     Widget w,
155     XtPointer cd,
156     XtPointer cb )
157 {
158     exit(0);
159 }
160
161 /*****************************************************************
162  *
163  * PopupMenu support
164  *
165  *****************************************************************/
166
167 static void
168 InitPopupCb(
169     Widget w,
170     String id,
171     XtPointer cb )
172 {
173     Widget popupWindow = XtNameToWidget (shell, id);
174
175     XtAddEventHandler (popupWindow, ButtonPressMask, False,
176                 (XtEventHandler)PopupHandler, (XtPointer) w);
177 }
178
179 static void
180 PopupHandler (
181     Widget w,
182     Widget pw,
183     XEvent *event,
184     Boolean *ctd )
185 {
186     if (((XButtonEvent *)event)->button != Button3) return;
187
188     XmMenuPosition((Widget) pw, (XButtonEvent *)event);
189     XtManageChild ((Widget) pw);
190 }
191
192 /*****************************************************************
193  *
194  * Clipboard support
195  *
196  *****************************************************************/
197
198 static void
199 CutCb(
200     Widget w,
201     XtPointer cd,
202     XmPushButtonCallbackStruct *cb )
203 {
204     XButtonEvent *be = (XButtonEvent *)cb->event;
205     Arg args[1];
206     int i;
207
208     if (CopyToClipboard(be->time) == True) {
209
210         /* clear graph data */
211
212         XtSetArg (args[0], XmNvalue, 0);
213         for (i=0; i < nbars; i++)
214             XtSetValues (bars[i], args, 1);
215     }
216     else {
217         XBell (XtDisplay(w), 0);
218     }
219 }
220
221 static void
222 CopyCb(
223     Widget w,
224     XtPointer cd,
225     XmPushButtonCallbackStruct *cb )
226 {
227     XButtonEvent *be = (XButtonEvent *)cb->event;
228
229     if (CopyToClipboard(be->time) != True) {
230         XBell (XtDisplay(w), 0);
231     }
232 }
233
234 static void
235 PasteCb(
236     Widget w,
237     XtPointer cd,
238     XmPushButtonCallbackStruct *cb )
239 {
240     XButtonEvent *be = (XButtonEvent *)cb->event;
241
242     if (PasteClipboard(be->time) != True) {
243         XBell (XtDisplay(w), 0);
244     }
245 }
246
247 static Boolean
248 CopyToClipboard(
249     Time time )
250 {
251     Display *dpy = XtDisplay (shell);
252     Window window = XtWindow (shell);
253     long itemId = 0;
254     XmString clipLabel;
255     long *graphData;
256     int value;
257     int i;
258     Arg args[1];
259
260     if (XmClipboardLock (dpy, window) == XmClipboardLocked)
261         return (False);
262
263     clipLabel = XmStringCreateLtoR ("cutpaste", XmFONTLIST_DEFAULT_TAG);
264
265     XmClipboardStartCopy ( dpy, window, clipLabel, time, NULL, NULL, &itemId);
266
267     /* copy graph data */
268
269     graphData = (long *)XtMalloc (nbars * sizeof (long));
270     XtSetArg (args[0], XmNvalue, &value);
271     for (i=0; i < nbars; i++) {
272         XtGetValues (bars[i], args, 1);
273         graphData[i] = (long)value;
274     }
275
276     XmClipboardCopy (dpy, window, itemId, GraphFormat, (XtPointer)graphData,
277                         (nbars * sizeof(long)), 0, NULL);
278
279     XmClipboardEndCopy (dpy, window, itemId);
280
281     XmClipboardUnlock (dpy, window, False);
282
283     XtFree ((char *)clipLabel);
284     XtFree ((char *)graphData);
285
286     return (True);
287 }
288
289 static Boolean
290 PasteClipboard(
291     Time time )
292 {
293     Display *dpy = XtDisplay (shell);
294     Window window = XtWindow (shell);
295     unsigned long length = 0;
296     long *graphData;
297     int i;
298     int setBars;
299     Arg args[1];
300
301     if (XmClipboardStartRetrieve (dpy, window, time) == XmClipboardLocked)
302         return (False);
303
304     XmClipboardInquireLength(dpy, window, GraphFormat, &length);
305
306     if (length == 0) {
307         XmClipboardEndRetrieve (dpy, window);
308         return (False);
309     }
310
311     graphData = (long *)XtMalloc (length * sizeof (long));
312
313     XmClipboardRetrieve(dpy, window, GraphFormat, graphData, length,
314                         NULL, NULL);
315
316     XmClipboardEndRetrieve(dpy, window);
317
318     setBars = MIN (nbars, length);
319     for (i = 0; i < setBars; i++) {
320         XtSetArg (args[0], XmNvalue, graphData[i]);
321         XtSetValues (bars[i], args, 1);
322     }
323
324     XtFree ((char *)graphData);
325
326     return (True);
327 }
328