scrl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <plumb.h>
  19. #include "dat.h"
  20. #include "fns.h"
  21. static Image *scrtmp;
  22. static
  23. Rectangle
  24. scrpos(Rectangle r, uint p0, uint p1, uint tot)
  25. {
  26. Rectangle q;
  27. int h;
  28. q = r;
  29. h = q.max.y-q.min.y;
  30. if(tot == 0)
  31. return q;
  32. if(tot > 1024*1024){
  33. tot>>=10;
  34. p0>>=10;
  35. p1>>=10;
  36. }
  37. if(p0 > 0)
  38. q.min.y += h*p0/tot;
  39. if(p1 < tot)
  40. q.max.y -= h*(tot-p1)/tot;
  41. if(q.max.y < q.min.y+2){
  42. if(q.min.y+2 <= r.max.y)
  43. q.max.y = q.min.y+2;
  44. else
  45. q.min.y = q.max.y-2;
  46. }
  47. return q;
  48. }
  49. void
  50. scrlresize(void)
  51. {
  52. freeimage(scrtmp);
  53. scrtmp = allocimage(display, Rect(0, 0, 32, screen->r.max.y), screen->chan, 0, DNofill);
  54. if(scrtmp == nil)
  55. error("scroll alloc");
  56. }
  57. void
  58. textscrdraw(Text *t)
  59. {
  60. Rectangle r, r1, r2;
  61. Image *b;
  62. if(t->w==nil || t!=&t->w->body)
  63. return;
  64. if(scrtmp == nil)
  65. scrlresize();
  66. r = t->scrollr;
  67. b = scrtmp;
  68. r1 = r;
  69. r1.min.x = 0;
  70. r1.max.x = Dx(r);
  71. r2 = scrpos(r1, t->org, t->org+t->Frame.nchars, t->file->Buffer.nc);
  72. if(!eqrect(r2, t->lastsr)){
  73. t->lastsr = r2;
  74. draw(b, r1, t->Frame.cols[BORD], nil, ZP);
  75. draw(b, r2, t->Frame.cols[BACK], nil, ZP);
  76. r2.min.x = r2.max.x-1;
  77. draw(b, r2, t->Frame.cols[BORD], nil, ZP);
  78. draw(t->Frame.b, r, b, nil, Pt(0, r1.min.y));
  79. /*flushimage(display, 1);/ *BUG?*/
  80. }
  81. }
  82. void
  83. scrsleep(uint dt)
  84. {
  85. Timer *timer;
  86. static Alt alts[3];
  87. timer = timerstart(dt);
  88. alts[0].c = timer->c;
  89. alts[0].v = nil;
  90. alts[0].op = CHANRCV;
  91. alts[1].c = mousectl->c;
  92. alts[1].v = (Mouse *)mousectl;
  93. alts[1].op = CHANRCV;
  94. alts[2].op = CHANEND;
  95. for(;;)
  96. switch(alt(alts)){
  97. case 0:
  98. timerstop(timer);
  99. return;
  100. case 1:
  101. timercancel(timer);
  102. return;
  103. }
  104. }
  105. void
  106. textscroll(Text *t, int but)
  107. {
  108. uint p0, oldp0;
  109. Rectangle s;
  110. int x, y, my, h, first;
  111. s = insetrect(t->scrollr, 1);
  112. h = s.max.y-s.min.y;
  113. x = (s.min.x+s.max.x)/2;
  114. oldp0 = ~0;
  115. first = TRUE;
  116. do{
  117. flushimage(display, 1);
  118. my = mouse->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(mouse->xy, Pt(x, my))){
  124. moveto(mousectl, Pt(x, my));
  125. readmouse(mousectl); /* absorb event generated by moveto() */
  126. }
  127. if(but == 2){
  128. y = my;
  129. p0 = (int64_t)t->file->Buffer.nc*(y-s.min.y)/h;
  130. if(p0 >= t->q1)
  131. p0 = textbacknl(t, p0, 2);
  132. if(oldp0 != p0)
  133. textsetorigin(t, p0, FALSE);
  134. oldp0 = p0;
  135. readmouse(mousectl);
  136. continue;
  137. }
  138. if(but == 1)
  139. p0 = textbacknl(t, t->org, (my-s.min.y)/t->Frame.font->height);
  140. else
  141. p0 = t->org+frcharofpt(&t->Frame, Pt(s.max.x, my));
  142. if(oldp0 != p0)
  143. textsetorigin(t, p0, TRUE);
  144. oldp0 = p0;
  145. /* debounce */
  146. if(first){
  147. flushimage(display, 1);
  148. sleep(200);
  149. nbrecv(mousectl->c, (Mouse *)mousectl);
  150. first = FALSE;
  151. }
  152. scrsleep(80);
  153. }while(mouse->buttons & (1<<(but-1)));
  154. while(mouse->buttons)
  155. readmouse(mousectl);
  156. }