]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/panel.py
fixed spacer image
[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 self.page2.Hide()
41 sizer = wxBoxSizer()
42 sizer.Add(wxBoxSizer()) # 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 = 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
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):
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 # Normal or SizerItem page
91 isGBSizerItem = isinstance(xxx.parent, xxxGridBagSizer)
92 cacheID = (xxx.__class__, 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, wxEXPAND)
103 if xxx.hasChild:
104 # Special label for child objects - they may have different GUI
105 cacheID = (xxx.child.__class__, 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, wxEXPAND | wxTOP, 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, wxEXPAND)
135 # Add page if not exists
136 if not self.GetPageCount() == 2:
137 self.AddPage(self.page2, 'Style')
138 self.page2.Layout()
139 size = self.page2.GetSizer().GetMinSize()
140 self.page2.SetScrollbars(1, 1, size.width, size.height, 0, 0, True)
141 else:
142 # Remove page if exists
143 if self.GetPageCount() == 2:
144 self.SetSelection(0)
145 self.page1.Refresh()
146 self.RemovePage(1)
147 self.modified = False
148
149 def Clear(self):
150 self.SetData(None)
151 self.modified = False
152
153 # If some parameter has changed
154 def IsModified(self):
155 return self.modified
156
157 def SetModified(self, value):
158 # Register undo object when modifying first time
159 if not self.modified and value:
160 g.undoMan.RegisterUndo(UndoEdit())
161 self.modified = value
162
163 def Apply(self):
164 for p in self.pages: p.Apply()
165
166 ################################################################################
167
168 # General class for notebook pages
169 class ParamPage(wxPanel):
170 def __init__(self, parent, xxx):
171 wxPanel.__init__(self, parent, -1)
172 self.SetBackgroundColour(parent.GetBackgroundColour())
173 self.SetForegroundColour(parent.GetForegroundColour())
174 self.xxx = xxx
175 # Register event handlers
176 for id in paramIDs.values():
177 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.element
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.element, 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 paramStyles = xxx.allParams + xxx.styles
203 for p in paramStyles[paramStyles.index(param) + 1:]:
204 # Content params don't have same type
205 if xxx.params.has_key(p) and p != 'content':
206 found = True
207 break
208 if found:
209 nextTextElem = xxx.params[p].node
210 objElem.insertBefore(elem, nextTextElem)
211 else:
212 objElem.appendChild(elem)
213 else:
214 # Remove parameter
215 xxx.params[param].remove()
216 del xxx.params[param]
217 w.SetValue('')
218 w.modified = False # mark as not changed
219 w.Enable(False)
220 # Set modified flag (provokes undo storing is necessary)
221 panel.SetModified(True)
222 def Apply(self):
223 xxx = self.xxx
224 if self.controlName:
225 name = self.controlName.GetValue()
226 if xxx.name != name:
227 xxx.name = name
228 xxx.element.setAttribute('name', name)
229 for param, w in self.controls.items():
230 if w.modified:
231 paramObj = xxx.params[param]
232 value = w.GetValue()
233 if param in xxx.specials:
234 xxx.setSpecial(param, value)
235 else:
236 paramObj.update(value)
237 # Save current state
238 def SaveState(self):
239 self.origChecks = map(lambda i: (i[0], i[1].GetValue()), self.checks.items())
240 self.origControls = map(lambda i: (i[0], i[1].GetValue(), i[1].IsEnabled()),
241 self.controls.items())
242 if self.controlName:
243 self.origName = self.controlName.GetValue()
244 # Return original values
245 def GetState(self):
246 if self.controlName:
247 return (self.origChecks, self.origControls, self.origName)
248 else:
249 return (self.origChecks, self.origControls)
250 # Set values from undo data
251 def SetState(self, state):
252 for k,v in state[0]:
253 self.checks[k].SetValue(v)
254 for k,v,e in state[1]:
255 self.controls[k].SetValue(v)
256 self.controls[k].Enable(e)
257 if e: self.controls[k].modified = True
258 if self.controlName:
259 self.controlName.SetValue(state[2])
260
261 ################################################################################
262
263 LABEL_WIDTH = 125
264
265 # Panel for displaying properties
266 class PropPage(ParamPage):
267 def __init__(self, parent, label, xxx):
268 ParamPage.__init__(self, parent, xxx)
269 self.box = wxStaticBox(self, -1, label)
270 self.box.SetFont(g.labelFont())
271 topSizer = wxStaticBoxSizer(self.box, wxVERTICAL)
272 sizer = wxFlexGridSizer(len(xxx.allParams), 2, 1, 1)
273 sizer.AddGrowableCol(1)
274 if xxx.hasName:
275 label = wxStaticText(self, -1, 'XML ID:', size=(LABEL_WIDTH,-1))
276 control = ParamText(self, 'XML_name', 200)
277 sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL),
278 (control, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM | wxGROW, 5) ])
279 self.controlName = control
280 for param in xxx.allParams:
281 present = xxx.params.has_key(param)
282 if param in xxx.required:
283 label = wxStaticText(self, paramIDs[param], param + ':',
284 size = (LABEL_WIDTH,-1), name = param)
285 else:
286 # Notebook has one very loooooong parameter
287 if param == 'usenotebooksizer': sParam = 'usesizer:'
288 else: sParam = param + ':'
289 label = wxCheckBox(self, paramIDs[param], sParam,
290 size = (LABEL_WIDTH,-1), name = param)
291 self.checks[param] = label
292 try:
293 typeClass = xxx.paramDict[param]
294 except KeyError:
295 try:
296 # Standart type
297 typeClass = paramDict[param]
298 except KeyError:
299 # Default
300 typeClass = ParamText
301 control = typeClass(self, param)
302 control.Enable(present)
303 sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL),
304 (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ])
305 self.controls[param] = control
306 topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3)
307 self.SetAutoLayout(True)
308 self.SetSizer(topSizer)
309 topSizer.Fit(self)
310 def SetValues(self, xxx):
311 self.xxx = xxx
312 self.origChecks = []
313 self.origControls = []
314 # Set values, checkboxes to False, disable defaults
315 if xxx.hasName:
316 self.controlName.SetValue(xxx.name)
317 self.origName = xxx.name
318 for param in xxx.allParams:
319 w = self.controls[param]
320 w.modified = False
321 try:
322 value = xxx.params[param].value()
323 w.Enable(True)
324 w.SetValue(value)
325 if not param in xxx.required:
326 self.checks[param].SetValue(True)
327 self.origChecks.append((param, True))
328 self.origControls.append((param, value, True))
329 except KeyError:
330 self.checks[param].SetValue(False)
331 w.SetValue('')
332 w.Enable(False)
333 self.origChecks.append((param, False))
334 self.origControls.append((param, '', False))
335
336 ################################################################################
337
338 # Style notebook page
339 class StylePage(ParamPage):
340 def __init__(self, parent, label, xxx):
341 ParamPage.__init__(self, parent, xxx)
342 box = wxStaticBox(self, -1, label)
343 box.SetFont(g.labelFont())
344 topSizer = wxStaticBoxSizer(box, wxVERTICAL)
345 sizer = wxFlexGridSizer(len(xxx.styles), 2, 1, 1)
346 sizer.AddGrowableCol(1)
347 for param in xxx.styles:
348 present = xxx.params.has_key(param)
349 check = wxCheckBox(self, paramIDs[param],
350 param + ':', size = (LABEL_WIDTH,-1), name = param)
351 check.SetValue(present)
352 control = paramDict[param](self, name = param)
353 control.Enable(present)
354 sizer.AddMany([ (check, 0, wxALIGN_CENTER_VERTICAL),
355 (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ])
356 self.checks[param] = check
357 self.controls[param] = control
358 topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3)
359 self.SetAutoLayout(True)
360 self.SetSizer(topSizer)
361 topSizer.Fit(self)
362 # Set data for a cahced page
363 def SetValues(self, xxx):
364 self.xxx = xxx
365 self.origChecks = []
366 self.origControls = []
367 for param in xxx.styles:
368 present = xxx.params.has_key(param)
369 check = self.checks[param]
370 check.SetValue(present)
371 w = self.controls[param]
372 w.modified = False
373 if present:
374 value = xxx.params[param].value()
375 else:
376 value = ''
377 w.SetValue(value)
378 w.Enable(present)
379 self.origChecks.append((param, present))
380 self.origControls.append((param, value, present))
381