1#!/usr/bin/env python
2"""
3"""
4from __future__ import unicode_literals
5
6from pygments.lexers.html import HtmlLexer
7
8from prompt_toolkit.application import Application
9from prompt_toolkit.application.current import get_app
10from prompt_toolkit.completion import WordCompleter
11from prompt_toolkit.key_binding import KeyBindings
12from prompt_toolkit.key_binding.bindings.focus import (
13    focus_next,
14    focus_previous,
15)
16from prompt_toolkit.layout.containers import Float, HSplit, VSplit
17from prompt_toolkit.layout.dimension import D
18from prompt_toolkit.layout.layout import Layout
19from prompt_toolkit.layout.menus import CompletionsMenu
20from prompt_toolkit.lexers import PygmentsLexer
21from prompt_toolkit.styles import Style
22from prompt_toolkit.widgets import (
23    Box,
24    Button,
25    Checkbox,
26    Dialog,
27    Frame,
28    Label,
29    MenuContainer,
30    MenuItem,
31    ProgressBar,
32    RadioList,
33    TextArea,
34)
35
36
37def accept_yes():
38    get_app().exit(result=True)
39
40
41def accept_no():
42    get_app().exit(result=False)
43
44
45def do_exit():
46    get_app().exit(result=False)
47
48
49yes_button = Button(text='Yes', handler=accept_yes)
50no_button = Button(text='No', handler=accept_no)
51textfield  = TextArea(lexer=PygmentsLexer(HtmlLexer))
52checkbox1 = Checkbox(text='Checkbox')
53checkbox2 = Checkbox(text='Checkbox')
54
55radios = RadioList(values=[
56    ('Red', 'red'),
57    ('Green', 'green'),
58    ('Blue', 'blue'),
59    ('Orange', 'orange'),
60    ('Yellow', 'yellow'),
61    ('Purple', 'Purple'),
62    ('Brown', 'Brown'),
63])
64
65animal_completer = WordCompleter([
66    'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison',
67    'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphin',
68    'dove', 'duck', 'eagle', 'elephant', 'fish', 'goat', 'gorilla', 'kangaroo',
69    'leopard', 'lion', 'mouse', 'rabbit', 'rat', 'snake', 'spider', 'turkey',
70    'turtle', ], ignore_case=True)
71
72root_container = HSplit([
73    VSplit([
74        Frame(body=Label(text='Left frame\ncontent')),
75        Dialog(title='The custom window',
76               body=Label('hello\ntest')),
77        textfield,
78    ], height=D()),
79    VSplit([
80        Frame(body=ProgressBar(),
81              title='Progress bar'),
82        Frame(title='Checkbox list',
83              body=HSplit([
84                  checkbox1,
85                  checkbox2,
86              ])),
87        Frame(title='Radio list', body=radios),
88    ], padding=1),
89    Box(
90        body=VSplit([
91            yes_button,
92            no_button,
93        ], align='CENTER', padding=3),
94        style='class:button-bar',
95        height=3,
96    ),
97])
98
99root_container = MenuContainer(body=root_container, menu_items=[
100    MenuItem('File', children=[
101        MenuItem('New'),
102        MenuItem('Open', children=[
103            MenuItem('From file...'),
104            MenuItem('From URL...'),
105            MenuItem('Something else..', children=[
106                MenuItem('A'),
107                MenuItem('B'),
108                MenuItem('C'),
109                MenuItem('D'),
110                MenuItem('E'),
111            ]),
112        ]),
113        MenuItem('Save'),
114        MenuItem('Save as...'),
115        MenuItem('-', disabled=True),
116        MenuItem('Exit', handler=do_exit),
117        ]),
118    MenuItem('Edit', children=[
119        MenuItem('Undo'),
120        MenuItem('Cut'),
121        MenuItem('Copy'),
122        MenuItem('Paste'),
123        MenuItem('Delete'),
124        MenuItem('-', disabled=True),
125        MenuItem('Find'),
126        MenuItem('Find next'),
127        MenuItem('Replace'),
128        MenuItem('Go To'),
129        MenuItem('Select All'),
130        MenuItem('Time/Date'),
131    ]),
132    MenuItem('View', children=[
133        MenuItem('Status Bar'),
134    ]),
135    MenuItem('Info', children=[
136        MenuItem('About'),
137    ]),
138], floats=[
139    Float(xcursor=True,
140          ycursor=True,
141          content=CompletionsMenu(
142              max_height=16,
143              scroll_offset=1)),
144])
145
146# Global key bindings.
147bindings = KeyBindings()
148bindings.add('tab')(focus_next)
149bindings.add('s-tab')(focus_previous)
150
151
152style = Style.from_dict({
153    'window.border': '#888888',
154    'shadow': 'bg:#222222',
155
156    'menu-bar': 'bg:#aaaaaa #888888',
157    'menu-bar.selected-item': 'bg:#ffffff #000000',
158    'menu': 'bg:#888888 #ffffff',
159    'menu.border': '#aaaaaa',
160    'window.border shadow': '#444444',
161
162    'focused  button': 'bg:#880000 #ffffff noinherit',
163
164    # Styling for Dialog widgets.
165
166    'radiolist focused': 'noreverse',
167    'radiolist focused radio.selected': 'reverse',
168
169    'button-bar': 'bg:#aaaaff'
170})
171
172
173application = Application(
174    layout=Layout(
175        root_container,
176        focused_element=yes_button,
177    ),
178    key_bindings=bindings,
179    style=style,
180    mouse_support=True,
181    full_screen=True)
182
183
184def run():
185    result = application.run()
186    print('You said: %r' % result)
187
188
189if __name__ == '__main__':
190    run()
191