from GUI import Font, Window, TextField, Button, application

fancy = Font("Times", 24, ['italic'])

win_num = 0


class TestWindow(Window):
    pass
    
    def key_down(self, e):
        print e
        Window.key_down(self, e)


def show_text(win):
    fields = [win.tf1, win.tf2, win.tf3]
    n = None
    t = win.target
    for i, f in enumerate(fields):
        print "Field %d:" % (i + 1), repr(f.text)
        if f is t:
            n = i + 1
    if n:
        print "Focus: Field %d: Selection = %r" % (n, t.selection)
    else:
        print "No focus"

def make_window():
    global win_num
    win_num += 1
    win = TestWindow(size = (240, 200), title = "Text fields %d" % (win_num))	
    win.tf1 = TextField(position = (20, 20), width = 200)
    win.tf2 = TextField(position = (20, win.tf1.bottom + 10), width = 200,
        text = "Spam\nGlorious Spam", multiline = True, lines = 2)
    win.tf3 = TextField(position = (20, win.tf2.bottom + 10), width = 200, font = fancy)
    sbut = Button("Show", position = (20, win.tf3.bottom + 20),	
        action = (show_text, win))
    nbut = Button("New", action = make_window)
    nbut.position = (win.tf3.bottom + 20, win.tf3.right - nbut.width)
    win.add(win.tf1)
    win.add(win.tf2)
    win.add(win.tf3)
    win.add(sbut)
    win.add(nbut)
    win.tf1.become_target()
    win.show()
    return win

win = make_window()

instructions = """
There should be a window containing 3 text fields:

1. A single-line text field
2. A 2-line text field with some initial text
3. A single-line field with a large italic font

A. Field 1 should have the initial keyboard focus.

B. Field 2 should allow multi-line editing, the others should not.

C. Return or Enter in a single-line field should report a key event.

D. Tabbing between all fields should work.

E. Cut, Copy, Paste, Clear, Select All commands and their keyboard equivalents
should work. Their menu items should be enabled or disabled as appropriate.

F. The Show button should report the contents of each text field and the
selection range of the field having the keyboard focus.

G. Use New button to create an additional window and ensure that switching
focus between windows and cut/copy/paste between windows works correctly.
"""

print instructions

application().run()
