]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/rio/scrl.c
rio: fix handling "resize" wctl for hidden windows
[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 y, my, h, first;
130
131         s = insetrect(w->scrollr, 1);
132         h = s.max.y-s.min.y;
133         oldp0 = ~0;
134         first = TRUE;
135         do{
136                 flushimage(display, 1);
137                 my = w->mc.xy.y;
138                 if(my < s.min.y)
139                         my = s.min.y;
140                 if(my >= s.max.y)
141                         my = s.max.y;
142                 if(but == 2){
143                         y = my;
144                         if(y > s.max.y-2)
145                                 y = s.max.y-2;
146                         if(w->nr > 1024*1024)
147                                 p0 = ((w->nr>>10)*(y-s.min.y)/h)<<10;
148                         else
149                                 p0 = w->nr*(y-s.min.y)/h;
150                         if(oldp0 != p0)
151                                 wsetorigin(w, p0, FALSE);
152                         oldp0 = p0;
153                         readmouse(&w->mc);
154                         continue;
155                 }
156                 if(but == 1 || but == 4){
157                         y = max(1, (my-s.min.y)/w->font->height);
158                         p0 = wbacknl(w, w->org, y);
159                 }else{
160                         y = max(my, s.min.y+w->font->height);
161                         p0 = w->org+frcharofpt(w, Pt(s.max.x, y));
162                 }
163                 if(oldp0 != p0)
164                         wsetorigin(w, p0, TRUE);
165                 oldp0 = p0;
166                 /* debounce */
167                 if(first){
168                         flushimage(display, 1);
169                         if(but > 3)
170                                 return;
171                         sleep(200);
172                         nbrecv(w->mc.c, &w->mc.Mouse);
173                         first = FALSE;
174                 }
175                 wscrsleep(w, 100);
176         }while(w->mc.buttons & (1<<(but-1)));
177         while(w->mc.buttons)
178                 readmouse(&w->mc);
179 }