|
Re: Embedding objects in styledtext - Snippet 212 questions [message #467774 is a reply to message #467452] |
Mon, 06 February 2006 16:33 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
> 1. When replacing the image w/ widgets such as CCombo or button,
> these requires a parent container.. should the StyledText created be the
> parent container for the widgets? or ?
Create as a child of StyledText
> 2. I tried to replace the images with a button (created via
> FormToolkit) but I got stuck at the
> styledText.addPaintObjectListener(...), where I need to find an equivalent
> of gc.drawImage(Image, x, y) for button... Is it at all possible ?? What
> would be equivalent for a CCombo ?
See example below. You do not paint the widget but rather position it.
> 3. Can this work with FormToolkit created widgets ? (ie
> toolkit.createButton(...) would work ? ) or do they have to be base SWT
> widgets ? or ?
You should be able to use FormToolkit.createButton() etc.
Modified Snippet212 for controls:
public class Snippet212Modified {
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;
static 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);
}
public static void main(String [] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true));
styledText.setText(text);
controls = new Control[2];
Button button = new Button(styledText, SWT.PUSH);
button.setText("Button 1");
controls[0] = button;
Combo combo = new Combo(styledText, SWT.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;
}
// use a verify listener to keep the offsets up to date
styledText.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent 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] != null &&
!controls[i].isDisposed()) {
controls[i].dispose();
controls[i] = null;
}
offset = -1;
}
if (offset != -1 && offset >= start)
offset += newCharCount - replaceCharCount;
offsets[i] = offset;
}
}
});
// reposition widgets on paint event
styledText.addPaintObjectListener(new PaintObjectListener()
{
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 point =
styledText.getLocationAtOffset(offset);
controls[i].setLocation(point.x
+ MARGIN, point.y + MARGIN);
break;
}
}
}
});
shell.setSize(400, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|
|
|
Re: Embedding objects in styledtext - Snippet 212 questions [message #467794 is a reply to message #467774] |
Mon, 06 February 2006 19:45 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
There was a minor mistake in the code below.
I have added the following snippet to the SWT snippets page:
http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org. eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet217 .java
"Veronika Irvine" <veronika_irvine@oti.com> wrote in message
news:ds7tpo$l7h$1@utils.eclipse.org...
>> 1. When replacing the image w/ widgets such as CCombo or button,
>> these requires a parent container.. should the StyledText created be the
>> parent container for the widgets? or ?
>
> Create as a child of StyledText
>
>> 2. I tried to replace the images with a button (created via
>> FormToolkit) but I got stuck at the
>> styledText.addPaintObjectListener(...), where I need to find an
>> equivalent of gc.drawImage(Image, x, y) for button... Is it at all
>> possible ?? What would be equivalent for a CCombo ?
>
> See example below. You do not paint the widget but rather position it.
>
>> 3. Can this work with FormToolkit created widgets ? (ie
>> toolkit.createButton(...) would work ? ) or do they have to be base SWT
>> widgets ? or ?
>
> You should be able to use FormToolkit.createButton() etc.
>
> Modified Snippet212 for controls:
>
> public class Snippet212Modified {
>
> 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;
>
> static 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);
> }
>
> public static void main(String [] args) {
> final Display display = new Display();
> final Shell shell = new Shell(display);
> shell.setLayout(new GridLayout());
> styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
> styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
> true, true));
> styledText.setText(text);
> controls = new Control[2];
> Button button = new Button(styledText, SWT.PUSH);
> button.setText("Button 1");
> controls[0] = button;
> Combo combo = new Combo(styledText, SWT.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;
> }
>
> // use a verify listener to keep the offsets up to date
> styledText.addVerifyListener(new VerifyListener() {
> public void verifyText(VerifyEvent 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] != null &&
> !controls[i].isDisposed()) {
>
> controls[i].dispose();
> controls[i] = null;
> }
> offset = -1;
> }
> if (offset != -1 && offset >=
> start) offset += newCharCount - replaceCharCount;
> offsets[i] = offset;
> }
> }
> });
>
> // reposition widgets on paint event
> styledText.addPaintObjectListener(new PaintObjectListener()
> {
> 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 point =
> styledText.getLocationAtOffset(offset);
>
> controls[i].setLocation(point.x + MARGIN, point.y + MARGIN);
> break;
> }
> }
> }
> });
>
> shell.setSize(400, 400);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
>
|
|
|
Powered by
FUDForum. Page generated in 0.03603 seconds