emenuhit.c 7.0 KB

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