]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/samterm/scroll.c
awk: make empty FS unicodely-correct.
[plan9front.git] / sys / src / cmd / samterm / scroll.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <mouse.h>
6 #include <keyboard.h>
7 #include <frame.h>
8 #include "flayer.h"
9 #include "samterm.h"
10
11 static Image *scrtmp;
12 static Image *scrback;
13
14 void
15 scrtemps(void)
16 {
17         int h;
18
19         if(scrtmp)
20                 return;
21         if(screensize(0, &h) == 0)
22                 h = 2048;
23         scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, 0);
24         scrback = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, 0);
25         if(scrtmp==0 || scrback==0)
26                 panic("scrtemps");
27 }
28
29 Rectangle
30 scrpos(Rectangle r, long p0, long p1, long tot)
31 {
32         Rectangle q;
33         int h;
34
35         q = r;
36         h = q.max.y-q.min.y;
37         if(tot == 0)
38                 return q;
39         if(tot > 1024L*1024L)
40                 tot>>=10, p0>>=10, p1>>=10;
41         if(p0 > 0)
42                 q.min.y += h*p0/tot;
43         if(p1 < tot)
44                 q.max.y -= h*(tot-p1)/tot;
45         if(q.max.y < q.min.y+2){
46                 if(q.min.y+2 <= r.max.y)
47                         q.max.y = q.min.y+2;
48                 else
49                         q.min.y = q.max.y-2;
50         }
51         return q;
52 }
53
54 void
55 scrmark(Flayer *l, Rectangle r)
56 {
57         r.max.x--;
58         if(rectclip(&r, l->scroll)) {
59                 if (l->f.b == nil)
60                         panic("scrmark: nil l->f.b");
61                 draw(l->f.b, r, l->f.cols[HIGH], nil, ZP);
62         }
63 }
64
65 void
66 scrunmark(Flayer *l, Rectangle r)
67 {
68         if(rectclip(&r, l->scroll)) {
69                 if (l->f.b == nil)
70                         panic("scrunmark: nil l->f.b");
71                 draw(l->f.b, r, scrback, nil, Pt(0, r.min.y-l->scroll.min.y));
72         }
73 }
74
75 void
76 scrdraw(Flayer *l, long tot)
77 {
78         Rectangle r, r1, r2;
79         Image *b;
80
81         scrtemps();
82         if(l->f.b == 0)
83                 panic("scrdraw");
84         r = l->scroll;
85         r1 = r;
86         if(l->visible == All){
87                 b = scrtmp;
88                 r1.min.x = 0;
89                 r1.max.x = Dx(r);
90         }else
91                 b = l->f.b;
92         r2 = scrpos(r1, l->origin, l->origin+l->f.nchars, tot);
93         if(!eqrect(r2, l->lastsr)){
94                 l->lastsr = r2;
95                 draw(b, r1, l->f.cols[BORD], nil, ZP);
96                 draw(b, r2, l->f.cols[BACK], nil, r2.min);
97                 r2 = r1;
98                 r2.min.x = r2.max.x-1;
99                 draw(b, r2, l->f.cols[BORD], nil, ZP);
100                 if(b!=l->f.b)
101                         draw(l->f.b, r, b, nil, r1.min);
102         }
103 }
104
105 void
106 scroll(Flayer *l, int but)
107 {
108         int in = 0, oin;
109         long tot = scrtotal(l);
110         Rectangle scr, r, s, rt;
111         int x, y, my, oy, h;
112         long p0;
113
114         s = l->scroll;
115         x = s.min.x+FLSCROLLWID/2;
116         scr = scrpos(l->scroll, l->origin, l->origin+l->f.nchars, tot);
117         r = scr;
118         y = scr.min.y;
119         my = mousep->xy.y;
120         draw(scrback, Rect(0,0,Dx(l->scroll), Dy(l->scroll)), l->f.b, nil, l->scroll.min);
121         do{
122                 oin = in;
123                 in = (but > 3) || (but == 2) || abs(x-mousep->xy.x)<=FLSCROLLWID/2;
124                 if(oin && !in)
125                         scrunmark(l, r);
126                 if(in){
127                         scrmark(l, r);
128                         oy = y;
129                         my = mousep->xy.y;
130                         if(my < s.min.y)
131                                 my = s.min.y;
132                         if(my >= s.max.y)
133                                 my = s.max.y;
134                         if(but == 1 || but == 4){
135                                 p0 = l->origin-frcharofpt(&l->f, Pt(s.max.x, my));
136                                 rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
137                                 y = rt.min.y;
138                         }else if(but == 2){
139                                 y = my;
140                                 if(y > s.max.y-2)
141                                         y = s.max.y-2;
142                         }else if(but == 3 || but == 5){
143                                 p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
144                                 rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
145                                 y = rt.min.y;
146                         }
147                         if(y != oy){
148                                 scrunmark(l, r);
149                                 r = rectaddpt(scr, Pt(0, y-scr.min.y));
150                                 scrmark(l, r);
151                         }
152                 }
153         }while(but <= 3 && button(but));
154         if(in){
155                 h = s.max.y-s.min.y;
156                 scrunmark(l, r);
157                 p0 = 0;
158                 if(but == 1 || but == 4){
159                         but = 1;
160                         p0 = (long)(my-s.min.y)/l->f.font->height+1;
161                 }else if(but == 2){
162                         if(tot > 1024L*1024L)
163                                 p0 = ((tot>>10)*(y-s.min.y)/h)<<10;
164                         else
165                                 p0 = tot*(y-s.min.y)/h;
166                 }else if(but == 3 || but == 5){
167                         but = 3;
168                         p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
169                         if(p0 > tot)
170                                 p0 = tot;
171                 }
172                 scrorigin(l, but, p0);
173         }
174 }