#
#   Python GUI - Text fields - Macintosh
#

from struct import pack, unpack
from Carbon.Ctl import CreateEditTextControl, SetKeyboardFocus
from Carbon.Controls import kControlEditTextTextTag, kControlEditTextSelectionTag, \
    kControlEditTextPasswordTag, kControlFocusNextPart, kControlFocusNoPart, \
    kControlEditTextTEHandleTag
from Carbon.Events import cmdKey
from MacOS import Error as MacOS_Error
from Properties import overridable_property
from MacDummy import _mac_dummy_window
from StdFonts import application_font
from GTextFields import TextField as GTextField

class TextField(GTextField):
    
    #_multiline = 0
    
    def __init__(self, font = application_font, lines = 1,
            text = "", multiline = 0, password = 0, **kwds):
        self._multiline = multiline
        self._password = password
        height = lines * font.height
        mac_control = CreateEditTextControl(_mac_dummy_window,
            (0, 0, 150, height), text, password, 1, 
            (0, 0, 0, 0, 0, 0, (0, 0, 0), (0, 0, 0)))
        GTextField.__init__(self, mac_control, **kwds)
    
    def get_text(self):
        tag = self._mac_text_data_tag()
        return self._mac_control.GetControlData(0, tag)
    
    def set_text(self, text):
        tag = self._mac_text_data_tag()
        #self._with_focus(self._mac_control.SetControlData, 0, tag, text)
        self._with_focus(self._mac_set_data, tag, text)
    
    def _mac_set_data(self, tag, data):
        # NOTE: Must be called inside _with_focus.
        self._mac_control.SetControlData(0, tag, data)
        self._mac_invalidate()
    
    def _mac_text_data_tag(self):
        if self._password:
            return kControlEditTextPasswordTag
        else:
            return kControlEditTextTextTag

    def get_selection(self):
        data = self._mac_control.GetControlData(0, kControlEditTextSelectionTag)
        return unpack("hh", data)
    
    def set_selection(self, v):
        data = pack("hh", v)
        self._with_focus(self._mac_control.SetControlData, 0, kControlEditTextSelectionTag, data)
    
    def get_multiline(self):	
        return self._multiline
    
    def get_password(self):
        return self._password
    
    def _text_size(self):
        return self._mac_control.GetControlDataSize(0, kControlEditTextTextTag)

    def select_all(self):
        self.selection = (0, self._text_size())

    def _mac_handle_mouse_event(self, event):
        self.become_target()
        GTextField._mac_handle_mouse_event(self, event)
    
    def targeted(self, state):
        win = self.window
        if win:
            if state:
                part = kControlFocusNextPart
            else:
                part = kControlFocusNoPart
            self._with_focus(SetKeyboardFocus, win._mac_window, self._mac_control, part)
    
    def key_down(self, event):
        c = event.char
        if c in '\x03\x1b' or (c == '\r' and not self._multiline):
            self.pass_to_next_handler('key_down', event)
        else:
            GTextField.key_down(self, event)
    
#	def setup_menus(self, m):
#		selbeg, selend = self.selection
#		anysel = selbeg < selend
#		try:
#			anyscrap = GetCurrentScrap().GetScrapFlavorSize('TEXT') > 0
#		except MacOS_Error:
#			anyscrap = 0
#		m.cut_cmd.enabled = anysel
#		m.copy_cmd.enabled = anysel
#		m.paste_cmd.enabled = anyscrap
#		m.clear_cmd.enabled = anysel
    
    def cut_cmd(self):
        self._with_focus(self._mac_control.HandleControlKey, 0, ord('X'), cmdKey)

    def copy_cmd(self):
        self._with_focus(self._mac_control.HandleControlKey, 0, ord('C'), cmdKey)

    def paste_cmd(self):
        self._with_focus(self._mac_control.HandleControlKey, 0, ord('V'), cmdKey)

    def clear_cmd(self):
        self._with_focus(self._mac_control.HandleControlKey, 0, 8, 0)

