129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.TableOwnerDrawSupport;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.custom.StyleRange;
|
|
19 import dwt.graphics.Color;
|
|
20 import dwt.graphics.GC;
|
|
21 import dwt.graphics.Image;
|
|
22 import dwt.graphics.Rectangle;
|
|
23 import dwt.graphics.TextLayout;
|
|
24 import dwt.widgets.Event;
|
|
25 import dwt.widgets.Listener;
|
|
26 import dwt.widgets.Table;
|
|
27 import dwt.widgets.TableItem;
|
|
28
|
|
29
|
|
30 /**
|
|
31 * Adds owner draw support for tables.
|
|
32 *
|
|
33 * @since 3.4
|
|
34 */
|
|
35 public class TableOwnerDrawSupport : Listener {
|
|
36
|
|
37 private static final String STYLED_RANGES_KEY= "styled_ranges"; //$NON-NLS-1$
|
|
38
|
|
39 private TextLayout fLayout;
|
|
40
|
|
41 public static void install(Table table) {
|
|
42 TableOwnerDrawSupport listener= new TableOwnerDrawSupport(table);
|
|
43 table.addListener(DWT.Dispose, listener);
|
|
44 table.addListener(DWT.MeasureItem, listener);
|
|
45 table.addListener(DWT.EraseItem, listener);
|
|
46 table.addListener(DWT.PaintItem, listener);
|
|
47 }
|
|
48
|
|
49 /**
|
|
50 * Stores the styled ranges in the given table item.
|
|
51 *
|
|
52 * @param item table item
|
|
53 * @param column the column index
|
|
54 * @param ranges the styled ranges or <code>null</code> to remove them
|
|
55 */
|
|
56 public static void storeStyleRanges(TableItem item, int column, StyleRange[] ranges) {
|
|
57 item.setData(STYLED_RANGES_KEY + column, ranges);
|
|
58 }
|
|
59
|
|
60 /**
|
|
61 * Returns the styled ranges which are stored in the given table item.
|
|
62 *
|
|
63 * @param item table item
|
|
64 * @param column the column index
|
|
65 * @return the styled ranges
|
|
66 */
|
|
67 private static StyleRange[] getStyledRanges(TableItem item, int column) {
|
|
68 return (StyleRange[])item.getData(STYLED_RANGES_KEY + column);
|
|
69 }
|
|
70
|
|
71 private TableOwnerDrawSupport(Table table) {
|
|
72 int orientation= table.getStyle() & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT);
|
|
73 fLayout= new TextLayout(table.getDisplay());
|
|
74 fLayout.setOrientation(orientation);
|
|
75 }
|
|
76
|
|
77 /*
|
|
78 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event)
|
|
79 */
|
|
80 public void handleEvent(Event event) {
|
|
81 switch (event.type) {
|
|
82 case DWT.MeasureItem:
|
|
83 break;
|
|
84 case DWT.EraseItem:
|
|
85 event.detail &= ~DWT.FOREGROUND;
|
|
86 break;
|
|
87 case DWT.PaintItem:
|
|
88 performPaint(event);
|
|
89 break;
|
|
90 case DWT.Dispose:
|
|
91 widgetDisposed();
|
|
92 break;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Performs the paint operation.
|
|
98 *
|
|
99 * @param event the event
|
|
100 */
|
|
101 private void performPaint(Event event) {
|
|
102 TableItem item= (TableItem) event.item;
|
|
103 GC gc= event.gc;
|
|
104 int index= event.index;
|
|
105
|
|
106 bool isSelected= (event.detail & DWT.SELECTED) !is 0;
|
|
107
|
|
108 // Remember colors to restore the GC later
|
|
109 Color oldForeground= gc.getForeground();
|
|
110 Color oldBackground= gc.getBackground();
|
|
111
|
|
112 if (!isSelected) {
|
|
113 Color foreground= item.getForeground(index);
|
|
114 gc.setForeground(foreground);
|
|
115
|
|
116 Color background= item.getBackground(index);
|
|
117 gc.setBackground(background);
|
|
118 }
|
|
119
|
|
120 Image image=item.getImage(index);
|
|
121 if (image !is null) {
|
|
122 Rectangle imageBounds=item.getImageBounds(index);
|
|
123 Rectangle bounds=image.getBounds();
|
|
124 int x=imageBounds.x + Math.max(0, (imageBounds.width - bounds.width) / 2);
|
|
125 int y=imageBounds.y + Math.max(0, (imageBounds.height - bounds.height) / 2);
|
|
126 gc.drawImage(image, x, y);
|
|
127 }
|
|
128
|
|
129 fLayout.setFont(item.getFont(index));
|
|
130
|
|
131 // XXX: needed to clear the style info, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=226090
|
|
132 fLayout.setText(""); //$NON-NLS-1$
|
|
133
|
|
134 fLayout.setText(item.getText(index));
|
|
135
|
|
136 StyleRange[] ranges= getStyledRanges(item, index);
|
|
137 if (ranges !is null) {
|
|
138 for (int i= 0; i < ranges.length; i++) {
|
|
139 StyleRange curr= ranges[i];
|
|
140 if (isSelected) {
|
|
141 curr= (StyleRange) curr.clone();
|
|
142 curr.foreground= null;
|
|
143 curr.background= null;
|
|
144 }
|
|
145 fLayout.setStyle(curr, curr.start, curr.start + curr.length - 1);
|
|
146 }
|
|
147 }
|
|
148
|
|
149 Rectangle textBounds=item.getTextBounds(index);
|
|
150 if (textBounds !is null) {
|
|
151 Rectangle layoutBounds=fLayout.getBounds();
|
|
152 int x=textBounds.x;
|
|
153 int y=textBounds.y + Math.max(0, (textBounds.height - layoutBounds.height) / 2);
|
|
154 fLayout.draw(gc, x, y);
|
|
155 }
|
|
156
|
|
157 if ((event.detail & DWT.FOCUSED) !is 0) {
|
|
158 Rectangle focusBounds=item.getBounds();
|
|
159 gc.drawFocus(focusBounds.x, focusBounds.y, focusBounds.width, focusBounds.height);
|
|
160 }
|
|
161
|
|
162 if (!isSelected) {
|
|
163 gc.setForeground(oldForeground);
|
|
164 gc.setBackground(oldBackground);
|
|
165 }
|
|
166 }
|
|
167
|
|
168 private void widgetDisposed() {
|
|
169 fLayout.dispose();
|
|
170 }
|
|
171 }
|
|
172
|