File: menu.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (189 lines) | stat: -rw-r--r-- 6,826 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#---------------------------------------------------------------------------
# Name:        etg/menu.py
# Author:      Kevin Ollivier
#              Robin Dunn
#
# Created:     25-Aug-2011
# Copyright:   (c) 2011 by Wide Open Technologies
# Copyright:   (c) 2011-2020 by Total Control Software
# License:     wxWindows License
#---------------------------------------------------------------------------

import etgtools
import etgtools.tweaker_tools as tools

PACKAGE   = "wx"
MODULE    = "_core"
NAME      = "menu"   # Base name of the file to generate to for this script
DOCSTRING = ""

# The classes and/or the basename of the Doxygen XML files to be processed by
# this script.
ITEMS  = [ 'wxMenu', 'wxMenuBar' ]

#---------------------------------------------------------------------------

def run():
    # Parse the XML file(s) building a collection of Extractor objects
    module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
    etgtools.parseDoxyXML(module, ITEMS)

    #-----------------------------------------------------------------
    # Tweak the parsed meta objects in the module object as needed for
    # customizing the generated code and docstrings.

    def addTransferAnnotations(c, arg):
        for method in c.findAll('Append') + c.findAll('Insert') + \
                      c.findAll('Replace') + c.findAll('Prepend'):
            arg_def = method.findItem(arg)
            if arg_def:
                arg_def.transfer = True

        for method in c.findAll('Remove') + c.findAll('Replace'):
            method.transferBack = True


    #-----------------------------------------------------------------
    c = module.find('wxMenu')
    assert isinstance(c, etgtools.ClassDef)
    c.mustHaveApp()
    tools.removeVirtuals(c)
    c.find('Prepend.item').name = 'menuItem'
    c.find('Prepend.submenu').name = 'subMenu'

    addTransferAnnotations(c, 'menuItem')
    addTransferAnnotations(c, 'subMenu')
    c.find('AppendSubMenu.submenu').transfer = True
    c.find('Insert.submenu').transfer = True

    # We only need one of these overloads, the non-const/const is not enough
    # to distinguish a unique Python signature.
    c.find('GetMenuItems').overloads = []

    # Ensure that no copy is made of the list object, so we only wrap the
    # existing object and keep it owned by C++
    c.find('GetMenuItems').noCopy = True


    c.addPyMethod('AppendMenu', '(self, id, item, subMenu, help="")', deprecated='Use Append instead.',
                  body='return self.Append(id, item, subMenu, help)')
    c.addPyMethod('AppendItem', '(self, menuItem)', deprecated='Use Append instead.',
                  body='return self.Append(menuItem)')

    c.addPyMethod('InsertMenu', '(self, pos, id, item, subMenu, help="")', deprecated='Use Insert instead.',
                  body='return self.Insert(pos, id, item, subMenu, help)')
    c.addPyMethod('InsertItem', '(self, pos, menuItem)', deprecated='Use Insert instead.',
                  body='return self.Insert(pos, menuItem)')

    c.addPyMethod('PrependMenu', '(self, id, item, subMenu, help="")', deprecated='Use Prepend instead.',
                  body='return self.Prepend(id, item, subMenu, help)')
    c.addPyMethod('PrependItem', '(self, menuItem)', deprecated='Use Prepend instead.',
                  body='return self.Prepend(menuItem)')

    c.addPyMethod('RemoveMenu', '(self, id, item, subMenu, help="")', deprecated='Use Remove instead.',
                  body='return self.Remove(id, item, subMenu, help)')
    c.addPyMethod('RemoveItem', '(self, menuItem)', deprecated='Use Remove instead.',
                  body='return self.Remove(menuItem)')

    # Don't hide the Destroy inherited from wxObject
    c.find('Destroy').findOverload('int').pyName = 'DestroyItem'
    c.find('Destroy').findOverload('wxMenuItem').pyName = 'DestroyItem'

    c.find('FindChildItem.pos').out = True
    c.find('FindItem.menu').out = True
    c.addCppMethod('wxMenuItem*', 'FindItemById', '(int id)', isConst=True,
        doc="""\
            FindItemById(id) -> MenuItem

            Finds the menu item object associated with the given menu item identifier.""",
        body="""\
            return self->FindItem(id);""")

    c.find('MSWCommand').ignore()

    #-----------------------------------------------------------------
    c = module.find('wxMenuBar')
    assert isinstance(c, etgtools.ClassDef)
    c.mustHaveApp()
    tools.removeVirtuals(c)
    addTransferAnnotations(c, 'menu')
    c.find('wxMenuBar').findOverload('wxMenu *menus[], const wxString titles[], long style=0)').ignore()

    c.find('FindItem.menu').out = True
    c.addCppMethod('wxMenuItem*', 'FindItemById', '(int id)', isConst=True,
        doc="""\
            FindItemById(id) -> MenuItem

            Finds the menu item object associated with the given menu item identifier.""",
        body="""\
            return self->FindItem(id);""")


    mac_scmb = c.find('MacSetCommonMenuBar')
    mac_scmb.setCppCode("""\
    #ifdef __WXMAC__
        wxMenuBar::MacSetCommonMenuBar(menubar);
    #endif
    """)

    mac_gcmb = c.find('MacGetCommonMenuBar')
    mac_gcmb.setCppCode("""\
    #ifdef __WXMAC__
        return wxMenuBar::MacGetCommonMenuBar();
    #else
        return NULL;
    #endif
    """)

    mac_gcmb = c.find('OSXGetAppleMenu')
    mac_gcmb.setCppCode("""\
    #ifdef __WXMAC__
        return self->OSXGetAppleMenu();
    #else
        return NULL;
    #endif
    """)

    # don't transfer on other platforms, as this is a no-op there.
    import sys
    if sys.platform.startswith('darwin'):
        mac_scmb.find('menubar').transfer = True

    c.find('FindItem.menu').out = True


    c.addPyMethod('GetMenus', '(self)',
        doc="""\
        GetMenus() -> (menu, label)\n
        Return a list of (menu, label) items for the menus in the :class:`MenuBar`.""",
        body="""\
        return [(self.GetMenu(i), self.GetMenuLabel(i)) for i in range(self.GetMenuCount())]
        """)
    c.addPyMethod('SetMenus', '(self, items)',
        doc="""\
        SetMenus()\n
        Clear and add new menus to the :class:`MenuBar` from a list of (menu, label) items.""",
        body="""\
        for i in range(self.GetMenuCount()-1, -1, -1):
            self.Remove(i)
        for m, l in items:
            self.Append(m, l)
        """)
    c.addPyProperty('Menus GetMenus SetMenus')


    # deprecated and removed
    c.find('GetLabelTop').ignore()
    c.find('SetLabelTop').ignore()

    module.addItem(tools.wxListWrapperTemplate('wxMenuList', 'wxMenu', module))

    #-----------------------------------------------------------------
    tools.doCommonTweaks(module)
    tools.runGenerators(module)


#---------------------------------------------------------------------------
if __name__ == '__main__':
    run()