/usr/lib/python2.7/dist-packages/wxglade/widgets/custom_widget/codegen.py is in python-wxglade 0.6.8-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # codegen.py: code generator functions for CustomWidget objects
# $Id: codegen.py,v 1.13 2007/08/07 12:13:43 agriggio Exp $
#
# Copyright (c) 2002-2007 Alberto Griggio <agriggio@users.sourceforge.net>
# License: MIT (see license.txt)
# THIS PROGRAM COMES WITH NO WARRANTY
import common
class ArgumentsCodeHandler:
def __init__(self):
self.arguments = []
self.curr_arg = []
def start_elem(self, name, attrs):
pass
def end_elem(self, name, code_obj):
if name == 'arguments':
code_obj.properties['arguments'] = self.arguments
return True
elif name == 'argument':
tab_name = "".join(self.curr_arg)
self.arguments.append(tab_name)
self.curr_arg = []
return False
def char_data(self, data):
self.curr_arg.append(data)
# end of class ArgumentsCodeHandler
def _fix_arguments(arguments, parent, id, size):
# Dinogen, 29 oct 2003
# adding $width e $height:
vSize = size.split(',')
for i in range(len(arguments)):
if arguments[i] == '$parent': arguments[i] = parent
elif arguments[i] == '$id': arguments[i] = id
elif arguments[i] == '$width': arguments[i] = vSize[0]
elif arguments[i] == '$height': arguments[i] = vSize[1]
return arguments
class PythonCodeGenerator:
def get_code(self, widget):
if widget.preview and widget.klass not in widget.parser.class_names:
# if this CustomWidget refers to another class in the same wxg
# file, use that for the preview
return self.get_code_preview(widget)
pygen = common.code_writers['python']
prop = widget.properties
id_name, id = pygen.generate_code_id(widget)
if not widget.parent.is_toplevel:
parent = 'self.%s' % widget.parent.name
else: parent = 'self'
init = []
if id_name: init.append(id_name)
arguments = _fix_arguments(
prop.get('arguments', []), parent, id,
prop.get('size', '-1, -1').strip())
ctor = widget.klass
cust_ctor = prop.get('custom_ctor', '').strip()
if cust_ctor:
ctor = cust_ctor
init.append('self.%s = %s(%s)\n' % (widget.name, ctor,
", ".join(arguments)))
props_buf = pygen.generate_common_properties(widget)
return init, props_buf, []
def get_code_preview(self, widget):
pygen = common.code_writers['python']
if not widget.parent.is_toplevel:
parent = 'self.%s' % widget.parent.name
else: parent = 'self'
init = []
append = init.append
append('self.%s = wx.Window(%s, -1)\n' % (widget.name, parent))
on_paint_code = """\
def self_%s_on_paint(event):
widget = self.%s
dc = wx.PaintDC(widget)
dc.BeginDrawing()
dc.SetBrush(wx.WHITE_BRUSH)
dc.SetPen(wx.BLACK_PEN)
dc.SetBackground(wx.WHITE_BRUSH)
dc.Clear()
w, h = widget.GetClientSize()
dc.DrawLine(0, 0, w, h)
dc.DrawLine(w, 0, 0, h)
text = 'Custom Widget: %s'
tw, th = dc.GetTextExtent(text)
x = (w - tw)/2
y = (h - th)/2
dc.SetPen(wx.ThePenList.FindOrCreatePen(wx.BLACK, 0, wx.TRANSPARENT))
dc.DrawRectangle(x-1, y-1, tw+2, th+2)
dc.DrawText(text, x, y)
dc.EndDrawing()
""" % ((widget.name,) * 3)
for line in on_paint_code.splitlines():
append(line + '\n')
append('wx.EVT_PAINT(self.%s, self_%s_on_paint)\n' %
(widget.name, widget.name))
return init, [], []
# end of class PythonCodeGenerator
class CppCodeGenerator:
def get_code(self, widget):
cppgen = common.code_writers['C++']
prop = widget.properties
id_name, id = cppgen.generate_code_id(widget)
if id_name: ids = [ id_name ]
else: ids = []
if not widget.parent.is_toplevel: parent = '%s' % widget.parent.name
else: parent = 'this'
arguments = _fix_arguments(
prop.get('arguments', []), parent, id,
prop.get('size', '-1, -1').strip())
ctor = 'new ' + widget.klass
cust_ctor = prop.get('custom_ctor', '').strip()
if cust_ctor:
ctor = cust_ctor
init = ['%s = %s(%s);\n' % (widget.name, ctor,
", ".join(arguments)) ]
props_buf = cppgen.generate_common_properties(widget)
return init, ids, props_buf, []
# end of class CppCodeGenerator
def xrc_code_generator(obj):
xrcgen = common.code_writers['XRC']
class CustomXrcObject(xrcgen.DefaultXrcObject):
from xml.sax.saxutils import escape
def write(self, outfile, ntabs):
# first, fix the class:
self.klass = obj.klass
# delete the custom constructor property
if 'custom_ctor' in self.properties:
del self.properties['custom_ctor']
# then, the attributes:
if 'arguments' in self.properties:
args = self.properties['arguments']
del self.properties['arguments']
for arg in args:
try:
name, val = [s.strip() for s in arg.split(':', 1)]
except Exception, e:
print 'Exception:', e
continue # silently ignore malformed arguments
self.properties[name] = val
xrcgen.DefaultXrcObject.write(self, outfile, ntabs)
return CustomXrcObject(obj)
def initialize():
common.class_names['CustomWidget'] = 'CustomWidget'
# python code generation functions
pygen = common.code_writers.get('python')
if pygen:
pygen.add_widget_handler('CustomWidget', PythonCodeGenerator())
pygen.add_property_handler('arguments', ArgumentsCodeHandler,
'CustomWidget')
cppgen = common.code_writers.get('C++')
if cppgen:
cppgen.add_widget_handler('CustomWidget', CppCodeGenerator())
cppgen.add_property_handler('arguments', ArgumentsCodeHandler,
'CustomWidget')
xrcgen = common.code_writers.get('XRC')
if xrcgen:
xrcgen.add_widget_handler('CustomWidget', xrc_code_generator)
xrcgen.add_property_handler('arguments', ArgumentsCodeHandler,
'CustomWidget')
|