#
#   Python GUI - Controls - Macintosh
#

from Carbon import Ctl
from Carbon.Controls import \
    kControlUseFontMask, kControlUseFaceMask, kControlUseSizeMask, \
    kControlUseJustMask, kControlUseForeColorMask
from Carbon import TextEdit as TEk
from Properties import overridable_property
from StdColors import black
from StdFonts import system_font
from GControls import Control as GControl

class Control(GControl):
    """A component which encapsulates a Macintosh control."""
    
    _color = black
    _font = system_font
    _just = 'left'
    
    def __init__(self, mac_control, **kwds):
        self._mac_control = mac_control
        mac_control.DeactivateControl()
        GControl.__init__(self, **kwds)
    
    def get_title(self):
        return self._mac_control.GetControlTitle()
    
    def set_title(self, v):
        self._with_focus(_mac_control.SetControlTitle, v)
    
    def get_enabled(self):
        return self._mac_control.IsControlEnabled()
    
    def set_enabled(self, v):
        if v:
            self._with_focus(self._mac_control.EnableControl)
        else:
            self._with_focus(self._mac_control.DisableControl)
    
    def get_color(self):
        return self._color
    
    def set_color(self, v):
        self._color = v
        self._with_focus(self._mac_control.SetControlFontStyle, (kControlUseForeColorMask,
            0, 0, 0, 0, 0, self._color._rgb, (0, 0, 0)))
            
    def get_font(self):
        return self._font
    
    def set_font(self, f):
        self._font = f
        self._with_focus(self._mac_control.SetControlFontStyle, (
            kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask,
            f._mac_font, f._mac_size, f._mac_face, 0, 0, (0, 0, 0), (0, 0, 0)))
        
    def get_just(self):
        return self._just
    
    def set_just(self, v):
        self._just = v
        mac_just = self._just_to_mac_just[v]
        self._with_focus(self._mac_control.SetControlFontStyle, (kControlUseJustMask,
            0, 0, 0, 0, mac_just, (0, 0, 0), (0, 0, 0)))
        
    _just_to_mac_just = {
        'left': TEk.teFlushLeft, 
        'centre': TEk.teCenter, 
        'center': TEk.teCenter, 
        'right': TEk.teJustRight
    }
    
    def get_lines(self):
        return self.height / self.font.height
    
    def set_lines(self, v):
        self.height = v * self.font.height
    
    def _mac_get_control_value(self):
        return self._mac_control.GetControlValue()
    
    def _mac_set_control_value(self, v):
        self._with_focus(self._mac_control.SetControlValue, v)
    
    def _mac_update_viewport(self):
        #print "Control._mac_update_viewport" ###
        container = self._container
        if container:
            window = container.window
            if window:
                #print "Component._mac_update_viewport: embedding control" ###
                self._mac_control.EmbedControl(window._mac_root_control)

    def _mac_update(self):
        self._mac_control.DrawControlInCurrentPort()
    
    def get_bounds(self):
        return self._mac_control.GetControlBounds()

    def _mac_component_change_bounds(self, v):
        self._mac_control.SetControlBounds(v)

    def _dispatch_mouse_event(self, event):
        #print "Control._dispatch_mouse_event:", event ###
        self._with_focus(self._mac_handle_mouse_event, event)
    
    def _mac_handle_mouse_event(self, event):
        #print "Control._mac_handle_mouse_event:", event.position,  event._mac_modifiers ###
        part = self._mac_control.HandleControlClick(event.position, event._mac_modifiers, None)
        #print "...part =", part ###
        self._mac_control_part_hit(part)
    
    def _mac_control_part_hit(self, mac_part_code):
        pass
    
    def key_down(self, event):
        #print "Control.key_down:", event ###
        self._with_focus(self._mac_control.HandleControlKey,
            event._mac_key_code, ord(event.char), event._mac_modifiers)

    def activated(self, state):
        #print "Control.activated:", state ###
        if state:
            self._with_focus(self._mac_control.ActivateControl)
        else:
            self._with_focus(self._mac_control.DeactivateControl)
