xref: /AOO41X/main/toolkit/src2xml/source/boxer.py (revision 9f22d7c2b35e9612da695275f00a04cc0e734348)
1*9f22d7c2SAndrew Rist# *************************************************************
2*9f22d7c2SAndrew Rist#
3*9f22d7c2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*9f22d7c2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*9f22d7c2SAndrew Rist#  distributed with this work for additional information
6*9f22d7c2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*9f22d7c2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*9f22d7c2SAndrew Rist#  "License"); you may not use this file except in compliance
9*9f22d7c2SAndrew Rist#  with the License.  You may obtain a copy of the License at
10*9f22d7c2SAndrew Rist#
11*9f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*9f22d7c2SAndrew Rist#
13*9f22d7c2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*9f22d7c2SAndrew Rist#  software distributed under the License is distributed on an
15*9f22d7c2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f22d7c2SAndrew Rist#  KIND, either express or implied.  See the License for the
17*9f22d7c2SAndrew Rist#  specific language governing permissions and limitations
18*9f22d7c2SAndrew Rist#  under the License.
19*9f22d7c2SAndrew Rist#
20*9f22d7c2SAndrew Rist# *************************************************************
21*9f22d7c2SAndrew Rist
22cdf0e10cSrcweir
23cdf0e10cSrcweirfrom globals import *
24cdf0e10cSrcweir
25cdf0e10cSrcweir
26cdf0e10cSrcweirclass DlgLayoutBuilder(object):
27cdf0e10cSrcweir    def __init__ (self, dlgnode):
28cdf0e10cSrcweir        self.dlgnode = dlgnode
29cdf0e10cSrcweir        self.rows = {}
30cdf0e10cSrcweir
31cdf0e10cSrcweir    def addWidget (self, elem):
32cdf0e10cSrcweir        x, y = int(elem.getAttr('x')), int(elem.getAttr('y'))
33cdf0e10cSrcweir        self.rows[y] = self.rows.get (y, {})
34cdf0e10cSrcweir        while self.rows[y].has_key(x):
35cdf0e10cSrcweir            y += 1
36cdf0e10cSrcweir            self.rows[y] = self.rows.get (y, {})
37cdf0e10cSrcweir        self.rows[y][x] = elem
38cdf0e10cSrcweir
39cdf0e10cSrcweir    def build (self):
40cdf0e10cSrcweir        root = Element('vbox')
41cdf0e10cSrcweir        ys = self.rows.keys()
42cdf0e10cSrcweir        ys.sort()
43cdf0e10cSrcweir        for y in ys:
44cdf0e10cSrcweir            xs = self.rows[y].keys()
45cdf0e10cSrcweir            xs.sort()
46cdf0e10cSrcweir
47cdf0e10cSrcweir            if len(xs) == 1:
48cdf0e10cSrcweir                root.appendChild(self.rows[y][xs[0]])
49cdf0e10cSrcweir                continue
50cdf0e10cSrcweir
51cdf0e10cSrcweir            hbox = Element('hbox')
52cdf0e10cSrcweir            root.appendChild(hbox)
53cdf0e10cSrcweir            for x in xs:
54cdf0e10cSrcweir                elem = self.rows[y][x]
55cdf0e10cSrcweir                hbox.appendChild(elem)
56cdf0e10cSrcweir
57cdf0e10cSrcweir        return root
58cdf0e10cSrcweir
59cdf0e10cSrcweir
60cdf0e10cSrcweirclass Boxer(object):
61cdf0e10cSrcweir    def __init__ (self, root):
62cdf0e10cSrcweir        self.root = root
63cdf0e10cSrcweir
64cdf0e10cSrcweir    def layout (self):
65cdf0e10cSrcweir
66cdf0e10cSrcweir        newroot = RootNode()
67cdf0e10cSrcweir        for dlgnode in self.root.children:
68cdf0e10cSrcweir            newdlgnode = self.__walkDlgNode(dlgnode)
69cdf0e10cSrcweir            newroot.children.append(newdlgnode)
70cdf0e10cSrcweir
71cdf0e10cSrcweir        return newroot
72cdf0e10cSrcweir
73cdf0e10cSrcweir    def __walkDlgNode (self, dlgnode):
74cdf0e10cSrcweir
75cdf0e10cSrcweir        newnode = Element(dlgnode.name)
76cdf0e10cSrcweir        newnode.clone(dlgnode)
77cdf0e10cSrcweir        if dlgnode.name == 'string':
78cdf0e10cSrcweir            return newnode
79cdf0e10cSrcweir        newnode.setAttr("xmlns", "http://openoffice.org/2007/layout")
80cdf0e10cSrcweir        newnode.setAttr("xmlns:cnt", "http://openoffice.org/2007/layout/container")
81cdf0e10cSrcweir        mx = DlgLayoutBuilder(newnode)
82cdf0e10cSrcweir
83cdf0e10cSrcweir        # Each dialog node is expected to have a flat list of widgets.
84cdf0e10cSrcweir        for widget in dlgnode.children:
85cdf0e10cSrcweir            if widget.hasAttr('x') and widget.hasAttr('y'):
86cdf0e10cSrcweir                mx.addWidget(widget)
87cdf0e10cSrcweir            else:
88cdf0e10cSrcweir                newnode.appendChild(widget)
89cdf0e10cSrcweir
90cdf0e10cSrcweir        vbox = mx.build()
91cdf0e10cSrcweir        if len(vbox.children) > 0:
92cdf0e10cSrcweir            newnode.appendChild(vbox)
93cdf0e10cSrcweir
94cdf0e10cSrcweir        return newnode
95