Maintain color highlighting when text is copied to word doc

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Maintain color highlighting when text is copied to word doc

Postby cherllea » Thu Mar 05, 2009 12:11 pm

Hi,
When I copy the text from the RSyntaxTextArea to a word doc, the pasted code is plain text in black color but I want the text to be copied with the color code that was in the textarea. I noticed the method copyasRTF(). Can some one tell me if there is any simple method to switch on and off the color highlighting or should I write an actionlistener overwriting stuff.
thanks
Cherllea
cherllea
 

Re: Maintain color highlighting when text is copied to word doc

Postby robert » Thu Mar 05, 2009 3:41 pm

Copying as RTF currently isn't tied to a keystroke by default, mainly due to it not being tested very much, but you can do so yourself like so:

Code: Select all
InputMap im = textArea.getInputMap();
ActionMap am = textArea.getActionMap();
int mods = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | InputEvent.SHIFT_DOWN_MASK;
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, mods);
im.put(ks, "copyAsRtf");
am.put("copyAsRtf", new RSyntaxTextAreaEditorKit.CopyAsRtfAction());


This way you don't have to call copyAsRtf() yourself in something such as a KeyListener. Just add that code wherever you create your RSTA instance.

The above example ties the "Copy as RTF" action to Ctrl+Shift+C (Cmd+Shift+C on Macs) so it doesn't conflict with the standard Ctrl+C copy. There's no reason you couldn't simply override the current copy functionality by removing the "| InputEvent.SHIFT_DOWN_MASK" from the snippet above. Then all of your copying will be in RTF (unless the destination doesn't understand RTF, in which case it'll be plain text like usual).

Let me know how this works out for you!
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Re: Maintain color highlighting when text is copied to word doc

Postby cherllea » Thu Mar 05, 2009 5:34 pm

Hey,

works for both ctrl+c and ctrl+shift+c. thanks a lot.
cheers,
cherllea
cherllea
 


Return to Help

Who is online

Users browsing this forum: No registered users and 6 guests

cron