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