1 /*  Copyright 1992 John Bovey, University of Kent at Canterbury.
2  *
3  *  Redistribution and use in source code and/or executable forms, with
4  *  or without modification, are permitted provided that the following
5  *  condition is met:
6  *
7  *  Any redistribution must retain the above copyright notice, this
8  *  condition and the following disclaimer, either as part of the
9  *  program source code included in the redistribution or in human-
10  *  readable materials provided with the redistribution.
11  *
12  *  THIS SOFTWARE IS PROVIDED "AS IS".  Any express or implied
13  *  warranties concerning this software are disclaimed by the copyright
14  *  holder to the fullest extent permitted by applicable law.  In no
15  *  event shall the copyright-holder be liable for any damages of any
16  *  kind, however caused and on any theory of liability, arising in any
17  *  way out of the use of, or inability to use, this software.
18  *
19  *  -------------------------------------------------------------------
20  *
21  *  In other words, do not misrepresent my work as your own work, and
22  *  do not sue me if it causes problems.  Feel free to do anything else
23  *  you wish with it.
24  */
25 
26 char xvt_sbar_c_sccsid[] = "@(#)sbar.c	1.2 16/11/93 (UKC)";
27 
28 #include <stdio.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xresource.h>
32 #include "rvt.h"
33 #include "xsetup.h"
34 #include "command.h"
35 #include "ttyinit.h"
36 #include "sbar.h"
37 
38 /*  External global variables that are initialised at startup.
39  */
40 extern Display		*display;
41 extern Window		sb_win;		/* scroll bar window */
42 extern GC 		sbgc;
43 
44 static unsigned int width;	/* scrollbar width */
45 static unsigned int height;	/* scrollbar height */
46 static int mtop;	/* top of marked area */
47 static int mbot;	/* bottom of marked area */
48 
49 /*  Most recent arguments to sbar_show
50  */
51 static int last_length = 100;	/* initial values to give a full bar */
52 static int last_low = 0;
53 static int last_high = 100;
54 
55 static unsigned char stipple_bits[] = {
56 	0x55, 0x55,
57 	0xaa, 0xaa,
58 	0x55, 0x55,
59 	0xaa, 0xaa,
60 	0x55, 0x55,
61 	0xaa, 0xaa,
62 	0x55, 0x55,
63 	0xaa, 0xaa,
64 	0x55, 0x55,
65 	0xaa, 0xaa,
66 	0x55, 0x55,
67 	0xaa, 0xaa,
68 	0x55, 0x55,
69 	0xaa, 0xaa,
70 	0x55, 0x55,
71 	0xaa, 0xaa,
72 };
73 
74 /*  Initialise scrollbar data structures - called just once.
75  */
76 void
sbar_init()77 sbar_init()
78 {
79 	Pixmap stipple;
80 	XGCValues gcv;
81 
82 	stipple = XCreateBitmapFromData(display,sb_win,stipple_bits,16,16);
83 	if (stipple == 0) {
84 		error("Cannot create scrollbar bitmap");
85 		quit(1);
86 	}
87 	gcv.fill_style = FillOpaqueStippled;
88 	gcv.stipple = stipple;
89 	XChangeGC(display,sbgc,GCFillStyle|GCStipple,&gcv);
90 	sbar_reset();
91 }
92 
93 /*  Redraw the scrollbar after a size change
94  */
95 void
sbar_reset()96 sbar_reset()
97 {
98 	Window root;
99 	int x, y;
100 	unsigned int border_width, depth;
101 
102 	XGetGeometry(display,sb_win,&root,&x,&y,&width,&height,&border_width,&depth);
103 	mbot = -1;	/* force a redraw */
104 	sbar_show(last_length,last_low,last_high);
105 }
106 
107 /*  Redraw the scrollbar to show the area from low to high proportional to length.
108  */
109 void
sbar_show(length,low,high)110 sbar_show(length,low,high)
111 int length, low, high;
112 {
113 	int top, bot;
114 
115 	if (length == 0)
116 		return;
117 
118 	last_length = length;
119 	last_low = low;
120 	last_high = high;
121 
122 	top = height - 1 - height * high / length;
123 	bot = height - 1 - height * low / length;
124 
125 	if (top == mtop && bot == mbot)
126 		return;
127 	if (top > 0)
128 		XClearArea(display,sb_win,0,0,width,top - 1,False);
129 	if (bot >= top)
130 		XFillRectangle(display,sb_win,sbgc,0,top,width,bot - top + 1);
131 	if (bot < height - 1)
132 		XClearArea(display,sb_win,0,bot + 1,width,height - bot - 1,False);
133 }
134