462
|
1 /* GTK selection processing for XEmacs
|
|
2 Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
|
771
|
3 Copyright (C) 2001 Ben Wing.
|
462
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not synched with FSF. */
|
|
23
|
|
24 /* Authorship:
|
|
25
|
|
26 Written by Kevin Gallo for FSF Emacs.
|
|
27 Rewritten for mswindows by Jonathan Harris, December 1997 for 21.0.
|
|
28 Rewritten for GTK by William Perry, April 2000 for 21.1
|
|
29 */
|
|
30
|
|
31
|
|
32 #include <config.h>
|
|
33 #include "lisp.h"
|
|
34 #include "events.h"
|
|
35 #include "buffer.h"
|
|
36 #include "device.h"
|
|
37 #include "console-gtk.h"
|
|
38 #include "select.h"
|
|
39 #include "opaque.h"
|
|
40 #include "frame.h"
|
|
41
|
|
42 static Lisp_Object Vretrieved_selection;
|
|
43 static gboolean waiting_for_selection;
|
|
44 Lisp_Object Vgtk_sent_selection_hooks;
|
|
45
|
647
|
46 static GdkAtom
|
|
47 symbol_to_gtk_atom (struct device *d, Lisp_Object sym, int only_if_exists)
|
|
48 {
|
|
49 if (NILP (sym)) return GDK_SELECTION_PRIMARY;
|
|
50 if (EQ (sym, Qt)) return GDK_SELECTION_SECONDARY;
|
|
51 if (EQ (sym, QPRIMARY)) return GDK_SELECTION_PRIMARY;
|
|
52 if (EQ (sym, QSECONDARY)) return GDK_SELECTION_SECONDARY;
|
|
53
|
|
54 {
|
|
55 const Extbyte *nameext;
|
|
56 LISP_STRING_TO_EXTERNAL (Fsymbol_name (sym), nameext, Qctext);
|
|
57 return gdk_atom_intern (nameext, only_if_exists ? TRUE : FALSE);
|
|
58 }
|
|
59 }
|
462
|
60
|
647
|
61 static Lisp_Object
|
|
62 atom_to_symbol (struct device *d, GdkAtom atom)
|
|
63 {
|
|
64 if (atom == GDK_SELECTION_PRIMARY) return (QPRIMARY);
|
|
65 if (atom == GDK_SELECTION_SECONDARY) return (QSECONDARY);
|
|
66
|
|
67 {
|
771
|
68 Intbyte *intstr;
|
647
|
69 Extbyte *str = gdk_atom_name (atom);
|
|
70
|
|
71 if (! str) return Qnil;
|
462
|
72
|
771
|
73 EXTERNAL_TO_C_STRING (str, intstr, Qctext);
|
647
|
74 g_free (str);
|
771
|
75 return intern_int (intstr);
|
647
|
76 }
|
|
77 }
|
|
78
|
|
79 #define PROCESSING_GTK_CODE
|
|
80 #include "select-common.h"
|
|
81 #undef PROCESSING_GTK_CODE
|
|
82
|
|
83
|
462
|
84 /* Set the selection data to GDK_NONE and NULL data, meaning we were
|
|
85 ** unable to do what they wanted.
|
|
86 */
|
|
87 static void
|
|
88 gtk_decline_selection_request (GtkSelectionData *data)
|
|
89 {
|
|
90 gtk_selection_data_set (data, GDK_NONE, 0, NULL, 0);
|
|
91 }
|
|
92
|
|
93 /* Used as an unwind-protect clause so that, if a selection-converter signals
|
|
94 an error, we tell the requestor that we were unable to do what they wanted
|
|
95 before we throw to top-level or go into the debugger or whatever.
|
|
96 */
|
|
97 struct _selection_closure
|
|
98 {
|
|
99 GtkSelectionData *data;
|
|
100 gboolean successful;
|
|
101 };
|
|
102
|
|
103 static Lisp_Object
|
|
104 gtk_selection_request_lisp_error (Lisp_Object closure)
|
|
105 {
|
|
106 struct _selection_closure *cl = (struct _selection_closure *)
|
|
107 get_opaque_ptr (closure);
|
|
108
|
|
109 free_opaque_ptr (closure);
|
|
110 if (cl->successful == TRUE)
|
|
111 return Qnil;
|
|
112 gtk_decline_selection_request (cl->data);
|
|
113 return Qnil;
|
|
114 }
|
|
115
|
|
116 /* This provides the current selection to a requester.
|
|
117 **
|
|
118 ** This is connected to the selection_get() signal of the application
|
|
119 ** shell in device-gtk.c:gtk_init_device().
|
|
120 **
|
|
121 ** This is radically different than the old selection code (21.1.x),
|
|
122 ** but has been modeled after the X code, and appears to work.
|
|
123 **
|
|
124 ** WMP Feb 12 2001
|
|
125 */
|
|
126 void
|
|
127 emacs_gtk_selection_handle (GtkWidget *widget,
|
|
128 GtkSelectionData *selection_data,
|
|
129 guint info,
|
|
130 guint time_stamp,
|
|
131 gpointer data)
|
|
132 {
|
|
133 /* This function can GC */
|
|
134 struct gcpro gcpro1, gcpro2;
|
|
135 Lisp_Object temp_obj;
|
|
136 Lisp_Object selection_symbol;
|
|
137 Lisp_Object target_symbol = Qnil;
|
|
138 Lisp_Object converted_selection = Qnil;
|
|
139 guint32 local_selection_time;
|
|
140 Lisp_Object successful_p = Qnil;
|
|
141 int count;
|
|
142 struct device *d = decode_gtk_device (Qnil);
|
|
143 struct _selection_closure *cl = NULL;
|
|
144
|
|
145 GCPRO2 (converted_selection, target_symbol);
|
|
146
|
|
147 selection_symbol = atom_to_symbol (d, selection_data->selection);
|
|
148 target_symbol = atom_to_symbol (d, selection_data->target);
|
|
149
|
|
150 #if 0 /* #### MULTIPLE doesn't work yet */
|
|
151 if (EQ (target_symbol, QMULTIPLE))
|
|
152 target_symbol = fetch_multiple_target (selection_data);
|
|
153 #endif
|
|
154
|
|
155 temp_obj = Fget_selection_timestamp (selection_symbol);
|
|
156
|
|
157 if (NILP (temp_obj))
|
|
158 {
|
|
159 /* We don't appear to have the selection. */
|
|
160 gtk_decline_selection_request (selection_data);
|
|
161
|
|
162 goto DONE_LABEL;
|
|
163 }
|
|
164
|
|
165 local_selection_time = * (guint32 *) XOPAQUE_DATA (temp_obj);
|
|
166
|
|
167 if (time_stamp != GDK_CURRENT_TIME &&
|
|
168 local_selection_time > time_stamp)
|
|
169 {
|
|
170 /* Someone asked for the selection, and we have one, but not the one
|
|
171 they're looking for. */
|
|
172 gtk_decline_selection_request (selection_data);
|
|
173 goto DONE_LABEL;
|
|
174 }
|
|
175
|
|
176 converted_selection = select_convert_out (selection_symbol,
|
|
177 target_symbol, Qnil);
|
|
178
|
|
179 /* #### Is this the right thing to do? I'm no X expert. -- ajh */
|
|
180 if (NILP (converted_selection))
|
|
181 {
|
|
182 /* We don't appear to have a selection in that data type. */
|
|
183 gtk_decline_selection_request (selection_data);
|
|
184 goto DONE_LABEL;
|
|
185 }
|
|
186
|
|
187 count = specpdl_depth ();
|
|
188
|
|
189 cl = (struct _selection_closure *) xmalloc (sizeof (*cl));
|
|
190 cl->data = selection_data;
|
|
191 cl->successful = FALSE;
|
|
192
|
|
193 record_unwind_protect (gtk_selection_request_lisp_error,
|
|
194 make_opaque_ptr (cl));
|
|
195
|
|
196 {
|
647
|
197 UChar_Binary *data;
|
665
|
198 Bytecount size;
|
462
|
199 int format;
|
|
200 GdkAtom type;
|
|
201 lisp_data_to_selection_data (d, converted_selection,
|
|
202 &data, &type, &size, &format);
|
|
203
|
647
|
204 gtk_selection_data_set (selection_data, type, format, data,
|
|
205 /* #### is this right? */
|
|
206 (unsigned int) size);
|
462
|
207 successful_p = Qt;
|
|
208 /* Tell x_selection_request_lisp_error() it's cool. */
|
|
209 cl->successful = TRUE;
|
|
210 xfree (data);
|
|
211 }
|
|
212
|
771
|
213 unbind_to (count);
|
462
|
214
|
|
215 DONE_LABEL:
|
|
216
|
|
217 if (cl) xfree (cl);
|
|
218
|
|
219 UNGCPRO;
|
|
220
|
|
221 /* Let random lisp code notice that the selection has been asked for. */
|
|
222 {
|
|
223 Lisp_Object val = Vgtk_sent_selection_hooks;
|
|
224 if (!UNBOUNDP (val) && !NILP (val))
|
|
225 {
|
|
226 Lisp_Object rest;
|
|
227 if (CONSP (val) && !EQ (XCAR (val), Qlambda))
|
|
228 for (rest = val; !NILP (rest); rest = Fcdr (rest))
|
|
229 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
|
|
230 else
|
|
231 call3 (val, selection_symbol, target_symbol, successful_p);
|
|
232 }
|
|
233 }
|
|
234 }
|
|
235
|
|
236
|
746
|
237 void
|
|
238 emacs_gtk_selection_clear_event_handle (GtkWidget *widget,
|
|
239 GdkEventSelection *event,
|
|
240 gpointer data)
|
|
241 {
|
|
242 GdkAtom selection = event->selection;
|
|
243 guint32 changed_owner_time = event->time;
|
|
244 struct device *d = decode_gtk_device (Qnil);
|
|
245
|
|
246 Lisp_Object selection_symbol, local_selection_time_lisp;
|
|
247 guint32 local_selection_time;
|
|
248
|
|
249 selection_symbol = atom_to_symbol (d, selection);
|
|
250
|
|
251 local_selection_time_lisp = Fget_selection_timestamp (selection_symbol);
|
|
252
|
|
253 /* We don't own the selection, so that's fine. */
|
|
254 if (NILP (local_selection_time_lisp))
|
|
255 return;
|
|
256
|
|
257 local_selection_time = *(guint32 *) XOPAQUE_DATA (local_selection_time_lisp);
|
|
258
|
|
259 /* This SelectionClear is for a selection that we no longer own, so we can
|
|
260 disregard it. (That is, we have reasserted the selection since this
|
|
261 request was generated.)
|
|
262 */
|
|
263 if (changed_owner_time != GDK_CURRENT_TIME &&
|
|
264 local_selection_time > changed_owner_time)
|
|
265 return;
|
|
266
|
|
267 handle_selection_clear (selection_symbol);
|
|
268 }
|
|
269
|
|
270
|
462
|
271
|
|
272 static GtkWidget *reading_selection_reply;
|
|
273 static GdkAtom reading_which_selection;
|
|
274 static int selection_reply_timed_out;
|
|
275
|
|
276 /* Gets the current selection owned by another application */
|
|
277 void
|
|
278 emacs_gtk_selection_received (GtkWidget *widget,
|
|
279 GtkSelectionData *selection_data,
|
|
280 gpointer user_data)
|
|
281 {
|
|
282 waiting_for_selection = FALSE;
|
|
283 Vretrieved_selection = Qnil;
|
|
284
|
|
285 reading_selection_reply = NULL;
|
|
286
|
|
287 signal_fake_event ();
|
|
288
|
|
289 if (selection_data->length < 0)
|
|
290 {
|
|
291 return;
|
|
292 }
|
|
293
|
|
294 Vretrieved_selection =
|
|
295 selection_data_to_lisp_data (NULL,
|
|
296 selection_data->data,
|
|
297 selection_data->length,
|
|
298 selection_data->type,
|
|
299 selection_data->format);
|
|
300 }
|
|
301
|
|
302 static int
|
|
303 selection_reply_done (void *ignore)
|
|
304 {
|
|
305 return !reading_selection_reply;
|
|
306 }
|
|
307
|
|
308 /* Do protocol to read selection-data from the server.
|
|
309 Converts this to lisp data and returns it.
|
|
310 */
|
|
311 static Lisp_Object
|
|
312 gtk_get_foreign_selection (Lisp_Object selection_symbol,
|
|
313 Lisp_Object target_type)
|
|
314 {
|
|
315 /* This function can GC */
|
|
316 struct device *d = decode_gtk_device (Qnil);
|
|
317 GtkWidget *requestor = DEVICE_GTK_APP_SHELL (d);
|
|
318 guint32 requestor_time = DEVICE_GTK_MOUSE_TIMESTAMP (d);
|
|
319 GdkAtom selection_atom = symbol_to_gtk_atom (d, selection_symbol, 0);
|
|
320 int speccount;
|
|
321 GdkAtom type_atom = symbol_to_gtk_atom (d, (CONSP (target_type) ?
|
|
322 XCAR (target_type) : target_type), 0);
|
|
323
|
|
324 gtk_selection_convert (requestor, selection_atom, type_atom,
|
|
325 requestor_time);
|
|
326
|
|
327 signal_fake_event ();
|
|
328
|
|
329 /* Block until the reply has been read. */
|
|
330 reading_selection_reply = requestor;
|
|
331 reading_which_selection = selection_atom;
|
|
332 selection_reply_timed_out = 0;
|
|
333
|
|
334 speccount = specpdl_depth ();
|
|
335
|
|
336 #if 0
|
|
337 /* add a timeout handler */
|
|
338 if (gtk_selection_timeout > 0)
|
|
339 {
|
|
340 Lisp_Object id = Fadd_timeout (make_int (x_selection_timeout),
|
|
341 Qx_selection_reply_timeout_internal,
|
|
342 Qnil, Qnil);
|
|
343 record_unwind_protect (Fdisable_timeout, id);
|
|
344 }
|
|
345 #endif
|
|
346
|
|
347 /* This is ^Gable */
|
|
348 wait_delaying_user_input (selection_reply_done, 0);
|
|
349
|
|
350 if (selection_reply_timed_out)
|
563
|
351 signal_error (Qselection_conversion_error, "timed out waiting for reply from selection owner", Qunbound);
|
462
|
352
|
771
|
353 unbind_to (speccount);
|
462
|
354
|
|
355 /* otherwise, the selection is waiting for us on the requested property. */
|
|
356 return select_convert_in (selection_symbol,
|
|
357 target_type,
|
|
358 Vretrieved_selection);
|
|
359 }
|
|
360
|
|
361
|
|
362 #if 0
|
|
363 static void
|
|
364 gtk_get_window_property (struct device *d, GtkWidget *window, GdkAtom property,
|
|
365 Extbyte **data_ret, int *bytes_ret,
|
|
366 GdkAtom *actual_type_ret, int *actual_format_ret,
|
|
367 unsigned long *actual_size_ret, int delete_p)
|
|
368 {
|
647
|
369 /* deleted */
|
462
|
370 }
|
|
371
|
|
372
|
|
373 static void
|
|
374 receive_incremental_selection (Display *display, Window window, Atom property,
|
|
375 /* this one is for error messages only */
|
|
376 Lisp_Object target_type,
|
|
377 unsigned int min_size_bytes,
|
|
378 Extbyte **data_ret, int *size_bytes_ret,
|
|
379 Atom *type_ret, int *format_ret,
|
|
380 unsigned long *size_ret)
|
|
381 {
|
647
|
382 /* deleted */
|
462
|
383 }
|
|
384
|
|
385
|
|
386 static Lisp_Object
|
|
387 gtk_get_window_property_as_lisp_data (struct device *d,
|
|
388 GtkWidget *window,
|
|
389 GdkAtom property,
|
|
390 /* next two for error messages only */
|
|
391 Lisp_Object target_type,
|
|
392 GdkAtom selection_atom)
|
|
393 {
|
647
|
394 /* deleted */
|
462
|
395 }
|
|
396 #endif
|
|
397
|
|
398
|
|
399
|
|
400 static Lisp_Object
|
|
401 gtk_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
|
712
|
402 Lisp_Object how_to_add, Lisp_Object selection_type, int owned_p)
|
462
|
403 {
|
|
404 struct device *d = decode_gtk_device (Qnil);
|
|
405 GtkWidget *selecting_window = GTK_WIDGET (DEVICE_GTK_APP_SHELL (d));
|
|
406 Lisp_Object selection_time;
|
|
407 /* Use the time of the last-read mouse or keyboard event.
|
|
408 For selection purposes, we use this as a sleazy way of knowing what the
|
|
409 current time is in server-time. This assumes that the most recently read
|
|
410 mouse or keyboard event has something to do with the assertion of the
|
|
411 selection, which is probably true.
|
|
412 */
|
|
413 guint32 thyme = DEVICE_GTK_MOUSE_TIMESTAMP (d);
|
|
414 GdkAtom selection_atom;
|
|
415
|
|
416 CHECK_SYMBOL (selection_name);
|
|
417 selection_atom = symbol_to_gtk_atom (d, selection_name, 0);
|
|
418
|
|
419 gtk_selection_owner_set (selecting_window,
|
|
420 selection_atom,
|
|
421 thyme);
|
|
422
|
|
423 /* We do NOT use time_to_lisp() here any more, like we used to.
|
|
424 That assumed equivalence of time_t and Time, which is not
|
|
425 necessarily the case (e.g. under OSF on the Alphas, where
|
|
426 Time is a 64-bit quantity and time_t is a 32-bit quantity).
|
|
427
|
|
428 Opaque pointers are the clean way to go here.
|
|
429 */
|
|
430 selection_time = make_opaque (&thyme, sizeof (thyme));
|
|
431
|
|
432 return selection_time;
|
|
433 }
|
|
434
|
|
435 static void
|
|
436 gtk_disown_selection (Lisp_Object selection, Lisp_Object timeval)
|
|
437 {
|
|
438 struct device *d = decode_gtk_device (Qnil);
|
|
439 GdkAtom selection_atom;
|
|
440 guint32 timestamp;
|
|
441
|
|
442 CHECK_SYMBOL (selection);
|
|
443 selection_atom = symbol_to_gtk_atom (d, selection, 0);
|
|
444
|
|
445 if (NILP (timeval))
|
|
446 timestamp = DEVICE_GTK_MOUSE_TIMESTAMP (d);
|
|
447 else
|
|
448 {
|
|
449 time_t the_time;
|
|
450 lisp_to_time (timeval, &the_time);
|
|
451 timestamp = (guint32) the_time;
|
|
452 }
|
|
453
|
|
454 gtk_selection_owner_set (NULL, selection_atom, timestamp);
|
|
455 }
|
|
456
|
|
457 static Lisp_Object
|
|
458 gtk_selection_exists_p (Lisp_Object selection,
|
|
459 Lisp_Object selection_type)
|
|
460 {
|
|
461 struct device *d = decode_gtk_device (Qnil);
|
|
462
|
|
463 return (gdk_selection_owner_get (symbol_to_gtk_atom (d, selection, 0)) ? Qt : Qnil);
|
|
464 }
|
|
465
|
|
466
|
|
467
|
|
468 /************************************************************************/
|
|
469 /* initialization */
|
|
470 /************************************************************************/
|
|
471
|
|
472 void
|
|
473 syms_of_select_gtk (void)
|
|
474 {
|
|
475 }
|
|
476
|
|
477 void
|
|
478 console_type_create_select_gtk (void)
|
|
479 {
|
|
480 CONSOLE_HAS_METHOD (gtk, own_selection);
|
|
481 CONSOLE_HAS_METHOD (gtk, disown_selection);
|
|
482 CONSOLE_HAS_METHOD (gtk, selection_exists_p);
|
|
483 CONSOLE_HAS_METHOD (gtk, get_foreign_selection);
|
|
484 }
|
|
485
|
|
486 void
|
|
487 vars_of_select_gtk (void)
|
|
488 {
|
|
489 staticpro (&Vretrieved_selection);
|
|
490 Vretrieved_selection = Qnil;
|
|
491
|
|
492 DEFVAR_LISP ("gtk-sent-selection-hooks", &Vgtk_sent_selection_hooks /*
|
|
493 A function or functions to be called after we have responded to some
|
|
494 other client's request for the value of a selection that we own. The
|
|
495 function(s) will be called with four arguments:
|
|
496 - the name of the selection (typically PRIMARY, SECONDARY, or CLIPBOARD);
|
|
497 - the name of the selection-type which we were requested to convert the
|
|
498 selection into before sending (for example, STRING or LENGTH);
|
|
499 - and whether we successfully transmitted the selection.
|
|
500 We might have failed (and declined the request) for any number of reasons,
|
|
501 including being asked for a selection that we no longer own, or being asked
|
|
502 to convert into a type that we don't know about or that is inappropriate.
|
|
503 This hook doesn't let you change the behavior of emacs's selection replies,
|
|
504 it merely informs you that they have happened.
|
|
505 */ );
|
|
506 Vgtk_sent_selection_hooks = Qunbound;
|
|
507 }
|