frdraw.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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)
  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. string(f->b, pt, f->cols[TEXT], ZP, f->font, (char *)b->ptr);
  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. if(pt.y > qt.y)
  71. draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt);
  72. }
  73. ptr = (char*)b->ptr;
  74. if(p < p0){ /* beginning of region: advance into box */
  75. ptr += nbytes(ptr, p0-p);
  76. nr -= (p0-p);
  77. p = p0;
  78. }
  79. trim = 0;
  80. if(p+nr > p1){ /* end of region: trim box */
  81. nr -= (p+nr)-p1;
  82. trim = 1;
  83. }
  84. if(b->nrune<0 || nr==b->nrune)
  85. w = b->wid;
  86. else
  87. w = stringnwidth(f->font, ptr, nr);
  88. x = pt.x+w;
  89. if(x > f->r.max.x)
  90. x = f->r.max.x;
  91. draw(f->b, Rect(pt.x, pt.y, x, pt.y+f->font->height), back, nil, pt);
  92. if(b->nrune >= 0)
  93. stringn(f->b, pt, text, ZP, f->font, ptr, nr);
  94. pt.x += w;
  95. Continue:
  96. b++;
  97. p += nr;
  98. }
  99. /* if this is end of last plain text box on wrapped line, fill to end of line */
  100. if(p1>p0 && b>f->box && b<f->box+f->nbox && b[-1].nrune>0 && !trim){
  101. qt = pt;
  102. _frcklinewrap(f, &pt, b);
  103. if(pt.y > qt.y)
  104. draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt);
  105. }
  106. }
  107. void
  108. frtick(Frame *f, Point pt, int ticked)
  109. {
  110. Rectangle r;
  111. if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r))
  112. return;
  113. pt.x--; /* looks best just left of where requested */
  114. r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height);
  115. if(ticked){
  116. draw(f->tickback, f->tickback->r, f->b, nil, pt);
  117. draw(f->b, r, f->tick, nil, ZP);
  118. }else
  119. draw(f->b, r, f->tickback, nil, ZP);
  120. f->ticked = ticked;
  121. }
  122. Point
  123. _frdraw(Frame *f, Point pt)
  124. {
  125. Frbox *b;
  126. int nb, n;
  127. for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
  128. _frcklinewrap0(f, &pt, b);
  129. if(pt.y == f->r.max.y){
  130. f->nchars -= _frstrlen(f, nb);
  131. _frdelbox(f, nb, f->nbox-1);
  132. break;
  133. }
  134. if(b->nrune > 0){
  135. n = _frcanfit(f, pt, b);
  136. if(n == 0)
  137. drawerror(f->display, "_frcanfit==0");
  138. if(n != b->nrune){
  139. _frsplitbox(f, nb, n);
  140. b = &f->box[nb];
  141. }
  142. pt.x += b->wid;
  143. }else{
  144. if(b->bc == '\n'){
  145. pt.x = f->r.min.x;
  146. pt.y+=f->font->height;
  147. }else
  148. pt.x += _frnewwid(f, pt, b);
  149. }
  150. }
  151. return pt;
  152. }
  153. int
  154. _frstrlen(Frame *f, int nb)
  155. {
  156. int n;
  157. for(n=0; nb<f->nbox; nb++)
  158. n += NRUNE(&f->box[nb]);
  159. return n;
  160. }