def make_pango_layout(view, widget=None): doc = view.document yoffset, xoffset = map(int, view.scroll_pos) chars, rows = map(int, view.textbox_dimensions) colours = view.colours tab_size = doc.tab_size if not widget: widget = view.textarea.drawingarea # only tokens for the text that's on the screen tokens = doc.tokenizer.get_normalised_tokens(yoffset, yoffset + rows) # pango layout # doc_lines = doc.get_lines(yoffset, yoffset+rows) start_index = doc.line_offsets[yoffset] if yoffset + rows < doc.num_lines: end_index = doc.line_offsets[yoffset + rows] else: end_index = len(doc.content) doc_lines = doc.content[start_index:end_index].splitlines() lines = [] for line in doc_lines: line = line.replace("\t", " " * doc.tab_size) lines.append(pad(line[xoffset : xoffset + chars], chars, tab_size)) pl = widget.create_pango_layout("\n".join(lines)) # build the list of pango attributes attrs = pango.AttrList() start_index = 0 ltokens = line_tokens(tokens, tab_size) for ttype, length in clipped_tokens(ltokens, xoffset, chars): if not ttype is Token.Text.Whitespace: style = get_style_for_ttype(ttype) fg = colours[style]["pango"] fg_attr = pango.AttrForeground( red=fg.red, green=fg.green, blue=fg.blue, start_index=start_index, end_index=start_index + length ) attrs.insert(fg_attr) if colours[style].get("bold", False): wt_attr = pango.AttrWeight(pango.WEIGHT_BOLD, start_index=start_index, end_index=start_index + length) attrs.insert(wt_attr) if colours[style].get("italic", False): st_attr = pango.AttrStyle(pango.STYLE_ITALIC, start_index=start_index, end_index=start_index + length) attrs.insert(st_attr) start_index += length pl.set_attributes(attrs) return pl
def test_pad(): s = '\t' line = pad(s, 80, 8) assert len(line) == 73
return True def get_cursor_coords(self, (maxcol,)): col = len(self.get_text()) return col, 0 def rows(self, (maxcol,), focus=False): return 1 def render(self, (maxcol,), focus=False): text = self.get_text() cursor = None if focus: cursor = self.get_cursor_coords((maxcol,)) return urwid.TextCanvas([pad(text, maxcol)], \ attr=[[('statusbar', maxcol)]], cursor=cursor) def keypress(self, (maxcol,), key): if key in self.options or self.default_option: if not key in self.options and self.default_option: # if the key is not a valid option, make it trigger the default # option key = self.default_option if self.callbacks.has_key(key): self.callbacks[key]() self.hide() return None class UrwidFileDialog(object): def __init__(self, editor):