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