frdelete.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. frdelete(Frame *f, uint32_t p0, uint32_t p1)
  17. {
  18. Point pt0, pt1, ppt0;
  19. Frbox *b;
  20. int n0, n1, n;
  21. uint32_t cn1;
  22. Rectangle r;
  23. int nn0;
  24. Image *col;
  25. if(p0>=f->nchars || p0==p1 || f->b==nil)
  26. return 0;
  27. if(p1 > f->nchars)
  28. p1 = f->nchars;
  29. n0 = _frfindbox(f, 0, 0, p0);
  30. if(n0 == f->nbox)
  31. drawerror(f->display, "off end in frdelete");
  32. n1 = _frfindbox(f, n0, p0, p1);
  33. pt0 = _frptofcharnb(f, p0, n0);
  34. pt1 = frptofchar(f, p1);
  35. if(f->p0 == f->p1)
  36. frtick(f, frptofchar(f, f->p0), 0);
  37. nn0 = n0;
  38. ppt0 = pt0;
  39. _frfreebox(f, n0, n1-1);
  40. f->modified = 1;
  41. /*
  42. * Invariants:
  43. * - pt0 points to beginning, pt1 points to end
  44. * - n0 is box containing beginning of stuff being deleted
  45. * - n1, b are box containing beginning of stuff to be kept after deletion
  46. * - cn1 is char position of n1
  47. * - f->p0 and f->p1 are not adjusted until after all deletion is done
  48. */
  49. b = &f->box[n1];
  50. cn1 = p1;
  51. while(pt1.x!=pt0.x && n1<f->nbox){
  52. _frcklinewrap0(f, &pt0, b);
  53. _frcklinewrap(f, &pt1, b);
  54. n = _frcanfit(f, pt0, b);
  55. if(n==0)
  56. drawerror(f->display, "_frcanfit==0");
  57. r.min = pt0;
  58. r.max = pt0;
  59. r.max.y += f->font->height;
  60. if(b->nrune > 0){
  61. if(n != b->nrune){
  62. _frsplitbox(f, n1, n);
  63. b = &f->box[n1];
  64. }
  65. r.max.x += b->wid;
  66. draw(f->b, r, f->b, nil, pt1);
  67. cn1 += b->nrune;
  68. }else{
  69. r.max.x += _frnewwid0(f, pt0, b);
  70. if(r.max.x > f->r.max.x)
  71. r.max.x = f->r.max.x;
  72. col = f->cols[BACK];
  73. if(f->p0<=cn1 && cn1<f->p1)
  74. col = f->cols[HIGH];
  75. draw(f->b, r, col, nil, pt0);
  76. cn1++;
  77. }
  78. _fradvance(f, &pt1, b);
  79. pt0.x += _frnewwid(f, pt0, b);
  80. f->box[n0++] = f->box[n1++];
  81. b++;
  82. }
  83. if(n1==f->nbox && pt0.x!=pt1.x) /* deleting last thing in window; must clean up */
  84. frselectpaint(f, pt0, pt1, f->cols[BACK]);
  85. if(pt1.y != pt0.y){
  86. Point pt2;
  87. pt2 = _frptofcharptb(f, 32767, pt1, n1);
  88. if(pt2.y > f->r.max.y)
  89. drawerror(f->display, "frptofchar in frdelete");
  90. if(n1 < f->nbox){
  91. int q0, q1, q2;
  92. q0 = pt0.y+f->font->height;
  93. q1 = pt1.y+f->font->height;
  94. q2 = pt2.y+f->font->height;
  95. if(q2 > f->r.max.y)
  96. q2 = f->r.max.y;
  97. draw(f->b, Rect(pt0.x, pt0.y, pt0.x+(f->r.max.x-pt1.x), q0),
  98. f->b, nil, pt1);
  99. draw(f->b, Rect(f->r.min.x, q0, f->r.max.x, q0+(q2-q1)),
  100. f->b, nil, Pt(f->r.min.x, q1));
  101. frselectpaint(f, Pt(pt2.x, pt2.y-(pt1.y-pt0.y)), pt2, f->cols[BACK]);
  102. }else
  103. frselectpaint(f, pt0, pt2, f->cols[BACK]);
  104. }
  105. _frclosebox(f, n0, n1-1);
  106. if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->r.min.x){
  107. --nn0;
  108. ppt0.x -= f->box[nn0].wid;
  109. }
  110. _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
  111. if(f->p1 > p1)
  112. f->p1 -= p1-p0;
  113. else if(f->p1 > p0)
  114. f->p1 = p0;
  115. if(f->p0 > p1)
  116. f->p0 -= p1-p0;
  117. else if(f->p0 > p0)
  118. f->p0 = p0;
  119. f->nchars -= p1-p0;
  120. if(f->p0 == f->p1)
  121. frtick(f, frptofchar(f, f->p0), 1);
  122. pt0 = frptofchar(f, f->nchars);
  123. n = f->nlines;
  124. f->nlines = (pt0.y-f->r.min.y)/f->font->height+(pt0.x>f->r.min.x);
  125. return n - f->nlines;
  126. }