Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/TableOwnerDrawSupport.d @ 134:51e6e63f930e
Regex fix for casts
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 01:46:20 +0200 |
parents | 7d818bd32d63 |
children | 000f9136b8f7 |
rev | line source |
---|---|
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 | |
131 | 15 import dwtx.jface.internal.text.NonDeletingPositionUpdater; // packageimport |
16 import dwtx.jface.internal.text.InternalAccessor; // packageimport | |
17 import dwtx.jface.internal.text.StickyHoverManager; // packageimport | |
18 import dwtx.jface.internal.text.InformationControlReplacer; // packageimport | |
19 import dwtx.jface.internal.text.DelayedInputChangeListener; // packageimport | |
20 | |
21 | |
129 | 22 import dwt.dwthelper.utils; |
23 | |
24 import dwt.DWT; | |
25 import dwt.custom.StyleRange; | |
26 import dwt.graphics.Color; | |
27 import dwt.graphics.GC; | |
28 import dwt.graphics.Image; | |
29 import dwt.graphics.Rectangle; | |
30 import dwt.graphics.TextLayout; | |
31 import dwt.widgets.Event; | |
32 import dwt.widgets.Listener; | |
33 import dwt.widgets.Table; | |
34 import dwt.widgets.TableItem; | |
35 | |
36 | |
37 /** | |
38 * Adds owner draw support for tables. | |
39 * | |
40 * @since 3.4 | |
41 */ | |
42 public class TableOwnerDrawSupport : Listener { | |
43 | |
44 private static final String STYLED_RANGES_KEY= "styled_ranges"; //$NON-NLS-1$ | |
45 | |
46 private TextLayout fLayout; | |
47 | |
48 public static void install(Table table) { | |
49 TableOwnerDrawSupport listener= new TableOwnerDrawSupport(table); | |
50 table.addListener(DWT.Dispose, listener); | |
51 table.addListener(DWT.MeasureItem, listener); | |
52 table.addListener(DWT.EraseItem, listener); | |
53 table.addListener(DWT.PaintItem, listener); | |
54 } | |
55 | |
56 /** | |
57 * Stores the styled ranges in the given table item. | |
58 * | |
59 * @param item table item | |
60 * @param column the column index | |
61 * @param ranges the styled ranges or <code>null</code> to remove them | |
62 */ | |
63 public static void storeStyleRanges(TableItem item, int column, StyleRange[] ranges) { | |
64 item.setData(STYLED_RANGES_KEY + column, ranges); | |
65 } | |
66 | |
67 /** | |
68 * Returns the styled ranges which are stored in the given table item. | |
69 * | |
70 * @param item table item | |
71 * @param column the column index | |
72 * @return the styled ranges | |
73 */ | |
74 private static StyleRange[] getStyledRanges(TableItem item, int column) { | |
75 return (StyleRange[])item.getData(STYLED_RANGES_KEY + column); | |
76 } | |
77 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
78 private this(Table table) { |
129 | 79 int orientation= table.getStyle() & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT); |
80 fLayout= new TextLayout(table.getDisplay()); | |
81 fLayout.setOrientation(orientation); | |
82 } | |
83 | |
84 /* | |
85 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event) | |
86 */ | |
87 public void handleEvent(Event event) { | |
88 switch (event.type) { | |
89 case DWT.MeasureItem: | |
90 break; | |
91 case DWT.EraseItem: | |
92 event.detail &= ~DWT.FOREGROUND; | |
93 break; | |
94 case DWT.PaintItem: | |
95 performPaint(event); | |
96 break; | |
97 case DWT.Dispose: | |
98 widgetDisposed(); | |
99 break; | |
100 } | |
101 } | |
102 | |
103 /** | |
104 * Performs the paint operation. | |
105 * | |
106 * @param event the event | |
107 */ | |
108 private void performPaint(Event event) { | |
134 | 109 TableItem item= cast(TableItem) event.item; |
129 | 110 GC gc= event.gc; |
111 int index= event.index; | |
112 | |
113 bool isSelected= (event.detail & DWT.SELECTED) !is 0; | |
114 | |
115 // Remember colors to restore the GC later | |
116 Color oldForeground= gc.getForeground(); | |
117 Color oldBackground= gc.getBackground(); | |
118 | |
119 if (!isSelected) { | |
120 Color foreground= item.getForeground(index); | |
121 gc.setForeground(foreground); | |
122 | |
123 Color background= item.getBackground(index); | |
124 gc.setBackground(background); | |
125 } | |
126 | |
127 Image image=item.getImage(index); | |
128 if (image !is null) { | |
129 Rectangle imageBounds=item.getImageBounds(index); | |
130 Rectangle bounds=image.getBounds(); | |
131 int x=imageBounds.x + Math.max(0, (imageBounds.width - bounds.width) / 2); | |
132 int y=imageBounds.y + Math.max(0, (imageBounds.height - bounds.height) / 2); | |
133 gc.drawImage(image, x, y); | |
134 } | |
135 | |
136 fLayout.setFont(item.getFont(index)); | |
137 | |
138 // XXX: needed to clear the style info, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=226090 | |
139 fLayout.setText(""); //$NON-NLS-1$ | |
140 | |
141 fLayout.setText(item.getText(index)); | |
142 | |
143 StyleRange[] ranges= getStyledRanges(item, index); | |
144 if (ranges !is null) { | |
145 for (int i= 0; i < ranges.length; i++) { | |
146 StyleRange curr= ranges[i]; | |
147 if (isSelected) { | |
134 | 148 curr= cast(StyleRange) curr.clone(); |
129 | 149 curr.foreground= null; |
150 curr.background= null; | |
151 } | |
152 fLayout.setStyle(curr, curr.start, curr.start + curr.length - 1); | |
153 } | |
154 } | |
155 | |
156 Rectangle textBounds=item.getTextBounds(index); | |
157 if (textBounds !is null) { | |
158 Rectangle layoutBounds=fLayout.getBounds(); | |
159 int x=textBounds.x; | |
160 int y=textBounds.y + Math.max(0, (textBounds.height - layoutBounds.height) / 2); | |
161 fLayout.draw(gc, x, y); | |
162 } | |
163 | |
164 if ((event.detail & DWT.FOCUSED) !is 0) { | |
165 Rectangle focusBounds=item.getBounds(); | |
166 gc.drawFocus(focusBounds.x, focusBounds.y, focusBounds.width, focusBounds.height); | |
167 } | |
168 | |
169 if (!isSelected) { | |
170 gc.setForeground(oldForeground); | |
171 gc.setBackground(oldBackground); | |
172 } | |
173 } | |
174 | |
175 private void widgetDisposed() { | |
176 fLayout.dispose(); | |
177 } | |
178 } | |
179 |