home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / bitmap / CutPaste.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  5.5 KB  |  217 lines

  1. /*
  2.  * $XConsortium: CutPaste.c,v 1.6 91/07/24 15:33:34 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Davor Matic, MIT X Consortium
  24.  */
  25.  
  26. #include <X11/IntrinsicP.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Xatom.h>
  29. #include "BitmapP.h"
  30.     
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <math.h>
  34.  
  35. #ifndef abs
  36. #define abs(x)                        (((x) > 0) ? (x) : -(x))
  37. #endif
  38. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  39. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  40.  
  41.  
  42. extern Boolean DEBUG;
  43.  
  44. /*****************************************************************************
  45.  *                               Cut and Paste                               *
  46.  *****************************************************************************/
  47.  
  48. /* ARGSUSED */
  49. Boolean ConvertSelection(w, selection, target, type, val_ret, length, format)
  50.     Widget w;
  51.     Atom *selection, *target, *type;
  52.     XtPointer *val_ret;
  53.     unsigned long *length;
  54.     int *format;
  55. {
  56.     caddr_t *value = (caddr_t *)val_ret;
  57.     BitmapWidget BW = (BitmapWidget) w;
  58.     Pixmap *pixmap;
  59.     char *data;
  60.     XImage *image;
  61.     Dimension width, height;
  62.  
  63.     switch (*target) {
  64.  
  65. /*  XA_TARGETS undefined ?!?
  66.  
  67.     case XA_TARGETS:
  68.     *type = XA_ATOM;
  69.     *value = (caddr_t) bitmapClassRec.bitmap_class.targets;
  70.     *length = bitmapClassRec.bitmap_class.num_targets;
  71.     *format = 32;
  72.     return True;
  73.  
  74. */
  75.  
  76.     case XA_BITMAP:
  77.     case XA_PIXMAP:
  78.     if (BWQueryMarked(w)) {
  79.       width = BW->bitmap.mark.to_x - BW->bitmap.mark.from_x + 1;
  80.       height = BW->bitmap.mark.to_y - BW->bitmap.mark.from_y + 1;
  81.       data = CreateCleanData(Length(width, height));
  82.       image = CreateBitmapImage(BW, data, width, height);
  83.       CopyImageData(BW->bitmap.image, image, 
  84.             BW->bitmap.mark.from_x, BW->bitmap.mark.from_y,
  85.             BW->bitmap.mark.to_x, BW->bitmap.mark.to_y, 0, 0);
  86.       pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
  87.       *pixmap = GetPixmap(BW, image);
  88.       DestroyBitmapImage(&image);
  89.     }
  90.     else if (BWQueryStored(w)) {
  91.       pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
  92.       *pixmap = GetPixmap(BW, BW->bitmap.storage);
  93.     }
  94.     else return False;
  95.     *type = XA_PIXMAP;
  96.     *value = (caddr_t) pixmap;
  97.     *length = 1;
  98.     *format = 32;
  99.     return True;
  100.  
  101.     default:
  102.     return False;
  103.     }
  104. }
  105.  
  106. /* ARGSUSED */
  107. void LoseSelection(w, selection)
  108.     Widget w;
  109.     Atom *selection;
  110. {
  111.     BitmapWidget BW = (BitmapWidget) w;
  112.  
  113.     if (DEBUG)
  114.     fprintf(stderr, "Lost Selection\n");
  115.     BW->bitmap.selection.own = False;
  116.  
  117.     BWUnmark(w);
  118. }
  119.  
  120. /* ARGSUSED */
  121. void SelectionDone(w, selection, target)
  122.     Widget w;
  123.     Atom *selection, *target;
  124. {
  125. /*  Done Automatically ?!?
  126.  
  127.     BitmapWidget BW = (BitmapWidget) w; 
  128.  
  129.     if (*target != XA_TARGETS)
  130.     XtFree(BW->bitmap.value);
  131.  
  132. */
  133. }
  134.  
  135. void BWGrabSelection(w, btime)
  136.     Widget w;
  137.     Time btime;
  138. {
  139.     BitmapWidget BW = (BitmapWidget) w;
  140.  
  141.     BW->bitmap.selection.own = XtOwnSelection(w, XA_PRIMARY, btime,
  142.                           ConvertSelection, 
  143.                           LoseSelection, 
  144.                           SelectionDone);
  145.     if (DEBUG && BW->bitmap.selection.own)
  146.         fprintf(stderr, "Own the selection\n");
  147. }
  148.  
  149. XImage *GetImage();
  150.  
  151. /* ARGSUSED */
  152. void SelectionCallback(w, cldat, selection, type, val, length, format)
  153.     Widget w;
  154.     XtPointer cldat;
  155.     Atom *selection, *type;
  156.     XtPointer val;
  157.     unsigned long *length;
  158.     int *format;
  159. {
  160.     caddr_t value = (caddr_t)val;
  161.     BitmapWidget BW = (BitmapWidget) w;
  162.     Pixmap *pixmap;
  163.  
  164.    switch (*type) {
  165.     
  166.     case XA_BITMAP:
  167.     case XA_PIXMAP:
  168.     DestroyBitmapImage(&BW->bitmap.storage);
  169.     pixmap = (Pixmap *) value;
  170.     BW->bitmap.storage = GetImage(BW, *pixmap);
  171.     XFree((char *)pixmap);
  172.     break;
  173.     
  174.     default:
  175.     XtWarning(" selection request failed.  BitmapWidget");
  176.     break;
  177.     }
  178.  
  179.     BW->bitmap.selection.limbo = FALSE;
  180. }
  181.  
  182. void BWRequestSelection(w, btime, wait)
  183.     Widget w;
  184.     Time btime;
  185.     Boolean wait;
  186. {
  187.   BitmapWidget BW = (BitmapWidget) w;
  188.   
  189.   if (BW->bitmap.selection.own)
  190.     BWStore(w);
  191.   else {
  192.     XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP,
  193.             SelectionCallback, NULL, btime);
  194.     
  195.     BW->bitmap.selection.limbo = TRUE;
  196.     
  197.     if (wait)
  198.       while (BW->bitmap.selection.limbo) {
  199.     XEvent event;
  200.     XtNextEvent(&event);
  201.     XtDispatchEvent(&event);
  202.       }
  203.   }
  204. }
  205.  
  206. /* ARGSUSED */
  207. /* Returns true if there is a transferable selection */
  208. Boolean BWQuerySelection(w, btime)
  209.     Widget w;
  210.     Time btime;
  211. {
  212. /* To be written.  XA_TARGETS to be used.  So far undefined ?!? */
  213.  
  214.   return True;
  215. }
  216. /*****************************************************************************/
  217.