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)
71 r = insetrect(r, Margin);
74 return (p.y-r.min.y)/(font->height+Vspacing);
79 paintitem(Menu *menu, Rectangle textr, int off, int i, int highlight, Image *save, Image *restore)
87 r = menurect(textr, i);
89 draw(screen, r, restore, nil, restore->r.min);
93 draw(save, save->r, screen, nil, r.min);
94 item = menu->item? menu->item[i+off] : (*menu->gen)(i+off);
95 pt.x = (textr.min.x+textr.max.x-stringwidth(font, item))/2;
96 pt.y = textr.min.y+i*(font->height+Vspacing);
97 draw(screen, r, highlight? high : back, nil, pt);
98 string(screen, pt, highlight? htext : text, pt, font, item);
102 * menur is a rectangle holding all the highlightable text elements.
103 * track mouse while inside the box, return what's selected when button
104 * is raised, -1 as soon as it leaves box.
105 * invariant: nothing is highlighted on entry or exit.
108 menuscan(Menu *menu, int but, Mouse *m, Rectangle textr, int off, int lasti, Image *save)
112 paintitem(menu, textr, off, lasti, 1, save, nil);
113 flushimage(display, 1); /* in case display->locking is set */
115 while(m->buttons & (1<<(but-1))){
116 flushimage(display, 1); /* in case display->locking is set */
118 i = menusel(textr, m->xy);
119 if(i != -1 && i == lasti)
121 paintitem(menu, textr, off, lasti, 0, nil, save);
125 paintitem(menu, textr, off, lasti, 1, save, nil);
131 menupaint(Menu *menu, Rectangle textr, int off, int nitemdrawn)
135 draw(screen, insetrect(textr, Border-Margin), back, nil, ZP);
136 for(i = 0; i<nitemdrawn; i++)
137 paintitem(menu, textr, off, i, 0, nil, nil);
141 menuscrollpaint(Rectangle scrollr, int off, int nitem, int nitemdrawn)
145 draw(screen, scrollr, back, nil, ZP);
146 r.min.x = scrollr.min.x;
147 r.max.x = scrollr.max.x;
148 r.min.y = scrollr.min.y + (Dy(scrollr)*off)/nitem;
149 r.max.y = scrollr.min.y + (Dy(scrollr)*(off+nitemdrawn))/nitem;
150 if(r.max.y < r.min.y+2)
152 border(screen, r, 1, bord, ZP);
154 menutxt = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DDarkgreen);
156 draw(screen, insetrect(r, 1), menutxt, nil, ZP);
160 emenuhit(int but, Mouse *m, Menu *menu)
162 int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem;
164 Rectangle r, menur, sc, textr, scrollr;
172 replclipr(screen, 0, screen->r);
175 item = menu->item? menu->item[nitem] : (*menu->gen)(nitem);
177 i = stringwidth(font, item);
181 if(menu->lasthit<0 || menu->lasthit>=nitem)
183 screenitem = (Dy(screen->r)-10)/(font->height+Vspacing);
184 if(nitem>Maxunscroll || nitem>screenitem){
186 nitemdrawn = Nscroll;
187 if(nitemdrawn > screenitem)
188 nitemdrawn = screenitem;
189 wid = maxwid + Gap + Scrollwid;
190 off = menu->lasthit - nitemdrawn/2;
193 if(off > nitem-nitemdrawn)
194 off = nitem-nitemdrawn;
195 lasti = menu->lasthit-off;
201 lasti = menu->lasthit;
203 r = insetrect(Rect(0, 0, wid, nitemdrawn*(font->height+Vspacing)), -Margin);
204 r = rectsubpt(r, Pt(wid/2, lasti*(font->height+Vspacing)+font->height/2));
205 r = rectaddpt(r, m->xy);
207 if(r.max.x>screen->r.max.x)
208 pt.x = screen->r.max.x-r.max.x;
209 if(r.max.y>screen->r.max.y)
210 pt.y = screen->r.max.y-r.max.y;
211 if(r.min.x<screen->r.min.x)
212 pt.x = screen->r.min.x-r.min.x;
213 if(r.min.y<screen->r.min.y)
214 pt.y = screen->r.min.y-r.min.y;
215 menur = rectaddpt(r, pt);
216 textr.max.x = menur.max.x-Margin;
217 textr.min.x = textr.max.x-maxwid;
218 textr.min.y = menur.min.y+Margin;
219 textr.max.y = textr.min.y + nitemdrawn*(font->height+Vspacing);
221 scrollr = insetrect(menur, Border);
222 scrollr.max.x = scrollr.min.x+Scrollwid;
224 scrollr = Rect(0, 0, 0, 0);
226 b = allocimage(display, menur, screen->chan, 0, 0);
229 draw(b, menur, screen, nil, menur.min);
230 draw(screen, menur, back, nil, ZP);
231 border(screen, menur, Blackborder, bord, ZP);
232 save = allocimage(display, menurect(textr, 0), screen->chan, 0, -1);
233 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);
265 flushimage(display, 1);
267 menu->lasthit = lasti+off;
268 return menu->lasthit;