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