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 module org.eclipse.swt.snippets.Snippet244;
|
|
15 /*
|
|
16 * StyledText snippet: Draw a box around text.
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21
|
|
22 import org.eclipse.swt.SWT;
|
|
23 import org.eclipse.swt.custom.StyledText;
|
|
24 import org.eclipse.swt.custom.StyleRange;
|
|
25 import org.eclipse.swt.layout.FillLayout;
|
|
26 import org.eclipse.swt.widgets.Display;
|
|
27 import org.eclipse.swt.widgets.Shell;
|
|
28 import org.eclipse.swt.widgets.Listener;
|
|
29 import org.eclipse.swt.widgets.Event;
|
|
30 import org.eclipse.swt.graphics.Color;
|
|
31 import org.eclipse.swt.graphics.Point;
|
|
32
|
|
33 import java.lang.all;
|
|
34 version(JIVE){
|
|
35 import jive.stacktrace;
|
|
36 }
|
|
37
|
|
38 void main() {
|
|
39 static String SEARCH_STRING = "box";
|
|
40 Display display = new Display();
|
|
41 Color RED = display.getSystemColor(SWT.COLOR_RED);
|
|
42 Shell shell = new Shell(display);
|
|
43 shell.setBounds(10,10,250,250);
|
|
44 StyledText text = new StyledText(shell, SWT.NONE);
|
|
45 text.setBounds(10,10,200,200);
|
|
46
|
|
47 void onPaint(Event event) {
|
|
48 String contents = text.getText();
|
|
49 int stringWidth = event.gc.stringExtent(SEARCH_STRING).x;
|
|
50 int lineHeight = text.getLineHeight();
|
|
51 event.gc.setForeground(RED);
|
|
52 int index = contents.indexOf(SEARCH_STRING);
|
|
53 while (index != -1) {
|
|
54 Point topLeft = text.getLocationAtOffset(index);
|
|
55 event.gc.drawRectangle(topLeft.x - 1, topLeft.y, stringWidth + 1, lineHeight - 1);
|
|
56 index = contents.indexOf(SEARCH_STRING, index + 1);
|
|
57 }
|
|
58 }
|
|
59
|
|
60 text.addListener(SWT.Paint, dgListener(&onPaint));
|
|
61
|
|
62 text.setText("This demonstrates drawing a box\naround every occurrence of the word\nbox in the StyledText");
|
|
63 shell.open();
|
|
64 while (!shell.isDisposed()) {
|
|
65 if (!display.readAndDispatch()) display.sleep();
|
|
66 }
|
|
67 display.dispose();
|
|
68 }
|