File: codegen.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 (83 lines) | stat: -rw-r--r-- 2,820 bytes parent folder | download
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
"""\
Code generator functions for wxButton objects

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

import common
import wcodegen


class PythonButtonGenerator(wcodegen.PythonWidgetCodeWriter):

    tmpl = '%(name)s = %(klass)s(%(parent)s, %(id)s, %(label)s%(style)s)\n'

    def _prepare_tmpl_content(self, obj):
        wcodegen.PythonWidgetCodeWriter._prepare_tmpl_content(self, obj)

        if obj.check_prop_truth("stockitem"):
            self.tmpl_dict['id_number'] = self.codegen.cn("wxID_" + obj.stockitem)
            self.tmpl_dict['id'] = self.tmpl_dict['id_number']
            self.tmpl_dict['label'] = self.codegen.quote_str('')

        self.has_setdefault = obj.default

        return



class CppButtonGenerator(wcodegen.CppWidgetCodeWriter):
    tmpl = '%(name)s = new %(klass)s(%(parent)s, %(id)s, %(label)s%(style)s);\n'

    def _prepare_tmpl_content(self, obj):
        wcodegen.CppWidgetCodeWriter._prepare_tmpl_content(self, obj)

        if obj.check_prop_truth("stockitem"):
            self.tmpl_dict['label'] = self.codegen.quote_str('')
            self.tmpl_dict['id_number'] = self.codegen.cn("wxID_" + obj.stockitem)
            self.tmpl_dict['id'] = self.tmpl_dict['id_number']

        self.has_setdefault = obj.default

        return



def xrc_code_generator(obj):
    xrcgen = common.code_writers['XRC']

    class ButtonXrcObject(xrcgen.DefaultXrcObject):
        def write(self, out_file, ntabs):
            stockitem = obj.stockitem or 'None'
            properties = {}
            if stockitem != 'None':
                properties["name"] = 'wxID_' + stockitem
                properties["stockitem"] = properties['label'] = None  # don't write
            xrcgen.DefaultXrcObject.write(self, out_file, ntabs, properties)

        def write_property(self, name, val, output, tabs):
            if name == 'label':
                # translate & into _ as accelerator marker, but leave &&
                val2 = val.replace('&', '_')
                if '&&' in val:
                    index = -1
                    while True:
                        index = val.find('&&', index+1)
                        if index < 0: break
                        val = val2[:index] + '&&' + val2[index+2:]
                else:
                    val = val2
            xrcgen.DefaultXrcObject.write_property(self, name, val, output, tabs)
    # end of class ButtonXrcObject

    return ButtonXrcObject(obj)


def initialize():
    klass = 'wxButton'
    common.class_names['EditButton'] = klass
    common.register('python', klass, PythonButtonGenerator(klass))
    common.register('C++', klass, CppButtonGenerator(klass))
    common.register('XRC', klass, xrc_code_generator)