#
#   Python GUI - Text fields - Gtk
#

import gtk
from Properties import overridable_property
from Applications import application
from StdFonts import application_font
from GTextFields import TextField as GTextField

class TextField(GTextField):
    
    _multiline = 0
    
    def __init__(self, #font = application_font, #lines = 1, 
            multiline = 0, password = 0, **kwds):
        self._multiline = multiline
        if multiline:
            gtk_textbuffer = gtk.TextBuffer()
            gtk_textview = gtk.TextView(gtk_textbuffer)
            gtk_frame = gtk.Frame()
            gtk_frame.set_shadow_type(gtk.SHADOW_IN)
            gtk_frame.add(gtk_textview)
            self._vertical_padding = 2 * gtk_frame.style.ythickness
            #gtk_frame.set_size_request(150, lines * font.height + 2 * gtk_frame.style.ythickness)
            self._gtk_textbuffer = gtk_textbuffer
            gtk_outer = gtk_frame
            #self._gtk_connect(gtk_textbuffer, 'key-press-event', self._gtk_key_press_event)
        else:
            gtk_entry = gtk.Entry()
            self._gtk_entry = gtk_entry
            gtk_outer = gtk_entry
        if password:
            if not multiline:
                self._gtk_entry.set_visibility(0)
            else:
                raise ValueError("The password option is not supported for multiline"
                    " TextFields in this implementation")
        gtk_outer.show_all()
        GTextField.__init__(self, _gtk_outer = gtk_outer, **kwds)
    
    def get_text(self):
        if self._multiline:
            gtk_textbuffer = self._gtk_textbuffer
            start = gtk_textbuffer.get_start_iter()
            end = gtk_textbuffer.get_end_iter()
            return self._gtk_textbuffer.get_text(start, end, 1)
        else:
            return self._gtk_entry.get_text()
    
    def set_text(self, text):
        if self._multiline:
            self._gtk_textbuffer.set_text(text)
        else:
            self._gtk_entry.set_text(text)
    
    def get_selection(self):
        if self._multiline:
            gtk_textbuffer = self._gtk_textbuffer
            start_iter, end_iter = self._gtk_get_sel_iters()
            start = start_iter.get_offset()
            end = end_iter.get_offset()
            sel = (start, end)
        else:
            sel = self._gtk_get_sel_positions()
        return sel
    
    def _gtk_get_sel_iters(self):
        gtk_textbuffer = self._gtk_textbuffer
        sel_iters = gtk_textbuffer.get_selection_bounds()
        if not sel_iters:
            insert_mark = gtk_textbuffer.get_insert()
            insert_iter = gtk_textbuffer.get_iter_at_mark(insert_mark)
            sel_iters = (insert_iter, insert_iter)
        return sel_iters
    
    def _gtk_get_sel_positions(self):
        gtk_entry = self._gtk_entry
        sel = gtk_entry.get_selection_bounds()
        if not sel:
            pos = gtk_entry.get_position()
            sel = (pos, pos)
        return sel
    
    def set_selection(self, (start, end)):
        if self._multiline:
            gtk_textbuffer = self._gtk_textbuffer
            start_iter = gtk_textbuffer.get_iter_at_offset(start)
            end_iter = gtk_textbuffer.get_iter_at_offset(end)
            gtk_textbuffer.select_range(start_iter, end_iter)
        else:
            self._gtk_entry.select_region(start, end)
    
    def get_multiline(self):	
        return self._multiline
    
    def get_password(self):
        return self._multiline and \
            not self._gtk_entry.get_visibility()
    
#	def set_password(self, v):
#		if not self._multiline:
#			self._gtk_entry.set_visibility(not v)
#		elif v:
#			raise ValueError("The password option is not supported for multiline"
#				" TextFields in this implementation")
    
    def select_all(self):
        if self._multiline:
            gtk_textbuffer = self._gtk_textbuffer
            start = gtk_textbuffer.get_start_iter()
            end = gtk_textbuffer.get_end_iter()
            gtk_textbuffer.select_range(start_iter, end_iter)
        else:
            self._gtk_entry.select_region(0, -1)

    def cut_cmd(self):
        self.copy_cmd()
        self.clear_cmd()
    
    def copy_cmd(self):
        if self._multiline:
            gtk_textbuffer = self._gtk_textbuffer
            start_iter, end_iter = self._gtk_get_sel_iters()
            text = gtk_textbuffer.get_text(start_iter, end_iter, 1)
        else:
            start, end = self._gtk_get_sel_positions()
            text = self._gtk_entry.get_chars(start, end)
        if text:
            application().set_clipboard(text)
    
    def paste_cmd(self):
        text = application().get_clipboard()
        self.clear_cmd()
        if self._multiline:
            self._gtk_textbuffer.insert_at_cursor(text)
        else:
            gtk_entry = self._gtk_entry
            pos = gtk_entry.get_position()
            gtk_entry.insert_text(text, pos)
    
    def clear_cmd(self):
        if self._multiline:
            gtk_textbuffer = self._gtk_textbuffer
            start_iter, end_iter = self._gtk_get_sel_iters()
            gtk_textbuffer.delete(start_iter, end_iter)
        else:
            start, end = self._gtk_get_sel_positions()
            self._gtk_entry.delete_text(start, end)

#	def _gtk_key_press_event(self, gtk_event):
#		if gtk_event.keyval == 0x0D and not self._multiline:
#			self._handle_gtk_key_event(gtk_event)
#			return 1
#		else:
#			return 0
