CutPaste.c revision 2b32c8f7
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/*
30 * Author:  Davor Matic, MIT X Consortium
31 */
32
33#include <X11/IntrinsicP.h>
34#include <X11/StringDefs.h>
35#include <X11/Xatom.h>
36#include "BitmapP.h"
37
38#include <stdio.h>
39#include <math.h>
40
41#define min(x, y)                     (((x) < (y)) ? (x) : (y))
42#define max(x, y)                     (((x) > (y)) ? (x) : (y))
43
44
45
46/*****************************************************************************
47 *                               Cut and Paste                               *
48 *****************************************************************************/
49
50/* ARGSUSED */
51static Boolean
52ConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type,
53		 XtPointer *val_ret, unsigned long *length, int *format)
54{
55    XPointer *value = (XPointer *)val_ret;
56    BitmapWidget BW = (BitmapWidget) w;
57    Pixmap *pixmap;
58    char *data;
59    XImage *image;
60    Dimension width, height;
61
62    switch (*target) {
63
64/*  XA_TARGETS undefined ?!?
65
66    case XA_TARGETS:
67	*type = XA_ATOM;
68	*value = (XPointer) bitmapClassRec.bitmap_class.targets;
69	*length = bitmapClassRec.bitmap_class.num_targets;
70	*format = 32;
71	return True;
72
73*/
74
75    case XA_BITMAP:
76    case XA_PIXMAP:
77	if (BWQueryMarked(w)) {
78	  width = BW->bitmap.mark.to_x - BW->bitmap.mark.from_x + 1;
79	  height = BW->bitmap.mark.to_y - BW->bitmap.mark.from_y + 1;
80	  data = CreateCleanData(Length(width, height));
81	  image = CreateBitmapImage(BW, data, width, height);
82	  CopyImageData(BW->bitmap.image, image,
83			BW->bitmap.mark.from_x, BW->bitmap.mark.from_y,
84			BW->bitmap.mark.to_x, BW->bitmap.mark.to_y, 0, 0);
85	  pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
86	  *pixmap = GetPixmap(BW, image);
87	  DestroyBitmapImage(&image);
88	}
89	else if (BWQueryStored(w)) {
90	  pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
91	  *pixmap = GetPixmap(BW, BW->bitmap.storage);
92	}
93	else return False;
94	*type = XA_PIXMAP;
95	*value = (XPointer) pixmap;
96	*length = 1;
97	*format = 32;
98	return True;
99
100    default:
101	return False;
102    }
103}
104
105/* ARGSUSED */
106static void
107LoseSelection(Widget w, Atom selection)
108{
109    BitmapWidget BW = (BitmapWidget) w;
110
111    if (DEBUG)
112	fprintf(stderr, "Lost Selection\n");
113    BW->bitmap.selection.own = False;
114
115    BWUnmark(w);
116}
117
118/* ARGSUSED */
119static void
120SelectionDone(Widget w, Atom *selection, Atom *target)
121{
122/*  Done Automatically ?!?
123
124    BitmapWidget BW = (BitmapWidget) w;
125
126    if (*target != XA_TARGETS)
127	XtFree(BW->bitmap.value);
128
129*/
130}
131
132void
133BWGrabSelection(Widget w, Time btime)
134{
135    BitmapWidget BW = (BitmapWidget) w;
136
137    BW->bitmap.selection.own = XtOwnSelection(w, XA_PRIMARY, btime,
138					      ConvertSelection,
139					      (XtLoseSelectionProc)LoseSelection,
140					      SelectionDone);
141	if (DEBUG && BW->bitmap.selection.own)
142	    fprintf(stderr, "Own the selection\n");
143}
144
145
146/* ARGSUSED */
147static void
148SelectionCallback(Widget w, XtPointer cldat, Atom *selection, Atom *type,
149		  XtPointer val, unsigned long *length, int *format)
150{
151    XPointer value = (XPointer)val;
152    BitmapWidget BW = (BitmapWidget) w;
153    Pixmap *pixmap;
154
155   switch (*type) {
156
157    case XA_BITMAP:
158    case XA_PIXMAP:
159	DestroyBitmapImage(&BW->bitmap.storage);
160	pixmap = (Pixmap *) value;
161	BW->bitmap.storage = GetImage(BW, *pixmap);
162	XFree((char *)pixmap);
163	break;
164
165    default:
166	XtWarning(" selection request failed.  BitmapWidget");
167	break;
168    }
169
170    BW->bitmap.selection.limbo = FALSE;
171}
172
173void
174BWRequestSelection(Widget w, Time btime, Boolean wait)
175{
176  BitmapWidget BW = (BitmapWidget) w;
177
178  if (BW->bitmap.selection.own)
179    BWStore(w);
180  else {
181    XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP,
182			SelectionCallback, NULL, btime);
183
184    BW->bitmap.selection.limbo = TRUE;
185
186    if (wait)
187      while (BW->bitmap.selection.limbo) {
188	XEvent event;
189	XtNextEvent(&event);
190	XtDispatchEvent(&event);
191      }
192  }
193}
194
195/* ARGSUSED */
196/* Returns true if there is a transferable selection */
197Boolean
198BWQuerySelection(Widget w, Time btime)
199{
200/* To be written.  XA_TARGETS to be used.  So far undefined ?!? */
201
202  return True;
203}
204/*****************************************************************************/
205