1/*
2
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28/*
29 * Author:  Davor Matic, MIT X Consortium
30 */
31
32#include <X11/IntrinsicP.h>
33#include <X11/Xmu/StdSel.h>
34#include <X11/Xmu/Atoms.h>
35#include <X11/Xatom.h>
36#include "ScaleP.h"	/* This file should be part of the Scale widget */
37#include "Scale.h"
38#include "CutPaste.h"
39#include <stdio.h>
40
41
42static Boolean
43ConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type,
44		 XtPointer *value, unsigned long *length, int *format)
45{
46    Boolean success;
47
48    if (*target == XA_PIXMAP || *target == XA_BITMAP) {
49	ScaleWidget sw = (ScaleWidget) w;
50	Pixmap *pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
51	*pixmap = XCreatePixmap(XtDisplay(w), XtWindow(w),
52				sw->scale.image->width,
53				sw->scale.image->height,
54				sw->scale.image->depth);
55	XPutImage(XtDisplay(w), *pixmap, sw->scale.gc, sw->scale.image,
56		  0, 0, 0, 0, sw->scale.image->width, sw->scale.image->height);
57	*type = XA_PIXMAP;
58	*value = (XtPointer) pixmap;
59	*length = 1;
60	*format = 32;
61	success = True;
62    } else {
63	/* Xt will always respond to selection requests for the TIMESTAMP
64	   target, so we can pass a bogus time to XmuConvertStandardSelection.
65	   In addition to the targets provided by XmuConvertStandardSelection,
66	   Xt converts MULTIPLE, and we convert PIXMAP and BITMAP.
67	 */
68	success = XmuConvertStandardSelection(w, (Time)0, selection, target,
69					      type, (XPointer *)value, length,
70					      format);
71	if (success && *target == XA_TARGETS(XtDisplay(w))) {
72	    Atom* tmp;
73	    tmp = (Atom *) XtRealloc(*value, (*length + 3) * sizeof(Atom));
74	    tmp[(*length)++] = XInternAtom(XtDisplay(w), "MULTIPLE", False);
75	    tmp[(*length)++] = XA_PIXMAP;
76	    tmp[(*length)++] = XA_BITMAP;
77	    *value = (XtPointer) tmp;
78	}
79    }
80    return success;
81}
82
83void
84SWGrabSelection(Widget w, Time time)
85{
86    (void) XtOwnSelection(w, XA_PRIMARY, time, ConvertSelection, NULL, NULL);
87}
88
89
90static void
91SelectionCallback(Widget w, _X_UNUSED XtPointer client_data,
92                  _X_UNUSED Atom *selection, Atom *type, XtPointer value,
93                  _X_UNUSED unsigned long *length, _X_UNUSED int *format)
94{
95
96    if  (*type == XA_PIXMAP) {
97	Pixmap *pixmap;
98	XImage *image;
99	Window root;
100	int x, y;
101	unsigned int width, height, border_width, depth;
102
103	pixmap = (Pixmap *) value;
104	XGetGeometry(XtDisplay(w), *pixmap, &root, &x, &y,
105		     &width, &height, &border_width, &depth);
106	image = XGetImage(XtDisplay(w), *pixmap, 0, 0, width, height,
107			  AllPlanes, ZPixmap);
108	SWAutoscale(w, NULL, NULL, NULL);
109	SWSetImage(w, image);
110	XtFree(value);
111	XDestroyImage(image);
112    }
113}
114
115void
116SWRequestSelection(Widget w, Time time)
117{
118    XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP, SelectionCallback, NULL,
119			time);
120}
121