xref: /openbsd-src/usr.bin/tmux/grid-view.c (revision 89c96e90242a81edf0d9a0550930a11563268b9d)
1 /* $OpenBSD: grid-view.c,v 1.26 2016/10/13 20:27:27 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
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 
33 /* Get cell. */
34 void
35 grid_view_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
36 {
37 	grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
38 }
39 
40 /* Set cell. */
41 void
42 grid_view_set_cell(struct grid *gd, u_int px, u_int py,
43     const struct grid_cell *gc)
44 {
45 	grid_set_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
46 }
47 
48 /* Clear into history. */
49 void
50 grid_view_clear_history(struct grid *gd, u_int bg)
51 {
52 	struct grid_line	*gl;
53 	u_int			 yy, last;
54 
55 	/* Find the last used line. */
56 	last = 0;
57 	for (yy = 0; yy < gd->sy; yy++) {
58 		gl = &gd->linedata[grid_view_y(gd, yy)];
59 		if (gl->cellused != 0)
60 			last = yy + 1;
61 	}
62 	if (last == 0) {
63 		grid_view_clear(gd, 0, 0, gd->sx, gd->sy, bg);
64 		return;
65 	}
66 
67 	/* Scroll the lines into the history. */
68 	for (yy = 0; yy < last; yy++) {
69 		grid_collect_history(gd, bg);
70 		grid_scroll_history(gd, bg);
71 	}
72 	if (last < gd->sy)
73 		grid_view_clear(gd, 0, 0, gd->sx, gd->sy - last, bg);
74 	gd->hscrolled = 0;
75 }
76 
77 /* Clear area. */
78 void
79 grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny,
80     u_int bg)
81 {
82 	px = grid_view_x(gd, px);
83 	py = grid_view_y(gd, py);
84 
85 	grid_clear(gd, px, py, nx, ny, bg);
86 }
87 
88 /* Scroll region up. */
89 void
90 grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower)
91 {
92 	if (gd->flags & GRID_HISTORY) {
93 		grid_collect_history(gd, 8);
94 		if (rupper == 0 && rlower == gd->sy - 1)
95 			grid_scroll_history(gd, 8);
96 		else {
97 			rupper = grid_view_y(gd, rupper);
98 			rlower = grid_view_y(gd, rlower);
99 			grid_scroll_history_region(gd, rupper, rlower);
100 		}
101 	} else {
102 		rupper = grid_view_y(gd, rupper);
103 		rlower = grid_view_y(gd, rlower);
104 		grid_move_lines(gd, rupper, rupper + 1, rlower - rupper, 8);
105 	}
106 }
107 
108 /* Scroll region down. */
109 void
110 grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower)
111 {
112 	rupper = grid_view_y(gd, rupper);
113 	rlower = grid_view_y(gd, rlower);
114 
115 	grid_move_lines(gd, rupper + 1, rupper, rlower - rupper, 8);
116 }
117 
118 /* Insert lines. */
119 void
120 grid_view_insert_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
121 {
122 	u_int	sy;
123 
124 	py = grid_view_y(gd, py);
125 
126 	sy = grid_view_y(gd, gd->sy);
127 
128 	grid_move_lines(gd, py + ny, py, sy - py - ny, bg);
129 }
130 
131 /* Insert lines in region. */
132 void
133 grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
134     u_int ny, u_int bg)
135 {
136 	u_int	ny2;
137 
138 	rlower = grid_view_y(gd, rlower);
139 
140 	py = grid_view_y(gd, py);
141 
142 	ny2 = rlower + 1 - py - ny;
143 	grid_move_lines(gd, rlower + 1 - ny2, py, ny2, bg);
144 	grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
145 }
146 
147 /* Delete lines. */
148 void
149 grid_view_delete_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
150 {
151 	u_int	sy;
152 
153 	py = grid_view_y(gd, py);
154 
155 	sy = grid_view_y(gd, gd->sy);
156 
157 	grid_move_lines(gd, py, py + ny, sy - py - ny, bg);
158 	grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny), bg);
159 }
160 
161 /* Delete lines inside scroll region. */
162 void
163 grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
164     u_int ny, u_int bg)
165 {
166 	u_int	ny2;
167 
168 	rlower = grid_view_y(gd, rlower);
169 
170 	py = grid_view_y(gd, py);
171 
172 	ny2 = rlower + 1 - py - ny;
173 	grid_move_lines(gd, py, py + ny, ny2, bg);
174 	grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
175 }
176 
177 /* Insert characters. */
178 void
179 grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
180 {
181 	u_int	sx;
182 
183 	px = grid_view_x(gd, px);
184 	py = grid_view_y(gd, py);
185 
186 	sx = grid_view_x(gd, gd->sx);
187 
188 	if (px == sx - 1)
189 		grid_clear(gd, px, py, 1, 1, bg);
190 	else
191 		grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
192 }
193 
194 /* Delete characters. */
195 void
196 grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
197 {
198 	u_int	sx;
199 
200 	px = grid_view_x(gd, px);
201 	py = grid_view_y(gd, py);
202 
203 	sx = grid_view_x(gd, gd->sx);
204 
205 	grid_move_cells(gd, px, px + nx, py, sx - px - nx, bg);
206 	grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1, bg);
207 }
208 
209 /* Convert cells into a string. */
210 char *
211 grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
212 {
213 	px = grid_view_x(gd, px);
214 	py = grid_view_y(gd, py);
215 
216 	return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
217 }
218