"""\
XRC code generator
Generates the xml code for the app in XRC format.
Calls the appropriate ``writers'' of the various objects. These functions
return an instance of XrcObject
@copyright: 2002-2007 Alberto Griggio
@copyright: 2012-2016 Carsten Grohmann
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
import StringIO
from xml.sax.saxutils import escape, quoteattr
from codegen import BaseLangCodeWriter, \
EventsPropertyHandler, \
ExtraPropertiesPropertyHandler
from ordereddict import OrderedDict
import common
import errors
import wcodegen
from wcodegen.taghandler import BaseCodeWriterTagHandler
class FontPropertyHandler(BaseCodeWriterTagHandler):
props = {'size': '', 'family': '', 'style': '', 'weight': '',
'underlined': '', 'face': ''}
def __init__(self):
super(FontPropertyHandler, self).__init__()
self.current = None
def start_elem(self, name, attrs):
self.current = name
def end_elem(self, name, code_obj):
if name == 'font':
code_obj.properties['font'] = self.props
return True # to remove this handler
def char_data(self, data):
super(FontPropertyHandler, self).char_data(data)
self.props[self.current] = self.get_char_data()
# end of class FontHandler
class XrcObject(wcodegen.XrcWidgetCodeWriter):
"""\
Class to produce the XRC code for a given widget. This is a base
class which does nothing
"""
def __init__(self, klass=None):
wcodegen.XrcWidgetCodeWriter.__init__(self, klass)
self.properties = {}
self.children = [] # sub-objects
def write_child_prologue(self, child, out_file, ntabs):
pass
def write_child_epilogue(self, child, out_file, ntabs):
pass
def write_property(self, name, val, outfile, ntabs):
pass
def write(self, out_file, ntabs):
pass
def warning(self, msg):
"""\
Show a warning message
@param msg: Warning message
@type msg: String
"""
self._logger.warning(msg)
# end of class XrcObject
class SizerItemXrcObject(XrcObject):
"""\
XrcObject to handle sizer items
"""
def __init__(self, obj, option, flag, border):
XrcObject.__init__(self)
self.obj = obj # the XrcObject representing the widget
self.option = option
self.flag = flag
self.border = border
def write(self, out_file, ntabs):
write = out_file.write
tabs = self.tabs(ntabs)
tabs1 = self.tabs(ntabs + 1)
write(tabs + '\n')
# end of class SizerItemXrcObject
class SpacerXrcObject(XrcObject):
"""\
XrcObject to handle widgets
"""
def __init__(self, size_str, option, flag, border):
XrcObject.__init__(self)
self.size_str = size_str
self.option = option
self.flag = flag
self.border = border
def write(self, out_file, ntabs):
write = out_file.write
tabs = self.tabs(ntabs)
tabs1 = self.tabs(ntabs + 1)
write(tabs + '\n')
# end of class SpacerXrcObject
class DefaultXrcObject(XrcObject):
"""\
Standard XrcObject for every widget, used if no specific XrcObject is
available
"""
def __init__(self, code_obj):
XrcObject.__init__(self, code_obj.klass)
self.properties = code_obj.properties
self.code_obj = code_obj
self.name = code_obj.name
self.klass = code_obj.base # custom classes aren't allowed in XRC
self.subclass = code_obj.klass
def write_property(self, name, val, outfile, ntabs):
if not val:
return
if name in ['icon', 'bitmap']:
prop = self._format_bitmap_property(name, val)
else:
prop = common.format_xml_tag(name, val)
if prop:
line = '%s%s' % (self.tabs(ntabs), prop)
outfile.write(line)
def _format_bitmap_property(self, name, val):
"""\
Return formatted bitmap/icon XRC property.
@rtype: str | None
"""
if val.startswith('art:'):
content = val[4:]
elements = [item.strip() for item in content.split(',')]
art_id = elements[0]
art_client = elements[1]
if art_client != 'wxART_OTHER':
prop = common.format_xml_tag(
name, '', stock_id=art_id, stock_client=art_client)
else:
prop = common.format_xml_tag(name, u'', stock_id=art_id)
elif val.startswith('code:') or val.startswith('empty:') or \
val.startswith('var:'):
self._logger.warn(
_('XRC: Unsupported bitmap statement "%s" for %s "%s"'),
val, self.klass, self.name)
prop = None
else:
prop = common.format_xml_tag(name, val)
return prop
def write(self, out_file, ntabs):
write = out_file.write
if self.code_obj.in_sizers:
write(self.tabs(ntabs) +
'