home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / win / tkWinClipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-17  |  6.6 KB  |  282 lines

  1. /* 
  2.  * tkWinClipboard.c --
  3.  *
  4.  *    This file contains functions for managing the clipboard.
  5.  *
  6.  * Copyright (c) 1995 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: @(#) tkWinClipboard.c 1.5 96/02/15 18:56:07
  12.  */
  13.  
  14. #include "tkWinInt.h"
  15. #include "tkSelect.h"
  16.  
  17.  
  18. /*
  19.  *----------------------------------------------------------------------
  20.  *
  21.  * TkSelGetSelection --
  22.  *
  23.  *    Retrieve the specified selection from another process.  For
  24.  *    now, only fetching XA_STRING from CLIPBOARD is supported.
  25.  *    Eventually other types should be allowed.
  26.  * 
  27.  * Results:
  28.  *    The return value is a standard Tcl return value.
  29.  *    If an error occurs (such as no selection exists)
  30.  *    then an error message is left in interp->result.
  31.  *
  32.  * Side effects:
  33.  *    None.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. int
  39. TkSelGetSelection(interp, tkwin, selection, target, proc, clientData)
  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.     char *data, *buffer, *destPtr;
  53.     HGLOBAL handle;
  54.     int result, length;
  55.  
  56.     if ((selection == Tk_InternAtom(tkwin, "CLIPBOARD"))
  57.         && (target == XA_STRING)) {
  58.     if (OpenClipboard(NULL)) {
  59.         handle = GetClipboardData(CF_TEXT);
  60.         if (handle != NULL) {
  61.         data = GlobalLock(handle);
  62.         length = strlen(data);
  63.         buffer = ckalloc(length+1);
  64.         destPtr = buffer;
  65.         while (*data != '\0') {
  66.             if (*data != '\r') {
  67.             *destPtr = *data;
  68.             destPtr++;
  69.             }
  70.             data++;
  71.         }
  72.         *destPtr = '\0';
  73.         GlobalUnlock(handle);
  74.         CloseClipboard();
  75.         result = (*proc)(clientData, interp, buffer);
  76.         ckfree(buffer);
  77.         return result;
  78.         }
  79.         CloseClipboard();
  80.     }
  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.  *    Empties the system clipboard, and claims ownership.
  103.  *
  104.  *----------------------------------------------------------------------
  105.  */
  106.  
  107. void
  108. XSetSelectionOwner(display, selection, owner, time)
  109.     Display* display;
  110.     Atom selection;
  111.     Window owner;
  112.     Time time;
  113. {
  114.     HWND hwnd = owner ? TkWinGetHWND(owner) : NULL;
  115.     Tk_Window tkwin;
  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.     if (GetClipboardOwner() != hwnd) {
  132.         OpenClipboard(hwnd);
  133.         EmptyClipboard();
  134.         SetClipboardData(CF_TEXT, NULL);
  135.         CloseClipboard();
  136.     }
  137.     }
  138. }
  139.  
  140. /*
  141.  *----------------------------------------------------------------------
  142.  *
  143.  * TkWinClipboardRender --
  144.  *
  145.  *    This function supplies the contents of the clipboard in
  146.  *    response to a WM_RENDERFORMAT message.
  147.  *
  148.  * Results:
  149.  *    None.
  150.  *
  151.  * Side effects:
  152.  *    Sets the contents of the clipboard.
  153.  *
  154.  *----------------------------------------------------------------------
  155.  */
  156.  
  157. void
  158. TkWinClipboardRender(winPtr, format)
  159.     TkWindow *winPtr;
  160.     UINT format;
  161. {
  162.     TkClipboardTarget *targetPtr;
  163.     TkClipboardBuffer *cbPtr;
  164.     TkDisplay *dispPtr = winPtr->dispPtr;
  165.     HGLOBAL handle;
  166.     char *buffer;
  167.     int length;
  168.  
  169.     for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
  170.         targetPtr = targetPtr->nextPtr) {
  171.     if (targetPtr->type == XA_STRING)
  172.         break;
  173.     }
  174.     length = 0;
  175.     if (targetPtr != NULL) {
  176.     for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
  177.         cbPtr = cbPtr->nextPtr) {
  178.         length += cbPtr->length;
  179.     }
  180.     }
  181.     handle = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, length+1);
  182.     if (!handle) {
  183.     return;
  184.     }
  185.     buffer = GlobalLock(handle);
  186.     if (targetPtr != NULL) {
  187.     for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
  188.         cbPtr = cbPtr->nextPtr) {
  189.         strncpy(buffer, cbPtr->buffer, cbPtr->length);
  190.         buffer += cbPtr->length;
  191.     }
  192.     }
  193.     *buffer = '\0';
  194.     GlobalUnlock(handle);
  195.     SetClipboardData(CF_TEXT, handle);
  196.     return;
  197. }
  198.  
  199. /*
  200.  *----------------------------------------------------------------------
  201.  *
  202.  * TkSelUpdateClipboard --
  203.  *
  204.  *    This function is called to force the clipboard to be updated
  205.  *    after new data is added.
  206.  *
  207.  * Results:
  208.  *    None.
  209.  *
  210.  * Side effects:
  211.  *    Clears the current contents of the clipboard.
  212.  *
  213.  *----------------------------------------------------------------------
  214.  */
  215.  
  216. void
  217. TkSelUpdateClipboard(winPtr, targetPtr)
  218.     TkWindow *winPtr;
  219.     TkClipboardTarget *targetPtr;
  220. {
  221.     HWND hwnd = TkWinGetHWND(winPtr->window);
  222.  
  223.     OpenClipboard(hwnd);
  224.     EmptyClipboard();
  225.     SetClipboardData(CF_TEXT, NULL);
  226.     CloseClipboard();
  227. }
  228.  
  229. /*
  230.  *--------------------------------------------------------------
  231.  *
  232.  * TkSelEventProc --
  233.  *
  234.  *    This procedure is invoked whenever a selection-related
  235.  *    event occurs. 
  236.  *
  237.  * Results:
  238.  *    None.
  239.  *
  240.  * Side effects:
  241.  *    Lots:  depends on the type of event.
  242.  *
  243.  *--------------------------------------------------------------
  244.  */
  245.  
  246. void
  247. TkSelEventProc(tkwin, eventPtr)
  248.     Tk_Window tkwin;        /* Window for which event was
  249.                  * targeted. */
  250.     register XEvent *eventPtr;    /* X event:  either SelectionClear,
  251.                  * SelectionRequest, or
  252.                  * SelectionNotify. */
  253. {
  254.     if (eventPtr->type == SelectionClear) {
  255.     TkSelClearSelection(tkwin, eventPtr);
  256.     }
  257. }
  258.  
  259. /*
  260.  *----------------------------------------------------------------------
  261.  *
  262.  * TkSelPropProc --
  263.  *
  264.  *    This procedure is invoked when property-change events
  265.  *    occur on windows not known to the toolkit.  This is a stub
  266.  *    function under Windows.
  267.  *
  268.  * Results:
  269.  *    None.
  270.  *
  271.  * Side effects:
  272.  *    None.
  273.  *
  274.  *----------------------------------------------------------------------
  275.  */
  276.  
  277. void
  278. TkSelPropProc(eventPtr)
  279.     register XEvent *eventPtr;        /* X PropertyChange event. */
  280. {
  281. }
  282.