Project

Profile

Help

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

github / src / ui / window.py @ 45e6bcc7

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

    
3
import wx
4

    
5
from ytranslate.tools import t
6

    
7
from dialogs.preferences import PreferencesDialog
8
from dialogs.macro import MacroDialog
9
from ui.event import EVT_FOCUS, FocusEvent, myEVT_FOCUS
10

    
11
class MainWindow(wx.Frame):
12

    
13
    def __init__(self, engine):
14
        super(MainWindow, self).__init__(None)
15
        self.engine = engine
16
        self.CreateMenuBar()
17
        self.InitUI()
18

    
19
    def _get_client(self):
20
        return self.panel.client
21
    def _set_client(self, client):
22
        self.panel.client = client
23
    client = property(_get_client, _set_client)
24

    
25
    def CreateMenuBar(self):
26
        """Create the GUI menu bar and hierarchy of menus."""
27
        menubar = wx.MenuBar()
28

    
29
        # Differemtn menus
30
        fileMenu = wx.Menu()
31
        gameMenu = wx.Menu()
32

    
33
        # File menu
34
        ## Preferences
35
        preferences = wx.MenuItem(fileMenu, -1, t("ui.main.menu.preferences"))
36
        self.Bind(wx.EVT_MENU, self.OnPreferences, preferences)
37
        fileMenu.AppendItem(preferences)
38

    
39
        ## Quit
40
        quit = wx.MenuItem(fileMenu, -1, t("ui.main.menu.quit"))
41
        self.Bind(wx.EVT_MENU, self.OnQuit, quit)
42
        fileMenu.AppendItem(quit)
43

    
44
        # Game menu
45
        macro = wx.MenuItem(fileMenu, -1, t("ui.main.menu.macro"))
46
        self.Bind(wx.EVT_MENU, self.OnMacro, macro)
47
        gameMenu.AppendItem(macro)
48

    
49
        menubar.Append(fileMenu, t("ui.main.menu.file"))
50
        menubar.Append(gameMenu, t("ui.main.menu.game"))
51

    
52
        self.SetMenuBar(menubar)
53

    
54
    def InitUI(self):
55
        self.panel = MUDPanel(self, self.engine)
56
        self.SetTitle("CocoMUD client")
57
        self.Maximize()
58
        self.Show()
59

    
60
    def OnPreferences(self, e):
61
        """Open the preferences dialog box."""
62
        dialog = PreferencesDialog(self.engine)
63
        dialog.ShowModal()
64
        dialog.Destroy()
65

    
66
    def OnMacro(self, e):
67
        """Open the macro dialog box."""
68
        dialog = MacroDialog(self.engine)
69
        dialog.ShowModal()
70
        dialog.Destroy()
71

    
72
    def OnQuit(self, e):
73
        self.Close()
74

    
75
    # Methods to handle client's events
76
    def handle_message(self, message):
77
        """The client has just received a message."""
78
        pos = self.panel.output.GetInsertionPoint()
79
        self.panel.output.write(message)
80
        self.panel.output.SetInsertionPoint(pos)
81

    
82
    def handle_option(self, command):
83
        """Handle the specified option.
84

85
        The command is a string representing the received option.
86
        The following options are supported:
87
            "hide":  the input should be hidden
88
            "show":  the input should be shown
89

90
        """
91
        if command == "hide":
92
            evt = FocusEvent(myEVT_FOCUS, -1, "password")
93
            wx.PostEvent(self.panel, evt)
94
        elif command == "show":
95
            evt = FocusEvent(myEVT_FOCUS, -1, "input")
96
            wx.PostEvent(self.panel, evt)
97

    
98
class MUDPanel(wx.Panel):
99

    
100
    def __init__(self, parent, engine):
101
        wx.Panel.__init__(self, parent)
102
        self.engine = engine
103
        self.client = None
104

    
105
        mainSizer = wx.GridBagSizer(5, 5)
106

    
107
        # Input
108
        l_input = wx.StaticText(self, -1, "Input")
109
        t_input = wx.TextCtrl(self, -1, "", size=(125, -1),
110
                style=wx.TE_PROCESS_ENTER)
111
        self.input = t_input
112

    
113
        # Password
114
        l_password = wx.StaticText(self, -1, "Password")
115
        t_password = wx.TextCtrl(self, -1, "", size=(20, -1),
116
                style=wx.TE_PROCESS_ENTER | wx.TE_PASSWORD)
117
        self.password = t_password
118
        t_password.Hide()
119

    
120
        # Add the input field in the sizer
121
        mainSizer.Add(l_input, pos=(0, 0))
122
        mainSizer.Add(t_input, pos=(0, 1), span=(1, 6))
123
        mainSizer.Add(l_password, pos=(0, 7))
124
        mainSizer.Add(t_password, pos=(0, 8), span=(1, 2))
125

    
126
        # Ouput
127
        l_output = wx.StaticText(self, -1, "Output")
128
        t_output = wx.TextCtrl(self, -1, "",
129
                size=(600, 600), style=wx.TE_MULTILINE|wx.TE_READONLY)
130
        self.output = t_output
131

    
132
        # Add the output fields in the sizer
133
        mainSizer.Add(l_output, pos=(1, 0))
134
        mainSizer.Add(t_output, pos=(10, 10))
135

    
136
        # Event handler
137
        t_input.Bind(wx.EVT_TEXT_ENTER, self.EvtText)
138
        t_password.Bind(wx.EVT_TEXT_ENTER, self.EvtText)
139
        self.Bind(EVT_FOCUS, self.OnFocus)
140
        t_input.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
141
        t_password.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
142
        t_output.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
143

    
144
    def EvtText(self, event):
145
        """One of the input fields is sending text."""
146
        self.input.Clear()
147
        self.password.Clear()
148
        msg = event.GetString().encode("utf-8", "replace")
149
        self.client.write(msg + "\r\n")
150

    
151
    def OnFocus(self, evt):
152
        val = evt.GetValue()
153
        if val == "input":
154
            self.input.Show()
155
            self.input.SetFocus()
156
            self.password.Hide()
157
        elif val == "password":
158
            self.password.Show()
159
            self.password.SetFocus()
160
            self.input.Hide()
161

    
162
    def OnKeyDown(self, e):
163
        """A key is pressed in the window."""
164
        modifiers = e.GetModifiers()
165
        key = e.GetUnicodeKey()
166
        if not key:
167
            key = e.GetKeyCode()
168

    
169
        # Look for matching macros
170
        for code, macro in self.engine.macros.items():
171
            if code == (key, modifiers):
172
                macro.execute(self.engine, self.client)
173

    
174
        e.Skip()
(3-3/3)