comparison engine/extensions/pychan/manager.py @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 9a1529f9625e
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 # coding: utf-8
2
3 import fife
4 import widgets
5 import fonts
6 from exceptions import *
7
8 class Manager(fife.IWidgetListener):
9 manager = None
10
11 def __init__(self, engine, debug = False):
12 super(Manager,self).__init__()
13 self.engine = engine
14 self.debug = debug
15
16 if not self.engine.getEventManager():
17 raise InitializationError("No event manager installed.")
18 if not self.engine.getGuiManager():
19 raise InitializationError("No GUI manager installed.")
20
21 self.guimanager = engine.getGuiManager()
22 self.fonts = {}
23 #glyphs = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/:();%`\'*#=[]"'
24 self.fonts['default'] = self.engine.getDefaultFont()
25
26 self.styles = {}
27 self.addStyle('default',DEFAULT_STYLE)
28
29 self.widgetEvents = {}
30 self.engine.getEventManager().addWidgetListener(self)
31 Manager.manager = self
32
33 self.mainLoop = None
34 self.breakFromMainLoop = None
35 self.can_execute = True
36
37 def setupModalExecution(self,mainLoop,breakFromMainLoop):
38 """
39 Setup synchronous execution of dialogs.
40 """
41 self.mainLoop = mainLoop
42 self.breakFromMainLoop = breakFromMainLoop
43 self.can_execute = True
44
45 def show(self,widget):
46 """
47 Shows a widget on screen. Used by L{Widget.show} - do not use directly.
48 """
49 if widget.position_technique == "automatic":
50 w,h = self.engine.getSettings().getScreenWidth(), self.engine.getSettings().getScreenHeight()
51 widget.position = (w-widget.width)/2,(h-widget.height)/2
52 self.guimanager.add( widget.real_widget )
53
54 def hide(self,widget):
55 """
56 Hides a widget again. Used by L{Widget.hide} - do not use directly.
57 """
58 self.guimanager.remove( widget.real_widget )
59
60 def getFont(self,name):
61 """
62 Returns a GuiFont identified by its name.
63
64 @param name: A string identifier from the font definitions in pychans config files.
65 """
66 font = self.fonts.get(name)
67
68 # For the default font directly return it,
69 # otherwise the GuiFont is in the font attribute.
70 return getattr(font,"font",font)
71
72 def addFont(self,font):
73 """
74 Add a font to the font registry. It's not necessary to call this directly.
75 But it expects a L{Font} instance and throws an L{InitializationError}
76 otherwise.
77
78 @param font: A L{Font} instance.
79 """
80 if not isinstance(font,fonts.Font):
81 raise InitializationError("PyChan Manager expected a fonts.Font instance, not %s." % repr(font))
82 self.fonts[font.name] = font
83
84 def addStyle(self,name,style):
85 style = self._remapStyleKeys(style)
86
87 for k,v in self.styles.get('default',{}).items():
88 style[k] = style.get(k,v)
89 self.styles[name] = style
90
91 def stylize(self,widget, style, **kwargs):
92 style = self.styles[style]
93 for k,v in style.get('default',{}).items():
94 v = kwargs.get(k,v)
95 setattr(widget,k,v)
96
97 cls = widget.__class__
98 for applicable,specstyle in style.items():
99 if not isinstance(applicable,tuple):
100 applicable = (applicable,)
101 if cls in applicable:
102 for k,v in specstyle.items():
103 v = kwargs.get(k,v)
104 setattr(widget,k,v)
105
106 def _remapStyleKeys(self,style):
107 # Remap class names, create copy:
108 def _toClass(class_):
109 if class_ == "default":
110 return class_
111
112 if type(class_) == type(widgets.Widget) and issubclass(class_,widgets.Widget):
113 return class_
114 if not widgets.WIDGETS.has_key(str(class_)):
115 raise InitializationError("Can't resolve %s to a widget class." % repr(class_))
116 return widgets.WIDGETS[str(class_)]
117
118 style_copy = {}
119 for k,v in style.items():
120 if isinstance(k,tuple):
121 new_k = tuple(map(_toClass,k))
122 else:
123 new_k = _toClass(k)
124 style_copy[new_k] = v
125 return style_copy
126
127 def loadImage(self,filename):
128 return fife.GuiImage(self.engine.imagePool.addResourceFromFile(filename),self.engine.imagePool)
129
130 def defaultWidgetAction(self,event):
131 if self.debug:
132 print "Event(%s) received." % event.getId()
133
134 def onWidgetAction(self, event):
135 #print event.getId(),self.widgetEvents
136 handler = self.widgetEvents.get( event.getId(), self.defaultWidgetAction )
137 handler( event )
138
139 # Default Widget style.
140
141 DEFAULT_STYLE = {
142 'default' : {
143 'border_size': 1,
144 'margins': (0,0),
145 'base_color' : fife.Color(0,0,100),
146 'foreground_color' : fife.Color(255,255,255),
147 'background_color' : fife.Color(0,0,0),
148 },
149 'Button' : {
150 'border_size': 0,
151 'margins' : (10,5),
152 },
153 ('CheckBox','RadioButton') : {
154 'border_size': 0,
155 },
156 'TextBox' : {
157 'border_size' : 0,
158 },
159 'Label' : {
160 'border_size': 0,
161 },
162 'ListBox' : {
163 'border_size': 0,
164 },
165 'Window' : {
166 'border_size': 1,
167 'margins': (5,5),
168 },
169 ('Container','HBox','VBox') : {
170 'border_size': 0,
171 'opaque' : False
172 },
173 'Icon' : {
174 'border_size' : 0
175 }
176 }