Package libxyz :: Package ui :: Module box_input |
[hide private]
[frames] | no frames]

# Source Code for Module libxyz.ui.box_input

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
      4  #
      5  # This file is part of XYZCommander.
      6  # XYZCommander is free software: you can redistribute it and/or modify
      7  # it under the terms of the GNU Lesser Public License as published by
      8  # the Free Software Foundation, either version 3 of the License, or
      9  # (at your option) any later version.
     10  # XYZCommander is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  # GNU Lesser Public License for more details.
     14  # You should have received a copy of the GNU Lesser Public License
     15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
     16
     17  from libxyz.ui import lowui
     18  from libxyz.ui import align
     19  from libxyz.ui import Box
     20  from libxyz.ui import Border
     21
     22  import libxyz.ui
     23
     24 -class InputBox(Box):
     25      """
     26      Shows a message and waits for input
     27      """
     28
     29      # Skin rulesets resolution order
     30      resolution = (u"input_box", u"box", u"widget")
     31
     32 -    def __init__(self, xyz, body, message, title="", text="", width=70):
     33          """
     34          @param xyz: XYZ dictionary
     35          @param body: Top-level widget
     36          @param message: Message to display
     37          @param title: Box title
     38          @param width: Box width (including mount box)
     39
     40          Required resources: title, box, border, mount, input, button
     41          """
     42
     43          super(InputBox, self).__init__(xyz, body, message, title, width)
     44          self.calc_size(7)
     45
     46          self.keys = libxyz.ui.Keys()
     47
     48          _hint = lowui.Text(_(u"Press ENTER to submit value. ESCAPE to quit"),
     49                             align=align.CENTER)
     50
     51          _title = self._strip_title(title.replace(u"\n", u" "))
     52
     53          if _title:
     54              _title_attr = self._attr(u"title")
     55          else:
     56              _title, _title_attr = None, None
     57
     58          _mount = lowui.AttrWrap(lowui.Filler(lowui.Text(u"")),
     59                                  self._attr(u"mount"))
     60
     61          # Main dialog text
     62          _text = lowui.Text((self._attr(u"box"), message), align.CENTER)
     63          _blank = lowui.Text((self._attr(u"box"), ""))
     64
     65          self._edit = lowui.AttrWrap(lowui.Edit(wrap="clip", edit_text=text),
     66                                      self._attr(u"input"))
     67
     68          _widgets = [_text, _blank, self._edit, _blank, _hint]
     69          _box = lowui.Filler(lowui.Pile(_widgets), valign=align.BOTTOM)
     70          _box = Border(_box, _title, _title_attr, self._attr(u"border"))
     71          _box = lowui.AttrWrap(_box, self._attr(u"box"))
     72
     73          _mount = lowui.Overlay(_mount, body, align.CENTER, self.full_width,
     74                               align.MIDDLE, self.full_height)
     75          _box = lowui.Overlay(_box, _mount, align.CENTER, self.box_width,
     76                               align.MIDDLE, self.box_height)
     77
     78          self.parent_init(_box)
     79
     80      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     81
     82 -    def show(self, dim=None):
     83          """
     84          Show box and return input value.
     85          Return None if Escape was pressed
     86          """
     87
     88          if dim is None:
     89              dim = self.screen.get_cols_rows()
     90          while True:
     91              try:
     92                  self.screen.draw_screen(dim, self.render(dim, focus=True))
     93
     94                  _keys = self.xyz.input.get()
     95
     96                  if self.xyz.input.WIN_RESIZE in _keys:
     97                      dim = self.screen.get_cols_rows()
     98                      continue
     99
    100                  if self.keys.ESCAPE in _keys:
    101                      return None
    102
    103                  if self.keys.ENTER in _keys:
    104                      return self._edit.get_edit_text()
    105
    106                  for k in _keys:
    107                      self._edit.keypress((dim[0],), k)
    108              except KeyboardInterrupt:
    109                  continue
    110