Mercurial > fife-parpg
comparison engine/python/fife/extensions/pychan/dialogs.py @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children | e34b44afd428 |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 # #################################################################### | |
4 # Copyright (C) 2005-2009 by the FIFE team | |
5 # http://www.fifengine.de | |
6 # This file is part of FIFE. | |
7 # | |
8 # FIFE is free software; you can redistribute it and/or | |
9 # modify it under the terms of the GNU Lesser General Public | |
10 # License as published by the Free Software Foundation; either | |
11 # version 2.1 of the License, or (at your option) any later version. | |
12 # | |
13 # This library 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 GNU | |
16 # Lesser General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU Lesser General Public | |
19 # License along with this library; if not, write to the | |
20 # Free Software Foundation, Inc., | |
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 # #################################################################### | |
23 | |
24 from fife.extensions import pychan | |
25 from fife.extensions.pychan import loadXML | |
26 import fife.extensions.pychan.tools as tools | |
27 import widgets | |
28 from internal import get_manager, screen_width, screen_height | |
29 from StringIO import StringIO | |
30 | |
31 OK,YES,NO,CANCEL = True,True,False,None | |
32 | |
33 def print_event(**kwargs): | |
34 print kwargs | |
35 | |
36 class XMLDialog(object): | |
37 def __init__(self, xml, ok_field = None, cancel_field = None,initial_data={},data={}): | |
38 self.gui = loadXML(xml) | |
39 self.ok_field = ok_field | |
40 self.cancel_field = cancel_field | |
41 self.initial_data= initial_data | |
42 self.data= data | |
43 self.max_size=None | |
44 self.min_size=None | |
45 self.gui.capture(print_event,"mouseEntered") | |
46 | |
47 def execute(self): | |
48 self.gui.distributeInitialData(self.initial_data) | |
49 self.gui.distributeData(self.data) | |
50 | |
51 screen_w, screen_h = screen_width(), screen_height() | |
52 if self.max_size is None: | |
53 self.max_size = screen_w/2, screen_h/3 | |
54 if self.min_size is None: | |
55 self.min_size = screen_w/2, screen_h/4 | |
56 self.gui.max_size = self.max_size | |
57 self.gui.min_size = self.min_size | |
58 | |
59 resultMap = {} | |
60 if self.gui.findChild(name="okButton"): | |
61 resultMap["okButton"] = OK | |
62 | |
63 if self.gui.findChild(name="cancelButton"): | |
64 resultMap["cancelButton"] = CANCEL | |
65 | |
66 if self.gui.findChild(name="yesButton"): | |
67 resultMap["noButton"] = NO | |
68 | |
69 if self.gui.findChild(name="yesButton"): | |
70 resultMap["yesButton"] = YES | |
71 | |
72 ok = self.gui.execute(resultMap) | |
73 if ok: | |
74 return self.getOkResult() | |
75 return self.getCancelResult() | |
76 | |
77 def getOkResult(self): | |
78 if self.ok_field: | |
79 return self.gui.collectData(self.ok_field) | |
80 return True | |
81 | |
82 def getCancelResult(self): | |
83 if self.cancel_field: | |
84 return self.gui.collectData(self.cancel_field) | |
85 return False | |
86 | |
87 MESSAGE_BOX_XML = """\ | |
88 <Window name="window" title="Message"> | |
89 <ScrollArea> | |
90 <Label wrap_text="1" text="$MESSAGE" name="message" vexpanding="1"/> | |
91 </ScrollArea> | |
92 <HBox> | |
93 <Spacer/><Button min_size="50,0" name="okButton" text="OK"/> | |
94 </HBox> | |
95 </Window> | |
96 """ | |
97 | |
98 YESNO_BOX_XML = """\ | |
99 <Window name="window" title="Question"> | |
100 <ScrollArea> | |
101 <Label wrap_text="1" text="$MESSAGE" name="message" vexpanding="1"/> | |
102 </ScrollArea> | |
103 <HBox> | |
104 <Spacer/> | |
105 <Button min_size="50,0" name="yesButton" text="Yes"/> | |
106 <Button min_size="50,0" name="noButton" text="No"/> | |
107 </HBox> | |
108 </Window> | |
109 """ | |
110 | |
111 YESNOCANCEL_BOX_XML = """\ | |
112 <Window name="window" title="Question"> | |
113 <ScrollArea> | |
114 <Label wrap_text="1" text="$MESSAGE" name="message" vexpanding="1"/> | |
115 </ScrollArea> | |
116 <HBox> | |
117 <Spacer/> | |
118 <Button min_width="50" name="yesButton" text="Yes"/> | |
119 <Button min_width="50" name="noButton" text="No"/> | |
120 <Button min_width="50" name="cancelButton" text="Cancel"/> | |
121 </HBox> | |
122 </Window> | |
123 """ | |
124 | |
125 SELECT_BOX_XML = """\ | |
126 <Window name="window" title="Select"> | |
127 <Label wrap_text="1" text="$MESSAGE" name="message"/> | |
128 <ScrollArea> | |
129 <ListBox name="selection"> | |
130 </ListBox> | |
131 </ScrollArea> | |
132 <HBox> | |
133 <Spacer/> | |
134 <Button min_width="50" name="okButton" text="Select"/> | |
135 <Button min_width="50" name="cancelButton" text="Cancel"/> | |
136 </HBox> | |
137 </Window> | |
138 """ | |
139 | |
140 EXCEPTION_CATCHER_XML="""\ | |
141 <Window name="window" title="An exception occurred - what now?"> | |
142 <VBox hexpand="1"> | |
143 <Label wrap_text="1" max_size="400,90000" text="$MESSAGE" name="message"/> | |
144 <ScrollArea> | |
145 <Label text="$MESSAGE" name="traceback"/> | |
146 </ScrollArea> | |
147 </VBox> | |
148 <HBox> | |
149 <Spacer/> | |
150 <Button name="yesButton" text="Retry"/> | |
151 <Button name="noButton" text="Ignore"/> | |
152 <Button name="cancelButton" text="Reraise"/> | |
153 </HBox> | |
154 </Window> | |
155 """ | |
156 | |
157 def _make_text(message): | |
158 if callable(message): | |
159 message = message() | |
160 if hasattr(message,"read"): | |
161 message = message.read() | |
162 return message | |
163 | |
164 def message(message="",caption="Message"): | |
165 text = _make_text(message) | |
166 dialog = XMLDialog(StringIO(MESSAGE_BOX_XML), | |
167 initial_data={'message':text,'window':caption}) | |
168 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 | |
169 dialog.execute() | |
170 | |
171 def yesNo(message="",caption="Message"): | |
172 text = _make_text(message) | |
173 dialog = XMLDialog(StringIO(YESNO_BOX_XML), | |
174 initial_data={'message':text,'window':caption}) | |
175 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 | |
176 return dialog.execute() | |
177 | |
178 def yesNoCancel(message="",caption="Message"): | |
179 text = _make_text(message) | |
180 dialog = XMLDialog(StringIO(YESNOCANCEL_BOX_XML), | |
181 initial_data={'message':text,'window':caption}) | |
182 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 | |
183 return dialog.execute() | |
184 | |
185 def select(message="",options=[],caption="Message"): | |
186 text = _make_text(message) | |
187 dialog = XMLDialog(StringIO(SELECT_BOX_XML), | |
188 initial_data={'message':text,'window':caption}) | |
189 dialog.size = screen_width()/3, 2*screen_height()/3 | |
190 | |
191 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 | |
192 listbox = dialog.gui.findChild(name="selection") | |
193 listbox.items = options | |
194 if dialog.execute(): | |
195 return listbox.selected_item | |
196 return None | |
197 | |
198 def trace(f): | |
199 import sys, traceback | |
200 def new_f(*args,**kwargs): | |
201 try: | |
202 return pychan.tools.applyOnlySuitable(f,*args,**kwargs) | |
203 | |
204 except Exception, e: | |
205 dialog = XMLDialog(StringIO(EXCEPTION_CATCHER_XML), | |
206 initial_data={'message':str(e)} | |
207 ) | |
208 tb = traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback) | |
209 dialog.gui.findChild(name="traceback").text = "".join(tb) | |
210 dialog.min_size = screen_width()/2,3*screen_height()/4 | |
211 dialog.max_size = screen_width()/2,3*screen_height()/4 | |
212 result = dialog.execute() | |
213 if result == YES: | |
214 return new_f(*args,**kwargs) | |
215 elif result == NO: | |
216 return | |
217 raise | |
218 return new_f | |
219 |