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