comparison dwt/custom/ControlEditor.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children f565d3a95c0a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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 *
11 * Port to the D Programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
13 *******************************************************************************/
14 module dwt.custom.ControlEditor;
15
16 import Math = tango.math.Math;
17
18 import dwt.DWT;
19 import dwt.graphics.Rectangle;
20 import dwt.widgets.Composite;
21 import dwt.widgets.Control;
22 import dwt.widgets.Event;
23 import dwt.widgets.Listener;
24 import dwt.widgets.ScrollBar;
25
26 /**
27 *
28 * A ControlEditor is a manager for a Control that appears above a composite and tracks with the
29 * moving and resizing of that composite. It can be used to display one control above
30 * another control. This could be used when editing a control that does not have editing
31 * capabilities by using a text editor or for launching a dialog by placing a button
32 * above a control.
33 *
34 * <p> Here is an example of using a ControlEditor:
35 *
36 * <code><pre>
37 * Canvas canvas = new Canvas(shell, DWT.BORDER);
38 * canvas.setBounds(10, 10, 300, 300);
39 * Color color = new Color(null, 255, 0, 0);
40 * canvas.setBackground(color);
41 * ControlEditor editor = new ControlEditor (canvas);
42 * // The editor will be a button in the bottom right corner of the canvas.
43 * // When selected, it will launch a Color dialog that will change the background
44 * // of the canvas.
45 * Button button = new Button(canvas, DWT.PUSH);
46 * button.setText("Select Color...");
47 * button.addSelectionListener (new SelectionAdapter() {
48 * public void widgetSelected(SelectionEvent e) {
49 * ColorDialog dialog = new ColorDialog(shell);
50 * dialog.open();
51 * RGB rgb = dialog.getRGB();
52 * if (rgb !is null) {
53 * if (color !is null) color.dispose();
54 * color = new Color(null, rgb);
55 * canvas.setBackground(color);
56 * }
57 *
58 * }
59 * });
60 *
61 * editor.horizontalAlignment = DWT.RIGHT;
62 * editor.verticalAlignment = DWT.BOTTOM;
63 * editor.grabHorizontal = false;
64 * editor.grabVertical = false;
65 * Point size = button.computeSize(DWT.DEFAULT, DWT.DEFAULT);
66 * editor.minimumWidth = size.x;
67 * editor.minimumHeight = size.y;
68 * editor.setEditor (button);
69 * </pre></code>
70 */
71 public class ControlEditor {
72
73 /**
74 * Specifies how the editor should be aligned relative to the control. Allowed values
75 * are DWT.LEFT, DWT.RIGHT and DWT.CENTER. The default value is DWT.CENTER.
76 */
77 public int horizontalAlignment = DWT.CENTER;
78
79 /**
80 * Specifies whether the editor should be sized to use the entire width of the control.
81 * True means resize the editor to the same width as the cell. False means do not adjust
82 * the width of the editor. The default value is false.
83 */
84 public bool grabHorizontal = false;
85
86 /**
87 * Specifies the minimum width the editor can have. This is used in association with
88 * a true value of grabHorizontal. If the cell becomes smaller than the minimumWidth, the
89 * editor will not made smaller than the minimum width value. The default value is 0.
90 */
91 public int minimumWidth = 0;
92
93 /**
94 * Specifies how the editor should be aligned relative to the control. Allowed values
95 * are DWT.TOP, DWT.BOTTOM and DWT.CENTER. The default value is DWT.CENTER.
96 */
97 public int verticalAlignment = DWT.CENTER;
98
99 /**
100 * Specifies whether the editor should be sized to use the entire height of the control.
101 * True means resize the editor to the same height as the underlying control. False means do not adjust
102 * the height of the editor. The default value is false.
103 */
104 public bool grabVertical = false;
105
106 /**
107 * Specifies the minimum height the editor can have. This is used in association with
108 * a true value of grabVertical. If the control becomes smaller than the minimumHeight, the
109 * editor will not made smaller than the minimum height value. The default value is 0.
110 */
111 public int minimumHeight = 0;
112
113 Composite parent;
114 Control editor;
115 private bool hadFocus;
116 private Listener controlListener;
117 private Listener scrollbarListener;
118
119 private const static int[] EVENTS = [DWT.KeyDown, DWT.KeyUp, DWT.MouseDown, DWT.MouseUp, DWT.Resize];
120
121 /**
122 * Creates a ControlEditor for the specified Composite.
123 *
124 * @param parent the Composite above which this editor will be displayed
125 *
126 */
127 public this (Composite parent) {
128 this.parent = parent;
129
130 controlListener = new class Listener {
131 public void handleEvent (Event e) {
132 layout();
133 }
134 };
135 for (int i = 0; i < EVENTS.length; i++) {
136 parent.addListener(EVENTS[i], controlListener);
137 }
138
139 scrollbarListener = new class Listener {
140 public void handleEvent (Event e) {
141 scroll(e);
142 }
143 };
144 ScrollBar hBar = parent.getHorizontalBar();
145 if (hBar !is null)
146 hBar.addListener(DWT.Selection, scrollbarListener);
147 ScrollBar vBar = parent.getVerticalBar();
148 if (vBar !is null)
149 vBar.addListener(DWT.Selection, scrollbarListener);
150 }
151
152 Rectangle computeBounds () {
153 Rectangle clientArea = parent.getClientArea();
154 Rectangle editorRect = new Rectangle(clientArea.x, clientArea.y, minimumWidth, minimumHeight);
155
156 if (grabHorizontal)
157 editorRect.width = Math.max(clientArea.width, minimumWidth);
158
159 if (grabVertical)
160 editorRect.height = Math.max(clientArea.height, minimumHeight);
161
162 switch (horizontalAlignment) {
163 case DWT.RIGHT:
164 editorRect.x += clientArea.width - editorRect.width;
165 break;
166 case DWT.LEFT:
167 // do nothing - clientArea.x is the right answer
168 break;
169 default:
170 // default is CENTER
171 editorRect.x += (clientArea.width - editorRect.width) / 2;
172 }
173
174 switch (verticalAlignment) {
175 case DWT.BOTTOM:
176 editorRect.y += clientArea.height - editorRect.height;
177 break;
178 case DWT.TOP:
179 // do nothing - clientArea.y is the right answer
180 break;
181 default:
182 // default is CENTER
183 editorRect.y += (clientArea.height - editorRect.height) / 2;
184 }
185
186 return editorRect;
187
188 }
189
190 /**
191 * Removes all associations between the Editor and the underlying composite. The
192 * composite and the editor Control are <b>not</b> disposed.
193 */
194 public void dispose () {
195 if (parent !is null && !parent.isDisposed()) {
196 for (int i = 0; i < EVENTS.length; i++) {
197 parent.removeListener(EVENTS[i], controlListener);
198 }
199 ScrollBar hBar = parent.getHorizontalBar();
200 if (hBar !is null)
201 hBar.removeListener(DWT.Selection, scrollbarListener);
202 ScrollBar vBar = parent.getVerticalBar();
203 if (vBar !is null)
204 vBar.removeListener(DWT.Selection, scrollbarListener);
205 }
206
207 parent = null;
208 editor = null;
209 hadFocus = false;
210 controlListener = null;
211 scrollbarListener = null;
212 }
213
214 /**
215 * Returns the Control that is displayed above the composite being edited.
216 *
217 * @return the Control that is displayed above the composite being edited
218 */
219 public Control getEditor () {
220 return editor;
221 }
222
223 /**
224 * Lays out the control within the underlying composite. This
225 * method should be called after changing one or more fields to
226 * force the Editor to resize.
227 *
228 * @since 2.1
229 */
230 public void layout () {
231 if (editor is null || editor.isDisposed())
232 return;
233 if (editor.getVisible()) {
234 hadFocus = editor.isFocusControl();
235 } // this doesn't work because
236 // resizing the column takes the focus away
237 // before we get here
238 editor.setBounds(computeBounds());
239 if (hadFocus) {
240 if (editor is null || editor.isDisposed())
241 return;
242 editor.setFocus();
243 }
244 }
245
246 void scroll (Event e) {
247 if (editor is null || editor.isDisposed())
248 return;
249 layout();
250 }
251
252 /**
253 * Specify the Control that is to be displayed.
254 *
255 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent
256 * being the Composite specified in the ControlEditor constructor.
257 *
258 * @param editor the Control that is displayed above the composite being edited
259 */
260 public void setEditor (Control editor) {
261
262 if (editor is null) {
263 // this is the case where the caller is setting the editor to be blank
264 // set all the values accordingly
265 this.editor = null;
266 return;
267 }
268
269 this.editor = editor;
270 layout();
271 if (this.editor is null || this.editor.isDisposed())
272 return;
273 editor.setVisible(true);
274 }
275 }