Skip to main content
summaryrefslogtreecommitdiffstats
blob: d0cfc19e0db4f4e49dfc9361bec7cf2771ef8592 (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
/*******************************************************************************
 * Copyright (c) 2008, 2016 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.internal.text;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;


/**
 * Adds owner draw support for tables.
 *
 * @since 3.4
 */
public class TableOwnerDrawSupport implements Listener {

	private static final String STYLED_RANGES_KEY= "styled_ranges"; //$NON-NLS-1$

	// shared text layout
	private TextLayout fSharedLayout;

	private int fDeltaOfLastMeasure;

	public static void install(Table table) {
		TableOwnerDrawSupport listener= new TableOwnerDrawSupport(table);
		table.addListener(SWT.Dispose, listener);
		table.addListener(SWT.MeasureItem, listener);
		table.addListener(SWT.EraseItem, listener);
		table.addListener(SWT.PaintItem, listener);
	}

	/**
	 * Stores the styled ranges in the given table item.
	 *
	 * @param item table item
	 * @param column the column index
	 * @param ranges the styled ranges or <code>null</code> to remove them
	 */
	public static void storeStyleRanges(TableItem item, int column, StyleRange[] ranges) {
		item.setData(STYLED_RANGES_KEY + column, ranges);
	}

	/**
	 * Returns the styled ranges which are stored in the given table item.
	 *
	 * @param item table item
	 * @param column the column index
	 * @return the styled ranges
	 */
	private static StyleRange[] getStyledRanges(TableItem item, int column) {
		return (StyleRange[])item.getData(STYLED_RANGES_KEY + column);
	}

	private TableOwnerDrawSupport(Table table) {
		int orientation= table.getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
		fSharedLayout= new TextLayout(table.getDisplay());
		fSharedLayout.setOrientation(orientation);
	}

	@Override
	public void handleEvent(Event event) {
		switch (event.type) {
			case SWT.MeasureItem:
				measureItem(event);
				break;
			case SWT.EraseItem:
				event.detail &= ~SWT.FOREGROUND;
				break;
			case SWT.PaintItem:
				performPaint(event);
				break;
			case SWT.Dispose:
				widgetDisposed();
				break;
		}
	}

	/**
	 * Handles the measure event.
	 *
	 * @param event the measure event
	 */
	private void measureItem(Event event) {
		boolean isSelected= (event.detail & SWT.SELECTED) != 0;
		fDeltaOfLastMeasure= updateTextLayout((TableItem) event.item, event.index, isSelected);
		event.width+= fDeltaOfLastMeasure;
	}

	private int updateTextLayout(TableItem item, int index, boolean isSelected) {
		fSharedLayout.setFont(item.getFont(index));

		// XXX: needed to clear the style info, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=226090
		fSharedLayout.setText(""); //$NON-NLS-1$

		fSharedLayout.setText(item.getText(index));

		int originalTextWidth= fSharedLayout.getBounds().width; // text width without any styles

		StyleRange[] ranges= getStyledRanges(item, index);
		if (ranges != null) {
			for (int i= 0; i < ranges.length; i++) {
				StyleRange curr= ranges[i];
				if (isSelected) {
					curr= (StyleRange) curr.clone();
					curr.foreground= null;
					curr.background= null;
				}
				fSharedLayout.setStyle(curr, curr.start, curr.start + curr.length - 1);
			}
		}

		return fSharedLayout.getBounds().width - originalTextWidth;
	}

	/**
	 * Performs the paint operation.
	 *
	 * @param event the event
	 */
	private void performPaint(Event event) {
		TableItem item= (TableItem) event.item;
		GC gc= event.gc;
		int index= event.index;

		boolean isSelected= (event.detail & SWT.SELECTED) != 0;

		// Remember colors to restore the GC later
		Color oldForeground= gc.getForeground();
		Color oldBackground= gc.getBackground();

		if (!isSelected) {
			Color foreground= item.getForeground(index);
			gc.setForeground(foreground);

			Color background= item.getBackground(index);
			gc.setBackground(background);
		}

		Image image=item.getImage(index);
		if (image != null) {
			Rectangle imageBounds=item.getImageBounds(index);
			Rectangle bounds=image.getBounds();
			int x=imageBounds.x + Math.max(0, (imageBounds.width - bounds.width) / 2);
			int y=imageBounds.y + Math.max(0, (imageBounds.height - bounds.height) / 2);
			gc.drawImage(image, x, y);
		}

		// fSharedLayout already configured in measureItem(Event)
		Rectangle textBounds=item.getTextBounds(index);
		if (textBounds != null) {
			Rectangle layoutBounds=fSharedLayout.getBounds();
			int x=textBounds.x;
			int y=textBounds.y + Math.max(0, (textBounds.height - layoutBounds.height) / 2);
			fSharedLayout.draw(gc, x, y);
		}

		if ((event.detail & SWT.FOCUSED) != 0) {
			Rectangle focusBounds=item.getBounds();
			gc.drawFocus(focusBounds.x, focusBounds.y, focusBounds.width + fDeltaOfLastMeasure, focusBounds.height);
		}

		if (!isSelected) {
			gc.setForeground(oldForeground);
			gc.setBackground(oldBackground);
		}
	}

	private void widgetDisposed() {
		fSharedLayout.dispose();
	}
}

Back to the top