scrl.c 3.7 KB

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