1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25 """
26 Yes/No box. Shows a message and waits for Yes or No button pressed
27 """
28
29
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
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
131
132
133
135 """
136 Move focus
137 """
138
139 for key in keys:
140 _widget = None
141
142
143 if key in (self.keys.RIGHT, self.keys.UP):
144 _widget = 1
145
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
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