scrl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <cursor.h>
  6. #include <mouse.h>
  7. #include <keyboard.h>
  8. #include <frame.h>
  9. #include <fcall.h>
  10. #include "dat.h"
  11. #include "fns.h"
  12. static Image *scrtmp;
  13. static
  14. void
  15. scrtemps(void)
  16. {
  17. int h;
  18. if(scrtmp)
  19. return;
  20. h = BIG*Dy(screen->r);
  21. scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, DWhite);
  22. if(scrtmp == nil)
  23. error("scrtemps");
  24. }
  25. void
  26. freescrtemps(void)
  27. {
  28. freeimage(scrtmp);
  29. scrtmp = nil;
  30. }
  31. static
  32. Rectangle
  33. scrpos(Rectangle r, uint p0, uint p1, uint tot)
  34. {
  35. Rectangle q;
  36. int h;
  37. q = r;
  38. h = q.max.y-q.min.y;
  39. if(tot == 0)
  40. return q;
  41. if(tot > 1024*1024){
  42. tot>>=10;
  43. p0>>=10;
  44. p1>>=10;
  45. }
  46. if(p0 > 0)
  47. q.min.y += h*p0/tot;
  48. if(p1 < tot)
  49. q.max.y -= h*(tot-p1)/tot;
  50. if(q.max.y < q.min.y+2){
  51. if(q.min.y+2 <= r.max.y)
  52. q.max.y = q.min.y+2;
  53. else
  54. q.min.y = q.max.y-2;
  55. }
  56. return q;
  57. }
  58. void
  59. wscrdraw(Window *w)
  60. {
  61. Rectangle r, r1, r2;
  62. Image *b;
  63. scrtemps();
  64. if(w->i == nil)
  65. error("scrdraw");
  66. r = w->scrollr;
  67. b = scrtmp;
  68. r1 = r;
  69. r1.min.x = 0;
  70. r1.max.x = Dx(r);
  71. r2 = scrpos(r1, w->org, w->org+w->nchars, w->nr);
  72. if(!eqrect(r2, w->lastsr)){
  73. w->lastsr = r2;
  74. /* move r1, r2 to (0,0) to avoid clipping */
  75. r2 = rectsubpt(r2, r1.min);
  76. r1 = rectsubpt(r1, r1.min);
  77. draw(b, r1, w->cols[BORD], nil, ZP);
  78. draw(b, r2, w->cols[BACK], nil, ZP);
  79. r2.min.x = r2.max.x-1;
  80. draw(b, r2, w->cols[BORD], nil, ZP);
  81. draw(w->i, r, b, nil, Pt(0, r1.min.y));
  82. }
  83. }
  84. void
  85. wscrsleep(Window *w, uint dt)
  86. {
  87. Timer *timer;
  88. int y, b;
  89. static Alt alts[3];
  90. timer = timerstart(dt);
  91. y = w->mc.xy.y;
  92. b = w->mc.buttons;
  93. alts[0].c = timer->c;
  94. alts[0].v = nil;
  95. alts[0].op = CHANRCV;
  96. alts[1].c = w->mc.c;
  97. alts[1].v = &w->mc.Mouse;
  98. alts[1].op = CHANRCV;
  99. alts[2].op = CHANEND;
  100. for(;;)
  101. switch(alt(alts)){
  102. case 0:
  103. timerstop(timer);
  104. return;
  105. case 1:
  106. if(abs(w->mc.xy.y-y)>2 || w->mc.buttons!=b){
  107. timercancel(timer);
  108. return;
  109. }
  110. break;
  111. }
  112. }
  113. void
  114. wscroll(Window *w, int but)
  115. {
  116. uint p0, oldp0;
  117. Rectangle s;
  118. int x, y, my, h, first;
  119. s = insetrect(w->scrollr, 1);
  120. h = s.max.y-s.min.y;
  121. x = (s.min.x+s.max.x)/2;
  122. oldp0 = ~0;
  123. first = TRUE;
  124. do{
  125. flushimage(display, 1);
  126. if(w->mc.xy.x<s.min.x || s.max.x<=w->mc.xy.x){
  127. readmouse(&w->mc);
  128. }else{
  129. my = w->mc.xy.y;
  130. if(my < s.min.y)
  131. my = s.min.y;
  132. if(my >= s.max.y)
  133. my = s.max.y;
  134. if(!eqpt(w->mc.xy, Pt(x, my))){
  135. wmovemouse(w, Pt(x, my));
  136. readmouse(&w->mc); /* absorb event generated by moveto() */
  137. }
  138. if(but == 2){
  139. y = my;
  140. if(y > s.max.y-2)
  141. y = s.max.y-2;
  142. if(w->nr > 1024*1024)
  143. p0 = ((w->nr>>10)*(y-s.min.y)/h)<<10;
  144. else
  145. p0 = w->nr*(y-s.min.y)/h;
  146. if(oldp0 != p0)
  147. wsetorigin(w, p0, FALSE);
  148. oldp0 = p0;
  149. readmouse(&w->mc);
  150. continue;
  151. }
  152. if(but == 1)
  153. p0 = wbacknl(w, w->org, (my-s.min.y)/w->font->height);
  154. else
  155. p0 = w->org+frcharofpt(w, Pt(s.max.x, my));
  156. if(oldp0 != p0)
  157. wsetorigin(w, p0, TRUE);
  158. oldp0 = p0;
  159. /* debounce */
  160. if(first){
  161. flushimage(display, 1);
  162. sleep(200);
  163. nbrecv(w->mc.c, &w->mc.Mouse);
  164. first = FALSE;
  165. }
  166. wscrsleep(w, 100);
  167. }
  168. }while(w->mc.buttons & (1<<(but-1)));
  169. while(w->mc.buttons)
  170. readmouse(&w->mc);
  171. }