1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 from compat import guichan, in_fife
25 import widgets
26 from fife.extensions import fife_timer as timer
27 import fonts
28 from exceptions import *
29 from traceback import print_exc
30
32 """
33 Get the manager from inside pychan.
34
35 To avoid cyclic imports write::
36 from internal import get_manager
37 """
38 return Manager.manager
39
42
45
47 manager = None
48
49 - def __init__(self, hook, debug = False, compat_layout = False):
83
91
92 - def show(self,widget):
93 """
94 Shows a widget on screen. Used by L{Widget.show} - do not use directly.
95 """
96 self.placeWidget(widget, widget.position_technique)
97 assert widget not in self.allWidgets
98 self.allWidgets[ widget ] = 1
99 self.hook.add_widget( widget.real_widget )
100
101 - def hide(self,widget):
102 """
103 Hides a widget again. Used by L{Widget.hide} - do not use directly.
104
105 @todo: delete calls occur even if the widget is not in self.allWidgets
106 we can't rely on a perfect show/hide/show/hide chain here (which we do!)
107
108 this method is called way too frequent and unmanaged as that we can
109 ever hope to achieve a strict show/hide/show chain
110
111 weak ref should only be deleted if there is one, because
112 hiding/showing a widget is a different process then updating
113 the self.allWidgets datastructure
114
115 the assert check will trigger on a frequent basis, thus
116 I added it but uncommented it for further notice
117 """
118 self.hook.remove_widget( widget.real_widget )
119
120
121
122
123 if widget not in self.allWidgets:
124 return
125 del self.allWidgets[ widget ]
126
128 """
129 Gets a reference to the console
130 """
131 return self.hook.console
132
134 """
135 Returns the default font
136 """
137 return self.fonts['default']
138
141
143 """
144 B{pending deprecation}
145
146 Returns a GuiFont identified by its name.
147
148 @param name: A string identifier from the font definitions in pychans config files.
149 """
150 if in_fife:
151 font = self.fonts.get(name)
152 if isinstance(font,guichan.GuiFont):
153 return font
154 if hasattr(font,"font") and isinstance(getattr(font,"font"),guichan.GuiFont):
155 return font.font
156 raise InitializationError("Couldn't find the font '%s' - did you forget loading a .fontdef?" % str(name))
157 else:
158 return self.hook.get_font(name)
159
160 - def createFont(self, path="", size=0, glyphs=""):
161 """
162 Creates and returns a GuiFont from the GUI Manager
163 """
164 return self.hook.create_font(path,size,glyphs)
165
167 """
168 Releases a font from memory. Expects a guichan.GuiFont.
169
170 @todo This needs to be tested. Also should add a way to release
171 a font by name (fonts.Font).
172 """
173 if not isinstance(font,guichan.GuiFont):
174 raise InitializationError("PyChan Manager expected a guichan.GuiFont instance, not %s." % repr(font))
175 self.hook.release_font(font)
176
178 """
179 B{deprecated}
180
181 Add a font to the font registry. It's not necessary to call this directly.
182 But it expects a L{Font} instance and throws an L{InitializationError}
183 otherwise.
184
185 @param font: A L{Font} instance.
186 """
187 if not isinstance(font,fonts.Font):
188 raise InitializationError("PyChan Manager expected a fonts.Font instance, not %s." % repr(font))
189 self.fonts[font.name] = font
190
197
198 - def stylize(self,widget, style, **kwargs):
199 style = self.styles[style]
200 for k,v in style.get('default',{}).items():
201 v = kwargs.get(k,v)
202 setattr(widget,k,v)
203
204 cls = widget.__class__
205 for applicable,specstyle in style.items():
206 if not isinstance(applicable,tuple):
207 applicable = (applicable,)
208 if cls in applicable:
209 for k,v in specstyle.items():
210 v = kwargs.get(k,v)
211 setattr(widget,k,v)
212
214 """
215 Translate style selectors to tuples of widget classes. (internal)
216 """
217
218 def _toClass(class_):
219 if class_ == "default":
220 return class_
221
222 if type(class_) == type(widgets.Widget) and issubclass(class_,widgets.Widget):
223 return class_
224 if not widgets.WIDGETS.has_key(str(class_)):
225 raise InitializationError("Can't resolve %s to a widget class." % repr(class_))
226 return widgets.WIDGETS[str(class_)]
227
228 style_copy = {}
229 for k,v in style.items():
230 if isinstance(k,tuple):
231 new_k = tuple(map(_toClass,k))
232 else:
233 new_k = _toClass(k)
234 style_copy[new_k] = v
235 return style_copy
236
241
242
243
244 DEFAULT_STYLE = {
245 'default' : {
246 'border_size': 0,
247 'margins': (0,0),
248 'base_color' : guichan.Color(28,28,28),
249 'foreground_color' : guichan.Color(255,255,255),
250 'background_color' : guichan.Color(50,50,50),
251 'selection_color' : guichan.Color(80,80,80),
252 'font' : 'default'
253 },
254 'Button' : {
255 'border_size': 2,
256 'margins' : (5,2),
257 'min_size' : (15,10),
258 },
259 'CheckBox' : {
260 'border_size': 0,
261 },
262 'RadioButton' : {
263 'border_size': 0,
264 'background_color' : guichan.Color(0,0,0),
265 },
266 'Label' : {
267 'border_size': 0,
268 'background_color' : guichan.Color(50,50,50,0)
269 },
270 'ListBox' : {
271 'border_size': 0,
272 },
273 'Window' : {
274 'border_size': 0,
275 'margins': (5,5),
276 'opaque' : 1,
277 'padding':2,
278 'titlebar_height' : 12,
279 'background_image' : None,
280 },
281 'TextBox' : {
282 },
283 ('Container','HBox','VBox') : {
284 'border_size': 0,
285 'margins': (0,0),
286 'padding':2,
287 'opaque' : 1,
288 'background_image' : None,
289 }
290 }
291