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

Source Code for Module libxyz.ui.box

  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   
20 -class Box(lowui.WidgetWrap):
21 """ 22 Base box 23 """ 24 25 # Skin rulesets resolution order 26 resolution = (u"box", u"widget") 27
28 - def __init__(self, xyz, body, message, title="", width=60):
29 """ 30 @param xyz: XYZ data 31 @param body: Top-level widget 32 @param message: Message to display 33 @param title: Box title 34 @param width: Box width 35 36 Required resources: title, box, mount 37 """ 38 39 self.xyz = xyz 40 self.screen = xyz.screen 41 self.skin = xyz.skin 42 self.message = message 43 self.full_width = width 44 self._enc = xyzenc 45 46 self.mount_span = {u"vertical": 2, u"horizontal": 2} 47 48 self._attr = lambda name: self.skin.attr(self.resolution, name)
49 50 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 51
52 - def calc_size(self, rowspan):
53 """ 54 Calculate size 55 """ 56 57 self.rowspan = rowspan 58 self.box_width = self.full_width - self.mount_span[u"horizontal"] 59 self.box_height = self._rows(self.message) 60 self.full_height = self.box_height + self.mount_span[u"vertical"]
61 62 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 63
64 - def parent_init(self, box):
65 """ 66 Init parent class 67 """ 68 69 super(Box, self).__init__(box)
70 71 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 72
73 - def show(self, dim=None):
74 """ 75 Show box until key pressed 76 @param dim: Dimension 77 """ 78 79 if dim is None: 80 dim = self.screen.get_cols_rows() 81 82 self.screen.draw_screen(dim, self.render(dim, True)) 83 84 _input = None 85 86 while True: 87 _input = self.xyz.input.get() 88 89 if _input: 90 break 91 92 return _input
93 94 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 95
96 - def _rows(self, msg):
97 """ 98 Calculate required rows 99 """ 100 101 # 2 for two rows: on top and bottom 102 _maxrows = self.screen.get_cols_rows()[1] - \ 103 2 - self.mount_span[u"vertical"] 104 _lines = msg.count("\n") 105 106 if _lines + self.rowspan > _maxrows: 107 _rows = _maxrows 108 else: 109 _rows = _lines + self.rowspan 110 111 return _rows
112 113 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 114
115 - def _strip_title(self, title):
116 """ 117 Strip title if needed 118 """ 119 120 _maxlen = self.box_width - 6 121 _len = len(title) 122 123 _stripped = title 124 125 if _len >= _maxlen: 126 _stripped = u"%s..." % title[:_maxlen - 3] 127 128 return _stripped
129