1eaef79e5Smrg/*
2eaef79e5Smrg
3eaef79e5SmrgCopyright 1989, 1998  The Open Group
4eaef79e5Smrg
5eaef79e5SmrgPermission to use, copy, modify, distribute, and sell this software and its
6eaef79e5Smrgdocumentation for any purpose is hereby granted without fee, provided that
7eaef79e5Smrgthe above copyright notice appear in all copies and that both that
8eaef79e5Smrgcopyright notice and this permission notice appear in supporting
9eaef79e5Smrgdocumentation.
10eaef79e5Smrg
11eaef79e5SmrgThe above copyright notice and this permission notice shall be included
12eaef79e5Smrgin all copies or substantial portions of the Software.
13eaef79e5Smrg
14eaef79e5SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15eaef79e5SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16eaef79e5SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17eaef79e5SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18eaef79e5SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19eaef79e5SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20eaef79e5SmrgOTHER DEALINGS IN THE SOFTWARE.
21eaef79e5Smrg
22eaef79e5SmrgExcept as contained in this notice, the name of The Open Group shall
23eaef79e5Smrgnot be used in advertising or otherwise to promote the sale, use or
24eaef79e5Smrgother dealings in this Software without prior written authorization
25eaef79e5Smrgfrom The Open Group.
26eaef79e5Smrg
27eaef79e5Smrg*/
28eaef79e5Smrg
29eaef79e5Smrg/*
30eaef79e5Smrg * Author:  Davor Matic, MIT X Consortium
31eaef79e5Smrg */
32eaef79e5Smrg
33433d0511Smrg#ifdef HAVE_CONFIG_H
34433d0511Smrg# include "config.h"
35433d0511Smrg#endif
36433d0511Smrg
37eaef79e5Smrg#include <X11/IntrinsicP.h>
38eaef79e5Smrg#include <X11/StringDefs.h>
39eaef79e5Smrg#include <X11/Xatom.h>
40eaef79e5Smrg#include "BitmapP.h"
41cbc4e2beSmrg
42eaef79e5Smrg#include <stdio.h>
43eaef79e5Smrg#include <math.h>
44eaef79e5Smrg
45eaef79e5Smrg#define min(x, y)                     (((x) < (y)) ? (x) : (y))
46eaef79e5Smrg#define max(x, y)                     (((x) > (y)) ? (x) : (y))
47eaef79e5Smrg
48eaef79e5Smrg
49eaef79e5Smrg
50eaef79e5Smrg/*****************************************************************************
51eaef79e5Smrg *                               Cut and Paste                               *
52eaef79e5Smrg *****************************************************************************/
53eaef79e5Smrg
54eaef79e5Smrg/* ARGSUSED */
55cbc4e2beSmrgstatic Boolean
56cbc4e2beSmrgConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type,
57eaef79e5Smrg		 XtPointer *val_ret, unsigned long *length, int *format)
58eaef79e5Smrg{
59eaef79e5Smrg    XPointer *value = (XPointer *)val_ret;
60eaef79e5Smrg    BitmapWidget BW = (BitmapWidget) w;
61eaef79e5Smrg    Pixmap *pixmap;
62eaef79e5Smrg    char *data;
63eaef79e5Smrg    XImage *image;
64eaef79e5Smrg    Dimension width, height;
65cbc4e2beSmrg
66eaef79e5Smrg    switch (*target) {
67eaef79e5Smrg
68eaef79e5Smrg/*  XA_TARGETS undefined ?!?
69eaef79e5Smrg
70eaef79e5Smrg    case XA_TARGETS:
71eaef79e5Smrg	*type = XA_ATOM;
72eaef79e5Smrg	*value = (XPointer) bitmapClassRec.bitmap_class.targets;
73eaef79e5Smrg	*length = bitmapClassRec.bitmap_class.num_targets;
74eaef79e5Smrg	*format = 32;
75eaef79e5Smrg	return True;
76eaef79e5Smrg
77eaef79e5Smrg*/
78eaef79e5Smrg
79eaef79e5Smrg    case XA_BITMAP:
80eaef79e5Smrg    case XA_PIXMAP:
81eaef79e5Smrg	if (BWQueryMarked(w)) {
82eaef79e5Smrg	  width = BW->bitmap.mark.to_x - BW->bitmap.mark.from_x + 1;
83eaef79e5Smrg	  height = BW->bitmap.mark.to_y - BW->bitmap.mark.from_y + 1;
84eaef79e5Smrg	  data = CreateCleanData(Length(width, height));
85eaef79e5Smrg	  image = CreateBitmapImage(BW, data, width, height);
86cbc4e2beSmrg	  CopyImageData(BW->bitmap.image, image,
87eaef79e5Smrg			BW->bitmap.mark.from_x, BW->bitmap.mark.from_y,
88eaef79e5Smrg			BW->bitmap.mark.to_x, BW->bitmap.mark.to_y, 0, 0);
89eaef79e5Smrg	  pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
90eaef79e5Smrg	  *pixmap = GetPixmap(BW, image);
91eaef79e5Smrg	  DestroyBitmapImage(&image);
92eaef79e5Smrg	}
93eaef79e5Smrg	else if (BWQueryStored(w)) {
94eaef79e5Smrg	  pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
95eaef79e5Smrg	  *pixmap = GetPixmap(BW, BW->bitmap.storage);
96eaef79e5Smrg	}
97eaef79e5Smrg	else return False;
98eaef79e5Smrg	*type = XA_PIXMAP;
99eaef79e5Smrg	*value = (XPointer) pixmap;
100eaef79e5Smrg	*length = 1;
101eaef79e5Smrg	*format = 32;
102eaef79e5Smrg	return True;
103eaef79e5Smrg
104eaef79e5Smrg    default:
105eaef79e5Smrg	return False;
106eaef79e5Smrg    }
107eaef79e5Smrg}
108eaef79e5Smrg
109eaef79e5Smrg/* ARGSUSED */
110cbc4e2beSmrgstatic void
111eaef79e5SmrgLoseSelection(Widget w, Atom selection)
112eaef79e5Smrg{
113eaef79e5Smrg    BitmapWidget BW = (BitmapWidget) w;
114eaef79e5Smrg
115eaef79e5Smrg    if (DEBUG)
116eaef79e5Smrg	fprintf(stderr, "Lost Selection\n");
117eaef79e5Smrg    BW->bitmap.selection.own = False;
118eaef79e5Smrg
119eaef79e5Smrg    BWUnmark(w);
120eaef79e5Smrg}
121eaef79e5Smrg
122eaef79e5Smrg/* ARGSUSED */
123cbc4e2beSmrgstatic void
124eaef79e5SmrgSelectionDone(Widget w, Atom *selection, Atom *target)
125eaef79e5Smrg{
126eaef79e5Smrg/*  Done Automatically ?!?
127eaef79e5Smrg
128cbc4e2beSmrg    BitmapWidget BW = (BitmapWidget) w;
129eaef79e5Smrg
130eaef79e5Smrg    if (*target != XA_TARGETS)
131eaef79e5Smrg	XtFree(BW->bitmap.value);
132eaef79e5Smrg
133eaef79e5Smrg*/
134eaef79e5Smrg}
135eaef79e5Smrg
136cbc4e2beSmrgvoid
137eaef79e5SmrgBWGrabSelection(Widget w, Time btime)
138eaef79e5Smrg{
139eaef79e5Smrg    BitmapWidget BW = (BitmapWidget) w;
140eaef79e5Smrg
141eaef79e5Smrg    BW->bitmap.selection.own = XtOwnSelection(w, XA_PRIMARY, btime,
142cbc4e2beSmrg					      ConvertSelection,
143cbc4e2beSmrg					      (XtLoseSelectionProc)LoseSelection,
144eaef79e5Smrg					      SelectionDone);
145eaef79e5Smrg	if (DEBUG && BW->bitmap.selection.own)
146eaef79e5Smrg	    fprintf(stderr, "Own the selection\n");
147eaef79e5Smrg}
148eaef79e5Smrg
149eaef79e5Smrg
150eaef79e5Smrg/* ARGSUSED */
151cbc4e2beSmrgstatic void
152cbc4e2beSmrgSelectionCallback(Widget w, XtPointer cldat, Atom *selection, Atom *type,
153eaef79e5Smrg		  XtPointer val, unsigned long *length, int *format)
154eaef79e5Smrg{
155eaef79e5Smrg    XPointer value = (XPointer)val;
156eaef79e5Smrg    BitmapWidget BW = (BitmapWidget) w;
157eaef79e5Smrg    Pixmap *pixmap;
158eaef79e5Smrg
159eaef79e5Smrg   switch (*type) {
160cbc4e2beSmrg
161eaef79e5Smrg    case XA_BITMAP:
162eaef79e5Smrg    case XA_PIXMAP:
163eaef79e5Smrg	DestroyBitmapImage(&BW->bitmap.storage);
164eaef79e5Smrg	pixmap = (Pixmap *) value;
165eaef79e5Smrg	BW->bitmap.storage = GetImage(BW, *pixmap);
166eaef79e5Smrg	XFree((char *)pixmap);
167eaef79e5Smrg	break;
168cbc4e2beSmrg
169eaef79e5Smrg    default:
170eaef79e5Smrg	XtWarning(" selection request failed.  BitmapWidget");
171eaef79e5Smrg	break;
172eaef79e5Smrg    }
173eaef79e5Smrg
174eaef79e5Smrg    BW->bitmap.selection.limbo = FALSE;
175eaef79e5Smrg}
176eaef79e5Smrg
177cbc4e2beSmrgvoid
178eaef79e5SmrgBWRequestSelection(Widget w, Time btime, Boolean wait)
179eaef79e5Smrg{
180eaef79e5Smrg  BitmapWidget BW = (BitmapWidget) w;
181cbc4e2beSmrg
182eaef79e5Smrg  if (BW->bitmap.selection.own)
183eaef79e5Smrg    BWStore(w);
184eaef79e5Smrg  else {
185eaef79e5Smrg    XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP,
186eaef79e5Smrg			SelectionCallback, NULL, btime);
187cbc4e2beSmrg
188eaef79e5Smrg    BW->bitmap.selection.limbo = TRUE;
189cbc4e2beSmrg
190eaef79e5Smrg    if (wait)
191eaef79e5Smrg      while (BW->bitmap.selection.limbo) {
192eaef79e5Smrg	XEvent event;
193eaef79e5Smrg	XtNextEvent(&event);
194eaef79e5Smrg	XtDispatchEvent(&event);
195eaef79e5Smrg      }
196eaef79e5Smrg  }
197eaef79e5Smrg}
198eaef79e5Smrg
199eaef79e5Smrg/* ARGSUSED */
200eaef79e5Smrg/* Returns true if there is a transferable selection */
201cbc4e2beSmrgBoolean
202eaef79e5SmrgBWQuerySelection(Widget w, Time btime)
203eaef79e5Smrg{
204eaef79e5Smrg/* To be written.  XA_TARGETS to be used.  So far undefined ?!? */
205eaef79e5Smrg
206eaef79e5Smrg  return True;
207eaef79e5Smrg}
208eaef79e5Smrg/*****************************************************************************/
209