Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b57cd30f1855addde08c64c1251dcf8775880e38 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 Wind River Systems 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.internal.ui.disassembly;

import java.util.Iterator;

import org.eclipse.cdt.dsf.debug.internal.ui.disassembly.model.DisassemblyDocument;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.IVerticalRulerColumn;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;

/**
 * DisassemblyViewer
 */
public class DisassemblyViewer extends SourceViewer {

	class ResizeListener implements ControlListener {
		/*
		 * @see ControlListener#controlResized(ControlEvent)
		 */
		public void controlResized(ControlEvent e) {
			updateViewportListeners(RESIZE);
		}
		/*
		 * @see ControlListener#controlMoved(ControlEvent)
		 */
		public void controlMoved(ControlEvent e) {
		}
	}
	
	private boolean fUserTriggeredScrolling;
	private int fCachedLastTopPixel;
	
	// extra resize listener to workaround bug 171018
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=171018
	private ResizeListener fResizeListener;

	/**
	 * Create a new DisassemblyViewer.
	 * @param parent
	 * @param ruler
	 * @param overviewRuler
	 * @param showsAnnotationOverview
	 * @param styles
	 */
	public DisassemblyViewer(Composite parent, IVerticalRuler ruler, IOverviewRuler overviewRuler, boolean showsAnnotationOverview, int styles) {
		super(parent, ruler, overviewRuler, showsAnnotationOverview, styles);
		// always readonly
		setEditable(false);
	}

	/*
	 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
	 */
	@Override
	protected void createControl(Composite parent, int styles) {
		super.createControl(parent, styles);
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=171018
		getTextWidget().addControlListener(fResizeListener= new ResizeListener());
	}

	/*
	 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
	 */
	@Override
	protected void handleDispose() {
		if (fResizeListener != null) {
			getTextWidget().removeControlListener(fResizeListener);
		}
		super.handleDispose();
	}

