📄 tkclipboard.c
字号:
/* * tkClipboard.c -- * * This file manages the clipboard for the Tk toolkit, * maintaining a collection of data buffers that will be * supplied on demand to requesting applications. * * Copyright (c) 1994 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: @(#) tkClipboard.c 1.15 96/05/03 10:51:08 */#include "tkInt.h"#include "tkPort.h"#include "tkSelect.h"/* * Prototypes for procedures used only in this file: */static int ClipboardAppHandler _ANSI_ARGS_((ClientData clientData, int offset, char *buffer, int maxBytes));static int ClipboardHandler _ANSI_ARGS_((ClientData clientData, int offset, char *buffer, int maxBytes));static int ClipboardWindowHandler _ANSI_ARGS_(( ClientData clientData, int offset, char *buffer, int maxBytes));static void ClipboardLostSel _ANSI_ARGS_((ClientData clientData));/* *---------------------------------------------------------------------- * * ClipboardHandler -- * * This procedure acts as selection handler for the * clipboard manager. It extracts the required chunk of * data from the buffer chain for a given selection target. * * Results: * The return value is a count of the number of bytes * actually stored at buffer. * * Side effects: * None. * *---------------------------------------------------------------------- */static intClipboardHandler(clientData, offset, buffer, maxBytes) ClientData clientData; /* Information about data to fetch. */ 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. */{ TkClipboardTarget *targetPtr = (TkClipboardTarget*) clientData; TkClipboardBuffer *cbPtr; char *srcPtr, *destPtr; int count = 0; int scanned = 0; size_t length, freeCount; /* * Skip to buffer containing offset byte */ for (cbPtr = targetPtr->firstBufferPtr; ; cbPtr = cbPtr->nextPtr) { if (cbPtr == NULL) { return 0; } if (scanned + cbPtr->length > offset) { break; } scanned += cbPtr->length; } /* * Copy up to maxBytes or end of list, switching buffers as needed. */ freeCount = maxBytes; srcPtr = cbPtr->buffer + (offset - scanned); destPtr = buffer; length = cbPtr->length - (offset - scanned); while (1) { if (length > freeCount) { strncpy(destPtr, srcPtr, freeCount); return maxBytes; } else { strncpy(destPtr, srcPtr, length); destPtr += length; count += length; freeCount -= length; } cbPtr = cbPtr->nextPtr; if (cbPtr == NULL) { break; } srcPtr = cbPtr->buffer; length = cbPtr->length; } return count;}/* *---------------------------------------------------------------------- * * ClipboardAppHandler -- * * This procedure acts as selection handler for retrievals of type * TK_APPLICATION. It returns the name of the application that * owns the clipboard. Note: we can't use the default Tk * selection handler for this selection type, because the clipboard * window isn't a "real" window and doesn't have the necessary * information. * * Results: * The return value is a count of the number of bytes * actually stored at buffer. * * Side effects: * None. * *---------------------------------------------------------------------- */static intClipboardAppHandler(clientData, offset, buffer, maxBytes) ClientData clientData; /* Pointer to TkDisplay structure. */ 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. */{ TkDisplay *dispPtr = (TkDisplay *) clientData; size_t length; char *p; p = dispPtr->clipboardAppPtr->winPtr->nameUid; length = strlen(p); length -= offset; if (length <= 0) { return 0; } if (length > (size_t) maxBytes) { length = maxBytes; } strncpy(buffer, p, length); return length;}/* *---------------------------------------------------------------------- * * ClipboardWindowHandler -- * * This procedure acts as selection handler for retrievals of * type TK_WINDOW. Since the clipboard doesn't correspond to * any particular window, we just return ".". We can't use Tk's * default handler for this selection type, because the clipboard * window isn't a valid window. * * Results: * The return value is 1, the number of non-null bytes stored * at buffer. * * Side effects: * None. * *---------------------------------------------------------------------- */static intClipboardWindowHandler(clientData, offset, buffer, maxBytes) ClientData clientData; /* Not used. */ 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. */{ buffer[0] = '.'; buffer[1] = 0; return 1;}/* *---------------------------------------------------------------------- * * ClipboardLostSel -- * * This procedure is invoked whenever clipboard ownership is * claimed by another window. It just sets a flag so that we * know the clipboard was taken away. * * Results: * None. * * Side effects: * The clipboard is marked as inactive. * *---------------------------------------------------------------------- */static voidClipboardLostSel(clientData) ClientData clientData; /* Pointer to TkDisplay structure. */{ TkDisplay *dispPtr = (TkDisplay*) clientData; dispPtr->clipboardActive = 0;}/* *---------------------------------------------------------------------- * * Tk_ClipboardClear -- * * Take control of the clipboard and clear out the previous * contents. This procedure must be invoked before any * calls to Tk_AppendToClipboard. * * Results: * A standard Tcl result. If an error occurs, an error message is * left in interp->result. * * Side effects: * From now on, requests for the CLIPBOARD selection will be * directed to the clipboard manager routines associated with * clipWindow for the display of tkwin. In order to guarantee * atomicity, no event handling should occur between * Tk_ClipboardClear and the following Tk_AppendToClipboard * calls. This procedure may cause a user-defined LostSel command * to be invoked when the CLIPBOARD is claimed, so any calling * function should be reentrant at the point Tk_ClipboardClear is * invoked. * *---------------------------------------------------------------------- */intTk_ClipboardClear(interp, tkwin) Tcl_Interp *interp; /* Interpreter to use for error reporting. */ Tk_Window tkwin; /* Window in application that is clearing * clipboard; identifies application and * display. */{ TkWindow *winPtr = (TkWindow *) tkwin; TkDisplay *dispPtr = winPtr->dispPtr; TkClipboardTarget *targetPtr, *nextTargetPtr; TkClipboardBuffer *cbPtr, *nextCbPtr; if (dispPtr->clipWindow == NULL) { int result; result = TkClipInit(interp, dispPtr); if (result != TCL_OK) { return result; } } /* * Discard any existing clipboard data and delete the selection * handler(s) associated with that data. */ for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL; targetPtr = nextTargetPtr) { for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL; cbPtr = nextCbPtr) { ckfree(cbPtr->buffer); nextCbPtr = cbPtr->nextPtr; ckfree((char *) cbPtr); } nextTargetPtr = targetPtr->nextPtr; Tk_DeleteSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom, targetPtr->type); ckfree((char *) targetPtr); } dispPtr->clipTargetPtr = NULL; /* * Reclaim the clipboard selection if we lost it. */ if (!dispPtr->clipboardActive) { Tk_OwnSelection(dispPtr->clipWindow, dispPtr->clipboardAtom, ClipboardLostSel, (ClientData) dispPtr); dispPtr->clipboardActive = 1; } dispPtr->clipboardAppPtr = winPtr->mainPtr; return TCL_OK;}/* *---------------------------------------------------------------------- * * Tk_ClipboardAppend -- * * Append a buffer of data to the clipboard. The first buffer of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -