color chooser widget
[nikiroo-utils.git] / src / jexer / tterminal / DisplayLine.java
... / ...
CommitLineData
1/*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31package jexer.tterminal;
32
33import jexer.bits.Cell;
34import jexer.bits.CellAttributes;
35
36/**
37 * This represents a single line of the display buffer.
38 */
39public final class DisplayLine {
40 /**
41 * Maximum line length.
42 */
43 private static final int MAX_LINE_LENGTH = 256;
44
45 /**
46 * The characters/attributes of the line.
47 */
48 private Cell [] chars;
49
50 /**
51 * Get the Cell at a specific column.
52 *
53 * @param idx the character index
54 * @return the Cell
55 */
56 public Cell charAt(final int idx) {
57 return chars[idx];
58 }
59
60 /**
61 * Get the length of this line.
62 *
63 * @return line length
64 */
65 public int length() {
66 return chars.length;
67 }
68
69 /**
70 * Double-width line flag.
71 */
72 private boolean doubleWidth = false;
73
74 /**
75 * Get double width flag.
76 *
77 * @return double width
78 */
79 public boolean isDoubleWidth() {
80 return doubleWidth;
81 }
82
83 /**
84 * Set double width flag.
85 *
86 * @param doubleWidth new value for double width flag
87 */
88 public void setDoubleWidth(final boolean doubleWidth) {
89 this.doubleWidth = doubleWidth;
90 }
91
92 /**
93 * Double height line flag. Valid values are:
94 *
95 * <p><pre>
96 * 0 = single height
97 * 1 = top half double height
98 * 2 = bottom half double height
99 * </pre>
100 */
101 private int doubleHeight = 0;
102
103 /**
104 * Get double height flag.
105 *
106 * @return double height
107 */
108 public int getDoubleHeight() {
109 return doubleHeight;
110 }
111
112 /**
113 * Set double height flag.
114 *
115 * @param doubleHeight new value for double height flag
116 */
117 public void setDoubleHeight(final int doubleHeight) {
118 this.doubleHeight = doubleHeight;
119 }
120
121 /**
122 * DECSCNM - reverse video. We copy the flag to the line so that
123 * reverse-mode scrollback lines still show inverted colors correctly.
124 */
125 private boolean reverseColor = false;
126
127 /**
128 * Get reverse video flag.
129 *
130 * @return reverse video
131 */
132 public boolean isReverseColor() {
133 return reverseColor;
134 }
135
136 /**
137 * Set double-height flag.
138 *
139 * @param reverseColor new value for reverse video flag
140 */
141 public void setReverseColor(final boolean reverseColor) {
142 this.reverseColor = reverseColor;
143 }
144
145 /**
146 * Public constructor sets everything to drawing attributes.
147 *
148 * @param attr current drawing attributes
149 */
150 public DisplayLine(final CellAttributes attr) {
151 chars = new Cell[MAX_LINE_LENGTH];
152 for (int i = 0; i < chars.length; i++) {
153 chars[i] = new Cell();
154 chars[i].setTo(attr);
155 }
156 }
157
158 /**
159 * Insert a character at the specified position.
160 *
161 * @param idx the character index
162 * @param newCell the new Cell
163 */
164 public void insert(final int idx, final Cell newCell) {
165 System.arraycopy(chars, idx, chars, idx + 1, chars.length - idx - 1);
166 chars[idx] = newCell;
167 }
168
169 /**
170 * Replace character at the specified position.
171 *
172 * @param idx the character index
173 * @param newCell the new Cell
174 */
175 public void replace(final int idx, final Cell newCell) {
176 chars[idx].setTo(newCell);
177 }
178
179 /**
180 * Set the Cell at the specified position to the blank (reset).
181 *
182 * @param idx the character index
183 */
184 public void setBlank(final int idx) {
185 chars[idx].reset();
186 }
187
188 /**
189 * Set the character (just the char, not the attributes) at the specified
190 * position to ch.
191 *
192 * @param idx the character index
193 * @param ch the new char
194 */
195 public void setChar(final int idx, final char ch) {
196 chars[idx].setChar(ch);
197 }
198
199 /**
200 * Set the attributes (just the attributes, not the char) at the
201 * specified position to attr.
202 *
203 * @param idx the character index
204 * @param attr the new attributes
205 */
206 public void setAttr(final int idx, final CellAttributes attr) {
207 chars[idx].setAttr(attr);
208 }
209
210 /**
211 * Delete character at the specified position, filling in the new
212 * character on the right with newCell.
213 *
214 * @param idx the character index
215 * @param newCell the new Cell
216 */
217 public void delete(final int idx, final Cell newCell) {
218 System.arraycopy(chars, idx + 1, chars, idx, chars.length - idx - 1);
219 chars[chars.length - 1] = newCell;
220 }
221
222}