# -*- coding: utf-8 -*-

import wx
import win32clipboard
import win32con
import gui
import treeInterceptorHandler
import textInfos
import globalVars

def getSelectedText():
	obj = globalVars.focusObject
	if isinstance(obj.treeInterceptor, treeInterceptorHandler.DocumentTreeInterceptor) and not obj.treeInterceptor.passThrough:
		obj = obj.treeInterceptor
	try:
		info = obj.makeTextInfo(textInfos.POSITION_SELECTION)
	except (RuntimeError, NotImplementedError):
		info = None
	if not info or info.isCollapsed:
		return None
	return info.text

def 
():
	try:
		win32clipboard.OpenClipboard()
	except win32clipboard.error:
		return None
	try:
		text = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
	except:
		text = None
	finally:
		win32clipboard.CloseClipboard()
	return text

def setClipboardText(text):
	if not isinstance(text, unicode) or len(text)==0 or text.isspace():
		return False

	try:
		win32clipboard.OpenClipboard()
	except win32clipboard.error:
		return False

	try:
		win32clipboard.EmptyClipboard()
		win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text)
		success = True
	except:
		success = False

	win32clipboard.CloseClipboard()
	return success

class TextWindow(wx.Frame):

	def __init__(self, text, title, readOnly=True):
		super(TextWindow, self).__init__(gui.mainFrame, title=title)
		sizer = wx.BoxSizer(wx.VERTICAL)
		style = wx.TE_MULTILINE | wx.TE_RICH
		if readOnly:
			style |= wx.TE_READONLY
		self.outputCtrl = wx.TextCtrl(self, style=style)
		self.outputCtrl.Bind(wx.EVT_KEY_DOWN, self.onOutputKeyDown)
		sizer.Add(self.outputCtrl, proportion=1, flag=wx.EXPAND)
		self.SetSizer(sizer)
		sizer.Fit(self)
		self.outputCtrl.SetValue(text)
		self.outputCtrl.SetFocus()
		self.Raise()
		self.Maximize()
		self.Show()

	def onOutputKeyDown(self, event):
		if event.GetKeyCode() == wx.WXK_ESCAPE:
			self.Close()
		event.Skip()

getClipboardText