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