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