comparison dwtx/jface/viewers/FocusCellOwnerDrawHighlighter.d @ 70:46a6e0e6ccd4

Merge with d-fied sources of 3.4M7
author Frank Benoit <benoit@tionex.de>
date Thu, 22 May 2008 01:36:46 +0200
parents ea8ff534f622
children
comparison
equal deleted inserted replaced
69:07b9d96fd764 70:46a6e0e6ccd4
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2007 IBM Corporation and others. 2 * Copyright (c) 2007, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation
9 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation 10 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
10 * - fix for bug 183850, 182652 11 * - fix for bug 183850, 182652, 182800, 215069
11 * IBM Corporation - initial API and implementation
12 * Port to the D programming language: 12 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de> 13 * Frank Benoit <benoit@tionex.de>
14 ******************************************************************************/ 14 *******************************************************************************/
15 15
16 module dwtx.jface.viewers.FocusCellOwnerDrawHighlighter; 16 module dwtx.jface.viewers.FocusCellOwnerDrawHighlighter;
17 17
18 import dwtx.jface.viewers.FocusCellHighlighter; 18 import dwtx.jface.viewers.FocusCellHighlighter;
19 import dwtx.jface.viewers.ViewerCell; 19 import dwtx.jface.viewers.ViewerCell;
29 import dwtx.core.runtime.Assert; 29 import dwtx.core.runtime.Assert;
30 30
31 import dwt.dwthelper.utils; 31 import dwt.dwthelper.utils;
32 32
33 /** 33 /**
34 * A concrete implementation of {@link FocusCellHighlighter} using by setting
35 * the control into owner draw mode and highlighting the currently selected
36 * cell. To make the use this class you should create the control with the
37 * {@link DWT#FULL_SELECTION} bit set
38 *
39 * This class can be subclassed to configure how the coloring of the selected
40 * cell.
41 *
34 * @since 3.3 42 * @since 3.3
35 * 43 *
36 */ 44 */
37 public class FocusCellOwnerDrawHighlighter : FocusCellHighlighter { 45 public class FocusCellOwnerDrawHighlighter : FocusCellHighlighter {
38 46 /**
39 private ViewerCell oldCell; 47 * Create a new instance which can be passed to a
40 48 * {@link TreeViewerFocusCellManager}
41 // Needed to work-around problem in bug 183850 49 *
42 private static const bool WIN_32;
43
44 static this(){
45 WIN_32 = "win32".equals(DWT.getPlatform()); //$NON-NLS-1$
46 }
47
48 /**
49 * @param viewer 50 * @param viewer
50 * the viewer 51 * the viewer
51 */ 52 */
52 public this(ColumnViewer viewer) { 53 public this(ColumnViewer viewer) {
53 super(viewer); 54 super(viewer);
54 hookListener(viewer); 55 hookListener(viewer);
55 } 56 }
56 57
57 private void markFocusedCell(Event event, ViewerCell cell) { 58 private void markFocusedCell(Event event, ViewerCell cell) {
58 Color background = getSelectedCellBackgroundColor(cell); 59 Color background = (cell.getControl().isFocusControl()) ? getSelectedCellBackgroundColor(cell)
59 Color foreground = getSelectedCellForegroundColor(cell); 60 : getSelectedCellBackgroundColorNoFocus(cell);
60 61 Color foreground = (cell.getControl().isFocusControl()) ? getSelectedCellForegroundColor(cell)
61 if ( WIN_32 || foreground !is null || background !is null) { 62 : getSelectedCellForegroundColorNoFocus(cell);
63
64 if (foreground !is null || background !is null || onlyTextHighlighting(cell)) {
62 GC gc = event.gc; 65 GC gc = event.gc;
63 66
64 if (background is null) { 67 if (background is null) {
65 background = cell.getItem().getDisplay().getSystemColor( 68 background = cell.getItem().getDisplay().getSystemColor(
66 DWT.COLOR_LIST_SELECTION); 69 DWT.COLOR_LIST_SELECTION);
71 DWT.COLOR_LIST_SELECTION_TEXT); 74 DWT.COLOR_LIST_SELECTION_TEXT);
72 } 75 }
73 76
74 gc.setBackground(background); 77 gc.setBackground(background);
75 gc.setForeground(foreground); 78 gc.setForeground(foreground);
76 gc.fillRectangle(event.getBounds()); 79
77 80 if (onlyTextHighlighting(cell)) {
78 // This is a workaround for an DWT-Bug on WinXP bug 169517 81 Rectangle area = event.getBounds();
79 gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$ 82 Rectangle rect = cell.getTextBounds();
83 if( rect !is null ) {
84 area.x = rect.x;
85 }
86 gc.fillRectangle(area);
87 } else {
88 gc.fillRectangle(event.getBounds());
89 }
90
80 event.detail &= ~DWT.SELECTED; 91 event.detail &= ~DWT.SELECTED;
81 } 92 }
82 } 93 }
83 94
84 private void removeSelectionInformation(Event event, ViewerCell cell) { 95 private void removeSelectionInformation(Event event, ViewerCell cell) {
86 gc.setBackground(cell.getViewerRow().getBackground( 97 gc.setBackground(cell.getViewerRow().getBackground(
87 cell.getColumnIndex())); 98 cell.getColumnIndex()));
88 gc.setForeground(cell.getViewerRow().getForeground( 99 gc.setForeground(cell.getViewerRow().getForeground(
89 cell.getColumnIndex())); 100 cell.getColumnIndex()));
90 gc.fillRectangle(cell.getBounds()); 101 gc.fillRectangle(cell.getBounds());
91 // This is a workaround for an DWT-Bug on WinXP bug 169517
92 gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
93 event.detail &= ~DWT.SELECTED; 102 event.detail &= ~DWT.SELECTED;
94 } 103 }
95 104
96 private void hookListener(ColumnViewer viewer) { 105 private void hookListener(ColumnViewer viewer) {
97 106
121 }; 130 };
122 viewer.getControl().addListener(DWT.EraseItem, listener); 131 viewer.getControl().addListener(DWT.EraseItem, listener);
123 } 132 }
124 133
125 /** 134 /**
126 * @param cell 135 * The color to use when rendering the background of the selected cell when
127 * the cell which is colored 136 * the control has the input focus
128 * @return the color 137 *
138 * @param cell
139 * the cell which is colored
140 * @return the color or <code>null</code> to use the default
129 */ 141 */
130 protected Color getSelectedCellBackgroundColor(ViewerCell cell) { 142 protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
131 return null; 143 return null;
132 } 144 }
133 145
134 /** 146 /**
135 * @param cell 147 * The color to use when rendering the foreground (=text) of the selected
136 * the cell which is colored 148 * cell when the control has the input focus
137 * @return the color 149 *
150 * @param cell
151 * the cell which is colored
152 * @return the color or <code>null</code> to use the default
138 */ 153 */
139 protected Color getSelectedCellForegroundColor(ViewerCell cell) { 154 protected Color getSelectedCellForegroundColor(ViewerCell cell) {
140 return null; 155 return null;
141 } 156 }
142 157
143 /* 158 /**
144 * (non-Javadoc) 159 * The color to use when rendering the foreground (=text) of the selected
145 * 160 * cell when the control has <b>no</b> input focus
146 * @see dwtx.jface.viewers.FocusCellHighlighter#focusCellChanged(dwtx.jface.viewers.ViewerCell) 161 *
147 */ 162 * @param cell
148 protected override void focusCellChanged(ViewerCell cell) { 163 * the cell which is colored
149 super.focusCellChanged(cell); 164 * @return the color or <code>null</code> to use the same used when
165 * control has focus
166 * @since 3.4
167 */
168 protected Color getSelectedCellForegroundColorNoFocus(ViewerCell cell) {
169 return null;
170 }
171
172 /**
173 * The color to use when rendering the background of the selected cell when
174 * the control has <b>no</b> input focus
175 *
176 * @param cell
177 * the cell which is colored
178 * @return the color or <code>null</code> to use the same used when
179 * control has focus
180 * @since 3.4
181 */
182 protected Color getSelectedCellBackgroundColorNoFocus(ViewerCell cell) {
183 return null;
184 }
185
186 /**
187 * Controls whether the whole cell or only the text-area is highlighted
188 *
189 * @param cell
190 * the cell which is highlighted
191 * @return <code>true</code> if only the text area should be highlighted
192 * @since 3.4
193 */
194 protected bool onlyTextHighlighting(ViewerCell cell) {
195 return false;
196 }
197
198 protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
199 super.focusCellChanged(newCell, oldCell);
150 200
151 // Redraw new area 201 // Redraw new area
152 if (cell !is null) { 202 if (newCell !is null) {
153 Rectangle rect = cell.getBounds(); 203 Rectangle rect = newCell.getBounds();
154 int x = cell.getColumnIndex() is 0 ? 0 : rect.x; 204 int x = newCell.getColumnIndex() is 0 ? 0 : rect.x;
155 int width = cell.getColumnIndex() is 0 ? rect.x + rect.width 205 int width = newCell.getColumnIndex() is 0 ? rect.x + rect.width
156 : rect.width; 206 : rect.width;
157 // 1 is a fix for Linux-GTK 207 // 1 is a fix for Linux-GTK
158 cell.getControl().redraw(x, rect.y-1, width, rect.height+1, true); 208 newCell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
209 true);
159 } 210 }
160 211
161 if (oldCell !is null) { 212 if (oldCell !is null) {
162 Rectangle rect = oldCell.getBounds(); 213 Rectangle rect = oldCell.getBounds();
163 int x = oldCell.getColumnIndex() is 0 ? 0 : rect.x; 214 int x = oldCell.getColumnIndex() is 0 ? 0 : rect.x;
164 int width = oldCell.getColumnIndex() is 0 ? rect.x + rect.width 215 int width = oldCell.getColumnIndex() is 0 ? rect.x + rect.width
165 : rect.width; 216 : rect.width;
166 // 1 is a fix for Linux-GTK 217 // 1 is a fix for Linux-GTK
167 oldCell.getControl().redraw(x, rect.y-1, width, rect.height+1, true); 218 oldCell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
219 true);
168 } 220 }
169
170 this.oldCell = cell;
171 } 221 }
172 } 222 }