]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/samterm/scroll.c
merge
[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         if(l->visible==None)
115                 return;
116
117         s = l->scroll;
118         x = s.min.x+FLSCROLLWID/2;
119         scr = scrpos(l->scroll, l->origin, l->origin+l->f.nchars, tot);
120         r = scr;
121         y = scr.min.y;
122         my = mousep->xy.y;
123         draw(scrback, Rect(0,0,Dx(l->scroll), Dy(l->scroll)), l->f.b, nil, l->scroll.min);
124         do{
125                 oin = in;
126                 in = (but > 3) || (but == 2) || abs(x-mousep->xy.x)<=FLSCROLLWID/2;
127                 if(oin && !in)
128                         scrunmark(l, r);
129                 if(in){
130                         scrmark(l, r);
131                         oy = y;
132                         my = mousep->xy.y;
133                         if(my < s.min.y)
134                                 my = s.min.y;
135                         if(my >= s.max.y)
136                                 my = s.max.y;
137                         if(but == 1 || but == 4){
138                                 p0 = l->origin-frcharofpt(&l->f, Pt(s.max.x, my));
139                                 rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
140                                 y = rt.min.y;
141                         }else if(but == 2){
142                                 y = my;
143                                 if(y > s.max.y-2)
144                                         y = s.max.y-2;
145                         }else if(but == 3 || but == 5){
146                                 p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
147                                 rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
148                                 y = rt.min.y;
149                         }
150                         if(y != oy){
151                                 scrunmark(l, r);
152                                 r = rectaddpt(scr, Pt(0, y-scr.min.y));
153                                 scrmark(l, r);
154                         }
155                 }
156         }while(but <= 3 && button(but));
157         if(in){
158                 h = s.max.y-s.min.y;
159                 scrunmark(l, r);
160                 p0 = 0;
161                 if(but == 1 || but == 4){
162                         but = 1;
163                         p0 = (long)(my-s.min.y)/l->f.font->height+1;
164                 }else if(but == 2){
165                         if(tot > 1024L*1024L)
166                                 p0 = ((tot>>10)*(y-s.min.y)/h)<<10;
167                         else
168                                 p0 = tot*(y-s.min.y)/h;
169                 }else if(but == 3 || but == 5){
170                         but = 3;
171                         p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
172                         if(p0 > tot)
173                                 p0 = tot;
174                 }
175                 scrorigin(l, but, p0);
176         }
177 }