comparison snippets/styledtext/Snippet244.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
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.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 dwt.DWT;
23 import dwt.custom.StyledText;
24 import dwt.custom.StyleRange;
25 import dwt.layout.FillLayout;
26 import dwt.widgets.Display;
27 import dwt.widgets.Shell;
28 import dwt.widgets.Listener;
29 import dwt.widgets.Event;
30 import dwt.graphics.Color;
31 import dwt.graphics.Point;
32
33 import dwt.dwthelper.utils;
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(DWT.COLOR_RED);
42 Shell shell = new Shell(display);
43 shell.setBounds(10,10,250,250);
44 StyledText text = new StyledText(shell, DWT.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(DWT.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 }