]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/panel.py
Fix compile error when building for Python 2.2
[wxWidgets.git] / wxPython / wx / tools / XRCed / panel.py
1 # Name: panel.py
2 # Purpose: XRC editor, Panel class and related
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
4 # Created: 02.12.2002
5 # RCS-ID: $Id$
6
7 from xxx import * # xxx imports globals and params
8 from undo import *
9 from wxPython.html import wxHtmlWindow
10
11 # Properties panel containing notebook
12 class Panel(wxNotebook):
13 def __init__(self, parent, id = -1):
14 if wxPlatform != '__WXMAC__': # some problems with this style on macs
15 wxNotebook.__init__(self, parent, id, style=wxNB_BOTTOM)
16 else:
17 wxNotebook.__init__(self, parent, id)
18 global panel
19 g.panel = panel = self
20 self.modified = False
21
22 # Set common button size for parameter buttons
23 bTmp = wxButton(self, -1, '')
24 import params
25 params.buttonSize = (self.DLG_SZE(buttonSize)[0], bTmp.GetSize()[1])
26 bTmp.Destroy()
27 del bTmp
28
29 # List of child windows
30 self.pages = []
31 # Create scrolled windows for pages
32 self.page1 = wxScrolledWindow(self, -1)
33 sizer = wxBoxSizer()
34 sizer.Add(wxBoxSizer()) # dummy sizer
35 self.page1.SetAutoLayout(True)
36 self.page1.SetSizer(sizer)
37 self.AddPage(self.page1, 'Properties')
38 # Second page
39 self.page2 = wxScrolledWindow(self, -1)
40 sizer = wxBoxSizer()
41 sizer.Add(wxBoxSizer()) # dummy sizer
42 self.page2.SetAutoLayout(True)
43 self.page2.SetSizer(sizer)
44 # Cache for already used panels
45 self.pageCache = {} # cached property panels
46 self.stylePageCache = {} # cached style panels
47 # Dummy parent window for cache pages
48 self.cacheParent = wxFrame(None, -1, 'i am invisible')
49 # Delete child windows and recreate page sizer
50 def ResetPage(self, page):
51 topSizer = page.GetSizer()
52 sizer = topSizer.GetChildren()[0].GetSizer()
53 for w in page.GetChildren():
54 sizer.RemoveWindow(w)
55 if isinstance(w, ParamPage):
56 # With SetParent, we wouldn't need this
57 w.Reparent(self.cacheParent)
58 else:
59 w.Destroy()
60 topSizer.RemoveSizer(sizer)
61 # Create new windows
62 sizer = wxBoxSizer(wxVERTICAL)
63 # Special case - resize html window
64 if g.conf.panic:
65 topSizer.Add(sizer, 1, wxEXPAND)
66 else:
67 topSizer.Add(sizer, 0, wxALL, 5)
68 return sizer
69 def SetData(self, xxx):
70 self.pages = []
71 # First page
72 # Set cached or new page
73 # Remove current objects and sizer
74 sizer = self.ResetPage(self.page1)
75 if not xxx or (not xxx.allParams and not xxx.hasName):
76 if g.tree.selection:
77 sizer.Add(wxStaticText(self.page1, -1, 'This item has no properties.'))
78 else: # nothing selected
79 # If first time, show some help
80 if g.conf.panic:
81 html = wxHtmlWindow(self.page1, -1, wxDefaultPosition,
82 wxDefaultSize, wxSUNKEN_BORDER)
83 html.SetPage(g.helpText)
84 sizer.Add(html, 1, wxEXPAND)
85 g.conf.panic = False
86 else:
87 sizer.Add(wxStaticText(self.page1, -1, 'Select a tree item.'))
88 else:
89 g.currentXXX = xxx.treeObject()
90 try:
91 page = self.pageCache[xxx.__class__]
92 page.box.SetLabel(xxx.panelName())
93 page.Reparent(self.page1)
94 except KeyError:
95 page = PropPage(self.page1, xxx.panelName(), xxx)
96 self.pageCache[xxx.__class__] = page
97 page.SetValues(xxx)
98 self.pages.append(page)
99 sizer.Add(page, 1, wxEXPAND)
100 if xxx.hasChild:
101 # Special label for child objects - they may have different GUI
102 cacheID = (xxx.child.__class__, xxx.__class__)
103 try:
104 page = self.pageCache[cacheID]
105 page.box.SetLabel(xxx.child.panelName())
106 page.Reparent(self.page1)
107 except KeyError:
108 page = PropPage(self.page1, xxx.child.panelName(), xxx.child)
109 self.pageCache[cacheID] = page
110 page.SetValues(xxx.child)
111 self.pages.append(page)
112 sizer.Add(page, 0, wxEXPAND | wxTOP, 5)
113 self.page1.Layout()
114 size = self.page1.GetSizer().GetMinSize()
115 self.page1.SetScrollbars(1, 1, size.width, size.height, 0, 0, True)
116
117 # Second page
118 # Create if does not exist
119 if xxx and xxx.treeObject().hasStyle:
120 xxx = xxx.treeObject()
121 # Simplest case: set data if class is the same
122 sizer = self.ResetPage(self.page2)
123 try:
124 page = self.stylePageCache[xxx.__class__]
125 page.Reparent(self.page2)
126 except KeyError:
127 page = StylePage(self.page2, xxx.className + ' style', xxx)
128 self.stylePageCache[xxx.__class__] = page
129 page.SetValues(xxx)
130 self.pages.append(page)
131 sizer.Add(page, 0, wxEXPAND)
132 # Add page if not exists
133 if not self.GetPageCount() == 2:
134 self.AddPage(self.page2, 'Style')
135 self.page2.Layout()
136 size = self.page2.GetSizer().GetMinSize()
137 self.page2.SetScrollbars(1, 1, size.width, size.height, 0, 0, True)
138 else:
139 # Remove page if exists
140 if self.GetPageCount() == 2:
141 self.SetSelection(0)
142 self.page1.Refresh()
143 self.RemovePage(1)
144 self.modified = False
145 def Clear(self):
146 self.SetData(None)
147 self.modified = False
148 # If some parameter has changed
149 def IsModified(self):
150 return self.modified
151 def SetModified(self, value):
152 # Register undo object when modifying first time
153 if not self.modified and value:
154 g.undoMan.RegisterUndo(UndoEdit())
155 self.modified = value
156 def Apply(self):
157 for p in self.pages: p.Apply()
158
159 ################################################################################
160
161 # General class for notebook pages
162 class ParamPage(wxPanel):
163 def __init__(self, parent, xxx):
164 wxPanel.__init__(self, parent, -1)
165 self.xxx = xxx
166 # Register event handlers
167 for id in paramIDs.values():
168 EVT_CHECKBOX(self, id, self.OnCheckParams)
169 self.checks = {}
170 self.controls = {} # save python objects
171 self.controlName = None
172
173 def OnCheckParams(self, evt):
174 xxx = self.xxx
175 param = evt.GetEventObject().GetName()
176 w = self.controls[param]
177 w.Enable(True)
178 objElem = xxx.element
179 if evt.IsChecked():
180 # Ad new text node in order of allParams
181 w.SetValue('') # set empty (default) value
182 w.SetModified() # mark as changed
183 elem = g.tree.dom.createElement(param)
184 # Some classes are special
185 if param == 'font':
186 xxx.params[param] = xxxParamFont(xxx.element, elem)
187 elif param in xxxObject.bitmapTags:
188 xxx.params[param] = xxxParamBitmap(elem)
189 else:
190 xxx.params[param] = xxxParam(elem)
191 # Find place to put new element: first present element after param
192 found = False
193 paramStyles = xxx.allParams + xxx.styles
194 for p in paramStyles[paramStyles.index(param) + 1:]:
195 # Content params don't have same type
196 if xxx.params.has_key(p) and p != 'content':
197 found = True
198 break
199 if found:
200 nextTextElem = xxx.params[p].node
201 objElem.insertBefore(elem, nextTextElem)
202 else:
203 objElem.appendChild(elem)
204 else:
205 # Remove parameter
206 xxx.params[param].remove()
207 del xxx.params[param]
208 w.SetValue('')
209 w.modified = False # mark as not changed
210 w.Enable(False)
211 # Set modified flag (provokes undo storing is necessary)
212 panel.SetModified(True)
213 def Apply(self):
214 xxx = self.xxx
215 if self.controlName:
216 name = self.controlName.GetValue()
217 if xxx.name != name:
218 xxx.name = name
219 xxx.element.setAttribute('name', name)
220 for param, w in self.controls.items():
221 if w.modified:
222 paramObj = xxx.params[param]
223 value = w.GetValue()
224 if param in xxx.specials:
225 xxx.setSpecial(param, value)
226 else:
227 paramObj.update(value)
228 # Save current state
229 def SaveState(self):
230 self.origChecks = map(lambda i: (i[0], i[1].GetValue()), self.checks.items())
231 self.origControls = map(lambda i: (i[0], i[1].GetValue(), i[1].IsEnabled()),
232 self.controls.items())
233 if self.controlName:
234 self.origName = self.controlName.GetValue()
235 # Return original values
236 def GetState(self):
237 if self.controlName:
238 return (self.origChecks, self.origControls, self.origName)
239 else:
240 return (self.origChecks, self.origControls)
241 # Set values from undo data
242 def SetState(self, state):
243 for k,v in state[0]:
244 self.checks[k].SetValue(v)
245 for k,v,e in state[1]:
246 self.controls[k].SetValue(v)
247 self.controls[k].Enable(e)
248 if e: self.controls[k].modified = True
249 if self.controlName:
250 self.controlName.SetValue(state[2])
251
252 ################################################################################
253
254 # Panel for displaying properties
255 class PropPage(ParamPage):
256 def __init__(self, parent, label, xxx):
257 ParamPage.__init__(self, parent, xxx)
258 self.box = wxStaticBox(self, -1, label)
259 self.box.SetFont(labelFont)
260 topSizer = wxStaticBoxSizer(self.box, wxVERTICAL)
261 sizer = wxFlexGridSizer(len(xxx.allParams), 2, 1, 1)
262 sizer.AddGrowableCol(1)
263 if xxx.hasName:
264 label = wxStaticText(self, -1, 'XML ID:', size=(100,-1))
265 control = ParamText(self, 'XML_name', 200)
266 sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL),
267 (control, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM | wxGROW, 5) ])
268 self.controlName = control
269 for param in xxx.allParams:
270 present = xxx.params.has_key(param)
271 if param in xxx.required:
272 label = wxStaticText(self, paramIDs[param], param + ':',
273 size = (100,-1), name = param)
274 else:
275 # Notebook has one very loooooong parameter
276 if param == 'usenotebooksizer': sParam = 'usesizer:'
277 else: sParam = param + ':'
278 label = wxCheckBox(self, paramIDs[param], sParam,
279 size = (100,-1), name = param)
280 self.checks[param] = label
281 try:
282 typeClass = xxx.paramDict[param]
283 except KeyError:
284 try:
285 # Standart type
286 typeClass = paramDict[param]
287 except KeyError:
288 # Default
289 typeClass = ParamText
290 control = typeClass(self, param)
291 control.Enable(present)
292 sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL),
293 (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ])
294 self.controls[param] = control
295 topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3)
296 self.SetAutoLayout(True)
297 self.SetSizer(topSizer)
298 topSizer.Fit(self)
299 def SetValues(self, xxx):
300 self.xxx = xxx
301 self.origChecks = []
302 self.origControls = []
303 # Set values, checkboxes to False, disable defaults
304 if xxx.hasName:
305 self.controlName.SetValue(xxx.name)
306 self.origName = xxx.name
307 for param in xxx.allParams:
308 w = self.controls[param]
309 w.modified = False
310 try:
311 value = xxx.params[param].value()
312 w.Enable(True)
313 w.SetValue(value)
314 if not param in xxx.required:
315 self.checks[param].SetValue(True)
316 self.origChecks.append((param, True))
317 self.origControls.append((param, value, True))
318 except KeyError:
319 self.checks[param].SetValue(False)
320 w.SetValue('')
321 w.Enable(False)
322 self.origChecks.append((param, False))
323 self.origControls.append((param, '', False))
324
325 ################################################################################
326
327 # Style notebook page
328 class StylePage(ParamPage):
329 def __init__(self, parent, label, xxx):
330 ParamPage.__init__(self, parent, xxx)
331 box = wxStaticBox(self, -1, label)
332 box.SetFont(labelFont)
333 topSizer = wxStaticBoxSizer(box, wxVERTICAL)
334 sizer = wxFlexGridSizer(len(xxx.styles), 2, 1, 1)
335 sizer.AddGrowableCol(1)
336 for param in xxx.styles:
337 present = xxx.params.has_key(param)
338 check = wxCheckBox(self, paramIDs[param],
339 param + ':', size = (100,-1), name = param)
340 check.SetValue(present)
341 control = paramDict[param](self, name = param)
342 control.Enable(present)
343 sizer.AddMany([ (check, 0, wxALIGN_CENTER_VERTICAL),
344 (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ])
345 self.checks[param] = check
346 self.controls[param] = control
347 topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3)
348 self.SetAutoLayout(True)
349 self.SetSizer(topSizer)
350 topSizer.Fit(self)
351 # Set data for a cahced page
352 def SetValues(self, xxx):
353 self.xxx = xxx
354 self.origChecks = []
355 self.origControls = []
356 for param in xxx.styles:
357 present = xxx.params.has_key(param)
358 check = self.checks[param]
359 check.SetValue(present)
360 w = self.controls[param]
361 w.modified = False
362 if present:
363 value = xxx.params[param].value()
364 else:
365 value = ''
366 w.SetValue(value)
367 w.Enable(present)
368 self.origChecks.append((param, present))
369 self.origControls.append((param, value, present))
370