scroll.c 3.3 KB

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