scroll.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <keyboard.h>
  15. #include <frame.h>
  16. #include "flayer.h"
  17. #include "samterm.h"
  18. static Image *scrtmp;
  19. static Image *scrback;
  20. void
  21. scrtemps(void)
  22. {
  23. int h;
  24. if(scrtmp)
  25. return;
  26. if(screensize(0, &h) == 0)
  27. h = 2048;
  28. scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, 0);
  29. scrback = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, 0);
  30. if(scrtmp==0 || scrback==0)
  31. panic("scrtemps");
  32. }
  33. Rectangle
  34. scrpos(Rectangle r, long p0, long p1, long tot)
  35. {
  36. Rectangle q;
  37. int h;
  38. q = r;
  39. h = q.max.y-q.min.y;
  40. if(tot == 0)
  41. return q;
  42. if(tot > 1024L*1024L)
  43. tot>>=10, p0>>=10, p1>>=10;
  44. if(p0 > 0)
  45. q.min.y += h*p0/tot;
  46. if(p1 < tot)
  47. q.max.y -= h*(tot-p1)/tot;
  48. if(q.max.y < q.min.y+2){
  49. if(q.min.y+2 <= r.max.y)
  50. q.max.y = q.min.y+2;
  51. else
  52. q.min.y = q.max.y-2;
  53. }
  54. return q;
  55. }
  56. void
  57. scrmark(Flayer *l, Rectangle r)
  58. {
  59. r.max.x--;
  60. if(rectclip(&r, l->scroll)) {
  61. if (l->f.b == nil)
  62. panic("scrmark: nil l->f.b");
  63. draw(l->f.b, r, l->f.cols[HIGH], nil, ZP);
  64. }
  65. }
  66. void
  67. scrunmark(Flayer *l, Rectangle r)
  68. {
  69. if(rectclip(&r, l->scroll)) {
  70. if (l->f.b == nil)
  71. panic("scrunmark: nil l->f.b");
  72. draw(l->f.b, r, scrback, nil, Pt(0, r.min.y-l->scroll.min.y));
  73. }
  74. }
  75. void
  76. scrdraw(Flayer *l, long tot)
  77. {
  78. Rectangle r, r1, r2;
  79. Image *b;
  80. scrtemps();
  81. if(l->f.b == 0)
  82. panic("scrdraw");
  83. r = l->scroll;
  84. r1 = r;
  85. if(l->visible == All){
  86. b = scrtmp;
  87. r1.min.x = 0;
  88. r1.max.x = Dx(r);
  89. }else
  90. b = l->f.b;
  91. r2 = scrpos(r1, l->origin, l->origin+l->f.nchars, tot);
  92. if(!eqrect(r2, l->lastsr)){
  93. l->lastsr = r2;
  94. draw(b, r1, l->f.cols[BORD], nil, ZP);
  95. draw(b, r2, l->f.cols[BACK], nil, r2.min);
  96. r2 = r1;
  97. r2.min.x = r2.max.x-1;
  98. draw(b, r2, l->f.cols[BORD], nil, ZP);
  99. if(b!=l->f.b)
  100. draw(l->f.b, r, b, nil, r1.min);
  101. }
  102. }
  103. void
  104. scroll(Flayer *l, int but)
  105. {
  106. int in = 0, oin;
  107. long tot = scrtotal(l);
  108. Rectangle scr, r, s, rt;
  109. int x, y, my, oy, h;
  110. long p0;
  111. s = l->scroll;
  112. x = s.min.x+FLSCROLLWID/2;
  113. scr = scrpos(l->scroll, l->origin, l->origin+l->f.nchars, tot);
  114. r = scr;
  115. y = scr.min.y;
  116. my = mousep->xy.y;
  117. draw(scrback, Rect(0,0,Dx(l->scroll), Dy(l->scroll)), l->f.b, nil, l->scroll.min);
  118. do{
  119. oin = in;
  120. in = abs(x-mousep->xy.x)<=FLSCROLLWID/2;
  121. if(oin && !in)
  122. scrunmark(l, r);
  123. if(in){
  124. scrmark(l, r);
  125. oy = y;
  126. my = mousep->xy.y;
  127. if(my < s.min.y)
  128. my = s.min.y;
  129. if(my >= s.max.y)
  130. my = s.max.y;
  131. if(!eqpt(mousep->xy, Pt(x, my)))
  132. moveto(mousectl, Pt(x, my));
  133. if(but == 1){
  134. p0 = l->origin-frcharofpt(&l->f, Pt(s.max.x, my));
  135. rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
  136. y = rt.min.y;
  137. }else if(but == 2){
  138. y = my;
  139. if(y > s.max.y-2)
  140. y = s.max.y-2;
  141. }else if(but == 3){
  142. p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
  143. rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
  144. y = rt.min.y;
  145. }
  146. if(y != oy){
  147. scrunmark(l, r);
  148. r = rectaddpt(scr, Pt(0, y-scr.min.y));
  149. scrmark(l, r);
  150. }
  151. }
  152. }while(button(but));
  153. if(in){
  154. h = s.max.y-s.min.y;
  155. scrunmark(l, r);
  156. p0 = 0;
  157. if(but == 1)
  158. p0 = (long)(my-s.min.y)/l->f.font->height+1;
  159. else if(but == 2){
  160. if(tot > 1024L*1024L)
  161. p0 = ((tot>>10)*(y-s.min.y)/h)<<10;
  162. else
  163. p0 = tot*(y-s.min.y)/h;
  164. }else if(but == 3){
  165. p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
  166. if(p0 > tot)
  167. p0 = tot;
  168. }
  169. scrorigin(l, but, p0);
  170. }
  171. }