1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file clipboard.c
*
* This file is part of the XForms library package.
* Copyright (c) 1997-2002 T.C. Zhao
* All rights reserved.
*
* Implementation of clipboard. Event handling violates the ICCCM,
* but should work ok. Need server time from the mainloop.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "include/forms.h"
#include "flinternal.h"
typedef struct {
FL_OBJECT * ob; /* the object that stuff'ed cp */
FL_OBJECT * req_ob; /* the object that requested cp */
Window window;
Window req_window;
long type,
size;
FL_LOSE_SELECTION_CB lose_callback;
FL_SELECTION_CB got_it_callback;
} ClipBoard;
static ClipBoard clipboard;
int ( * fli_handle_clipboard )( void * ) = NULL; /* also needed in handling.c */
static int handle_clipboard_event( void * );
/***************************************
***************************************/
int
fl_stuff_clipboard( FL_OBJECT * ob,
long type FL_UNUSED_ARG,
const void * data,
long size,
FL_LOSE_SELECTION_CB lose_callback )
{
Window win = FL_ObjWin( ob );
ClipBoard *cp = &clipboard;
fli_handle_clipboard = handle_clipboard_event;
if ( ! win )
{
M_err( "fl_stuff_clipboard", "Bad object %s", ob ? ob->label : "null" );
return 0;
}
XSetSelectionOwner( flx->display, XA_PRIMARY, win, CurrentTime );
/* Make sure we got it */
if ( XGetSelectionOwner( flx->display, XA_PRIMARY ) != win )
{
M_err( "fl_stuff_clipboard", "Failed to get owner" );
return 0;
}
/* Create structure that holds clipboard info */
cp->window = win;
cp->ob = ob;
cp->size = size;
cp->lose_callback = lose_callback ? lose_callback : NULL;
/* Cheap (and fast!) shot */
XStoreBuffer( flx->display, data, size, 0 );
return size;
}
static Atom clipboard_prop;
static Atom targets_prop;
/***************************************
***************************************/
int
fl_request_clipboard( FL_OBJECT * ob,
long type FL_UNUSED_ARG,
FL_SELECTION_CB got_it_callback )
{
Window win;
ClipBoard *cp = &clipboard;
void *buf;
int nb = 0;
cp->req_ob = ob;
if ( got_it_callback == NULL )
{
M_warn( "fl_request_clipboard", "Callback is NULL" );
return -1;
}
if ( ! clipboard_prop )
{
clipboard_prop = XInternAtom( flx->display, "FL_CLIPBOARD", False );
fli_handle_clipboard = handle_clipboard_event;
}
cp->got_it_callback = got_it_callback;
cp->req_window = FL_ObjWin( ob );
win = XGetSelectionOwner( flx->display, XA_PRIMARY );
if ( win == None )
{
XSetSelectionOwner( flx->display, XA_PRIMARY, cp->req_window,
CurrentTime );
buf = XFetchBuffer( flx->display, &nb, 0 );
cp->window = XGetSelectionOwner( flx->display, XA_PRIMARY );
cp->ob = NULL;
cp->size = nb;
cp->got_it_callback( cp->req_ob, XA_STRING, buf, nb );
XFree( buf );
}
else if ( win != cp->req_window )
{
/* We don't own it, request it */
M_warn( "fl_request_clipboard", "Requesting selection from %ld", win );
XConvertSelection( flx->display,
XA_PRIMARY, XA_STRING,
clipboard_prop,
cp->req_window, CurrentTime );
nb = -1;
}
else if ( win == cp->req_window )
{
/* We own the buffer */
buf = XFetchBuffer( flx->display, &nb, 0 );
cp->got_it_callback( cp->req_ob, XA_STRING, buf, nb );
XFree( buf );
}
return nb;
}
/***************************************
* Returns a negative number if not known how to handle an event
***************************************/
static int
handle_clipboard_event( void * event )
{
XSelectionRequestEvent *sreq = event;
XEvent *xev = event;
XSelectionEvent sev;
ClipBoard *cp = &clipboard;
char *s;
int n;
/* SelectionClear confirms loss of selection
SelectionRequest indicates that another app wants to own selection
SelectionNotify confirms that request of selection is ok */
if ( ! targets_prop )
targets_prop = XInternAtom( flx->display, "TARGETS", False );
if ( ! clipboard_prop )
clipboard_prop = XInternAtom( flx->display, "FL_CLIPBOARD", False );
if ( ! cp->req_window && ! cp->window )
{
M_warn( "handle_clipboard_event", "InternalError" );
return -1;
}
if ( xev->type == SelectionClear )
{
if ( cp->ob && cp->lose_callback )
cp->lose_callback( cp->ob, cp->type );
cp->ob = NULL;
cp->window = None;
}
else if ( xev->type == SelectionNotify && cp->req_ob )
{
/* Our request went through, go and get it */
Atom ret_type;
int ret_format;
unsigned long ret_len = 0,
ret_after;
unsigned char *ret = NULL;
/* X guarantees 16K request size */
long chunksize = fli_context->max_request_size,
offset = 0;
char *buf = NULL;
int buflen = 0;
/* Get the stuff. Repeat until we get all */
do
{
XGetWindowProperty( flx->display, xev->xselection.requestor,
xev->xselection.property, offset, chunksize,
False, xev->xselection.target, &ret_type,
&ret_format, &ret_len, &ret_after, &ret );
if ( ret_len && ret )
{
if ( ret_after == 0 && ! buf )
cp->got_it_callback( cp->req_ob, ret_type, ret, ret_len );
else
{
buf = fl_realloc( buf, buflen + ret_len );
memcpy( buf + buflen, ret, ret_len );
buflen += ret_len;
}
XFree( ret );
ret = NULL;
}
offset += ret_len * ret_format / 32;
chunksize = ( ret_after + 3 ) / 4;
if ( chunksize > fli_context->max_request_size )
chunksize = fli_context->max_request_size;
} while ( ret_after );
if ( buflen )
{
cp->got_it_callback( cp->req_ob, ret_type, buf, buflen );
fl_free( buf );
}
XDeleteProperty( flx->display, xev->xselection.requestor,
xev->xselection.property );
}
else if ( xev->type == SelectionRequest )
{
/* Someone wants our selection */
M_warn( "handle_clipboard_event", "SelectionRequest" );
if ( sreq->owner != cp->window )
{
M_err( "handle_clipboard_event", "Wrong owner" );
return -1;
}
/* Set up the event to be sent to the requestor */
sev.type = SelectionNotify;
sev.display = sreq->display;
sev.requestor = sreq->requestor;
sev.selection = sreq->selection;
sev.target = sreq->target;
sev.property = None;
sev.time = sreq->time;
if ( sreq->selection == XA_PRIMARY )
{
if ( sreq->target == XA_STRING )
{
s = XFetchBuffer( flx->display, &n, 0 );
XChangeProperty( flx->display,
sreq->requestor, sreq->property, sreq->target,
8, PropModeReplace, (unsigned char *) s, n );
sev.property = sreq->property;
XFree( s );
}
else if ( sreq->target == targets_prop ) /* aixterm wants this */
{
Atom alist = XA_STRING;
XChangeProperty( flx->display,
sreq->requestor, sreq->property, XA_ATOM,
32, PropModeReplace,
( unsigned char * ) &alist, 1 );
sev.property = sreq->property;
}
else
{
/* If we have other types conversion routine should be
called here */
M_warn( "handle_clipboard_event", "Received request with "
"unknown/not implemented XAtom target type: %d\n",
( int ) sreq->target );
}
XSendEvent( flx->display, sreq->requestor, False, 0,
( XEvent * ) & sev );
}
else
{
/* not XA_PRIMARY request */
M_warn( "handle_clipboard_event", "Unknown selection request: %d",
sreq->selection );
return -1;
}
}
return 0;
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|