scroll.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. draw(l->f.b, r, l->f.cols[HIGH], nil, ZP);
  54. }
  55. void
  56. scrunmark(Flayer *l, Rectangle r)
  57. {
  58. if(rectclip(&r, l->scroll))
  59. draw(l->f.b, r, scrback, nil, Pt(0, r.min.y-l->scroll.min.y));
  60. }
  61. void
  62. scrdraw(Flayer *l, long tot)
  63. {
  64. Rectangle r, r1, r2;
  65. Image *b;
  66. scrtemps();
  67. if(l->f.b == 0)
  68. panic("scrdraw");
  69. r = l->scroll;
  70. r1 = r;
  71. if(l->visible == All){
  72. b = scrtmp;
  73. r1.min.x = 0;
  74. r1.max.x = Dx(r);
  75. }else
  76. b = l->f.b;
  77. r2 = scrpos(r1, l->origin, l->origin+l->f.nchars, tot);
  78. if(!eqrect(r2, l->lastsr)){
  79. l->lastsr = r2;
  80. draw(b, r1, l->f.cols[BORD], nil, ZP);
  81. draw(b, r2, l->f.cols[BACK], nil, r2.min);
  82. r2 = r1;
  83. r2.min.x = r2.max.x-1;
  84. draw(b, r2, l->f.cols[BORD], nil, ZP);
  85. if(b!=l->f.b)
  86. draw(l->f.b, r, b, nil, r1.min);
  87. }
  88. }
  89. void
  90. scroll(Flayer *l, int but)
  91. {
  92. int in = 0, oin;
  93. long tot = scrtotal(l);
  94. Rectangle scr, r, s, rt;
  95. int x, y, my, oy, h;
  96. long p0;
  97. s = l->scroll;
  98. x = s.min.x+FLSCROLLWID/2;
  99. scr = scrpos(l->scroll, l->origin, l->origin+l->f.nchars, tot);
  100. r = scr;
  101. y = scr.min.y;
  102. my = mousep->xy.y;
  103. draw(scrback, Rect(0,0,Dx(l->scroll), Dy(l->scroll)), l->f.b, nil, l->scroll.min);
  104. do{
  105. oin = in;
  106. in = abs(x-mousep->xy.x)<=FLSCROLLWID/2;
  107. if(oin && !in)
  108. scrunmark(l, r);
  109. if(in){
  110. scrmark(l, r);
  111. oy = y;
  112. my = mousep->xy.y;
  113. if(my < s.min.y)
  114. my = s.min.y;
  115. if(my >= s.max.y)
  116. my = s.max.y;
  117. if(!eqpt(mousep->xy, Pt(x, my)))
  118. moveto(mousectl, Pt(x, my));
  119. if(but == 1){
  120. p0 = l->origin-frcharofpt(&l->f, Pt(s.max.x, my));
  121. rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
  122. y = rt.min.y;
  123. }else if(but == 2){
  124. y = my;
  125. if(y > s.max.y-2)
  126. y = s.max.y-2;
  127. }else if(but == 3){
  128. p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
  129. rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
  130. y = rt.min.y;
  131. }
  132. if(y != oy){
  133. scrunmark(l, r);
  134. r = rectaddpt(scr, Pt(0, y-scr.min.y));
  135. scrmark(l, r);
  136. }
  137. }
  138. }while(button(but));
  139. if(in){
  140. h = s.max.y-s.min.y;
  141. scrunmark(l, r);
  142. p0 = 0;
  143. if(but == 1)
  144. p0 = (long)(my-s.min.y)/l->f.font->height+1;
  145. else if(but == 2){
  146. if(tot > 1024L*1024L)
  147. p0 = ((tot>>10)*(y-s.min.y)/h)<<10;
  148. else
  149. p0 = tot*(y-s.min.y)/h;
  150. }else if(but == 3){
  151. p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
  152. if(p0 > tot)
  153. p0 = tot;
  154. }
  155. scrorigin(l, but, p0);
  156. }
  157. }