home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d386 / xlispstat.lha / XLispStat / src3.lzh / UNIX / X11scroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  7.6 KB  |  232 lines

  1. /* X11buttons - buttons for X11 dialogs and windows                    */
  2. /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
  3. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  4. /* You may give out copies of this software; for conditions see the    */
  5. /* file COPYING included with this distribution.                       */
  6.  
  7. /***********************************************************************/
  8. /**                                                                   **/
  9. /**                    General Includes and Definitions               **/
  10. /**                                                                   **/
  11. /***********************************************************************/
  12.  
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/Xos.h>
  16.  
  17. #include "xlisp.h"
  18. #include "StGWWindow.h"
  19.  
  20. extern Display *StX11Display();
  21. extern LVAL StX11ItemObject();
  22.  
  23. #define nil 0L
  24.  
  25. extern XContext EventContext, ObjectContext, ThumbContext, ScrollActionContext;
  26. extern Cursor DoubleArrowCursor, RightArrowCursor, LeftArrowCursor;
  27. extern Cursor UpDownArrowCursor, UpArrowCursor, DownArrowCursor;
  28. extern Pixmap ScrollThumbPM;
  29.  
  30. /***********************************************************************/
  31. /**                                                                   **/
  32. /**                           Event Handler                           **/
  33. /**                                                                   **/
  34. /***********************************************************************/
  35.  
  36. static LVAL scroll_handler(report, modal)
  37.      XEvent report;
  38.      int modal;
  39. {
  40.   Display *dpy = StX11Display();
  41.   Window s;
  42.   LVAL object;
  43.   int up, is_vertical, x, y, old_x, old_y, (*action)();
  44.  
  45.   s = report.xany.window;
  46.   if (XFindContext(dpy, s, ObjectContext, &object) == 0 && objectp(object)) {
  47.     is_vertical = is_vertical_scroll_bar(s);
  48.     if (XFindContext(dpy, s, ScrollActionContext, &action) != 0)
  49.       xlfail("can't find thumb context");
  50.     switch (report.type) {
  51.     case ButtonPress:
  52.       switch (report.xbutton.button) {
  53.       case Button1:
  54.       case Button3:
  55.     up = (report.xbutton.button == Button1) ? TRUE : FALSE;
  56.     if (is_vertical)
  57.       XDefineCursor(dpy, s, (up) ? UpArrowCursor : DownArrowCursor);
  58.     else 
  59.       XDefineCursor(dpy, s, (up) ? LeftArrowCursor : RightArrowCursor);
  60.     if (action != nil)
  61.       (*action)(object, s, (up) ? 'L' : 'R',
  62.             report.xbutton.x, report.xbutton.y);
  63.     while (! XCheckTypedEvent(dpy, ButtonRelease, &report));
  64.     if (is_vertical) XDefineCursor(dpy, s, UpDownArrowCursor);
  65.     else XDefineCursor(dpy, s, DoubleArrowCursor);
  66.     break;
  67.       case Button2:
  68.     if (action != nil)
  69.       (*action)(object, s, 'M', report.xbutton.x, report.xbutton.y);
  70.     XSync(dpy, FALSE);
  71.     XGrabPointer(dpy, s, TRUE, ButtonMotionMask,
  72.              GrabModeAsync, GrabModeAsync, s, None, CurrentTime);
  73.  
  74.     x = report.xbutton.x;
  75.     y = report.xbutton.y;
  76.     old_x = x;
  77.     old_y = y;    
  78.     while (! XCheckWindowEvent(dpy, s, ButtonReleaseMask, &report)) {
  79.       while (XCheckWindowEvent(dpy, s, ButtonMotionMask, &report)) {
  80.         x = report.xmotion.x;
  81.         y = report.xmotion.y;
  82.       }
  83.       if (x != old_x || y != old_y) {
  84.         if (action != nil)
  85.           (*action)(object, s, 'M', report.xbutton.x, report.xbutton.y);
  86.         XSync(dpy, FALSE);
  87.         old_x = x;
  88.         old_y = y;
  89.       }
  90.     }
  91.     XUngrabPointer(dpy, CurrentTime);
  92.     break;
  93.       }
  94.       break;
  95.     }
  96.   }
  97.   return(NIL);
  98. }
  99.  
  100. /***********************************************************************/
  101. /**                                                                   **/
  102. /**               Constructing and Removing Scrollbars                **/
  103. /**                                                                   **/
  104. /***********************************************************************/
  105.  
  106. InstallScrollBar(w, object, left, top, width, height, ps, action)
  107.      Window w, *ps;
  108.      LVAL object;
  109.      int left, top, width, height, (*action)();
  110. {
  111.   Display *dpy = StX11Display();
  112.   int screen = StX11Screen();
  113.   Window s, thumb;
  114.  
  115.   s = XCreateSimpleWindow(dpy, w, left, top, width, height, 1,
  116.               BlackPixel(dpy, screen), WhitePixel(dpy, screen));
  117.   XSelectInput(dpy, s, ExposureMask | ButtonPressMask | ButtonReleaseMask);
  118.  
  119.   thumb = XCreateSimpleWindow(dpy, s, left, top, width, height, 0,
  120.                   WhitePixel(dpy, screen), 
  121.                   WhitePixel(dpy, screen));
  122.   XSetWindowBackgroundPixmap(dpy, thumb, ScrollThumbPM);
  123.  
  124.   if (width > height) XDefineCursor(dpy, s, DoubleArrowCursor);
  125.   else XDefineCursor(dpy, s, UpDownArrowCursor);
  126.  
  127.   if (XSaveContext(dpy, s, EventContext, (XContext) scroll_handler) != 0)
  128.     xlfail("could not install event handler");
  129.   if (XSaveContext(dpy, s, ObjectContext, (XContext) object) != 0)
  130.     xlfail("could not install object in scroll bar");
  131.   if (XSaveContext(dpy, s, ThumbContext, (XContext) thumb) != 0)
  132.     xlfail("could not install thumb in scroll bar");
  133.   if (XSaveContext(dpy, s, ScrollActionContext, (XContext) action) != 0)
  134.     xlfail("could not install action in scroll bar");
  135.  
  136.   XMapSubwindows(dpy, s);
  137.   *ps = s;
  138. }
  139.  
  140. DeleteScrollBar(s)
  141.      Window s;
  142. {
  143.   Display *dpy = StX11Display();
  144.  
  145.   if (XDeleteContext(dpy, s, EventContext) != 0)
  146.     xlfail("could not delete event context");
  147.   if (XDeleteContext(dpy, s, ObjectContext) != 0)
  148.     xlfail("could not delete object context");
  149.   if (XDeleteContext(dpy, s, ThumbContext) != 0)
  150.     xlfail("could not delete thumb context");
  151.   if (XDeleteContext(dpy, s, ScrollActionContext) != 0)
  152.     xlfail("could not delete action context");
  153. }
  154.  
  155. /***********************************************************************/
  156. /**                                                                   **/
  157. /**                   Hiding and Showing Scrollbars                   **/
  158. /**                                                                   **/
  159. /***********************************************************************/
  160.  
  161. ShowScrollBar(s, left, top, width, height)
  162.      Window s;
  163.      int left, top, width, height;
  164. {
  165.   Display *dpy = StX11Display();
  166.  
  167.   XMoveResizeWindow(dpy, s, left, top, width, height);
  168.   XMapWindow(dpy, s);
  169. }
  170.  
  171. HideScrollBar(s)
  172.      Window s;
  173. {
  174.   Display *dpy = StX11Display();
  175.  
  176.   XUnmapWindow(dpy, s);
  177. }
  178.  
  179. /***********************************************************************/
  180. /**                                                                   **/
  181. /**                       Adjusting Scrollbars                        **/
  182. /**                                                                   **/
  183. /***********************************************************************/
  184.  
  185. AdjustScrollBar(s, val, page, max)
  186.      Window s;
  187. {
  188.   Display *dpy = StX11Display();
  189.   Window root, thumb;
  190.   int left, top;
  191.   unsigned int width, height, b_width, depth;
  192.   int tleft, ttop, twidth, theight;
  193.   double val_frac, page_frac;
  194.  
  195.   if (max > 0) {
  196.     XGetGeometry(dpy, s, &root, &left, &top, &width, &height, 
  197.          &b_width, &depth);
  198.  
  199.     if (XFindContext(dpy, s, ThumbContext, &thumb) != 0)
  200.       xlfail("can't find thumb context");
  201.  
  202.     val_frac = ((double) val / (double) max);
  203.     page_frac = ((double) page / (double) max);
  204.     if (width > height) { /* horizontal scroll bar */
  205.       ttop = 0;
  206.       theight = height;
  207.       tleft = width * val_frac;
  208.       twidth = width * page_frac;
  209.     }
  210.     else { /* vertical scroll bar */
  211.       tleft = 0;
  212.       twidth = width;
  213.       ttop = height * val_frac;
  214.       theight = height * page_frac;
  215.     }
  216.     XMoveResizeWindow(dpy, thumb, tleft, ttop, twidth, theight);
  217.   }
  218. }
  219.  
  220. static is_vertical_scroll_bar(s)
  221.      Window s;
  222. {
  223.   Display *dpy = StX11Display();
  224.   Window root;
  225.   int lx, ly;
  226.   unsigned int width, height, b_width, depth;
  227.  
  228.   XGetGeometry(dpy, s, &root, &lx, &ly, &width, &height, &b_width, &depth);
  229.  
  230.   return((height > width) ? TRUE : FALSE);
  231. }
  232.