📄 pl_codegen.py
字号:
if not write_body: write('\treturn;\n') else: for l in obj_p: write('\t' +l) write('\n# end wxGlade\n') if is_new: write('}\n') if prev_src is not None and not is_new: # replace the lines inside the __set_properties wxGlade block # with the new ones tag = '#<%swxGlade replace %s %s>' % (nonce, code_obj.klass, '__set_properties') if prev_src.content.find(tag) < 0: # no __set_properties tag found, issue a warning and do nothing print >> sys.stderr, "WARNING: wxGlade __set_properties block " \ "not found, __set_properties code NOT generated" else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) buffer = [] write = buffer.append # __do_layout if is_new: write('\nsub __do_layout {\n\tmy $self = shift;\n\n') layout_lines = classes[code_obj.klass].layout sizers_init_lines = classes[code_obj.klass].sizers_init # check if there are extra layout lines to add if hasattr(builder, 'get_layout_code'): extra_layout_lines = builder.get_layout_code(code_obj) else: extra_layout_lines = [] write('# begin wxGlade: %s::__do_layout\n\n' % code_obj.klass) if layout_lines or sizers_init_lines or extra_layout_lines: sizers_init_lines.reverse() for l in sizers_init_lines : write('\t' +l) for l in layout_lines : write('\t' +l) for l in extra_layout_lines : write('\t' +l) else: write('\treturn;\n') write('\n# end wxGlade\n') if is_new: write('}\n') if prev_src is not None and not is_new: # replace the lines inside the __do_layout wxGlade block # with the new ones tag = '#<%swxGlade replace %s %s>' % (nonce, code_obj.klass, '__do_layout') if prev_src.content.find(tag) < 0: # no __do_layout tag found, issue a warning and do nothing print >> sys.stderr, "WARNING: wxGlade __do_layout block " \ "not found, __do_layout code NOT generated" else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) if prev_src is not None and not is_new: already_there = prev_src.event_handlers.get(code_obj.klass, {}) buf = [] for name, event, handler in event_handlers: if handler not in already_there: buf.append('\nsub %s {\n' '\tmy ($self, $event) = @_;\n' '# wxGlade: %s::%s <event_handler>\n\n' '\twarn "Event handler (%s) not implemented";\n' '\t$event->Skip;\n\n# end wxGlade\n}\n\n' % (handler, code_obj.klass,handler, handler)) already_there[handler] = 1 tag = '#<%swxGlade event_handlers %s>' % (nonce, code_obj.klass) if prev_src.content.find(tag) < 0: # no event_handlers tag found, issue a warning and do nothing print >> sys.stderr, "WARNING: wxGlade event_handlers block " \ "not found, event_handlers code NOT generated" else: prev_src.content = prev_src.content.replace(tag, "".join(buf)) del buf else: already_there = {} for name, event, handler in event_handlers: if handler not in already_there: write('\nsub %s {\n' '\tmy ($self, $event) = @_;\n' '# wxGlade: %s::%s <event_handler>\n\n' '\twarn "Event handler (%s) not implemented";\n' '\t$event->Skip;\n\n# end wxGlade\n}\n\n' % (handler, code_obj.klass,handler, handler)) already_there[handler] = 1 # the code has been generated classes[code_obj.klass].done = True write('\n# end of class %s\n\n1;\n\n' % code_obj.klass) if not multiple_files and prev_src is not None: # if this is a new class, add its code to the new_classes list of the # SourceFileContent instance if is_new: prev_src.new_classes.append("".join(buffer)) return if multiple_files: #return # not implemented yet -- crazyinsomniac if prev_src is not None: tag = '#<%swxGlade insert new_classes>' % nonce prev_src.content = prev_src.content.replace(tag, "") # insert the extra modules tag = '#<%swxGlade extra_modules>\n' % nonce code = "".join(_current_extra_modules.keys()) prev_src.content = prev_src.content.replace(tag, code) # insert the module dependencies of this class extra_modules = classes[code_obj.klass].dependencies.keys() deps = ['# begin wxGlade: ::dependencies\n'] + extra_modules + \ ['# end wxGlade\n'] tag = '#<%swxGlade replace %s dependencies>' % \ (nonce, code_obj.klass) prev_src.content = prev_src.content.replace(tag, "".join(deps)) try: # store the new file contents to disk common.save_file(filename, prev_src.content, 'codegen') except: raise IOError("pl_codegen.add_class: %s, %s, %s" % \ (out_dir, prev_src.name, code_obj.klass)) return # create the new source file filename = code_obj.klass.replace('::', os.sep ) + '.pm' # MODULE!! filename = os.path.join(out_dir, filename ) out = cStringIO.StringIO() write = out.write # write the common lines for line in header_lines: write(line) # write the class body for line in buffer: write(line) try: dirname = os.path.dirname(filename) # create Foo in Foo::Bar, # Foo/Bar.pm if not os.path.exists(dirname): os.makedirs(dirname) # store the contents to filename common.save_file(filename, out.getvalue(), 'codegen') except: import traceback; traceback.print_exc() out.close() else: # not multiple_files # write the class body onto the single source file for dep in classes[code_obj.klass].dependencies: _current_extra_modules[dep] = 1 write = output_file.write for line in buffer: write(line) _app_added = Falsedef add_app(app_attrs, top_win_class): """\ Generates the code for a wxApp instance. If the file to write into already exists, this function does nothing. """ global _app_added _app_added = True name = app_attrs.get('name') if not name: name = 'app' if not multiple_files: prev_src = previous_source else: filename = os.path.join(out_dir, name + '.pl') if not os.path.exists(filename): prev_src = None elif _overwrite: prev_src = None else: # prev_src doesn't need to be a SourceFileContent instance in this # case, as we do nothing if it is not None prev_src = 1 if prev_src is not None: return # do nothing if the file existed klass = app_attrs.get('class') top_win = app_attrs.get('top_window') if not top_win: return # do nothing if there is no top window lines = [] append = lines.append if klass: append('package %s;\n' % klass) append('\nuse base qw(Wx::App);\nuse strict;\n\n') if multiple_files: # import the top window module append('use %s;\n\n' % top_win_class) append('sub OnInit {\n\tmy( $self ) = shift;\n\n') else: append('1;\n\npackage main;\n') if multiple_files: # import the top window module append('\nuse %s;\n\n' % top_win_class ) append('\nunless(caller){\n') if _use_gettext: append('\tmy $local = Wx::Locale->new("English", "en", "en");' + ' # replace with ??\n') append('\t$local->AddCatalog("%s");' % name + ' # replace with the appropriate catalog name\n\n' )# and now, basically fake wxPySimpleApp append('\tlocal *Wx::App::OnInit = sub{1};\n') append('\tmy $%s = Wx::App->new();\n'% name) append('\tWx::InitAllImageHandlers();\n\n') # we add this to avoid troubles append('\tmy $%s = %s->new();\n\n' % (top_win, top_win_class)) if klass: append('\t$self->SetTopWindow($%s);\n' % top_win) append('\t$%s->Show(1);\n\n' % top_win) append('\treturn 1;\n}\n') append('# end of class %s\n\n' % klass) append('package main;\n\nunless(caller){\n') if _use_gettext: append('\tmy $local = Wx::Locale->new("English", "en", "en");' + ' # replace with ??\n') append('\t$local->AddCatalog("%s");' % name + ' # replace with the appropriate catalog name\n\n') append('\tmy $%s = %s->new();\n' % (name, klass)) else: append('\t$%s->SetTopWindow($%s);\n' % (name, top_win)) append('\t$%s->Show(1);\n' % top_win) append('\t$%s->MainLoop();\n}\n' % name) if multiple_files: # not read yet filename = os.path.join(out_dir, name + '.pl') out = cStringIO.StringIO() write = out.write write('#!/usr/bin/perl -w -- \n') # write the common lines for line in header_lines: write(line) # write the wxApp code for line in lines: write(line) try: common.save_file(filename, out.getvalue(), 'codegen') except: import traceback; traceback.print_wexc() # make the file executable try: os.chmod(filename, 0755) except OSError: pass # this is not a bad error out.close() else: write = output_file.write for line in lines: write(line)def _get_code_name(obj): """\ returns the name of the variable ( either $self, $foo, or $self->{foo} ) """ if obj.is_toplevel: return '$self' else: if test_attribute(obj): return '$self->{%s}' % obj.name else: # it's an already declared lexical (my $foo) if obj.name[0] == '$': return obj.name else: return '$' + obj.name def generate_code_size(obj): """\ returns the code fragment that sets the size of the given object. """ name = _get_code_name(obj) 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 name + '->' + method + \ '(%s->ConvertDialogSizeToPixels(Wx::Size->new(%s)));\n' % \ (name, size[:-1]) return name + '->' + method + '(Wx::Size->new(%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. """ self = _get_code_name(obj) try: color = 'Wx::Colour->new(%s)' % \ _string_to_colour(obj.properties['foreground']) except (IndexError, ValueError): # the color is from system settings color = 'Wx::SystemSettings::GetColour(%s)' % \ obj.properties['foreground'] return self + '->SetForegroundColour(%s);\n' % colordef generate_code_background(obj): """\ returns the code fragment that sets the background colour of the given object. """ self = _get_code_name(obj) try: color = 'Wx::Colour->new(%s)' % \ _string_to_colour(obj.properties['background']) except (IndexError, ValueError): # the color is from system settings color = 'Wx::SystemSettings::GetColour(%s)' % \ obj.properties['background'] return self + '->SetBackgroundColour(%s);\n' % colordef generate_code_font(obj): """\ returns the code fragment that sets the font of 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'\"') self = _get_code_name(obj) return self + \ '->SetFont(Wx::Font->new(%s, %s, %s, %s, %s, %s));\n' % \ (size, family, style, weight, underlined, face)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -