]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/emenuhit.c
ircrc: freenode -> oftc
[plan9front.git] / sys / src / libdraw / emenuhit.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5
6 enum
7 {
8         Margin = 4,             /* outside to text */
9         Border = 2,             /* outside to selection boxes */
10         Blackborder = 2,        /* width of outlining border */
11         Vspacing = 2,           /* extra spacing between lines of text */
12         Maxunscroll = 25,       /* maximum #entries before scrolling turns on */
13         Nscroll = 20,           /* number entries in scrolling part */
14         Scrollwid = 14,         /* width of scroll bar */
15         Gap = 4,                        /* between text and scroll bar */
16 };
17
18 static  Image   *menutxt;
19 static  Image   *back;
20 static  Image   *high;
21 static  Image   *bord;
22 static  Image   *text;
23 static  Image   *htext;
24
25 static
26 void
27 menucolors(void)
28 {
29         /* Main tone is greenish, with negative selection */
30         back = allocimagemix(display, DPalegreen, DWhite);
31         high = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkgreen);        /* dark green */
32         bord = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DMedgreen); /* not as dark green */
33         if(back==nil || high==nil || bord==nil)
34                 goto Error;
35         text = display->black;
36         htext = back;
37         return;
38
39     Error:
40         freeimage(back);
41         freeimage(high);
42         freeimage(bord);
43         back = display->white;
44         high = display->black;
45         bord = display->black;
46         text = display->black;
47         htext = display->white;
48 }
49
50 /*
51  * r is a rectangle holding the text elements.
52  * return the rectangle, including its black edge, holding element i.
53  */
54 static Rectangle
55 menurect(Rectangle r, int i)
56 {
57         if(i < 0)
58                 return Rect(0, 0, 0, 0);
59         r.min.y += (font->height+Vspacing)*i;
60         r.max.y = r.min.y+font->height+Vspacing;
61         return insetrect(r, Border-Margin);
62 }
63
64 /*
65  * r is a rectangle holding the text elements.
66  * return the element number containing p.
67  */
68 static int
69 menusel(Rectangle r, Point p)
70 {
71         if(!ptinrect(p, r))
72                 return -1;
73         return (p.y-r.min.y)/(font->height+Vspacing);
74 }
75
76 static
77 void
78 paintitem(Menu *menu, Rectangle textr, int off, int i, int highlight, Image *save, Image *restore)
79 {
80         char *item;
81         Rectangle r;
82         Point pt;
83
84         if(i < 0)
85                 return;
86         r = menurect(textr, i);
87         if(restore){
88                 draw(screen, r, restore, nil, restore->r.min);
89                 return;
90         }
91         if(save)
92                 draw(save, save->r, screen, nil, r.min);
93         item = menu->item? menu->item[i+off] : (*menu->gen)(i+off);
94         pt.x = (textr.min.x+textr.max.x-stringwidth(font, item))/2;
95         pt.y = textr.min.y+i*(font->height+Vspacing);
96         draw(screen, r, highlight? high : back, nil, pt);
97         string(screen, pt, highlight? htext : text, pt, font, item);
98 }
99
100 /*
101  * menur is a rectangle holding all the highlightable text elements.
102  * track mouse while inside the box, return what's selected when button
103  * is raised, -1 as soon as it leaves box.
104  * invariant: nothing is highlighted on entry or exit.
105  */
106 static int
107 menuscan(Menu *menu, int but, Mouse *m, Rectangle textr, int off, int lasti, Image *save)
108 {
109         int i;
110
111         paintitem(menu, textr, off, lasti, 1, save, nil);
112         flushimage(display, 1); /* in case display->locking is set */
113         *m = emouse();
114         while(m->buttons & (1<<(but-1))){
115                 flushimage(display, 1); /* in case display->locking is set */
116                 *m = emouse();
117                 i = menusel(textr, m->xy);
118                 if(i != -1 && i == lasti)
119                         continue;
120                 paintitem(menu, textr, off, lasti, 0, nil, save);
121                 if(i == -1)
122                         return i;
123                 lasti = i;
124                 paintitem(menu, textr, off, lasti, 1, save, nil);
125         }
126         return lasti;
127 }
128
129 static void
130 menupaint(Menu *menu, Rectangle textr, int off, int nitemdrawn)
131 {
132         int i;
133
134         draw(screen, insetrect(textr, Border-Margin), back, nil, ZP);
135         for(i = 0; i<nitemdrawn; i++)
136                 paintitem(menu, textr, off, i, 0, nil, nil);
137 }
138
139 static void
140 menuscrollpaint(Rectangle scrollr, int off, int nitem, int nitemdrawn)
141 {
142         Rectangle r;
143
144         draw(screen, scrollr, back, nil, ZP);
145         r.min.x = scrollr.min.x;
146         r.max.x = scrollr.max.x;
147         r.min.y = scrollr.min.y + (Dy(scrollr)*off)/nitem;
148         r.max.y = scrollr.min.y + (Dy(scrollr)*(off+nitemdrawn))/nitem;
149         if(r.max.y < r.min.y+2)
150                 r.max.y = r.min.y+2;
151         border(screen, r, 1, bord, ZP);
152         if(menutxt == 0)
153                 menutxt = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DDarkgreen);
154         if(menutxt)
155                 draw(screen, insetrect(r, 1), menutxt, nil, ZP);
156 }
157
158 int
159 emenuhit(int but, Mouse *m, Menu *menu)
160 {
161         int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem;
162         int scrolling;
163         Rectangle r, menur, sc, textr, scrollr;
164         Image *b, *save;
165         Point pt;
166         char *item;
167
168         if(back == nil)
169                 menucolors();
170         sc = screen->clipr;
171         replclipr(screen, 0, screen->r);
172         maxwid = 0;
173         for(nitem = 0;
174             item = menu->item? menu->item[nitem] : (*menu->gen)(nitem);
175             nitem++){
176                 i = stringwidth(font, item);
177                 if(i > maxwid)
178                         maxwid = i;
179         }
180         if(menu->lasthit<0 || menu->lasthit>=nitem)
181                 menu->lasthit = 0;
182         screenitem = (Dy(screen->r)-10)/(font->height+Vspacing);
183         if(nitem>Maxunscroll || nitem>screenitem){
184                 scrolling = 1;
185                 nitemdrawn = Nscroll;
186                 if(nitemdrawn > screenitem)
187                         nitemdrawn = screenitem;
188                 wid = maxwid + Gap + Scrollwid;
189                 off = menu->lasthit - nitemdrawn/2;
190                 if(off < 0)
191                         off = 0;
192                 if(off > nitem-nitemdrawn)
193                         off = nitem-nitemdrawn;
194                 lasti = menu->lasthit-off;
195         }else{
196                 scrolling = 0;
197                 nitemdrawn = nitem;
198                 wid = maxwid;
199                 off = 0;
200                 lasti = menu->lasthit;
201         }
202         r = insetrect(Rect(0, 0, wid, nitemdrawn*(font->height+Vspacing)), -Margin);
203         r = rectsubpt(r, Pt(wid/2, lasti*(font->height+Vspacing)+font->height/2));
204         r = rectaddpt(r, m->xy);
205         pt = ZP;
206         if(r.max.x>screen->r.max.x)
207                 pt.x = screen->r.max.x-r.max.x;
208         if(r.max.y>screen->r.max.y)
209                 pt.y = screen->r.max.y-r.max.y;
210         if(r.min.x<screen->r.min.x)
211                 pt.x = screen->r.min.x-r.min.x;
212         if(r.min.y<screen->r.min.y)
213                 pt.y = screen->r.min.y-r.min.y;
214         menur = rectaddpt(r, pt);
215         textr.max.x = menur.max.x-Margin;
216         textr.min.x = textr.max.x-maxwid;
217         textr.min.y = menur.min.y+Margin;
218         textr.max.y = textr.min.y + nitemdrawn*(font->height+Vspacing);
219         if(scrolling){
220                 scrollr = insetrect(menur, Border);
221                 scrollr.max.x = scrollr.min.x+Scrollwid;
222         }else
223                 scrollr = Rect(0, 0, 0, 0);
224
225         b = allocimage(display, menur, screen->chan, 0, 0);
226         if(b == 0)
227                 b = screen;
228         draw(b, menur, screen, nil, menur.min);
229         draw(screen, menur, back, nil, ZP);
230         border(screen, menur, Blackborder, bord, ZP);
231         save = allocimage(display, menurect(textr, 0), screen->chan, 0, -1);
232         r = menurect(textr, lasti);
233         if(pt.x || pt.y)
234                 emoveto(divpt(addpt(r.min, r.max), 2));
235         menupaint(menu, textr, off, nitemdrawn);
236         if(scrolling)
237                 menuscrollpaint(scrollr, off, nitem, nitemdrawn);
238         while(m->buttons & (1<<(but-1))){
239                 lasti = menuscan(menu, but, m, textr, off, lasti, save);
240                 if(lasti >= 0)
241                         break;
242                 while(!ptinrect(m->xy, textr) && (m->buttons & (1<<(but-1)))){
243                         if(scrolling && ptinrect(m->xy, scrollr)){
244                                 noff = ((m->xy.y-scrollr.min.y)*nitem)/Dy(scrollr);
245                                 noff -= nitemdrawn/2;
246                                 if(noff < 0)
247                                         noff = 0;
248                                 if(noff > nitem-nitemdrawn)
249                                         noff = nitem-nitemdrawn;
250                                 if(noff != off){
251                                         off = noff;
252                                         menupaint(menu, textr, off, nitemdrawn);
253                                         menuscrollpaint(scrollr, off, nitem, nitemdrawn);
254                                 }
255                         }
256                         flushimage(display, 1); /* in case display->locking is set */
257                         *m = emouse();
258                 }
259         }
260         draw(screen, menur, b, nil, menur.min);
261         if(b != screen)
262                 freeimage(b);
263         freeimage(save);
264         replclipr(screen, 0, sc);
265         if(lasti >= 0){
266                 menu->lasthit = lasti+off;
267                 return menu->lasthit;
268         }
269         return -1;
270 }