comparison dwtx/jface/viewers/ColorCellEditor.d @ 10:b6c35faf97c8

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