File: X11scroll.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (238 lines) | stat: -rw-r--r-- 7,970 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* X11buttons - buttons for X11 dialogs and windows                    */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */
 
/***********************************************************************/
/**                                                                   **/
/**                    General Includes and Definitions               **/
/**                                                                   **/
/***********************************************************************/

#include "xlisp.h"
#include "xlstat.h"
#include "xlgraph.h"

extern Display *StX11Display();
extern LVAL StX11ItemObject();

extern XContext EventContext, ObjectContext, ThumbContext, ScrollActionContext;
extern Cursor DoubleArrowCursor, RightArrowCursor, LeftArrowCursor;
extern Cursor UpDownArrowCursor, UpArrowCursor, DownArrowCursor;
extern Pixmap ScrollThumbPM;

/* forward declarations */
LOCAL int is_vertical_scroll_bar _((Window s));

/***********************************************************************/
/**                                                                   **/
/**                           Event Handler                           **/
/**                                                                   **/
/***********************************************************************/

static LVAL scroll_handler(report, modal)
     XEvent report;
     int modal;
{
  Display *dpy = StX11Display();
  Window s;
  LVAL object;
  int up, is_vertical, x, y, old_x, old_y, (*action)();

  s = report.xany.window;
  if (XFindContext(dpy, s, ObjectContext, (caddr_t *) &object) == 0 &&
      objectp(object)) {
    is_vertical = is_vertical_scroll_bar(s);
    if (XFindContext(dpy, s, ScrollActionContext, (caddr_t *) &action) != 0)
      xlfail("can't find thumb context");
    switch (report.type) {
    case ButtonPress:
      switch (report.xbutton.button) {
      case Button1:
      case Button3:
	up = (report.xbutton.button == Button1) ? TRUE : FALSE;
	if (is_vertical)
	  XDefineCursor(dpy, s, (up) ? UpArrowCursor : DownArrowCursor);
	else 
	  XDefineCursor(dpy, s, (up) ? LeftArrowCursor : RightArrowCursor);
	if (action != NULL)
	  (*action)(object, s, (up) ? 'L' : 'R',
		    report.xbutton.x, report.xbutton.y);
	while (! XCheckTypedEvent(dpy, ButtonRelease, &report));
	if (is_vertical) XDefineCursor(dpy, s, UpDownArrowCursor);
	else XDefineCursor(dpy, s, DoubleArrowCursor);
	break;
      case Button2:
	if (action != NULL)
	  (*action)(object, s, 'M', report.xbutton.x, report.xbutton.y);
	XSync(dpy, FALSE);
#ifdef DODO
	XGrabPointer(dpy, s, TRUE, ButtonMotionMask,
		     GrabModeAsync, GrabModeAsync, s, None, CurrentTime);
#endif /* DODO */

	x = report.xbutton.x;
	y = report.xbutton.y;
	old_x = x;
	old_y = y;	
	while (! XCheckWindowEvent(dpy, s, ButtonReleaseMask, &report)) {
	  while (XCheckWindowEvent(dpy, s, ButtonMotionMask, &report)) {
	    x = report.xmotion.x;
	    y = report.xmotion.y;
	  }
	  if (x != old_x || y != old_y) {
	    if (action != NULL)
	      (*action)(object, s, 'M', report.xbutton.x, report.xbutton.y);
	    XSync(dpy, FALSE);
	    old_x = x;
	    old_y = y;
	  }
	}
#ifdef DODO
	XUngrabPointer(dpy, CurrentTime);
#endif /* DODO */
	break;
      }
      break;
    }
  }
  return(NIL);
}

/***********************************************************************/
/**                                                                   **/
/**               Constructing and Removing Scrollbars                **/
/**                                                                   **/
/***********************************************************************/

