frdraw.c 3.9 KB

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