changeset 155:04d05db6dca4

styledtext: Snippet211, Snippet213, Snippet217, Snippet218, Snippet222, Snippet244
author yidabu <yidabu@gmail.com>
date Fri, 22 Aug 2008 07:27:56 +0800
parents 57cb6d948bf7
children da0f1a62caba
files snippets/styledtext/Snippet211.d snippets/styledtext/Snippet213.d snippets/styledtext/Snippet217.d snippets/styledtext/Snippet218.d snippets/styledtext/Snippet222.d snippets/styledtext/Snippet244.d
diffstat 6 files changed, 579 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet211.d	Fri Aug 22 07:27:56 2008 +0800
@@ -0,0 +1,97 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+
+module styledtext.Snippet211;
+/*
+ * SWT StyledText snippet: use rise and font with StyleRange.
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ *
+ * @since 3.2
+ */
+
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.layout.FillLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.graphics.FontData;
+import dwt.graphics.Font;
+import dwt.graphics.Rectangle;
+import dwt.graphics.GC;
+import dwt.graphics.Image;
+import dwt.dwthelper.utils;
+version(JIVE){
+    import jive.stacktrace;
+}
+
+void main() {
+    static String text =
+        "You can set any font you want in a range. You can also set a baseline rise and all other old features"
+        " like background and foreground, and mix them any way you want. Totally awesome.";
+
+    Display display = new Display();
+    Shell shell = new Shell(display);
+    shell.setLayout(new FillLayout());
+    StyledText styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
+    styledText.setText(text);
+    FontData data = styledText.getFont().getFontData()[0];
+    Font font1 = new Font(display, data.getName(), data.getHeight() * 2f, data.getStyle());
+    Font font2 = new Font(display, data.getName(), data.getHeight() * 4f / 5, data.getStyle());
+    StyleRange[] styles = new StyleRange[8];
+    styles[0] = new StyleRange();
+    styles[0].font = font1;
+    styles[1] = new StyleRange();
+    styles[1].rise = data.getHeight() / 3;
+    styles[2] = new StyleRange();
+    styles[2].background = display.getSystemColor(DWT.COLOR_GREEN);
+    styles[3] = new StyleRange();
+    styles[3].foreground = display.getSystemColor(DWT.COLOR_MAGENTA);
+    styles[4] = new StyleRange();
+    styles[4].font = font2;
+    styles[4].foreground = display.getSystemColor(DWT.COLOR_BLUE);;
+    styles[4].underline = true;
+    styles[5] = new StyleRange();
+    styles[5].rise = -data.getHeight() / 3;
+    styles[5].strikeout = true;
+    styles[5].underline = true;
+    styles[6] = new StyleRange();
+    styles[6].font = font1;
+    styles[6].foreground = display.getSystemColor(DWT.COLOR_YELLOW);
+    styles[6].background = display.getSystemColor(DWT.COLOR_BLUE);
+    styles[7] = new StyleRange();
+    styles[7].rise =  data.getHeight() / 3;
+    styles[7].underline = true;
+    styles[7].fontStyle = DWT.BOLD;
+    styles[7].foreground = display.getSystemColor(DWT.COLOR_RED);
+    styles[7].background = display.getSystemColor(DWT.COLOR_BLACK);
+
+    int[] ranges = [16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7];
+    styledText.setStyleRanges(ranges, styles);
+
+    shell.setSize(300, 300);
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch())
+            display.sleep();
+    }
+    font1.dispose();
+    font2.dispose();
+    display.dispose();
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet213.d	Fri Aug 22 07:27:56 2008 +0800
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+
+module styledtext.Snippet213;
+/*
+ * SWT StyledText snippet: use indent, alignment and justify.
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ *
+ * @since 3.2
+ */
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.layout.FillLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+
+import dwt.dwthelper.utils;
+version(JIVE){
+    import jive.stacktrace;
+}
+
+void main() {
+    static String text =
+        "The first paragraph has an indentation of fifty pixels. Indentation is the amount of white space in front of the first line of a paragraph. If this paragraph wraps to several lines you should see the indentation only on the first line.\n\n"
+        "The second paragraph is center aligned. Alignment only works when the StyledText is using word wrap. Alignment, as with all other line attributes, can be set for the whole widget or just for a set of lines.\n\n"
+        "The third paragraph is justified. Like alignment, justify only works when the StyledText is using word wrap. If the paragraph wraps to several lines, the justification is performed on all lines but the last one.\n\n"
+        "The last paragraph is justified and right aligned. In this case, the alignment is only noticeable in the final line.";
+
+
+    Display display = new Display();
+    Shell shell = new Shell(display);
+    shell.setLayout(new FillLayout());
+    StyledText styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
+    styledText.setText(text);
+    styledText.setLineIndent(0, 1, 50);
+    styledText.setLineAlignment(2, 1, DWT.CENTER);
+    styledText.setLineJustify(4, 1, true);
+    styledText.setLineAlignment(6, 1, DWT.RIGHT);
+    styledText.setLineJustify(6, 1, true);
+
+    shell.setSize(300, 400);
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch())
+            display.sleep();
+    }
+    display.dispose();
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet217.d	Fri Aug 22 07:27:56 2008 +0800
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+
+module styledtext.Snippet217;
+/**
+ * StyledText snippet: embed controls
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ *
+ * @since 3.2
+ */
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.custom.PaintObjectEvent;
+import dwt.custom.PaintObjectListener;
+import dwt.layout.GridData;
+import dwt.layout.GridLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Control;
+import dwt.widgets.Button;
+import dwt.widgets.Combo;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.graphics.FontData;
+import dwt.graphics.Font;
+import dwt.graphics.Rectangle;
+import dwt.graphics.Point;
+import dwt.graphics.GC;
+import dwt.graphics.Image;
+import dwt.graphics.GlyphMetrics;
+
+import dwt.dwthelper.utils;
+version(JIVE){
+    import jive.stacktrace;
+}
+
+void main() {
+    static StyledText styledText;
+    static String text =
+        "This snippet shows how to embed widgets in a StyledText.\n"
+        "Here is one: \uFFFC, and here is another: \uFFFC.";
+    static int[] offsets;
+    static Control[] controls;
+    static int MARGIN = 5;
+
+    void addControl(Control control, int offset) {
+        StyleRange style = new StyleRange ();
+        style.start = offset;
+        style.length = 1;
+        control.pack();
+        Rectangle rect = control.getBounds();
+        int ascent = 2*rect.height/3;
+        int descent = rect.height - ascent;
+        style.metrics = new GlyphMetrics(ascent + MARGIN, descent + MARGIN, rect.width + 2*MARGIN);
+        styledText.setStyleRange(style);
+    }
+
+
+    Display display = new Display();
+    Font font = new Font(display, "Tahoma", 32f, DWT.NORMAL);
+    Shell shell = new Shell(display);
+    shell.setLayout(new GridLayout());
+    styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
+    styledText.setFont(font);
+    styledText.setLayoutData(new GridData(DWT.FILL, DWT.FILL, true, true));
+    styledText.setText(text);
+    controls = new Control[2];
+    Button button = new Button(styledText, DWT.PUSH);
+    button.setText("Button 1");
+    controls[0] = button;
+    Combo combo = new Combo(styledText, DWT.NONE);
+    combo.add("item 1");
+    combo.add("another item");
+    controls[1] = combo;
+    offsets = new int[controls.length];
+    int lastOffset = 0;
+    for (int i = 0; i < controls.length; i++) {
+        int offset = text.indexOf("\uFFFC", lastOffset);
+        offsets[i] = offset;
+        addControl(controls[i], offsets[i]);
+        lastOffset = offset + 1;
+    }
+
+    void onVerify(Event e) {
+        int start = e.start;
+        int replaceCharCount = e.end - e.start;
+        int newCharCount = e.text.length;
+        for (int i = 0; i < offsets.length; i++) {
+            int offset = offsets[i];
+            if (start <= offset && offset < start + replaceCharCount) {
+                // this widget is being deleted from the text
+                if (controls[i] !is null && !controls[i].isDisposed()) {
+                    controls[i].dispose();
+                    controls[i] = null;
+                }
+                offset = -1;
+            }
+            if (offset != -1 && offset >= start) offset += newCharCount - replaceCharCount;
+            offsets[i] = offset;
+        }
+    }
+    // use a verify listener to keep the offsets up to date
+    styledText.addListener(DWT.Verify, dgListener(&onVerify));
+
+    // reposition widgets on paint event
+    styledText.addPaintObjectListener(new class(offsets) PaintObjectListener {
+        int[] offsets;
+        this(int[] offsets_) {
+            this.offsets = offsets_;
+        }
+        public void paintObject(PaintObjectEvent event) {
+            StyleRange style = event.style;
+            int start = style.start;
+            for (int i = 0; i < offsets.length; i++) {
+                int offset = offsets[i];
+                if (start == offset) {
+                    Point pt = controls[i].getSize();
+                    int x = event.x + MARGIN;
+                    int y = event.y + event.ascent - 2*pt.y/3;
+                    controls[i].setLocation(x, y);
+                    break;
+                }
+            }
+        }
+    });
+
+    shell.setSize(400, 400);
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch())
+            display.sleep();
+    }
+    font.dispose();
+    display.dispose();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet218.d	Fri Aug 22 07:27:56 2008 +0800
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+
+module styledtext.Snippet218;
+/*
+ * SWT StyledText snippet: use gradient background.
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ *
+ * @since 3.2
+ */
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.layout.FillLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.graphics.FontData;
+import dwt.graphics.Font;
+import dwt.graphics.Rectangle;
+import dwt.graphics.GC;
+import dwt.graphics.Image;
+
+import dwt.dwthelper.utils;
+
+import Math = tango.math.Math;
+version(JIVE){
+    import jive.stacktrace;
+}
+
+void main() {
+    static String text = "Plans do not materialize out of nowhere, nor are they entirely static. To ensure the planning process is "
+        "transparent and open to the entire Eclipse community, we (the Eclipse PMC) post plans in an embryonic "
+        "form and revise them throughout the release cycle. \n"
+        "The first part of the plan deals with the important matters of release deliverables, release milestones, target "
+        "operating environments, and release-to-release compatibility. These are all things that need to be clear for "
+        "any release, even if no features were to change.  \n";
+    static Image oldImage;
+
+
+    Display display = new Display();
+    Shell shell = new Shell(display);
+    shell.setLayout(new FillLayout());
+    StyledText styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
+    styledText.setText(text);
+    FontData data = display.getSystemFont().getFontData()[0];
+    Font font = new Font(display, data.getName(), 16f, DWT.BOLD);
+    styledText.setFont(font);
+    styledText.setForeground(display.getSystemColor (DWT.COLOR_BLUE));
+
+    void onResize (Event event) {
+        Rectangle rect = styledText.getClientArea ();
+        Image newImage = new Image (display, 1, Math.max (1, rect.height));
+        GC gc = new GC (newImage);
+        gc.setForeground (display.getSystemColor (DWT.COLOR_WHITE));
+        gc.setBackground (display.getSystemColor (DWT.COLOR_YELLOW));
+        gc.fillGradientRectangle (rect.x, rect.y, 1, rect.height, true);
+        gc.dispose ();
+        styledText.setBackgroundImage (newImage);
+        if (oldImage !is null) oldImage.dispose ();
+        oldImage = newImage;
+    }
+
+    styledText.addListener (DWT.Resize, dgListener(&onResize));
+
+    shell.setSize(700, 400);
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch())
+            display.sleep();
+    }
+    if (oldImage !is null) oldImage.dispose ();
+    font.dispose();
+    display.dispose();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet222.d	Fri Aug 22 07:27:56 2008 +0800
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+
+module styledtext.Snippet222;
+/*
+ * example snippet: StyledText bulleted list example
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ *
+ * @since 3.2
+ */
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.custom.PaintObjectEvent;
+import dwt.custom.PaintObjectListener;
+import dwt.custom.Bullet;
+import dwt.custom.ST;
+import dwt.layout.GridData;
+import dwt.layout.FillLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Button;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.graphics.FontData;
+import dwt.graphics.Font;
+import dwt.graphics.TextLayout;
+import dwt.graphics.GlyphMetrics;
+import tango.text.Text;
+alias Text!(char) StringBuffer;
+import tango.util.Convert;
+
+import dwt.dwthelper.utils;
+version(JIVE){
+    import jive.stacktrace;
+}
+
+void main() {
+    Display display = new Display();
+    Shell shell = new Shell(display);
+    shell.setText("StyledText Bullet Example");
+    shell.setLayout(new FillLayout());
+    StyledText styledText = new StyledText (shell, DWT.FULL_SELECTION | DWT.BORDER | DWT.WRAP | DWT.V_SCROLL);
+    StringBuffer text = new StringBuffer();
+    text.append("Here is StyledText with some bulleted lists:\n\n");
+    for (int i = 0; i < 4; i++) text.append("Red Bullet List Item " ~ to!(char[])(i) ~ "\n");
+    text.append("\n");
+    for (int i = 0; i < 2; i++) text.append("Numbered List Item " ~ to!(char[])(i) ~ "\n");
+    for (int i = 0; i < 4; i++) text.append("Sub List Item " ~ to!(char[])(i) ~ "\n");
+    for (int i = 0; i < 2; i++) text.append("Numbered List Item " ~ to!(char[])(2+i) ~ "\n");
+    text.append("\n");
+    for (int i = 0; i < 4; i++) text.append("Custom Draw List Item " ~ to!(char[])(i) ~ "\n");
+    styledText.setText(text.toString());
+
+    StyleRange style0 = new StyleRange();
+    style0.metrics = new GlyphMetrics(0, 0, 40);
+    style0.foreground = display.getSystemColor(DWT.COLOR_RED);
+    Bullet bullet0 = new Bullet (style0);
+    StyleRange style1 = new StyleRange();
+    style1.metrics = new GlyphMetrics(0, 0, 50);
+    style1.foreground = display.getSystemColor(DWT.COLOR_BLUE);
+    Bullet bullet1 = new Bullet (ST.BULLET_NUMBER | ST.BULLET_TEXT, style1);
+    bullet1.text = ".";
+    StyleRange style2 = new StyleRange();
+    style2.metrics = new GlyphMetrics(0, 0, 80);
+    style2.foreground = display.getSystemColor(DWT.COLOR_GREEN);
+    Bullet bullet2 = new Bullet (ST.BULLET_TEXT, style2);
+    bullet2.text = "\u2713";
+    StyleRange style3 = new StyleRange();
+    style3.metrics = new GlyphMetrics(0, 0, 50);
+    Bullet bullet3 = new Bullet (ST.BULLET_CUSTOM, style2);
+
+    styledText.setLineBullet(2, 4, bullet0);
+    styledText.setLineBullet(7, 2, bullet1);
+    styledText.setLineBullet(9, 4, bullet2);
+    styledText.setLineBullet(13, 2, bullet1);
+    styledText.setLineBullet(16, 4, bullet3);
+
+    styledText.addPaintObjectListener(new class() PaintObjectListener {
+        public void paintObject(PaintObjectEvent event) {
+            Display display = event.display;
+            StyleRange style = event.style;
+            Font font = style.font;
+            if (font is null) font = styledText.getFont();
+            TextLayout layout = new TextLayout(display);
+            layout.setAscent(event.ascent);
+            layout.setDescent(event.descent);
+            layout.setFont(font);
+            layout.setText("\u2023 1." ~ to!(char[])( event.bulletIndex) ~ ")");
+            layout.draw(event.gc, event.x + 10, event.y);
+            layout.dispose();
+        }
+    });
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch()) display.sleep();
+    }
+    display.dispose ();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet244.d	Fri Aug 22 07:27:56 2008 +0800
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+
+module styledtext.Snippet244;
+/*
+ * StyledText snippet: Draw a box around text.
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ */
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.layout.FillLayout;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Listener;
+import dwt.widgets.Event;
+import dwt.graphics.Color;
+import dwt.graphics.Point;
+
+import dwt.dwthelper.utils;
+version(JIVE){
+    import jive.stacktrace;
+}
+
+void main() {
+    static String SEARCH_STRING = "box";
+    Display display = new Display();
+    Color RED = display.getSystemColor(DWT.COLOR_RED);
+    Shell shell = new Shell(display);
+    shell.setBounds(10,10,250,250);
+    StyledText text = new StyledText(shell, DWT.NONE);
+    text.setBounds(10,10,200,200);
+
+    void onPaint(Event event) {
+        String contents = text.getText();
+        int stringWidth = event.gc.stringExtent(SEARCH_STRING).x;
+        int lineHeight = text.getLineHeight();
+        event.gc.setForeground(RED);
+        int index = contents.indexOf(SEARCH_STRING);
+        while (index != -1) {
+            Point topLeft = text.getLocationAtOffset(index);
+            event.gc.drawRectangle(topLeft.x - 1, topLeft.y, stringWidth + 1, lineHeight - 1);
+            index = contents.indexOf(SEARCH_STRING, index + 1);
+        }
+    }
+
+    text.addListener(DWT.Paint, dgListener(&onPaint));
+
+    text.setText("This demonstrates drawing a box\naround every occurrence of the word\nbox in the StyledText");
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch()) display.sleep();
+    }
+    display.dispose();
+}