[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.tools] Re: cut, copy, paste in StyledText Widget
|
In the example below I can select some text in the StyledText widget and copy the selection to the clipboard using the popup menu on
the StyledText widget or using Ctrl+C. Same for the regular text widget. The text on the clipboard is printed to the console when
you press ctrl+c in either widget.
See http://bugs.eclipse.org/bugs/show_bug.cgi?id=35210 if you're wondering why I'm testing for \u0003 to catch the Ctrl+C in the key
listener.
Knut
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class StyledTextClipboard {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final StyledText styledText = new StyledText(shell, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
styledText.setText("StyledText widget");
final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
text.setText("Text widget");
Text pasteText = new Text(shell, SWT.BORDER | SWT.MULTI);
pasteText.setText("paste here");
Menu popupMenu = new Menu(styledText);
MenuItem copyItem = new MenuItem(popupMenu, SWT.PUSH);
copyItem.setText("Copy");
copyItem.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("popup menu copy");
styledText.copy();
}
});
styledText.setMenu(popupMenu);
Listener copyListener = new Listener() {
public void handleEvent(Event event) {
if (event.character == '\u0003') {
Clipboard clipboard = new Clipboard(display);
TextTransfer transfer = TextTransfer.getInstance();
String text = (String) clipboard.getContents(transfer);
System.out.println("clipboard contents: " + text);
clipboard.dispose();
}
}
};
styledText.addListener(SWT.KeyDown, copyListener);
text.addListener(SWT.KeyDown, copyListener);
shell.setSize(500, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
"kyle farnand" <kfarnand@xxxxxxxxxxxxx> wrote in message news:b80q0h$6ri$1@xxxxxxxxxxxxxxxx
> Thanks for confirmation Paul. I am also using R2.0.2 and Win2k.
>
> Knut and/or Lynne, maybe you could post some simple sample code that you
> know works in your environment. If it works on my machine, I can figure
> out what the difference is (between your working code and my failing
> code) and post the results. If not, we will atleast know it is not a
> difference in code that is causing the our different results.
>
> Open to oother suggestions but this seems like a potential bug (either
> document related or functional) that should be tracked down.
>
> Paul T. Keyser wrote:
> > R2.0.2, Win2K
> >
> > I agree with Kyle Farnand who describes the problem accurately and correctly: I see exactly the same thing (and have posted
before
> > about this, but the threads died).
> >
> > As far as I can tell, these are the facts (R2.0.2, Win2K):
> > 1) Text widget: comes with an OS-supplied popupmenu in which are items Cut/Copy/Paste (but Ctrl-C/V/X do *NOT* work); if you add
> > your own popupmenu (via SWT) you lose the whole OS-supplied menu; if you then add to your own popupmenu, your own items
> > Cut/Copy/Paste, and hook the API for those actions, you can restore the Cut/Copy/Paste function, but there appears to be NO way
to
> > get Ctrl-C/V/X to work (with or without a popupmenu).
> >
> > 2) StyledText widget: comes with *NO* popupmenu; you can add your own popupmenu (via SWT, pretty much the same as for the Text);
> > to your own popupmenu, you can add your own items Cut/Copy/Paste, and hook the API for those actions, thus for the first time
> > obtaining the Cut/Copy/Paste function, but there appears to be NO way to get Ctrl-C/V/X to work (with or without a popupmenu).
> >
> > Maybe what Knut and Lynne are saying is that in R2.1, the Ctrl-C/V/X has somehow been fixed (via fixing some bug not known to
have
> > been connected, but nevertheless connected)?
> >
> > Paul K
> >
> > Knut Radloff wrote:
> >
> >
> >>Which build and OS are you using? Copy to clipboard (using Ctrl+C) works for me in a read-only single line Text and StyledText
> >>widget in R2.1
> >>I searched the StyledText bugs we fixed in 2.1 and didn't see anything cut/paste related that may have caused your problem.
> >>
> >>Knut
> >>"kyle farnand" <kfarnand@xxxxxxxxxxxxx> wrote in message news:b7n3jl$sh7$1@xxxxxxxxxxxxxxxx
> >>
> >>>Looking at the Text tab of the SWT Controls sample view and playing
> >>>around with all the different parameters, there were no parameters that
> >>>I could set to get CTRL-C to work in either the Text or Styled Text
> >>>widget (including READ_ONLY). Right Click->Copy worked with any
> >>>parameter settings on the Text widget but not on the StyledText widget.
> >>>
> >>>Is this the desired functionality for the StyledText widget?
> >>>
> >>>kyle farnand wrote:
> >>>
> >>>>I must be doing something wrong then. I tried CTRL-C and paste the
> >>>>results into notepad and don't get anything. I know about the API but
> >>>>was hoping to get this functionality for free (which seems possible). In
> >>>>the Text widget you get <right-click> menu -> copy functionality and was
> >>>>hoping that would be the same in StyledText widget.
> >>>>
> >>>>Lynne Kues wrote:
> >>>>
> >>>>
> >>>>>I do not understand what you are talking about.
> >>>>>
> >>>>>If the StyledText widget is READ_ONLY, the copy operation is available
> >>>>>(select some text and press CTRL-C), but you cannot paste to or cut
> >>>>>from the
> >>>>>widget since it is readonly. Also note that there is API in the
> >>>>>StyledText
> >>>>>widget to cut, copy and paste programatically.
> >>>>>
> >>>>>"kyle farnand" <kfarnand@xxxxxxxxxxxxx> wrote in message
> >>>>>news:b7mtas$n7d$1@xxxxxxxxxxxxxxxx
> >>>>>
> >>>>>
> >>>>>>When I use the below code, I get copy and paste for free.
> >>>>>>
> >>>>>>
> >>>>>>>Text modelText = new Text(modelXMLContainer, SWT.READ_ONLY |SWT.MULTI);
> >>>>>>>modelText.setText(modelString);
> >>>>>>
> >>>>>>But when I used the StyledText widget, I lose this functionality.
> >>>>>>
> >>>>>>
> >>>>>>>StyledText modelText = new StyledText(modelXMLContainer,
> >>>>>>
> >>>>>>SWT.READ_ONLY);
> >>>>>>
> >>>>>>>modelText.setText(modelString);
> >>>>>>
> >>>>>>An earlier post said I should get the same copy and paste functionality
> >>>>>>if I use SWT.READ_ONLY in the constructor from both widgets but that is
> >>>>>>not the case.
> >>>>>>
> >>>>>>Is there an easy way to get this copy and paste functionality out of the
> >>>>>>StyledText widget (as in the Text widget) or do I have to implement it
> >>>>>>myself?
> >>>>>>
> >>>>>
> >>>>>
> >>>>>
> >
>
- References:
- [news.eclipse.tools] cut, copy, paste in StyledText Widget
- [news.eclipse.tools] Re: cut, copy, paste in StyledText Widget
- [news.eclipse.tools] Re: cut, copy, paste in StyledText Widget
- [news.eclipse.tools] Re: cut, copy, paste in StyledText Widget
- [news.eclipse.tools] Re: cut, copy, paste in StyledText Widget
- [news.eclipse.tools] Re: cut, copy, paste in StyledText Widget
- [news.eclipse.tools] Re: cut, copy, paste in StyledText Widget