19aa228fdSmrg/*
29aa228fdSmrg
39aa228fdSmrgCopyright 1989, 1998  The Open Group
49aa228fdSmrg
59aa228fdSmrgPermission to use, copy, modify, distribute, and sell this software and its
69aa228fdSmrgdocumentation for any purpose is hereby granted without fee, provided that
79aa228fdSmrgthe above copyright notice appear in all copies and that both that
89aa228fdSmrgcopyright notice and this permission notice appear in supporting
99aa228fdSmrgdocumentation.
109aa228fdSmrg
119aa228fdSmrgThe above copyright notice and this permission notice shall be included
129aa228fdSmrgin all copies or substantial portions of the Software.
139aa228fdSmrg
149aa228fdSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
159aa228fdSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
169aa228fdSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
179aa228fdSmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
189aa228fdSmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
199aa228fdSmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
209aa228fdSmrgOTHER DEALINGS IN THE SOFTWARE.
219aa228fdSmrg
229aa228fdSmrgExcept as contained in this notice, the name of The Open Group shall
239aa228fdSmrgnot be used in advertising or otherwise to promote the sale, use or
249aa228fdSmrgother dealings in this Software without prior written authorization
259aa228fdSmrgfrom The Open Group.
269aa228fdSmrg
279aa228fdSmrg*/
289aa228fdSmrg/*
299aa228fdSmrg * Author:  Davor Matic, MIT X Consortium
309aa228fdSmrg */
319aa228fdSmrg
329aa228fdSmrg#include <X11/IntrinsicP.h>
339aa228fdSmrg#include <X11/Xmu/StdSel.h>
349aa228fdSmrg#include <X11/Xmu/Atoms.h>
359aa228fdSmrg#include <X11/Xatom.h>
369aa228fdSmrg#include "ScaleP.h"	/* This file should be part of the Scale widget */
379aa228fdSmrg#include "Scale.h"
389aa228fdSmrg#include "CutPaste.h"
399aa228fdSmrg#include <stdio.h>
409aa228fdSmrg
419aa228fdSmrg
429aa228fdSmrgstatic Boolean
430c7e83b2SmrgConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type,
449aa228fdSmrg		 XtPointer *value, unsigned long *length, int *format)
459aa228fdSmrg{
469aa228fdSmrg    Boolean success;
479aa228fdSmrg
489aa228fdSmrg    if (*target == XA_PIXMAP || *target == XA_BITMAP) {
499aa228fdSmrg	ScaleWidget sw = (ScaleWidget) w;
509aa228fdSmrg	Pixmap *pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
519aa228fdSmrg	*pixmap = XCreatePixmap(XtDisplay(w), XtWindow(w),
520c7e83b2Smrg				sw->scale.image->width,
530c7e83b2Smrg				sw->scale.image->height,
549aa228fdSmrg				sw->scale.image->depth);
559aa228fdSmrg	XPutImage(XtDisplay(w), *pixmap, sw->scale.gc, sw->scale.image,
569aa228fdSmrg		  0, 0, 0, 0, sw->scale.image->width, sw->scale.image->height);
579aa228fdSmrg	*type = XA_PIXMAP;
589aa228fdSmrg	*value = (XtPointer) pixmap;
599aa228fdSmrg	*length = 1;
609aa228fdSmrg	*format = 32;
619aa228fdSmrg	success = True;
629aa228fdSmrg    } else {
639aa228fdSmrg	/* Xt will always respond to selection requests for the TIMESTAMP
649aa228fdSmrg	   target, so we can pass a bogus time to XmuConvertStandardSelection.
659aa228fdSmrg	   In addition to the targets provided by XmuConvertStandardSelection,
669aa228fdSmrg	   Xt converts MULTIPLE, and we convert PIXMAP and BITMAP.
679aa228fdSmrg	 */
689aa228fdSmrg	success = XmuConvertStandardSelection(w, (Time)0, selection, target,
699aa228fdSmrg					      type, (XPointer *)value, length,
709aa228fdSmrg					      format);
719aa228fdSmrg	if (success && *target == XA_TARGETS(XtDisplay(w))) {
729aa228fdSmrg	    Atom* tmp;
739aa228fdSmrg	    tmp = (Atom *) XtRealloc(*value, (*length + 3) * sizeof(Atom));
749aa228fdSmrg	    tmp[(*length)++] = XInternAtom(XtDisplay(w), "MULTIPLE", False);
759aa228fdSmrg	    tmp[(*length)++] = XA_PIXMAP;
769aa228fdSmrg	    tmp[(*length)++] = XA_BITMAP;
779aa228fdSmrg	    *value = (XtPointer) tmp;
789aa228fdSmrg	}
799aa228fdSmrg    }
809aa228fdSmrg    return success;
819aa228fdSmrg}
829aa228fdSmrg
830c7e83b2Smrgvoid
849aa228fdSmrgSWGrabSelection(Widget w, Time time)
859aa228fdSmrg{
869aa228fdSmrg    (void) XtOwnSelection(w, XA_PRIMARY, time, ConvertSelection, NULL, NULL);
879aa228fdSmrg}
889aa228fdSmrg
899aa228fdSmrg
909aa228fdSmrgstatic void
9146374b8dSmrgSelectionCallback(Widget w, _X_UNUSED XtPointer client_data,
9246374b8dSmrg                  _X_UNUSED Atom *selection, Atom *type, XtPointer value,
9346374b8dSmrg                  _X_UNUSED unsigned long *length, _X_UNUSED int *format)
949aa228fdSmrg{
959aa228fdSmrg
969aa228fdSmrg    if  (*type == XA_PIXMAP) {
979aa228fdSmrg	Pixmap *pixmap;
989aa228fdSmrg	XImage *image;
999aa228fdSmrg	Window root;
1009aa228fdSmrg	int x, y;
1019aa228fdSmrg	unsigned int width, height, border_width, depth;
1029aa228fdSmrg
1039aa228fdSmrg	pixmap = (Pixmap *) value;
1049aa228fdSmrg	XGetGeometry(XtDisplay(w), *pixmap, &root, &x, &y,
1059aa228fdSmrg		     &width, &height, &border_width, &depth);
1060c7e83b2Smrg	image = XGetImage(XtDisplay(w), *pixmap, 0, 0, width, height,
1079aa228fdSmrg			  AllPlanes, ZPixmap);
1089aa228fdSmrg	SWAutoscale(w, NULL, NULL, NULL);
1099aa228fdSmrg	SWSetImage(w, image);
1109aa228fdSmrg	XtFree(value);
1119aa228fdSmrg	XDestroyImage(image);
1129aa228fdSmrg    }
1139aa228fdSmrg}
1149aa228fdSmrg
1150c7e83b2Smrgvoid
1169aa228fdSmrgSWRequestSelection(Widget w, Time time)
1179aa228fdSmrg{
1189aa228fdSmrg    XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP, SelectionCallback, NULL,
1199aa228fdSmrg			time);
1209aa228fdSmrg}
121