menuhit.c 7.5 KB

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