1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet222"
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.Snippet222;
50 /*
51  * example snippet: StyledText bulleted list example
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 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.custom.StyledText;
61 import org.eclipse.swt.custom.StyleRange;
62 import org.eclipse.swt.custom.PaintObjectEvent;
63 import org.eclipse.swt.custom.PaintObjectListener;
64 import org.eclipse.swt.custom.Bullet;
65 import org.eclipse.swt.custom.ST;
66 import org.eclipse.swt.layout.GridData;
67 import org.eclipse.swt.layout.FillLayout;
68 import org.eclipse.swt.widgets.Display;
69 import org.eclipse.swt.widgets.Shell;
70 import org.eclipse.swt.widgets.Button;
71 import org.eclipse.swt.widgets.Event;
72 import org.eclipse.swt.widgets.Listener;
73 import org.eclipse.swt.graphics.FontData;
74 import org.eclipse.swt.graphics.Font;
75 import org.eclipse.swt.graphics.TextLayout;
76 import org.eclipse.swt.graphics.GlyphMetrics;
77 version(Tango){
78     import tango.util.Convert;
79 } else { // Phobos
80     import std.conv;
81 }
82 
83 import java.lang.all;
84 version(JIVE){
85     import jive.stacktrace;
86 }
87 
88 void main() {
89     Display display = new Display();
90     Shell shell = new Shell(display);
91     shell.setText("StyledText Bullet Example");
92     shell.setLayout(new FillLayout());
93     StyledText styledText = new StyledText (shell, SWT.FULL_SELECTION | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
94     StringBuffer text = new StringBuffer();
95     text.append("Here is StyledText with some bulleted lists:\n\n");
96     for (int i = 0; i < 4; i++) text.append("Red Bullet List Item " ~ to!(String)(i) ~ "\n");
97     text.append("\n");
98     for (int i = 0; i < 2; i++) text.append("Numbered List Item " ~ to!(String)(i) ~ "\n");
99     for (int i = 0; i < 4; i++) text.append("Sub List Item " ~ to!(String)(i) ~ "\n");
100     for (int i = 0; i < 2; i++) text.append("Numbered List Item " ~ to!(String)(2+i) ~ "\n");
101     text.append("\n");
102     for (int i = 0; i < 4; i++) text.append("Custom Draw List Item " ~ to!(String)(i) ~ "\n");
103     styledText.setText(text.toString());
104 
105     StyleRange style0 = new StyleRange();
106     style0.metrics = new GlyphMetrics(0, 0, 40);
107     style0.foreground = display.getSystemColor(SWT.COLOR_RED);
108     Bullet bullet0 = new Bullet (style0);
109     StyleRange style1 = new StyleRange();
110     style1.metrics = new GlyphMetrics(0, 0, 50);
111     style1.foreground = display.getSystemColor(SWT.COLOR_BLUE);
112     Bullet bullet1 = new Bullet (ST.BULLET_NUMBER | ST.BULLET_TEXT, style1);
113     bullet1.text = ".";
114     StyleRange style2 = new StyleRange();
115     style2.metrics = new GlyphMetrics(0, 0, 80);
116     style2.foreground = display.getSystemColor(SWT.COLOR_GREEN);
117     Bullet bullet2 = new Bullet (ST.BULLET_TEXT, style2);
118     bullet2.text = "\u2713";
119     StyleRange style3 = new StyleRange();
120     style3.metrics = new GlyphMetrics(0, 0, 50);
121     Bullet bullet3 = new Bullet (ST.BULLET_CUSTOM, style2);
122 
123     styledText.setLineBullet(2, 4, bullet0);
124     styledText.setLineBullet(7, 2, bullet1);
125     styledText.setLineBullet(9, 4, bullet2);
126     styledText.setLineBullet(13, 2, bullet1);
127     styledText.setLineBullet(16, 4, bullet3);
128 
129     styledText.addPaintObjectListener(new class PaintObjectListener {
130         public void paintObject(PaintObjectEvent event) {
131             Display display = event.display;
132             StyleRange style = event.style;
133             Font font = style.font;
134             if (font is null) font = styledText.getFont();
135             TextLayout layout = new TextLayout(display);
136             layout.setAscent(event.ascent);
137             layout.setDescent(event.descent);
138             layout.setFont(font);
139             layout.setText("\u2023 1." ~ to!(String)( event.bulletIndex) ~ ")");
140             layout.draw(event.gc, event.x + 10, event.y);
141             layout.dispose();
142         }
143     });
144     shell.open();
145     while (!shell.isDisposed()) {
146         if (!display.readAndDispatch()) display.sleep();
147     }
148     display.dispose ();
149 }
150