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 */
18 static Image *menutxt;
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)
35 text = display->black;
43 back = display->white;
44 high = display->black;
45 bord = display->black;
46 text = display->black;
47 htext = display->white;
51 * r is a rectangle holding the text elements.
52 * return the rectangle, including its black edge, holding element i.
55 menurect(Rectangle r, int i)
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);
65 * r is a rectangle holding the text elements.
66 * return the element number containing p.
69 menusel(Rectangle r, Point p)
73 return (p.y-r.min.y)/(font->height+Vspacing);
78 paintitem(Menu *menu, Rectangle textr, int off, int i, int highlight, Image *save, Image *restore)
86 r = menurect(textr, i);
88 draw(screen, r, restore, nil, restore->r.min);
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);
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.
107 menuscan(Menu *menu, int but, Mouse *m, Rectangle textr, int off, int lasti, Image *save)
111 paintitem(menu, textr, off, lasti, 1, save, nil);
112 flushimage(display, 1); /* in case display->locking is set */
114 while(m->buttons & (1<<(but-1))){
115 flushimage(display, 1); /* in case display->locking is set */
117 i = menusel(textr, m->xy);
118 if(i != -1 && i == lasti)
120 paintitem(menu, textr, off, lasti, 0, nil, save);
124 paintitem(menu, textr, off, lasti, 1, save, nil);
130 menupaint(Menu *menu, Rectangle textr, int off, int nitemdrawn)
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);
140 menuscrollpaint(Rectangle scrollr, int off, int nitem, int nitemdrawn)
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)
151 border(screen, r, 1, bord, ZP);
153 menutxt = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DDarkgreen);
155 draw(screen, insetrect(r, 1), menutxt, nil, ZP);
159 emenuhit(int but, Mouse *m, Menu *menu)
161 int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem;
163 Rectangle r, menur, sc, textr, scrollr;
171 replclipr(screen, 0, screen->r);
174 item = menu->item? menu->item[nitem] : (*menu->gen)(nitem);
176 i = stringwidth(font, item);
180 if(menu->lasthit<0 || menu->lasthit>=nitem)
182 screenitem = (Dy(screen->r)-10)/(font->height+Vspacing);
183 if(nitem>Maxunscroll || nitem>screenitem){
185 nitemdrawn = Nscroll;
186 if(nitemdrawn > screenitem)
187 nitemdrawn = screenitem;
188 wid = maxwid + Gap + Scrollwid;
189 off = menu->lasthit - nitemdrawn/2;
192 if(off > nitem-nitemdrawn)
193 off = nitem-nitemdrawn;
194 lasti = menu->lasthit-off;
200 lasti = menu->lasthit;
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);
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);
220 scrollr = insetrect(menur, Border);
221 scrollr.max.x = scrollr.min.x+Scrollwid;
223 scrollr = Rect(0, 0, 0, 0);
225 b = allocimage(display, menur, screen->chan, 0, 0);
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);
234 emoveto(divpt(addpt(r.min, r.max), 2));
235 menupaint(menu, textr, off, nitemdrawn);
237 menuscrollpaint(scrollr, off, nitem, nitemdrawn);
238 while(m->buttons & (1<<(but-1))){
239 lasti = menuscan(menu, but, m, textr, off, lasti, save);
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;
248 if(noff > nitem-nitemdrawn)
249 noff = nitem-nitemdrawn;
252 menupaint(menu, textr, off, nitemdrawn);
253 menuscrollpaint(scrollr, off, nitem, nitemdrawn);
256 flushimage(display, 1); /* in case display->locking is set */
260 draw(screen, menur, b, nil, menur.min);
264 replclipr(screen, 0, sc);
266 menu->lasthit = lasti+off;
267 return menu->lasthit;