| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2000, 2003 IBM Corporation and others. |
|---|
| 3 |
* All rights reserved. This program and the accompanying materials |
|---|
| 4 |
* are made available under the terms of the Common Public License v1.0 |
|---|
| 5 |
* which accompanies this distribution, and is available at |
|---|
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
|---|
| 7 |
* |
|---|
| 8 |
* Contributors: |
|---|
| 9 |
* IBM Corporation - initial API and implementation |
|---|
| 10 |
*******************************************************************************/ |
|---|
| 11 |
module controlexample.styledtexttab; |
|---|
| 12 |
|
|---|
| 13 |
private { |
|---|
| 14 |
import swt.all; |
|---|
| 15 |
import controlexample.tab; |
|---|
| 16 |
import controlexample.scrollabletab; |
|---|
| 17 |
import controlexample.controlexample; |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class StyledTextTab : ScrollableTab { |
|---|
| 22 |
/* Example widgets and groups that contain them */ |
|---|
| 23 |
StyledText styledText; |
|---|
| 24 |
Group styledTextGroup, styledTextStyleGroup; |
|---|
| 25 |
|
|---|
| 26 |
/* Style widgets added to the "Style" group */ |
|---|
| 27 |
Button wrapButton, readOnlyButton, fullSelectionButton; |
|---|
| 28 |
|
|---|
| 29 |
/* Buttons for adding StyleRanges to StyledText */ |
|---|
| 30 |
Button boldButton, italicButton, redButton, yellowButton; |
|---|
| 31 |
Image boldImage, italicImage, redImage, yellowImage; |
|---|
| 32 |
|
|---|
| 33 |
/** |
|---|
| 34 |
* Creates the Tab within a given instance_ren of ControlExample. |
|---|
| 35 |
*/ |
|---|
| 36 |
this(ControlExample instance_ren) { |
|---|
| 37 |
super(instance_ren); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
/** |
|---|
| 41 |
* Creates a bitmap image. |
|---|
| 42 |
*/ |
|---|
| 43 |
Image createBitmapImage (Display display, String name) { |
|---|
| 44 |
ImageData source = new ImageData(name ~ ".bmp"); |
|---|
| 45 |
ImageData mask = new ImageData(name ~ "_mask.bmp"); |
|---|
| 46 |
return new Image (display, source, mask); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
/** |
|---|
| 50 |
* Creates the "Control" widget children. |
|---|
| 51 |
*/ |
|---|
| 52 |
void createControlWidgets () { |
|---|
| 53 |
super.createControlWidgets (); |
|---|
| 54 |
|
|---|
| 55 |
/* Add a group for modifying the StyledText widget */ |
|---|
| 56 |
createStyledTextStyleGroup (); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
/** |
|---|
| 60 |
* Creates the "Example" group. |
|---|
| 61 |
*/ |
|---|
| 62 |
void createExampleGroup () { |
|---|
| 63 |
super.createExampleGroup (); |
|---|
| 64 |
|
|---|
| 65 |
/* Create a group for the styled text widget */ |
|---|
| 66 |
styledTextGroup = new Group (exampleGroup, SWT.NONE); |
|---|
| 67 |
styledTextGroup.setLayout (new GridLayout ()); |
|---|
| 68 |
styledTextGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); |
|---|
| 69 |
styledTextGroup.setText ("StyledText"); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* Creates the "Example" widgets. |
|---|
| 74 |
*/ |
|---|
| 75 |
void createExampleWidgets () { |
|---|
| 76 |
|
|---|
| 77 |
/* Compute the widget style */ |
|---|
| 78 |
int style = getDefaultStyle(); |
|---|
| 79 |
if (singleButton.getSelection ()) style |= SWT.SINGLE; |
|---|
| 80 |
if (multiButton.getSelection ()) style |= SWT.MULTI; |
|---|
| 81 |
if (horizontalButton.getSelection ()) style |= SWT.H_SCROLL; |
|---|
| 82 |
if (verticalButton.getSelection ()) style |= SWT.V_SCROLL; |
|---|
| 83 |
if (wrapButton.getSelection ()) style |= SWT.WRAP; |
|---|
| 84 |
if (readOnlyButton.getSelection ()) style |= SWT.READ_ONLY; |
|---|
| 85 |
if (borderButton.getSelection ()) style |= SWT.BORDER; |
|---|
| 86 |
if (fullSelectionButton.getSelection ()) style |= SWT.FULL_SELECTION; |
|---|
| 87 |
|
|---|
| 88 |
/* Create the example widgets */ |
|---|
| 89 |
styledText = new StyledText (styledTextGroup, style); |
|---|
| 90 |
styledText.setText ("The quick brown fox jumps over the lazy dog.");//ControlExample.getResourceString("Example_string")); |
|---|
| 91 |
styledText.append ("\n"); |
|---|
| 92 |
styledText.append ("One Two Three");//ControlExample.getResourceString("One_Two_Three")); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
/** |
|---|
| 96 |
* Creates the "Style" group. |
|---|
| 97 |
*/ |
|---|
| 98 |
void createStyleGroup() { |
|---|
| 99 |
super.createStyleGroup(); |
|---|
| 100 |
|
|---|
| 101 |
/* Create the extra widgets */ |
|---|
| 102 |
wrapButton = new Button (styleGroup, SWT.CHECK); |
|---|
| 103 |
wrapButton.setText ("SWT.WRAP"); |
|---|
| 104 |
readOnlyButton = new Button (styleGroup, SWT.CHECK); |
|---|
| 105 |
readOnlyButton.setText ("SWT.READ_ONLY"); |
|---|
| 106 |
fullSelectionButton = new Button (styleGroup, SWT.CHECK); |
|---|
| 107 |
fullSelectionButton.setText ("SWT.FULL_SELECTION"); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
/** |
|---|
| 111 |
* Creates the "StyledText Style" group. |
|---|
| 112 |
*/ |
|---|
| 113 |
void createStyledTextStyleGroup () { |
|---|
| 114 |
Display display = controlGroup.getDisplay (); |
|---|
| 115 |
styledTextStyleGroup = new Group (controlGroup, SWT.NONE); |
|---|
| 116 |
styledTextStyleGroup.setText (ControlExample.getResourceString ("StyledText_Styles")); |
|---|
| 117 |
styledTextStyleGroup.setLayout (new GridLayout(5, false)); |
|---|
| 118 |
GridData data = new GridData (GridData.HORIZONTAL_ALIGN_FILL); |
|---|
| 119 |
data.horizontalSpan = 2; |
|---|
| 120 |
styledTextStyleGroup.setLayoutData (data); |
|---|
| 121 |
|
|---|
| 122 |
/* Get images */ |
|---|
| 123 |
boldImage = createBitmapImage (display, "bold"); |
|---|
| 124 |
italicImage = createBitmapImage (display, "italic"); |
|---|
| 125 |
redImage = createBitmapImage (display, "red"); |
|---|
| 126 |
yellowImage = createBitmapImage (display, "yellow"); |
|---|
| 127 |
|
|---|
| 128 |
/* Create controls to modify the StyledText */ |
|---|
| 129 |
Label label = new Label (styledTextStyleGroup, SWT.NONE); |
|---|
| 130 |
label.setText (ControlExample.getResourceString ("StyledText_Style_Instructions")); |
|---|
| 131 |
data = new GridData(GridData.FILL_HORIZONTAL); |
|---|
| 132 |
data.horizontalSpan = 5; |
|---|
| 133 |
label.setLayoutData(data); |
|---|
| 134 |
with(new Label (styledTextStyleGroup, SWT.NONE)){ |
|---|
| 135 |
setText (ControlExample.getResourceString ("Bold")); |
|---|
| 136 |
} |
|---|
| 137 |
boldButton = new Button (styledTextStyleGroup, SWT.PUSH); |
|---|
| 138 |
boldButton.setImage (boldImage); |
|---|
| 139 |
boldButton.handleSelection(this, delegate(SelectionEvent e) { |
|---|
| 140 |
StyledTextTab c = cast(StyledTextTab)e.cData; |
|---|
| 141 |
Point sel = c.styledText.getSelectionRange(); |
|---|
| 142 |
if ((sel is null) || (sel.y == 0)) return; |
|---|
| 143 |
StyleRange style; |
|---|
| 144 |
for (int i = sel.x; i<sel.x+sel.y; i++) { |
|---|
| 145 |
StyleRange range = c.styledText.getStyleRangeAtOffset(i); |
|---|
| 146 |
if (range is null) {style = new StyleRange(i, 1, null, null, SWT.BOLD);} |
|---|
| 147 |
else {style = new StyleRange(i, 1, range.foreground, range.background, range.fontStyle | SWT.BOLD);}; |
|---|
| 148 |
c.styledText.setStyleRange(style); |
|---|
| 149 |
} |
|---|
| 150 |
c.styledText.setSelectionRange(sel.x + sel.y, 0); |
|---|
| 151 |
}); |
|---|
| 152 |
with(new Label (styledTextStyleGroup, SWT.NONE)){ |
|---|
| 153 |
setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|---|
| 154 |
} |
|---|
| 155 |
with(new Label (styledTextStyleGroup, SWT.NONE)) { |
|---|
| 156 |
setText (ControlExample.getResourceString ("Foreground_Style")); |
|---|
| 157 |
} |
|---|
| 158 |
redButton = new Button (styledTextStyleGroup, SWT.PUSH); |
|---|
| 159 |
redButton.setImage (redImage); |
|---|
| 160 |
with(new Label (styledTextStyleGroup, SWT.NONE)) { |
|---|
| 161 |
setText (ControlExample.getResourceString ("Italic")); |
|---|
| 162 |
} |
|---|
| 163 |
italicButton = new Button (styledTextStyleGroup, SWT.PUSH); |
|---|
| 164 |
italicButton.setImage (italicImage); |
|---|
| 165 |
italicButton.handleSelection(this, delegate(SelectionEvent e) { |
|---|
| 166 |
StyledTextTab c = cast(StyledTextTab)e.cData; |
|---|
| 167 |
Point sel = c.styledText.getSelectionRange(); |
|---|
| 168 |
if ((sel is null) || (sel.y == 0)) return; |
|---|
| 169 |
StyleRange style; |
|---|
| 170 |
for (int i = sel.x; i<sel.x+sel.y; i++) { |
|---|
| 171 |
StyleRange range = c.styledText.getStyleRangeAtOffset(i); |
|---|
| 172 |
if (range is null) {style = new StyleRange(i, 1, null, null, SWT.ITALIC);} |
|---|
| 173 |
else {style = new StyleRange(i, 1, range.foreground, range.background, range.fontStyle | SWT.ITALIC);}; |
|---|
| 174 |
c.styledText.setStyleRange(style); |
|---|
| 175 |
} |
|---|
| 176 |
c.styledText.setSelectionRange(sel.x + sel.y, 0); |
|---|
| 177 |
}); |
|---|
| 178 |
with(new Label (styledTextStyleGroup, SWT.NONE)) { |
|---|
| 179 |
setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|---|
| 180 |
} |
|---|
| 181 |
with(new Label (styledTextStyleGroup, SWT.NONE)){ |
|---|
| 182 |
setText (ControlExample.getResourceString ("Background_Style")); |
|---|
| 183 |
} |
|---|
| 184 |
yellowButton = new Button (styledTextStyleGroup, SWT.PUSH); |
|---|
| 185 |
yellowButton.setImage (yellowImage); |
|---|
| 186 |
|
|---|
| 187 |
void widgetSelected (SelectionEvent e) { |
|---|
| 188 |
StyledTextTab c = cast(StyledTextTab)e.cData; |
|---|
| 189 |
Point sel = c.styledText.getSelectionRange(); |
|---|
| 190 |
if ((sel is null) || (sel.y == 0)) return; |
|---|
| 191 |
Color fg, bg; |
|---|
| 192 |
if (e.widget is c.redButton) { |
|---|
| 193 |
fg = display.getSystemColor (SWT.COLOR_RED); |
|---|
| 194 |
bg = null; |
|---|
| 195 |
} else if (e.widget is yellowButton) { |
|---|
| 196 |
fg = null; |
|---|
| 197 |
bg = display.getSystemColor (SWT.COLOR_YELLOW); |
|---|
| 198 |
} else { |
|---|
| 199 |
fg = bg = null; |
|---|
| 200 |
} |
|---|
| 201 |
StyleRange style; |
|---|
| 202 |
for (int i = sel.x; i<sel.x+sel.y; i++) { |
|---|
| 203 |
StyleRange range = c.styledText.getStyleRangeAtOffset(i); |
|---|
| 204 |
if (range is null) { |
|---|
| 205 |
style = new StyleRange(i, 1, fg, bg, SWT.NORMAL); |
|---|
| 206 |
} |
|---|
| 207 |
else { |
|---|
| 208 |
if (range.foreground !== null) fg = range.foreground; |
|---|
| 209 |
if (range.background !== null) bg = range.background; |
|---|
| 210 |
style = new StyleRange(i, 1, fg, bg, range.fontStyle); |
|---|
| 211 |
}; |
|---|
| 212 |
c.styledText.setStyleRange(style); |
|---|
| 213 |
} |
|---|
| 214 |
c.styledText.setSelectionRange(sel.x + sel.y, 0); |
|---|
| 215 |
}; |
|---|
| 216 |
|
|---|
| 217 |
redButton.handleSelection(this, &widgetSelected); |
|---|
| 218 |
yellowButton.handleSelection(this, &widgetSelected); |
|---|
| 219 |
yellowButton.handleDispose(this, delegate(DisposeEvent e) { |
|---|
| 220 |
StyledTextTab c = cast(StyledTextTab)e.cData; |
|---|
| 221 |
c.boldImage.dispose(); |
|---|
| 222 |
c.italicImage.dispose(); |
|---|
| 223 |
c.redImage.dispose(); |
|---|
| 224 |
c.yellowImage.dispose(); |
|---|
| 225 |
}); |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
/** |
|---|
| 229 |
* Creates the tab folder page. |
|---|
| 230 |
* |
|---|
| 231 |
* @param tabFolder org.eclipse.swt.widgets.TabFolder |
|---|
| 232 |
* @return the new page for the tab folder |
|---|
| 233 |
*/ |
|---|
| 234 |
Composite createTabFolderPage (TabFolder tabFolder) { |
|---|
| 235 |
super.createTabFolderPage (tabFolder); |
|---|
| 236 |
|
|---|
| 237 |
/* |
|---|
| 238 |
* Add a resize listener to the tabFolderPage so that |
|---|
| 239 |
* if the user types into the example widget to change |
|---|
| 240 |
* its preferred size, and then resizes the shell, we |
|---|
| 241 |
* recalculate the preferred size correctly. |
|---|
| 242 |
*/ |
|---|
| 243 |
tabFolderPage.handleEvent(this, SWT.Resize, &setExampleWidgetSize); |
|---|
| 244 |
|
|---|
| 245 |
return tabFolderPage; |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
/** |
|---|
| 249 |
* Gets the "Example" widget children. |
|---|
| 250 |
*/ |
|---|
| 251 |
Control [] getExampleWidgets () { |
|---|
| 252 |
Control [] controls; |
|---|
| 253 |
controls ~= styledText; |
|---|
| 254 |
return controls; |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
/** |
|---|
| 258 |
* Gets the text for the tab folder item. |
|---|
| 259 |
*/ |
|---|
| 260 |
String getTabText () { |
|---|
| 261 |
return "StyledText"; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
/** |
|---|
| 265 |
* Sets the state of the "Example" widgets. |
|---|
| 266 |
*/ |
|---|
| 267 |
void setExampleWidgetState () { |
|---|
| 268 |
super.setExampleWidgetState (); |
|---|
| 269 |
wrapButton.setSelection ((styledText.getStyle () & SWT.WRAP) != 0); |
|---|
| 270 |
readOnlyButton.setSelection ((styledText.getStyle () & SWT.READ_ONLY) != 0); |
|---|
| 271 |
fullSelectionButton.setSelection ((styledText.getStyle () & SWT.FULL_SELECTION) != 0); |
|---|
| 272 |
horizontalButton.setEnabled ((styledText.getStyle () & SWT.MULTI) != 0); |
|---|
| 273 |
verticalButton.setEnabled ((styledText.getStyle () & SWT.MULTI) != 0); |
|---|
| 274 |
wrapButton.setEnabled ((styledText.getStyle () & SWT.MULTI) != 0); |
|---|
| 275 |
} |
|---|
| 276 |
} |
|---|