xref: /aoo42x/main/toolkit/src2xml/source/boxer.py (revision 77de67d5)
19f22d7c2SAndrew Rist# *************************************************************
29f22d7c2SAndrew Rist#
39f22d7c2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49f22d7c2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59f22d7c2SAndrew Rist#  distributed with this work for additional information
69f22d7c2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79f22d7c2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
89f22d7c2SAndrew Rist#  "License"); you may not use this file except in compliance
99f22d7c2SAndrew Rist#  with the License.  You may obtain a copy of the License at
109f22d7c2SAndrew Rist#
119f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
129f22d7c2SAndrew Rist#
139f22d7c2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
149f22d7c2SAndrew Rist#  software distributed under the License is distributed on an
159f22d7c2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f22d7c2SAndrew Rist#  KIND, either express or implied.  See the License for the
179f22d7c2SAndrew Rist#  specific language governing permissions and limitations
189f22d7c2SAndrew Rist#  under the License.
199f22d7c2SAndrew Rist#
209f22d7c2SAndrew Rist# *************************************************************
219f22d7c2SAndrew 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')
41*77de67d5SPedro Giffuni        ys = sorted(self.rows.keys())
42cdf0e10cSrcweir        for y in ys:
43*77de67d5SPedro Giffuni            xs = sorted(self.rows[y].keys())
44cdf0e10cSrcweir
45cdf0e10cSrcweir            if len(xs) == 1:
46cdf0e10cSrcweir                root.appendChild(self.rows[y][xs[0]])
47cdf0e10cSrcweir                continue
48cdf0e10cSrcweir
49cdf0e10cSrcweir            hbox = Element('hbox')
50cdf0e10cSrcweir            root.appendChild(hbox)
51cdf0e10cSrcweir            for x in xs:
52cdf0e10cSrcweir                elem = self.rows[y][x]
53cdf0e10cSrcweir                hbox.appendChild(elem)
54cdf0e10cSrcweir
55cdf0e10cSrcweir        return root
56cdf0e10cSrcweir
57cdf0e10cSrcweir
58cdf0e10cSrcweirclass Boxer(object):
59cdf0e10cSrcweir    def __init__ (self, root):
60cdf0e10cSrcweir        self.root = root
61cdf0e10cSrcweir
62cdf0e10cSrcweir    def layout (self):
63cdf0e10cSrcweir
64cdf0e10cSrcweir        newroot = RootNode()
65cdf0e10cSrcweir        for dlgnode in self.root.children:
66cdf0e10cSrcweir            newdlgnode = self.__walkDlgNode(dlgnode)
67cdf0e10cSrcweir            newroot.children.append(newdlgnode)
68cdf0e10cSrcweir
69cdf0e10cSrcweir        return newroot
70cdf0e10cSrcweir
71cdf0e10cSrcweir    def __walkDlgNode (self, dlgnode):
72cdf0e10cSrcweir
73cdf0e10cSrcweir        newnode = Element(dlgnode.name)
74cdf0e10cSrcweir        newnode.clone(dlgnode)
75cdf0e10cSrcweir        if dlgnode.name == 'string':
76cdf0e10cSrcweir            return newnode
77cdf0e10cSrcweir        newnode.setAttr("xmlns", "http://openoffice.org/2007/layout")
78cdf0e10cSrcweir        newnode.setAttr("xmlns:cnt", "http://openoffice.org/2007/layout/container")
79cdf0e10cSrcweir        mx = DlgLayoutBuilder(newnode)
80cdf0e10cSrcweir
81cdf0e10cSrcweir        # Each dialog node is expected to have a flat list of widgets.
82cdf0e10cSrcweir        for widget in dlgnode.children:
83cdf0e10cSrcweir            if widget.hasAttr('x') and widget.hasAttr('y'):
84cdf0e10cSrcweir                mx.addWidget(widget)
85cdf0e10cSrcweir            else:
86cdf0e10cSrcweir                newnode.appendChild(widget)
87cdf0e10cSrcweir
88cdf0e10cSrcweir        vbox = mx.build()
89cdf0e10cSrcweir        if len(vbox.children) > 0:
90cdf0e10cSrcweir            newnode.appendChild(vbox)
91cdf0e10cSrcweir
92cdf0e10cSrcweir        return newnode
93