frdraw.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. _frredraw(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. void
  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. }
  108. void
  109. frtick(Frame *f, Point pt, int ticked)
  110. {
  111. Rectangle r;
  112. if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r))
  113. return;
  114. pt.x--; /* looks best just left of where requested */
  115. r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height);
  116. /* can go into left border but not right */
  117. if(r.max.x > f->r.max.x)
  118. r.max.x = f->r.max.x;
  119. if(ticked){
  120. draw(f->tickback, f->tickback->r, f->b, nil, pt);
  121. draw(f->b, r, f->tick, nil, ZP);
  122. }else
  123. draw(f->b, r, f->tickback, nil, ZP);
  124. f->ticked = ticked;
  125. }
  126. Point
  127. _frdraw(Frame *f, Point pt)
  128. {
  129. Frbox *b;
  130. int nb, n;
  131. for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
  132. _frcklinewrap0(f, &pt, b);
  133. if(pt.y == f->r.max.y){
  134. f->nchars -= _frstrlen(f, nb);
  135. _frdelbox(f, nb, f->nbox-1);
  136. break;
  137. }
  138. if(b->nrune > 0){
  139. n = _frcanfit(f, pt, b);
  140. if(n == 0)
  141. drawerror(f->display, "_frcanfit==0");
  142. if(n != b->nrune){
  143. _frsplitbox(f, nb, n);
  144. b = &f->box[nb];
  145. }
  146. pt.x += b->wid;
  147. }else{
  148. if(b->bc == '\n'){
  149. pt.x = f->r.min.x;
  150. pt.y+=f->font->height;
  151. }else
  152. pt.x += _frnewwid(f, pt, b);
  153. }
  154. }
  155. return pt;
  156. }
  157. int
  158. _frstrlen(Frame *f, int nb)
  159. {
  160. int n;
  161. for(n=0; nb<f->nbox; nb++)
  162. n += NRUNE(&f->box[nb]);
  163. return n;
  164. }