tuikit/combobox.py
author Radek Brich <radek.brich@devl.cz>
Wed, 27 Aug 2014 23:09:53 +0200
changeset 103 49f212aa0228
parent 62 2f61931520c9
permissions -rw-r--r--
Update TextField: Handle home, end keys.

# -*- coding: utf-8 -*-

from tuikit.layout import HorizontalLayout
from tuikit.editfield import EditField
from tuikit.button import Button
from tuikit.menu import Menu
import logging


class ComboBox(HorizontalLayout):
    def __init__(self, value='', items=[]):
        HorizontalLayout.__init__(self)
        self._default_size.update(15, 1)
        self.allow_focus = True
        self.colorprefix = 'combo:'

        self._edit = EditField(value)
        self._edit.resize(self.width - 3, 1)
        self.add(self._edit, expand=True, fill=True)

        self._btn = Button('v')
        self._btn.prefix = ''
        self._btn.suffix = ''
        self._btn.resize(3, 1)
        self._btn.add_handler('click', self._on_btn_click)
        self.add(self._btn, expand=False)

        self._menu = Menu(items)
        self._menu.hide()
        self.add_floater(self._menu)

    def _on_btn_click(self, ev):
        self._menu.show()

    def on_resize(self, ev):
        HorizontalLayout.on_resize(self, ev)
        logging.getLogger('tuikit').info('w = %s', self.width)
        self._menu.move(self.x, self.y + 1)
        self._menu.resize(w=self.width)