Annotation of src/usr.bin/tmux/grid-view.c, Revision 1.30
1.30 ! nicm 1: /* $OpenBSD: grid-view.c,v 1.29 2017/05/12 13:00:56 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.27 nicm 46: }
47:
48: /* Set cells. */
49: void
50: grid_view_set_cells(struct grid *gd, u_int px, u_int py,
51: const struct grid_cell *gc, const char *s, size_t slen)
52: {
53: grid_set_cells(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc, s,
54: slen);
1.1 nicm 55: }
56:
1.9 nicm 57: /* Clear into history. */
58: void
1.26 nicm 59: grid_view_clear_history(struct grid *gd, u_int bg)
1.9 nicm 60: {
61: struct grid_line *gl;
62: u_int yy, last;
63:
64: /* Find the last used line. */
65: last = 0;
66: for (yy = 0; yy < gd->sy; yy++) {
67: gl = &gd->linedata[grid_view_y(gd, yy)];
1.26 nicm 68: if (gl->cellused != 0)
1.9 nicm 69: last = yy + 1;
70: }
1.26 nicm 71: if (last == 0) {
72: grid_view_clear(gd, 0, 0, gd->sx, gd->sy, bg);
1.9 nicm 73: return;
1.26 nicm 74: }
1.9 nicm 75:
76: /* Scroll the lines into the history. */
1.10 nicm 77: for (yy = 0; yy < last; yy++) {
1.30 ! nicm 78: grid_collect_history(gd);
1.26 nicm 79: grid_scroll_history(gd, bg);
1.10 nicm 80: }
1.26 nicm 81: if (last < gd->sy)
82: grid_view_clear(gd, 0, 0, gd->sx, gd->sy - last, bg);
1.25 nicm 83: gd->hscrolled = 0;
1.1 nicm 84: }
85:
86: /* Clear area. */
87: void
1.26 nicm 88: grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny,
89: u_int bg)
1.1 nicm 90: {
91: px = grid_view_x(gd, px);
92: py = grid_view_y(gd, py);
93:
1.26 nicm 94: grid_clear(gd, px, py, nx, ny, bg);
1.1 nicm 95: }
96:
97: /* Scroll region up. */
98: void
1.29 nicm 99: grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower,
100: u_int bg)
1.1 nicm 101: {
1.7 nicm 102: if (gd->flags & GRID_HISTORY) {
1.30 ! nicm 103: grid_collect_history(gd);
1.7 nicm 104: if (rupper == 0 && rlower == gd->sy - 1)
1.29 nicm 105: grid_scroll_history(gd, bg);
1.7 nicm 106: else {
107: rupper = grid_view_y(gd, rupper);
108: rlower = grid_view_y(gd, rlower);
1.29 nicm 109: grid_scroll_history_region(gd, rupper, rlower, bg);
1.7 nicm 110: }
111: } else {
112: rupper = grid_view_y(gd, rupper);
113: rlower = grid_view_y(gd, rlower);
1.29 nicm 114: grid_move_lines(gd, rupper, rupper + 1, rlower - rupper, bg);
1.1 nicm 115: }
116: }
117:
118: /* Scroll region down. */
119: void
1.29 nicm 120: grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower,
121: u_int bg)
1.1 nicm 122: {
123: rupper = grid_view_y(gd, rupper);
124: rlower = grid_view_y(gd, rlower);
125:
1.29 nicm 126: grid_move_lines(gd, rupper + 1, rupper, rlower - rupper, bg);
1.1 nicm 127: }
128:
129: /* Insert lines. */
130: void
1.26 nicm 131: grid_view_insert_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 132: {
133: u_int sy;
134:
135: py = grid_view_y(gd, py);
136:
137: sy = grid_view_y(gd, gd->sy);
138:
1.26 nicm 139: grid_move_lines(gd, py + ny, py, sy - py - ny, bg);
1.1 nicm 140: }
141:
142: /* Insert lines in region. */
143: void
1.16 nicm 144: grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
1.26 nicm 145: u_int ny, u_int bg)
1.1 nicm 146: {
1.5 nicm 147: u_int ny2;
148:
1.1 nicm 149: rlower = grid_view_y(gd, rlower);
150:
151: py = grid_view_y(gd, py);
152:
1.5 nicm 153: ny2 = rlower + 1 - py - ny;
1.26 nicm 154: grid_move_lines(gd, rlower + 1 - ny2, py, ny2, bg);
155: grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
1.1 nicm 156: }
157:
158: /* Delete lines. */
159: void
1.26 nicm 160: grid_view_delete_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 161: {
162: u_int sy;
163:
164: py = grid_view_y(gd, py);
165:
166: sy = grid_view_y(gd, gd->sy);
167:
1.26 nicm 168: grid_move_lines(gd, py, py + ny, sy - py - ny, bg);
169: grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny), bg);
1.1 nicm 170: }
171:
172: /* Delete lines inside scroll region. */
173: void
1.16 nicm 174: grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
1.26 nicm 175: u_int ny, u_int bg)
1.1 nicm 176: {
1.5 nicm 177: u_int ny2;
178:
1.1 nicm 179: rlower = grid_view_y(gd, rlower);
180:
181: py = grid_view_y(gd, py);
182:
1.5 nicm 183: ny2 = rlower + 1 - py - ny;
1.26 nicm 184: grid_move_lines(gd, py, py + ny, ny2, bg);
185: grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
1.1 nicm 186: }
187:
188: /* Insert characters. */
189: void
1.26 nicm 190: grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
1.1 nicm 191: {
192: u_int sx;
193:
194: px = grid_view_x(gd, px);
195: py = grid_view_y(gd, py);
196:
1.21 nicm 197: sx = grid_view_x(gd, gd->sx);
1.1 nicm 198:
1.28 nicm 199: if (px >= sx - 1)
1.26 nicm 200: grid_clear(gd, px, py, 1, 1, bg);
1.1 nicm 201: else
1.26 nicm 202: grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
1.1 nicm 203: }
204:
205: /* Delete characters. */
206: void
1.26 nicm 207: grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
1.1 nicm 208: {
209: u_int sx;
210:
211: px = grid_view_x(gd, px);
212: py = grid_view_y(gd, py);
213:
1.21 nicm 214: sx = grid_view_x(gd, gd->sx);
1.1 nicm 215:
1.26 nicm 216: grid_move_cells(gd, px, px + nx, py, sx - px - nx, bg);
217: grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1, bg);
1.2 nicm 218: }
219:
220: /* Convert cells into a string. */
221: char *
222: grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
223: {
224: px = grid_view_x(gd, px);
225: py = grid_view_y(gd, py);
226:
1.14 nicm 227: return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
1.1 nicm 228: }