Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet212.d @ 120:536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
===D2===
* added [Try]Immutable/Const/Shared templates to work with differenses in D1/D2 instead of version statements
used these templates to work with strict type storage rules of dmd-2.053
* com.ibm.icu now also compilable with D2, but not tested yet
* small fixes
Snippet288 - shared data is in TLS
===Phobos===
* fixed critical bugs in Phobos implemention
completely incorrect segfault prone fromStringz (Linux's port ruthless killer)
terrible, incorrect StringBuffer realization (StyledText killer)
* fixed small bugs as well
Snippet72 - misprint in the snippet
* implemented missed functionality for Phobos
ByteArrayOutputStream implemented (image loading available)
formatting correctly works for all DWT's cases
As a result, folowing snippets now works with Phobos (Snippet### - what is fixed):
Snippet24, 42, 111, 115, 130, 235, 276 - bad string formatting
Snippet48, 282 - crash on image loading
Snippet163, 189, 211, 213, 217, 218, 222 - crash on copy/cut in StyledText
Snippet244 - hang-up
===Tango===
* few changes for the latest Tango trunc-r5661
* few small performance improvments
===General===
* implMissing-s for only one version changed to implMissingInTango/InPhobos
* incorrect calls to Format in toString-s fixed
* fixed loading \uXXXX characters in ResourceBundle
* added good UTF-8 support for StyledText, TextLayout (Win32) and friends
UTF functions revised and tested. It is now in java.nonstandard.*Utf modules
StyledText and TextLayout (Win32) modules revised for UTF-8 support
* removed small diferences in most identical files in *.swt.* folders
*.swt.internal.image, *.swt.events and *.swt.custom are identical in Win32/Linux32
now 179 of 576 (~31%) files in *.swt.* folders are fully identical
* Win32: snippets now have right subsystem, pretty icons and native system style controls
* small fixes in snippets
Snippet44 - it's not Snippet44
Snippet212 - functions work with different images and offsets arrays
Win32: Snippet282 - crash on close if the button has an image
Snippet293 - setGrayed is commented
and others
Win32: As a result, folowing snippets now works
Snippet68 - color doesn't change
Snippet163, 189, 211, 213, 217, 218, 222 - UTF-8 issues (see above)
Snippet193 - no tabel headers
author | Denis Shelomovskij <verylonglogin.reg@gmail.com> |
---|---|
date | Sat, 09 Jul 2011 15:50:20 +0300 |
parents | 9f4c18c268b2 |
children |
rev | line source |
---|---|
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 *******************************************************************************/ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
13 |
28 | 14 module org.eclipse.swt.snippets.Snippet212; |
15 /** | |
16 * StyledText snippet: embed images | |
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 org.eclipse.swt.SWT; | |
25 import org.eclipse.swt.custom.StyledText; | |
26 import org.eclipse.swt.custom.StyleRange; | |
27 import org.eclipse.swt.layout.GridLayout; | |
28 import org.eclipse.swt.layout.GridData; | |
29 import org.eclipse.swt.widgets.Display; | |
30 import org.eclipse.swt.widgets.Shell; | |
31 import org.eclipse.swt.widgets.Button; | |
32 import org.eclipse.swt.widgets.FileDialog; | |
33 import org.eclipse.swt.widgets.Event; | |
34 import org.eclipse.swt.widgets.Listener; | |
35 import org.eclipse.swt.custom.PaintObjectEvent; | |
36 import org.eclipse.swt.custom.PaintObjectListener; | |
37 import org.eclipse.swt.events.VerifyEvent; | |
38 import org.eclipse.swt.events.VerifyListener; | |
39 import org.eclipse.swt.graphics.FontData; | |
40 import org.eclipse.swt.graphics.Font; | |
41 import org.eclipse.swt.graphics.Rectangle; | |
42 import org.eclipse.swt.graphics.GC; | |
43 import org.eclipse.swt.graphics.Image; | |
44 import org.eclipse.swt.graphics.GlyphMetrics; | |
45 import java.lang.all; | |
46 | |
112 | 47 const String OBJ_MARKER = "\uFFFC"; |
28 | 48 |
49 void main() { | |
50 static StyledText styledText; | |
51 static String text = | |
52 "This snippet shows how to embed images in a StyledText.\n" | |
53 "Here is one: " ~ OBJ_MARKER ~ ", and here is another: " ~ OBJ_MARKER ~ "." | |
54 "Use the add button to add an image from your filesystem to the StyledText at the current caret offset."; | |
55 static Image[] images; | |
56 static int[] offsets; | |
57 | |
58 static void addImage(Image image, int offset) { | |
59 StyleRange style = new StyleRange (); | |
60 style.start = offset; | |
61 style.length = OBJ_MARKER.length; | |
62 Rectangle rect = image.getBounds(); | |
63 style.metrics = new GlyphMetrics(rect.height, 0, rect.width); | |
64 styledText.setStyleRange(style); | |
65 } | |
66 | |
67 Display display = new Display(); | |
68 Shell shell = new Shell(display); | |
69 shell.setLayout(new GridLayout()); | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
70 styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); |
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
71 styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
28 | 72 styledText.setText(text); |
73 images = [ | |
74 display.getSystemImage(SWT.ICON_QUESTION), | |
75 display.getSystemImage(SWT.ICON_INFORMATION) | |
76 ]; | |
77 offsets = new int[images.length]; | |
78 int lastOffset = 0; | |
79 for (int i = 0; i < images.length; i++) { | |
80 int offset = text.indexOf(OBJ_MARKER, lastOffset); | |
81 offsets[i] = offset; | |
82 addImage(images[i], offset); | |
83 lastOffset = offset + 1; | |
84 } | |
85 | |
86 void onVerify(Event e) { | |
87 int start = e.start; | |
88 int replaceCharCount = e.end - e.start; | |
89 int newCharCount = e.text.length; | |
90 for (int i = 0; i < offsets.length; i++) { | |
91 int offset = offsets[i]; | |
92 if (start <= offset && offset < start + replaceCharCount) { | |
93 // this image is being deleted from the text | |
94 if (images[i] !is null && !images[i].isDisposed()) { | |
95 images[i].dispose(); | |
96 images[i] = null; | |
97 } | |
98 offset = -1; | |
99 } | |
100 if (offset != -1 && offset >= start) offset += newCharCount - replaceCharCount; | |
101 offsets[i] = offset; | |
102 } | |
103 } | |
104 // use a verify listener to keep the offsets up to date | |
105 styledText.addListener(SWT.Verify, dgListener(&onVerify)); | |
106 | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
112
diff
changeset
|
107 styledText.addPaintObjectListener(new class PaintObjectListener { |
28 | 108 public void paintObject(PaintObjectEvent event) { |
109 GC gc = event.gc; | |
110 StyleRange style = event.style; | |
111 int start = style.start; | |
112 for (int i = 0; i < offsets.length; i++) { | |
113 int offset = offsets[i]; | |
114 if (start == offset) { | |
115 Image image = images[i]; | |
116 int x = event.x; | |
117 int y = event.y + event.ascent - style.metrics.ascent; | |
118 gc.drawImage(image, x, y); | |
119 } | |
120 } | |
121 } | |
122 }); | |
123 | |
124 Button button = new Button (shell, SWT.PUSH); | |
125 button.setText("Add Image"); | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
126 button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); |
28 | 127 |
128 void onSelection(Event e) { | |
129 FileDialog dialog = new FileDialog(shell); | |
130 String filename = dialog.open(); | |
131 if (filename !is null) { | |
132 try { | |
133 Image image = new Image(display, filename); | |
134 int offset = styledText.getCaretOffset(); | |
135 styledText.replaceTextRange(offset, 0, OBJ_MARKER); | |
136 int index = 0; | |
137 while (index < offsets.length) { | |
138 if (offsets[index] == -1 && images[index] is null) break; | |
139 index++; | |
140 } | |
141 if (index == offsets.length) { | |
142 int[] tmpOffsets = new int[index + 1]; | |
143 System.arraycopy(offsets, 0, tmpOffsets, 0, offsets.length); | |
144 offsets = tmpOffsets; | |
145 Image[] tmpImages = new Image[index + 1]; | |
146 System.arraycopy(images, 0, tmpImages, 0, images.length); | |
147 images = tmpImages; | |
148 } | |
149 offsets[index] = offset; | |
150 images[index] = image; | |
151 addImage(image, offset); | |
152 } catch (Exception e) { | |
153 ExceptionPrintStackTrace(e); | |
154 } | |
155 } | |
156 } | |
157 button.addListener(SWT.Selection, dgListener(&onSelection)); | |
158 shell.setSize(400, 400); | |
159 shell.open(); | |
160 while (!shell.isDisposed()) { | |
161 if (!display.readAndDispatch()) | |
162 display.sleep(); | |
163 } | |
164 for (int i = 0; i < images.length; i++) { | |
165 Image image = images[i]; | |
166 if (image !is null && !image.isDisposed()) { | |
167 image.dispose(); | |
168 } | |
169 } | |
170 display.dispose(); | |
171 | |
172 } | |
173 |