annotate frontends/sortilege_old/window.py @ 325:d62eb9003375

primitivus: removed forgotten debug stuff
author Goffi <goffi@goffi.org>
date Wed, 11 May 2011 19:49:55 +0200
parents b1794cbb88e5
children 2a072735e459
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 sortilege: a SAT frontend
228
b1794cbb88e5 2011 copyright upgrade
Goffi <goffi@goffi.org>
parents: 112
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22
goffi@necton2
parents:
diff changeset
23 import curses
goffi@necton2
parents:
diff changeset
24 import os
goffi@necton2
parents:
diff changeset
25 import pdb
goffi@necton2
parents:
diff changeset
26
goffi@necton2
parents:
diff changeset
27
goffi@necton2
parents:
diff changeset
28 class Window():
goffi@necton2
parents:
diff changeset
29 def __init__(self, parent, height, width, y, x, border=False, title="", code="utf-8"):
goffi@necton2
parents:
diff changeset
30 self.__border=border
goffi@necton2
parents:
diff changeset
31 self.__title=title
goffi@necton2
parents:
diff changeset
32 self.__active=False
goffi@necton2
parents:
diff changeset
33 self.__parent=parent
goffi@necton2
parents:
diff changeset
34 self.__code=code
goffi@necton2
parents:
diff changeset
35 self.__hide=False
goffi@necton2
parents:
diff changeset
36
goffi@necton2
parents:
diff changeset
37 self.resize(height, width, y, x)
goffi@necton2
parents:
diff changeset
38 self.oriCoords=self.__coords #FIXME: tres moche, a faire en mieux
goffi@necton2
parents:
diff changeset
39
goffi@necton2
parents:
diff changeset
40 def hide(self, hide=True):
goffi@necton2
parents:
diff changeset
41 self.__hide=hide
goffi@necton2
parents:
diff changeset
42
goffi@necton2
parents:
diff changeset
43 def show(self):
goffi@necton2
parents:
diff changeset
44 self.__hide=False
goffi@necton2
parents:
diff changeset
45
goffi@necton2
parents:
diff changeset
46 def isHidden(self):
goffi@necton2
parents:
diff changeset
47 return self.__hide
goffi@necton2
parents:
diff changeset
48
goffi@necton2
parents:
diff changeset
49 def getY(self):
goffi@necton2
parents:
diff changeset
50 return self.__coords[2]
goffi@necton2
parents:
diff changeset
51
goffi@necton2
parents:
diff changeset
52 def getX(self):
goffi@necton2
parents:
diff changeset
53 return self.__coords[3]
goffi@necton2
parents:
diff changeset
54
goffi@necton2
parents:
diff changeset
55 def getHeight(self):
goffi@necton2
parents:
diff changeset
56 return self.__coords[0]
goffi@necton2
parents:
diff changeset
57
goffi@necton2
parents:
diff changeset
58 def getWidth(self):
goffi@necton2
parents:
diff changeset
59 return self.__coords[1]
goffi@necton2
parents:
diff changeset
60
goffi@necton2
parents:
diff changeset
61
goffi@necton2
parents:
diff changeset
62 #FIXME: tres moche, a faire en plus joli
goffi@necton2
parents:
diff changeset
63 def getOriY(self):
goffi@necton2
parents:
diff changeset
64 return self.oriCoords[2]
goffi@necton2
parents:
diff changeset
65
goffi@necton2
parents:
diff changeset
66 def getOriX(self):
goffi@necton2
parents:
diff changeset
67 return self.oriCoords[3]
goffi@necton2
parents:
diff changeset
68
goffi@necton2
parents:
diff changeset
69 def getOriHeight(self):
goffi@necton2
parents:
diff changeset
70 return self.oriCoords[0]
goffi@necton2
parents:
diff changeset
71
goffi@necton2
parents:
diff changeset
72 def getOriWidth(self):
goffi@necton2
parents:
diff changeset
73 return self.oriCoords[1]
goffi@necton2
parents:
diff changeset
74
goffi@necton2
parents:
diff changeset
75 def defInsideCoord(self):
goffi@necton2
parents:
diff changeset
76 """define the inside coordinates (win coordinates minus decorations)"""
goffi@necton2
parents:
diff changeset
77 height,width,y,x=self.__coords
goffi@necton2
parents:
diff changeset
78 self.oriX = x if not self.__border else x+1
goffi@necton2
parents:
diff changeset
79 self.oriY = y if not self.__border else y+1
goffi@necton2
parents:
diff changeset
80 self.endX = x+width if not self.__border else x+width-2
goffi@necton2
parents:
diff changeset
81 self.endY = y+height if not self.__border else y+height-2
goffi@necton2
parents:
diff changeset
82 self.rWidth = width if not self.__border else width-2
goffi@necton2
parents:
diff changeset
83 self.rHeight = height if not self.__border else height-2
goffi@necton2
parents:
diff changeset
84
goffi@necton2
parents:
diff changeset
85 def resize(self, height, width, y, x):
goffi@necton2
parents:
diff changeset
86 self.__coords=[height, width, y, x]
goffi@necton2
parents:
diff changeset
87
goffi@necton2
parents:
diff changeset
88 # we check that coordinates are under limits
goffi@necton2
parents:
diff changeset
89 self.__coordAdjust(self.__coords)
goffi@necton2
parents:
diff changeset
90 height,width,y,x=self.__coords
goffi@necton2
parents:
diff changeset
91
goffi@necton2
parents:
diff changeset
92 self.window = self.__parent.subwin(height, width, y, x)
goffi@necton2
parents:
diff changeset
93 self.defInsideCoord()
goffi@necton2
parents:
diff changeset
94
goffi@necton2
parents:
diff changeset
95 def __coordAdjust(self, coords):
goffi@necton2
parents:
diff changeset
96 """Check that coordinates are under limits, adjust them else otherwise"""
goffi@necton2
parents:
diff changeset
97 height,width,y,x=coords
goffi@necton2
parents:
diff changeset
98 parentY, parentX = self.__parent.getbegyx()
goffi@necton2
parents:
diff changeset
99 parentHeight, parentWidth = self.__parent.getmaxyx()
goffi@necton2
parents:
diff changeset
100
goffi@necton2
parents:
diff changeset
101 if y < parentY:
goffi@necton2
parents:
diff changeset
102 y = parentY
goffi@necton2
parents:
diff changeset
103 if x < parentX:
goffi@necton2
parents:
diff changeset
104 x = parentX
goffi@necton2
parents:
diff changeset
105 if height > parentHeight - y:
goffi@necton2
parents:
diff changeset
106 height = parentHeight - y
goffi@necton2
parents:
diff changeset
107 if width > parentWidth - x:
goffi@necton2
parents:
diff changeset
108 width = parentWidth - x
goffi@necton2
parents:
diff changeset
109 coords[0], coords[1], coords[2], coords[3] = [height, width, y, x]
goffi@necton2
parents:
diff changeset
110
goffi@necton2
parents:
diff changeset
111
goffi@necton2
parents:
diff changeset
112 def activate(self,state=True):
goffi@necton2
parents:
diff changeset
113 """Declare this window as current active one"""
goffi@necton2
parents:
diff changeset
114 self.__active=state
goffi@necton2
parents:
diff changeset
115 self.update()
goffi@necton2
parents:
diff changeset
116
goffi@necton2
parents:
diff changeset
117 def isActive(self):
goffi@necton2
parents:
diff changeset
118 return self.__active
goffi@necton2
parents:
diff changeset
119
goffi@necton2
parents:
diff changeset
120 def addYXStr(self, y, x, text, attr = 0, limit=0):
goffi@necton2
parents:
diff changeset
121 if self.__border:
goffi@necton2
parents:
diff changeset
122 x=x+1
goffi@necton2
parents:
diff changeset
123 y=y+1
goffi@necton2
parents:
diff changeset
124 n = self.rWidth-x if not limit else limit
goffi@necton2
parents:
diff changeset
125 encoded = text.encode(self.__code)
goffi@necton2
parents:
diff changeset
126 adjust = len(encoded) - len(text) # hack because addnstr doesn't manage unicode
goffi@necton2
parents:
diff changeset
127 try:
goffi@necton2
parents:
diff changeset
128 self.window.addnstr(y, x, encoded, n + adjust, attr)
goffi@necton2
parents:
diff changeset
129 except:
goffi@necton2
parents:
diff changeset
130 #We have to catch error to write on last line last col FIXME: is there a better way ?
goffi@necton2
parents:
diff changeset
131 pass
goffi@necton2
parents:
diff changeset
132
goffi@necton2
parents:
diff changeset
133 def move(self, y, x):
goffi@necton2
parents:
diff changeset
134 self.window.move(y,x)
goffi@necton2
parents:
diff changeset
135
goffi@necton2
parents:
diff changeset
136 def noutrefresh(self):
goffi@necton2
parents:
diff changeset
137 self.window.noutrefresh()
goffi@necton2
parents:
diff changeset
138
goffi@necton2
parents:
diff changeset
139 def update(self):
goffi@necton2
parents:
diff changeset
140 """redraw all the window"""
goffi@necton2
parents:
diff changeset
141 if self.__hide:
goffi@necton2
parents:
diff changeset
142 return
goffi@necton2
parents:
diff changeset
143 self.clear()
goffi@necton2
parents:
diff changeset
144
goffi@necton2
parents:
diff changeset
145 def border(self):
goffi@necton2
parents:
diff changeset
146 """redraw the border and title"""
goffi@necton2
parents:
diff changeset
147 y,x = self.window.getbegyx()
goffi@necton2
parents:
diff changeset
148 width = self.window.getmaxyx()[1]
goffi@necton2
parents:
diff changeset
149 if self.__border:
goffi@necton2
parents:
diff changeset
150 self.window.border()
goffi@necton2
parents:
diff changeset
151 if self.__title:
goffi@necton2
parents:
diff changeset
152 if len(self.__title)>width:
goffi@necton2
parents:
diff changeset
153 self.__title=""
goffi@necton2
parents:
diff changeset
154 else:
goffi@necton2
parents:
diff changeset
155 self.window.addstr(y,x+(width-len(self.__title))/2, self.__title)
goffi@necton2
parents:
diff changeset
156
goffi@necton2
parents:
diff changeset
157 def clear(self):
goffi@necton2
parents:
diff changeset
158 self.window.clear()
goffi@necton2
parents:
diff changeset
159 self.border()