9 Margin = 4, /* outside to text */
10 Border = 2, /* outside to selection boxes */
11 Blackborder = 2, /* width of outlining border */
12 Vspacing = 2, /* extra spacing between lines of text */
13 Maxunscroll = 25, /* maximum #entries before scrolling turns on */
14 Nscroll = 20, /* number entries in scrolling part */
15 Scrollwid = 14, /* width of scroll bar */
16 Gap = 4, /* between text and scroll bar */
19 static Image *menutxt;
30 /* Main tone is greenish, with negative selection */
31 back = allocimagemix(display, DPalegreen, DWhite);
32 high = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkgreen); /* dark green */
33 bord = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedgreen); /* not as dark green */
34 if(back==nil || high==nil || bord==nil)
36 text = display->black;
44 back = display->white;
45 high = display->black;
46 bord = display->black;
47 text = display->black;
48 htext = display->white;
52 * r is a rectangle holding the text elements.
53 * return the rectangle, including its black edge, holding element i.
56 menurect(Rectangle r, int i)
59 return Rect(0, 0, 0, 0);
60 r.min.y += (font->height+Vspacing)*i;
61 r.max.y = r.min.y+font->height+Vspacing;
62 return insetrect(r, Border-Margin);
66 * r is a rectangle holding the text elements.
67 * return the element number containing p.
70 menusel(Rectangle r, Point p)
72 r = insetrect(r, Margin);
75 return (p.y-r.min.y)/(font->height+Vspacing);
80 paintitem(Image *m, Menu *menu, Rectangle textr, int off, int i, int highlight, Image *save, Image *restore)
88 r = menurect(textr, i);
90 draw(m, r, restore, nil, restore->r.min);
94 draw(save, save->r, m, nil, r.min);
95 item = menu->item? menu->item[i+off] : (*menu->gen)(i+off);
96 pt.x = (textr.min.x+textr.max.x-stringwidth(font, item))/2;
97 pt.y = textr.min.y+i*(font->height+Vspacing);
98 draw(m, r, highlight? high : back, nil, pt);
99 string(m, pt, highlight? htext : text, pt, font, item);
103 * menur is a rectangle holding all the highlightable text elements.
104 * track mouse while inside the box, return what's selected when button
105 * is raised, -1 as soon as it leaves box.
106 * invariant: nothing is highlighted on entry or exit.
109 menuscan(Image *m, Menu *menu, int but, Mousectl *mc, Rectangle textr, int off, int lasti, Image *save)
113 paintitem(m, menu, textr, off, lasti, 1, save, nil);
114 for(readmouse(mc); mc->m.buttons & (1<<(but-1)); readmouse(mc)){
115 i = menusel(textr, mc->m.xy);
116 if(i != -1 && i == lasti)
118 paintitem(m, menu, textr, off, lasti, 0, nil, save);
122 paintitem(m, menu, textr, off, lasti, 1, save, nil);
128 menupaint(Image *m, Menu *menu, Rectangle textr, int off, int nitemdrawn)
132 draw(m, insetrect(textr, Border-Margin), back, nil, ZP);
133 for(i = 0; i<nitemdrawn; i++)
134 paintitem(m, menu, textr, off, i, 0, nil, nil);
138 menuscrollpaint(Image *m, Rectangle scrollr, int off, int nitem, int nitemdrawn)
142 draw(m, scrollr, back, nil, ZP);
143 r.min.x = scrollr.min.x;
144 r.max.x = scrollr.max.x;
145 r.min.y = scrollr.min.y + (Dy(scrollr)*off)/nitem;
146 r.max.y = scrollr.min.y + (Dy(scrollr)*(off+nitemdrawn))/nitem;
147 if(r.max.y < r.min.y+2)
149 border(m, r, 1, bord, ZP);
151 menutxt = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DDarkgreen); /* border color; BUG? */
153 draw(m, insetrect(r, 1), menutxt, nil, ZP);
157 menuhit(int but, Mousectl *mc, Menu *menu, Screen *scr)
159 int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem;
161 Rectangle r, menur, sc, textr, scrollr;
162 Image *b, *save, *backup;
169 replclipr(screen, 0, screen->r);
172 item = menu->item? menu->item[nitem] : (*menu->gen)(nitem);
174 i = stringwidth(font, item);
178 if(menu->lasthit<0 || menu->lasthit>=nitem)
180 screenitem = (Dy(screen->r)-10)/(font->height+Vspacing);
181 if(nitem>Maxunscroll || nitem>screenitem){
183 nitemdrawn = Nscroll;
184 if(nitemdrawn > screenitem)
185 nitemdrawn = screenitem;
186 wid = maxwid + Gap + Scrollwid;
187 off = menu->lasthit - nitemdrawn/2;
190 if(off > nitem-nitemdrawn)
191 off = nitem-nitemdrawn;
192 lasti = menu->lasthit-off;
198 lasti = menu->lasthit;
200 r = insetrect(Rect(0, 0, wid, nitemdrawn*(font->height+Vspacing)), -Margin);
201 r = rectsubpt(r, Pt(wid/2, lasti*(font->height+Vspacing)+font->height/2));
202 r = rectaddpt(r, mc->m.xy);
204 if(r.max.x>screen->r.max.x)
205 pt.x = screen->r.max.x-r.max.x;
206 if(r.max.y>screen->r.max.y)
207 pt.y = screen->r.max.y-r.max.y;
208 if(r.min.x<screen->r.min.x)
209 pt.x = screen->r.min.x-r.min.x;
210 if(r.min.y<screen->r.min.y)
211 pt.y = screen->r.min.y-r.min.y;
212 menur = rectaddpt(r, pt);
213 textr.max.x = menur.max.x-Margin;
214 textr.min.x = textr.max.x-maxwid;
215 textr.min.y = menur.min.y+Margin;
216 textr.max.y = textr.min.y + nitemdrawn*(font->height+Vspacing);
218 scrollr = insetrect(menur, Border);
219 scrollr.max.x = scrollr.min.x+Scrollwid;
221 scrollr = Rect(0, 0, 0, 0);
224 b = allocwindow(scr, menur, Refbackup, DWhite);
230 backup = allocimage(display, menur, screen->chan, 0, -1);
232 draw(backup, menur, screen, nil, menur.min);
234 draw(b, menur, back, nil, ZP);
235 border(b, menur, Blackborder, bord, ZP);
236 save = allocimage(display, menurect(textr, 0), screen->chan, 0, -1);
237 r = menurect(textr, lasti);
238 moveto(mc, divpt(addpt(r.min, r.max), 2));
239 menupaint(b, menu, textr, off, nitemdrawn);
241 menuscrollpaint(b, scrollr, off, nitem, nitemdrawn);
242 while(mc->m.buttons & (1<<(but-1))){
243 lasti = menuscan(b, menu, but, mc, textr, off, lasti, save);
246 while(!ptinrect(mc->m.xy, textr) && (mc->m.buttons & (1<<(but-1)))){
247 if(scrolling && ptinrect(mc->m.xy, scrollr)){
248 noff = ((mc->m.xy.y-scrollr.min.y)*nitem)/Dy(scrollr);
249 noff -= nitemdrawn/2;
252 if(noff > nitem-nitemdrawn)
253 noff = nitem-nitemdrawn;
256 menupaint(b, menu, textr, off, nitemdrawn);
257 menuscrollpaint(b, scrollr, off, nitem, nitemdrawn);
266 draw(screen, menur, backup, nil, menur.min);
270 replclipr(screen, 0, sc);
271 flushimage(display, 1);
273 menu->lasthit = lasti+off;
274 return menu->lasthit;