#
#   Python GUI - Scroll bars - Macintosh
#

from Carbon.Ctl import CreateScrollBarControl
from Carbon.Controls import kControlUpButtonPart, kControlDownButtonPart, \
    kControlPageUpPart, kControlPageDownPart, kControlIndicatorPart
from Properties import overridable_property
from MacDummy import _mac_dummy_window
from Controls import Control
from GScrollBars import ScrollBar as GScrollBar

class ScrollBar(Control, GScrollBar):

    value = overridable_property('value', "Setting of the scroll bar (0.0 - 1.0).")
    proportion = overridable_property('proportion', "Relative thumb size (0.0 - 1.0).")

    def __init__(self, direction = None, **kwds):
        mac_control = CreateScrollBarControl(_mac_dummy_window,
            (0, 0, 15, 15), 0, 0, 32767, 0, 0, None)
        Control.__init__(self, mac_control, **kwds)

    def destroy(self):
        GScrollBar.destroy(self)
        Control.destroy(self)
    
    def get_value(self):
        return self._mac_control.GetControlValue() / 32767.0
     
    def set_value(self, x):
        self._change_value(max(0.0, min(x, 1.0)))
    
    def _change_value(self, v):
        self._with_focus(self._mac_control.SetControlValue, int(32767.0 * v))
    
    def get_proportion(self):
        return self._mac_control.GetControlViewSize() / 32767.0
    
    def set_proportion(self, x):
        self._change_proportion(max(0.0, min(x, 1.0)))

    def _change_proportion(self, v):
        self._with_focus(self._mac_control.SetControlViewSize, int(32767.0 * v))

    def is_vertical(self):
        """Returns 1 if scroll bar is vertical, 0 if horizontal."""
        width, height = self.size
        return width < height

    def update(self):
        client = self._client
        if client:
            if self.is_vertical():
                v, p = client.v_scroll_bar_value()
            else:
                v, p = client.h_scroll_bar_value()
            self.value = v
            self.proportion = p

    def notify_client(self, msg, *args):
        client = self._client
        if client:
            getattr(client, msg)(*args)
    def _mac_control_part_hit(self, mac_part):
        vertical = self.is_vertical()
        if mac_part == kControlUpButtonPart:
            if vertical:
                self.notify_client('scroll_line_up')
            else:
                self.notify_client('scroll_line_left')
        elif mac_part == kControlDownButtonPart:
            if vertical:
                self.notify_client('scroll_line_down')
            else:
                self.notify_client('scroll_line_right')
        elif mac_part == kControlPageUpPart:
            if vertical:
                self.notify_client('scroll_page_up')
            else:
                self.notify_client('scroll_page_left')
        elif mac_part == kControlPageDownPart:
            if vertical:
                self.notify_client('scroll_page_down')
            else:
                self.notify_client('scroll_page_right')
        elif mac_part == kControlIndicatorPart:
            if vertical:
                self.notify_client('scroll_to_v', self.value)
            else:
                self.notify_client('scroll_to_h', self.value)