	/*
	 * @see org.eclipse.jface.text.source.SourceViewer#doOperation(int)
	 */
	@Override
	public void doOperation(int operation) {
		switch (operation) {
		case COPY:
			StyledText textWidget = getTextWidget();
			if (textWidget == null || !redraws()) {
				return;
			}
			if (textWidget.getSelectionCount() == 0) {
				return;
			}
			String selectedText;
			try {
				selectedText = getSelectedText();
			} catch (BadLocationException e) {
				// should not happend
				DsfUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, e.getLocalizedMessage(), e));
				return;
			}
			Clipboard clipboard = new Clipboard(textWidget.getDisplay());
			clipboard.setContents(new Object[] { selectedText }, new Transfer[] { TextTransfer.getInstance() });
			clipboard.dispose();
			break;
		default:
			super.doOperation(operation);
		}
	}

	/**
	 * Get the selected text together with text displayed in visible
	 * ruler columns.
	 * @return  the selected text
	 * @throws BadLocationException
	 */
	public String getSelectedText() throws BadLocationException {
		StringBuffer text = new StringBuffer(200);
		String lineSeparator = System.getProperty("line.separator"); //$NON-NLS-1$
		DisassemblyDocument doc = (DisassemblyDocument)getDocument();
		Point selection = getSelectedRange();
		int startOffset = selection.x;
		int length = selection.y;
		int endOffset = startOffset + length;
		int startLine = doc.getLineOfOffset(startOffset);
		int endLine = doc.getLineOfOffset(endOffset);
		int firstLineOffset = startOffset - doc.getLineOffset(startLine);
		if (firstLineOffset > 0) {
			// partial first line
			int lineLength = doc.getLineInformation(startLine).getLength();
			text.append(doc.get(startOffset, Math.min(lineLength - firstLineOffset, length)));
			++startLine;
			if (startLine <= endLine) {
				text.append(lineSeparator);
			}
		}
		for (int line = startLine; line < endLine; ++line) {
			String lineText = getLineText(line);
			text.append(lineText);
			text.append(lineSeparator);
		}
		if (doc.getLineOffset(endLine) < endOffset) {
			// partial last line
			if (startLine <= endLine) {
				int lineStart = doc.getLineOffset(endLine);
				text.append(getLinePrefix(endLine));
				text.append(doc.get(lineStart, endOffset - lineStart));
			}
		}
		return text.toString();
	}

	/**
	 * Return the content of the given line, excluding line separator.
	 * @param line  the line number
	 * @return the line content
	 * @throws BadLocationException
	 */
	public String getLineText(int line) throws BadLocationException {
		IDocument doc = getDocument();
		IRegion lineRegion = doc.getLineInformation(line);
		return getLinePrefix(line) + doc.get(lineRegion.getOffset(), lineRegion.getLength());
	}

	/**
	 * Get the line prefix by concatenating the text displayed by
	 * the visible ruler columns.
	 * @param line  the line number
	 * @return the prefix string with trailing blank or the empty string
	 */
	public String getLinePrefix(int line) {
		StringBuffer prefix = new StringBuffer(10);
		IVerticalRuler ruler = getVerticalRuler();
		if (ruler instanceof CompositeRuler) {
			for (Iterator<?> iter = ((CompositeRuler)ruler).getDecoratorIterator(); iter.hasNext();) {
				IVerticalRulerColumn column = (IVerticalRulerColumn) iter.next();
				if (column instanceof DisassemblyRulerColumn) {
					DisassemblyRulerColumn disassColumn = (DisassemblyRulerColumn)column;
					String columnText = disassColumn.createDisplayString(line);
					prefix.append(columnText);
					int columnWidth = disassColumn.computeNumberOfCharacters();
					columnWidth -= columnText.length();
					while(columnWidth-- > 0)
						prefix.append(' ');
					prefix.append(' ');
				}
			}
		}
		return prefix.toString();
	}

	/**
	 * Scroll the given position into the visible area if it is not yet visible.
	 * @param offset
	 * @see org.eclipse.jface.text.TextViewer#revealRange(int, int)
	 */
	public void revealOffset(int offset, boolean onTop) {
		try {
			IDocument doc = getVisibleDocument();

			int focusLine = doc.getLineOfOffset(offset);

			StyledText textWidget = getTextWidget();
			int top = textWidget.getTopIndex();
			if (top > -1) {

				// scroll vertically
				int lines = getEstimatedVisibleLinesInViewport();
				int bottom = top + lines;

				int bottomBuffer = Math.max(1, lines / 3);
				
				if (!onTop && focusLine >= top && focusLine <= bottom - bottomBuffer) {
					// do not scroll at all as it is already visible
				} else {
					if (focusLine > bottom - bottomBuffer && focusLine <= bottom) {
						// focusLine is already in bottom bufferZone
						// scroll to top of bottom bufferzone - for smooth down-scrolling
						int scrollDelta = focusLine - (bottom - bottomBuffer);
						textWidget.setTopIndex(top + scrollDelta);
					} else {
						// scroll to top of visible area minus buffer zone
						int topBuffer = onTop ? 0 : lines / 3;
						textWidget.setTopIndex(Math.max(0, focusLine - topBuffer));
					}
					updateViewportListeners(INTERNAL);
				}
			}
		} catch (BadLocationException ble) {
			throw new IllegalArgumentException(ble.getLocalizedMessage());
		}
	}

	/**
	 * @return the number of visible lines in the viewport assuming a constant
	 *         line height.
	 */
	private int getEstimatedVisibleLinesInViewport() {
		StyledText textWidget = getTextWidget();
		if (textWidget != null) {
			Rectangle clArea= textWidget.getClientArea();
			if (!clArea.isEmpty())
				return clArea.height / textWidget.getLineHeight();
		}
		return -1;
	}

	int getLastTopPixel() {
		return fCachedLastTopPixel;
	}
	boolean isUserTriggeredScrolling() {
		return fUserTriggeredScrolling;
	}

	/*
	 * @see org.eclipse.jface.text.TextViewer#updateViewportListeners(int)
	 */
	@Override
	protected void updateViewportListeners(int origin) {
		fCachedLastTopPixel = fLastTopPixel;
		fUserTriggeredScrolling = origin != INTERNAL && origin != RESIZE;
		super.updateViewportListeners(origin);
	}
	
}

Back to the top