tkMacClipboard.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * tkMacClipboard.c --
  3.  *
  4.  *  This file manages the clipboard for the Tk toolkit.
  5.  *
  6.  * Copyright (c) 1995-1997 Sun Microsystems, Inc.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * RCS: @(#) $Id: tkMacClipboard.c,v 1.6 2000/02/10 08:53:11 jingham Exp $
  12.  */
  13. #include "tkInt.h"
  14. #include "tkPort.h"
  15. #include "tkMacInt.h"
  16. #include <Scrap.h>
  17. #include <Events.h>
  18. #include "tkSelect.h"
  19. /*
  20.  *----------------------------------------------------------------------
  21.  *
  22.  * TkSelGetSelection --
  23.  *
  24.  * Retrieve the specified selection from another process.  For
  25.  * now, only fetching XA_STRING from CLIPBOARD is supported.
  26.  * Eventually other types should be allowed.
  27.  * 
  28.  * Results:
  29.  * The return value is a standard Tcl return value.
  30.  * If an error occurs (such as no selection exists)
  31.  * then an error message is left in the interp's result.
  32.  *
  33.  * Side effects:
  34.  * None.
  35.  *
  36.  *----------------------------------------------------------------------
  37.  */
  38. int
  39. TkSelGetSelection(
  40.     Tcl_Interp *interp, /* Interpreter to use for reporting
  41.  * errors. */
  42.     Tk_Window tkwin, /* Window on whose behalf to retrieve
  43.  * the selection (determines display
  44.  * from which to retrieve). */
  45.     Atom selection, /* Selection to retrieve. */
  46.     Atom target, /* Desired form in which selection
  47.  * is to be returned. */
  48.     Tk_GetSelProc *proc, /* Procedure to call to process the
  49.  * selection, once it has been retrieved. */
  50.     ClientData clientData) /* Arbitrary value to pass to proc. */
  51. {
  52.     int result;
  53.     long length, offset = 0;
  54.     Handle handle;
  55.     if ((selection == Tk_InternAtom(tkwin, "CLIPBOARD"))
  56.     && (target == XA_STRING)) {
  57. /* 
  58.  * Get the scrap from the Macintosh global clipboard.
  59.  */
  60. handle = NewHandle(1);
  61. length = GetScrap(handle, 'TEXT', &offset);
  62. if (length > 0) {
  63.     Tcl_DString encodedText;
  64.     SetHandleSize(handle, (Size) length + 1);
  65.     HLock(handle);
  66.     (*handle)[length] = '';
  67.     Tcl_ExternalToUtfDString(NULL, *handle, length, &encodedText);
  68.     result = (*proc)(clientData, interp,
  69.     Tcl_DStringValue(&encodedText));
  70.     Tcl_DStringFree(&encodedText);
  71.     HUnlock(handle);
  72.     DisposeHandle(handle);
  73.     return result;
  74. }
  75. DisposeHandle(handle);
  76.     }
  77.     
  78.     Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection),
  79. " selection doesn't exist or form "", Tk_GetAtomName(tkwin, target),
  80. "" not defined", (char *) NULL);
  81.     return TCL_ERROR;
  82. }
  83. /*
  84.  *----------------------------------------------------------------------
  85.  *
  86.  * TkSetSelectionOwner --
  87.  *
  88.  * This function claims ownership of the specified selection.
  89.  * If the selection is CLIPBOARD, then we empty the system
  90.  * clipboard.
  91.  *
  92.  * Results:
  93.  * None.
  94.  *
  95.  * Side effects:
  96.  * None.
  97.  *
  98.  *----------------------------------------------------------------------
  99.  */
  100. void
  101. XSetSelectionOwner(
  102.     Display* display, /* X Display. */
  103.     Atom selection, /* What selection to own. */
  104.     Window owner, /* Window to be the owner. */
  105.     Time time) /* The current time? */
  106. {
  107.     Tk_Window tkwin;
  108.     TkDisplay *dispPtr;
  109.     /*
  110.      * This is a gross hack because the Tk_InternAtom interface is broken.
  111.      * It expects a Tk_Window, even though it only needs a Tk_Display.
  112.      */
  113.     tkwin = (Tk_Window) TkGetMainInfoList()->winPtr;
  114.     if (selection == Tk_InternAtom(tkwin, "CLIPBOARD")) {
  115. /*
  116.  * Only claim and empty the clipboard if we aren't already the
  117.  * owner of the clipboard.
  118.  */
  119. dispPtr = TkGetMainInfoList()->winPtr->dispPtr;
  120. if (dispPtr->clipboardActive) {
  121.     return;
  122. }
  123. ZeroScrap();
  124.     }
  125. }
  126. /*
  127.  *----------------------------------------------------------------------
  128.  *
  129.  * TkSelUpdateClipboard --
  130.  *
  131.  * This function is called to force the clipboard to be updated
  132.  * after new data is added.  On the Mac we don't need to do
  133.  * anything.
  134.  *
  135.  * Results:
  136.  * None.
  137.  *
  138.  * Side effects:
  139.  * None.
  140.  *
  141.  *----------------------------------------------------------------------
  142.  */
  143. void
  144. TkSelUpdateClipboard(
  145.     TkWindow *winPtr, /* Window associated with clipboard. */
  146.     TkClipboardTarget *targetPtr) /* Info about the content. */
  147. {
  148. }
  149. /*
  150.  *--------------------------------------------------------------
  151.  *
  152.  * TkSelEventProc --
  153.  *
  154.  * This procedure is invoked whenever a selection-related
  155.  * event occurs. 
  156.  *
  157.  * Results:
  158.  * None.
  159.  *
  160.  * Side effects:
  161.  * Lots:  depends on the type of event.
  162.  *
  163.  *--------------------------------------------------------------
  164.  */
  165. void
  166. TkSelEventProc(
  167.     Tk_Window tkwin, /* Window for which event was
  168.  * targeted. */
  169.     register XEvent *eventPtr) /* X event:  either SelectionClear,
  170.  * SelectionRequest, or
  171.  * SelectionNotify. */
  172. {
  173.     if (eventPtr->type == SelectionClear) {
  174. TkSelClearSelection(tkwin, eventPtr);
  175.     }
  176. }
  177. /*
  178.  *----------------------------------------------------------------------
  179.  *
  180.  * TkSelPropProc --
  181.  *
  182.  * This procedure is invoked when property-change events
  183.  * occur on windows not known to the toolkit.  This is a stub
  184.  * function under Windows.
  185.  *
  186.  * Results:
  187.  * None.
  188.  *
  189.  * Side effects:
  190.  * None.
  191.  *
  192.  *----------------------------------------------------------------------
  193.  */
  194. void
  195. TkSelPropProc(
  196.     register XEvent *eventPtr) /* X PropertyChange event. */
  197. {
  198. }
  199. /*
  200.  *----------------------------------------------------------------------
  201.  *
  202.  * TkSuspendClipboard --
  203.  *
  204.  * Handle clipboard conversion as required by the suppend event.
  205.  * This function is also called on exit.
  206.  *
  207.  * Results:
  208.  * None.
  209.  *
  210.  * Side effects:
  211.  * The local scrap is moved to the global scrap.
  212.  *
  213.  *----------------------------------------------------------------------
  214.  */
  215. void
  216. TkSuspendClipboard()
  217. {
  218.     TkClipboardTarget *targetPtr;
  219.     TkClipboardBuffer *cbPtr;
  220.     TkDisplay *dispPtr;
  221.     char *buffer, *p, *endPtr, *buffPtr;
  222.     long length;
  223.     dispPtr = TkGetDisplayList();
  224.     if ((dispPtr == NULL) || !dispPtr->clipboardActive) {
  225. return;
  226.     }
  227.     for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
  228.     targetPtr = targetPtr->nextPtr) {
  229. if (targetPtr->type == XA_STRING)
  230.     break;
  231.     }
  232.     if (targetPtr != NULL) {
  233. Tcl_DString encodedText;
  234. length = 0;
  235. for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
  236. cbPtr = cbPtr->nextPtr) {
  237.     length += cbPtr->length;
  238. }
  239. buffer = ckalloc(length);
  240. buffPtr = buffer;
  241. for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
  242. cbPtr = cbPtr->nextPtr) {
  243.     for (p = cbPtr->buffer, endPtr = p + cbPtr->length;
  244.     p < endPtr; p++) {
  245. if (*p == 'n') {
  246.     *buffPtr++ = 'r';
  247. } else {
  248.     *buffPtr++ = *p;
  249. }
  250.     }
  251. }
  252. ZeroScrap();
  253. Tcl_UtfToExternalDString(NULL, buffer, length, &encodedText);
  254. PutScrap(Tcl_DStringLength(&encodedText), 'TEXT',
  255. Tcl_DStringValue(&encodedText));
  256. Tcl_DStringFree(&encodedText);
  257. ckfree(buffer);
  258.     }
  259.     /*
  260.      * The system now owns the scrap.  We tell Tk that it has
  261.      * lost the selection so that it will look for it the next time
  262.      * it needs it.  (Window list NULL if quiting.)
  263.      */
  264.     if (TkGetMainInfoList() != NULL) {
  265. Tk_ClearSelection((Tk_Window) TkGetMainInfoList()->winPtr, 
  266. Tk_InternAtom((Tk_Window) TkGetMainInfoList()->winPtr,
  267. "CLIPBOARD"));
  268.     }
  269.     return;
  270. }