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