1 /* $OpenBSD$ */
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
grid_view_get_cell(struct grid * gd,u_int px,u_int py,struct grid_cell * gc)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
grid_view_set_cell(struct grid * gd,u_int px,u_int py,const struct grid_cell * gc)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
grid_view_clear_history(struct grid * gd)50 grid_view_clear_history(struct grid *gd)
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->cellsize != 0)
60 last = yy + 1;
61 }
62 if (last == 0)
63 return;
64
65 /* Scroll the lines into the history. */
66 for (yy = 0; yy < last; yy++) {
67 grid_collect_history(gd);
68 grid_scroll_history(gd);
69 }
70 }
71
72 /* Clear area. */
73 void
grid_view_clear(struct grid * gd,u_int px,u_int py,u_int nx,u_int ny)74 grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
75 {
76 px = grid_view_x(gd, px);
77 py = grid_view_y(gd, py);
78
79 grid_clear(gd, px, py, nx, ny);
80 }
81
82 /* Scroll region up. */
83 void
grid_view_scroll_region_up(struct grid * gd,u_int rupper,u_int rlower)84 grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower)
85 {
86 if (gd->flags & GRID_HISTORY) {
87 grid_collect_history(gd);
88 if (rupper == 0 && rlower == gd->sy - 1)
89 grid_scroll_history(gd);
90 else {
91 rupper = grid_view_y(gd, rupper);
92 rlower = grid_view_y(gd, rlower);
93 grid_scroll_history_region(gd, rupper, rlower);
94 }
95 } else {
96 rupper = grid_view_y(gd, rupper);
97 rlower = grid_view_y(gd, rlower);
98 grid_move_lines(gd, rupper, rupper + 1, rlower - rupper);
99 }
100 }
101
102 /* Scroll region down. */
103 void
grid_view_scroll_region_down(struct grid * gd,u_int rupper,u_int rlower)104 grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower)
105 {
106 rupper = grid_view_y(gd, rupper);
107 rlower = grid_view_y(gd, rlower);
108
109 grid_move_lines(gd, rupper + 1, rupper, rlower - rupper);
110 }
111
112 /* Insert lines. */
113 void
grid_view_insert_lines(struct grid * gd,u_int py,u_int ny)114 grid_view_insert_lines(struct grid *gd, u_int py, u_int ny)
115 {
116 u_int sy;
117
118 py = grid_view_y(gd, py);
119
120 sy = grid_view_y(gd, gd->sy);
121
122 grid_move_lines(gd, py + ny, py, sy - py - ny);
123 }
124
125 /* Insert lines in region. */
126 void
grid_view_insert_lines_region(struct grid * gd,u_int rlower,u_int py,u_int ny)127 grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
128 u_int ny)
129 {
130 u_int ny2;
131
132 rlower = grid_view_y(gd, rlower);
133
134 py = grid_view_y(gd, py);
135
136 ny2 = rlower + 1 - py - ny;
137 grid_move_lines(gd, rlower + 1 - ny2, py, ny2);
138 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2);
139 }
140
141 /* Delete lines. */
142 void
grid_view_delete_lines(struct grid * gd,u_int py,u_int ny)143 grid_view_delete_lines(struct grid *gd, u_int py, u_int ny)
144 {
145 u_int sy;
146
147 py = grid_view_y(gd, py);
148
149 sy = grid_view_y(gd, gd->sy);
150
151 grid_move_lines(gd, py, py + ny, sy - py - ny);
152 grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny));
153 }
154
155 /* Delete lines inside scroll region. */
156 void
grid_view_delete_lines_region(struct grid * gd,u_int rlower,u_int py,u_int ny)157 grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
158 u_int ny)
159 {
160 u_int ny2;
161
162 rlower = grid_view_y(gd, rlower);
163
164 py = grid_view_y(gd, py);
165
166 ny2 = rlower + 1 - py - ny;
167 grid_move_lines(gd, py, py + ny, ny2);
168 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2);
169 }
170
171 /* Insert characters. */
172 void
grid_view_insert_cells(struct grid * gd,u_int px,u_int py,u_int nx)173 grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx)
174 {
175 u_int sx;
176
177 px = grid_view_x(gd, px);
178 py = grid_view_y(gd, py);
179
180 sx = grid_view_x(gd, gd->sx);
181
182 if (px == sx - 1)
183 grid_clear(gd, px, py, 1, 1);
184 else
185 grid_move_cells(gd, px + nx, px, py, sx - px - nx);
186 }
187
188 /* Delete characters. */
189 void
grid_view_delete_cells(struct grid * gd,u_int px,u_int py,u_int nx)190 grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx)
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 grid_move_cells(gd, px, px + nx, py, sx - px - nx);
200 grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1);
201 }
202
203 /* Convert cells into a string. */
204 char *
grid_view_string_cells(struct grid * gd,u_int px,u_int py,u_int nx)205 grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
206 {
207 px = grid_view_x(gd, px);
208 py = grid_view_y(gd, py);
209
210 return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
211 }
212