]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/rio/scrl.c
rio: make sure flush replies are send only *after* the request got flushed or was...
[plan9front.git] / sys / src / cmd / rio / scrl.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include "dat.h"
11 #include "fns.h"
12
13 static Image *scrtmp;
14
15 static
16 Image*
17 scrtemps(void)
18 {
19         int h;
20
21         if(scrtmp == nil){
22                 h = BIG*Dy(screen->r);
23                 scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, DNofill);
24         }
25         return scrtmp;
26 }
27
28 void
29 freescrtemps(void)
30 {
31         if(scrtmp){
32                 freeimage(scrtmp);
33                 scrtmp = nil;
34         }
35 }
36
37 static
38 Rectangle
39 scrpos(Rectangle r, uint p0, uint p1, uint tot)
40 {
41         Rectangle q;
42         int h;
43
44         q = r;
45         h = q.max.y-q.min.y;
46         if(tot == 0)
47                 return q;
48         if(tot > 1024*1024){
49                 tot>>=10;
50                 p0>>=10;
51                 p1>>=10;
52         }
53         if(p0 > 0)
54                 q.min.y += h*p0/tot;
55         if(p1 < tot)
56                 q.max.y -= h*(tot-p1)/tot;
57         if(q.max.y < q.min.y+2){
58                 if(q.min.y+2 <= r.max.y)
59                         q.max.y = q.min.y+2;
60                 else
61                         q.min.y = q.max.y-2;
62         }
63         return q;
64 }
65
66 void
67 wscrdraw(Window *w)
68 {
69         Rectangle r, r1, r2;
70         Image *b;
71
72         b = scrtemps();
73         if(b == nil || w->i == nil)
74                 return;
75         r = w->scrollr;
76         r1 = r;
77         r1.min.x = 0;
78         r1.max.x = Dx(r);
79         r2 = scrpos(r1, w->org, w->org+w->nchars, w->nr);
80         if(!eqrect(r2, w->lastsr)){
81                 w->lastsr = r2;
82                 /* move r1, r2 to (0,0) to avoid clipping */
83                 r2 = rectsubpt(r2, r1.min);
84                 r1 = rectsubpt(r1, r1.min);
85                 draw(b, r1, w->cols[BORD], nil, ZP);
86                 draw(b, r2, w->cols[BACK], nil, ZP);
87                 r2.min.x = r2.max.x-1;
88                 draw(b, r2, w->cols[BORD], nil, ZP);
89                 draw(w->i, r, b, nil, Pt(0, r1.min.y));
90         }
91 }
92
93 void
94 wscrsleep(Window *w, uint dt)
95 {
96         Timer   *timer;
97         int y, b;
98         static Alt alts[3];
99
100         timer = timerstart(dt);
101         y = w->mc.xy.y;
102         b = w->mc.buttons;
103         alts[0].c = timer->c;
104         alts[0].v = nil;
105         alts[0].op = CHANRCV;
106         alts[1].c = w->mc.c;
107         alts[1].v = &w->mc.Mouse;
108         alts[1].op = CHANRCV;
109         alts[2].op = CHANEND;
110         for(;;)
111                 switch(alt(alts)){
112                 case 0:
113                         timerstop(timer);
114                         return;
115                 case 1:
116                         if(abs(w->mc.xy.y-y)>2 || w->mc.buttons!=b){
117                                 timercancel(timer);
118                                 return;
119                         }
120                         break;
121                 }
122 }
123
124 void
125 wscroll(Window *w, int but)
126 {
127         uint p0, oldp0;
128         Rectangle s;
129         int x, y, my, h, first;
130
131         s = insetrect(w->scrollr, 1);
132         h = s.max.y-s.min.y;
133         x = (s.min.x+s.max.x)/2;
134         oldp0 = ~0;
135         first = TRUE;
136         do{
137                 flushimage(display, 1);
138                 my = w->mc.xy.y;
139                 if(my < s.min.y)
140                         my = s.min.y;
141                 if(my >= s.max.y)
142                         my = s.max.y;
143                 if(but == 2){
144                         y = my;
145                         if(y > s.max.y-2)
146                                 y = s.max.y-2;
147                         if(w->nr > 1024*1024)
148                                 p0 = ((w->nr>>10)*(y-s.min.y)/h)<<10;
149                         else
150                                 p0 = w->nr*(y-s.min.y)/h;
151                         if(oldp0 != p0)
152                                 wsetorigin(w, p0, FALSE);
153                         oldp0 = p0;
154                         readmouse(&w->mc);
155                         continue;
156                 }
157                 if(but == 1)
158                         p0 = wbacknl(w, w->org, (my-s.min.y)/w->font->height);
159                 else
160                         p0 = w->org+frcharofpt(w, Pt(s.max.x, my));
161                 if(oldp0 != p0)
162                         wsetorigin(w, p0, TRUE);
163                 oldp0 = p0;
164                 /* debounce */
165                 if(first){
166                         flushimage(display, 1);
167                         sleep(200);
168                         nbrecv(w->mc.c, &w->mc.Mouse);
169                         first = FALSE;
170                 }
171                 wscrsleep(w, 100);
172         }while(w->mc.buttons & (1<<(but-1)));
173         while(w->mc.buttons)
174                 readmouse(&w->mc);
175 }