File: MenuTree.py

package info (click to toggle)
wxglade 1%3A1.1.1%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,592 kB
  • sloc: python: 30,644; javascript: 740; makefile: 169; cpp: 99; perl: 90; lisp: 62; xml: 61; sh: 3
file content (82 lines) | stat: -rw-r--r-- 3,025 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""\
A class to represent a menu on a wxMenuBar

@copyright: 2002-2007 Alberto Griggio
@copyright: 2016 Carsten Grohmann
@copyright: 2017 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""

from common import format_xml_tag
import compat

__all__ = ['MenuTree']

class MenuTree(object):
    "A class to represent a menu on a wxMenuBar"
    class Node:
        # the following are for compatibility with the tree structure, used by format_generic_access
        IS_CLASS = IS_SIZER = False
        klass = classname = None
        properties = []
        IS_NAMED = True
        WX_CLASS = None  # for Perl
        def __init__(self, label="", id="", name="", help_str="", checkable="", radio="", handler=""):
            self.label = label
            self.id = id
            self.name = name
            self.help_str = help_str
            self.checkable = checkable
            self.radio = radio
            self.handler = handler
            self.children = []
            self.parent = None

        def write(self, output, tabs, top=False):
            inner_xml = []
            if not top and not self.children:
                if self.label:
                    inner_xml += format_xml_tag(u'label', self.label, tabs+1)
                if self.id:
                    inner_xml += format_xml_tag(u'id', self.id, tabs+1)
                if self.name:
                    inner_xml += format_xml_tag(u'name', self.name, tabs+1)
                if self.help_str:
                    inner_xml += format_xml_tag(u'help_str', self.help_str, tabs+1)
                try:
                    checkable = int(self.checkable)
                except ValueError:
                    checkable = 0
                if checkable:
                    inner_xml += format_xml_tag(u'checkable', checkable, tabs+1)

                try:
                    radio = int(self.radio)
                except ValueError:
                    radio = 0
                if radio:
                    inner_xml += format_xml_tag(u'radio', radio, tabs+1)

                if self.handler:
                    inner_xml += format_xml_tag(u'handler', self.handler, tabs+1)
                output.extend( format_xml_tag(u'item', inner_xml, tabs, is_xml=True) )
            else:
                attrs = {'name': self.name}
                if self.id:
                    attrs[u'itemid'] = self.id
                if self.handler:
                    attrs[u'handler'] = self.handler
                attrs[u'label'] = self.label
                inner_xml = []
                for c in self.children:
                    c.write(inner_xml, tabs + 1)
                output.extend( format_xml_tag( u'menu', inner_xml, tabs, is_xml=True, **attrs ) )

    #end of class Node

    def __init__(self, name, label, id="", help_str="", handler=""):
        self.root = self.Node(label, id, name, help_str, handler=handler)

    def write(self, output, tabs):
        self.root.write(output, tabs, top=True)