]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/panel.py
Support for custom controls, first attempt
[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 wx.html import HtmlWindow
10
11 # Properties panel containing notebook
12 class Panel(wx.Notebook):
13 def __init__(self, parent, id = -1):
14 if wx.Platform != '__WXMAC__': # some problems with this style on macs
15 wx.Notebook.__init__(self, parent, id, style=wx.NB_BOTTOM)
16 else:
17 wx.Notebook.__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 = wx.Button(self, -1, '')
24 import params
25 params.buttonSize = (self.DLG_SZE(buttonSizeD)[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 = wx.ScrolledWindow(self, -1)
33 sizer = wx.BoxSizer()
34 sizer.Add(wx.BoxSizer()) # dummy sizer
35 self.page1.SetAutoLayout(True)
36 self.page1.SetSizer(sizer)
37 self.AddPage(self.page1, 'Properties')
38 # Second page
39 self.page2 = wx.ScrolledWindow(self, -1)
40 self.page2.Hide()
41 sizer = wx.BoxSizer()
42 sizer.Add(wx.BoxSizer()) # dummy sizer
43 self.page2.SetAutoLayout(True)
44 self.page2.SetSizer(sizer)
45 # Cache for already used panels
46 self.pageCache = {} # cached property panels
47 self.stylePageCache = {} # cached style panels
48
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.Detach(w)
55 if isinstance(w, ParamPage):
56 if w.IsShown():
57 w.Hide()
58 else:
59 w.Destroy()
60 topSizer.Remove(sizer)
61 # Create new windows
62 sizer = wx.BoxSizer(wx.VERTICAL)
63 # Special case - resize html window
64 if g.conf.panic:
65 topSizer.Add(sizer, 1, wx.EXPAND)
66 else:
67 topSizer.Add(sizer, 0, wx.ALL, 5)
68 return sizer
69
70 def SetData(self, xxx):
71 self.pages = []
72 # First 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 and not xxx.hasChild):
76 if g.tree.selection:
77 sizer.Add(wx.StaticText(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 = HtmlWindow(self.page1, -1, wx.DefaultPosition,
82 wx.DefaultSize, wx.SUNKEN_BORDER)
83 html.SetPage(g.helpText)
84 sizer.Add(html, 1, wx.EXPAND)
85 g.conf.panic = False
86 else:
87 sizer.Add(wx.StaticText(self.page1, -1, 'Select a tree item.'))
88 else:
89 g.currentXXX = xxx.treeObject()
90 # Normal or SizerItem page
91 isGBSizerItem = isinstance(xxx.parent, xxxGridBagSizer)
92 cacheID = (xxx.panelName(), isGBSizerItem)
93 try:
94 page = self.pageCache[cacheID]
95 page.box.SetLabel(xxx.panelName())
96 page.Show()
97 except KeyError:
98 page = PropPage(self.page1, xxx.panelName(), xxx)
99 self.pageCache[cacheID] = page
100 page.SetValues(xxx)
101 self.pages.append(page)
102 sizer.Add(page, 1, wx.EXPAND)
103 if xxx.hasChild:
104 # Special label for child objects - they may have different GUI
105 cacheID = (xxx.child.panelName(), xxx.__class__)
106 try:
107 page = self.pageCache[cacheID]
108 page.box.SetLabel(xxx.child.panelName())
109 page.Show()
110 except KeyError:
111 page = PropPage(self.page1, xxx.child.panelName(), xxx.child)
112 self.pageCache[cacheID] = page
113 page.SetValues(xxx.child)
114 self.pages.append(page)
115 sizer.Add(page, 0, wx.EXPAND | wx.TOP, 5)
116 self.page1.Layout()
117 size = self.page1.GetSizer().GetMinSize()
118 self.page1.SetScrollbars(1, 1, size.width, size.height, 0, 0, True)
119
120 # Second page
121 # Create if does not exist
122 if xxx and xxx.treeObject().hasStyle:
123 xxx = xxx.treeObject()
124 # Simplest case: set data if class is the same
125 sizer = self.ResetPage(self.page2)
126 try:
127 page = self.stylePageCache[xxx.__class__]
128 page.Show()
129 except KeyError:
130 page = StylePage(self.page2, xxx.className + ' style', xxx)
131 self.stylePageCache[xxx.__class__] = page
132 page.SetValues(xxx)
133 self.pages.append(page)
134 sizer.Add(page, 0, wx.EXPAND)
135 # Add page if not exists
136 if not self.GetPageCount() == 2:
137 self.AddPage(self.page2, 'Style')
138 self.page2.Layout()
139 if 'wxGTK' in wx.PlatformInfo:
140 self.page2.Show(True)
141 size = self.page2.GetSizer().GetMinSize()
142 self.page2.SetScrollbars(1, 1, size.width, size.height, 0, 0, True)
143 else:
144 # Remove page if exists
145 if self.GetPageCount() == 2:
146 self.SetSelection(0)
147 self.page1.Refresh()
148 self.RemovePage(1)
149 self.modified = False
150
151 def Clear(self):
152 self.SetData(None)
153 self.modified = False
154
155 # If some parameter has changed
156 def IsModified(self):
157 return self.modified
158
159 def SetModified(self, value):
160 # Register undo object when modifying first time
161 if not self.modified and value:
162 g.undoMan.RegisterUndo(UndoEdit())
163 self.modified = value
164
165 def Apply(self):
166 for p in self.pages: p.Apply()
167
168 ################################################################################
169
170 # General class for notebook pages
171 class ParamPage(wx.Panel):
172 def __init__(self, parent, xxx):
173 wx.Panel.__init__(self, parent, -1)
174 self.xxx = xxx
175 # Register event handlers
176 for id in paramIDs.values():
177 wx.EVT_CHECKBOX(self, id, self.OnCheckParams)
178 self.checks = {}
179 self.controls = {} # save python objects
180 self.controlName = None
181
182 def OnCheckParams(self, evt):
183 xxx = self.xxx
184 param = evt.GetEventObject().GetName()
185 w = self.controls[param]
186 w.Enable(True)
187 objElem = xxx.node
188 if evt.IsChecked():
189 # Ad new text node in order of allParams
190 w.SetValue('') # set empty (default) value
191 w.SetModified() # mark as changed
192 elem = g.tree.dom.createElement(param)
193 # Some classes are special
194 if param == 'font':
195 xxx.params[param] = xxxParamFont(xxx.node, elem)
196 elif param in xxxObject.bitmapTags:
197 xxx.params[param] = xxxParamBitmap(elem)
198 else:
199 xxx.params[param] = xxxParam(elem)
200 # Find place to put new element: first present element after param
201 found = False
202 if xxx.hasStyle:
203 paramStyles = xxx.allParams + xxx.styles
204 else:
205 paramStyles = xxx.allParams
206 for p in paramStyles[paramStyles.index(param) + 1:]:
207 # Content params don't have same type
208 if xxx.params.has_key(p) and p != 'content':
209 found = True
210 break
211 if found:
212 nextTextElem = xxx.params[p].node
213 objElem.insertBefore(elem, nextTextElem)
214 else:
215 objElem.appendChild(elem)
216 else:
217 # Remove parameter
218 xxx.params[param].remove()
219 del xxx.params[param]
220 w.SetValue('')
221 w.SetModified(False) # mark as not changed
222 w.Enable(False)
223 # Set modified flag (provokes undo storing is necessary)
224 panel.SetModified(True)
225 def Apply(self):
226 xxx = self.xxx
227 if self.controlName:
228 name = self.controlName.GetValue()
229 if xxx.name != name:
230 xxx.name = name
231 xxx.node.setAttribute('name', name)
232 for param, w in self.controls.items():
233 if w.modified:
234 paramObj = xxx.params[param]
235 value = w.GetValue()
236 if param in xxx.specials:
237 xxx.setSpecial(param, value)
238 else:
239 paramObj.update(value)
240
241 # Save current state
242 def SaveState(self):
243 self.origChecks = map(lambda i: (i[0], i[1].GetValue()), self.checks.items())
244 self.origControls = map(lambda i: (i[0], i[1].GetValue(), i[1].IsEnabled()),
245 self.controls.items())
246 if self.controlName:
247 self.origName = self.controlName.GetValue()
248 # Return original values
249 def GetState(self):
250 if self.controlName:
251 return (self.origChecks, self.origControls, self.origName)
252 else:
253 return (self.origChecks, self.origControls)
254 # Set values from undo data
255 def SetState(self, state):
256 for k,v in state[0]:
257 self.checks[k].SetValue(v)
258 for k,v,e in state[1]:
259 self.controls[k].SetValue(v)
260 self.controls[k].Enable(e)
261 if e: self.controls[k].modified = True
262 if self.controlName:
263 self.controlName.SetValue(state[2])
264
265 ################################################################################
266
267 LABEL_WIDTH = 125
268
269 # Panel for displaying properties
270 class PropPage(ParamPage):
271 def __init__(self, parent, label, xxx):
272 ParamPage.__init__(self, parent, xxx)
273 self.box = wx.StaticBox(self, -1, label)
274 self.box.SetFont(g.labelFont())
275 topSizer = wx.StaticBoxSizer(self.box, wx.VERTICAL)
276 sizer = wx.FlexGridSizer(len(xxx.allParams), 2, 0, 1)
277 sizer.AddGrowableCol(1)
278 if xxx.hasName:
279 label = wx.StaticText(self, -1, 'XML ID:', size=(LABEL_WIDTH,-1))
280 control = ParamText(self, 'XML_name', 200)
281 sizer.AddMany([ (label, 0, wx.ALIGN_CENTER_VERTICAL),
282 (control, 0, wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM | wx.GROW, 10) ])
283 self.controlName = control
284 for param in xxx.allParams:
285 present = xxx.params.has_key(param)
286 if param in xxx.required:
287 if isinstance(xxx, xxxComment):
288 label = None
289 else:
290 label = wx.StaticText(self, paramIDs[param], param + ':',
291 size = (LABEL_WIDTH,-1), name = param)
292 else:
293 # Notebook has one very loooooong parameter
294 if param == 'usenotebooksizer': sParam = 'usesizer:'
295 else: sParam = param + ':'
296 label = wx.CheckBox(self, paramIDs[param], sParam,
297 size = (LABEL_WIDTH,-1), name = param)
298 self.checks[param] = label
299 try:
300 typeClass = xxx.paramDict[param]
301 except KeyError:
302 try:
303 # Standart type
304 typeClass = paramDict[param]
305 except KeyError:
306 # Default
307 typeClass = ParamText
308 control = typeClass(self, param)
309 control.Enable(present)
310 # Comment has only one parameter
311 if isinstance(xxx, xxxComment):
312 # Bind char event to check Enter key
313 control.text.Bind(wx.EVT_CHAR, self.OnEnter)
314 sizer.Add(control, 0, wx.ALIGN_CENTER_VERTICAL | wx.GROW)
315 else:
316 sizer.AddMany([ (label, 0, wx.ALIGN_CENTER_VERTICAL),
317 (control, 0, wx.ALIGN_CENTER_VERTICAL | wx.GROW) ])
318 self.controls[param] = control
319 topSizer.Add(sizer, 1, wx.ALL | wx.EXPAND, 3)
320 self.SetSizer(topSizer)
321 topSizer.Fit(self)
322
323 def SetValues(self, xxx):
324 self.xxx = xxx
325 self.origChecks = []
326 self.origControls = []
327 # Set values, checkboxes to False, disable defaults
328 if xxx.hasName:
329 self.controlName.SetValue(xxx.name)
330 self.origName = xxx.name
331 for param in xxx.allParams:
332 w = self.controls[param]
333 w.modified = False
334 try:
335 value = xxx.params[param].value()
336 w.Enable(True)
337 w.SetValue(value)
338 if not param in xxx.required:
339 self.checks[param].SetValue(True)
340 self.origChecks.append((param, True))
341 self.origControls.append((param, value, True))
342 except KeyError:
343 self.checks[param].SetValue(False)
344 w.SetValue('')
345 w.Enable(False)
346 self.origChecks.append((param, False))
347 self.origControls.append((param, '', False))
348
349 # This is called only for comment now
350 def OnEnter(self, evt):
351 if evt.GetKeyCode() == 13:
352 g.tree.Apply(self.xxx, g.tree.selection)
353 else:
354 evt.Skip()
355
356 ################################################################################
357
358 # Style notebook page
359 class StylePage(ParamPage):
360 def __init__(self, parent, label, xxx):
361 ParamPage.__init__(self, parent, xxx)
362 box = wx.StaticBox(self, -1, label)
363 box.SetFont(g.labelFont())
364 topSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
365 sizer = wx.FlexGridSizer(len(xxx.styles), 2, 0, 1)
366 sizer.AddGrowableCol(1)
367 for param in xxx.styles:
368 present = xxx.params.has_key(param)
369 check = wx.CheckBox(self, paramIDs[param],
370 param + ':', size = (LABEL_WIDTH,-1), name = param)
371 check.SetValue(present)
372 control = paramDict[param](self, name = param)
373 control.Enable(present)
374 sizer.AddMany([ (check, 0, wx.ALIGN_CENTER_VERTICAL),
375 (control, 0, wx.ALIGN_CENTER_VERTICAL | wx.GROW) ])
376 self.checks[param] = check
377 self.controls[param] = control
378 topSizer.Add(sizer, 1, wx.ALL | wx.EXPAND, 3)
379 self.SetAutoLayout(True)
380 self.SetSizer(topSizer)
381 topSizer.Fit(self)
382
383 # Set data for a cahced page
384 def SetValues(self, xxx):
385 self.xxx = xxx
386 self.origChecks = []
387 self.origControls = []
388 for param in xxx.styles:
389 present = xxx.params.has_key(param)
390 check = self.checks[param]
391 check.SetValue(present)
392 w = self.controls[param]
393 w.modified = False
394 if present:
395 value = xxx.params[param].value()
396 else:
397 value = ''
398 w.SetValue(value)
399 w.Enable(present)
400 self.origChecks.append((param, present))
401 self.origControls.append((param, value, present))
402