Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/ColorCellEditor.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | 6f068362a363 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 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 org.eclipse.jface.viewers.ColorCellEditor; | |
14 | |
15 import org.eclipse.jface.viewers.DialogCellEditor; | |
16 | |
17 import org.eclipse.swt.SWT; | |
18 import org.eclipse.swt.custom.TableTree; | |
19 import org.eclipse.swt.graphics.Color; | |
20 import org.eclipse.swt.graphics.FontMetrics; | |
21 import org.eclipse.swt.graphics.GC; | |
22 import org.eclipse.swt.graphics.Image; | |
23 import org.eclipse.swt.graphics.ImageData; | |
24 import org.eclipse.swt.graphics.PaletteData; | |
25 import org.eclipse.swt.graphics.Point; | |
26 import org.eclipse.swt.graphics.RGB; | |
27 import org.eclipse.swt.graphics.Rectangle; | |
28 import org.eclipse.swt.widgets.ColorDialog; | |
29 import org.eclipse.swt.widgets.Composite; | |
30 import org.eclipse.swt.widgets.Control; | |
31 import org.eclipse.swt.widgets.Label; | |
32 import org.eclipse.swt.widgets.Layout; | |
33 import org.eclipse.swt.widgets.Table; | |
34 import org.eclipse.swt.widgets.Tree; | |
35 | |
36 import java.lang.all; | |
37 import java.util.Set; | |
38 import tango.text.convert.Format; | |
39 | |
40 /** | |
41 * A cell editor that manages a color field. | |
42 * The cell editor's value is the color (an SWT <code>RBG</code>). | |
43 * <p> | |
44 * This class may be instantiated; it is not intended to be subclassed. | |
45 * </p> | |
46 * @noextend This class is not intended to be subclassed by clients. | |
47 */ | |
48 public class ColorCellEditor : DialogCellEditor { | |
49 | |
50 /** | |
51 * The default extent in pixels. | |
52 */ | |
53 private static const int DEFAULT_EXTENT = 16; | |
54 | |
55 /** | |
56 * Gap between between image and text in pixels. | |
57 */ | |
58 private static const int GAP = 6; | |
59 | |
60 /** | |
61 * The composite widget containing the color and RGB label widgets | |
62 */ | |
63 private Composite composite; | |
64 | |
65 /** | |
66 * The label widget showing the current color. | |
67 */ | |
68 private Label colorLabel; | |
69 | |
70 /** | |
71 * The label widget showing the RGB values. | |
72 */ | |
73 private Label rgbLabel; | |
74 | |
75 /** | |
76 * The image. | |
77 */ | |
78 private Image image; | |
79 | |
80 /** | |
81 * Internal class for laying out this cell editor. | |
82 */ | |
83 private class ColorCellLayout : Layout { | |
84 public override Point computeSize(Composite editor, int wHint, int hHint, | |
85 bool force) { | |
86 if (wHint !is SWT.DEFAULT && hHint !is SWT.DEFAULT) { | |
87 return new Point(wHint, hHint); | |
88 } | |
89 Point colorSize = colorLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT, | |
90 force); | |
91 Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT, | |
92 force); | |
93 return new Point(colorSize.x + GAP + rgbSize.x, Math.max( | |
94 colorSize.y, rgbSize.y)); | |
95 } | |
96 | |
97 public override void layout(Composite editor, bool force) { | |
98 Rectangle bounds = editor.getClientArea(); | |
99 Point colorSize = colorLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT, | |
100 force); | |
101 Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT, | |
102 force); | |
103 int ty = (bounds.height - rgbSize.y) / 2; | |
104 if (ty < 0) { | |
105 ty = 0; | |
106 } | |
107 colorLabel.setBounds(-1, 0, colorSize.x, colorSize.y); | |
108 rgbLabel.setBounds(colorSize.x + GAP - 1, ty, bounds.width | |
109 - colorSize.x - GAP, bounds.height); | |
110 } | |
111 } | |
112 | |
113 /** | |
114 * Creates a new color cell editor parented under the given control. | |
115 * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no | |
116 * validator. | |
117 * | |
118 * @param parent the parent control | |
119 */ | |
120 public this(Composite parent) { | |
121 this(parent, SWT.NONE); | |
122 } | |
123 | |
124 /** | |
125 * Creates a new color cell editor parented under the given control. | |
126 * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no | |
127 * validator. | |
128 * | |
129 * @param parent the parent control | |
130 * @param style the style bits | |
131 * @since 2.1 | |
132 */ | |
133 public this(Composite parent, int style) { | |
134 super(parent, style); | |
135 doSetValue(new RGB(0, 0, 0)); | |
136 } | |
137 | |
138 /** | |
139 * Creates and returns the color image data for the given control | |
140 * and RGB value. The image's size is either the control's item extent | |
141 * or the cell editor's default extent, which is 16 pixels square. | |
142 * | |
143 * @param w the control | |
144 * @param color the color | |
145 */ | |
146 private ImageData createColorImage(Control w, RGB color) { | |
147 | |
148 GC gc = new GC(w); | |
149 FontMetrics fm = gc.getFontMetrics(); | |
150 int size = fm.getAscent(); | |
151 gc.dispose(); | |
152 | |
153 int indent = 6; | |
154 int extent = DEFAULT_EXTENT; | |
155 if ( auto i = cast(Table)w ) { | |
156 extent = i.getItemHeight() - 1; | |
157 } else if ( auto i = cast(Tree)w ) { | |
158 extent = i.getItemHeight() - 1; | |
159 } else if ( auto i = cast(TableTree)w ) { | |
160 extent = i.getItemHeight() - 1; | |
161 } | |
162 | |
163 if (size > extent) { | |
164 size = extent; | |
165 } | |
166 | |
167 int width = indent + size; | |
168 int height = extent; | |
169 | |
170 int xoffset = indent; | |
171 int yoffset = (height - size) / 2; | |
172 | |
173 RGB black = new RGB(0, 0, 0); | |
174 PaletteData dataPalette = new PaletteData([ black, black, | |
175 color ]); | |
176 ImageData data = new ImageData(width, height, 4, dataPalette); | |
177 data.transparentPixel = 0; | |
178 | |
179 int end = size - 1; | |
180 for (int y = 0; y < size; y++) { | |
181 for (int x = 0; x < size; x++) { | |
182 if (x is 0 || y is 0 || x is end || y is end) { | |
183 data.setPixel(x + xoffset, y + yoffset, 1); | |
184 } else { | |
185 data.setPixel(x + xoffset, y + yoffset, 2); | |
186 } | |
187 } | |
188 } | |
189 | |
190 return data; | |
191 } | |
192 | |
193 /* (non-Javadoc) | |
194 * Method declared on DialogCellEditor. | |
195 */ | |
196 protected override Control createContents(Composite cell) { | |
197 Color bg = cell.getBackground(); | |
198 composite = new Composite(cell, getStyle()); | |
199 composite.setBackground(bg); | |
200 composite.setLayout(new ColorCellLayout()); | |
201 colorLabel = new Label(composite, SWT.LEFT); | |
202 colorLabel.setBackground(bg); | |
203 rgbLabel = new Label(composite, SWT.LEFT); | |
204 rgbLabel.setBackground(bg); | |
205 rgbLabel.setFont(cell.getFont()); | |
206 return composite; | |
207 } | |
208 | |
209 /* (non-Javadoc) | |
210 * Method declared on CellEditor. | |
211 */ | |
212 public override void dispose() { | |
213 if (image !is null) { | |
214 image.dispose(); | |
215 image = null; | |
216 } | |
217 super.dispose(); | |
218 } | |
219 | |
220 /* (non-Javadoc) | |
221 * Method declared on DialogCellEditor. | |
222 */ | |
223 protected override Object openDialogBox(Control cellEditorWindow) { | |
224 ColorDialog dialog = new ColorDialog(cellEditorWindow.getShell()); | |
225 Object value = getValue(); | |
226 if (value !is null) { | |
227 dialog.setRGB(cast(RGB) value); | |
228 } | |
229 value = dialog.open(); | |
230 return dialog.getRGB(); | |
231 } | |
232 | |
233 /* (non-Javadoc) | |
234 * Method declared on DialogCellEditor. | |
235 */ | |
236 protected override void updateContents(Object value) { | |
237 RGB rgb = cast(RGB) value; | |
238 // XXX: We don't have a value the first time this method is called". | |
239 if (rgb is null) { | |
240 rgb = new RGB(0, 0, 0); | |
241 } | |
242 // XXX: Workaround for 1FMQ0P3: SWT:ALL - TableItem.setImage doesn't work if using the identical image." | |
243 if (image !is null) { | |
244 image.dispose(); | |
245 } | |
246 | |
247 ImageData id = createColorImage(colorLabel.getParent().getParent(), rgb); | |
248 ImageData mask = id.getTransparencyMask(); | |
249 image = new Image(colorLabel.getDisplay(), id, mask); | |
250 colorLabel.setImage(image); | |
251 | |
252 rgbLabel | |
253 .setText( Format( "({},{},{})", rgb.red, rgb.green, rgb.blue));//$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ | |
254 } | |
255 } |