| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 Kevin Lamonte |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer.tterminal; |
| 30 | |
| 31 | import jexer.bits.Cell; |
| 32 | import jexer.bits.CellAttributes; |
| 33 | |
| 34 | /** |
| 35 | * This represents a single line of the display buffer. |
| 36 | */ |
| 37 | public class DisplayLine { |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Constants -------------------------------------------------------------- |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
| 43 | /** |
| 44 | * Maximum line length. |
| 45 | */ |
| 46 | private static final int MAX_LINE_LENGTH = 256; |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Variables -------------------------------------------------------------- |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | /** |
| 53 | * The characters/attributes of the line. |
| 54 | */ |
| 55 | private Cell [] chars; |
| 56 | |
| 57 | /** |
| 58 | * Double-width line flag. |
| 59 | */ |
| 60 | private boolean doubleWidth = false; |
| 61 | |
| 62 | /** |
| 63 | * Double height line flag. Valid values are: |
| 64 | * |
| 65 | * <p><pre> |
| 66 | * 0 = single height |
| 67 | * 1 = top half double height |
| 68 | * 2 = bottom half double height |
| 69 | * </pre> |
| 70 | */ |
| 71 | private int doubleHeight = 0; |
| 72 | |
| 73 | /** |
| 74 | * DECSCNM - reverse video. We copy the flag to the line so that |
| 75 | * reverse-mode scrollback lines still show inverted colors correctly. |
| 76 | */ |
| 77 | private boolean reverseColor = false; |
| 78 | |
| 79 | // ------------------------------------------------------------------------ |
| 80 | // Constructors ----------------------------------------------------------- |
| 81 | // ------------------------------------------------------------------------ |
| 82 | |
| 83 | /** |
| 84 | * Public constructor sets everything to drawing attributes. |
| 85 | * |
| 86 | * @param attr current drawing attributes |
| 87 | */ |
| 88 | public DisplayLine(final CellAttributes attr) { |
| 89 | chars = new Cell[MAX_LINE_LENGTH]; |
| 90 | for (int i = 0; i < chars.length; i++) { |
| 91 | chars[i] = new Cell(); |
| 92 | chars[i].setTo(attr); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // ------------------------------------------------------------------------ |
| 97 | // DisplayLine ------------------------------------------------------------ |
| 98 | // ------------------------------------------------------------------------ |
| 99 | |
| 100 | /** |
| 101 | * Get the Cell at a specific column. |
| 102 | * |
| 103 | * @param idx the character index |
| 104 | * @return the Cell |
| 105 | */ |
| 106 | public Cell charAt(final int idx) { |
| 107 | return chars[idx]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the length of this line. |
| 112 | * |
| 113 | * @return line length |
| 114 | */ |
| 115 | public int length() { |
| 116 | return chars.length; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get double width flag. |
| 121 | * |
| 122 | * @return double width |
| 123 | */ |
| 124 | public boolean isDoubleWidth() { |
| 125 | return doubleWidth; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set double width flag. |
| 130 | * |
| 131 | * @param doubleWidth new value for double width flag |
| 132 | */ |
| 133 | public void setDoubleWidth(final boolean doubleWidth) { |
| 134 | this.doubleWidth = doubleWidth; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get double height flag. |
| 139 | * |
| 140 | * @return double height |
| 141 | */ |
| 142 | public int getDoubleHeight() { |
| 143 | return doubleHeight; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Set double height flag. |
| 148 | * |
| 149 | * @param doubleHeight new value for double height flag |
| 150 | */ |
| 151 | public void setDoubleHeight(final int doubleHeight) { |
| 152 | this.doubleHeight = doubleHeight; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get reverse video flag. |
| 157 | * |
| 158 | * @return reverse video |
| 159 | */ |
| 160 | public boolean isReverseColor() { |
| 161 | return reverseColor; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Set double-height flag. |
| 166 | * |
| 167 | * @param reverseColor new value for reverse video flag |
| 168 | */ |
| 169 | public void setReverseColor(final boolean reverseColor) { |
| 170 | this.reverseColor = reverseColor; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Insert a character at the specified position. |
| 175 | * |
| 176 | * @param idx the character index |
| 177 | * @param newCell the new Cell |
| 178 | */ |
| 179 | public void insert(final int idx, final Cell newCell) { |
| 180 | System.arraycopy(chars, idx, chars, idx + 1, chars.length - idx - 1); |
| 181 | chars[idx] = new Cell(); |
| 182 | chars[idx].setTo(newCell); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Replace character at the specified position. |
| 187 | * |
| 188 | * @param idx the character index |
| 189 | * @param newCell the new Cell |
| 190 | */ |
| 191 | public void replace(final int idx, final Cell newCell) { |
| 192 | chars[idx].setTo(newCell); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Set the Cell at the specified position to the blank (reset). |
| 197 | * |
| 198 | * @param idx the character index |
| 199 | */ |
| 200 | public void setBlank(final int idx) { |
| 201 | chars[idx].reset(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Set the character (just the char, not the attributes) at the specified |
| 206 | * position to ch. |
| 207 | * |
| 208 | * @param idx the character index |
| 209 | * @param ch the new char |
| 210 | */ |
| 211 | public void setChar(final int idx, final char ch) { |
| 212 | chars[idx].setChar(ch); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Set the attributes (just the attributes, not the char) at the |
| 217 | * specified position to attr. |
| 218 | * |
| 219 | * @param idx the character index |
| 220 | * @param attr the new attributes |
| 221 | */ |
| 222 | public void setAttr(final int idx, final CellAttributes attr) { |
| 223 | chars[idx].setAttr(attr); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Delete character at the specified position, filling in the new |
| 228 | * character on the right with newCell. |
| 229 | * |
| 230 | * @param idx the character index |
| 231 | * @param newCell the new Cell |
| 232 | */ |
| 233 | public void delete(final int idx, final Cell newCell) { |
| 234 | System.arraycopy(chars, idx + 1, chars, idx, chars.length - idx - 1); |
| 235 | chars[chars.length - 1] = new Cell(); |
| 236 | chars[chars.length - 1].setTo(newCell); |
| 237 | } |
| 238 | |
| 239 | } |