Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3093d98e7a04d36385ac1a6e722381c9ecd9b152 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*******************************************************************************
 * Copyright (c) 2006, 2012 Wind River Systems, Inc., IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation - https://bugs.eclipse.org/bugs/show_bug.cgi?id=22712
 *     Anton Leherbauer (Wind River Systems) - [painting] Long lines take too long to display when "Show Whitespace Characters" is enabled - https://bugs.eclipse.org/bugs/show_bug.cgi?id=196116
 *     Anton Leherbauer (Wind River Systems) - [painting] Whitespace characters not drawn when scrolling to right slowly - https://bugs.eclipse.org/bugs/show_bug.cgi?id=206633
 *     Tom Eicher (Avaloq Evolution AG) - block selection mode
 *******************************************************************************/
package org.eclipse.jface.text;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.StyledTextContent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;


/**
 * A painter for drawing visible characters for (invisible) whitespace
 * characters.
 *
 * @since 3.3
 */
public class WhitespaceCharacterPainter implements IPainter, PaintListener {

	private static final char SPACE_SIGN= '\u00b7';
	private static final char IDEOGRAPHIC_SPACE_SIGN= '\u00b0';
	private static final char TAB_SIGN= '\u00bb';
	private static final char CARRIAGE_RETURN_SIGN= '\u00a4';
	private static final char LINE_FEED_SIGN= '\u00b6';

	/** Indicates whether this painter is active. */
	private boolean fIsActive= false;
	/** The source viewer this painter is attached to. */
	private ITextViewer fTextViewer;
	/** The viewer's widget. */
	private StyledText fTextWidget;
	/** Tells whether the advanced graphics sub system is available. */
	private final boolean fIsAdvancedGraphicsPresent;
	/**
	 * Tells whether the text widget was created with the full selection style bit or not.
	 * @since 3.7
	 */
	private final boolean fIsFullSelectionStyle;
	/** @since 3.7 */
	private boolean fShowLeadingSpaces= true;
	/** @since 3.7 */
	private boolean fShowEnclosedSpace= true;
	/** @since 3.7 */
	private boolean fShowTrailingSpaces= true;
	/** @since 3.7 */
	private boolean fShowLeadingIdeographicSpaces= true;
	/** @since 3.7 */
	private boolean fShowEnclosedIdeographicSpaces= true;
	/** @since 3.7 */
	private boolean fShowTrailingIdeographicSpaces= true;
	/** @since 3.7 */
	private boolean fShowLeadingTabs= true;
	/** @since 3.7 */
	private boolean fShowEnclosedTabs= true;
	/** @since 3.7 */
	private boolean fShowTrailingTabs= true;
	/** @since 3.7 */
	private boolean fShowCarriageReturn= true;
	/** @since 3.7 */
	private boolean fShowLineFeed= true;
	/** @since 3.7 */
	private int fAlpha= 80;

	/**
	 * Creates a new painter for the given text viewer.
	 *
	 * @param textViewer  the text viewer the painter should be attached to
	 */
	public WhitespaceCharacterPainter(ITextViewer textViewer) {
		super();
		fTextViewer= textViewer;
		fTextWidget= textViewer.getTextWidget();
		GC gc= new GC(fTextWidget);
		gc.setAdvanced(true);
		fIsAdvancedGraphicsPresent= gc.getAdvanced();
		gc.dispose();
		fIsFullSelectionStyle= (fTextWidget.getStyle() & SWT.FULL_SELECTION) != SWT.NONE;
	}

