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