75
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 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 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.ui.forms.widgets.ScrolledFormText;
|
|
14
|
|
15 import dwtx.ui.forms.widgets.SharedScrolledComposite;
|
|
16 import dwtx.ui.forms.widgets.FormText;
|
|
17
|
|
18 import dwt.DWT;
|
|
19 import dwt.graphics.Color;
|
|
20 import dwt.widgets.Composite;
|
|
21 import dwt.widgets.Control;
|
|
22
|
|
23 import dwt.dwthelper.utils;
|
|
24 import dwt.dwthelper.InputStream;
|
|
25
|
|
26 /**
|
|
27 * ScrolledFormText is a control that is capable of scrolling an instance of
|
|
28 * the FormText class. It should be created in a parent that will allow it to
|
|
29 * use all the available area (for example, a shell, a view or an editor). The
|
|
30 * form text can be created by the class itself, or set from outside. In the
|
|
31 * later case, the form text instance must be a direct child of the
|
|
32 * ScrolledFormText instance.
|
|
33 * <p>
|
|
34 * The class assumes that text to be rendered contains formatting tags. In case
|
|
35 * of a string, it will enclose the text in 'form' root element if missing from
|
|
36 * the text as a convinience. For example:
|
|
37 *
|
|
38 * <pre>
|
|
39 * ftext.setText("<p>Some text here</>");
|
|
40 * </pre>
|
|
41 *
|
|
42 * will not cause an error. The same behavior does not exist for content from
|
|
43 * the input stream, however - it must be well formed in that case.
|
|
44 * </p>
|
|
45
|
|
46 * @since 3.0
|
|
47 * @see FormText
|
|
48 */
|
|
49 public class ScrolledFormText : SharedScrolledComposite {
|
|
50 private FormText content;
|
|
51 private String text;
|
|
52 /**
|
|
53 * Creates the new scrolled text instance in the provided parent
|
|
54 *
|
|
55 * @param parent
|
|
56 * the parent composite
|
|
57 * @param createFormText
|
|
58 * if <code>true</code>, enclosing form text instance will be
|
|
59 * created in this constructor.
|
|
60 */
|
|
61 public this(Composite parent, bool createFormText) {
|
|
62 this(parent, DWT.V_SCROLL | DWT.H_SCROLL, createFormText);
|
|
63 }
|
|
64 /**
|
|
65 * Creates the new scrolled text instance in the provided parent
|
|
66 *
|
|
67 * @param parent
|
|
68 * the parent composite
|
|
69 * @param style
|
|
70 * the style to pass to the scrolled composite
|
|
71 * @param createFormText
|
|
72 * if <code>true</code>, enclosing form text instance will be
|
|
73 * created in this constructor.
|
|
74 */
|
|
75 public this(Composite parent, int style, bool createFormText) {
|
|
76 super(parent, style);
|
|
77 if (createFormText)
|
|
78 setFormText(new FormText(this, DWT.NULL));
|
|
79 }
|
|
80 /**
|
|
81 * Sets the form text to be managed by this scrolled form text. The
|
|
82 * instance must be a direct child of this class. If this method is used,
|
|
83 * <code>false</code> must be passed in either of the constructors to
|
|
84 * avoid creating form text instance.
|
|
85 *
|
|
86 * @param formText
|
|
87 * the form text instance to use.
|
|
88 */
|
|
89 public void setFormText(FormText formText) {
|
|
90 this.content = formText;
|
|
91 super.setContent(content);
|
|
92 content.setMenu(getMenu());
|
|
93 if (text !is null)
|
|
94 loadText(text);
|
|
95 }
|
|
96 /**
|
|
97 * Sets the foreground color of the scrolled form text.
|
|
98 *
|
|
99 * @param fg
|
|
100 * the foreground color
|
|
101 */
|
|
102 public void setForeground(Color fg) {
|
|
103 super.setForeground(fg);
|
|
104 if (content !is null)
|
|
105 content.setForeground(fg);
|
|
106 }
|
|
107 /**
|
|
108 * Sets the background color of the scrolled form text.
|
|
109 *
|
|
110 * @param bg
|
|
111 * the background color
|
|
112 */
|
|
113 public void setBackground(Color bg) {
|
|
114 super.setBackground(bg);
|
|
115 if (content !is null)
|
|
116 content.setBackground(bg);
|
|
117 }
|
|
118 /**
|
|
119 * The class sets the content widget. This method should not be called by
|
|
120 * classes that instantiate this widget.
|
|
121 *
|
|
122 * @param c
|
|
123 * content control
|
|
124 */
|
|
125 public final void setContent(Control c) {
|
|
126 }
|
|
127 /**
|
|
128 * Sets the text to be rendered in the scrolled form text. The text must
|
|
129 * contain formatting tags.
|
|
130 *
|
|
131 * @param text
|
|
132 * the text to be rendered
|
|
133 */
|
|
134 public void setText(String text) {
|
|
135 this.text = text;
|
|
136 loadText(text);
|
|
137 reflow(true);
|
|
138 }
|
|
139 /**
|
|
140 * Sets the contents to rendered in the scrolled form text. The stream must
|
|
141 * contain formatting tags. The caller is responsible for closing the input
|
|
142 * stream. The call may be long running. For best results, call this method
|
|
143 * from another thread and call 'reflow' when done (but make both calls
|
|
144 * using 'Display.asyncExec' because these calls must be made in the event
|
|
145 * dispatching thread).
|
|
146 *
|
|
147 * @param is
|
|
148 * content input stream
|
|
149 */
|
|
150 public void setContents(InputStream is_) {
|
|
151 loadContents(is_);
|
|
152 }
|
|
153 /**
|
|
154 * Returns the instance of the form text.
|
|
155 *
|
|
156 * @return the form text instance
|
|
157 */
|
|
158 public FormText getFormText() {
|
|
159 return content;
|
|
160 }
|
|
161 private void loadText(String text) {
|
|
162 if (content !is null) {
|
|
163 String markup = text;
|
|
164 if (!markup.startsWith("<form>")) //$NON-NLS-1$
|
|
165 markup = "<form>" ~ text ~ "</form>"; //$NON-NLS-1$//$NON-NLS-2$
|
|
166 content.setText(markup, true, false);
|
|
167 }
|
|
168 }
|
|
169 private void loadContents(InputStream is_) {
|
|
170 if (content !is null) {
|
|
171 content.setContents(is_, false);
|
|
172 }
|
|
173 }
|
|
174 }
|