1 /* $OpenBSD: grid-view.c,v 1.10 2012/01/29 21:31:11 nicm Exp $ */ 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 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) 58 { 59 return (grid_peek_utf8(gd, grid_view_x(gd, px), grid_view_y(gd, py))); 60 } 61 62 /* Get UTF-8 for writing. */ 63 struct grid_utf8 * 64 grid_view_get_utf8(struct grid *gd, u_int px, u_int py) 65 { 66 return (grid_get_utf8(gd, grid_view_x(gd, px), grid_view_y(gd, py))); 67 } 68 69 /* Set UTF-8. */ 70 void 71 grid_view_set_utf8( 72 struct grid *gd, u_int px, u_int py, const struct grid_utf8 *gu) 73 { 74 grid_set_utf8(gd, grid_view_x(gd, px), grid_view_y(gd, py), gu); 75 } 76 77 /* Clear into history. */ 78 void 79 grid_view_clear_history(struct grid *gd) 80 { 81 struct grid_line *gl; 82 u_int yy, last; 83 84 GRID_DEBUG(gd, ""); 85 86 /* Find the last used line. */ 87 last = 0; 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) 91 last = yy + 1; 92 } 93 if (last == 0) 94 return; 95 96 /* Scroll the lines into the history. */ 97 for (yy = 0; yy < last; yy++) { 98 grid_collect_history(gd); 99 grid_scroll_history(gd); 100 } 101 } 102 103 /* Clear area. */ 104 void 105 grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny) 106 { 107 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u, ny=%u", px, py, nx, ny); 108 109 px = grid_view_x(gd, px); 110 py = grid_view_y(gd, py); 111 112 grid_clear(gd, px, py, nx, ny); 113 } 114 115 /* Scroll region up. */ 116 void 117 grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower) 118 { 119 GRID_DEBUG(gd, "rupper=%u, rlower=%u", rupper, rlower); 120 121 if (gd->flags & GRID_HISTORY) { 122 grid_collect_history(gd); 123 if (rupper == 0 && rlower == gd->sy - 1) 124 grid_scroll_history(gd); 125 else { 126 rupper = grid_view_y(gd, rupper); 127 rlower = grid_view_y(gd, rlower); 128 grid_scroll_history_region(gd, rupper, rlower); 129 } 130 } else { 131 rupper = grid_view_y(gd, rupper); 132 rlower = grid_view_y(gd, rlower); 133 grid_move_lines(gd, rupper, rupper + 1, rlower - rupper); 134 } 135 } 136 137 /* Scroll region down. */ 138 void 139 grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower) 140 { 141 GRID_DEBUG(gd, "rupper=%u, rlower=%u", rupper, rlower); 142 143 rupper = grid_view_y(gd, rupper); 144 rlower = grid_view_y(gd, rlower); 145 146 grid_move_lines(gd, rupper + 1, rupper, rlower - rupper); 147 } 148 149 /* Insert lines. */ 150 void 151 grid_view_insert_lines(struct grid *gd, u_int py, u_int ny) 152 { 153 u_int sy; 154 155 GRID_DEBUG(gd, "py=%u, ny=%u", py, ny); 156 157 py = grid_view_y(gd, py); 158 159 sy = grid_view_y(gd, gd->sy); 160 161 grid_move_lines(gd, py + ny, py, sy - py - ny); 162 } 163 164 /* Insert lines in region. */ 165 void 166 grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py, u_int ny) 167 { 168 u_int ny2; 169 170 GRID_DEBUG(gd, "rlower=%u, py=%u, ny=%u", rlower, py, ny); 171 172 rlower = grid_view_y(gd, rlower); 173 174 py = grid_view_y(gd, py); 175 176 ny2 = rlower + 1 - py - ny; 177 grid_move_lines(gd, rlower + 1 - ny2, py, ny2); 178 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2); 179 } 180 181 /* Delete lines. */ 182 void 183 grid_view_delete_lines(struct grid *gd, u_int py, u_int ny) 184 { 185 u_int sy; 186 187 GRID_DEBUG(gd, "py=%u, ny=%u", py, ny); 188 189 py = grid_view_y(gd, py); 190 191 sy = grid_view_y(gd, gd->sy); 192 193 grid_move_lines(gd, py, py + ny, sy - py - ny); 194 grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny)); 195 } 196 197 /* Delete lines inside scroll region. */ 198 void 199 grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py, u_int ny) 200 { 201 u_int ny2; 202 203 GRID_DEBUG(gd, "rlower=%u, py=%u, ny=%u", rlower, py, ny); 204 205 rlower = grid_view_y(gd, rlower); 206 207 py = grid_view_y(gd, py); 208 209 ny2 = rlower + 1 - py - ny; 210 grid_move_lines(gd, py, py + ny, ny2); 211 grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2); 212 } 213 214 /* Insert characters. */ 215 void 216 grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx) 217 { 218 u_int sx; 219 220 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx); 221 222 px = grid_view_x(gd, px); 223 py = grid_view_y(gd, py); 224 225 sx = grid_view_x(gd, gd->sx); 226 227 if (px == sx - 1) 228 grid_clear(gd, px, py, 1, 1); 229 else 230 grid_move_cells(gd, px + nx, px, py, sx - px - nx); 231 } 232 233 /* Delete characters. */ 234 void 235 grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx) 236 { 237 u_int sx; 238 239 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx); 240 241 px = grid_view_x(gd, px); 242 py = grid_view_y(gd, py); 243 244 sx = grid_view_x(gd, gd->sx); 245 246 grid_move_cells(gd, px, px + nx, py, sx - px - nx); 247 grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1); 248 } 249 250 /* Convert cells into a string. */ 251 char * 252 grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx) 253 { 254 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx); 255 256 px = grid_view_x(gd, px); 257 py = grid_view_y(gd, py); 258 259 return (grid_string_cells(gd, px, py, nx)); 260 } 261