tkSelect.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/*
* tkSelect.c --
*
* This file manages the selection for the Tk toolkit,
* translating between the standard X ICCCM conventions
* and Tcl commands.
*
* Copyright (c) 1990-1993 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkSelect.c 1.56 96/03/21 13:16:29
*/
#include "tkInt.h"
#include "tkSelect.h"
/*
* When a selection handler is set up by invoking "selection handle",
* one of the following data structures is set up to hold information
* about the command to invoke and its interpreter.
*/
typedef struct {
int cmdLength; /* # of non-NULL bytes in command. */
* allocated as large as necessary. This
* must be the last entry in the structure. */
} CommandInfo;
/*
* When selection ownership is claimed with the "selection own" Tcl command,
* one of the following structures is created to record the Tcl command
* to be executed when the selection is lost again.
*/
typedef struct LostCommand {
* allocated as large as necessary. This
* must be the last entry in the structure. */
} LostCommand;
/*
* Shared variables:
*/
/* Topmost search in progress, or
* NULL if none. */
/*
* Forward declarations for procedures defined in this file:
*/
/*
*--------------------------------------------------------------
*
* Tk_CreateSelHandler --
*
* This procedure is called to register a procedure
* as the handler for selection requests of a particular
* target type on a particular window for a particular
* selection.
*
* Results:
* None.
*
* Side effects:
* In the future, whenever the selection is in tkwin's
* window and someone requests the selection in the
* form given by target, proc will be invoked to provide
* part or all of the selection in the given form. If
* there was already a handler declared for the given
* window, target and selection type, then it is replaced.
* Proc should have the following form:
*
* int
* proc(clientData, offset, buffer, maxBytes)
* ClientData clientData;
* int offset;
* char *buffer;
* int maxBytes;
* {
* }
*
* The clientData argument to proc will be the same as
* the clientData argument to this procedure. The offset
* argument indicates which portion of the selection to
* return: skip the first offset bytes. Buffer is a
* pointer to an area in which to place the converted
* selection, and maxBytes gives the number of bytes
* available at buffer. Proc should place the selection
* in buffer as a string, and return a count of the number
* of bytes of selection actually placed in buffer (not
* including the terminating NULL character). If the
* return value equals maxBytes, this is a sign that there
* is probably still more selection information available.
*
*--------------------------------------------------------------
*/
void
* that can be handled by proc,
* e.g. TARGETS or STRING. */
* selection to type "target". */
* information should be returned to
* the requestor. XA_STRING is best by
* far, but anything listed in the ICCCM
* will be tolerated (blech). */
{
register TkSelHandler *selPtr;
}
/*
* See if there's already a handler for this target and selection on
* this window. If so, re-use it. If not, create a new one.
*/
break;
}
/*
* Special case: when replacing handler created by
* "selection handle", free up memory. Should there be a
* callback to allow other clients to do this too?
*/
}
break;
}
}
} else {
}
}
/*
*----------------------------------------------------------------------
*
* Tk_DeleteSelHandler --
*
* Remove the selection handler for a given window, target, and
* selection, if it exists.
*
* Results:
* None.
*
* Side effects:
* The selection handler for tkwin and target is removed. If there
* is no such handler then nothing happens.
*
*----------------------------------------------------------------------
*/
void
* is to be removed. */
* handler is to be removed. */
{
register TkSelInProgress *ipPtr;
/*
* Find the selection handler to be deleted, or return if it doesn't
* exist.
*/
return;
}
break;
}
}
/*
* If ConvertSelection is processing this handler, tell it that the
* handler is dead.
*/
}
}
/*
* Free resources associated with the handler.
*/
} else {
}
}
}
/*
*--------------------------------------------------------------
*
* Tk_OwnSelection --
*
* Arrange for tkwin to become the owner of a selection.
*
* Results:
* None.
*
* Side effects:
* From now on, requests for the selection will be directed
* to procedures associated with tkwin (they must have been
* declared with calls to Tk_CreateSelHandler). When the
* selection is lost by this window, proc will be invoked
* (see the manual entry for details). This procedure may
* invoke callbacks, including Tcl scripts, so any calling
* function should be reentrant at the point where
* Tk_OwnSelection is invoked.
*
*--------------------------------------------------------------
*/
void
* owner. */
* is taken away from tkwin. */
* pass to proc. */
{
* prevent compiler warning. */
}
/*
* This code is somewhat tricky. First, we find the specified selection
* on the selection list. If the previous owner is in this process, and
* is a different window, then we need to invoke the clearProc. However,
* it's dangerous to call the clearProc right now, because it could
* invoke a Tcl script that wrecks the current state (e.g. it could
* delete the window). To be safe, defer the call until the end of the
* procedure when we no longer care about the state.
*/
break;
}
}
/*
* If the selection handler is one created by "selection own",
* be sure to free the record for it; otherwise there will be
* a memory leak.
*/
}
}
/*
* Note that we are using CurrentTime, even though ICCCM recommends against
* this practice (the problem is that we don't necessarily have a valid
* time to use). We will not be able to retrieve a useful timestamp for
* the TIMESTAMP target later.
*/
/*
* Note that we are not checking to see if the selection claim succeeded.
* If the ownership does not change, then the clearProc may never be
* invoked, and we will return incorrect information when queried for the
* current selection owner.
*/
/*
* Now that we are done, we can invoke clearProc without running into
* reentrancy problems.
*/
}
}
/*
*----------------------------------------------------------------------
*
* Tk_ClearSelection --
*
* Eliminate the specified selection on tkwin's display, if there is one.
*
* Results:
* None.
*
* Side effects:
* The specified selection is cleared, so that future requests to retrieve
* it will fail until some application owns it again. This procedure
* invokes callbacks, possibly including Tcl scripts, so any calling
* function should be reentrant at the point Tk_ClearSelection is invoked.
*
*----------------------------------------------------------------------
*/
void
{
* prevent compiler warning. */
}
} else {
}
break;
}
}
}
}
}
/*
*--------------------------------------------------------------
*
* Tk_GetSelection --
*
* Retrieve the value of a selection and pass it off (in
* pieces, possibly) to a given procedure.
*
* Results:
* The return value is a standard Tcl return value.
* If an error occurs (such as no selection exists)
* then an error message is left in interp->result.
*
* Side effects:
* The standard X11 protocols are used to retrieve the
* selection. When it arrives, it is passed to proc. If
* the selection is very large, it will be passed to proc
* in several pieces. Proc should have the following
* structure:
*
* int
* proc(clientData, interp, portion)
* ClientData clientData;
* Tcl_Interp *interp;
* char *portion;
* {
* }
*
* The interp and clientData arguments to proc will be the
* same as the corresponding arguments to Tk_GetSelection.
* The portion argument points to a character string
* containing part of the selection, and numBytes indicates
* the length of the portion, not including the terminating
* NULL character. If the selection arrives in several pieces,
* the "portion" arguments in separate calls will contain
* successive parts of the selection. Proc should normally
* return TCL_OK. If it detects an error then it should return
* TCL_ERROR and leave an error message in interp->result; the
* remainder of the selection retrieval will be aborted.
*
*--------------------------------------------------------------
*/
int
* errors. */
* the selection (determines display
* from which to retrieve). */
* is to be returned. */
* selection, once it has been retrieved. */
{
}
/*
* If the selection is owned by a window managed by this
* process, then call the retrieval procedure directly,
* rather than going through the X server (it's dangerous
* to go through the X server in this case because it could
* result in deadlock if an INCR-style selection results).
*/
break;
}
register TkSelHandler *selPtr;
break;
}
}
if (count > TK_SEL_BYTES_AT_ONCE) {
panic("selection handler returned too many bytes");
}
if (count < 0) {
goto cantget;
}
} else {
offset = 0;
pendingPtr = &ip;
while (1) {
goto cantget;
}
if (count > TK_SEL_BYTES_AT_ONCE) {
panic("selection handler returned too many bytes");
}
break;
}
}
}
return result;
}
/*
* The selection is owned by some other process.
*/
"\" not defined", (char *) NULL);
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* Tk_SelectionCmd --
*
* This procedure is invoked to process the "selection" Tcl
* command. See the user documentation for details on what
* it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
int c, count;
char **args;
if (argc < 2) {
"wrong # args: should be \"%.50s option ?arg arg ...?\"",
argv[0]);
return TCL_ERROR;
}
c = argv[1][0];
if (args[0][0] != '-') {
break;
}
if (count < 2) {
"\" missing", (char *) NULL);
return TCL_ERROR;
}
c = args[0][1];
} else if ((c == 's')
} else {
"\"", (char *) NULL);
return TCL_ERROR;
}
}
if (count == 1) {
} else if (count > 1) {
" clear ?options?\"", (char *) NULL);
return TCL_ERROR;
}
}
return TCL_ERROR;
}
} else {
}
return TCL_OK;
char *targetName = NULL;
int result;
if (args[0][0] != '-') {
break;
}
if (count < 2) {
"\" missing", (char *) NULL);
return TCL_ERROR;
}
c = args[0][1];
} else if ((c == 's')
} else if ((c == 't')
} else {
"\"", (char *) NULL);
return TCL_ERROR;
}
}
}
return TCL_ERROR;
}
} else {
}
if (count > 1) {
" get ?options?\"", (char *) NULL);
return TCL_ERROR;
} else if (count == 1) {
} else if (targetName != NULL) {
} else {
}
(ClientData) &selBytes);
} else {
}
return result;
char *targetName = NULL;
char *formatName = NULL;
register CommandInfo *cmdInfoPtr;
int cmdLength;
if (args[0][0] != '-') {
break;
}
if (count < 2) {
"\" missing", (char *) NULL);
return TCL_ERROR;
}
c = args[0][1];
} else if ((c == 's')
} else if ((c == 't')
} else {
"\"", (char *) NULL);
return TCL_ERROR;
}
}
" handle ?options? window command\"", (char *) NULL);
return TCL_ERROR;
}
return TCL_ERROR;
}
} else {
}
if (count > 2) {
} else if (targetName != NULL) {
} else {
}
if (count > 3) {
} else if (formatName != NULL) {
} else {
}
if (cmdLength == 0) {
} else {
}
return TCL_OK;
register LostCommand *lostPtr;
int cmdLength;
if (args[0][0] != '-') {
break;
}
if (count < 2) {
"\" missing", (char *) NULL);
return TCL_ERROR;
}
c = args[0][1];
} else if ((c == 'd')
} else if ((c == 's')
} else {
"\"", (char *) NULL);
return TCL_ERROR;
}
}
if (count > 2) {
" own ?options? ?window?\"", (char *) NULL);
return TCL_ERROR;
}
} else {
}
if (count == 0) {
}
return TCL_ERROR;
}
break;
}
/*
* Ignore the internal clipboard window.
*/
}
return TCL_OK;
}
return TCL_ERROR;
}
if (count == 2) {
}
(ClientData) NULL);
return TCL_OK;
}
-3 + cmdLength));
return TCL_OK;
} else {
"bad option \"%.50s\": must be clear, get, handle, or own",
argv[1]);
return TCL_ERROR;
}
}
/*
*----------------------------------------------------------------------
*
* TkSelDeadWindow --
*
* This procedure is invoked just before a TkWindow is deleted.
* It performs selection-related cleanup.
*
* Results:
* None.
*
* Side effects:
* Frees up memory associated with the selection.
*
*----------------------------------------------------------------------
*/
void
{
register TkSelHandler *selPtr;
register TkSelInProgress *ipPtr;
/*
* While deleting all the handlers, be careful to check whether
* ConvertSelection or TkSelPropProc are about to process one of the
* deleted handlers.
*/
}
}
}
}
/*
* Remove selections owned by window being deleted.
*/
}
} else {
}
}
}
}
/*
*----------------------------------------------------------------------
*
* TkSelInit --
*
* Initialize selection-related information for a display.
*
* Results:
* None.
*
* Side effects:
* Selection-related information is initialized.
*
*----------------------------------------------------------------------
*/
void
* display to initialize). */
{
/*
* Fetch commonly-used atoms.
*/
}
/*
*----------------------------------------------------------------------
*
* TkSelClearSelection --
*
* This procedure is invoked to process a SelectionClear event.
*
* Results:
* None.
*
* Side effects:
* Invokes the clear procedure for the window which lost the
* selection.
*
*----------------------------------------------------------------------
*/
void
{
/*
* Invoke clear procedure for window that just lost the selection. This
* code is a bit tricky, because any callbacks due to selection changes
* between windows managed by the process have already been made. Thus,
* ignore the event unless it refers to the window that's currently the
* selection owner and the event was generated after the server saw the
* SetSelectionOwner request.
*/
break;
}
}
} else {
}
/*
* Because of reentrancy problems, calling clearProc must be done
* after the infoPtr has been removed from the selectionInfoPtr
* list (clearProc could modify the list, e.g. by creating
* a new selection).
*/
}
}
}
/*
*--------------------------------------------------------------
*
* SelGetProc --
*
* This procedure is invoked to process pieces of the selection
* as they arrive during "selection get" commands.
*
* Results:
* Always returns TCL_OK.
*
* Side effects:
* Bytes get appended to the dynamic string pointed to by the
* clientData argument.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static int
* assembled selection. */
* reporting (not used). */
char *portion; /* New information to be appended. */
{
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* HandleTclCommand --
*
* This procedure acts as selection handler for handlers created
* by the "selection handle" command. It invokes a Tcl command to
* retrieve the selection.
*
* Results:
* The return value is a count of the number of bytes actually
* stored at buffer, or -1 if an error occurs while executing
* the Tcl command to retrieve the selection.
*
* Side effects:
* None except for things done by the Tcl command.
*
*----------------------------------------------------------------------
*/
static int
int offset; /* Return selection bytes starting at this
* offset. */
char *buffer; /* Place to store converted selection. */
int maxBytes; /* Maximum # of bytes to store at buffer. */
{
int spaceNeeded, length;
#define MAX_STATIC_SIZE 100
char staticSpace[MAX_STATIC_SIZE];
char *command;
/*
* We must copy the interpreter pointer from CommandInfo because the
* command could delete the handler, freeing the CommandInfo data before we
* are done using it. We must also protect the interpreter from being
* deleted too soo.
*/
/*
* First, generate a command by taking the command string
* and appending the offset and maximum # of bytes.
*/
if (spaceNeeded < MAX_STATIC_SIZE) {
} else {
}
/*
* Execute the command. Be sure to restore the state of the
* interpreter after executing the command.
*/
}
} else {
length = -1;
}
if (command != staticSpace) {
}
return length;
}
/*
*----------------------------------------------------------------------
*
* TkSelDefaultSelection --
*
* This procedure is called to generate selection information
* for a few standard targets such as TIMESTAMP and TARGETS.
* It is invoked only if no handler has been declared by the
* application.
*
* Results:
* If "target" is a standard target understood by this procedure,
* the selection is converted to that form and stored as a
* character string in buffer. The type of the selection (e.g.
* STRING or ATOM) is stored in *typePtr, and the return value is
* a count of the # of non-NULL bytes at buffer. If the target
* wasn't understood, or if there isn't enough space at buffer
* to hold the entire selection (no INCR-mode transfers for this
* stuff!), then -1 is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
char *buffer; /* Place to put selection characters. */
int maxBytes; /* Maximum # of bytes to store at buffer. */
* for use in converting to proper X format. */
{
if (maxBytes < 20) {
return -1;
}
*typePtr = XA_INTEGER;
}
register TkSelHandler *selPtr;
char *atomString;
int length, atomLength;
if (maxBytes < 50) {
return -1;
}
return -1;
}
length += atomLength;
}
}
return length;
}
int length;
return -1;
}
return length;
}
int length;
return -1;
}
return length;
}
return -1;
}
/*
*----------------------------------------------------------------------
*
* LostSelection --
*
* This procedure is invoked when a window has lost ownership of
* the selection and the ownership was claimed with the command
* "selection own".
*
* Results:
* None.
*
* Side effects:
* A Tcl script is executed; it can do almost anything.
*
*----------------------------------------------------------------------
*/
static void
{
char *oldResultString;
/*
* Execute the command. Save the interpreter's result, if any, and
* restore it after executing the command.
*/
if (oldFreeProc != TCL_STATIC) {
} else {
oldResultString = (char *) ckalloc((unsigned)
}
}
/*
* Free the storage for the command, since we're done with it now.
*/
}