	/**
	 * Creates a new painter for the given text viewer and the painter options.
	 * 
	 * @param viewer the text viewer the painter should be attached to
	 * @param showLeadingSpaces if <code>true</code>, show leading Spaces
	 * @param showEnclosedSpaces if <code>true</code>, show enclosed Spaces
	 * @param showTrailingSpaces if <code>true</code>, show trailing Spaces
	 * @param showLeadingIdeographicSpaces if <code>true</code>, show leading Ideographic Spaces
	 * @param showEnclosedIdeographicSpaces if <code>true</code>, show enclosed Ideographic Spaces
	 * @param showTrailingIdeographicSpace if <code>true</code>, show trailing Ideographic Spaces
	 * @param showLeadingTabs if <code>true</code>, show leading Tabs
	 * @param showEnclosedTabs if <code>true</code>, show enclosed Tabs
	 * @param showTrailingTabs if <code>true</code>, show trailing Tabs
	 * @param showCarriageReturn if <code>true</code>, show Carriage Returns
	 * @param showLineFeed if <code>true</code>, show Line Feeds
	 * @param alpha the alpha value
	 * @since 3.7
	 */
	public WhitespaceCharacterPainter(ITextViewer viewer, boolean showLeadingSpaces, boolean showEnclosedSpaces, boolean showTrailingSpaces, boolean showLeadingIdeographicSpaces,
			boolean showEnclosedIdeographicSpaces, boolean showTrailingIdeographicSpace, boolean showLeadingTabs,
			boolean showEnclosedTabs, boolean showTrailingTabs, boolean showCarriageReturn, boolean showLineFeed, int alpha) {
		this(viewer);
		fShowLeadingSpaces= showLeadingSpaces;
		fShowEnclosedSpace= showEnclosedSpaces;
		fShowTrailingSpaces= showTrailingSpaces;
		fShowLeadingIdeographicSpaces= showLeadingIdeographicSpaces;
		fShowEnclosedIdeographicSpaces= showEnclosedIdeographicSpaces;
		fShowTrailingIdeographicSpaces= showTrailingIdeographicSpace;
		fShowLeadingTabs= showLeadingTabs;
		fShowEnclosedTabs= showEnclosedTabs;
		fShowTrailingTabs= showTrailingTabs;
		fShowCarriageReturn= showCarriageReturn;
		fShowLineFeed= showLineFeed;
		fAlpha= alpha;
	}

	@Override
	public void dispose() {
		fTextViewer= null;
		fTextWidget= null;
	}

	@Override
	public void paint(int reason) {
		IDocument document= fTextViewer.getDocument();
		if (document == null) {
			deactivate(false);
			return;
		}
		if (!fIsActive) {
			fIsActive= true;
			fTextWidget.addPaintListener(this);
			redrawAll();
		} else if (reason == CONFIGURATION || reason == INTERNAL) {
			redrawAll();
		}
	}

	@Override
	public void deactivate(boolean redraw) {
		if (fIsActive) {
			fIsActive= false;
			fTextWidget.removePaintListener(this);
			if (redraw) {
				redrawAll();
			}
		}
	}

	@Override
	public void setPositionManager(IPaintPositionManager manager) {
		// no need for a position manager
	}

	@Override
	public void paintControl(PaintEvent event) {
		if (fTextWidget != null) {
			handleDrawRequest(event.gc, event.x, event.y, event.width, event.height);
		}
	}

	/*
	 * Draw characters in view range.
	 */
	private void handleDrawRequest(GC gc, int x, int y, int w, int h) {
		int startLine= fTextWidget.getLineIndex(y);
		int endLine= fTextWidget.getLineIndex(y + h - 1);
		if (startLine <= endLine && startLine < fTextWidget.getLineCount()) {
			
			// avoid painting into the margins:
			Rectangle clipping= gc.getClipping();
			Rectangle clientArea= fTextWidget.getClientArea();
			int leftMargin= fTextWidget.getLeftMargin();
			int rightMargin= fTextWidget.getRightMargin();
			clientArea.x+= leftMargin;
			clientArea.width-= leftMargin + rightMargin;
			clipping.intersect(clientArea);
			gc.setClipping(clientArea);
			if (fIsAdvancedGraphicsPresent) {
				int alpha= gc.getAlpha();
				gc.setAlpha(fAlpha);
				drawLineRange(gc, startLine, endLine, x, w);
				gc.setAlpha(alpha);
			} else {
				drawLineRange(gc, startLine, endLine, x, w);
			}
			gc.setClipping(clipping);
		}
	}

