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