Mercurial > projects > dwt-samples
comparison snippets/styledtext/Snippet217.d @ 155:04d05db6dca4
styledtext: Snippet211, Snippet213, Snippet217, Snippet218, Snippet222, Snippet244
author | yidabu <yidabu@gmail.com> |
---|---|
date | Fri, 22 Aug 2008 07:27:56 +0800 |
parents | |
children | 8d6ec2b0357c |
comparison
equal
deleted
inserted
replaced
154:57cb6d948bf7 | 155:04d05db6dca4 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2004 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 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) | |
12 *******************************************************************************/ | |
13 | |
14 module styledtext.Snippet217; | |
15 /** | |
16 * StyledText snippet: embed controls | |
17 * | |
18 * For a list of all SWT example snippets see | |
19 * http://www.eclipse.org/swt/snippets/ | |
20 * | |
21 * @since 3.2 | |
22 */ | |
23 | |
24 import dwt.DWT; | |
25 import dwt.custom.StyledText; | |
26 import dwt.custom.StyleRange; | |
27 import dwt.custom.PaintObjectEvent; | |
28 import dwt.custom.PaintObjectListener; | |
29 import dwt.layout.GridData; | |
30 import dwt.layout.GridLayout; | |
31 import dwt.widgets.Display; | |
32 import dwt.widgets.Shell; | |
33 import dwt.widgets.Control; | |
34 import dwt.widgets.Button; | |
35 import dwt.widgets.Combo; | |
36 import dwt.widgets.Event; | |
37 import dwt.widgets.Listener; | |
38 import dwt.graphics.FontData; | |
39 import dwt.graphics.Font; | |
40 import dwt.graphics.Rectangle; | |
41 import dwt.graphics.Point; | |
42 import dwt.graphics.GC; | |
43 import dwt.graphics.Image; | |
44 import dwt.graphics.GlyphMetrics; | |
45 | |
46 import dwt.dwthelper.utils; | |
47 version(JIVE){ | |
48 import jive.stacktrace; | |
49 } | |
50 | |
51 void main() { | |
52 static StyledText styledText; | |
53 static String text = | |
54 "This snippet shows how to embed widgets in a StyledText.\n" | |
55 "Here is one: \uFFFC, and here is another: \uFFFC."; | |
56 static int[] offsets; | |
57 static Control[] controls; | |
58 static int MARGIN = 5; | |
59 | |
60 void addControl(Control control, int offset) { | |
61 StyleRange style = new StyleRange (); | |
62 style.start = offset; | |
63 style.length = 1; | |
64 control.pack(); | |
65 Rectangle rect = control.getBounds(); | |
66 int ascent = 2*rect.height/3; | |
67 int descent = rect.height - ascent; | |
68 style.metrics = new GlyphMetrics(ascent + MARGIN, descent + MARGIN, rect.width + 2*MARGIN); | |
69 styledText.setStyleRange(style); | |
70 } | |
71 | |
72 | |
73 Display display = new Display(); | |
74 Font font = new Font(display, "Tahoma", 32f, DWT.NORMAL); | |
75 Shell shell = new Shell(display); | |
76 shell.setLayout(new GridLayout()); | |
77 styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER); | |
78 styledText.setFont(font); | |
79 styledText.setLayoutData(new GridData(DWT.FILL, DWT.FILL, true, true)); | |
80 styledText.setText(text); | |
81 controls = new Control[2]; | |
82 Button button = new Button(styledText, DWT.PUSH); | |
83 button.setText("Button 1"); | |
84 controls[0] = button; | |
85 Combo combo = new Combo(styledText, DWT.NONE); | |
86 combo.add("item 1"); | |
87 combo.add("another item"); | |
88 controls[1] = combo; | |
89 offsets = new int[controls.length]; | |
90 int lastOffset = 0; | |
91 for (int i = 0; i < controls.length; i++) { | |
92 int offset = text.indexOf("\uFFFC", lastOffset); | |
93 offsets[i] = offset; | |
94 addControl(controls[i], offsets[i]); | |
95 lastOffset = offset + 1; | |
96 } | |
97 | |
98 void onVerify(Event e) { | |
99 int start = e.start; | |
100 int replaceCharCount = e.end - e.start; | |
101 int newCharCount = e.text.length; | |
102 for (int i = 0; i < offsets.length; i++) { | |
103 int offset = offsets[i]; | |
104 if (start <= offset && offset < start + replaceCharCount) { | |
105 // this widget is being deleted from the text | |
106 if (controls[i] !is null && !controls[i].isDisposed()) { | |
107 controls[i].dispose(); | |
108 controls[i] = null; | |
109 } | |
110 offset = -1; | |
111 } | |
112 if (offset != -1 && offset >= start) offset += newCharCount - replaceCharCount; | |
113 offsets[i] = offset; | |
114 } | |
115 } | |
116 // use a verify listener to keep the offsets up to date | |
117 styledText.addListener(DWT.Verify, dgListener(&onVerify)); | |
118 | |
119 // reposition widgets on paint event | |
120 styledText.addPaintObjectListener(new class(offsets) PaintObjectListener { | |
121 int[] offsets; | |
122 this(int[] offsets_) { | |
123 this.offsets = offsets_; | |
124 } | |
125 public void paintObject(PaintObjectEvent event) { | |
126 StyleRange style = event.style; | |
127 int start = style.start; | |
128 for (int i = 0; i < offsets.length; i++) { | |
129 int offset = offsets[i]; | |
130 if (start == offset) { | |
131 Point pt = controls[i].getSize(); | |
132 int x = event.x + MARGIN; | |
133 int y = event.y + event.ascent - 2*pt.y/3; | |
134 controls[i].setLocation(x, y); | |
135 break; | |
136 } | |
137 } | |
138 } | |
139 }); | |
140 | |
141 shell.setSize(400, 400); | |
142 shell.open(); | |
143 while (!shell.isDisposed()) { | |
144 if (!display.readAndDispatch()) | |
145 display.sleep(); | |
146 } | |
147 font.dispose(); | |
148 display.dispose(); | |
149 } |