Mercurial > projects > dwt-addons
annotate dwtx/ui/forms/widgets/ToggleHyperlink.d @ 77:26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 24 May 2008 06:59:31 +0200 |
parents | e193036d82c9 |
children | c3583c6ec027 |
rev | line source |
---|---|
75 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.ui.forms.widgets.ToggleHyperlink; | |
14 | |
15 import dwtx.ui.forms.widgets.AbstractHyperlink; | |
76 | 16 import dwtx.ui.forms.widgets.ExpandableComposite; |
75 | 17 |
18 import dwt.DWT; | |
19 import dwt.accessibility.ACC; | |
20 import dwt.accessibility.AccessibleAdapter; | |
21 import dwt.accessibility.AccessibleControlAdapter; | |
22 import dwt.accessibility.AccessibleControlEvent; | |
23 import dwt.accessibility.AccessibleEvent; | |
24 import dwt.graphics.Color; | |
25 import dwt.graphics.Point; | |
26 import dwt.graphics.Rectangle; | |
27 import dwt.widgets.Composite; | |
28 import dwt.widgets.Event; | |
29 import dwt.widgets.Listener; | |
30 import dwtx.ui.forms.events.HyperlinkAdapter; | |
31 import dwtx.ui.forms.events.HyperlinkEvent; | |
32 import dwtx.ui.internal.forms.Messages; | |
33 | |
34 import dwt.dwthelper.utils; | |
35 | |
36 /** | |
37 * A custom selectable control that can be used to control areas that can be | |
38 * expanded or collapsed. | |
39 * <p> | |
40 * This is an abstract class. Subclasses are responsible for rendering the | |
41 * control using decoration and hover decoration color. Control should be | |
42 * rendered based on the current expansion state. | |
43 * | |
44 * @since 3.0 | |
45 */ | |
46 public abstract class ToggleHyperlink : AbstractHyperlink { | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
76
diff
changeset
|
47 |
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
76
diff
changeset
|
48 alias AbstractHyperlink.computeSize computeSize; |
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
76
diff
changeset
|
49 |
75 | 50 protected int innerWidth; |
51 protected int innerHeight; | |
52 protected bool hover; | |
53 package bool hover_package(){ | |
54 return hover; | |
55 } | |
56 package bool hover_package( bool v){ | |
57 return (hover = v); | |
58 } | |
59 private bool expanded; | |
60 private Color decorationColor; | |
61 private Color hoverColor; | |
62 /** | |
63 * Creates a control in a provided composite. | |
64 * | |
65 * @param parent | |
66 * the parent | |
67 * @param style | |
68 * the style | |
69 */ | |
70 public this(Composite parent, int style) { | |
71 super(parent, style); | |
72 Listener listener = new class Listener { | |
73 public void handleEvent(Event e) { | |
74 switch (e.type) { | |
75 case DWT.MouseEnter: | |
76 hover=true; | |
77 redraw(); | |
78 break; | |
79 case DWT.MouseExit: | |
80 hover = false; | |
81 redraw(); | |
82 break; | |
83 case DWT.KeyDown: | |
84 onKeyDown(e); | |
85 break; | |
86 } | |
87 } | |
88 }; | |
89 addListener(DWT.MouseEnter, listener); | |
90 addListener(DWT.MouseExit, listener); | |
91 addListener(DWT.KeyDown, listener); | |
92 addHyperlinkListener(new class HyperlinkAdapter { | |
93 public void linkActivated(HyperlinkEvent e) { | |
94 setExpanded(!isExpanded()); | |
95 } | |
96 }); | |
97 initAccessible(); | |
98 } | |
99 /** | |
100 * Sets the color of the decoration. | |
101 * | |
102 * @param decorationColor | |
103 */ | |
104 public void setDecorationColor(Color decorationColor) { | |
105 this.decorationColor = decorationColor; | |
106 } | |
107 /** | |
108 * Returns the color of the decoration. | |
109 * | |
110 * @return decoration color | |
111 */ | |
112 public Color getDecorationColor() { | |
113 return decorationColor; | |
114 } | |
115 /** | |
116 * Sets the hover color of decoration. Hover color will be used when mouse | |
117 * enters the decoration area. | |
118 * | |
119 * @param hoverColor | |
120 * the hover color to use | |
121 */ | |
122 public void setHoverDecorationColor(Color hoverColor) { | |
123 this.hoverColor = hoverColor; | |
124 } | |
125 /** | |
126 * Returns the hover color of the decoration. | |
127 * | |
128 * @return the hover color of the decoration. | |
129 * @since 3.1 | |
130 */ | |
131 public Color getHoverDecorationColor() { | |
132 return hoverColor; | |
133 } | |
134 | |
135 /** | |
136 * Returns the hover color of the decoration. | |
137 * | |
138 * @return the hover color of the decoration. | |
139 * @deprecated use <code>getHoverDecorationColor</code> | |
140 * @see #getHoverDecorationColor() | |
141 */ | |
142 public Color geHoverDecorationColor() { | |
143 return hoverColor; | |
144 } | |
145 /** | |
146 * Computes the size of the control. | |
147 * | |
148 * @param wHint | |
149 * width hint | |
150 * @param hHint | |
151 * height hint | |
152 * @param changed | |
153 * if true, flush any saved layout state | |
154 */ | |
155 public Point computeSize(int wHint, int hHint, bool changed) { | |
156 int width, height; | |
157 /* | |
158 if (wHint !is DWT.DEFAULT) | |
159 width = wHint; | |
160 else */ | |
161 width = innerWidth + 2 * marginWidth; | |
162 /* | |
163 if (hHint !is DWT.DEFAULT) | |
164 height = hHint; | |
165 else */ | |
166 height = innerHeight + 2 * marginHeight; | |
167 return new Point(width, height); | |
168 } | |
169 /** | |
170 * Returns the expansion state of the toggle control. When toggle is in the | |
171 * normal (downward) state, the value is <samp>true </samp>. Collapsed | |
172 * control will return <samp>false </samp>. | |
173 * | |
174 * @return <samp>false </samp> if collapsed, <samp>true </samp> otherwise. | |
175 */ | |
176 public bool isExpanded() { | |
177 return expanded; | |
178 } | |
179 /** | |
180 * Sets the expansion state of the twistie control | |
181 * | |
182 * @param expanded the expansion state | |
183 */ | |
184 public void setExpanded(bool expanded) { | |
185 this.expanded = expanded; | |
186 getAccessible().selectionChanged(); | |
187 redraw(); | |
188 } | |
189 private void initAccessible() { | |
190 getAccessible().addAccessibleListener(new class AccessibleAdapter { | |
191 public void getHelp(AccessibleEvent e) { | |
192 e.result = getToolTipText(); | |
193 } | |
194 public void getName(AccessibleEvent e) { | |
195 String name=Messages.ToggleHyperlink_accessibleName; | |
196 if (null !is cast(ExpandableComposite)getParent() ) { | |
197 name ~= Messages.ToggleHyperlink_accessibleColumn ~ (cast(ExpandableComposite)getParent()).getText(); | |
198 int index = name.indexOf('&'); | |
199 if (index !is -1) { | |
76 | 200 name = name.substring(0, index) ~ name.substring(index + 1); |
75 | 201 } |
202 } | |
203 e.result = name; | |
204 } | |
205 public void getDescription(AccessibleEvent e) { | |
206 getName(e); | |
207 } | |
208 }); | |
209 getAccessible().addAccessibleControlListener( | |
210 new class AccessibleControlAdapter { | |
211 public void getChildAtPoint(AccessibleControlEvent e) { | |
212 Point testPoint = toControl(new Point(e.x, e.y)); | |
213 if (getBounds().contains(testPoint)) { | |
214 e.childID = ACC.CHILDID_SELF; | |
215 } | |
216 } | |
217 public void getLocation(AccessibleControlEvent e) { | |
218 Rectangle location = getBounds(); | |
219 Point pt = toDisplay(new Point(location.x, location.y)); | |
220 e.x = pt.x; | |
221 e.y = pt.y; | |
222 e.width = location.width; | |
223 e.height = location.height; | |
224 } | |
225 public void getSelection (AccessibleControlEvent e) { | |
226 if (this.outer.getSelection()) | |
227 e.childID = ACC.CHILDID_SELF; | |
228 } | |
229 | |
230 public void getFocus (AccessibleControlEvent e) { | |
231 if (this.outer.getSelection()) | |
232 e.childID = ACC.CHILDID_SELF; | |
233 } | |
234 public void getChildCount(AccessibleControlEvent e) { | |
235 e.detail = 0; | |
236 } | |
237 public void getRole(AccessibleControlEvent e) { | |
238 e.detail = ACC.ROLE_TREE; | |
239 } | |
240 public void getState(AccessibleControlEvent e) { | |
241 int state = ACC.STATE_FOCUSABLE; | |
242 if (this.outer.getSelection()) | |
243 state |= ACC.STATE_FOCUSED; | |
244 state |= this.outer.isExpanded() | |
245 ? ACC.STATE_EXPANDED | |
246 : ACC.STATE_COLLAPSED; | |
247 e.detail = state; | |
248 } | |
249 }); | |
250 } | |
251 private void onKeyDown(Event e) { | |
76 | 252 if (e.keyCode is DWT.ARROW_RIGHT) { |
75 | 253 // expand if collapsed |
254 if (!isExpanded()) { | |
255 handleActivate(e); | |
256 } | |
257 e.doit=false; | |
258 } | |
76 | 259 else if (e.keyCode is DWT.ARROW_LEFT) { |
75 | 260 // collapse if expanded |
261 if (isExpanded()) { | |
262 handleActivate(e); | |
263 } | |
264 e.doit=false; | |
265 } | |
266 } | |
267 } |