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