1/* $OpenBSD$ */
2
3/*
4 * Copyright (c) 2008 Nicholas Marriott <[email protected]>
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. */
34void
35grid_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. */
41void
42grid_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/* Set cells. */
49void
50grid_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);
55}
56
57/* Clear into history. */
58void
59grid_view_clear_history(struct grid *gd, u_int bg)
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 = grid_get_line(gd, grid_view_y(gd, yy));
68 if (gl->cellused != 0)
69 last = yy + 1;
70 }
71 if (last == 0) {
72 grid_view_clear(gd, 0, 0, gd->sx, gd->sy, bg);
73 return;
74 }
75
76 /* Scroll the lines into the history. */
77 for (yy = 0; yy < last; yy++) {
78 grid_collect_history(gd);
79 grid_scroll_history(gd, bg);
80 }
81 if (last < gd->sy)
82 grid_view_clear(gd, 0, 0, gd->sx, gd->sy - last, bg);
83 gd->hscrolled = 0;
84}
85
86/* Clear area. */
87void
88grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny,
89 u_int bg)
90{
91 px = grid_view_x(gd, px);
92 py = grid_view_y(gd, py);
93
94 grid_clear(gd, px, py, nx, ny, bg);
95}
96
97/* Scroll region up. */
98void
99grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower,
100 u_int bg)
101{
102 if (gd->flags & GRID_HISTORY) {
103 grid_collect_history(gd);
104 if (rupper == 0 && rlower == gd->sy - 1)
105 grid_scroll_history(gd, bg);
106 else {
107 rupper = grid_view_y(gd, rupper);
108 rlower = grid_view_y(gd, rlower);
109 grid_scroll_history_region(gd, rupper, rlower, bg);
110 }
111 } else {
112 rupper = grid_view_y(gd, rupper);
113 rlower = grid_view_y(gd, rlower);
114 grid_move_lines(gd, rupper, rupper + 1, rlower - rupper, bg);
115 }
116}
117
118/* Scroll region down. */
119void
120grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower,
121 u_int bg)
122{
123 rupper = grid_view_y(gd, rupper);
124 rlower = grid_view_y(gd, rlower);
125
126 grid_move_lines(gd, rupper + 1, rupper, rlower - rupper, bg);
127}
128
129/* Insert lines. */
130void
131grid_view_insert_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
132{
133 u_int sy;
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, bg);
140}
141
142/* Insert lines in region. */
143void
144grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
145 u_int ny, u_int bg)
146{
147 u_int ny2;
148
149 rlower = grid_view_y(gd, rlower);
150
151 py = grid_view_y(gd, py);
152
153 ny2 = rlower + 1 - py - ny;
154 grid_move_lines(gd, rlower + 1 - ny2, py, ny2, bg);
155 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
156}
157
158/* Delete lines. */
159void
160grid_view_delete_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
161{
162 u_int sy;
163
164 py = grid_view_y(gd, py);
165
166 sy = grid_view_y(gd, gd->sy);
167
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);
170}
171
172/* Delete lines inside scroll region. */
173void
174grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
175 u_int ny, u_int bg)
176{
177 u_int ny2;
178
179 rlower = grid_view_y(gd, rlower);
180
181 py = grid_view_y(gd, py);
182
183 ny2 = rlower + 1 - py - ny;
184 grid_move_lines(gd, py, py + ny, ny2, bg);
185 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
186}
187
188/* Insert characters. */
189void
190grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
191{
192 u_int sx;
193
194 px = grid_view_x(gd, px);
195 py = grid_view_y(gd, py);
196
197 sx = grid_view_x(gd, gd->sx);
198
199 if (px >= sx - 1)
200 grid_clear(gd, px, py, 1, 1, bg);
201 else
202 grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
203}
204
205/* Delete characters. */
206void
207grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
208{
209 u_int sx;
210
211 px = grid_view_x(gd, px);
212 py = grid_view_y(gd, py);
213
214 sx = grid_view_x(gd, gd->sx);
215
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);
218}
219
220/* Convert cells into a string. */
221char *
222grid_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
227 return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
228}
229