28
|
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 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at ntdll.dll (0x7c90316c) thread(1480)
|
|
15 ->us
|
|
16 #0 ?? () at org.eclipse.swt\graphics\TextLayout.d:2915 from ntdll.dll
|
|
17 #1 0x77f205bf in ?? () at org.eclipse.swt\graphics\TextLayout.d:2915 from GDI32.dll
|
|
18 #2 0x00483c72 in org.eclipse.swt.graphics.TextLayout.TextLayout.shape () at dwt\graphics\TextLayout.d:2915
|
|
19 #3 0x0047bfff in org.eclipse.swt.graphics.TextLayout.TextLayout.computeRuns () at dwt\graphics\TextLayout.d:267
|
|
20 #4 0x00480474 in org.eclipse.swt.graphics.Rectangle.Rectangle dwt.graphics.TextLayout.TextLayout.getBounds () at dwt\graphics\TextLayout.d:1387
|
|
21 #5 0x00411ea7 in org.eclipse.swt.graphics.Point.Point dwt.custom.StyledText.StyledText.computeSize () at dwt\custom\StyledText.d:1784
|
|
22 #6 0x0041f24b in org.eclipse.swt.layout.GridData.GridData.computeSize () at dwt\layout\GridData.d:484
|
|
23 #7 0x0043258f in org.eclipse.swt.graphics.Point.Point dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:232
|
|
24 #8 0x00432325 in org.eclipse.swt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:208
|
|
25 #9 0x004999af in org.eclipse.swt.widgets.Composite.Composite.updateLayout () at dwt\widgets\Composite.d:1170
|
|
26 #10 0x00498eba in org.eclipse.swt.widgets.Composite.Composite.sendResize () at dwt\widgets\Composite.d:879
|
|
27 #11 0x00499053 in org.eclipse.swt.widgets.Composite.Composite.setBounds () at dwt\widgets\Composite.d:924
|
|
28 #12 0x004d959d in org.eclipse.swt.widgets.Decorations.Decorations.setBounds () at dwt\widgets\Decorations.d:888
|
|
29 #13 0x0042f9a2 in org.eclipse.swt.widgets.Shell.Shell.setBounds () at dwt\widgets\Shell.d:1460
|
|
30 #14 0x004733f3 in org.eclipse.swt.widgets.Control.Control.setBounds () at dwt\widgets\Control.d:2639
|
|
31 #15 0x00473d27 in org.eclipse.swt.widgets.Control.Control.setSize () at dwt\widgets\Control.d:3153
|
|
32 #16 0x004042c8 in _Dmain () at Snippet212.d:171
|
|
33 #17 0x00645778 in extern (C) int dmain2.main(int, char**) . void runMain(void*) () from dmain2
|
|
34 #18 0x006457af in extern (C) int dmain2.main(int, char**) . void runAll(void*) () from dmain2
|
|
35 #19 0x006454f0 in _main () from dmain2
|
|
36 #20 0x0065f801 in _mainCRTStartup () from constart
|
|
37 #21 0x7c816fd7 in ?? () from KERNEL32.dll
|
|
38 */
|
|
39 module org.eclipse.swt.snippets.Snippet212;
|
|
40 /**
|
|
41 * StyledText snippet: embed images
|
|
42 *
|
|
43 * For a list of all SWT example snippets see
|
|
44 * http://www.eclipse.org/swt/snippets/
|
|
45 *
|
|
46 * @since 3.2
|
|
47 */
|
|
48
|
|
49 import org.eclipse.swt.SWT;
|
|
50 import org.eclipse.swt.custom.StyledText;
|
|
51 import org.eclipse.swt.custom.StyleRange;
|
|
52 import org.eclipse.swt.layout.GridLayout;
|
|
53 import org.eclipse.swt.layout.GridData;
|
|
54 import org.eclipse.swt.widgets.Display;
|
|
55 import org.eclipse.swt.widgets.Shell;
|
|
56 import org.eclipse.swt.widgets.Button;
|
|
57 import org.eclipse.swt.widgets.FileDialog;
|
|
58 import org.eclipse.swt.widgets.Event;
|
|
59 import org.eclipse.swt.widgets.Listener;
|
|
60 import org.eclipse.swt.custom.PaintObjectEvent;
|
|
61 import org.eclipse.swt.custom.PaintObjectListener;
|
|
62 import org.eclipse.swt.events.VerifyEvent;
|
|
63 import org.eclipse.swt.events.VerifyListener;
|
|
64 import org.eclipse.swt.graphics.FontData;
|
|
65 import org.eclipse.swt.graphics.Font;
|
|
66 import org.eclipse.swt.graphics.Rectangle;
|
|
67 import org.eclipse.swt.graphics.GC;
|
|
68 import org.eclipse.swt.graphics.Image;
|
|
69 import org.eclipse.swt.graphics.GlyphMetrics;
|
|
70 import java.lang.all;
|
|
71
|
|
72 const char[] OBJ_MARKER = "\uFFFC";
|
|
73
|
|
74 void main() {
|
|
75 static StyledText styledText;
|
|
76 static String text =
|
|
77 "This snippet shows how to embed images in a StyledText.\n"
|
|
78 "Here is one: " ~ OBJ_MARKER ~ ", and here is another: " ~ OBJ_MARKER ~ "."
|
|
79 "Use the add button to add an image from your filesystem to the StyledText at the current caret offset.";
|
|
80 static Image[] images;
|
|
81 static int[] offsets;
|
|
82
|
|
83 static void addImage(Image image, int offset) {
|
|
84 StyleRange style = new StyleRange ();
|
|
85 style.start = offset;
|
|
86 style.length = OBJ_MARKER.length;
|
|
87 Rectangle rect = image.getBounds();
|
|
88 style.metrics = new GlyphMetrics(rect.height, 0, rect.width);
|
|
89 styledText.setStyleRange(style);
|
|
90 }
|
|
91
|
|
92 Display display = new Display();
|
|
93 Shell shell = new Shell(display);
|
|
94 shell.setLayout(new GridLayout());
|
|
95 styledText = new StyledText(shell, SWT.WRAP | DWT.BORDER);
|
|
96 styledText.setLayoutData(new GridData(SWT.FILL, DWT.FILL, true, true));
|
|
97 styledText.setText(text);
|
|
98 images = [
|
|
99 display.getSystemImage(SWT.ICON_QUESTION),
|
|
100 display.getSystemImage(SWT.ICON_INFORMATION)
|
|
101 ];
|
|
102 offsets = new int[images.length];
|
|
103 int lastOffset = 0;
|
|
104 for (int i = 0; i < images.length; i++) {
|
|
105 int offset = text.indexOf(OBJ_MARKER, lastOffset);
|
|
106 offsets[i] = offset;
|
|
107 addImage(images[i], offset);
|
|
108 lastOffset = offset + 1;
|
|
109 }
|
|
110
|
|
111 void onVerify(Event e) {
|
|
112 int start = e.start;
|
|
113 int replaceCharCount = e.end - e.start;
|
|
114 int newCharCount = e.text.length;
|
|
115 for (int i = 0; i < offsets.length; i++) {
|
|
116 int offset = offsets[i];
|
|
117 if (start <= offset && offset < start + replaceCharCount) {
|
|
118 // this image is being deleted from the text
|
|
119 if (images[i] !is null && !images[i].isDisposed()) {
|
|
120 images[i].dispose();
|
|
121 images[i] = null;
|
|
122 }
|
|
123 offset = -1;
|
|
124 }
|
|
125 if (offset != -1 && offset >= start) offset += newCharCount - replaceCharCount;
|
|
126 offsets[i] = offset;
|
|
127 }
|
|
128 }
|
|
129 // use a verify listener to keep the offsets up to date
|
|
130 styledText.addListener(SWT.Verify, dgListener(&onVerify));
|
|
131
|
|
132 styledText.addPaintObjectListener(new class(images, offsets) PaintObjectListener {
|
|
133 Image[] images;
|
|
134 int[] offsets;
|
|
135 this( Image[] images_, int[] offsets_) {
|
|
136 this.images = images_;
|
|
137 this.offsets = offsets_;
|
|
138 }
|
|
139 public void paintObject(PaintObjectEvent event) {
|
|
140 GC gc = event.gc;
|
|
141 StyleRange style = event.style;
|
|
142 int start = style.start;
|
|
143 for (int i = 0; i < offsets.length; i++) {
|
|
144 int offset = offsets[i];
|
|
145 if (start == offset) {
|
|
146 Image image = images[i];
|
|
147 int x = event.x;
|
|
148 int y = event.y + event.ascent - style.metrics.ascent;
|
|
149 gc.drawImage(image, x, y);
|
|
150 }
|
|
151 }
|
|
152 }
|
|
153 });
|
|
154
|
|
155 Button button = new Button (shell, SWT.PUSH);
|
|
156 button.setText("Add Image");
|
|
157 button.setLayoutData(new GridData(SWT.CENTER, DWT.CENTER, false, false));
|
|
158
|
|
159 void onSelection(Event e) {
|
|
160 FileDialog dialog = new FileDialog(shell);
|
|
161 String filename = dialog.open();
|
|
162 if (filename !is null) {
|
|
163 try {
|
|
164 Image image = new Image(display, filename);
|
|
165 int offset = styledText.getCaretOffset();
|
|
166 styledText.replaceTextRange(offset, 0, OBJ_MARKER);
|
|
167 int index = 0;
|
|
168 while (index < offsets.length) {
|
|
169 if (offsets[index] == -1 && images[index] is null) break;
|
|
170 index++;
|
|
171 }
|
|
172 if (index == offsets.length) {
|
|
173 int[] tmpOffsets = new int[index + 1];
|
|
174 System.arraycopy(offsets, 0, tmpOffsets, 0, offsets.length);
|
|
175 offsets = tmpOffsets;
|
|
176 Image[] tmpImages = new Image[index + 1];
|
|
177 System.arraycopy(images, 0, tmpImages, 0, images.length);
|
|
178 images = tmpImages;
|
|
179 }
|
|
180 offsets[index] = offset;
|
|
181 images[index] = image;
|
|
182 addImage(image, offset);
|
|
183 } catch (Exception e) {
|
|
184 ExceptionPrintStackTrace(e);
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188 button.addListener(SWT.Selection, dgListener(&onSelection));
|
|
189 shell.setSize(400, 400);
|
|
190 shell.open();
|
|
191 while (!shell.isDisposed()) {
|
|
192 if (!display.readAndDispatch())
|
|
193 display.sleep();
|
|
194 }
|
|
195 for (int i = 0; i < images.length; i++) {
|
|
196 Image image = images[i];
|
|
197 if (image !is null && !image.isDisposed()) {
|
|
198 image.dispose();
|
|
199 }
|
|
200 }
|
|
201 display.dispose();
|
|
202
|
|
203 }
|
|
204
|