home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / mac / tkMacClipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  6.7 KB  |  294 lines  |  [TEXT/CWIE]

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