Esempio n. 1
0
class TextField(Control, ActionBase, EditCmdHandler):
    """A control for entering and editing small amounts of text."""

    text = overridable_property('text')
    selection = overridable_property('selection', "Range of text selected.")
    multiline = overridable_property('multiline',
                                     "Multiple text lines allowed.")
    password = overridable_property('password',
                                    "Display characters obfuscated.")
    enter_action = action_property(
        'enter_action', "Action to be performed "
        "when the Return or Enter key is pressed.")
    escape_action = action_property(
        'escape_action', "Action to be performed "
        "when the Escape key is pressed.")

    _may_be_password = True

    #_tabbable = True
    _default_tab_stop = True
    _user_tab_stop_override = False
    _enter_action = 'do_default_action'
    _escape_action = 'do_cancel_action'

    _intercept_tab_key = True

    def __init__(self, **kwds):
        self._multiline = kwds.pop('multiline')
        Control.__init__(self, **kwds)

    def get_multiline(self):
        return self._multiline

    def key_down(self, event):
        #print "GTextField.key_down for", self ###
        c = event.char
        if c == '\r':
            if event.key == 'enter' or not self._multiline:
                self.do_enter_action()
                return
        if c == '\x1b':
            self.do_escape_action()
            return
        if c == '\t':
            if self._intercept_tab_key:
                self.pass_event_to_next_handler(event)
                return
        Control.key_down(self, event)

    def setup_menus(self, m):
        Control.setup_menus(self, m)
        EditCmdHandler.setup_menus(self, m)

    def do_enter_action(self):
        self.do_named_action('enter_action')

    def do_escape_action(self):
        self.do_named_action('escape_action')

    def get_text_length(self):
        #  Implementations can override this if they have a more
        #  efficient way of getting the text length.
        return len(self.text)

    def get_value(self):
        return self.text

    def set_value(self, x):
        self.text = x
Esempio n. 2
0
class Dialog(Window, ActionBase):

    _default_keys = "\r"
    _cancel_keys = "\x1b"

    default_button = overridable_property(
        'default_button', "Button to be activated by the default key.")

    cancel_button = overridable_property(
        'cancel_button', "Button to be activated by the cancel key.")

    _default_button = None
    _cancel_button = None

    default_action = action_property(
        'default_action', "Action to perform when Return or Enter is pressed.")

    cancel_action = action_property(
        'cancel_action', "Action to perform when Escape is pressed.")

    _default_action = 'ok'
    _cancel_action = 'cancel'

    def __init__(self,
                 style='nonmodal_dialog',
                 closable=0,
                 zoomable=0,
                 resizable=0,
                 **kwds):
        if 'title' not in kwds:
            kwds['title'] = Globals.application_name
        Window.__init__(self,
                        style=style,
                        closable=closable,
                        zoomable=zoomable,
                        resizable=resizable,
                        **kwds)

    def get_default_button(self):
        return self._default_button

    def set_default_button(self, button):
        self._default_button = button
        if button:
            button.style = 'default'
            if not button.action:
                button.action = 'do_default_action'

    def get_cancel_button(self):
        return self._cancel_button

    def set_cancel_button(self, button):
        self._cancel_button = button
        if button:
            button.style = 'cancel'
            if not button.action:
                button.action = 'do_cancel_action'

    def key_down(self, event):
        #print "GDialog.key_down:", repr(event.char) ###
        c = event.char
        if c:
            if c in self._default_keys:
                self._activate_button(
                    self.default_button) or self.do_default_action()
                return
            elif c in self._cancel_keys:
                self._activate_button(
                    self.cancel_button) or self.do_cancel_action()
                return
        Window.key_down(self, event)

    def do_default_action(self):
        self.do_named_action('default_action')

    def do_cancel_action(self):
        self.do_named_action('cancel_action')

    def _activate_button(self, button):
        #print("GDialog._activate_button:", button)
        if button:
            button.activate()
            return True
        else:
            return False