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