#
#		Python GUI - Radio buttons - Macintosh
#

from Carbon.Ctl import CreateRadioButtonControl
from Actions import Action
from MacDummy import _mac_dummy_window
from StdFonts import system_font
from GRadioButtons import RadioButton as GRadioButton

class RadioButton(GRadioButton):
    """RadioButtons are used in groups to represent a 1-of-N
    choice. A group of RadioButtons is coordinated by a
    RadioGroup object. The 'group' property indicates the
    RadioGroup to which it belongs, and the 'value' property
    is the value to which the RadioGroup's value is set
    when this RadioButton is selected."""
    
    def __init__(self, title = "Radio Button", font = system_font, **kwds):
        title_width = font.width(title)
        mac_control = CreateRadioButtonControl(_mac_dummy_window,
            (0, 0, 20 + title_width, 18), title, 0, 0)
        GRadioButton.__init__(self, mac_control, **kwds)

    def _value_changed(self):
        self._update_state()

    def _update_state(self):
        group = self._group
        self._mac_set_control_value(group is not None
            and self._value == group._value)
    
    def _mac_control_part_hit(self, mac_control_part):
        if mac_control_part:
            self._select()

    def _select(self):
        group = self._group
        if group and group._value <> self._value:
            group.value = self._value
            group.do_action()


