#
#   Python GUI - PyObjC
#
#   Base class for controls based on an NSTextField
#

from Foundation import NSRect, NSPoint, NSSize
from AppKit import NSTextField, NSSecureTextField, NSTextFieldCell
from Colors import Color
from Controls import ns_size_to_fit
from StdFonts import system_font

class TextFieldBasedControl(object):

    #_leading_fraction = 0.25

    def _create_ns_textfield(self, editable, text, font,
            multiline = False, password = False, padding = (0, 0)):
        self._ns_is_password = password
        if password:
            ns_class = NSSecureTextField
        else:
            ns_class = _PyGUI_NSTextField
        ns_frame = NSRect(NSPoint(0, 0), NSSize(20, 10))
        ns_textfield = ns_class.alloc().initWithFrame_(ns_frame)
        if multiline and not password:
            ns_textfield.pygui_multiline = True
        ns_textfield.setBezeled_(editable)
        if not editable:
            ns_textfield.setBordered_(False)
            ns_textfield.setDrawsBackground_(False)
        ns_textfield.setEditable_(editable)
        ns_textfield.setSelectable_(editable)
        ns_textfield.setFont_(font._ns_font)
        ns_textfield.setStringValue_(text)
        ns_size_to_fit(ns_textfield, padding = padding)
        return ns_textfield

    def get_text(self):
        return self._ns_inner_view.stringValue()
    
    def set_text(self, v):
        self._ns_inner_view.setStringValue_(v)

    def get_color(self):
        return Color._from_ns_color(self._ns_inner_view.textColor())
    
    def set_color(self, v):
        self._ns_inner_view.setTextColor_(v._ns_color)

#------------------------------------------------------------------------------

#class _PyGUI_Multiline_NSTextFieldCell(NSTextFieldCell):
#
#	def setUpFieldEditorAttributes_(self, ns_text):
#		print "_PyGUI_Multiline_NSTextFieldCell.setUpFieldEditorAttributes_" ###
#		ns_text.setFieldEditor_(False)
#		return ns_text

class _PyGUI_NSTextField(NSTextField):

    pygui_multiline = False

    #def keyDown_(self, ns_event):
    #	print "_PyGUI_NSTextField.keyDown_:", ns_event ###
    #	NSTextField.keyDown_(self, ns_event)
