1 /* Id */
2
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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 for reading. */
34 const struct grid_cell *
grid_view_peek_cell(struct grid * gd,u_int px,u_int py)35 grid_view_peek_cell(struct grid *gd, u_int px, u_int py)
36 {
37 return (grid_peek_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py)));
38 }
39
40 /* Get cell for writing. */
41 struct grid_cell *
grid_view_get_cell(struct grid * gd,u_int px,u_int py)42 grid_view_get_cell(struct grid *gd, u_int px, u_int py)
43 {
44 return (grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py)));
45 }
46
47 /* Set cell. */
48 void
grid_view_set_cell(struct grid * gd,u_int px,u_int py,const struct grid_cell * gc)49 grid_view_set_cell(
50 struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
51 {
52 grid_set_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
53 }
54
55 /* Clear into history. */
56 void
grid_view_clear_history(struct grid * gd)57 grid_view_clear_history(struct grid *gd)
58 {
59 struct grid_line *gl;
60 u_int yy, last;
61
62 GRID_DEBUG(gd, "");
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)];
68 if (gl->cellsize != 0)
69 last = yy + 1;
70 }
71 if (last == 0)
72 return;
73
74 /* Scroll the lines into the history. */
75 for (yy = 0; yy < last; yy++) {
76 grid_collect_history(gd);
77 grid_scroll_history(gd);
78 }
79 }
80
81 /* Clear area. */
82 void
grid_view_clear(struct grid * gd,u_int px,u_int py,u_int nx,u_int ny)83 grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
84 {
85 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u, ny=%u", px, py, nx, ny);
86
87 px = grid_view_x(gd, px);
88 py = grid_view_y(gd, py);
89
90 grid_clear(gd, px, py, nx, ny);
91 }
92
93 /* Scroll region up. */
94 void
grid_view_scroll_region_up(struct grid * gd,u_int rupper,u_int rlower)95 grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower)
96 {
97 GRID_DEBUG(gd, "rupper=%u, rlower=%u", rupper, rlower);
98
99 if (gd->flags & GRID_HISTORY) {
100 grid_collect_history(gd);
101 if (rupper == 0 && rlower == gd->sy - 1)
102 grid_scroll_history(gd);
103 else {
104 rupper = grid_view_y(gd, rupper);
105 rlower = grid_view_y(gd, rlower);
106 grid_scroll_history_region(gd, rupper, rlower);
107 }
108 } else {
109 rupper = grid_view_y(gd, rupper);
110 rlower = grid_view_y(gd, rlower);
111 grid_move_lines(gd, rupper, rupper + 1, rlower - rupper);
112 }
113 }
114
115 /* Scroll region down. */
116 void
grid_view_scroll_region_down(struct grid * gd,u_int rupper,u_int rlower)117 grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower)
118 {
119 GRID_DEBUG(gd, "rupper=%u, rlower=%u", rupper, rlower);
120
121 rupper = grid_view_y(gd, rupper);
122 rlower = grid_view_y(gd, rlower);
123
124 grid_move_lines(gd, rupper + 1, rupper, rlower - rupper);
125 }
126
127 /* Insert lines. */
128 void
grid_view_insert_lines(struct grid * gd,u_int py,u_int ny)129 grid_view_insert_lines(struct grid *gd, u_int py, u_int ny)
130 {
131 u_int sy;
132
133 GRID_DEBUG(gd, "py=%u, ny=%u", py, ny);
134
135 py = grid_view_y(gd, py);
136
137 sy = grid_view_y(gd, gd->sy);
138
139 grid_move_lines(gd, py + ny, py, sy - py - ny);
140 }
141
142 /* Insert lines in region. */
143 void
grid_view_insert_lines_region(struct grid * gd,u_int rlower,u_int py,u_int ny)144 grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py, u_int ny)
145 {
146 u_int ny2;
147
148 GRID_DEBUG(gd, "rlower=%u, py=%u, ny=%u", rlower, py, ny);
149
150 rlower = grid_view_y(gd, rlower);
151
152 py = grid_view_y(gd, py);
153
154 ny2 = rlower + 1 - py - ny;
155 grid_move_lines(gd, rlower + 1 - ny2, py, ny2);
156 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2);
157 }
158
159 /* Delete lines. */
160 void
grid_view_delete_lines(struct grid * gd,u_int py,u_int ny)161 grid_view_delete_lines(struct grid *gd, u_int py, u_int ny)
162 {
163 u_int sy;
164
165 GRID_DEBUG(gd, "py=%u, ny=%u", py, ny);
166
167 py = grid_view_y(gd, py);
168
169 sy = grid_view_y(gd, gd->sy);
170
171 grid_move_lines(gd, py, py + ny, sy - py - ny);
172 grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny));
173 }
174
175 /* Delete lines inside scroll region. */
176 void
grid_view_delete_lines_region(struct grid * gd,u_int rlower,u_int py,u_int ny)177 grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py, u_int ny)
178 {
179 u_int ny2;
180
181 GRID_DEBUG(gd, "rlower=%u, py=%u, ny=%u", rlower, py, ny);
182
183 rlower = grid_view_y(gd, rlower);
184
185 py = grid_view_y(gd, py);
186
187 ny2 = rlower + 1 - py - ny;
188 grid_move_lines(gd, py, py + ny, ny2);
189 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2);
190 }
191
192 /* Insert characters. */
193 void
grid_view_insert_cells(struct grid * gd,u_int px,u_int py,u_int nx)194 grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx)
195 {
196 u_int sx;
197
198 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
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 if (px == sx - 1)
206 grid_clear(gd, px, py, 1, 1);
207 else
208 grid_move_cells(gd, px + nx, px, py, sx - px - nx);
209 }
210
211 /* Delete characters. */
212 void
grid_view_delete_cells(struct grid * gd,u_int px,u_int py,u_int nx)213 grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx)
214 {
215 u_int sx;
216
217 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
218
219 px = grid_view_x(gd, px);
220 py = grid_view_y(gd, py);
221
222 sx = grid_view_x(gd, gd->sx);
223
224 grid_move_cells(gd, px, px + nx, py, sx - px - nx);
225 grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1);
226 }
227
228 /* Convert cells into a string. */
229 char *
grid_view_string_cells(struct grid * gd,u_int px,u_int py,u_int nx)230 grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
231 {
232 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
233
234 px = grid_view_x(gd, px);
235 py = grid_view_y(gd, py);
236
237 return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
238 }
239