annotate frontends/sortilege/window.py @ 62:93cb45a7420f

SàT multi-profile: connection using profiles - /!\ plugins are temporarly deactivated
author Goffi <goffi@goffi.org>
date Fri, 29 Jan 2010 13:55:41 +1100
parents a5b5fb5fc9fd
children 8f2ed279784b
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
57
a5b5fb5fc9fd updated README and copyright note
Goffi <goffi@goffi.org>
parents: 0
diff changeset
6 Copyright (C) 2009, 2010 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 def echo(message):
goffi@necton2
parents:
diff changeset
29 return
goffi@necton2
parents:
diff changeset
30 os.system('echo "'+str(message)+'" >> /tmp/toto')
goffi@necton2
parents:
diff changeset
31
goffi@necton2
parents:
diff changeset
32 class Window():
goffi@necton2
parents:
diff changeset
33 def __init__(self, parent, height, width, y, x, border=False, title="", code="utf-8"):
goffi@necton2
parents:
diff changeset
34 self.__border=border
goffi@necton2
parents:
diff changeset
35 self.__title=title
goffi@necton2
parents:
diff changeset
36 self.__active=False
goffi@necton2
parents:
diff changeset
37 self.__parent=parent
goffi@necton2
parents:
diff changeset
38 self.__code=code
goffi@necton2
parents:
diff changeset
39 self.__hide=False
goffi@necton2
parents:
diff changeset
40
goffi@necton2
parents:
diff changeset
41 self.resize(height, width, y, x)
goffi@necton2
parents:
diff changeset
42 self.oriCoords=self.__coords #FIXME: tres moche, a faire en mieux
goffi@necton2
parents:
diff changeset
43
goffi@necton2
parents:
diff changeset
44 def hide(self, hide=True):
goffi@necton2
parents:
diff changeset
45 self.__hide=hide
goffi@necton2
parents:
diff changeset
46
goffi@necton2
parents:
diff changeset
47 def show(self):
goffi@necton2
parents:
diff changeset
48 self.__hide=False
goffi@necton2
parents:
diff changeset
49
goffi@necton2
parents:
diff changeset
50 def isHidden(self):
goffi@necton2
parents:
diff changeset
51 return self.__hide
goffi@necton2
parents:
diff changeset
52
goffi@necton2
parents:
diff changeset
53 def getY(self):
goffi@necton2
parents:
diff changeset
54 return self.__coords[2]
goffi@necton2
parents:
diff changeset
55
goffi@necton2
parents:
diff changeset
56 def getX(self):
goffi@necton2
parents:
diff changeset
57 return self.__coords[3]
goffi@necton2
parents:
diff changeset
58
goffi@necton2
parents:
diff changeset
59 def getHeight(self):
goffi@necton2
parents:
diff changeset
60 return self.__coords[0]
goffi@necton2
parents:
diff changeset
61
goffi@necton2
parents:
diff changeset
62 def getWidth(self):
goffi@necton2
parents:
diff changeset
63 return self.__coords[1]
goffi@necton2
parents:
diff changeset
64
goffi@necton2
parents:
diff changeset
65
goffi@necton2
parents:
diff changeset
66 #FIXME: tres moche, a faire en plus joli
goffi@necton2
parents:
diff changeset
67 def getOriY(self):
goffi@necton2
parents:
diff changeset
68 return self.oriCoords[2]
goffi@necton2
parents:
diff changeset
69
goffi@necton2
parents:
diff changeset
70 def getOriX(self):
goffi@necton2
parents:
diff changeset
71 return self.oriCoords[3]
goffi@necton2
parents:
diff changeset
72
goffi@necton2
parents:
diff changeset
73 def getOriHeight(self):
goffi@necton2
parents:
diff changeset
74 return self.oriCoords[0]
goffi@necton2
parents:
diff changeset
75
goffi@necton2
parents:
diff changeset
76 def getOriWidth(self):
goffi@necton2
parents:
diff changeset
77 return self.oriCoords[1]
goffi@necton2
parents:
diff changeset
78
goffi@necton2
parents:
diff changeset
79 def defInsideCoord(self):
goffi@necton2
parents:
diff changeset
80 """define the inside coordinates (win coordinates minus decorations)"""
goffi@necton2
parents:
diff changeset
81 height,width,y,x=self.__coords
goffi@necton2
parents:
diff changeset
82 self.oriX = x if not self.__border else x+1
goffi@necton2
parents:
diff changeset
83 self.oriY = y if not self.__border else y+1
goffi@necton2
parents:
diff changeset
84 self.endX = x+width if not self.__border else x+width-2
goffi@necton2
parents:
diff changeset
85 self.endY = y+height if not self.__border else y+height-2
goffi@necton2
parents:
diff changeset
86 self.rWidth = width if not self.__border else width-2
goffi@necton2
parents:
diff changeset
87 self.rHeight = height if not self.__border else height-2
goffi@necton2
parents:
diff changeset
88
goffi@necton2
parents:
diff changeset
89 def resize(self, height, width, y, x):
goffi@necton2
parents:
diff changeset
90 self.__coords=[height, width, y, x]
goffi@necton2
parents:
diff changeset
91
goffi@necton2
parents:
diff changeset
92 # we check that coordinates are under limits
goffi@necton2
parents:
diff changeset
93 self.__coordAdjust(self.__coords)
goffi@necton2
parents:
diff changeset
94 height,width,y,x=self.__coords
goffi@necton2
parents:
diff changeset
95
goffi@necton2
parents:
diff changeset
96 echo ("newwin %d %d %d %d" % (height, width, y, x))
goffi@necton2
parents:
diff changeset
97 self.window = self.__parent.subwin(height, width, y, x)
goffi@necton2
parents:
diff changeset
98 self.defInsideCoord()
goffi@necton2
parents:
diff changeset
99
goffi@necton2
parents:
diff changeset
100 def __coordAdjust(self, coords):
goffi@necton2
parents:
diff changeset
101 """Check that coordinates are under limits, adjust them else otherwise"""
goffi@necton2
parents:
diff changeset
102 height,width,y,x=coords
goffi@necton2
parents:
diff changeset
103 parentY, parentX = self.__parent.getbegyx()
goffi@necton2
parents:
diff changeset
104 parentHeight, parentWidth = self.__parent.getmaxyx()
goffi@necton2
parents:
diff changeset
105
goffi@necton2
parents:
diff changeset
106 if y < parentY:
goffi@necton2
parents:
diff changeset
107 y = parentY
goffi@necton2
parents:
diff changeset
108 if x < parentX:
goffi@necton2
parents:
diff changeset
109 x = parentX
goffi@necton2
parents:
diff changeset
110 if height > parentHeight - y:
goffi@necton2
parents:
diff changeset
111 height = parentHeight - y
goffi@necton2
parents:
diff changeset
112 if width > parentWidth - x:
goffi@necton2
parents:
diff changeset
113 width = parentWidth - x
goffi@necton2
parents:
diff changeset
114 coords[0], coords[1], coords[2], coords[3] = [height, width, y, x]
goffi@necton2
parents:
diff changeset
115
goffi@necton2
parents:
diff changeset
116
goffi@necton2
parents:
diff changeset
117 def activate(self,state=True):
goffi@necton2
parents:
diff changeset
118 """Declare this window as current active one"""
goffi@necton2
parents:
diff changeset
119 self.__active=state
goffi@necton2
parents:
diff changeset
120 self.update()
goffi@necton2
parents:
diff changeset
121
goffi@necton2
parents:
diff changeset
122 def isActive(self):
goffi@necton2
parents:
diff changeset
123 return self.__active
goffi@necton2
parents:
diff changeset
124
goffi@necton2
parents:
diff changeset
125 def addYXStr(self, y, x, text, attr = 0, limit=0):
goffi@necton2
parents:
diff changeset
126 if self.__border:
goffi@necton2
parents:
diff changeset
127 x=x+1
goffi@necton2
parents:
diff changeset
128 y=y+1
goffi@necton2
parents:
diff changeset
129 n = self.rWidth-x if not limit else limit
goffi@necton2
parents:
diff changeset
130 encoded = text.encode(self.__code)
goffi@necton2
parents:
diff changeset
131 adjust = len(encoded) - len(text) # hack because addnstr doesn't manage unicode
goffi@necton2
parents:
diff changeset
132 try:
goffi@necton2
parents:
diff changeset
133 self.window.addnstr(y, x, encoded, n + adjust, attr)
goffi@necton2
parents:
diff changeset
134 except:
goffi@necton2
parents:
diff changeset
135 #We have to catch error to write on last line last col FIXME: is there a better way ?
goffi@necton2
parents:
diff changeset
136 pass
goffi@necton2
parents:
diff changeset
137
goffi@necton2
parents:
diff changeset
138 def move(self, y, x):
goffi@necton2
parents:
diff changeset
139 self.window.move(y,x)
goffi@necton2
parents:
diff changeset
140
goffi@necton2
parents:
diff changeset
141 def noutrefresh(self):
goffi@necton2
parents:
diff changeset
142 self.window.noutrefresh()
goffi@necton2
parents:
diff changeset
143
goffi@necton2
parents:
diff changeset
144 def update(self):
goffi@necton2
parents:
diff changeset
145 """redraw all the window"""
goffi@necton2
parents:
diff changeset
146 if self.__hide:
goffi@necton2
parents:
diff changeset
147 return
goffi@necton2
parents:
diff changeset
148 self.clear()
goffi@necton2
parents:
diff changeset
149
goffi@necton2
parents:
diff changeset
150 def border(self):
goffi@necton2
parents:
diff changeset
151 """redraw the border and title"""
goffi@necton2
parents:
diff changeset
152 y,x = self.window.getbegyx()
goffi@necton2
parents:
diff changeset
153 width = self.window.getmaxyx()[1]
goffi@necton2
parents:
diff changeset
154 if self.__border:
goffi@necton2
parents:
diff changeset
155 self.window.border()
goffi@necton2
parents:
diff changeset
156 if self.__title:
goffi@necton2
parents:
diff changeset
157 if len(self.__title)>width:
goffi@necton2
parents:
diff changeset
158 self.__title=""
goffi@necton2
parents:
diff changeset
159 else:
goffi@necton2
parents:
diff changeset
160 self.window.addstr(y,x+(width-len(self.__title))/2, self.__title)
goffi@necton2
parents:
diff changeset
161
goffi@necton2
parents:
diff changeset
162 def clear(self):
goffi@necton2
parents:
diff changeset
163 self.window.clear()
goffi@necton2
parents:
diff changeset
164 self.border()