VOID InstallScrollBar(w, object, left, top, width, height, ps, action)
     Window w, *ps;
     LVAL object;
     int left, top, width, height;
     VOID (*action) ();
{
  Display *dpy = StX11Display();
  int screen = StX11Screen();
  Window s, thumb;

  s = XCreateSimpleWindow(dpy, w, left, top, width, height, 1,
			  BlackPixel(dpy, screen), WhitePixel(dpy, screen));
  XSelectInput(dpy, s, 
	       ExposureMask | ButtonMotionMask |
	       ButtonPressMask | ButtonReleaseMask);

  thumb = XCreateSimpleWindow(dpy, s, left, top, width, height, 0,
			      WhitePixel(dpy, screen), 
			      WhitePixel(dpy, screen));
  XSetWindowBackgroundPixmap(dpy, thumb, ScrollThumbPM);

  if (width > height) XDefineCursor(dpy, s, DoubleArrowCursor);
  else XDefineCursor(dpy, s, UpDownArrowCursor);

  if (XSaveContext(dpy, s, EventContext, (caddr_t) scroll_handler) != 0)
    xlfail("could not install event handler");
  if (XSaveContext(dpy, s, ObjectContext, (caddr_t) object) != 0)
    xlfail("could not install object in scroll bar");
  if (XSaveContext(dpy, s, ThumbContext, (caddr_t) thumb) != 0)
    xlfail("could not install thumb in scroll bar");
  if (XSaveContext(dpy, s, ScrollActionContext, (caddr_t) action) != 0)
    xlfail("could not install action in scroll bar");

  XMapSubwindows(dpy, s);
  *ps = s;
}

VOID DeleteScrollBar(s)
     Window s;
{
  Display *dpy = StX11Display();

  if (XDeleteContext(dpy, s, EventContext) != 0)
    xlfail("could not delete event context");
  if (XDeleteContext(dpy, s, ObjectContext) != 0)
    xlfail("could not delete object context");
  if (XDeleteContext(dpy, s, ThumbContext) != 0)
    xlfail("could not delete thumb context");
  if (XDeleteContext(dpy, s, ScrollActionContext) != 0)
    xlfail("could not delete action context");
}

/***********************************************************************/
/**                                                                   **/
/**                   Hiding and Showing Scrollbars                   **/
/**                                                                   **/
/***********************************************************************/

VOID ShowScrollBar(s, left, top, width, height)
     Window s;
     int left, top, width, height;
{
  Display *dpy = StX11Display();

  XMoveResizeWindow(dpy, s, left, top, width, height);
  XMapWindow(dpy, s);
}

VOID HideScrollBar(s)
     Window s;
{
  Display *dpy = StX11Display();

  XUnmapWindow(dpy, s);
}

/***********************************************************************/
/**                                                                   **/
/**                       Adjusting Scrollbars                        **/
/**                                                                   **/
/***********************************************************************/

VOID AdjustScrollBar(s, val, page, max)
     Window s;
     int val, page, max;
{
  Display *dpy = StX11Display();
  Window root, thumb;
  int left, top;
  unsigned int width, height, b_width, depth;
  int tleft, ttop, twidth, theight;
  double val_frac, page_frac;

  if (max > 0) {
    XGetGeometry(dpy, s, &root, &left, &top, &width, &height, 
		 &b_width, &depth);

    if (XFindContext(dpy, s, ThumbContext, (caddr_t *) &thumb) != 0)
      xlfail("can't find thumb context");

    val_frac = ((double) val / (double) max);
    page_frac = ((double) page / (double) max);
    if (width > height) { /* horizontal scroll bar */
      ttop = 0;
      theight = height;
      tleft = width * val_frac;
      twidth = width * page_frac;
    }
    else { /* vertical scroll bar */
      tleft = 0;
      twidth = width;
      ttop = height * val_frac;
      theight = height * page_frac;
    }
    XMoveResizeWindow(dpy, thumb, tleft, ttop, twidth, theight);
  }
}

LOCAL int is_vertical_scroll_bar(s)
     Window s;
{
  Display *dpy = StX11Display();
  Window root;
  int lx, ly;
  unsigned int width, height, b_width, depth;

  XGetGeometry(dpy, s, &root, &lx, &ly, &width, &height, &b_width, &depth);

  return((height > width) ? TRUE : FALSE);
}