merge the formfield patch from ooo-build
[ooovba.git] / toolkit / src2xml / source / boxer.py
blob1b716d7572990a37ed3b446c35632a6183337f1d
2 from globals import *
5 class DlgLayoutBuilder(object):
6 def __init__ (self, dlgnode):
7 self.dlgnode = dlgnode
8 self.rows = {}
10 def addWidget (self, elem):
11 x, y = int(elem.getAttr('x')), int(elem.getAttr('y'))
12 self.rows[y] = self.rows.get (y, {})
13 while self.rows[y].has_key(x):
14 y += 1
15 self.rows[y] = self.rows.get (y, {})
16 self.rows[y][x] = elem
18 def build (self):
19 root = Element('vbox')
20 ys = self.rows.keys()
21 ys.sort()
22 for y in ys:
23 xs = self.rows[y].keys()
24 xs.sort()
26 if len(xs) == 1:
27 root.appendChild(self.rows[y][xs[0]])
28 continue
30 hbox = Element('hbox')
31 root.appendChild(hbox)
32 for x in xs:
33 elem = self.rows[y][x]
34 hbox.appendChild(elem)
36 return root
39 class Boxer(object):
40 def __init__ (self, root):
41 self.root = root
43 def layout (self):
45 newroot = RootNode()
46 for dlgnode in self.root.children:
47 newdlgnode = self.__walkDlgNode(dlgnode)
48 newroot.children.append(newdlgnode)
50 return newroot
52 def __walkDlgNode (self, dlgnode):
54 newnode = Element(dlgnode.name)
55 newnode.clone(dlgnode)
56 if dlgnode.name == 'string':
57 return newnode
58 newnode.setAttr("xmlns", "http://openoffice.org/2007/layout")
59 newnode.setAttr("xmlns:cnt", "http://openoffice.org/2007/layout/container")
60 mx = DlgLayoutBuilder(newnode)
62 # Each dialog node is expected to have a flat list of widgets.
63 for widget in dlgnode.children:
64 if widget.hasAttr('x') and widget.hasAttr('y'):
65 mx.addWidget(widget)
66 else:
67 newnode.appendChild(widget)
69 vbox = mx.build()
70 if len(vbox.children) > 0:
71 newnode.appendChild(vbox)
73 return newnode