home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / xpaint-247 / pencilop.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  5KB  |  198 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. /* $Id: pencilOp.c,v 1.3 1996/04/19 08:53:17 torsten Exp $ */
  16.  
  17. #include <X11/Intrinsic.h>
  18. #include <X11/StringDefs.h>
  19. #include <X11/cursorfont.h>
  20. #include "xpaint.h"
  21. #include "Paint.h"
  22. #include "misc.h"
  23. #include "ops.h"
  24.  
  25. typedef struct {
  26.     Boolean isDots;
  27.     int startX, startY;
  28.     GC gc;
  29. } LocalInfo;
  30.  
  31. static void 
  32. press(Widget w, LocalInfo * l, XButtonEvent * event, OpInfo * info)
  33. {
  34.     XRectangle undo;
  35.  
  36.     if (info->surface == opWindow)
  37.     return;
  38.  
  39.     if ((event->state & AllButtonsMask) != 0)
  40.     return;
  41.  
  42.     l->startX = event->x;
  43.     l->startY = event->y;
  44.  
  45.     undo.x = event->x;
  46.     undo.y = event->y;
  47.     undo.width = 1;
  48.     undo.height = 1;
  49.  
  50.     if (event->button == Button2)
  51.     l->gc = info->second_gc;
  52.     else
  53.     l->gc = info->first_gc;
  54.     if (l->isDots) {
  55.     GC gc = XCreateGC(XtDisplay(w), info->drawable, 0, 0);
  56.     XCopyGC(XtDisplay(w), l->gc, ~GCLineWidth, gc);
  57.     l->gc = gc;
  58.     }
  59.     UndoStartPoint(w, info, event->x, event->y);
  60.  
  61.     XDrawLine(XtDisplay(w), info->drawable, l->gc,
  62.           l->startX, l->startY, event->x, event->y);
  63.     if (!info->isFat)
  64.     XDrawLine(XtDisplay(w), XtWindow(w), l->gc,
  65.           l->startX, l->startY, event->x, event->y);
  66.  
  67.     PwUpdate(w, &undo, False);
  68. }
  69.  
  70. static void 
  71. motion(Widget w, LocalInfo * l, XMotionEvent * event, OpInfo * info)
  72. {
  73.     XRectangle undo;
  74.  
  75.     if (info->surface == opWindow)
  76.     return;
  77.  
  78.     if (l->isDots) {
  79.     l->startX = event->x;
  80.     l->startY = event->y;
  81.     }
  82.     XDrawLine(XtDisplay(w), info->drawable, l->gc,
  83.           l->startX, l->startY, event->x, event->y);
  84.     if (!info->isFat)
  85.     XDrawLine(XtDisplay(w), XtWindow(w), l->gc,
  86.           l->startX, l->startY, event->x, event->y);
  87.  
  88.     UndoGrow(w, event->x, event->y);
  89.  
  90.     undo.x = MIN(l->startX, event->x);
  91.     undo.y = MIN(l->startY, event->y);
  92.     undo.width = MAX(l->startX, event->x) - undo.x + 1;
  93.     undo.height = MAX(l->startY, event->y) - undo.y + 1;
  94.  
  95.     l->startX = event->x;
  96.     l->startY = event->y;
  97.  
  98.     PwUpdate(w, &undo, False);
  99. }
  100. static void 
  101. release(Widget w, LocalInfo * l, XButtonEvent * event, OpInfo * info)
  102. {
  103.     int mask;
  104.     /*
  105.     **  Check to make sure all buttons are up, before doing this
  106.      */
  107.     mask = AllButtonsMask;
  108.     switch (event->button) {
  109.     case Button1:
  110.     mask ^= Button1Mask;
  111.     break;
  112.     case Button2:
  113.     mask ^= Button2Mask;
  114.     break;
  115.     case Button3:
  116.     mask ^= Button3Mask;
  117.     break;
  118.     case Button4:
  119.     mask ^= Button4Mask;
  120.     break;
  121.     case Button5:
  122.     mask ^= Button5Mask;
  123.     break;
  124.     }
  125.     if ((event->state & mask) != 0)
  126.     return;
  127.  
  128.     if (l->isDots) {
  129.     XFreeGC(XtDisplay(w), l->gc);
  130.     l->gc = None;
  131.     }
  132. }
  133.  
  134. /*
  135. **  Those public functions
  136.  */
  137. void *
  138. PencilAdd(Widget w)
  139. {
  140.     LocalInfo *l = (LocalInfo *) XtMalloc(sizeof(LocalInfo));
  141.  
  142.     l->isDots = False;
  143.     XtVaSetValues(w, XtNcompress, False, NULL);
  144.  
  145.     OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE,
  146.               (OpEventProc) press, l);
  147.     OpAddEventHandler(w, opPixmap, ButtonMotionMask, FALSE,
  148.               (OpEventProc) motion, l);
  149.     OpAddEventHandler(w, opPixmap, ButtonReleaseMask, FALSE,
  150.               (OpEventProc) release, l);
  151.  
  152.     SetPencilCursor(w);
  153.  
  154.     return l;
  155. }
  156. void 
  157. PencilRemove(Widget w, void *l)
  158. {
  159.     OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE,
  160.              (OpEventProc) press, l);
  161.     OpRemoveEventHandler(w, opPixmap, ButtonMotionMask, FALSE,
  162.              (OpEventProc) motion, l);
  163.     OpRemoveEventHandler(w, opPixmap, ButtonReleaseMask, FALSE,
  164.              (OpEventProc) release, l);
  165.     XtFree((XtPointer) l);
  166. }
  167.  
  168. void *
  169. DotPencilAdd(Widget w)
  170. {
  171.     LocalInfo *l = (LocalInfo *) XtMalloc(sizeof(LocalInfo));
  172.  
  173.     l->isDots = True;
  174.     XtVaSetValues(w, XtNcompress, True, NULL);
  175.  
  176.     OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE,
  177.               (OpEventProc) press, l);
  178.     OpAddEventHandler(w, opPixmap, ButtonMotionMask, FALSE,
  179.               (OpEventProc) motion, l);
  180.     OpAddEventHandler(w, opPixmap, ButtonReleaseMask, FALSE,
  181.               (OpEventProc) release, l);
  182.  
  183.     SetPencilCursor(w);
  184.  
  185.     return l;
  186. }
  187. void 
  188. DotPencilRemove(Widget w, void *l)
  189. {
  190.     OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE,
  191.              (OpEventProc) press, l);
  192.     OpRemoveEventHandler(w, opPixmap, ButtonMotionMask, FALSE,
  193.              (OpEventProc) motion, l);
  194.     OpRemoveEventHandler(w, opPixmap, ButtonReleaseMask, FALSE,
  195.              (OpEventProc) release, l);
  196.     XtFree((XtPointer) l);
  197. }
  198.