Mercurial > traipse
comparison orpg/gametree/nodehandlers/forms.py @ 0:4385a7d0efd1 grumpy-goblin
Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author | sirebral |
---|---|
date | Tue, 14 Jul 2009 16:41:58 -0500 |
parents | |
children | 97265586402b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4385a7d0efd1 |
---|---|
1 # Copyright (C) 2000-2001 The OpenRPG Project | |
2 # | |
3 # openrpg-dev@lists.sourceforge.net | |
4 # | |
5 # This program is free software; you can redistribute it and/or modify | |
6 # it under the terms of the GNU General Public License as published by | |
7 # the Free Software Foundation; either version 2 of the License, or | |
8 # (at your option) any later version. | |
9 # | |
10 # This program is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with this program; if not, write to the Free Software | |
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 # -- | |
19 # | |
20 # File: forms.py | |
21 # Author: Chris Davis | |
22 # Maintainer: | |
23 # Version: | |
24 # $Id: forms.py,v 1.53 2007/04/21 23:00:51 digitalxero Exp $ | |
25 # | |
26 # Description: The file contains code for the form based nodehanlers | |
27 # | |
28 | |
29 __version__ = "$Id: forms.py,v 1.53 2007/04/21 23:00:51 digitalxero Exp $" | |
30 | |
31 from containers import * | |
32 import wx.lib.scrolledpanel | |
33 | |
34 def bool2int(b): | |
35 #in wxPython 2.5+, evt.Checked() returns True or False instead of 1.0 or 0. | |
36 #by running the results of that through this function, we convert it. | |
37 #if it was an int already, nothing changes. The difference between 1.0 | |
38 #and 1, i.e. between ints and floats, is potentially dangerous when we | |
39 #use str() on it, but it seems to work fine right now. | |
40 if b: | |
41 return 1 | |
42 else: | |
43 return 0 | |
44 | |
45 ################################# | |
46 ## form container | |
47 ################################# | |
48 | |
49 class form_handler(container_handler): | |
50 """ | |
51 <nodehandler name='?' module='forms' class='form_handler' > | |
52 <form width='100' height='100' /> | |
53 </nodehandler> | |
54 """ | |
55 | |
56 def __init__(self,xml_dom,tree_node): | |
57 container_handler.__init__(self,xml_dom,tree_node) | |
58 | |
59 def load_children(self): | |
60 self.atts = None | |
61 children = self.master_dom._get_childNodes() | |
62 for c in children: | |
63 if c._get_tagName() == "form": | |
64 self.atts = c | |
65 else: | |
66 self.tree.load_xml(c,self.mytree_node) | |
67 if not self.atts: | |
68 elem = self.xml.minidom.Element('form') | |
69 elem.setAttribute("width","400") | |
70 elem.setAttribute("height","600") | |
71 self.atts = self.master_dom.appendChild(elem) | |
72 | |
73 def get_design_panel(self,parent): | |
74 return form_edit_panel(parent,self) | |
75 | |
76 def get_use_panel(self,parent): | |
77 return form_panel(parent,self) | |
78 | |
79 def on_drop(self,evt): | |
80 # make sure its a contorl node | |
81 container_handler.on_drop(self,evt) | |
82 | |
83 | |
84 class form_panel(wx.lib.scrolledpanel.ScrolledPanel): | |
85 def __init__(self, parent, handler): | |
86 wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent, wx.ID_ANY, style=wx.NO_BORDER|wx.VSCROLL|wx.HSCROLL) | |
87 self.height = int(handler.atts.getAttribute("height")) | |
88 self.width = int(handler.atts.getAttribute("width")) | |
89 | |
90 | |
91 self.SetSize((0,0)) | |
92 self.handler = handler | |
93 self.parent = parent | |
94 self.main_sizer = wx.BoxSizer(wx.VERTICAL) | |
95 tree = self.handler.tree | |
96 child = tree.GetFirstChild(handler.mytree_node) | |
97 if child[0].IsOk(): | |
98 handler.traverse(child[0], self.create_child_wnd, 0, None, False) | |
99 | |
100 self.SetSizer(self.main_sizer) | |
101 self.SetAutoLayout(True) | |
102 | |
103 self.SetupScrolling() | |
104 | |
105 parent.SetSize(self.GetSize()) | |
106 self.Fit() | |
107 | |
108 | |
109 def SetSize(self, xy): | |
110 (x, y) = self.GetSize() | |
111 (nx, ny) = xy | |
112 if x < nx: | |
113 x = nx+10 | |
114 y += ny+11 | |
115 wx.lib.scrolledpanel.ScrolledPanel.SetSize(self, (x, y)) | |
116 | |
117 | |
118 def create_child_wnd(self, obj, evt): | |
119 panel = obj.get_use_panel(self) | |
120 size = obj.get_size_constraint() | |
121 if panel: | |
122 self.main_sizer.Add(panel, size, wx.EXPAND) | |
123 self.main_sizer.Add(wx.Size(10,10)) | |
124 | |
125 | |
126 | |
127 F_HEIGHT = wx.NewId() | |
128 F_WIDTH = wx.NewId() | |
129 class form_edit_panel(wx.Panel): | |
130 def __init__(self, parent, handler): | |
131 wx.Panel.__init__(self, parent, -1) | |
132 self.handler = handler | |
133 sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Form Properties"), wx.VERTICAL) | |
134 wh_sizer = wx.BoxSizer(wx.HORIZONTAL) | |
135 self.text = { P_TITLE : wx.TextCtrl(self, P_TITLE, handler.master_dom.getAttribute('name')), | |
136 F_HEIGHT : wx.TextCtrl(self, F_HEIGHT, handler.atts.getAttribute('height')), | |
137 F_WIDTH : wx.TextCtrl(self, F_WIDTH, handler.atts.getAttribute('width')) | |
138 } | |
139 | |
140 wh_sizer.Add(wx.StaticText(self, -1, "Width:"), 0, wx.ALIGN_CENTER) | |
141 wh_sizer.Add(wx.Size(10,10)) | |
142 wh_sizer.Add(self.text[F_WIDTH], 0, wx.EXPAND) | |
143 wh_sizer.Add(wx.Size(10,10)) | |
144 wh_sizer.Add(wx.StaticText(self, -1, "Height:"), 0, wx.ALIGN_CENTER) | |
145 wh_sizer.Add(wx.Size(10,10)) | |
146 wh_sizer.Add(self.text[F_HEIGHT], 0, wx.EXPAND) | |
147 | |
148 sizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.EXPAND) | |
149 sizer.Add(self.text[P_TITLE], 0, wx.EXPAND) | |
150 sizer.Add(wx.Size(10,10)) | |
151 sizer.Add(wh_sizer,0,wx.EXPAND) | |
152 | |
153 self.SetSizer(sizer) | |
154 self.SetAutoLayout(True) | |
155 self.Fit() | |
156 parent.SetSize(self.GetBestSize()) | |
157 | |
158 self.Bind(wx.EVT_TEXT, self.on_text, id=P_TITLE) | |
159 self.Bind(wx.EVT_TEXT, self.on_text, id=F_HEIGHT) | |
160 self.Bind(wx.EVT_TEXT, self.on_text, id=F_WIDTH) | |
161 | |
162 def on_text(self,evt): | |
163 id = evt.GetId() | |
164 txt = self.text[id].GetValue() | |
165 if not len(txt): return | |
166 if id == P_TITLE: | |
167 self.handler.master_dom.setAttribute('name',txt) | |
168 self.handler.rename(txt) | |
169 elif id == F_HEIGHT or id == F_WIDTH: | |
170 try: | |
171 int(txt) | |
172 except: | |
173 return 0 | |
174 if id == F_HEIGHT: | |
175 self.handler.atts.setAttribute("height",txt) | |
176 elif id == F_WIDTH: | |
177 self.handler.atts.setAttribute("width",txt) | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 ########################## | |
184 ## control handler | |
185 ########################## | |
186 class control_handler(node_handler): | |
187 """ A nodehandler for form controls. | |
188 <nodehandler name='?' module='forms' class='control_handler' /> | |
189 """ | |
190 def __init__(self,xml_dom,tree_node): | |
191 node_handler.__init__(self,xml_dom,tree_node) | |
192 | |
193 def get_size_constraint(self): | |
194 return 0 | |
195 | |
196 | |
197 ########################## | |
198 ## textctrl handler | |
199 ########################## | |
200 # | |
201 # Updated by Snowdog (April 2003) | |
202 # Now includes Raw Send Mode (like the chat macro uses) | |
203 # and an option to remove the title from text when sent | |
204 # to the chat in the normal non-chat macro mode. | |
205 # | |
206 class textctrl_handler(node_handler): | |
207 """ <nodehandler class="textctrl_handler" module="form" name=""> | |
208 <text multiline='0' send_button='0' raw_mode='0' hide_title='0'>Text In Node</text> | |
209 </nodehandler> | |
210 """ | |
211 def __init__(self,xml_dom,tree_node): | |
212 node_handler.__init__(self,xml_dom,tree_node) | |
213 self.text_elem = self.master_dom.getElementsByTagName('text')[0] | |
214 self.text = safe_get_text_node(self.text_elem) | |
215 if self.text_elem.getAttribute("send_button") == "": | |
216 self.text_elem.setAttribute("send_button","0") | |
217 if self.text_elem.getAttribute("raw_mode") == "": | |
218 self.text_elem.setAttribute("raw_mode","0") | |
219 if self.text_elem.getAttribute("hide_title") == "": | |
220 self.text_elem.setAttribute("hide_title","0") | |
221 | |
222 def get_design_panel(self,parent): | |
223 return textctrl_edit_panel(parent,self) | |
224 | |
225 def get_use_panel(self,parent): | |
226 return text_panel(parent,self) | |
227 | |
228 def get_size_constraint(self): | |
229 return int(self.text_elem.getAttribute("multiline")) | |
230 | |
231 def is_multi_line(self): | |
232 return int(self.text_elem.getAttribute("multiline")) | |
233 | |
234 def is_raw_send(self): | |
235 return int(self.text_elem.getAttribute("raw_mode")) | |
236 | |
237 def is_hide_title(self): | |
238 return int(self.text_elem.getAttribute("hide_title")) | |
239 | |
240 def has_send_button(self): | |
241 return int(self.text_elem.getAttribute("send_button")) | |
242 | |
243 | |
244 def tohtml(self): | |
245 txt = self.text._get_nodeValue() | |
246 txt = string.replace(txt,'\n',"<br />") | |
247 if not self.is_hide_title(): | |
248 txt = "<b>"+self.master_dom.getAttribute("name")+":</b> "+txt | |
249 return txt | |
250 | |
251 | |
252 FORM_TEXT_CTRL = wx.NewId() | |
253 FORM_SEND_BUTTON = wx.NewId() | |
254 | |
255 class text_panel(wx.Panel): | |
256 def __init__(self, parent, handler): | |
257 wx.Panel.__init__(self, parent, -1) | |
258 self.chat = handler.chat | |
259 self.handler = handler | |
260 if handler.is_multi_line(): | |
261 text_style = wx.TE_MULTILINE | |
262 sizer_style = wx.EXPAND | |
263 sizer = wx.BoxSizer(wx.VERTICAL) | |
264 else: | |
265 sizer_style = wx.ALIGN_CENTER | |
266 text_style = 0 | |
267 sizer = wx.BoxSizer(wx.HORIZONTAL) | |
268 | |
269 txt = handler.text._get_nodeValue() | |
270 self.text = wx.TextCtrl(self, FORM_TEXT_CTRL, txt, style=text_style) | |
271 sizer.Add(wx.StaticText(self, -1, handler.master_dom.getAttribute('name')+": "), 0, sizer_style) | |
272 sizer.Add(wx.Size(5,0)) | |
273 sizer.Add(self.text, 1, sizer_style) | |
274 | |
275 if handler.has_send_button(): | |
276 sizer.Add(wx.Button(self, FORM_SEND_BUTTON, "Send"), 0, sizer_style) | |
277 | |
278 self.sizer = sizer | |
279 self.SetSizer(sizer) | |
280 self.SetAutoLayout(True) | |
281 | |
282 parent.SetSize(self.GetBestSize()) | |
283 self.Bind(wx.EVT_TEXT, self.on_text, id=FORM_TEXT_CTRL) | |
284 self.Bind(wx.EVT_BUTTON, self.on_send, id=FORM_SEND_BUTTON) | |
285 | |
286 def on_text(self,evt): | |
287 txt = self.text.GetValue() | |
288 txt = strip_text(txt) | |
289 self.handler.text._set_nodeValue(txt) | |
290 | |
291 def on_send(self,evt): | |
292 txt = self.text.GetValue() | |
293 if not self.handler.is_raw_send(): | |
294 #self.chat.ParsePost(self.tohtml(),True,True) | |
295 self.chat.ParsePost(self.handler.tohtml(),True,True) | |
296 return 1 | |
297 actionlist = txt.split("\n") | |
298 for line in actionlist: | |
299 if(line != ""): | |
300 if line[0] != "/": ## it's not a slash command | |
301 self.chat.ParsePost(line,True,True) | |
302 else: | |
303 action = line | |
304 self.chat.chat_cmds.docmd(action) | |
305 return 1 | |
306 | |
307 F_MULTI = wx.NewId() | |
308 F_SEND_BUTTON = wx.NewId() | |
309 F_RAW_SEND = wx.NewId() | |
310 F_HIDE_TITLE = wx.NewId() | |
311 F_TEXT = wx.NewId() | |
312 | |
313 class textctrl_edit_panel(wx.Panel): | |
314 def __init__(self, parent, handler): | |
315 wx.Panel.__init__(self, parent, -1) | |
316 self.handler = handler | |
317 sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Text Properties"), wx.VERTICAL) | |
318 | |
319 self.title = wx.TextCtrl(self, P_TITLE, handler.master_dom.getAttribute('name')) | |
320 self.multi = wx.CheckBox(self, F_MULTI, " Multi-Line") | |
321 self.multi.SetValue(handler.is_multi_line()) | |
322 self.raw_send = wx.CheckBox(self, F_RAW_SEND, " Send as Macro") | |
323 self.raw_send.SetValue(handler.is_raw_send()) | |
324 self.hide_title = wx.CheckBox(self, F_HIDE_TITLE, " Hide Title") | |
325 self.hide_title.SetValue(handler.is_hide_title()) | |
326 self.send_button = wx.CheckBox(self, F_SEND_BUTTON, " Send Button") | |
327 self.send_button.SetValue(handler.has_send_button()) | |
328 | |
329 sizer.Add(wx.StaticText(self, P_TITLE, "Title:"), 0, wx.EXPAND) | |
330 sizer.Add(self.title, 0, wx.EXPAND) | |
331 sizer.Add(wx.Size(10,10)) | |
332 sizer.Add(self.multi, 0, wx.EXPAND) | |
333 sizer.Add(self.raw_send, 0, wx.EXPAND) | |
334 sizer.Add(self.hide_title, 0, wx.EXPAND) | |
335 sizer.Add(self.send_button, 0 , wx.EXPAND) | |
336 sizer.Add(wx.Size(10,10)) | |
337 if handler.is_multi_line(): | |
338 sizer_style = wx.EXPAND | |
339 text_style = wx.TE_MULTILINE | |
340 multi = 1 | |
341 else: | |
342 sizer_style=wx.EXPAND | |
343 text_style = 0 | |
344 multi = 0 | |
345 self.text = wx.TextCtrl(self, F_TEXT, handler.text._get_nodeValue(),style=text_style) | |
346 sizer.Add(wx.Size(5,0)) | |
347 sizer.Add(self.text, multi, sizer_style) | |
348 self.SetSizer(sizer) | |
349 self.SetAutoLayout(True) | |
350 | |
351 self.Bind(wx.EVT_TEXT, self.on_text, id=P_TITLE) | |
352 self.Bind(wx.EVT_TEXT, self.on_text, id=F_TEXT) | |
353 self.Bind(wx.EVT_CHECKBOX, self.on_button, id=F_MULTI) | |
354 self.Bind(wx.EVT_CHECKBOX, self.on_raw_button, id=F_RAW_SEND) | |
355 self.Bind(wx.EVT_CHECKBOX, self.on_hide_button, id=F_HIDE_TITLE) | |
356 self.Bind(wx.EVT_CHECKBOX, self.on_send_button, id=F_SEND_BUTTON) | |
357 | |
358 def on_text(self,evt): | |
359 id = evt.GetId() | |
360 if id == P_TITLE: | |
361 txt = self.title.GetValue() | |
362 if not len(txt): return | |
363 self.handler.master_dom.setAttribute('name',txt) | |
364 self.handler.rename(txt) | |
365 if id == F_TEXT: | |
366 txt = self.text.GetValue() | |
367 txt = strip_text(txt) | |
368 self.handler.text._set_nodeValue(txt) | |
369 | |
370 def on_button(self,evt): | |
371 self.handler.text_elem.setAttribute("multiline",str(bool2int(evt.Checked()))) | |
372 | |
373 def on_raw_button(self,evt): | |
374 self.handler.text_elem.setAttribute("raw_mode",str(bool2int(evt.Checked()))) | |
375 | |
376 def on_hide_button(self,evt): | |
377 self.handler.text_elem.setAttribute("hide_title",str(bool2int(evt.Checked()))) | |
378 | |
379 def on_send_button(self,evt): | |
380 self.handler.text_elem.setAttribute("send_button",str(bool2int(evt.Checked()))) | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 ####################### | |
388 ## listbox handler | |
389 ####################### | |
390 # | |
391 # Updated by Snowdog (April 2003) | |
392 # Now includesan option to remove the title from | |
393 # text when sent to the chat. | |
394 # | |
395 L_DROP = 0 | |
396 L_LIST = 1 | |
397 L_RADIO = 2 | |
398 L_CHECK = 3 | |
399 L_ROLLER = 4 | |
400 | |
401 class listbox_handler(node_handler): | |
402 """ | |
403 <nodehandler class="listbox_handler" module="forms" name=""> | |
404 <list type="1" send_button='0' hide_title='0'> | |
405 <option value="" selected="" >Option Text I</option> | |
406 <option value="" selected="" >Option Text II</option> | |
407 </list> | |
408 </nodehandler> | |
409 """ | |
410 def __init__(self,xml_dom,tree_node): | |
411 node_handler.__init__(self,xml_dom,tree_node) | |
412 self.list = self.master_dom.getElementsByTagName('list')[0] | |
413 self.options = self.list.getElementsByTagName('option') | |
414 if self.list.getAttribute("send_button") == "": | |
415 self.list.setAttribute("send_button","0") | |
416 if self.list.getAttribute("hide_title") == "": | |
417 self.list.setAttribute("hide_title","0") | |
418 | |
419 def get_design_panel(self,parent): | |
420 return listbox_edit_panel(parent,self) | |
421 | |
422 def get_use_panel(self,parent): | |
423 return listbox_panel(parent,self) | |
424 | |
425 def get_type(self): | |
426 return int(self.list.getAttribute("type")) | |
427 | |
428 def set_type(self,type): | |
429 self.list.setAttribute("type",str(type)) | |
430 | |
431 def is_hide_title(self): | |
432 return int(self.list.getAttribute("hide_title")) | |
433 | |
434 # single selection methods | |
435 def get_selected_node(self): | |
436 for opt in self.options: | |
437 if opt.getAttribute("selected") == "1": return opt | |
438 return None | |
439 | |
440 def get_selected_index(self): | |
441 i = 0 | |
442 for opt in self.options: | |
443 if opt.getAttribute("selected") == "1": | |
444 return i | |
445 i += 1 | |
446 return 0 | |
447 | |
448 def get_selected_text(self): | |
449 node = self.get_selected_node() | |
450 if node: | |
451 return safe_get_text_node(node)._get_nodeValue() | |
452 else: | |
453 return "" | |
454 | |
455 | |
456 # mult selection methods | |
457 | |
458 def get_selections(self): | |
459 opts = [] | |
460 for opt in self.options: | |
461 if opt.getAttribute("selected") == "1": | |
462 opts.append(opt) | |
463 return opts | |
464 | |
465 def get_selections_text(self): | |
466 opts = [] | |
467 for opt in self.options: | |
468 if opt.getAttribute("selected") == "1": | |
469 opts.append(safe_get_text_node(opt)._get_nodeValue()) | |
470 return opts | |
471 | |
472 def get_selections_index(self): | |
473 opts = [] | |
474 i = 0 | |
475 for opt in self.options: | |
476 if opt.getAttribute("selected") == "1": | |
477 opts.append(i) | |
478 i += 1 | |
479 return opts | |
480 | |
481 # setting selection method | |
482 | |
483 def set_selected_node(self,index,selected=1): | |
484 if self.get_type() != L_CHECK: | |
485 self.clear_selections() | |
486 self.options[index].setAttribute("selected", str(bool2int(selected))) | |
487 | |
488 def clear_selections(self): | |
489 for opt in self.options: | |
490 opt.setAttribute("selected","0") | |
491 | |
492 # misc methods | |
493 | |
494 def get_options(self): | |
495 opts = [] | |
496 for opt in self.options: | |
497 opts.append(safe_get_text_node(opt)._get_nodeValue()) | |
498 return opts | |
499 | |
500 def get_option(self,index): | |
501 return safe_get_text_node(self.options[index])._get_nodeValue() | |
502 | |
503 def add_option(self,opt): | |
504 elem = minidom.Element('option') | |
505 elem.setAttribute("value","0") | |
506 elem.setAttribute("selected","0") | |
507 t_node = minidom.Text(opt) | |
508 t_node = elem.appendChild(t_node) | |
509 self.list.appendChild(elem) | |
510 self.options = self.list.getElementsByTagName('option') | |
511 | |
512 def remove_option(self,index): | |
513 self.list.removeChild(self.options[index]) | |
514 self.options = self.list.getElementsByTagName('option') | |
515 | |
516 def edit_option(self,index,value): | |
517 safe_get_text_node(self.options[index])._set_nodeValue(value) | |
518 | |
519 def has_send_button(self): | |
520 if self.list.getAttribute("send_button") == '0': | |
521 return False | |
522 else: | |
523 return True | |
524 | |
525 def get_size_constraint(self): | |
526 if self.get_type() == L_DROP: | |
527 return 0 | |
528 else: | |
529 return 1 | |
530 | |
531 def tohtml(self): | |
532 opts = self.get_selections_text() | |
533 text = "" | |
534 if not self.is_hide_title(): | |
535 text = "<b>"+self.master_dom.getAttribute("name")+":</b> " | |
536 comma = ", " | |
537 text += comma.join(opts) | |
538 return text | |
539 | |
540 | |
541 | |
542 F_LIST = wx.NewId() | |
543 F_SEND = wx.NewId() | |
544 | |
545 | |
546 class listbox_panel(wx.Panel): | |
547 def __init__(self, parent, handler): | |
548 wx.Panel.__init__(self, parent, -1) | |
549 self.handler = handler | |
550 self.chat = handler.chat | |
551 opts = handler.get_options() | |
552 cur_opt = handler.get_selected_text() | |
553 type = handler.get_type() | |
554 label = handler.master_dom.getAttribute('name') | |
555 | |
556 if type == L_DROP: | |
557 self.list = wx.ComboBox(self, F_LIST, cur_opt, choices=opts, style=wx.CB_READONLY) | |
558 if self.list.GetSize()[0] > 200: | |
559 self.list.Destroy() | |
560 self.list = wx.ComboBox(self, F_LIST, cur_opt, size=(200, -1), choices=opts, style=wx.CB_READONLY) | |
561 elif type == L_LIST: | |
562 self.list = wx.ListBox(self,F_LIST,choices=opts) | |
563 elif type == L_RADIO: | |
564 self.list = wx.RadioBox(self,F_LIST,label,choices=opts,majorDimension=3) | |
565 elif type == L_CHECK: | |
566 self.list = wx.CheckListBox(self,F_LIST,choices=opts) | |
567 self.set_checks() | |
568 | |
569 for i in handler.get_selections_text(): | |
570 if type == L_DROP: | |
571 self.list.SetValue( i ) | |
572 else: | |
573 self.list.SetStringSelection( i ) | |
574 | |
575 if type == L_DROP: | |
576 sizer = wx.BoxSizer(wx.HORIZONTAL) | |
577 | |
578 else: | |
579 sizer = wx.BoxSizer(wx.VERTICAL) | |
580 | |
581 if type != L_RADIO: | |
582 sizer.Add(wx.StaticText(self, -1, label+": "), 0, wx.EXPAND) | |
583 sizer.Add(wx.Size(5,0)) | |
584 | |
585 sizer.Add(self.list, 1, wx.EXPAND) | |
586 | |
587 if handler.has_send_button(): | |
588 sizer.Add(wx.Button(self, F_SEND, "Send"), 0, wx.EXPAND) | |
589 self.Bind(wx.EVT_BUTTON, self.handler.on_send_to_chat, id=F_SEND) | |
590 | |
591 self.sizer = sizer | |
592 self.SetSizer(sizer) | |
593 self.SetAutoLayout(True) | |
594 self.Fit() | |
595 | |
596 parent.SetSize(self.GetBestSize()) | |
597 | |
598 if type == L_DROP: | |
599 self.Bind(wx.EVT_COMBOBOX, self.on_change, id=F_LIST) | |
600 elif type == L_LIST: | |
601 self.Bind(wx.EVT_LISTBOX, self.on_change, id=F_LIST) | |
602 elif type == L_RADIO: | |
603 self.Bind(wx.EVT_RADIOBOX, self.on_change, id=F_LIST) | |
604 elif type == L_CHECK: | |
605 self.Bind(wx.EVT_CHECKLISTBOX, self.on_check, id=F_LIST) | |
606 | |
607 | |
608 self.type = type | |
609 | |
610 | |
611 def on_change(self,evt): | |
612 self.handler.set_selected_node(self.list.GetSelection()) | |
613 | |
614 def on_check(self,evt): | |
615 for i in xrange(self.list.GetCount()): | |
616 self.handler.set_selected_node(i, bool2int(self.list.IsChecked(i))) | |
617 | |
618 def set_checks(self): | |
619 for i in self.handler.get_selections_index(): | |
620 self.list.Check(i) | |
621 | |
622 | |
623 | |
624 BUT_ADD = wx.NewId() | |
625 BUT_REM = wx.NewId() | |
626 BUT_EDIT = wx.NewId() | |
627 F_TYPE = wx.NewId() | |
628 F_NO_TITLE = wx.NewId() | |
629 | |
630 class listbox_edit_panel(wx.Panel): | |
631 def __init__(self, parent, handler): | |
632 wx.Panel.__init__(self, parent, -1) | |
633 self.handler = handler | |
634 sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, "List Box Properties"), wx.VERTICAL) | |
635 | |
636 self.text = wx.TextCtrl(self, P_TITLE, handler.master_dom.getAttribute('name')) | |
637 | |
638 opts = handler.get_options() | |
639 self.listbox = wx.ListBox(self, F_LIST, choices=opts, style=wx.LB_HSCROLL|wx.LB_SINGLE|wx.LB_NEEDED_SB) | |
640 opts = ['Drop Down', 'List Box', 'Radio Box', 'Check List'] | |
641 self.type_radios = wx.RadioBox(self,F_TYPE,"List Type",choices=opts) | |
642 self.type_radios.SetSelection(handler.get_type()) | |
643 | |
644 self.send_button = wx.CheckBox(self, F_SEND_BUTTON, " Send Button") | |
645 self.send_button.SetValue(handler.has_send_button()) | |
646 | |
647 self.hide_title = wx.CheckBox(self, F_NO_TITLE, " Hide Title") | |
648 self.hide_title.SetValue(handler.is_hide_title()) | |
649 | |
650 but_sizer = wx.BoxSizer(wx.HORIZONTAL) | |
651 but_sizer.Add(wx.Button(self, BUT_ADD, "Add"), 1, wx.EXPAND) | |
652 but_sizer.Add(wx.Size(10,10)) | |
653 but_sizer.Add(wx.Button(self, BUT_EDIT, "Edit"), 1, wx.EXPAND) | |
654 but_sizer.Add(wx.Size(10,10)) | |
655 but_sizer.Add(wx.Button(self, BUT_REM, "Remove"), 1, wx.EXPAND) | |
656 | |
657 sizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.EXPAND) | |
658 sizer.Add(self.text, 0, wx.EXPAND) | |
659 sizer.Add(wx.Size(10,10)) | |
660 sizer.Add(self.type_radios, 0, wx.EXPAND) | |
661 sizer.Add(wx.Size(10,10)) | |
662 sizer.Add(self.send_button, 0 , wx.EXPAND) | |
663 sizer.Add(self.hide_title, 0, wx.EXPAND) | |
664 sizer.Add(wx.Size(10,10)) | |
665 sizer.Add(wx.StaticText(self, -1, "Options:"), 0, wx.EXPAND) | |
666 sizer.Add(self.listbox,1,wx.EXPAND); | |
667 sizer.Add(but_sizer,0,wx.EXPAND) | |
668 | |
669 self.SetSizer(sizer) | |
670 self.SetAutoLayout(True) | |
671 self.Fit() | |
672 parent.SetSize(self.GetBestSize()) | |
673 | |
674 self.Bind(wx.EVT_TEXT, self.on_text, id=P_TITLE) | |
675 self.Bind(wx.EVT_BUTTON, self.on_edit, id=BUT_EDIT) | |
676 self.Bind(wx.EVT_BUTTON, self.on_remove, id=BUT_REM) | |
677 self.Bind(wx.EVT_BUTTON, self.on_add, id=BUT_ADD) | |
678 self.Bind(wx.EVT_RADIOBOX, self.on_type, id=F_TYPE) | |
679 self.Bind(wx.EVT_CHECKBOX, self.on_hide_button, id=F_NO_TITLE) | |
680 self.Bind(wx.EVT_CHECKBOX, self.on_send_button, id=F_SEND_BUTTON) | |
681 | |
682 def on_type(self,evt): | |
683 self.handler.set_type(evt.GetInt()) | |
684 | |
685 def on_add(self,evt): | |
686 dlg = wx.TextEntryDialog(self, 'Enter option?','Add Option', '') | |
687 if dlg.ShowModal() == wx.ID_OK: | |
688 self.handler.add_option(dlg.GetValue()) | |
689 dlg.Destroy() | |
690 self.reload_options() | |
691 | |
692 def on_remove(self,evt): | |
693 index = self.listbox.GetSelection() | |
694 if index >= 0: | |
695 self.handler.remove_option(index) | |
696 self.reload_options() | |
697 | |
698 def on_edit(self,evt): | |
699 index = self.listbox.GetSelection() | |
700 if index >= 0: | |
701 txt = self.handler.get_option(index) | |
702 dlg = wx.TextEntryDialog(self, 'Enter option?','Edit Option', txt) | |
703 if dlg.ShowModal() == wx.ID_OK: | |
704 self.handler.edit_option(index,dlg.GetValue()) | |
705 dlg.Destroy() | |
706 self.reload_options() | |
707 | |
708 def reload_options(self): | |
709 self.listbox.Clear() | |
710 for opt in self.handler.get_options(): | |
711 self.listbox.Append(opt) | |
712 | |
713 def on_text(self,evt): | |
714 id = evt.GetId() | |
715 txt = self.text.GetValue() | |
716 if not len(txt): return | |
717 if id == P_TITLE: | |
718 self.handler.master_dom.setAttribute('name',txt) | |
719 self.handler.rename(txt) | |
720 | |
721 def on_send_button(self,evt): | |
722 self.handler.list.setAttribute("send_button", str( bool2int(evt.Checked()) )) | |
723 | |
724 def on_hide_button(self,evt): | |
725 print "hide_title, " + str(bool2int(evt.Checked())) | |
726 self.handler.list.setAttribute("hide_title", str( bool2int(evt.Checked()) )) | |
727 | |
728 | |
729 ############################### | |
730 ## link image handlers | |
731 ############################### | |
732 | |
733 class link_handler(node_handler): | |
734 """ A nodehandler for URLs. Will open URL in a wxHTMLFrame | |
735 <nodehandler name='?' module='forms' class='link_handler' > | |
736 <link href='http//??.??' /> | |
737 </nodehandler > | |
738 """ | |
739 def __init__(self,xml_dom,tree_node): | |
740 node_handler.__init__(self,xml_dom,tree_node) | |
741 self.link = self.master_dom._get_firstChild() | |
742 | |
743 def on_use(self,evt): | |
744 href = self.link.getAttribute("href") | |
745 wb = webbrowser.get() | |
746 wb.open(href) | |
747 | |
748 def get_design_panel(self,parent): | |
749 return link_edit_panel(parent,self) | |
750 | |
751 def get_use_panel(self,parent): | |
752 return link_panel(parent,self) | |
753 | |
754 def tohtml(self): | |
755 href = self.link.getAttribute("href") | |
756 title = self.master_dom.getAttribute("name") | |
757 return "<a href=\""+href+"\" >"+title+"</a>" | |
758 | |
759 class link_panel(wx.StaticText): | |
760 def __init__(self,parent,handler): | |
761 self.handler = handler | |
762 label = handler.master_dom.getAttribute('name') | |
763 wx.StaticText.__init__(self,parent,-1,label) | |
764 self.SetForegroundColour(wx.BLUE) | |
765 self.Bind(wx.EVT_LEFT_DOWN, self.handler.on_use) | |
766 | |
767 | |
768 P_URL = wx.NewId() | |
769 | |
770 class link_edit_panel(wx.Panel): | |
771 def __init__(self, parent, handler): | |
772 wx.Panel.__init__(self, parent, -1) | |
773 self.handler = handler | |
774 sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Link Properties"), wx.VERTICAL) | |
775 | |
776 self.text = {} | |
777 self.text[P_TITLE] = wx.TextCtrl(self, P_TITLE, handler.master_dom.getAttribute('name')) | |
778 self.text[P_URL] = wx.TextCtrl(self, P_URL, handler.link.getAttribute('href')) | |
779 | |
780 sizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.EXPAND) | |
781 sizer.Add(self.text[P_TITLE], 0, wx.EXPAND) | |
782 sizer.Add(wx.Size(10,10)) | |
783 sizer.Add(wx.StaticText(self, -1, "URL:"), 0, wx.EXPAND) | |
784 sizer.Add(self.text[P_URL], 0, wx.EXPAND) | |
785 self.SetSizer(sizer) | |
786 self.Bind(wx.EVT_TEXT, self.on_text, id=P_TITLE) | |
787 self.Bind(wx.EVT_TEXT, self.on_text, id=P_URL) | |
788 | |
789 def on_text(self,evt): | |
790 id = evt.GetId() | |
791 txt = self.text[id].GetValue() | |
792 if not len(txt): return | |
793 if id == P_TITLE: | |
794 self.handler.master_dom.setAttribute('name',txt) | |
795 self.handler.rename(txt) | |
796 elif id == P_URL: | |
797 self.handler.link.setAttribute('href',txt) | |
798 | |
799 ########################## | |
800 ## webimg node handler | |
801 ########################## | |
802 class webimg_handler(node_handler): | |
803 """ A nodehandler for URLs. Will open URL in a wxHTMLFrame | |
804 <nodehandler name='?' module='forms' class='webimg_handler' > | |
805 <link href='http//??.??' /> | |
806 </nodehandler > | |
807 """ | |
808 def __init__(self,xml_dom,tree_node): | |
809 node_handler.__init__(self,xml_dom,tree_node) | |
810 self.link = self.master_dom._get_firstChild() | |
811 | |
812 def get_design_panel(self,parent): | |
813 return link_edit_panel(parent,self) | |
814 | |
815 def get_use_panel(self,parent): | |
816 img = img_helper().load_url(self.link.getAttribute("href")) | |
817 #print img | |
818 if not img is None: | |
819 return wx.StaticBitmap(parent,-1,img,size= wx.Size(img.GetWidth(),img.GetHeight())) | |
820 return wx.EmptyBitmap(1, 1) | |
821 | |
822 def tohtml(self): | |
823 href = self.link.getAttribute("href") | |
824 title = self.master_dom.getAttribute("name") | |
825 return "<img src=\""+href+"\" alt="+title+" >" |