	/**
	 * Draw the given line range.
	 *
	 * @param gc the GC
	 * @param startLine first line number
	 * @param endLine last line number (inclusive)
	 * @param x the X-coordinate of the drawing range
	 * @param w the width of the drawing range
	 */
	private void drawLineRange(GC gc, int startLine, int endLine, int x, int w) {
		final int viewPortWidth= fTextWidget.getClientArea().width;
		for (int line= startLine; line <= endLine; line++) {
			int lineOffset= fTextWidget.getOffsetAtLine(line);
			// line end offset including line delimiter
			int lineEndOffset;
			if (line < fTextWidget.getLineCount() - 1) {
				lineEndOffset= fTextWidget.getOffsetAtLine(line + 1);
			} else {
				lineEndOffset= fTextWidget.getCharCount();
			}
			// line length excluding line delimiter
			int lineLength= lineEndOffset - lineOffset;
			while (lineLength > 0) {
				char c= fTextWidget.getTextRange(lineOffset + lineLength - 1, 1).charAt(0);
				if (c != '\r' && c != '\n') {
					break;
				}
				--lineLength;
			}
			// compute coordinates of last character on line
			Point endOfLine= fTextWidget.getLocationAtOffset(lineOffset + lineLength);
			if (x - endOfLine.x > viewPortWidth) {
				// line is not visible
				continue;
			}
			// Y-coordinate of line
			int y= fTextWidget.getLinePixel(line);
			// compute first visible char offset
			int startOffset;
			try {
				startOffset= fTextWidget.getOffsetAtLocation(new Point(x, y)) - 1;
				if (startOffset - 2 <= lineOffset) {
					startOffset= lineOffset;
				}
			} catch (IllegalArgumentException iae) {
				startOffset= lineOffset;
			}
			// compute last visible char offset
			int endOffset;
			if (x + w >= endOfLine.x) {
				// line end is visible
				endOffset= lineEndOffset;
			} else {
				try {
					endOffset= fTextWidget.getOffsetAtLocation(new Point(x + w - 1, y)) + 1;
					if (endOffset + 2 >= lineEndOffset) {
						endOffset= lineEndOffset;
					}
				} catch (IllegalArgumentException iae) {
					endOffset= lineEndOffset;
				}
			}
			// draw character range
			if (endOffset > startOffset) {
				drawCharRange(gc, startOffset, endOffset, lineOffset, lineEndOffset);
			}
		}
	}

	private boolean isWhitespaceCharacter(char c) {
		return c == ' ' || c == '\u3000' || c == '\t' || c == '\r' || c == '\n';
	}

	/**
	 * Draw characters of content range.
	 * 
	 * @param gc the GC
	 * @param startOffset inclusive start index of the drawing range
	 * @param endOffset exclusive end index of the drawing range
	 * @param lineOffset inclusive start index of the line
	 * @param lineEndOffset exclusive end index of the line
	 */
	private void drawCharRange(GC gc, int startOffset, int endOffset, int lineOffset, int lineEndOffset) {
		StyledTextContent content= fTextWidget.getContent();
		String lineText= content.getTextRange(lineOffset, lineEndOffset - lineOffset);
		int startOffsetInLine= startOffset - lineOffset;
		int endOffsetInLine= endOffset - lineOffset;

		int textBegin= -1;
		for (int i= 0; i < lineText.length(); ++i) {
			if (!isWhitespaceCharacter(lineText.charAt(i))) {
				textBegin= i;
				break;
			}
		}
		boolean isEmptyLine= textBegin == -1;
		int textEnd= lineText.length() - 1;
		if (!isEmptyLine) {
			for (int i= lineText.length() - 1; i >= 0; --i) {
				if (!isWhitespaceCharacter(lineText.charAt(i))) {
					textEnd= i;
					break;
				}
			}
		}

		StyleRange styleRange= null;
		Color fg= null;
		StringBuffer visibleChar= new StringBuffer(10);
		for (int textOffset= startOffsetInLine; textOffset <= endOffsetInLine; ++textOffset) {
			int delta= 0;
			boolean eol= false;
			if (textOffset < endOffsetInLine) {
				delta= 1;
				char c= lineText.charAt(textOffset);
				switch (c) {
					case ' ':
						if (isEmptyLine) {
							if (fShowLeadingSpaces || fShowEnclosedSpace || fShowTrailingSpaces) {
								visibleChar.append(SPACE_SIGN);
							}
						} else if (textOffset < textBegin) {
							if (fShowLeadingSpaces) {
								visibleChar.append(SPACE_SIGN);
							}
						} else if (textOffset < textEnd) {
							if (fShowEnclosedSpace) {
								visibleChar.append(SPACE_SIGN);
							}
						} else {
							if (fShowTrailingSpaces) {
								visibleChar.append(SPACE_SIGN);
							}
						}
						// 'continue' would improve performance but may produce drawing errors
						// for long runs of space if width of space and dot differ
						break;
					case '\u3000': // ideographic whitespace
						if (isEmptyLine) {
							if (fShowLeadingIdeographicSpaces || fShowEnclosedIdeographicSpaces || fShowTrailingIdeographicSpaces) {
								visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
							}
						} else if (textOffset < textBegin) {
							if (fShowLeadingIdeographicSpaces) {
								visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
							}
						} else if (textOffset < textEnd) {
							if (fShowEnclosedIdeographicSpaces) {
								visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
							}
						} else {
							if (fShowTrailingIdeographicSpaces) {
								visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
							}
						}
						// 'continue' would improve performance but may produce drawing errors
						// for long runs of space if width of space and dot differ
						break;
					case '\t':
						if (isEmptyLine) {
							if (fShowLeadingTabs || fShowEnclosedTabs || fShowTrailingTabs) {
								visibleChar.append(TAB_SIGN);
							}
						} else if (textOffset < textBegin) {
							if (fShowLeadingTabs) {
								visibleChar.append(TAB_SIGN);
							}
						} else if (textOffset < textEnd) {
							if (fShowEnclosedTabs) {
								visibleChar.append(TAB_SIGN);
							}
						} else {
							if (fShowTrailingTabs) {
								visibleChar.append(TAB_SIGN);
							}
						}
						break;
					case '\r':
						if (fShowCarriageReturn) {
							visibleChar.append(CARRIAGE_RETURN_SIGN);
						}
						if (textOffset >= endOffsetInLine - 1 || lineText.charAt(textOffset + 1) != '\n') {
							eol= true;
							break;
						}
						continue;
					case '\n':
						if (fShowLineFeed) {
							visibleChar.append(LINE_FEED_SIGN);
						}
						eol= true;
						break;
					default:
						delta= 0;
						break;
				}
			}
			if (visibleChar.length() > 0) {
				int widgetOffset= startOffset + textOffset - startOffsetInLine - visibleChar.length() + delta;
				if (!eol || !isFoldedLine(content.getLineAtOffset(widgetOffset))) {
					/*
					 * Block selection is drawn using alpha and no selection-inverting
					 * takes place, we always draw as 'unselected' in block selection mode.
					 */
					if (!fTextWidget.getBlockSelection() && fIsFullSelectionStyle && isOffsetSelected(fTextWidget, widgetOffset)) {
						fg= fTextWidget.getSelectionForeground();
					} else if (styleRange == null || styleRange.start + styleRange.length <= widgetOffset) {
						styleRange= fTextWidget.getStyleRangeAtOffset(widgetOffset);
						if (styleRange == null || styleRange.foreground == null) {
							fg= fTextWidget.getForeground();
						} else {
							fg= styleRange.foreground;
						}
					}
					draw(gc, widgetOffset, visibleChar.toString(), fg);
				}
				visibleChar.delete(0, visibleChar.length());
			}
		}
	}
	
