scrl.c 3.6 KB

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