author | wenzelm |
Wed, 15 Jun 2011 16:26:09 +0200 | |
changeset 43398 | c3e2a361b418 |
parent 43396 | 548a68eafaea |
child 43404 | c8369f3d88a1 |
permissions | -rw-r--r-- |
43381 | 1 |
/* Title: Tools/jEdit/src/text_area_painter.scala |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
3 |
|
43381 | 4 |
Painter setup for main jEdit text area. |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
6 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle.jedit |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
8 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
9 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
10 |
import isabelle._ |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
11 |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
12 |
import java.awt.{Graphics2D, Shape} |
43392 | 13 |
import java.awt.font.TextAttribute |
14 |
import java.text.AttributedString |
|
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
15 |
import java.util.ArrayList |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
16 |
|
43398
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
17 |
import org.gjt.sp.util.Log |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
18 |
|
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
19 |
import org.gjt.sp.jedit.Debug |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
20 |
import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk} |
43392 | 21 |
import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter, JEditTextArea} |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
22 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
23 |
|
43381 | 24 |
class Text_Area_Painter(doc_view: Document_View) |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
25 |
{ |
43381 | 26 |
private val model = doc_view.model |
43382 | 27 |
private val buffer = model.buffer |
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
28 |
private val text_area = doc_view.text_area |
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
29 |
|
43398
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
30 |
private def painter_body(body: => Unit) |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
31 |
{ |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
32 |
if (buffer == text_area.getBuffer) |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
33 |
Swing_Thread.now { |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
34 |
try { Isabelle.buffer_lock(buffer) { body } } |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
35 |
catch { case t: Throwable => Log.log(Log.ERROR, this, t) } |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
36 |
} |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
37 |
} |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
38 |
|
43392 | 39 |
|
40 |
/* original painters */ |
|
41 |
||
42 |
private def pick_extension(name: String): TextAreaExtension = |
|
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
43 |
{ |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
44 |
text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
45 |
match { |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
46 |
case List(x) => x |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
47 |
case _ => error("Expected exactly one " + name) |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
48 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
49 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
50 |
|
43392 | 51 |
private val orig_text_painter = |
52 |
pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText") |
|
53 |
||
54 |
||
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
55 |
/* common painter state */ |
43381 | 56 |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
57 |
@volatile private var painter_snapshot: Document.Snapshot = null |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
58 |
@volatile private var painter_clip: Shape = null |
43381 | 59 |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
60 |
private val set_state = new TextAreaExtension |
43381 | 61 |
{ |
62 |
override def paintScreenLineRange(gfx: Graphics2D, |
|
63 |
first_line: Int, last_line: Int, physical_lines: Array[Int], |
|
64 |
start: Array[Int], end: Array[Int], y: Int, line_height: Int) |
|
65 |
{ |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
66 |
painter_snapshot = model.snapshot() |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
67 |
painter_clip = gfx.getClip |
43381 | 68 |
} |
69 |
} |
|
70 |
||
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
71 |
private val reset_state = new TextAreaExtension |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
72 |
{ |
43381 | 73 |
override def paintScreenLineRange(gfx: Graphics2D, |
74 |
first_line: Int, last_line: Int, physical_lines: Array[Int], |
|
75 |
start: Array[Int], end: Array[Int], y: Int, line_height: Int) |
|
76 |
{ |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
77 |
painter_snapshot = null |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
78 |
painter_clip = null |
43381 | 79 |
} |
80 |
} |
|
81 |
||
82 |
||
83 |
/* text background */ |
|
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
84 |
|
43381 | 85 |
private val background_painter = new TextAreaExtension |
86 |
{ |
|
87 |
override def paintScreenLineRange(gfx: Graphics2D, |
|
88 |
first_line: Int, last_line: Int, physical_lines: Array[Int], |
|
89 |
start: Array[Int], end: Array[Int], y: Int, line_height: Int) |
|
90 |
{ |
|
43398
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
91 |
painter_body { |
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
92 |
val snapshot = painter_snapshot |
43381 | 93 |
val ascent = text_area.getPainter.getFontMetrics.getAscent |
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
94 |
|
43381 | 95 |
for (i <- 0 until physical_lines.length) { |
96 |
if (physical_lines(i) != -1) { |
|
97 |
val line_range = doc_view.proper_line_range(start(i), end(i)) |
|
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
98 |
|
43381 | 99 |
// background color: status |
100 |
val cmds = snapshot.node.command_range(snapshot.revert(line_range)) |
|
101 |
for { |
|
102 |
(command, command_start) <- cmds if !command.is_ignored |
|
103 |
val range = line_range.restrict(snapshot.convert(command.range + command_start)) |
|
104 |
r <- Isabelle.gfx_range(text_area, range) |
|
105 |
color <- Isabelle_Markup.status_color(snapshot, command) |
|
106 |
} { |
|
107 |
gfx.setColor(color) |
|
108 |
gfx.fillRect(r.x, y + i * line_height, r.length, line_height) |
|
109 |
} |
|
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
110 |
|
43381 | 111 |
// background color (1): markup |
112 |
for { |
|
113 |
Text.Info(range, Some(color)) <- |
|
114 |
snapshot.select_markup(line_range)(Isabelle_Markup.background1).iterator |
|
115 |
r <- Isabelle.gfx_range(text_area, range) |
|
116 |
} { |
|
117 |
gfx.setColor(color) |
|
118 |
gfx.fillRect(r.x, y + i * line_height, r.length, line_height) |
|
119 |
} |
|
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
120 |
|
43381 | 121 |
// background color (2): markup |
122 |
for { |
|
123 |
Text.Info(range, Some(color)) <- |
|
124 |
snapshot.select_markup(line_range)(Isabelle_Markup.background2).iterator |
|
125 |
r <- Isabelle.gfx_range(text_area, range) |
|
126 |
} { |
|
127 |
gfx.setColor(color) |
|
128 |
gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4) |
|
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
129 |
} |
43381 | 130 |
|
131 |
// sub-expression highlighting -- potentially from other snapshot |
|
132 |
doc_view.highlight_range match { |
|
133 |
case Some((range, color)) if line_range.overlaps(range) => |
|
134 |
Isabelle.gfx_range(text_area, line_range.restrict(range)) match { |
|
135 |
case None => |
|
136 |
case Some(r) => |
|
137 |
gfx.setColor(color) |
|
138 |
gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1) |
|
139 |
} |
|
140 |
case _ => |
|
141 |
} |
|
142 |
||
143 |
// squiggly underline |
|
144 |
for { |
|
145 |
Text.Info(range, Some(color)) <- |
|
146 |
snapshot.select_markup(line_range)(Isabelle_Markup.message).iterator |
|
147 |
r <- Isabelle.gfx_range(text_area, range) |
|
148 |
} { |
|
149 |
gfx.setColor(color) |
|
150 |
val x0 = (r.x / 2) * 2 |
|
151 |
val y0 = r.y + ascent + 1 |
|
152 |
for (x1 <- Range(x0, x0 + r.length, 2)) { |
|
153 |
val y1 = if (x1 % 4 < 2) y0 else y0 + 1 |
|
154 |
gfx.drawLine(x1, y1, x1 + 1, y1) |
|
43376
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
155 |
} |
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
156 |
} |
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
157 |
} |
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
158 |
} |
0f6880c1c759
some direct text foreground painting, instead of token marking;
wenzelm
parents:
43375
diff
changeset
|
159 |
} |
43381 | 160 |
} |
161 |
} |
|
162 |
||
163 |
||
164 |
/* text */ |
|
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
165 |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
166 |
def char_width(): Int = |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
167 |
{ |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
168 |
val painter = text_area.getPainter |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
169 |
val font = painter.getFont |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
170 |
val font_context = painter.getFontRenderContext |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
171 |
font.getStringBounds(" ", font_context).getWidth.round.toInt |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
172 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
173 |
|
43382 | 174 |
private def line_infos(physical_lines: Iterator[Int]): Map[Text.Offset, Chunk] = |
175 |
{ |
|
176 |
val painter = text_area.getPainter |
|
177 |
val font = painter.getFont |
|
178 |
val font_context = painter.getFontRenderContext |
|
179 |
||
180 |
// see org.gjt.sp.jedit.textarea.TextArea.propertiesChanged |
|
181 |
// see org.gjt.sp.jedit.textarea.TextArea.setMaxLineLength |
|
182 |
val margin = |
|
183 |
if (buffer.getStringProperty("wrap") != "soft") 0.0f |
|
184 |
else { |
|
185 |
val max = buffer.getIntegerProperty("maxLineLen", 0) |
|
186 |
if (max > 0) font.getStringBounds(" " * max, font_context).getWidth.toInt |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
187 |
else painter.getWidth - char_width() * 3 |
43382 | 188 |
}.toFloat |
189 |
||
190 |
val out = new ArrayList[Chunk] |
|
191 |
val handler = new DisplayTokenHandler |
|
192 |
||
193 |
var result = Map[Text.Offset, Chunk]() |
|
194 |
for (line <- physical_lines) { |
|
195 |
out.clear |
|
196 |
handler.init(painter.getStyles, font_context, painter, out, margin) |
|
197 |
buffer.markTokens(line, handler) |
|
198 |
||
199 |
val line_start = buffer.getLineStartOffset(line) |
|
200 |
for (i <- 0 until out.size) { |
|
201 |
val chunk = out.get(i) |
|
202 |
result += (line_start + chunk.offset -> chunk) |
|
203 |
} |
|
204 |
} |
|
205 |
result |
|
206 |
} |
|
207 |
||
208 |
private def paint_chunk_list(gfx: Graphics2D, |
|
43392 | 209 |
offset: Text.Offset, caret_offset: Text.Offset, head: Chunk, x: Float, y: Float): Float = |
43382 | 210 |
{ |
211 |
val clip_rect = gfx.getClipBounds |
|
43392 | 212 |
val painter = text_area.getPainter |
213 |
val font_context = painter.getFontRenderContext |
|
43382 | 214 |
|
215 |
var w = 0.0f |
|
216 |
var chunk_offset = offset |
|
217 |
var chunk = head |
|
218 |
while (chunk != null) { |
|
219 |
val chunk_length = if (chunk.str == null) 0 else chunk.str.length |
|
220 |
||
221 |
if (x + w + chunk.width > clip_rect.x && |
|
222 |
x + w < clip_rect.x + clip_rect.width && |
|
223 |
chunk.accessable && chunk.visible) |
|
224 |
{ |
|
225 |
val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk_length) |
|
226 |
val chunk_font = chunk.style.getFont |
|
227 |
val chunk_color = chunk.style.getForegroundColor |
|
228 |
||
229 |
val markup = painter_snapshot.select_markup(chunk_range)(Isabelle_Markup.foreground) |
|
230 |
||
231 |
gfx.setFont(chunk_font) |
|
232 |
if (!Debug.DISABLE_GLYPH_VECTOR && chunk.gv != null && |
|
43392 | 233 |
markup.forall(info => info.info.isEmpty) && |
234 |
!chunk_range.contains(caret_offset)) { |
|
43382 | 235 |
gfx.setColor(chunk_color) |
236 |
gfx.drawGlyphVector(chunk.gv, x + w, y) |
|
237 |
} |
|
238 |
else { |
|
43383 | 239 |
var x1 = x + w |
43382 | 240 |
for (Text.Info(range, info) <- markup) { |
241 |
val str = chunk.str.substring(range.start - chunk_offset, range.stop - chunk_offset) |
|
242 |
gfx.setColor(info.getOrElse(chunk_color)) |
|
43392 | 243 |
if (range.contains(caret_offset)) { |
244 |
val astr = new AttributedString(str) |
|
245 |
val i = caret_offset - range.start |
|
246 |
astr.addAttribute(TextAttribute.FONT, chunk_font) |
|
247 |
astr.addAttribute(TextAttribute.FOREGROUND, painter.getCaretColor, i, i + 1) |
|
248 |
astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON, i, i + 1) |
|
249 |
gfx.drawString(astr.getIterator, x1.toInt, y.toInt) |
|
250 |
} |
|
251 |
else { |
|
252 |
gfx.drawString(str, x1.toInt, y.toInt) |
|
253 |
} |
|
43383 | 254 |
x1 += chunk_font.getStringBounds(str, font_context).getWidth.toFloat |
43382 | 255 |
} |
256 |
} |
|
257 |
} |
|
258 |
w += chunk.width |
|
259 |
chunk_offset += chunk_length |
|
260 |
chunk = chunk.next.asInstanceOf[Chunk] |
|
261 |
} |
|
262 |
w |
|
263 |
} |
|
264 |
||
43381 | 265 |
private val text_painter = new TextAreaExtension |
266 |
{ |
|
267 |
override def paintScreenLineRange(gfx: Graphics2D, |
|
268 |
first_line: Int, last_line: Int, physical_lines: Array[Int], |
|
43382 | 269 |
start: Array[Int], end: Array[Int], y: Int, line_height: Int) |
43381 | 270 |
{ |
43398
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
271 |
painter_body { |
43381 | 272 |
val clip = gfx.getClip |
273 |
val x0 = text_area.getHorizontalOffset |
|
43382 | 274 |
val fm = text_area.getPainter.getFontMetrics |
275 |
var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent |
|
43372
2df2144b0910
use orig_text_painter for extras outside main text (also required to update internal line infos);
wenzelm
parents:
43371
diff
changeset
|
276 |
|
43392 | 277 |
val caret_offset = |
278 |
if (text_area.hasFocus) text_area.getCaretPosition |
|
279 |
else -1 |
|
280 |
||
43382 | 281 |
val infos = line_infos(physical_lines.iterator.filter(i => i != -1)) |
43381 | 282 |
for (i <- 0 until physical_lines.length) { |
283 |
if (physical_lines(i) != -1) { |
|
43392 | 284 |
val x1 = |
285 |
infos.get(start(i)) match { |
|
286 |
case None => x0 |
|
287 |
case Some(chunk) => |
|
288 |
gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height) |
|
289 |
val w = paint_chunk_list(gfx, start(i), caret_offset, chunk, x0, y0).toInt |
|
290 |
x0 + w.toInt |
|
291 |
} |
|
292 |
gfx.clipRect(x1, 0, Integer.MAX_VALUE, Integer.MAX_VALUE) |
|
293 |
orig_text_painter.paintValidLine(gfx, |
|
294 |
first_line + i, physical_lines(i), start(i), end(i), y + line_height * i) |
|
295 |
gfx.setClip(clip) |
|
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
296 |
} |
43381 | 297 |
y0 += line_height |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
298 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
299 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
300 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
301 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
302 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
303 |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
304 |
/* caret -- outside of text range */ |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
305 |
|
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
306 |
private class Caret_Painter(before: Boolean) extends TextAreaExtension |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
307 |
{ |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
308 |
override def paintValidLine(gfx: Graphics2D, |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
309 |
screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
310 |
{ |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
311 |
if (before) gfx.clipRect(0, 0, 0, 0) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
312 |
else gfx.setClip(painter_clip) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
313 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
314 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
315 |
|
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
316 |
private val before_caret_painter1 = new Caret_Painter(true) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
317 |
private val after_caret_painter1 = new Caret_Painter(false) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
318 |
private val before_caret_painter2 = new Caret_Painter(true) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
319 |
private val after_caret_painter2 = new Caret_Painter(false) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
320 |
|
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
321 |
private val caret_painter = new TextAreaExtension |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
322 |
{ |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
323 |
override def paintValidLine(gfx: Graphics2D, |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
324 |
screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
325 |
{ |
43398
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
326 |
painter_body { |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
327 |
if (text_area.hasFocus) { |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
328 |
val caret = text_area.getCaretPosition |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
329 |
if (start <= caret && caret == end - 1) { |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
330 |
val painter = text_area.getPainter |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
331 |
val fm = painter.getFontMetrics |
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
332 |
|
43398
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
333 |
val offset = caret - text_area.getLineStartOffset(physical_line) |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
334 |
val x = text_area.offsetToXY(physical_line, offset).x |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
335 |
gfx.setColor(painter.getCaretColor) |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
336 |
gfx.drawRect(x, y, char_width() - 1, fm.getHeight - 1) |
c3e2a361b418
more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents:
43396
diff
changeset
|
337 |
} |
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
338 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
339 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
340 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
341 |
} |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
342 |
|
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
343 |
|
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
344 |
/* activation */ |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
345 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
346 |
def activate() |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
347 |
{ |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
348 |
val painter = text_area.getPainter |
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
349 |
painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state) |
43381 | 350 |
painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter) |
351 |
painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter) |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
352 |
painter.addExtension(TextAreaPainter.CARET_LAYER - 1, before_caret_painter1) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
353 |
painter.addExtension(TextAreaPainter.CARET_LAYER + 1, after_caret_painter1) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
354 |
painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER - 1, before_caret_painter2) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
355 |
painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 1, after_caret_painter2) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
356 |
painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
357 |
painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_state) |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
358 |
painter.removeExtension(orig_text_painter) |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
359 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
360 |
|
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
361 |
def deactivate() |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
362 |
{ |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
363 |
val painter = text_area.getPainter |
43396 | 364 |
painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter) |
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
365 |
painter.removeExtension(reset_state) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
366 |
painter.removeExtension(caret_painter) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
367 |
painter.removeExtension(after_caret_painter2) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
368 |
painter.removeExtension(before_caret_painter2) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
369 |
painter.removeExtension(after_caret_painter1) |
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
370 |
painter.removeExtension(before_caret_painter1) |
43381 | 371 |
painter.removeExtension(text_painter) |
372 |
painter.removeExtension(background_painter) |
|
43393
f4141da52e92
more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents:
43392
diff
changeset
|
373 |
painter.removeExtension(set_state) |
43369
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
374 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
375 |
} |
4c86b3405010
separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff
changeset
|
376 |