2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: cutpaste.c /main/4 1995/10/27 10:41:39 rswiston $ */
25 * (c) Copyright 1989, 1990, 1991, 1992 OPEN SOFTWARE FOUNDATION, INC.
32 /******************************************************************************
36 *****************************************************************************/
40 #include <Xm/CutPaste.h>
41 #include <Mrm/MrmPublic.h>
44 static Boolean CopyToClipboard(
46 static Boolean PasteClipboard(
48 static void PopupHandler(
57 static void InitPopupCb(
64 XmPushButtonCallbackStruct *cb );
68 XmPushButtonCallbackStruct *cb );
72 XmPushButtonCallbackStruct *cb );
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 }
91 #define GraphFormat "MY_GRAPH_DATA"
93 #define MIN(x,y) ((x) > (y) ? (y) : (x))
95 static XtAppContext appContext;
98 static Cardinal nbars;
110 XtToolkitInitialize();
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]);
119 shell = XtAppCreateShell(argv[0], NULL, applicationShellWidgetClass,
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);
126 /* define graph data format */
128 while (XmClipboardRegisterFormat (display, GraphFormat, 32) ==
129 XmClipboardLocked) { }
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));
137 XtRealizeWidget(shell);
139 XtAppMainLoop(appContext);
149 XtManageChild (XtNameToWidget (shell, id));
161 /*****************************************************************
165 *****************************************************************/
173 Widget popupWindow = XtNameToWidget (shell, id);
175 XtAddEventHandler (popupWindow, ButtonPressMask, False,
176 (XtEventHandler)PopupHandler, (XtPointer) w);
186 if (((XButtonEvent *)event)->button != Button3) return;
188 XmMenuPosition((Widget) pw, (XButtonEvent *)event);
189 XtManageChild ((Widget) pw);
192 /*****************************************************************
196 *****************************************************************/
202 XmPushButtonCallbackStruct *cb )
204 XButtonEvent *be = (XButtonEvent *)cb->event;
208 if (CopyToClipboard(be->time) == True) {
210 /* clear graph data */
212 XtSetArg (args[0], XmNvalue, 0);
213 for (i=0; i < nbars; i++)
214 XtSetValues (bars[i], args, 1);
217 XBell (XtDisplay(w), 0);
225 XmPushButtonCallbackStruct *cb )
227 XButtonEvent *be = (XButtonEvent *)cb->event;
229 if (CopyToClipboard(be->time) != True) {
230 XBell (XtDisplay(w), 0);
238 XmPushButtonCallbackStruct *cb )
240 XButtonEvent *be = (XButtonEvent *)cb->event;
242 if (PasteClipboard(be->time) != True) {
243 XBell (XtDisplay(w), 0);
251 Display *dpy = XtDisplay (shell);
252 Window window = XtWindow (shell);
260 if (XmClipboardLock (dpy, window) == XmClipboardLocked)
263 clipLabel = XmStringCreateLtoR ("cutpaste", XmFONTLIST_DEFAULT_TAG);
265 XmClipboardStartCopy ( dpy, window, clipLabel, time, NULL, NULL, &itemId);
267 /* copy graph data */
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;
276 XmClipboardCopy (dpy, window, itemId, GraphFormat, (XtPointer)graphData,
277 (nbars * sizeof(long)), 0, NULL);
279 XmClipboardEndCopy (dpy, window, itemId);
281 XmClipboardUnlock (dpy, window, False);
283 XtFree ((char *)clipLabel);
284 XtFree ((char *)graphData);
293 Display *dpy = XtDisplay (shell);
294 Window window = XtWindow (shell);
295 unsigned long length = 0;
301 if (XmClipboardStartRetrieve (dpy, window, time) == XmClipboardLocked)
304 XmClipboardInquireLength(dpy, window, GraphFormat, &length);
307 XmClipboardEndRetrieve (dpy, window);
311 graphData = (long *)XtMalloc (length * sizeof (long));
313 XmClipboardRetrieve(dpy, window, GraphFormat, graphData, length,
316 XmClipboardEndRetrieve(dpy, window);
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);
324 XtFree ((char *)graphData);