Project

Profile

Help

How to connect?
Download (3.49 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / ui / window.py @ e1d2d0dc

1
"""This file contains the MainWindow class."""
2

    
3
import wx
4

    
5
from dialogs.preferences import PreferencesDialog
6
from event import EVT_FOCUS
7

    
8
class MainWindow(wx.Frame):
9

    
10
    def __init__(self, settings):
11
        super(MainWindow, self).__init__(None)
12
        self.settings = settings
13
        self.CreateMenuBar()
14
        self.InitUI()
15

    
16
    def CreateMenuBar(self):
17
        """Create the GUI menu bar and hierarchy of menus."""
18
        menubar = wx.MenuBar()
19
        fileMenu = wx.Menu()
20
        preferences = wx.MenuItem(fileMenu, -1, '&Preferences\tAlt+Enter')
21
        quit = wx.MenuItem(fileMenu, -1, '&Quit\tCtrl+Q')
22
        fileMenu.AppendItem(preferences)
23
        fileMenu.AppendItem(quit)
24

    
25
        self.Bind(wx.EVT_MENU, self.OnPreferences, preferences)
26
        self.Bind(wx.EVT_MENU, self.OnQuit, quit)
27

    
28
        menubar.Append(fileMenu, '&File')
29

    
30
        self.SetMenuBar(menubar)
31

    
32
    def InitUI(self):
33
        self.panel = MUDPanel(self)
34
        self.SetTitle("CocoMUD client")
35
        self.Centre()
36
        self.Show()
37

    
38
    def OnPreferences(self, e):
39
        """Open the preferences dialog box."""
40
        dialog = PreferencesDialog(self.settings)
41
        dialog.ShowModal()
42
        dialog.Destroy()
43
    def OnQuit(self, e):
44
        self.Close()
45

    
46

    
47
class MUDPanel(wx.Panel):
48

    
49
    def __init__(self, parent):
50
        wx.Panel.__init__(self, parent)
51
        self.client = None
52

    
53
        mainSizer = wx.GridBagSizer(5, 5)
54

    
55
        # Input
56
        l_input = wx.StaticText(self, -1, "Input")
57
        t_input = wx.TextCtrl(self, -1, "", size=(125, -1),
58
                style=wx.TE_PROCESS_ENTER)
59
        self.input = t_input
60

    
61
        # Password
62
        l_password = wx.StaticText(self, -1, "Password")
63
        t_password = wx.TextCtrl(self, -1, "", size=(20, -1),
64
                style=wx.TE_PROCESS_ENTER | wx.TE_PASSWORD)
65
        self.password = t_password
66
        t_password.Hide()
67

    
68
        # Add the input field in the sizer
69
        mainSizer.Add(l_input, pos=(0, 0))
70
        mainSizer.Add(t_input, pos=(0, 1), span=(1, 6))
71
        mainSizer.Add(l_password, pos=(0, 7))
72
        mainSizer.Add(t_password, pos=(0, 8), span=(1, 2))
73

    
74
        # Ouput
75
        l_output = wx.StaticText(self, -1, "Output")
76
        t_output = wx.TextCtrl(self, -1, "",
77
                size=(800, 800), style=wx.TE_MULTILINE|wx.TE_READONLY)
78
        self.output = t_output
79

    
80
        # Add the output fields in the sizer
81
        mainSizer.Add(l_output, pos=(1, 0))
82
        mainSizer.Add(t_output, pos=(10, 10))
83

    
84
        # Event handler
85
        t_input.Bind(wx.EVT_TEXT_ENTER, self.EvtText)
86
        t_password.Bind(wx.EVT_TEXT_ENTER, self.EvtText)
87
        self.Bind(EVT_FOCUS, self.OnFocus)
88
        t_output.Bind(wx.EVT_KEY_DOWN, self.OnKeyDownInOutput)
89

    
90
    def EvtText(self, event):
91
        """One of the input fields is sending text."""
92
        self.input.Clear()
93
        self.password.Clear()
94
        msg = event.GetString().encode("utf-8", "replace")
95
        self.client.write(msg + "\r\n")
96

    
97
    def OnFocus(self, evt):
98
        val = evt.GetValue()
99
        if val == "input":
100
            self.input.Show()
101
            self.input.SetFocus()
102
            self.password.Hide()
103
        elif val == "password":
104
            self.password.Show()
105
            self.password.SetFocus()
106
            self.input.Hide()
107

    
108
    def OnKeyDownInOutput(self, e):
109
        """A key is pressed while in the output."""
110
        key = e.GetUnicodeKey()
111
        print "pressing in output", key
112
        if key == 66:
113
            self.input.SetFocus()
114
            wx.PostEvent(self.input, e)
115
        else:
116
            e.Skip()
(3-3/3)