1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet211"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * Port to the D programming language:
46  *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
47  *******************************************************************************/
48 
49 module org.eclipse.swt.snippets.Snippet211;
50 /*
51  * SWT StyledText snippet: use rise and font with StyleRange.
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  *
56  * @since 3.2
57  */
58 
59 
60 import org.eclipse.swt.SWT;
61 import org.eclipse.swt.custom.StyledText;
62 import org.eclipse.swt.custom.StyleRange;
63 import org.eclipse.swt.layout.FillLayout;
64 import org.eclipse.swt.widgets.Display;
65 import org.eclipse.swt.widgets.Shell;
66 import org.eclipse.swt.widgets.Event;
67 import org.eclipse.swt.widgets.Listener;
68 import org.eclipse.swt.graphics.FontData;
69 import org.eclipse.swt.graphics.Font;
70 import org.eclipse.swt.graphics.Rectangle;
71 import org.eclipse.swt.graphics.GC;
72 import org.eclipse.swt.graphics.Image;
73 import java.lang.all;
74 version(JIVE){
75     import jive.stacktrace;
76 }
77 
78 void main() {
79     static String text =
80         "You can set any font you want in a range. You can also set a baseline rise and all other old features" ~
81         " like background and foreground, and mix them any way you want. Totally awesome.";
82 
83     Display display = new Display();
84     Shell shell = new Shell(display);
85     shell.setLayout(new FillLayout());
86     StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
87     styledText.setText(text);
88     FontData data = styledText.getFont().getFontData()[0];
89     Font font1 = new Font(display, data.getName(), data.getHeight() * 2f, data.getStyle());
90     Font font2 = new Font(display, data.getName(), data.getHeight() * 4f / 5, data.getStyle());
91     StyleRange[] styles = new StyleRange[8];
92     styles[0] = new StyleRange();
93     styles[0].font = font1;
94     styles[1] = new StyleRange();
95     styles[1].rise = data.getHeight() / 3;
96     styles[2] = new StyleRange();
97     styles[2].background = display.getSystemColor(SWT.COLOR_GREEN);
98     styles[3] = new StyleRange();
99     styles[3].foreground = display.getSystemColor(SWT.COLOR_MAGENTA);
100     styles[4] = new StyleRange();
101     styles[4].font = font2;
102     styles[4].foreground = display.getSystemColor(SWT.COLOR_BLUE);
103     styles[4].underline = true;
104     styles[5] = new StyleRange();
105     styles[5].rise = -data.getHeight() / 3;
106     styles[5].strikeout = true;
107     styles[5].underline = true;
108     styles[6] = new StyleRange();
109     styles[6].font = font1;
110     styles[6].foreground = display.getSystemColor(SWT.COLOR_YELLOW);
111     styles[6].background = display.getSystemColor(SWT.COLOR_BLUE);
112     styles[7] = new StyleRange();
113     styles[7].rise =  data.getHeight() / 3;
114     styles[7].underline = true;
115     styles[7].fontStyle = SWT.BOLD;
116     styles[7].foreground = display.getSystemColor(SWT.COLOR_RED);
117     styles[7].background = display.getSystemColor(SWT.COLOR_BLACK);
118 
119     int[] ranges = [16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7];
120     styledText.setStyleRanges(ranges, styles);
121 
122     shell.setSize(300, 300);
123     shell.open();
124     while (!shell.isDisposed()) {
125         if (!display.readAndDispatch())
126             display.sleep();
127     }
128     font1.dispose();
129     font2.dispose();
130     display.dispose();
131 
132 }