emenuhit.c 7.4 KB

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