frbox.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #define SLOP 25
  16. void
  17. _fraddbox(Frame *f, int bn, int n) /* add n boxes after bn, shift the rest up,
  18. * box[bn+n]==box[bn] */
  19. {
  20. int i;
  21. if(bn > f->nbox)
  22. drawerror(f->display, "_fraddbox");
  23. if(f->nbox+n > f->nalloc)
  24. _frgrowbox(f, n+SLOP);
  25. for(i=f->nbox; --i>=bn; )
  26. f->box[i+n] = f->box[i];
  27. f->nbox+=n;
  28. }
  29. void
  30. _frclosebox(Frame *f, int n0, int n1) /* inclusive */
  31. {
  32. int i;
  33. if(n0>=f->nbox || n1>=f->nbox || n1<n0)
  34. drawerror(f->display, "_frclosebox");
  35. n1++;
  36. for(i=n1; i<f->nbox; i++)
  37. f->box[i-(n1-n0)] = f->box[i];
  38. f->nbox -= n1-n0;
  39. }
  40. void
  41. _frdelbox(Frame *f, int n0, int n1) /* inclusive */
  42. {
  43. if(n0>=f->nbox || n1>=f->nbox || n1<n0)
  44. drawerror(f->display, "_frdelbox");
  45. _frfreebox(f, n0, n1);
  46. _frclosebox(f, n0, n1);
  47. }
  48. void
  49. _frfreebox(Frame *f, int n0, int n1) /* inclusive */
  50. {
  51. int i;
  52. if(n1<n0)
  53. return;
  54. if(n0>=f->nbox || n1>=f->nbox)
  55. drawerror(f->display, "_frfreebox");
  56. n1++;
  57. for(i=n0; i<n1; i++)
  58. if(f->box[i].nrune >= 0)
  59. free(f->box[i].ptr);
  60. }
  61. void
  62. _frgrowbox(Frame *f, int delta)
  63. {
  64. f->nalloc += delta;
  65. f->box = realloc(f->box, f->nalloc*sizeof(Frbox));
  66. if(f->box == 0)
  67. drawerror(f->display, "_frgrowbox");
  68. }
  69. static
  70. void
  71. dupbox(Frame *f, int bn)
  72. {
  73. uint8_t *p;
  74. if(f->box[bn].nrune < 0)
  75. drawerror(f->display, "dupbox");
  76. _fraddbox(f, bn, 1);
  77. if(f->box[bn].nrune >= 0){
  78. p = _frallocstr(f, NBYTE(&f->box[bn])+1);
  79. strcpy((char*)p, (char*)f->box[bn].ptr);
  80. f->box[bn+1].ptr = p;
  81. }
  82. }
  83. static
  84. uint8_t*
  85. runeindex(uint8_t *p, int n)
  86. {
  87. int i, w;
  88. Rune rune;
  89. for(i=0; i<n; i++,p+=w)
  90. if(*p < Runeself)
  91. w = 1;
  92. else{
  93. w = chartorune(&rune, (char*)p);
  94. USED(rune);
  95. }
  96. return p;
  97. }
  98. static
  99. void
  100. truncatebox(Frame *f, Frbox *b, int n) /* drop last n chars; no allocation done */
  101. {
  102. if(b->nrune<0 || b->nrune<n)
  103. drawerror(f->display, "truncatebox");
  104. b->nrune -= n;
  105. runeindex(b->ptr, b->nrune)[0] = 0;
  106. b->wid = stringwidth(f->font, (char *)b->ptr);
  107. }
  108. static
  109. void
  110. chopbox(Frame *f, Frbox *b, int n) /* drop first n chars; no allocation done */
  111. {
  112. char *p;
  113. if(b->nrune<0 || b->nrune<n)
  114. drawerror(f->display, "chopbox");
  115. p = (char*)runeindex(b->ptr, n);
  116. memmove((char*)b->ptr, p, strlen(p)+1);
  117. b->nrune -= n;
  118. b->wid = stringwidth(f->font, (char *)b->ptr);
  119. }
  120. void
  121. _frsplitbox(Frame *f, int bn, int n)
  122. {
  123. dupbox(f, bn);
  124. truncatebox(f, &f->box[bn], f->box[bn].nrune-n);
  125. chopbox(f, &f->box[bn+1], n);
  126. }
  127. void
  128. _frmergebox(Frame *f, int bn) /* merge bn and bn+1 */
  129. {
  130. Frbox *b;
  131. b = &f->box[bn];
  132. _frinsure(f, bn, NBYTE(&b[0])+NBYTE(&b[1])+1);
  133. strcpy((char*)runeindex(b[0].ptr, b[0].nrune), (char*)b[1].ptr);
  134. b[0].wid += b[1].wid;
  135. b[0].nrune += b[1].nrune;
  136. _frdelbox(f, bn+1, bn+1);
  137. }
  138. int
  139. _frfindbox(Frame *f, int bn, uint32_t p, uint32_t q) /* find box containing q and put q on a box boundary */
  140. {
  141. Frbox *b;
  142. for(b = &f->box[bn]; bn<f->nbox && p+NRUNE(b)<=q; bn++, b++)
  143. p += NRUNE(b);
  144. if(p != q)
  145. _frsplitbox(f, bn++, (int)(q-p));
  146. return bn;
  147. }