	/**
	 * Returns <code>true</code> if <code>offset</code> is selection in <code>widget</code>,
	 * <code>false</code> otherwise.
	 * 
	 * @param widget the widget
	 * @param offset the offset
	 * @return <code>true</code> if <code>offset</code> is selection, <code>false</code> otherwise
	 * @since 3.5
	 */
	private static final boolean isOffsetSelected(StyledText widget, int offset) {
		Point selection= widget.getSelection();
		return offset >= selection.x && offset < selection.y;
	}

	/**
	 * Check if the given widget line is a folded line.
	 *
	 * @param widgetLine  the widget line number
	 * @return <code>true</code> if the line is folded
	 */
	private boolean isFoldedLine(int widgetLine) {
		if (fTextViewer instanceof ITextViewerExtension5) {
			ITextViewerExtension5 extension= (ITextViewerExtension5)fTextViewer;
			int modelLine= extension.widgetLine2ModelLine(widgetLine);
			int widgetLine2= extension.modelLine2WidgetLine(modelLine + 1);
			return widgetLine2 == -1;
		}
		return false;
	}

	/**
	 * Redraw all of the text widgets visible content.
	 */
	private void redrawAll() {
		fTextWidget.redraw();
	}

	/**
	 * Draw string at widget offset.
	 *
	 * @param gc the GC
	 * @param offset the widget offset
	 * @param s the string to be drawn
	 * @param fg the foreground color
	 */
	private void draw(GC gc, int offset, String s, Color fg) {
		// Compute baseline delta (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=165640)
		int baseline= fTextWidget.getBaseline(offset);
		FontMetrics fontMetrics= gc.getFontMetrics();
		int fontBaseline= fontMetrics.getAscent() + fontMetrics.getLeading();
		int baslineDelta= baseline - fontBaseline;

		Point pos= fTextWidget.getLocationAtOffset(offset);
		gc.setForeground(fg);
		gc.drawString(s, pos.x, pos.y + baslineDelta, true);
	}

}

Back to the top