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

Source Code for Module libxyz.ui.box_yesno

  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 YesNoBox(Box):
25 """ 26 Yes/No box. Shows a message and waits for Yes or No button pressed 27 """ 28 29 # Skin rulesets resolution order 30 resolution = (u"yesno_box", u"box", u"widget") 31
32 - def __init__(self, xyz, body, message, title="", width=60):
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, button 41 """ 42 43 super(YesNoBox, self).__init__(xyz, body, message, title, width) 44 self.calc_size(6) 45 46 self.keys = libxyz.ui.Keys() 47 48 self._yes_txt = _(u"Yes") 49 self._no_txt = _(u"No") 50 self._value = False 51 52 self._buttons = self._init_buttons() 53 54 _title = self._strip_title(title.replace(u"\n", u" ")) 55 56 if _title: 57 _title_attr = self._attr(u"title") 58 else: 59 _title = None 60 _title_attr = None 61 62 _mount = lowui.AttrWrap(lowui.Filler(lowui.Text(u"")), 63 self._attr(u"mount")) 64 65 # Main dialog text 66 _text = lowui.Text((self._attr(u"box"), message), align.CENTER) 67 _blank = lowui.Text((self._attr(u"box"), "")) 68 69 _widgets = [_text, _blank, self._buttons] 70 _box = lowui.Filler(lowui.Pile(_widgets), valign=align.BOTTOM) 71 _box = Border(_box, _title, _title_attr, self._attr(u"border")) 72 _box = lowui.AttrWrap(_box, self._attr(u"box")) 73 74 _mount = lowui.Overlay(_mount, body, align.CENTER, self.full_width, 75 align.MIDDLE, self.full_height) 76 _box = lowui.Overlay(_box, _mount, align.CENTER, self.box_width, 77 align.MIDDLE, self.box_height) 78 79 self.parent_init(_box)
80 81 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 82
83 - def show(self, dim=None):
84 """ 85 Show box and return pressed button. 86 True if YES pressed, False if NO 87 """ 88 89 if dim is None: 90 dim = self.screen.get_cols_rows() 91 while True: 92 try: 93 self.screen.draw_screen(dim, self.render(dim, True)) 94 95 _keys = self.xyz.input.get() 96 97 if self.xyz.input.WIN_RESIZE in _keys: 98 dim = self.screen.get_cols_rows() 99 continue 100 101 if [x for x in (self.keys.LEFT, 102 self.keys.RIGHT, 103 self.keys.UP, 104 self.keys.DOWN, 105 ) if x in _keys]: 106 self._change_focus(_keys) 107 108 if self.keys.ESCAPE in _keys: 109 return False 110 111 if self.keys.ENTER in _keys: 112 _button = self._buttons.focus_cell.get_w() 113 self._pressed(_button) 114 return self._value 115 except KeyboardInterrupt: 116 continue
117 118 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 119
120 - def _init_buttons(self):
121 _yes_len = len(self._yes_txt) 122 _no_len = len(self._no_txt) 123 _b_attr = self._attr("button") 124 _b_size = max(_yes_len, _no_len) + 4 # [ ... ] 125 126 self._b_yes = lowui.AttrWrap(libxyz.ui.XYZButton(self._yes_txt),_b_attr) 127 self._b_no = lowui.AttrWrap(libxyz.ui.XYZButton(self._no_txt),_b_attr) 128 129 return lowui.GridFlow([self._b_yes, self._b_no], _b_size, 2, 0, 130 align.CENTER)
131 132 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 133
134 - def _change_focus(self, keys):
135 """ 136 Move focus 137 """ 138 139 for key in keys: 140 _widget = None 141 142 # Move right 143 if key in (self.keys.RIGHT, self.keys.UP): 144 _widget = 1 # index 145 # Move left 146 elif key in (self.keys.LEFT, self.keys.DOWN): 147 _widget = 0 148 else: 149 pass 150 151 if _widget is not None: 152 self._buttons.set_focus(_widget)
153 154 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 155
156 - def _pressed(self, button):
157 """ 158 Button pressed 159 """ 160 161 _label = button.get_label() 162 163 if _label == self._yes_txt: 164 self._value = True 165 else: 166 self._value = False
167