📄 cpp_codegen.py
字号:
for line in header_lines: write(line) # import the top window module write('#include "%s.h"\n\n' % top_win_class) # write the wxApp code for line in lines: write(line) common.save_file(filename, out.getvalue(), 'codegen') else: write = output_source.write for line in lines: write(line)def generate_code_size(obj): """\ returns the code fragment that sets the size of the given object. """ if obj.is_toplevel: name1 = ''; name2 = 'this' else: name1 = '%s->' % obj.name; name2 = obj.name size = obj.properties.get('size', '').strip() use_dialog_units = (size[-1] == 'd') if for_version < (2, 5) or obj.parent is None: method = 'SetSize' else: method = 'SetMinSize' if use_dialog_units: return name1 + method + '(wxDLG_UNIT(%s, wxSize(%s)));\n' % \ (name2, size[:-1]) else: return name1 + method + '(wxSize(%s));\n' % sizedef _string_to_colour(s): return '%d, %d, %d' % (int(s[1:3], 16), int(s[3:5], 16), int(s[5:], 16))def generate_code_foreground(obj): """\ returns the code fragment that sets the foreground colour of the given object. """ if not obj.is_toplevel: intro = '%s->' % obj.name else: intro = '' try: color = 'wxColour(%s)' % \ _string_to_colour(obj.properties['foreground']) except (IndexError, ValueError): # the color is from system settings color = 'wxSystemSettings::GetColour(%s)' % \ obj.properties['foreground'] return intro + 'SetForegroundColour(%s);\n' % colordef generate_code_background(obj): """\ returns the code fragment that sets the background colour of the given object. """ if not obj.is_toplevel: intro = '%s->' % obj.name else: intro = '' try: color = 'wxColour(%s)' % \ _string_to_colour(obj.properties['background']) except (IndexError, ValueError): # the color is from system settings color = 'wxSystemSettings::GetColour(%s)' % \ obj.properties['background'] return intro + 'SetBackgroundColour(%s);\n' % colordef generate_code_font(obj): """\ returns the code fragment that sets the font the given object. """ font = obj.properties['font'] size = font['size']; family = font['family'] underlined = font['underlined'] style = font['style']; weight = font['weight'] face = '"%s"' % font['face'].replace('"', r'\"') if obj.is_toplevel: intro = '' else: intro = '%s->' % obj.name return intro + 'SetFont(wxFont(%s, %s, %s, %s, %s, wxT(%s)));\n' % \ (size, family, style, weight, underlined, face)_last_generated_id = 1000def generate_code_id(obj, id=None): """\ returns a 2-tuple of strings representing the LOC that sets the id of the given object: the first line is the declaration of the variable, and is empty if the object's id is a constant, and the second line is the value of the id """ global _last_generated_id if id is None: id = obj.properties.get('id') if not id: return '', 'wxID_ANY' tokens = id.split('=') if len(tokens) > 1: name, val = tokens[:2] else: return '', tokens[0] # we assume name is declared elsewhere if not name: return '', val if val.strip() == '?': val = 'wxID_HIGHEST + ' + str(_last_generated_id) _last_generated_id += 1 return '%s = %s' % (name, val), namedef generate_code_tooltip(obj): """\ returns the code fragment that sets the tooltip of the given object. """ if not obj.is_toplevel: intro = '%s->' % obj.name else: intro = '' return intro + 'SetToolTip(%s);\n' % quote_str(obj.properties['tooltip'])def _get_code_name(obj): if not obj.is_toplevel: return '%s->' % obj.name else: return '' def generate_code_disabled(obj): self = _get_code_name(obj) try: disabled = int(obj.properties['disabled']) except: disabled = False if disabled: return self + 'Enable(false);\n'def generate_code_focused(obj): self = _get_code_name(obj) try: focused = int(obj.properties['focused']) except: focused = False if focused: return self + 'SetFocus();\n'def generate_code_hidden(obj): self = _get_code_name(obj) try: hidden = int(obj.properties['hidden']) except: hidden = False if hidden: return self + 'Hide();\n'def generate_code_extraproperties(obj): self = _get_code_name(obj) prop = obj.properties['extraproperties'] ret = [] for name in sorted(prop): ret.append(self + 'Set%s(%s);\n' % (name, prop[name])) return retdef generate_common_properties(widget): """\ generates the code for various properties common to all widgets (background and foreground colors, font, ...) Returns a list of strings containing the generated code """ prop = widget.properties out = [] if prop.get('size', '').strip(): out.append(generate_code_size(widget)) if prop.get('background'): out.append(generate_code_background(widget)) if prop.get('foreground'): out.append(generate_code_foreground(widget)) if prop.get('font'): out.append(generate_code_font(widget)) if prop.get('tooltip'): out.append(generate_code_tooltip(widget)) # trivial boolean properties if prop.get('disabled'): out.append(generate_code_disabled(widget)) if prop.get('focused'): out.append(generate_code_focused(widget)) if prop.get('hidden'): out.append(generate_code_hidden(widget)) # ALB 2007-09-01 extra properties if prop.get('extraproperties') and not widget.preview: out.extend(generate_code_extraproperties(widget)) return out# custom property handlersclass FontPropertyHandler: """Handler for font properties""" font_families = { 'default': 'wxDEFAULT', 'decorative': 'wxDECORATIVE', 'roman': 'wxROMAN', 'swiss': 'wxSWISS', 'script': 'wxSCRIPT', 'modern': 'wxMODERN', 'teletype': 'wxTELETYPE' } font_styles = { 'normal': 'wxNORMAL', 'slant': 'wxSLANT', 'italic': 'wxITALIC' } font_weights = { 'normal': 'wxNORMAL', 'light': 'wxLIGHT', 'bold': 'wxBOLD' } def __init__(self): self.dicts = { 'family': self.font_families, 'style': self.font_styles, 'weight': self.font_weights } self.attrs = { 'size': '0', 'style': '0', 'weight': '0', 'family': '0', 'underlined': '0', 'face': '' } self.current = None self.curr_data = [] def start_elem(self, name, attrs): self.curr_data = [] if name != 'font' and name in self.attrs: self.current = name else: self.current = None def end_elem(self, name, code_obj): if name == 'font': code_obj.properties['font'] = self.attrs return True elif self.current is not None: decode = self.dicts.get(self.current) if decode: val = decode.get("".join(self.curr_data), '0') else: val = "".join(self.curr_data) self.attrs[self.current] = val def char_data(self, data): self.curr_data.append(data)# end of class FontPropertyHandlerclass DummyPropertyHandler: """Empty handler for properties that do not need code""" def start_elem(self, name, attrs): pass def end_elem(self, name, code_obj): return True def char_data(self, data): pass# end of class DummyPropertyHandlerclass EventsPropertyHandler(object): def __init__(self): self.handlers = {} self.event_name = None self.curr_handler = [] def start_elem(self, name, attrs): if name == 'handler': self.event_name = attrs['event'] def end_elem(self, name, code_obj): if name == 'handler': if self.event_name and self.curr_handler: self.handlers[self.event_name] = ''.join(self.curr_handler) self.event_name = None self.curr_handler = [] elif name == 'events': code_obj.properties['events'] = self.handlers return True def char_data(self, data): data = data.strip() if data: self.curr_handler.append(data)# end of class EventsPropertyHandler class ExtraPropertiesPropertyHandler(object): def __init__(self): self.props = {} self.prop_name = None self.curr_prop = [] def start_elem(self, name, attrs): if name == 'property': name = attrs['name'] if name and name[0].islower(): name = name[0].upper() + name[1:] self.prop_name = name def end_elem(self, name, code_obj): if name == 'property': if self.prop_name and self.curr_prop: self.props[self.prop_name] = ''.join(self.curr_prop) self.prop_name = None self.curr_prop = [] elif name == 'extraproperties': code_obj.properties['extraproperties'] = self.props return True # to remove this handler def char_data(self, data): data = data.strip() if data: self.curr_prop.append(data)# end of class ExtraPropertiesPropertyHandler# dictionary whose items are custom handlers for widget properties_global_property_writers = { 'font': FontPropertyHandler, 'events': EventsPropertyHandler, 'extraproperties': ExtraPropertiesPropertyHandler, }# dictionary of dictionaries of property handlers specific for a widget# the keys are the class names of the widgets# Ex: _property_writers['wxRadioBox'] = {'choices', choices_handler}_property_writers = {}# dictionary of additional headers for objects_obj_headers = {}def get_property_handler(property_name, widget_name): try: cls = _property_writers[widget_name][property_name] except KeyError: cls = _global_property_writers.get(property_name, None) if cls: return cls() return Nonedef add_property_handler(property_name, handler, widget_name=None): """\ sets a function to parse a portion of XML to get the value of the property property_name. If widget_name is not None,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -