comparison snippets/styledtext/Snippet212.d @ 154:57cb6d948bf7

Fix visible OBJECT REPLACEMENT CHARACTER. In utf8 length is 2, which needed to be take care of in style.length.
author Frank Benoit <benoit@tionex.de>
date Thu, 21 Aug 2008 18:29:38 +0200
parents 297120c376f7
children
comparison
equal deleted inserted replaced
153:8907b6374258 154:57cb6d948bf7
68 import dwt.graphics.Image; 68 import dwt.graphics.Image;
69 import dwt.graphics.GlyphMetrics; 69 import dwt.graphics.GlyphMetrics;
70 import dwt.dwthelper.utils; 70 import dwt.dwthelper.utils;
71 import dwt.dwthelper.System; 71 import dwt.dwthelper.System;
72 72
73 const char[] OBJ_MARKER = "\uFFFC";
74
73 void main() { 75 void main() {
74 static StyledText styledText; 76 static StyledText styledText;
75 static String text = 77 static String text =
76 "This snippet shows how to embed images in a StyledText.\n" 78 "This snippet shows how to embed images in a StyledText.\n"
77 "Here is one: \uFFFC, and here is another: \uFFFC." 79 "Here is one: " ~ OBJ_MARKER ~ ", and here is another: " ~ OBJ_MARKER ~ "."
78 "Use the add button to add an image from your filesystem to the StyledText at the current caret offset."; 80 "Use the add button to add an image from your filesystem to the StyledText at the current caret offset.";
79 static Image[] images; 81 static Image[] images;
80 static int[] offsets; 82 static int[] offsets;
81 83
82 static void addImage(Image image, int offset) { 84 static void addImage(Image image, int offset) {
83 StyleRange style = new StyleRange (); 85 StyleRange style = new StyleRange ();
84 style.start = offset; 86 style.start = offset;
85 style.length = 1; 87 style.length = OBJ_MARKER.length;
86 Rectangle rect = image.getBounds(); 88 Rectangle rect = image.getBounds();
87 style.metrics = new GlyphMetrics(rect.height, 0, rect.width); 89 style.metrics = new GlyphMetrics(rect.height, 0, rect.width);
88 styledText.setStyleRange(style); 90 styledText.setStyleRange(style);
89 } 91 }
90 92
99 display.getSystemImage(DWT.ICON_INFORMATION) 101 display.getSystemImage(DWT.ICON_INFORMATION)
100 ]; 102 ];
101 offsets = new int[images.length]; 103 offsets = new int[images.length];
102 int lastOffset = 0; 104 int lastOffset = 0;
103 for (int i = 0; i < images.length; i++) { 105 for (int i = 0; i < images.length; i++) {
104 int offset = text.indexOf("\uFFFC", lastOffset); 106 int offset = text.indexOf(OBJ_MARKER, lastOffset);
105 offsets[i] = offset; 107 offsets[i] = offset;
106 addImage(images[i], offset); 108 addImage(images[i], offset);
107 lastOffset = offset + 1; 109 lastOffset = offset + 1;
108 } 110 }
109 111
160 String filename = dialog.open(); 162 String filename = dialog.open();
161 if (filename !is null) { 163 if (filename !is null) {
162 try { 164 try {
163 Image image = new Image(display, filename); 165 Image image = new Image(display, filename);
164 int offset = styledText.getCaretOffset(); 166 int offset = styledText.getCaretOffset();
165 styledText.replaceTextRange(offset, 0, "\uFFFC"); 167 styledText.replaceTextRange(offset, 0, OBJ_MARKER);
166 int index = 0; 168 int index = 0;
167 while (index < offsets.length) { 169 while (index < offsets.length) {
168 if (offsets[index] == -1 && images[index] is null) break; 170 if (offsets[index] == -1 && images[index] is null) break;
169 index++; 171 index++;
170 } 172 }
180 images[index] = image; 182 images[index] = image;
181 addImage(image, offset); 183 addImage(image, offset);
182 } catch (Exception e) { 184 } catch (Exception e) {
183 ExceptionPrintStackTrace(e); 185 ExceptionPrintStackTrace(e);
184 } 186 }
185 } 187 }
186 } 188 }
187 button.addListener(DWT.Selection, dgListener(&onSelection)); 189 button.addListener(DWT.Selection, dgListener(&onSelection));
188 shell.setSize(400, 400); 190 shell.setSize(400, 400);
189 shell.open(); 191 shell.open();
190 while (!shell.isDisposed()) { 192 while (!shell.isDisposed()) {