1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet217"
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.Snippet217;
50 /**
51  * StyledText snippet: embed controls
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.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Display;
67 import org.eclipse.swt.widgets.Shell;
68 import org.eclipse.swt.widgets.Control;
69 import org.eclipse.swt.widgets.Button;
70 import org.eclipse.swt.widgets.Combo;
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.Rectangle;
76 import org.eclipse.swt.graphics.Point;
77 import org.eclipse.swt.graphics.GC;
78 import org.eclipse.swt.graphics.Image;
79 import org.eclipse.swt.graphics.GlyphMetrics;
80 
81 import java.lang.all;
82 version(JIVE){
83     import jive.stacktrace;
84 }
85 
86 version(D_Version2)
87 {
88 const string OBJ_MARKER = "\uFFFC"; 
89 }
90 else
91 {
92 const char[] OBJ_MARKER = "\uFFFC"; //should be char[] because of dmd v1.069 @@@BUG@@@ Issue 6467    
93 }
94 void main() {
95     static StyledText styledText;
96     static String text =
97         "This snippet shows how to embed widgets in a StyledText.\n" ~
98         "Here is one: " ~ OBJ_MARKER ~ ", and here is another: " ~ OBJ_MARKER ~ ".";
99     static int[] offsets;
100     static Control[] controls;
101     static const int MARGIN = 5;
102 
103     static void addControl(Control control, int offset) {
104         StyleRange style = new StyleRange ();
105         style.start = offset;
106         style.length = OBJ_MARKER.length;
107         control.pack();
108         Rectangle rect = control.getBounds();
109         int ascent = 2*rect.height/3;
110         int descent = rect.height - ascent;
111         style.metrics = new GlyphMetrics(ascent + MARGIN, descent + MARGIN, rect.width + 2*MARGIN);
112         styledText.setStyleRange(style);
113     }
114 
115 
116     Display display = new Display();
117     Font font = new Font(display, "Tahoma", 32f, SWT.NORMAL);
118     Shell shell = new Shell(display);
119     shell.setLayout(new GridLayout());
120     styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
121     styledText.setFont(font);
122     styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
123     styledText.setText(text);
124     controls = new Control[2];
125     Button button = new Button(styledText, SWT.PUSH);
126     button.setText("Button 1");
127     controls[0] = button;
128     Combo combo = new Combo(styledText, SWT.NONE);
129     combo.add("item 1");
130     combo.add("another item");
131     controls[1] = combo;
132     offsets = new int[controls.length];
133     ptrdiff_t lastOffset = 0;
134     for (int i = 0; i < controls.length; i++) {
135         int offset = text.indexOf( OBJ_MARKER, cast(int)lastOffset);
136         assert(offset != -1, "Can't find OBJ_MARKER");
137         offsets[i] = offset;
138         addControl(controls[i], offsets[i]);
139         lastOffset = offset + OBJ_MARKER.length;
140     }
141 
142     void onVerify(Event e) {
143         int start = e.start;
144         int replaceCharCount = e.end - e.start;
145         ptrdiff_t newCharCount = e.text.length;
146         for (int i = 0; i < offsets.length; i++) {
147             int offset = offsets[i];
148             if (start <= offset && offset < start + replaceCharCount) {
149                 // this widget is being deleted from the text
150                 if (controls[i] !is null && !controls[i].isDisposed()) {
151                     controls[i].dispose();
152                     controls[i] = null;
153                 }
154                 offset = -1;
155             }
156             if (offset != -1 && offset >= start) offset += newCharCount - replaceCharCount;
157             offsets[i] = offset;
158         }
159     }
160     // use a verify listener to keep the offsets up to date
161     styledText.addListener(SWT.Verify, dgListener(&onVerify));
162 
163     // reposition widgets on paint event
164     styledText.addPaintObjectListener(new class PaintObjectListener {
165         public void paintObject(PaintObjectEvent event) {
166             StyleRange style = event.style;
167             int start = style.start;
168             for (int i = 0; i < offsets.length; i++) {
169                 int offset = offsets[i];
170                 if (start == offset) {
171                     Point pt = controls[i].getSize();
172                     int x = event.x + MARGIN;
173                     int y = event.y + event.ascent - 2*pt.y/3;
174                     controls[i].setLocation(x, y);
175                     break;
176                 }
177             }
178         }
179     });
180 
181     shell.setSize(400, 400);
182     shell.open();
183     while (!shell.isDisposed()) {
184         if (!display.readAndDispatch())
185             display.sleep();
186     }
187     font.dispose();
188     display.dispose();
189 }