frutil.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. int
  16. _frcanfit(Frame *f, Point pt, Frbox *b)
  17. {
  18. int left, w, nr;
  19. uint8_t *p;
  20. Rune r;
  21. left = f->r.max.x-pt.x;
  22. if(b->nrune < 0)
  23. return b->minwid <= left;
  24. if(left >= b->wid)
  25. return b->nrune;
  26. for(nr=0,p=b->ptr; *p; p+=w,nr++){
  27. r = *p;
  28. if(r < Runeself)
  29. w = 1;
  30. else
  31. w = chartorune(&r, (char*)p);
  32. left -= stringnwidth(f->font, (char*)p, 1);
  33. if(left < 0)
  34. return nr;
  35. }
  36. drawerror(f->display, "_frcanfit can't");
  37. return 0;
  38. }
  39. void
  40. _frcklinewrap(Frame *f, Point *p, Frbox *b)
  41. {
  42. if((b->nrune<0? b->minwid : b->wid) > f->r.max.x-p->x){
  43. p->x = f->r.min.x;
  44. p->y += f->font->height;
  45. }
  46. }
  47. void
  48. _frcklinewrap0(Frame *f, Point *p, Frbox *b)
  49. {
  50. if(_frcanfit(f, *p, b) == 0){
  51. p->x = f->r.min.x;
  52. p->y += f->font->height;
  53. }
  54. }
  55. void
  56. _fradvance(Frame *f, Point *p, Frbox *b)
  57. {
  58. if(b->nrune<0 && b->bc=='\n'){
  59. p->x = f->r.min.x;
  60. p->y += f->font->height;
  61. }else
  62. p->x += b->wid;
  63. }
  64. int
  65. _frnewwid(Frame *f, Point pt, Frbox *b)
  66. {
  67. b->wid = _frnewwid0(f, pt, b);
  68. return b->wid;
  69. }
  70. int
  71. _frnewwid0(Frame *f, Point pt, Frbox *b)
  72. {
  73. int c, x;
  74. c = f->r.max.x;
  75. x = pt.x;
  76. if(b->nrune>=0 || b->bc!='\t')
  77. return b->wid;
  78. if(x+b->minwid > c)
  79. x = pt.x = f->r.min.x;
  80. x += f->maxtab;
  81. x -= (x-f->r.min.x)%f->maxtab;
  82. if(x-pt.x<b->minwid || x>c)
  83. x = pt.x+b->minwid;
  84. return x-pt.x;
  85. }
  86. void
  87. _frclean(Frame *f, Point pt, int n0, int n1) /* look for mergeable boxes */
  88. {
  89. Frbox *b;
  90. int nb, c;
  91. c = f->r.max.x;
  92. for(nb=n0; nb<n1-1; nb++){
  93. b = &f->box[nb];
  94. _frcklinewrap(f, &pt, b);
  95. while(b[0].nrune>=0 && nb<n1-1 && b[1].nrune>=0 && pt.x+b[0].wid+b[1].wid<c){
  96. _frmergebox(f, nb);
  97. n1--;
  98. b = &f->box[nb];
  99. }
  100. _fradvance(f, &pt, &f->box[nb]);
  101. }
  102. for(; nb<f->nbox; nb++){
  103. b = &f->box[nb];
  104. _frcklinewrap(f, &pt, b);
  105. _fradvance(f, &pt, &f->box[nb]);
  106. }
  107. f->lastlinefull = 0;
  108. if(pt.y >= f->r.max.y)
  109. f->lastlinefull = 1;
  110. }