#
#   Python GUI - Alert base class - Generic
#

import textwrap
from ModalDialogs import ModalDialog
from Labels import Label
from Exceptions import Cancel


class BaseAlert(ModalDialog):

    _wrapwidth = 50
    _minimum_width = 200

    yes_button = None
    no_button = None
    other_button = None

    def __init__(self, kind, prompt, width = None, lines = None,
            button_labels = None, default = None, cancel = None):
        #if width is None:
        #	width = self._default_width
        #if lines is None:
        #	lines = self._default_lines
        ModalDialog.__init__(self)
        self.label = Label(text = self._wrap(prompt), lines = lines)
        if self.label.width < self._minimum_width:
            self.label.width = self._minimum_width
        self.create_buttons(*button_labels)
        self.default_button = self._find_button(default)
        self.cancel_button = self._find_button(cancel)
        self.layout(kind)
    
    def _wrap(self, text):
        width = self._wrapwidth
        return "\n\n".join(
            [textwrap.fill(para, width)
                for para in text.split("\n\n")])
    
    def _find_button(self, value):
        #print "BaseAlert._find_button:", value ###
        if value == 1:
            result = self.yes_button
        elif value == 0:
            result = self.no_button
        elif value == -1:
            result = self.other_button
        else:
            result = None
        #print "BaseAlert._find_button: result =", result ###
        return result
    
    def yes(self):
        self.dismiss(1)
    
    def no(self):
        self.dismiss(0)
    
    def other(self):
        self.dismiss(-1)



