grid-view.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <string.h>
  19. #include "tmux.h"
  20. /*
  21. * Grid view functions. These work using coordinates relative to the visible
  22. * screen area.
  23. */
  24. #define grid_view_x(gd, x) (x)
  25. #define grid_view_y(gd, y) ((gd)->hsize + (y))
  26. /* Get cell. */
  27. void
  28. grid_view_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
  29. {
  30. grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
  31. }
  32. /* Set cell. */
  33. void
  34. grid_view_set_cell(struct grid *gd, u_int px, u_int py,
  35. const struct grid_cell *gc)
  36. {
  37. grid_set_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
  38. }
  39. /* Clear into history. */
  40. void
  41. grid_view_clear_history(struct grid *gd)
  42. {
  43. struct grid_line *gl;
  44. u_int yy, last;
  45. /* Find the last used line. */
  46. last = 0;
  47. for (yy = 0; yy < gd->sy; yy++) {
  48. gl = &gd->linedata[grid_view_y(gd, yy)];
  49. if (gl->cellsize != 0)
  50. last = yy + 1;
  51. }
  52. if (last == 0)
  53. return;
  54. /* Scroll the lines into the history. */
  55. for (yy = 0; yy < last; yy++) {
  56. grid_collect_history(gd);
  57. grid_scroll_history(gd);
  58. }
  59. }
  60. /* Clear area. */
  61. void
  62. grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
  63. {
  64. px = grid_view_x(gd, px);
  65. py = grid_view_y(gd, py);
  66. grid_clear(gd, px, py, nx, ny);
  67. }
  68. /* Scroll region up. */
  69. void
  70. grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower)
  71. {
  72. if (gd->flags & GRID_HISTORY) {
  73. grid_collect_history(gd);
  74. if (rupper == 0 && rlower == gd->sy - 1)
  75. grid_scroll_history(gd);
  76. else {
  77. rupper = grid_view_y(gd, rupper);
  78. rlower = grid_view_y(gd, rlower);
  79. grid_scroll_history_region(gd, rupper, rlower);
  80. }
  81. } else {
  82. rupper = grid_view_y(gd, rupper);
  83. rlower = grid_view_y(gd, rlower);
  84. grid_move_lines(gd, rupper, rupper + 1, rlower - rupper);
  85. }
  86. }
  87. /* Scroll region down. */
  88. void
  89. grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower)
  90. {
  91. rupper = grid_view_y(gd, rupper);
  92. rlower = grid_view_y(gd, rlower);
  93. grid_move_lines(gd, rupper + 1, rupper, rlower - rupper);
  94. }
  95. /* Insert lines. */
  96. void
  97. grid_view_insert_lines(struct grid *gd, u_int py, u_int ny)
  98. {
  99. u_int sy;
  100. py = grid_view_y(gd, py);
  101. sy = grid_view_y(gd, gd->sy);
  102. grid_move_lines(gd, py + ny, py, sy - py - ny);
  103. }
  104. /* Insert lines in region. */
  105. void
  106. grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
  107. u_int ny)
  108. {
  109. u_int ny2;
  110. rlower = grid_view_y(gd, rlower);
  111. py = grid_view_y(gd, py);
  112. ny2 = rlower + 1 - py - ny;
  113. grid_move_lines(gd, rlower + 1 - ny2, py, ny2);
  114. grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2);
  115. }
  116. /* Delete lines. */
  117. void
  118. grid_view_delete_lines(struct grid *gd, u_int py, u_int ny)
  119. {
  120. u_int sy;
  121. py = grid_view_y(gd, py);
  122. sy = grid_view_y(gd, gd->sy);
  123. grid_move_lines(gd, py, py + ny, sy - py - ny);
  124. grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny));
  125. }
  126. /* Delete lines inside scroll region. */
  127. void
  128. grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
  129. u_int ny)
  130. {
  131. u_int ny2;
  132. rlower = grid_view_y(gd, rlower);
  133. py = grid_view_y(gd, py);
  134. ny2 = rlower + 1 - py - ny;
  135. grid_move_lines(gd, py, py + ny, ny2);
  136. grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2);
  137. }
  138. /* Insert characters. */
  139. void
  140. grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx)
  141. {
  142. u_int sx;
  143. px = grid_view_x(gd, px);
  144. py = grid_view_y(gd, py);
  145. sx = grid_view_x(gd, gd->sx);
  146. if (px == sx - 1)
  147. grid_clear(gd, px, py, 1, 1);
  148. else
  149. grid_move_cells(gd, px + nx, px, py, sx - px - nx);
  150. }
  151. /* Delete characters. */
  152. void
  153. grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx)
  154. {
  155. u_int sx;
  156. px = grid_view_x(gd, px);
  157. py = grid_view_y(gd, py);
  158. sx = grid_view_x(gd, gd->sx);
  159. grid_move_cells(gd, px, px + nx, py, sx - px - nx);
  160. grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1);
  161. }
  162. /* Convert cells into a string. */
  163. char *
  164. grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
  165. {
  166. px = grid_view_x(gd, px);
  167. py = grid_view_y(gd, py);
  168. return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
  169. }