4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
26 * Grid view functions. These work using coordinates relative to the visible
30 #define grid_view_x(gd, x) (x)
31 #define grid_view_y(gd, y) ((gd)->hsize + (y))
33 /* Get cell for reading. */
34 const struct grid_cell
*
35 grid_view_peek_cell(struct grid
*gd
, u_int px
, u_int py
)
37 return (grid_peek_cell(gd
, grid_view_x(gd
, px
), grid_view_y(gd
, py
)));
40 /* Get cell for writing. */
42 grid_view_get_cell(struct grid
*gd
, u_int px
, u_int py
)
44 return (grid_get_cell(gd
, grid_view_x(gd
, px
), grid_view_y(gd
, py
)));
50 struct grid
*gd
, u_int px
, u_int py
, const struct grid_cell
*gc
)
52 grid_set_cell(gd
, grid_view_x(gd
, px
), grid_view_y(gd
, py
), gc
);
55 /* Get UTF-8 for reading. */
56 const struct grid_utf8
*
57 grid_view_peek_utf8(struct grid
*gd
, u_int px
, u_int py
)
59 return (grid_peek_utf8(gd
, grid_view_x(gd
, px
), grid_view_y(gd
, py
)));
62 /* Get UTF-8 for writing. */
64 grid_view_get_utf8(struct grid
*gd
, u_int px
, u_int py
)
66 return (grid_get_utf8(gd
, grid_view_x(gd
, px
), grid_view_y(gd
, py
)));
72 struct grid
*gd
, u_int px
, u_int py
, const struct grid_utf8
*gu
)
74 grid_set_utf8(gd
, grid_view_x(gd
, px
), grid_view_y(gd
, py
), gu
);
77 /* Clear into history. */
79 grid_view_clear_history(struct grid
*gd
)
86 /* Find the last used line. */
88 for (yy
= 0; yy
< gd
->sy
; yy
++) {
89 gl
= &gd
->linedata
[grid_view_y(gd
, yy
)];
90 if (gl
->cellsize
!= 0 || gl
->utf8size
!= 0)
96 /* Scroll the lines into the history. */
97 for (yy
= 0; yy
< last
; yy
++)
98 grid_scroll_history(gd
);
103 grid_view_clear(struct grid
*gd
, u_int px
, u_int py
, u_int nx
, u_int ny
)
105 GRID_DEBUG(gd
, "px=%u, py=%u, nx=%u, ny=%u", px
, py
, nx
, ny
);
107 px
= grid_view_x(gd
, px
);
108 py
= grid_view_y(gd
, py
);
110 grid_clear(gd
, px
, py
, nx
, ny
);
113 /* Scroll region up. */
115 grid_view_scroll_region_up(struct grid
*gd
, u_int rupper
, u_int rlower
)
117 GRID_DEBUG(gd
, "rupper=%u, rlower=%u", rupper
, rlower
);
119 if (gd
->flags
& GRID_HISTORY
) {
120 grid_collect_history(gd
);
121 if (rupper
== 0 && rlower
== gd
->sy
- 1)
122 grid_scroll_history(gd
);
124 rupper
= grid_view_y(gd
, rupper
);
125 rlower
= grid_view_y(gd
, rlower
);
126 grid_scroll_history_region(gd
, rupper
, rlower
);
129 rupper
= grid_view_y(gd
, rupper
);
130 rlower
= grid_view_y(gd
, rlower
);
131 grid_move_lines(gd
, rupper
, rupper
+ 1, rlower
- rupper
);
135 /* Scroll region down. */
137 grid_view_scroll_region_down(struct grid
*gd
, u_int rupper
, u_int rlower
)
139 GRID_DEBUG(gd
, "rupper=%u, rlower=%u", rupper
, rlower
);
141 rupper
= grid_view_y(gd
, rupper
);
142 rlower
= grid_view_y(gd
, rlower
);
144 grid_move_lines(gd
, rupper
+ 1, rupper
, rlower
- rupper
);
149 grid_view_insert_lines(struct grid
*gd
, u_int py
, u_int ny
)
153 GRID_DEBUG(gd
, "py=%u, ny=%u", py
, ny
);
155 py
= grid_view_y(gd
, py
);
157 sy
= grid_view_y(gd
, gd
->sy
);
159 grid_move_lines(gd
, py
+ ny
, py
, sy
- py
- ny
);
162 /* Insert lines in region. */
164 grid_view_insert_lines_region(struct grid
*gd
, u_int rlower
, u_int py
, u_int ny
)
168 GRID_DEBUG(gd
, "rlower=%u, py=%u, ny=%u", rlower
, py
, ny
);
170 rlower
= grid_view_y(gd
, rlower
);
172 py
= grid_view_y(gd
, py
);
174 ny2
= rlower
+ 1 - py
- ny
;
175 grid_move_lines(gd
, rlower
+ 1 - ny2
, py
, ny2
);
176 grid_clear(gd
, 0, py
+ ny2
, gd
->sx
, ny
- ny2
);
181 grid_view_delete_lines(struct grid
*gd
, u_int py
, u_int ny
)
185 GRID_DEBUG(gd
, "py=%u, ny=%u", py
, ny
);
187 py
= grid_view_y(gd
, py
);
189 sy
= grid_view_y(gd
, gd
->sy
);
191 grid_move_lines(gd
, py
, py
+ ny
, sy
- py
- ny
);
192 grid_clear(gd
, 0, sy
- ny
, gd
->sx
, py
+ ny
- (sy
- ny
));
195 /* Delete lines inside scroll region. */
197 grid_view_delete_lines_region(struct grid
*gd
, u_int rlower
, u_int py
, u_int ny
)
201 GRID_DEBUG(gd
, "rlower=%u, py=%u, ny=%u", rlower
, py
, ny
);
203 rlower
= grid_view_y(gd
, rlower
);
205 py
= grid_view_y(gd
, py
);
207 ny2
= rlower
+ 1 - py
- ny
;
208 grid_move_lines(gd
, py
, py
+ ny
, ny2
);
209 grid_clear(gd
, 0, py
+ ny2
, gd
->sx
, ny
- ny2
);
212 /* Insert characters. */
214 grid_view_insert_cells(struct grid
*gd
, u_int px
, u_int py
, u_int nx
)
218 GRID_DEBUG(gd
, "px=%u, py=%u, nx=%u", px
, py
, nx
);
220 px
= grid_view_x(gd
, px
);
221 py
= grid_view_y(gd
, py
);
223 sx
= grid_view_x(gd
, gd
->sx
);
226 grid_clear(gd
, px
, py
, 1, 1);
228 grid_move_cells(gd
, px
+ nx
, px
, py
, sx
- px
- nx
);
231 /* Delete characters. */
233 grid_view_delete_cells(struct grid
*gd
, u_int px
, u_int py
, u_int nx
)
237 GRID_DEBUG(gd
, "px=%u, py=%u, nx=%u", px
, py
, nx
);
239 px
= grid_view_x(gd
, px
);
240 py
= grid_view_y(gd
, py
);
242 sx
= grid_view_x(gd
, gd
->sx
);
244 grid_move_cells(gd
, px
, px
+ nx
, py
, sx
- px
- nx
);
245 grid_clear(gd
, sx
- nx
, py
, px
+ nx
- (sx
- nx
), 1);
248 /* Convert cells into a string. */
250 grid_view_string_cells(struct grid
*gd
, u_int px
, u_int py
, u_int nx
)
252 GRID_DEBUG(gd
, "px=%u, py=%u, nx=%u", px
, py
, nx
);
254 px
= grid_view_x(gd
, px
);
255 py
= grid_view_y(gd
, py
);
257 return (grid_string_cells(gd
, px
, py
, nx
));