25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 org.eclipse.swt.widgets.Link;
|
|
14
|
|
15
|
|
16 import org.eclipse.swt.SWT;
|
|
17 import org.eclipse.swt.SWTException;
|
|
18 import org.eclipse.swt.accessibility.ACC;
|
|
19 import org.eclipse.swt.accessibility.Accessible;
|
|
20 import org.eclipse.swt.accessibility.AccessibleAdapter;
|
|
21 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
|
|
22 import org.eclipse.swt.accessibility.AccessibleControlEvent;
|
|
23 import org.eclipse.swt.accessibility.AccessibleEvent;
|
|
24 import org.eclipse.swt.events.SelectionEvent;
|
|
25 import org.eclipse.swt.events.SelectionListener;
|
|
26 import org.eclipse.swt.graphics.Color;
|
|
27 import org.eclipse.swt.graphics.Font;
|
|
28 import org.eclipse.swt.graphics.GC;
|
|
29 import org.eclipse.swt.graphics.GCData;
|
|
30 import org.eclipse.swt.graphics.Point;
|
|
31 import org.eclipse.swt.graphics.RGB;
|
|
32 import org.eclipse.swt.graphics.Rectangle;
|
|
33 import org.eclipse.swt.graphics.TextLayout;
|
|
34 import org.eclipse.swt.graphics.TextStyle;
|
|
35 import org.eclipse.swt.internal.gtk.OS;
|
|
36
|
|
37 import org.eclipse.swt.graphics.Cursor;
|
|
38 import org.eclipse.swt.widgets.Control;
|
|
39 import org.eclipse.swt.widgets.Composite;
|
|
40 import org.eclipse.swt.widgets.TypedListener;
|
|
41 import org.eclipse.swt.widgets.Event;
|
|
42 import java.lang.all;
|
|
43
|
48
|
44 version(Tango){
|
25
|
45 import tango.text.Unicode;
|
48
|
46 } else { // Phobos
|
|
47 }
|
25
|
48
|
|
49
|
|
50 /**
|
|
51 * Instances of this class represent a selectable
|
|
52 * user interface object that displays a text with
|
|
53 * links.
|
|
54 * <p>
|
|
55 * <dl>
|
|
56 * <dt><b>Styles:</b></dt>
|
|
57 * <dd>(none)</dd>
|
|
58 * <dt><b>Events:</b></dt>
|
|
59 * <dd>Selection</dd>
|
|
60 * </dl>
|
|
61 * <p>
|
|
62 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
|
|
63 * </p>
|
|
64 *
|
|
65 * @see <a href="http://www.eclipse.org/swt/snippets/#link">Link snippets</a>
|
|
66 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
|
|
67 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
68 *
|
|
69 * @since 3.1
|
|
70 */
|
|
71 public class Link : Control {
|
|
72
|
|
73 alias Control.computeSize computeSize;
|
|
74 alias Control.fixStyle fixStyle;
|
|
75 alias Control.setBounds setBounds;
|
|
76
|
|
77 String text;
|
|
78 TextLayout layout;
|
|
79 Color linkColor, disabledColor;
|
|
80 Point [] offsets;
|
|
81 Point selection;
|
|
82 String [] ids;
|
|
83 int [] mnemonics;
|
|
84 int focusIndex;
|
|
85
|
|
86 static RGB LINK_FOREGROUND;
|
|
87 static RGB LINK_DISABLED_FOREGROUND;
|
|
88
|
|
89 static void static_this(){
|
|
90 if( LINK_FOREGROUND is null ){
|
|
91 LINK_FOREGROUND = new RGB (0, 51, 153);
|
|
92 }
|
|
93 if( LINK_DISABLED_FOREGROUND is null ){
|
|
94 LINK_DISABLED_FOREGROUND = new RGB (172, 168, 153);
|
|
95 }
|
|
96 }
|
|
97
|
|
98 /**
|
|
99 * Constructs a new instance of this class given its parent
|
|
100 * and a style value describing its behavior and appearance.
|
|
101 * <p>
|
|
102 * The style value is either one of the style constants defined in
|
|
103 * class <code>SWT</code> which is applicable to instances of this
|
|
104 * class, or must be built by <em>bitwise OR</em>'ing together
|
|
105 * (that is, using the <code>int</code> "|" operator) two or more
|
|
106 * of those <code>SWT</code> style constants. The class description
|
|
107 * lists the style constants that are applicable to the class.
|
|
108 * Style bits are also inherited from superclasses.
|
|
109 * </p>
|
|
110 *
|
|
111 * @param parent a composite control which will be the parent of the new instance (cannot be null)
|
|
112 * @param style the style of control to construct
|
|
113 *
|
|
114 * @exception IllegalArgumentException <ul>
|
|
115 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
116 * </ul>
|
|
117 * @exception SWTException <ul>
|
|
118 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
119 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
|
|
120 * </ul>
|
|
121 *
|
|
122 * @see Widget#checkSubclass
|
|
123 * @see Widget#getStyle
|
|
124 */
|
|
125 public this (Composite parent, int style) {
|
|
126 static_this();
|
|
127 super (parent, style);
|
|
128 }
|
|
129
|
|
130 /**
|
|
131 * Adds the listener to the collection of listeners who will
|
|
132 * be notified when the control is selected by the user, by sending
|
|
133 * it one of the messages defined in the <code>SelectionListener</code>
|
|
134 * interface.
|
|
135 * <p>
|
|
136 * <code>widgetSelected</code> is called when the control is selected by the user.
|
|
137 * <code>widgetDefaultSelected</code> is not called.
|
|
138 * </p>
|
|
139 *
|
|
140 * @param listener the listener which should be notified
|
|
141 *
|
|
142 * @exception IllegalArgumentException <ul>
|
|
143 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
144 * </ul>
|
|
145 * @exception SWTException <ul>
|
|
146 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
147 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
148 * </ul>
|
|
149 *
|
|
150 * @see SelectionListener
|
|
151 * @see #removeSelectionListener
|
|
152 * @see SelectionEvent
|
|
153 */
|
|
154 public void addSelectionListener (SelectionListener listener) {
|
|
155 checkWidget ();
|
|
156 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
157 TypedListener typedListener = new TypedListener (listener);
|
|
158 addListener (SWT.Selection, typedListener);
|
|
159 addListener (SWT.DefaultSelection, typedListener);
|
|
160 }
|
|
161
|
|
162 public override Point computeSize (int wHint, int hHint, bool changed) {
|
|
163 checkWidget ();
|
|
164 if (wHint !is SWT.DEFAULT && wHint < 0) wHint = 0;
|
|
165 if (hHint !is SWT.DEFAULT && hHint < 0) hHint = 0;
|
|
166 int width, height;
|
|
167 int layoutWidth = layout.getWidth ();
|
|
168 //TEMPORARY CODE
|
|
169 if (wHint is 0) {
|
|
170 layout.setWidth (1);
|
|
171 Rectangle rect = layout.getBounds ();
|
|
172 width = 0;
|
|
173 height = rect.height;
|
|
174 } else {
|
|
175 layout.setWidth (wHint);
|
|
176 Rectangle rect = layout.getBounds ();
|
|
177 width = rect.width;
|
|
178 height = rect.height;
|
|
179 }
|
|
180 layout.setWidth (layoutWidth);
|
|
181 if (wHint !is SWT.DEFAULT) width = wHint;
|
|
182 if (hHint !is SWT.DEFAULT) height = hHint;
|
|
183 int border = getBorderWidth ();
|
|
184 width += border * 2;
|
|
185 height += border * 2;
|
|
186 return new Point (width, height);
|
|
187 }
|
|
188
|
|
189 override void createHandle(int index) {
|
|
190 state |= HANDLE | THEME_BACKGROUND;
|
|
191 handle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null);
|
|
192 if (handle is null) SWT.error (SWT.ERROR_NO_HANDLES);
|
|
193 OS.gtk_fixed_set_has_window (handle, true);
|
|
194 OS.GTK_WIDGET_SET_FLAGS (handle, OS.GTK_CAN_FOCUS);
|
|
195 layout = new TextLayout (display);
|
|
196 layout.setOrientation((style & SWT.RIGHT_TO_LEFT) !is 0? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT);
|
|
197 linkColor = new Color (display, LINK_FOREGROUND);
|
|
198 disabledColor = new Color (display, LINK_DISABLED_FOREGROUND);
|
|
199 offsets = null;
|
|
200 ids = null;
|
|
201 mnemonics = null;
|
|
202 selection = new Point (-1, -1);
|
|
203 focusIndex = -1;
|
|
204 }
|
|
205
|
|
206 override void createWidget (int index) {
|
|
207 super.createWidget (index);
|
|
208 layout.setFont (getFont ());
|
|
209 text = "";
|
|
210 initAccessible ();
|
|
211 }
|
|
212
|
|
213 override void enableWidget (bool enabled) {
|
|
214 super.enableWidget (enabled);
|
|
215 if (isDisposed ()) return;
|
|
216 TextStyle linkStyle = new TextStyle (null, enabled ? linkColor : disabledColor, null);
|
|
217 linkStyle.underline = true;
|
|
218 for (int i = 0; i < offsets.length; i++) {
|
|
219 Point point = offsets [i];
|
|
220 layout.setStyle (linkStyle, point.x, point.y);
|
|
221 }
|
|
222 redraw ();
|
|
223 }
|
|
224
|
|
225 override void fixStyle () {
|
|
226 fixStyle (handle);
|
|
227 }
|
|
228
|
|
229 void initAccessible () {
|
|
230 Accessible accessible = getAccessible ();
|
|
231 accessible.addAccessibleListener (new class () AccessibleAdapter {
|
|
232 public void getName (AccessibleEvent e) {
|
|
233 e.result = parse (text);
|
|
234 }
|
|
235 });
|
|
236
|
|
237 accessible.addAccessibleControlListener (new class () AccessibleControlAdapter {
|
|
238 public void getChildAtPoint (AccessibleControlEvent e) {
|
|
239 e.childID = ACC.CHILDID_SELF;
|
|
240 }
|
|
241
|
|
242 public void getLocation (AccessibleControlEvent e) {
|
|
243 Rectangle rect = display.map (getParent (), null, getBounds ());
|
|
244 e.x = rect.x;
|
|
245 e.y = rect.y;
|
|
246 e.width = rect.width;
|
|
247 e.height = rect.height;
|
|
248 }
|
|
249
|
|
250 public void getChildCount (AccessibleControlEvent e) {
|
|
251 e.detail = 0;
|
|
252 }
|
|
253
|
|
254 public void getRole (AccessibleControlEvent e) {
|
|
255 e.detail = ACC.ROLE_LINK;
|
|
256 }
|
|
257
|
|
258 public void getState (AccessibleControlEvent e) {
|
|
259 e.detail = ACC.STATE_FOCUSABLE;
|
|
260 if (hasFocus ()) e.detail |= ACC.STATE_FOCUSED;
|
|
261 }
|
|
262
|
|
263 public void getDefaultAction (AccessibleControlEvent e) {
|
|
264 e.result = SWT.getMessage ("SWT_Press"); //$NON-NLS-1$
|
|
265 }
|
|
266
|
|
267 public void getSelection (AccessibleControlEvent e) {
|
|
268 if (hasFocus ()) e.childID = ACC.CHILDID_SELF;
|
|
269 }
|
|
270
|
|
271 public void getFocus (AccessibleControlEvent e) {
|
|
272 if (hasFocus ()) e.childID = ACC.CHILDID_SELF;
|
|
273 }
|
|
274 });
|
|
275 }
|
|
276
|
|
277 override String getNameText () {
|
|
278 return getText ();
|
|
279 }
|
|
280
|
|
281 Rectangle [] getRectangles (int linkIndex) {
|
|
282 int lineCount = layout.getLineCount ();
|
|
283 Rectangle [] rects = new Rectangle [lineCount];
|
|
284 int [] lineOffsets = layout.getLineOffsets ();
|
|
285 Point point = offsets [linkIndex];
|
|
286 int lineStart = 1;
|
|
287 while (point.x > lineOffsets [lineStart]) lineStart++;
|
|
288 int lineEnd = 1;
|
|
289 while (point.y > lineOffsets [lineEnd]) lineEnd++;
|
|
290 int index = 0;
|
|
291 if (lineStart is lineEnd) {
|
|
292 rects [index++] = layout.getBounds (point.x, point.y);
|
|
293 } else {
|
|
294 rects [index++] = layout.getBounds (point.x, lineOffsets [lineStart]-1);
|
|
295 rects [index++] = layout.getBounds (lineOffsets [lineEnd-1], point.y);
|
|
296 if (lineEnd - lineStart > 1) {
|
|
297 for (int i = lineStart; i < lineEnd - 1; i++) {
|
|
298 rects [index++] = layout.getLineBounds (i);
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302 if (rects.length !is index) {
|
|
303 Rectangle [] tmp = new Rectangle [index];
|
|
304 System.arraycopy (rects, 0, tmp, 0, index);
|
|
305 rects = tmp;
|
|
306 }
|
|
307 return rects;
|
|
308 }
|
|
309
|
|
310 int getClientWidth () {
|
|
311 return (state & ZERO_WIDTH) !is 0 ? 0 : OS.GTK_WIDGET_WIDTH (handle);
|
|
312 }
|
|
313
|
|
314 /**
|
|
315 * Returns the receiver's text, which will be an empty
|
|
316 * string if it has never been set.
|
|
317 *
|
|
318 * @return the receiver's text
|
|
319 *
|
|
320 * @exception SWTException <ul>
|
|
321 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
322 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
323 * </ul>
|
|
324 */
|
|
325 public String getText () {
|
|
326 checkWidget ();
|
|
327 return text;
|
|
328 }
|
|
329
|
|
330 override int gtk_button_press_event (GtkWidget* widget, GdkEventButton* gdkEvent) {
|
|
331 int /*long*/ result = super.gtk_button_press_event (widget, gdkEvent);
|
|
332 if (result !is 0) return result;
|
|
333 if (gdkEvent.button is 1 && gdkEvent.type is OS.GDK_BUTTON_PRESS) {
|
|
334 if (focusIndex !is -1) setFocus ();
|
|
335 int x = cast(int) gdkEvent.x;
|
|
336 int y = cast(int) gdkEvent.y;
|
|
337 if ((style & SWT.MIRRORED) !is 0) x = getClientWidth () - x;
|
|
338 int offset = layout.getOffset (x, y, null);
|
|
339 int oldSelectionX = selection.x;
|
|
340 int oldSelectionY = selection.y;
|
|
341 selection.x = offset;
|
|
342 selection.y = -1;
|
|
343 if (oldSelectionX !is -1 && oldSelectionY !is -1) {
|
|
344 if (oldSelectionX > oldSelectionY) {
|
|
345 int temp = oldSelectionX;
|
|
346 oldSelectionX = oldSelectionY;
|
|
347 oldSelectionY = temp;
|
|
348 }
|
|
349 Rectangle rect = layout.getBounds (oldSelectionX, oldSelectionY);
|
|
350 redraw (rect.x, rect.y, rect.width, rect.height, false);
|
|
351 }
|
|
352 for (int j = 0; j < offsets.length; j++) {
|
|
353 Rectangle [] rects = getRectangles (j);
|
|
354 for (int i = 0; i < rects.length; i++) {
|
|
355 Rectangle rect = rects [i];
|
|
356 if (rect.contains (x, y)) {
|
|
357 focusIndex = j;
|
|
358 redraw ();
|
|
359 return result;
|
|
360 }
|
|
361 }
|
|
362 }
|
|
363 }
|
|
364 return result;
|
|
365 }
|
|
366
|
|
367 override int /*long*/ gtk_button_release_event (GtkWidget* widget, GdkEventButton* gdkEvent) {
|
|
368 int /*long*/ result = super.gtk_button_release_event (widget, gdkEvent);
|
|
369 if (result !is 0) return result;
|
|
370 if (focusIndex is -1) return result;
|
|
371 if (gdkEvent.button is 1) {
|
|
372 int x = cast(int) gdkEvent.x;
|
|
373 int y = cast(int) gdkEvent.y;
|
|
374 if ((style & SWT.MIRRORED) !is 0) x = getClientWidth () - x;
|
|
375 Rectangle [] rects = getRectangles (focusIndex);
|
|
376 for (int i = 0; i < rects.length; i++) {
|
|
377 Rectangle rect = rects [i];
|
|
378 if (rect.contains (x, y)) {
|
|
379 Event ev = new Event ();
|
|
380 ev.text = ids [focusIndex];
|
|
381 sendEvent (SWT.Selection, ev);
|
|
382 return result;
|
|
383 }
|
|
384 }
|
|
385 }
|
|
386 return result;
|
|
387 }
|
|
388
|
|
389 override int /*long*/ gtk_event_after (GtkWidget* widget, GdkEvent* event) {
|
|
390 int /*long*/ result = super.gtk_event_after (widget, event);
|
|
391 switch (event.type) {
|
|
392 case OS.GDK_FOCUS_CHANGE:
|
|
393 redraw ();
|
|
394 break;
|
|
395 default:
|
|
396 }
|
|
397 return result;
|
|
398 }
|
|
399
|
|
400 override int /*long*/ gtk_expose_event (GtkWidget* widget, GdkEventExpose* gdkEvent) {
|
|
401 if ((state & OBSCURED) !is 0) return 0;
|
|
402 GCData data = new GCData ();
|
|
403 data.damageRgn = gdkEvent.region;
|
|
404 GC gc = GC.gtk_new (this, data);
|
|
405 OS.gdk_gc_set_clip_region (gc.handle, gdkEvent.region);
|
|
406 int selStart = selection.x;
|
|
407 int selEnd = selection.y;
|
|
408 if (selStart > selEnd) {
|
|
409 selStart = selection.y;
|
|
410 selEnd = selection.x;
|
|
411 }
|
|
412 // temporary code to disable text selection
|
|
413 selStart = selEnd = -1;
|
|
414 if ((state & DISABLED) !is 0) gc.setForeground (disabledColor);
|
|
415 layout.draw (gc, 0, 0, selStart, selEnd, null, null);
|
|
416 if (hasFocus () && focusIndex !is -1) {
|
|
417 Rectangle [] rects = getRectangles (focusIndex);
|
|
418 for (int i = 0; i < rects.length; i++) {
|
|
419 Rectangle rect = rects [i];
|
|
420 gc.drawFocus (rect.x, rect.y, rect.width, rect.height);
|
|
421 }
|
|
422 }
|
|
423 if (hooks (SWT.Paint) || filters (SWT.Paint)) {
|
|
424 Event event = new Event ();
|
|
425 event.count = gdkEvent.count;
|
|
426 event.x = gdkEvent.area.x;
|
|
427 event.y = gdkEvent.area.y;
|
|
428 event.width = gdkEvent.area.width;
|
|
429 event.height = gdkEvent.area.height;
|
|
430 if ((style & SWT.MIRRORED) !is 0) event.x = getClientWidth () - event.width - event.x;
|
|
431 event.gc = gc;
|
|
432 sendEvent (SWT.Paint, event);
|
|
433 event.gc = null;
|
|
434 }
|
|
435 gc.dispose ();
|
|
436 return 0;
|
|
437 }
|
|
438
|
|
439 override int /*long*/ gtk_key_press_event (GtkWidget* widget, GdkEventKey* gdkEvent) {
|
|
440 int /*long*/ result = super.gtk_key_press_event (widget, gdkEvent);
|
|
441 if (result !is 0) return result;
|
|
442 if (focusIndex is -1) return result;
|
|
443 switch (gdkEvent.keyval) {
|
|
444 case OS.GDK_Return:
|
|
445 case OS.GDK_KP_Enter:
|
|
446 case OS.GDK_space:
|
|
447 Event event = new Event ();
|
|
448 event.text = ids [focusIndex];
|
|
449 sendEvent (SWT.Selection, event);
|
|
450 break;
|
|
451 case OS.GDK_Tab:
|
|
452 if (focusIndex < offsets.length - 1) {
|
|
453 focusIndex++;
|
|
454 redraw ();
|
|
455 }
|
|
456 break;
|
|
457 case OS.GDK_ISO_Left_Tab:
|
|
458 if (focusIndex > 0) {
|
|
459 focusIndex--;
|
|
460 redraw ();
|
|
461 }
|
|
462 break;
|
|
463 default:
|
|
464 }
|
|
465 return result;
|
|
466 }
|
|
467
|
|
468 override int /*long*/ gtk_motion_notify_event (GtkWidget* widget, GdkEventMotion* gdkEvent) {
|
|
469 int /*long*/ result = super.gtk_motion_notify_event (widget, gdkEvent);
|
|
470 if (result !is 0) return result;
|
|
471 int x = cast(int) gdkEvent.x;
|
|
472 int y = cast(int) gdkEvent.y;
|
|
473 if ((style & SWT.MIRRORED) !is 0) x = getClientWidth () - x;
|
|
474 if ((gdkEvent.state & OS.GDK_BUTTON1_MASK) !is 0) {
|
|
475 int oldSelection = selection.y;
|
|
476 selection.y = layout.getOffset (x, y, null);
|
|
477 if (selection.y !is oldSelection) {
|
|
478 int newSelection = selection.y;
|
|
479 if (oldSelection > newSelection) {
|
|
480 int temp = oldSelection;
|
|
481 oldSelection = newSelection;
|
|
482 newSelection = temp;
|
|
483 }
|
|
484 Rectangle rect = layout.getBounds (oldSelection, newSelection);
|
|
485 redraw (rect.x, rect.y, rect.width, rect.height, false);
|
|
486 }
|
|
487 } else {
|
|
488 for (int j = 0; j < offsets.length; j++) {
|
|
489 Rectangle [] rects = getRectangles (j);
|
|
490 for (int i = 0; i < rects.length; i++) {
|
|
491 Rectangle rect = rects [i];
|
|
492 if (rect.contains (x, y)) {
|
|
493 setCursor (display.getSystemCursor (SWT.CURSOR_HAND));
|
|
494 return result;
|
|
495 }
|
|
496 }
|
|
497 }
|
|
498 setCursor (null);
|
|
499 }
|
|
500 return result;
|
|
501 }
|
|
502
|
|
503 override void releaseWidget () {
|
|
504 super.releaseWidget ();
|
|
505 if (layout !is null) layout.dispose ();
|
|
506 layout = null;
|
|
507 if (linkColor !is null) linkColor.dispose ();
|
|
508 linkColor = null;
|
|
509 if (disabledColor !is null) disabledColor.dispose ();
|
|
510 disabledColor = null;
|
|
511 offsets = null;
|
|
512 ids = null;
|
|
513 mnemonics = null;
|
|
514 text = null;
|
|
515 }
|
|
516
|
|
517 /**
|
|
518 * Removes the listener from the collection of listeners who will
|
|
519 * be notified when the control is selected by the user.
|
|
520 *
|
|
521 * @param listener the listener which should no longer be notified
|
|
522 *
|
|
523 * @exception IllegalArgumentException <ul>
|
|
524 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
525 * </ul>
|
|
526 * @exception SWTException <ul>
|
|
527 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
528 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
529 * </ul>
|
|
530 *
|
|
531 * @see SelectionListener
|
|
532 * @see #addSelectionListener
|
|
533 */
|
|
534 public void removeSelectionListener (SelectionListener listener) {
|
|
535 checkWidget ();
|
|
536 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
537 if (eventTable is null) return;
|
|
538 eventTable.unhook (SWT.Selection, listener);
|
|
539 eventTable.unhook (SWT.DefaultSelection, listener);
|
|
540 }
|
|
541
|
|
542 String parse (String string) {
|
|
543 int length_ = string.length;
|
|
544 offsets = new Point[]( length_ / 4 );
|
|
545 ids = new String[]( length_ / 4 );
|
|
546 mnemonics = new int[] ( length_ / 4 + 1 );
|
|
547 StringBuffer result = new StringBuffer ();
|
|
548 char [] buffer = string.dup;
|
|
549
|
|
550 int index = 0, state = 0, linkIndex = 0;
|
|
551 int start = 0, tagStart = 0, linkStart = 0, endtagStart = 0, refStart = 0;
|
|
552 while (index < length_) {
|
|
553 int increment;
|
|
554 dchar c = CharacterFirstToLower (buffer [index .. $ ], increment );
|
|
555
|
|
556 switch (state) {
|
|
557 case 0:
|
|
558 if (c is '<') {
|
|
559 tagStart = index;
|
|
560 state++;
|
|
561 }
|
|
562 break;
|
|
563 case 1:
|
|
564 if (c is 'a') state++;
|
|
565 break;
|
|
566 case 2:
|
|
567 switch (c) {
|
|
568 case 'h':
|
|
569 state = 7;
|
|
570 break;
|
|
571 case '>':
|
|
572 linkStart = index + 1;
|
|
573 state++;
|
|
574 break;
|
|
575 default:
|
51
|
576 if (Character.isWhitespace(c)) break;
|
25
|
577 else state = 13;
|
|
578 }
|
|
579 break;
|
|
580 case 3:
|
|
581 if (c is '<') {
|
|
582 endtagStart = index;
|
|
583 state++;
|
|
584 }
|
|
585 break;
|
|
586 case 4:
|
|
587 state = c is '/' ? state + 1 : 3;
|
|
588 break;
|
|
589 case 5:
|
|
590 state = c is 'a' ? state + 1 : 3;
|
|
591 break;
|
|
592 case 6:
|
|
593 if (c is '>') {
|
|
594 mnemonics [linkIndex] = parseMnemonics (buffer, start, tagStart, result);
|
|
595 int offset = result.length ();
|
|
596 parseMnemonics (buffer, linkStart, endtagStart, result);
|
|
597 offsets [linkIndex] = new Point (offset, result.length () - 1);
|
|
598 if (ids [linkIndex] is null) {
|
51
|
599 ids [linkIndex] = buffer[ linkStart .. endtagStart ]._idup();
|
25
|
600 }
|
|
601 linkIndex++;
|
|
602 start = tagStart = linkStart = endtagStart = refStart = index + 1;
|
|
603 state = 0;
|
|
604 } else {
|
|
605 state = 3;
|
|
606 }
|
|
607 break;
|
|
608 case 7:
|
|
609 state = c is 'r' ? state + 1 : 0;
|
|
610 break;
|
|
611 case 8:
|
|
612 state = c is 'e' ? state + 1 : 0;
|
|
613 break;
|
|
614 case 9:
|
|
615 state = c is 'f' ? state + 1 : 0;
|
|
616 break;
|
|
617 case 10:
|
|
618 state = c is '=' ? state + 1 : 0;
|
|
619 break;
|
|
620 case 11:
|
|
621 if (c is '"') {
|
|
622 state++;
|
|
623 refStart = index + 1;
|
|
624 } else {
|
|
625 state = 0;
|
|
626 }
|
|
627 break;
|
|
628 case 12:
|
|
629 if (c is '"') {
|
51
|
630 ids[linkIndex] = buffer[ refStart .. index ]._idup();
|
25
|
631 state = 2;
|
|
632 }
|
|
633 break;
|
|
634 case 13:
|
51
|
635 if (Character.isWhitespace (c)) {
|
25
|
636 state = 0;
|
|
637 } else if (c is '='){
|
|
638 state++;
|
|
639 }
|
|
640 break;
|
|
641 case 14:
|
|
642 state = c is '"' ? state + 1 : 0;
|
|
643 break;
|
|
644 case 15:
|
|
645 if (c is '"') state = 2;
|
|
646 break;
|
|
647 default:
|
|
648 state = 0;
|
|
649 break;
|
|
650 }
|
|
651 index+=increment;
|
|
652 }
|
|
653 if (start < length_) {
|
|
654 int tmp = parseMnemonics (buffer, start, tagStart, result);
|
|
655 int mnemonic = parseMnemonics (buffer, Math.max (tagStart, linkStart), length_, result);
|
|
656 if (mnemonic is -1) mnemonic = tmp;
|
|
657 mnemonics [linkIndex] = mnemonic;
|
|
658 } else {
|
|
659 mnemonics [linkIndex] = -1;
|
|
660 }
|
|
661 if (offsets.length !is linkIndex) {
|
|
662 Point [] newOffsets = new Point [linkIndex];
|
|
663 System.arraycopy (offsets, 0, newOffsets, 0, linkIndex);
|
|
664 offsets = newOffsets;
|
|
665 String [] newIDs = new String [linkIndex];
|
|
666 System.arraycopy (ids, 0, newIDs, 0, linkIndex);
|
|
667 ids = newIDs;
|
|
668 int [] newMnemonics = new int [linkIndex + 1];
|
|
669 System.arraycopy (mnemonics, 0, newMnemonics, 0, linkIndex + 1);
|
|
670 mnemonics = newMnemonics;
|
|
671 }
|
|
672 return result.toString ();
|
|
673 }
|
|
674
|
|
675 int parseMnemonics (char[] buffer, int start, int end, StringBuffer result) {
|
|
676 int mnemonic = -1, index = start;
|
|
677 while (index < end) {
|
|
678 int incr = 1;
|
|
679 if ( buffer[index] is '&') {
|
|
680 if (index + 1 < end && buffer [index + 1] is '&') {
|
|
681 result.append (buffer [index]);
|
|
682 index++;
|
|
683 } else {
|
|
684 mnemonic = result.length();
|
|
685 }
|
|
686 } else {
|
|
687 result.append ( firstCodePointStr( buffer [index .. $ ], incr ));
|
|
688 }
|
|
689 index+=incr;
|
|
690 }
|
|
691 return mnemonic;
|
|
692 }
|
|
693
|
|
694 override int setBounds(int x, int y, int width, int height, bool move, bool resize) {
|
|
695 int result = super.setBounds (x, y, width,height, move, resize);
|
|
696 if ((result & RESIZED) !is 0) {
|
|
697 layout.setWidth (width > 0 ? width : -1);
|
|
698 redraw ();
|
|
699 }
|
|
700 return result;
|
|
701 }
|
|
702
|
|
703 override void setFontDescription (PangoFontDescription* font) {
|
|
704 super.setFontDescription (font);
|
|
705 layout.setFont (Font.gtk_new (display, font));
|
|
706 }
|
|
707
|
|
708 /**
|
|
709 * Sets the receiver's text.
|
|
710 * <p>
|
|
711 * The string can contain both regular text and hyperlinks. A hyperlink
|
|
712 * is delimited by an anchor tag, <A> and </A>. Within an
|
|
713 * anchor, a single HREF attribute is supported. When a hyperlink is
|
|
714 * selected, the text field of the selection event contains either the
|
|
715 * text of the hyperlink or the value of its HREF, if one was specified.
|
|
716 * In the rare case of identical hyperlinks within the same string, the
|
|
717 * HREF tag can be used to distinguish between them. The string may
|
|
718 * include the mnemonic character and line delimiters.
|
|
719 * </p>
|
|
720 *
|
|
721 * @param string the new text
|
|
722 *
|
|
723 * @exception SWTException <ul>
|
|
724 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
725 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
726 * </ul>
|
|
727 */
|
|
728 public void setText (String string) {
|
|
729 checkWidget ();
|
|
730 // SWT extension: allow null for zero length string
|
|
731 //if (string is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
732 if (string.equals(text)) return;
|
|
733 text = string;
|
|
734 layout.setText (parse (string));
|
|
735 focusIndex = offsets.length > 0 ? 0 : -1;
|
|
736 selection.x = selection.y = -1;
|
|
737 bool enabled = (state & DISABLED) is 0;
|
|
738 TextStyle linkStyle = new TextStyle (null, enabled ? linkColor : disabledColor, null);
|
|
739 linkStyle.underline = true;
|
|
740 int [] bidiSegments = new int [offsets.length*2];
|
|
741 for (int i = 0; i < offsets.length; i++) {
|
|
742 Point point = offsets [i];
|
|
743 layout.setStyle (linkStyle, point.x, point.y);
|
|
744 bidiSegments[i*2] = point.x;
|
|
745 bidiSegments[i*2+1] = point.y+1;
|
|
746 }
|
|
747 layout.setSegments (bidiSegments);
|
|
748 TextStyle mnemonicStyle = new TextStyle (null, null, null);
|
|
749 mnemonicStyle.underline = true;
|
|
750 for (int i = 0; i < mnemonics.length; i++) {
|
|
751 int mnemonic = mnemonics [i];
|
|
752 if (mnemonic !is -1) {
|
|
753 layout.setStyle (mnemonicStyle, mnemonic, mnemonic);
|
|
754 }
|
|
755 }
|
|
756 redraw ();
|
|
757 }
|
|
758
|
|
759 override void showWidget () {
|
|
760 super.showWidget ();
|
|
761 fixStyle (handle);
|
|
762 }
|
|
763
|
|
764 override int traversalCode (int key, GdkEventKey* event) {
|
|
765 if (offsets.length is 0) return 0;
|
|
766 int bits = super.traversalCode (key, event);
|
|
767 if (key is OS.GDK_Tab && focusIndex < offsets.length - 1) {
|
|
768 return bits & ~SWT.TRAVERSE_TAB_NEXT;
|
|
769 }
|
|
770 if (key is OS.GDK_ISO_Left_Tab && focusIndex > 0) {
|
|
771 return bits & ~SWT.TRAVERSE_TAB_PREVIOUS;
|
|
772 }
|
|
773 return bits;
|
|
774 }
|
|
775 }
|