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