]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/tools/XRCed/panel.py
wxCheckListBox doesn't require wxUSE_OWNER_DRAWN when using WXUNIVERSAL
[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)
2871fade 40 self.page2.Hide()
d14a1e28
RD
41 sizer = wxBoxSizer()
42 sizer.Add(wxBoxSizer()) # dummy sizer
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
RD
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
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)
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()
a4c013b2
RR
90 # Normal or SizerItem page
91 isGBSizerItem = isinstance(xxx.parent, xxxGridBagSizer)
5cd66f07
RD
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
d14a1e28
RD
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
5cd66f07
RD
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
d14a1e28
RD
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)
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)
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()
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())
163 self.modified = value
5cd66f07 164
d14a1e28
RD
165 def Apply(self):
166 for p in self.pages: p.Apply()
167
168################################################################################
169
170# General class for notebook pages
171class ParamPage(wxPanel):
172 def __init__(self, parent, xxx):
173 wxPanel.__init__(self, parent, -1)
b09afc93
RD
174 self.SetBackgroundColour(parent.GetBackgroundColour())
175 self.SetForegroundColour(parent.GetForegroundColour())
d14a1e28
RD
176 self.xxx = xxx
177 # Register event handlers
178 for id in paramIDs.values():
179 EVT_CHECKBOX(self, id, self.OnCheckParams)
180 self.checks = {}
181 self.controls = {} # save python objects
182 self.controlName = None
183
184 def OnCheckParams(self, evt):
185 xxx = self.xxx
186 param = evt.GetEventObject().GetName()
187 w = self.controls[param]
188 w.Enable(True)
189 objElem = xxx.element
190 if evt.IsChecked():
191 # Ad new text node in order of allParams
192 w.SetValue('') # set empty (default) value
193 w.SetModified() # mark as changed
194 elem = g.tree.dom.createElement(param)
195 # Some classes are special
196 if param == 'font':
197 xxx.params[param] = xxxParamFont(xxx.element, elem)
198 elif param in xxxObject.bitmapTags:
199 xxx.params[param] = xxxParamBitmap(elem)
200 else:
201 xxx.params[param] = xxxParam(elem)
202 # Find place to put new element: first present element after param
203 found = False
a3c8bbc1
RD
204 if xxx.hasStyle:
205 paramStyles = xxx.allParams + xxx.styles
206 else:
207 paramStyles = xxx.allParams
d14a1e28
RD
208 for p in paramStyles[paramStyles.index(param) + 1:]:
209 # Content params don't have same type
210 if xxx.params.has_key(p) and p != 'content':
211 found = True
212 break
213 if found:
214 nextTextElem = xxx.params[p].node
215 objElem.insertBefore(elem, nextTextElem)
216 else:
217 objElem.appendChild(elem)
218 else:
219 # Remove parameter
220 xxx.params[param].remove()
221 del xxx.params[param]
222 w.SetValue('')
223 w.modified = False # mark as not changed
224 w.Enable(False)
225 # Set modified flag (provokes undo storing is necessary)
226 panel.SetModified(True)
227 def Apply(self):
228 xxx = self.xxx
229 if self.controlName:
230 name = self.controlName.GetValue()
231 if xxx.name != name:
232 xxx.name = name
233 xxx.element.setAttribute('name', name)
234 for param, w in self.controls.items():
235 if w.modified:
236 paramObj = xxx.params[param]
237 value = w.GetValue()
238 if param in xxx.specials:
239 xxx.setSpecial(param, value)
240 else:
241 paramObj.update(value)
242 # Save current state
243 def SaveState(self):
244 self.origChecks = map(lambda i: (i[0], i[1].GetValue()), self.checks.items())
245 self.origControls = map(lambda i: (i[0], i[1].GetValue(), i[1].IsEnabled()),
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)
262 if e: self.controls[k].modified = True
263 if self.controlName:
264 self.controlName.SetValue(state[2])
265
266################################################################################
267
a78714eb
RD
268LABEL_WIDTH = 125
269
d14a1e28
RD
270# Panel for displaying properties
271class PropPage(ParamPage):
272 def __init__(self, parent, label, xxx):
273 ParamPage.__init__(self, parent, xxx)
2481bf3c 274 self.box = wxStaticBox(self, -1, label)
289128a4 275 self.box.SetFont(g.labelFont())
2481bf3c 276 topSizer = wxStaticBoxSizer(self.box, wxVERTICAL)
d14a1e28
RD
277 sizer = wxFlexGridSizer(len(xxx.allParams), 2, 1, 1)
278 sizer.AddGrowableCol(1)
279 if xxx.hasName:
a78714eb 280 label = wxStaticText(self, -1, 'XML ID:', size=(LABEL_WIDTH,-1))
d14a1e28
RD
281 control = ParamText(self, 'XML_name', 200)
282 sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL),
283 (control, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM | wxGROW, 5) ])
284 self.controlName = control
285 for param in xxx.allParams:
286 present = xxx.params.has_key(param)
287 if param in xxx.required:
288 label = wxStaticText(self, paramIDs[param], param + ':',
a78714eb 289 size = (LABEL_WIDTH,-1), name = param)
d14a1e28
RD
290 else:
291 # Notebook has one very loooooong parameter
292 if param == 'usenotebooksizer': sParam = 'usesizer:'
293 else: sParam = param + ':'
294 label = wxCheckBox(self, paramIDs[param], sParam,
a78714eb 295 size = (LABEL_WIDTH,-1), name = param)
d14a1e28
RD
296 self.checks[param] = label
297 try:
298 typeClass = xxx.paramDict[param]
299 except KeyError:
300 try:
301 # Standart type
302 typeClass = paramDict[param]
303 except KeyError:
304 # Default
305 typeClass = ParamText
306 control = typeClass(self, param)
307 control.Enable(present)
308 sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL),
309 (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ])
310 self.controls[param] = control
311 topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3)
312 self.SetAutoLayout(True)
313 self.SetSizer(topSizer)
314 topSizer.Fit(self)
315 def SetValues(self, xxx):
316 self.xxx = xxx
317 self.origChecks = []
318 self.origControls = []
319 # Set values, checkboxes to False, disable defaults
320 if xxx.hasName:
321 self.controlName.SetValue(xxx.name)
322 self.origName = xxx.name
323 for param in xxx.allParams:
324 w = self.controls[param]
325 w.modified = False
326 try:
327 value = xxx.params[param].value()
328 w.Enable(True)
329 w.SetValue(value)
330 if not param in xxx.required:
331 self.checks[param].SetValue(True)
332 self.origChecks.append((param, True))
333 self.origControls.append((param, value, True))
334 except KeyError:
335 self.checks[param].SetValue(False)
336 w.SetValue('')
337 w.Enable(False)
338 self.origChecks.append((param, False))
339 self.origControls.append((param, '', False))
340
341################################################################################
342
343# Style notebook page
344class StylePage(ParamPage):
345 def __init__(self, parent, label, xxx):
346 ParamPage.__init__(self, parent, xxx)
347 box = wxStaticBox(self, -1, label)
289128a4 348 box.SetFont(g.labelFont())
d14a1e28
RD
349 topSizer = wxStaticBoxSizer(box, wxVERTICAL)
350 sizer = wxFlexGridSizer(len(xxx.styles), 2, 1, 1)
351 sizer.AddGrowableCol(1)
352 for param in xxx.styles:
353 present = xxx.params.has_key(param)
354 check = wxCheckBox(self, paramIDs[param],
a78714eb 355 param + ':', size = (LABEL_WIDTH,-1), name = param)
d14a1e28
RD
356 check.SetValue(present)
357 control = paramDict[param](self, name = param)
358 control.Enable(present)
359 sizer.AddMany([ (check, 0, wxALIGN_CENTER_VERTICAL),
360 (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ])
361 self.checks[param] = check
362 self.controls[param] = control
363 topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3)
364 self.SetAutoLayout(True)
365 self.SetSizer(topSizer)
366 topSizer.Fit(self)
367 # Set data for a cahced page
368 def SetValues(self, xxx):
369 self.xxx = xxx
370 self.origChecks = []
371 self.origControls = []
372 for param in xxx.styles:
373 present = xxx.params.has_key(param)
374 check = self.checks[param]
375 check.SetValue(present)
376 w = self.controls[param]
377 w.modified = False
378 if present:
379 value = xxx.params[param].value()
380 else:
381 value = ''
382 w.SetValue(value)
383 w.Enable(present)
384 self.origChecks.append((param, present))
385 self.origControls.append((param, value, present))
1fded56b 386