frdraw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include <frame.h>
  15. void
  16. _frdrawtext(Frame *f, Point pt, Image *text, Image *back)
  17. {
  18. Frbox *b;
  19. int nb;
  20. static int x;
  21. for(nb=0,b=f->box; nb<f->nbox; nb++, b++){
  22. _frcklinewrap(f, &pt, b);
  23. if(b->nrune >= 0){
  24. stringbg(f->b, pt, text, ZP, f->font, (char*)b->ptr,
  25. back, ZP);
  26. }
  27. pt.x += b->wid;
  28. }
  29. }
  30. static int
  31. nbytes(char *s0, int nr)
  32. {
  33. char *s;
  34. Rune r;
  35. s = s0;
  36. while(--nr >= 0)
  37. s += chartorune(&r, s);
  38. return s-s0;
  39. }
  40. void
  41. frdrawsel(Frame *f, Point pt, uint32_t p0, uint32_t p1, int issel)
  42. {
  43. Image *back, *text;
  44. if(f->ticked)
  45. frtick(f, frptofchar(f, f->p0), 0);
  46. if(p0 == p1){
  47. frtick(f, pt, issel);
  48. return;
  49. }
  50. if(issel){
  51. back = f->cols[HIGH];
  52. text = f->cols[HTEXT];
  53. }else{
  54. back = f->cols[BACK];
  55. text = f->cols[TEXT];
  56. }
  57. frdrawsel0(f, pt, p0, p1, back, text);
  58. }
  59. Point
  60. frdrawsel0(Frame *f, Point pt, uint32_t p0, uint32_t p1, Image *back,
  61. Image *text)
  62. {
  63. Frbox *b;
  64. int nb, nr, w, x, trim;
  65. Point qt;
  66. uint p;
  67. char *ptr;
  68. p = 0;
  69. b = f->box;
  70. trim = 0;
  71. for(nb=0; nb<f->nbox && p<p1; nb++){
  72. nr = b->nrune;
  73. if(nr < 0)
  74. nr = 1;
  75. if(p+nr <= p0)
  76. goto Continue;
  77. if(p >= p0){
  78. qt = pt;
  79. _frcklinewrap(f, &pt, b);
  80. /* fill in the end of a wrapped line */
  81. if(pt.y > qt.y)
  82. draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt);
  83. }
  84. ptr = (char*)b->ptr;
  85. if(p < p0){ /* beginning of region: advance into box */
  86. ptr += nbytes(ptr, p0-p);
  87. nr -= (p0-p);
  88. p = p0;
  89. }
  90. trim = 0;
  91. if(p+nr > p1){ /* end of region: trim box */
  92. nr -= (p+nr)-p1;
  93. trim = 1;
  94. }
  95. if(b->nrune<0 || nr==b->nrune)
  96. w = b->wid;
  97. else
  98. w = stringnwidth(f->font, ptr, nr);
  99. x = pt.x+w;
  100. if(x > f->r.max.x)
  101. x = f->r.max.x;
  102. draw(f->b, Rect(pt.x, pt.y, x, pt.y+f->font->height), back, nil, pt);
  103. if(b->nrune >= 0)
  104. stringnbg(f->b, pt, text, ZP, f->font, ptr, nr, back, ZP);
  105. pt.x += w;
  106. Continue:
  107. b++;
  108. p += nr;
  109. }
  110. /* if this is end of last plain text box on wrapped line, fill to end of line */
  111. if(p1>p0 && b>f->box && b<f->box+f->nbox && b[-1].nrune>0 && !trim){
  112. qt = pt;
  113. _frcklinewrap(f, &pt, b);
  114. if(pt.y > qt.y)
  115. draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt);
  116. }
  117. return pt;
  118. }
  119. void
  120. frredraw(Frame *f)
  121. {
  122. int ticked;
  123. Point pt;
  124. if(f->p0 == f->p1){
  125. ticked = f->ticked;
  126. if(ticked)
  127. frtick(f, frptofchar(f, f->p0), 0);
  128. frdrawsel0(f, frptofchar(f, 0), 0, f->nchars, f->cols[BACK], f->cols[TEXT]);
  129. if(ticked)
  130. frtick(f, frptofchar(f, f->p0), 1);
  131. return;
  132. }
  133. pt = frptofchar(f, 0);
  134. pt = frdrawsel0(f, pt, 0, f->p0, f->cols[BACK], f->cols[TEXT]);
  135. pt = frdrawsel0(f, pt, f->p0, f->p1, f->cols[HIGH], f->cols[HTEXT]);
  136. pt = frdrawsel0(f, pt, f->p1, f->nchars, f->cols[BACK], f->cols[TEXT]);
  137. }
  138. void
  139. frtick(Frame *f, Point pt, int ticked)
  140. {
  141. Rectangle r;
  142. if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r))
  143. return;
  144. pt.x--; /* looks best just left of where requested */
  145. r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height);
  146. /* can go into left border but not right */
  147. if(r.max.x > f->r.max.x)
  148. r.max.x = f->r.max.x;
  149. if(ticked){
  150. draw(f->tickback, f->tickback->r, f->b, nil, pt);
  151. draw(f->b, r, f->tick, nil, ZP);
  152. }else
  153. draw(f->b, r, f->tickback, nil, ZP);
  154. f->ticked = ticked;
  155. }
  156. Point
  157. _frdraw(Frame *f, Point pt)
  158. {
  159. Frbox *b;
  160. int nb, n;
  161. for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
  162. _frcklinewrap0(f, &pt, b);
  163. if(pt.y == f->r.max.y){
  164. f->nchars -= _frstrlen(f, nb);
  165. _frdelbox(f, nb, f->nbox-1);
  166. break;
  167. }
  168. if(b->nrune > 0){
  169. n = _frcanfit(f, pt, b);
  170. if(n == 0)
  171. drawerror(f->display, "_frcanfit==0");
  172. if(n != b->nrune){
  173. _frsplitbox(f, nb, n);
  174. b = &f->box[nb];
  175. }
  176. pt.x += b->wid;
  177. }else{
  178. if(b->bc == '\n'){
  179. pt.x = f->r.min.x;
  180. pt.y+=f->font->height;
  181. }else
  182. pt.x += _frnewwid(f, pt, b);
  183. }
  184. }
  185. return pt;
  186. }
  187. int
  188. _frstrlen(Frame *f, int nb)
  189. {
  190. int n;
  191. for(n=0; nb<f->nbox; nb++)
  192. n += NRUNE(&f->box[nb]);
  193. return n;
  194. }