Issues (229)

client/pygameui/Window.py (1 issue)

Severity
1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Pygame.UI.
5
#
6
#  Pygame.UI is free software; you can redistribute it and/or modify
7
#  it under the terms of the Lesser GNU General Public License as published by
8
#  the Free Software Foundation; either version 2.1 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Pygame.UI is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  Lesser GNU General Public License for more details.
15
#
16
#  You should have received a copy of the Lesser GNU General Public License
17
#  along with Pygame.UI; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
import pygame
21
22
import Const
23
from Widget import widgets, registerWidget
24
from MetaWidget import MetaWidget
25
from Button import Button
26
27
class Window(MetaWidget):
28
29
    def __init__(self, parent, **kwargs):
30
        MetaWidget.__init__(self, parent)
31
        self.surface = None
32
        self.visible = 0
33
        # data
34
        self.title = None
35
        self.closeButton = 1
36
        # flags
37
        self.looseFocusClose = 0
38
        self.alwaysInBackground = 0
39
        self.alwaysOnTop = 0
40
        self.modal = 0
41
        self.rightButtonClose = 0
42
        self.escKeyClose = 0
43
        self.decorated = 1
44
        self.titleOnly = 0
45
        self.movable = 1
46
        self.acceptButton = None
47
        self._dragging = 0
48
        self._fullUpdate = True
49
        self.tabChange = False
50
        self.callEventHandler = False
51
        # register
52
        self.app.registerWindow(self)
53
        self.processKWArguments(kwargs)
54
55
    def show(self):
56
        self.visible = 1
57
        self.toFront()
58
        if self.widgets:
59
            for widget in self.widgets:
60
                if widget.focusable:
61
                    self.app.setFocus(widget)
62
                    break
63
64
    def hide(self):
65
        self.visible = 0
66
        self.app.hideWindow(self)
67
68
    def destroy(self):
69
        if self.visible:
70
            self.hide()
71
        self.app.unregisterWindow(self)
72
73
    def toFront(self):
74
        if not self.alwaysInBackground:
75
            self._fullUpdate = True
76
            self.app.moveWindowToFront(self)
77
        else:
78
            self.app.focusWindow(self)
79
80
    def drawMetaWidget(self, surface):
81
        if self.decorated:
82
            return self.theme.drawDecoratedWindow(surface, self)
83
        else:
84
            return self.theme.drawPlainWindow(surface, self)
85
86
    def draw(self, surface):
87
        if not self.surface or self.surface.get_size() != self.rect.size:
88
            self.surface = pygame.Surface(self.rect.size, self.app.windowSurfaceFlags)
89
        changed = MetaWidget.draw(self, self.surface)
90
        if self._fullUpdate:
91
            changed = self.surface.get_rect()
92
            self._fullUpdate = False
93
        if surface and changed:
94
            surface.blit(self.surface, self.rect.topleft)
95
            changed.move_ip(self.rect.topleft)
96
        return changed
97
98
    def transpose(self, pos):
99
        if self._widgetArea == None:
100
            # TODO fix this, should be assert self._widgetArea
101
            return (0, 0)
102
        return (pos[0] - self._widgetArea.left - self.rect.left,
103
                pos[1] - self._widgetArea.top - self.rect.top)
104
105
    def processMB1Down(self, evt):
106
        pos = (
107
            evt.pos[0] - self.rect.left,
108
            evt.pos[1] - self.rect.top
109
        )
110
        if self._widgetArea and not self._widgetArea.collidepoint(pos) and self.movable:
111
            self._dragging = 1
112
            return Const.NoEvent
113
        else:
114
            return MetaWidget.processMB1Down(self, evt)
115
116
    def processMB1Up(self, evt):
117
        self._dragging = 0
118
        return MetaWidget.processMB1Up(self, evt)
119
120
    def processMB1UpMissed(self, evt):
121
        self._dragging = 0
122
        return MetaWidget.processMB1UpMissed(self, evt)
123
124
    def processMB3Down(self, evt):
125
        if self.rightButtonClose:
126
            self.hide()
127
            return Const.NoEvent
128
        return MetaWidget.processMB3Down(self, evt)
129
130
    def processMMotion(self, evt):
131
        if self._dragging and evt.buttons[0]:
132
            self.rect.left += evt.rel[0]
133
            self.rect.top += evt.rel[1]
134
            self._fullUpdate = True
135
            self.parent.redrawWidgets[self] = None
136
            return Const.NoEvent
137
        elif self._dragging and not evt.buttons[0]:
138
            self._dragging = 0
139
            return Const.NoEvent
140
        else:
141
            return MetaWidget.processMMotion(self, evt)
142
143
    def processKeyUp(self, evt):
144
        if self.escKeyClose and evt.key == pygame.K_ESCAPE:
145
            self.hide()
146
            return Const.NoEvent
147
        elif self.tabChange and evt.key == pygame.K_TAB and self.widgets:
148
            self.focusNext()
149
        elif self.acceptButton != None and evt.key == pygame.K_RETURN:
150
            if isinstance(self.acceptButton, Button) and self.acceptButton.action != None:
151
                self.processAction(self.acceptButton.action)
152
        if self.callEventHandler:
153
            self.callEventHandler.processKeyUp(evt)
154
        return evt
155
156
    def processKeyDown(self, evt):
157
        if self.callEventHandler:
158
            self.callEventHandler.processKeyDown(evt)
159
160
    def focusNext(self):
161
        for widget in self.widgets:
162
            # We need set lastFocus even when nothing is focused, exception on line 175 without this
163
            lastFocus = widget.orderNo
164
            if widget.focused:
165
                # keep the focued orderNo
166
                break
167
168
        nextFocus = None
169
        minWidget = None
170
171
        for widget in self.widgets:
172
            if widget.orderNo == 0:
173
                continue
174
175
            if minWidget == None:
176
                minWidget = widget
177
            elif widget.orderNo < minWidget.orderNo:
178
                minWidget = widget
179
180
            if nextFocus == None:
181
                if widget.orderNo > lastFocus:
0 ignored issues
show
The variable lastFocus does not seem to be defined in case the for loop on line 161 is not entered. Are you sure this can never be the case?
Loading history...
182
                    nextFocus = widget
183
            elif widget.orderNo > lastFocus and widget.orderNo < nextFocus.orderNo:
184
                nextFocus = widget
185
186
        if nextFocus:
187
            self.app.setFocus(nextFocus)
188
        elif minWidget:
189
            self.app.setFocus(minWidget)
190
191
192
registerWidget(Window, 'window')
193