#!BPY

"""
Name: 'XPaste'
Blender: 240
Group: 'Misc'
Tooltip: 'Paste the text contained in the X Windows clipboard'
"""

__author__ = "Mitch Hughes (lobo_nz)"
__url__ = ("blender", "Author's homepage, http://blender.formworks.co.nz")
__version__ = "0.1"

__bpydoc__ = """\
"XPaste puts the text contained in the X Windows clipboard to a new text buffer

Usage:

Add the script to your blender/scripts directory
Run this script from the Scripts window, Scripts->Misc->XPaste
Click the paste button to update the text buffer (Defaults to: ###PASTE###)
with the contents of the X Windows clipboard.

Note: xclip is required to run this script - get it here
http://people.debian.org/~kims/xclip/
Currently looks for xclip in the same location as blender
You can set the full path to xclip below if you install it elsewhere.
"""

# $Id: xpaste.py,v 0.3 2005/11/03 20:03:10$
#
# --------------------------------------------------------------------------
# VR Object by Mitch Hughes (AKA lobo_nz)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

## The best I could achieve was to paste the text to a new
## text buffer, but I can atleast now get text copied
## from a website or other text source into blender
## without opening files constantly.

import os, sys
import Blender

#Modify this to the name of the text buffer you want it to paste the text to
textObName = "###PASTE###"

#change this to your xclip path if you dont put it into the blender app directory
#eg g_custom_xclip_location = '/usr/bin/xclip'
g_custom_xclip_location = None



def gui():
	Blender.BGL.glClearColor(0.6, 0.6, 0.6, 0.0)
	Blender.BGL.glClear(Blender.BGL.GL_COLOR_BUFFER_BIT)
	Blender.Draw.Button("Paste", 1, 10, 10, 80, 20)

def event(evt, mode):
	if evt == Blender.Draw.ESCKEY: Blender.Draw.Exit()

def bevent(evt):
	if g_custom_xclip_location != None:
		clipboard = os.popen(g_custom_xclip_location + " -o")
	else:
		clipboard = os.popen(Blender.sys.dirname(sys.executable) + "/xclip -o")
	clip = clipboard.read()
	clipboard.close()
	try:
		txt = Blender.Text.Get(textObName)
		txt.clear()
	except:
		txt = Blender.Text.New(textObName)
	
	txt.write(clip)

Blender.Draw.Register(gui,event,bevent)



