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