Annotation of src/usr.bin/tmux/grid-view.c, Revision 1.34
1.34 ! nicm 1: /* $OpenBSD: grid-view.c,v 1.33 2019/08/16 08:52:25 nicm Exp $ */
1.1 nicm 2:
3: /*
1.24 nicm 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <string.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * Grid view functions. These work using coordinates relative to the visible
27: * screen area.
28: */
29:
30: #define grid_view_x(gd, x) (x)
31: #define grid_view_y(gd, y) ((gd)->hsize + (y))
32:
1.23 nicm 33: /* Get cell. */
1.22 nicm 34: void
35: grid_view_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
1.1 nicm 36: {
1.22 nicm 37: grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
1.1 nicm 38: }
39:
40: /* Set cell. */
41: void
1.22 nicm 42: grid_view_set_cell(struct grid *gd, u_int px, u_int py,
43: const struct grid_cell *gc)
1.1 nicm 44: {
45: grid_set_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
1.34 ! nicm 46: }
! 47:
! 48: /* Set padding. */
! 49: void
! 50: grid_view_set_padding(struct grid *gd, u_int px, u_int py)
! 51: {
! 52: grid_set_padding(gd, grid_view_x(gd, px), grid_view_y(gd, py));
1.27 nicm 53: }
54:
55: /* Set cells. */
56: void
57: grid_view_set_cells(struct grid *gd, u_int px, u_int py,
58: const struct grid_cell *gc, const char *s, size_t slen)
59: {
60: grid_set_cells(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc, s,
61: slen);
1.1 nicm 62: }
63:
1.9 nicm 64: /* Clear into history. */
65: void
1.26 nicm 66: grid_view_clear_history(struct grid *gd, u_int bg)
1.9 nicm 67: {
68: struct grid_line *gl;
69: u_int yy, last;
70:
71: /* Find the last used line. */
72: last = 0;
73: for (yy = 0; yy < gd->sy; yy++) {
1.31 nicm 74: gl = grid_get_line(gd, grid_view_y(gd, yy));
1.26 nicm 75: if (gl->cellused != 0)
1.9 nicm 76: last = yy + 1;
77: }
1.26 nicm 78: if (last == 0) {
79: grid_view_clear(gd, 0, 0, gd->sx, gd->sy, bg);
1.9 nicm 80: return;
1.26 nicm 81: }
1.9 nicm 82:
83: /* Scroll the lines into the history. */
1.10 nicm 84: for (yy = 0; yy < last; yy++) {
1.30 nicm 85: grid_collect_history(gd);
1.26 nicm 86: grid_scroll_history(gd, bg);
1.10 nicm 87: }
1.26 nicm 88: if (last < gd->sy)
89: grid_view_clear(gd, 0, 0, gd->sx, gd->sy - last, bg);
1.25 nicm 90: gd->hscrolled = 0;
1.1 nicm 91: }
92:
93: /* Clear area. */
94: void
1.26 nicm 95: grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny,
96: u_int bg)
1.1 nicm 97: {
98: px = grid_view_x(gd, px);
99: py = grid_view_y(gd, py);
100:
1.26 nicm 101: grid_clear(gd, px, py, nx, ny, bg);
1.1 nicm 102: }
103:
104: /* Scroll region up. */
105: void
1.29 nicm 106: grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower,
107: u_int bg)
1.1 nicm 108: {
1.7 nicm 109: if (gd->flags & GRID_HISTORY) {
1.30 nicm 110: grid_collect_history(gd);
1.7 nicm 111: if (rupper == 0 && rlower == gd->sy - 1)
1.29 nicm 112: grid_scroll_history(gd, bg);
1.7 nicm 113: else {
114: rupper = grid_view_y(gd, rupper);
115: rlower = grid_view_y(gd, rlower);
1.29 nicm 116: grid_scroll_history_region(gd, rupper, rlower, bg);
1.7 nicm 117: }
118: } else {
119: rupper = grid_view_y(gd, rupper);
120: rlower = grid_view_y(gd, rlower);
1.29 nicm 121: grid_move_lines(gd, rupper, rupper + 1, rlower - rupper, bg);
1.1 nicm 122: }
123: }
124:
125: /* Scroll region down. */
126: void
1.29 nicm 127: grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower,
128: u_int bg)
1.1 nicm 129: {
130: rupper = grid_view_y(gd, rupper);
131: rlower = grid_view_y(gd, rlower);
132:
1.29 nicm 133: grid_move_lines(gd, rupper + 1, rupper, rlower - rupper, bg);
1.1 nicm 134: }
135:
136: /* Insert lines. */
137: void
1.26 nicm 138: grid_view_insert_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 139: {
140: u_int sy;
141:
142: py = grid_view_y(gd, py);
143:
144: sy = grid_view_y(gd, gd->sy);
145:
1.26 nicm 146: grid_move_lines(gd, py + ny, py, sy - py - ny, bg);
1.1 nicm 147: }
148:
149: /* Insert lines in region. */
150: void
1.16 nicm 151: grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
1.26 nicm 152: u_int ny, u_int bg)
1.1 nicm 153: {
1.5 nicm 154: u_int ny2;
155:
1.1 nicm 156: rlower = grid_view_y(gd, rlower);
157:
158: py = grid_view_y(gd, py);
159:
1.5 nicm 160: ny2 = rlower + 1 - py - ny;
1.26 nicm 161: grid_move_lines(gd, rlower + 1 - ny2, py, ny2, bg);
162: grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
1.1 nicm 163: }
164:
165: /* Delete lines. */
166: void
1.26 nicm 167: grid_view_delete_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 168: {
169: u_int sy;
170:
171: py = grid_view_y(gd, py);
172:
173: sy = grid_view_y(gd, gd->sy);
174:
1.26 nicm 175: grid_move_lines(gd, py, py + ny, sy - py - ny, bg);
176: grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny), bg);
1.1 nicm 177: }
178:
179: /* Delete lines inside scroll region. */
180: void
1.16 nicm 181: grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
1.26 nicm 182: u_int ny, u_int bg)
1.1 nicm 183: {
1.5 nicm 184: u_int ny2;
185:
1.1 nicm 186: rlower = grid_view_y(gd, rlower);
187:
188: py = grid_view_y(gd, py);
189:
1.5 nicm 190: ny2 = rlower + 1 - py - ny;
1.26 nicm 191: grid_move_lines(gd, py, py + ny, ny2, bg);
192: grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
1.1 nicm 193: }
194:
195: /* Insert characters. */
196: void
1.26 nicm 197: grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
1.1 nicm 198: {
199: u_int sx;
200:
201: px = grid_view_x(gd, px);
202: py = grid_view_y(gd, py);
203:
1.21 nicm 204: sx = grid_view_x(gd, gd->sx);
1.1 nicm 205:
1.28 nicm 206: if (px >= sx - 1)
1.26 nicm 207: grid_clear(gd, px, py, 1, 1, bg);
1.1 nicm 208: else
1.26 nicm 209: grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
1.1 nicm 210: }
211:
212: /* Delete characters. */
213: void
1.26 nicm 214: grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
1.1 nicm 215: {
216: u_int sx;
217:
218: px = grid_view_x(gd, px);
219: py = grid_view_y(gd, py);
220:
1.21 nicm 221: sx = grid_view_x(gd, gd->sx);
1.1 nicm 222:
1.26 nicm 223: grid_move_cells(gd, px, px + nx, py, sx - px - nx, bg);
1.33 nicm 224: grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1, bg);
1.2 nicm 225: }
226:
227: /* Convert cells into a string. */
228: char *
229: grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
230: {
231: px = grid_view_x(gd, px);
232: py = grid_view_y(gd, py);
233:
1.14 nicm 234: return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
1.1 nicm 235: }