Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
*/ |
40 |
package org.netbeans.lib.terminalemulator; |
41 |
|
42 |
import java.awt.Color; |
43 |
import java.awt.Dimension; |
44 |
import java.awt.Graphics; |
45 |
import java.awt.Point; |
46 |
import java.awt.Rectangle; |
47 |
import java.util.Arrays; |
48 |
|
49 |
/** |
50 |
* @author Ilia Gromov |
51 |
*/ |
52 |
public class Highlight { |
53 |
|
54 |
private final Term term; |
55 |
private final State state; |
56 |
|
57 |
private Extent[] highlightExtents = new Extent[0]; |
58 |
|
59 |
Highlight(Term term, State state) { |
60 |
this.term = term; |
61 |
this.state = state; |
62 |
} |
63 |
|
64 |
public Extent[] getHighlightExtents() { |
65 |
Extent[] copy = new Extent[highlightExtents.length]; |
66 |
for (int i = 0; i < highlightExtents.length; i++) { |
67 |
Extent x = highlightExtents[i]; |
68 |
copy[i] = new Extent(x.begin, x.end); |
69 |
} |
70 |
return copy; |
71 |
} |
72 |
|
73 |
public void setHighlightExtents(Extent[] highlightExtents) { |
74 |
this.highlightExtents = Arrays.copyOf(highlightExtents, highlightExtents.length); |
75 |
for (Extent extent : highlightExtents) { |
76 |
extent.order(); |
77 |
} |
78 |
} |
79 |
|
80 |
// properties: |
81 |
private Color color = new Color(230, 230, 255); // swing color |
82 |
|
83 |
void setColor(Color color) { |
84 |
this.color = color; |
85 |
} |
86 |
|
87 |
Color getColor() { |
88 |
return color; |
89 |
} |
90 |
|
91 |
private Color xor_color = Color.white; |
92 |
|
93 |
void setXORColor(Color color) { |
94 |
xor_color = color; |
95 |
} |
96 |
|
97 |
Color getXORColor() { |
98 |
return xor_color; |
99 |
} |
100 |
|
101 |
/** |
102 |
* Select inside one line Rows and columns are in absolute coords. |
103 |
*/ |
104 |
private void paint(Graphics g, int row, int bcol, int ecol) { |
105 |
|
106 |
// Instead of doing this SHOULD clip the Extent to what's in view |
107 |
// Row is outside the view |
108 |
if (row < state.firstx) { |
109 |
return; |
110 |
} |
111 |
if (row > state.firstx + state.rows) { |
112 |
return; |
113 |
} |
114 |
|
115 |
// Construct the rectangle we're going to paint |
116 |
BCoord begin = new BCoord(row, bcol); |
117 |
BCoord end = new BCoord(row, ecol); |
118 |
|
119 |
begin = term.toViewCoord(begin); |
120 |
end = term.toViewCoord(end); |
121 |
|
122 |
//Hotfix for issue 40189 |
123 |
if (begin == null || end == null) { |
124 |
return; |
125 |
} |
126 |
|
127 |
int lw; // width of last character in selection |
128 |
Line l = term.buf().lineAt(row); |
129 |
lw = l.width(term.metrics(), ecol); |
130 |
|
131 |
Point pbegin = term.toPixel(begin); |
132 |
Point pend = term.toPixel(end); |
133 |
pend.y += term.metrics().height; |
134 |
pend.x += term.metrics().width * lw; // xterm actually doesn't do this |
135 |
|
136 |
Dimension dim = new Dimension(pend.x - pbegin.x, |
137 |
pend.y - pbegin.y); |
138 |
Rectangle rect = new Rectangle(pbegin, dim); |
139 |
|
140 |
if (term.isSelectionXOR()) { |
141 |
g.setXORMode(xor_color); |
142 |
} else { |
143 |
g.setColor(color); |
144 |
} |
145 |
|
146 |
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
147 |
} |
148 |
|
149 |
void paint(final Graphics g) { |
150 |
/* |
151 |
* Paint the selection. |
152 |
*/ |
153 |
|
154 |
for (Extent x : highlightExtents) { |
155 |
if (x == null) { |
156 |
continue; |
157 |
} |
158 |
x.order(); |
159 |
|
160 |
// DEBUG System.out.println("Sel.paint extent: " + x); // NOI18N |
161 |
term.visitLines(x.begin, x.end, true, new LineVisitor() { |
162 |
public boolean visit(Line l, int row, int bcol, int ecol) { |
163 |
paint(g, row, bcol, ecol); |
164 |
return true; |
165 |
} |
166 |
}); |
167 |
} |
168 |
} |
169 |
|
170 |
public void clear() { |
171 |
highlightExtents = new Extent[0]; |
172 |
} |
173 |
} |