def OnFont(self, evt): if not self.rtc.HasSelection(): return r = self.rtc.GetSelectionRange() fontData = wx.FontData() fontData.EnableEffects(False) attr = rt.TextAttrEx() attr.SetFlags(rt.TEXT_ATTR_FONT) if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr): fontData.SetInitialFont(attr.GetFont()) dlg = wx.FontDialog(self, fontData) if dlg.ShowModal() == wx.ID_OK: fontData = dlg.GetFontData() font = fontData.GetChosenFont() if font: attr.SetFlags(rt.TEXT_ATTR_FONT) attr.SetFont(font) if r[0] < 0 and r[1] < 0: self.styleChangeMode = 'paragraph' i = self.rtc.GetInsertionPoint() self.rtc.SetStyle((i, i + 1), attr) else: self.rtc.SetStyle(r, attr) dlg.Destroy()
def OnAddImage(self, evt): self.Modified() from mainlogic import _ imageFileName = wx.FileSelector( _("Choose image file"), wildcard= "PNG files (*.png)|*.png|BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif|JPEG Files (*.jpeg)|*.jpeg|JPG Files (*.jpg)|*.jpg", default_extension='png', flags=wx.FD_OPEN) if not imageFileName: return position = self.rtc.GetInsertionPoint() previousTags = 0 for entry in self.tagList: if entry['rtrange'][0] < position: previousTags += 1 continue shift = 0 for i in range(previousTags): entry = self.tagList[i] shift += (entry['rtrange'][1] - entry['rtrange'][0]) - ( entry['range'][1] - entry['range'][0]) oposition = position - shift self.ortc.SetInsertionPoint(oposition) style = rt.TextAttrEx() self.rtc.GetStyle(position, style) self.ortc.WriteImageFile(imageFileName, wx.BITMAP_TYPE_ANY) self.ortc.SetStyleEx((oposition - 1, oposition), style) self.RenderText()
def Lines(self, event): attr = text.TextAttrEx() attr.SetFlags(text.TEXT_ATTR_LEFT_INDENT) ip = self.win.GetInsertionPoint() if self.win.GetStyle(ip, attr): r = text.RichTextRange(ip, ip) if self.win.HasSelection(): r = self.win.GetSelectionRange() attr.SetLeftIndent(attr.GetLeftIndent() + 100) attr.SetFlags(text.TEXT_ATTR_LEFT_INDENT) self.win.SetStyle(r, attr)
def OnLineSpacingDouble(self, evt): attr = rt.TextAttrEx() attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) ip = self.rtc.GetInsertionPoint() if self.rtc.GetStyle(ip, attr): r = rt.RichTextRange(ip, ip) if self.rtc.HasSelection(): r = self.rtc.GetSelectionRange() attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) attr.SetLineSpacing(20) self.rtc.SetStyle(r, attr)
def OnParagraphSpacingMore(self, evt): attr = rt.TextAttrEx() attr.SetFlags(rt.TEXT_ATTR_PARA_SPACING_AFTER) ip = self.rtc.GetInsertionPoint() if self.rtc.GetStyle(ip, attr): r = rt.RichTextRange(ip, ip) if self.rtc.HasSelection(): r = self.rtc.GetSelectionRange() attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20) attr.SetFlags(rt.TEXT_ATTR_PARA_SPACING_AFTER) self.rtc.SetStyle(r, attr)
def OnIndentMore(self, evt): attr = rt.TextAttrEx() attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT) ip = self.rtc.GetInsertionPoint() if self.rtc.GetStyle(ip, attr): r = rt.RichTextRange(ip, ip) if self.rtc.HasSelection(): r = self.rtc.GetSelectionRange() attr.SetLeftIndent(attr.GetLeftIndent() + 100) attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT) self.rtc.SetStyle(r, attr)
def OnContentInserted(self, event): self.Modified() if not self.rtcFeedbackEnabled: event.Skip() return addedRange = event.GetRange() addedText = self.GetTextWithImagePlaceholders( self.rtc.GetValue())[addedRange[0]:addedRange[1] + 1] if '@' in addedText: self.rtcFeedbackEnabled = False self.rtc.GetBuffer().DeleteRangeWithUndo(addedRange, self.rtc) self.rtcFeedbackEnabled = True event.Skip() return style = rt.TextAttrEx() self.rtc.GetStyle((addedRange[0] + addedRange[1]) / 2, style) style.SetTextColour('black') amount = addedRange[1] + 1 - addedRange[0] previousTags = 0 for entry in self.tagList: if entry['rtrange'][0] <= addedRange[0] and entry['rtrange'][ 1] >= addedRange[0]: self.rtcFeedbackEnabled = False self.rtc.GetBuffer().DeleteRangeWithUndo(addedRange, self.rtc) self.rtcFeedbackEnabled = True event.Skip() return if entry['rtrange'][0] < addedRange[0]: previousTags += 1 continue entry['rtrange'][0] += amount entry['rtrange'][1] += amount entry['range'][0] += amount entry['range'][1] += amount shift = 0 for i in range(previousTags): entry = self.tagList[i] shift += (entry['rtrange'][1] - entry['rtrange'][0]) - ( entry['range'][1] - entry['range'][0]) shiftedRange = (addedRange[0] - shift, addedRange[0] - shift + amount - 1) if addedText and addedText != '@': self.ortc.GetBuffer().InsertTextWithUndo(shiftedRange[0], addedText, self.ortc) #self.rtc.SetStyleEx((addedRange[0],addedRange[1]+1),style,rt.RICHTEXT_SETSTYLE_CHARACTERS_ONLY) self.ortc.SetStyleEx((shiftedRange[0], shiftedRange[1] + 1), style, rt.RICHTEXT_SETSTYLE_CHARACTERS_ONLY)
def URL_ap(self, event): if self.aText.GetValue().encode('utf-8').decode('latin-1').encode('latin-1') != _("Write name link"): if self.aText.GetValue() != '': name_text = self.aText.GetValue().encode('utf-8').decode('latin-1').encode('latin-1') if self.path != '': name_url = self.path.encode('utf-8').decode('latin-1').encode('latin-1') urlStyle = text.TextAttrEx() urlStyle.SetTextColour(wx.RED) urlStyle.SetFontUnderlined(True) self.win.BeginStyle(urlStyle) self.win.BeginURL(name_url) self.win.WriteText(name_text) self.win.EndURL() self.win.Newline() self.win.EndStyle() self.pnn.Destroy() self.win.WriteText(' ')
def OnFont(self, evt): if not self.win.HasSelection(): return r = self.win.GetSelectionRange() fontData = wx.FontData() fontData.EnableEffects(False) attr = text.TextAttrEx() attr.SetFlags(text.TEXT_ATTR_FONT) if self.win.GetStyle(self.win.GetInsertionPoint(), attr): fontData.SetInitialFont(attr.GetFont()) dlg = wx.FontDialog(self, fontData) if dlg.ShowModal() == wx.ID_OK: fontData = dlg.GetFontData() font = fontData.GetChosenFont() if font: attr.SetFlags(text.TEXT_ATTR_FONT) attr.SetFont(font) self.win.SetStyle(r, attr) dlg.Destroy()
def OnColour(self, evt): colourData = wx.ColourData() attr = text.TextAttrEx() attr.SetFlags(text.TEXT_ATTR_TEXT_COLOUR) if self.win.GetStyle(self.win.GetInsertionPoint(), attr): colourData.SetColour(attr.GetTextColour()) dlg = wx.ColourDialog(self, colourData) if dlg.ShowModal() == wx.ID_OK: colourData = dlg.GetColourData() colour = colourData.GetColour() if colour: if not self.win.HasSelection(): self.win.BeginTextColour(colour) else: r = self.win.GetSelectionRange() attr.SetFlags(text.TEXT_ATTR_TEXT_COLOUR) attr.SetTextColour(colour) self.win.SetStyle(r, attr) dlg.Destroy()
def OnStyleChanged(self, event): self.Modified() if not self.rtcFeedbackEnabled: event.Skip() return changedRange = event.GetRange() previousTags = [0, 0] for entry in self.tagList: if entry['rtrange'][0] < changedRange[0]: previousTags[0] += 1 if entry['rtrange'][0] < changedRange[1]: previousTags[1] += 1 shiftedRange = [changedRange[0], changedRange[1]] shift = 0 for i in range(previousTags[0]): entry = self.tagList[i] shiftedRange[0] -= (entry['rtrange'][1] - entry['rtrange'][0]) - ( entry['range'][1] - entry['range'][0]) for i in range(previousTags[1]): entry = self.tagList[i] shiftedRange[1] -= (entry['rtrange'][1] - entry['rtrange'][0]) - ( entry['range'][1] - entry['range'][0]) style = rt.TextAttrEx() self.rtc.GetStyle((changedRange[0] + changedRange[1]) / 2, style) #flags = rt.RICHTEXT_SETSTYLE_NONE if changedRange[1] - changedRange[0] > 1: if self.styleChangeMode == 'paragraph': flags = rt.RICHTEXT_SETSTYLE_WITH_UNDO | rt.RICHTEXT_SETSTYLE_PARAGRAPHS_ONLY else: flags = rt.RICHTEXT_SETSTYLE_WITH_UNDO | rt.RICHTEXT_SETSTYLE_CHARACTERS_ONLY self.ortc.SetStyleEx((shiftedRange[0], shiftedRange[1] + 1), style, flags) else: if self.styleChangeMode == 'paragraph': flags = rt.RICHTEXT_SETSTYLE_WITH_UNDO | rt.RICHTEXT_SETSTYLE_PARAGRAPHS_ONLY self.ortc.SetStyleEx((shiftedRange[0], shiftedRange[1] + 1), style, flags) else: self.ortc.SetStyleEx((shiftedRange[0], shiftedRange[1] + 1), style) self.styleChangeMode = 'character'
def __init__(self, *args, **kw): wx.Frame.__init__(self, *args, **kw) self.MakeMenuBar() self.MakeToolBar() self.CreateStatusBar() self.SetStatusText("Welcome to wx.richtext.RichTextCtrl!") self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER) wx.CallAfter(self.rtc.SetFocus) self.rtc.Freeze() self.rtc.BeginSuppressUndo() self.rtc.BeginParagraphSpacing(0, 20) self.rtc.BeginAlignment(rt.TEXT_ALIGNMENT_CENTRE) self.rtc.BeginBold() self.rtc.BeginFontSize(14) self.rtc.WriteText( "Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images" ) self.rtc.EndFontSize() self.rtc.Newline() self.rtc.BeginItalic() self.rtc.WriteText("by Julian Smart") self.rtc.EndItalic() self.rtc.EndBold() self.rtc.Newline() self.rtc.WriteImage(images._rt_zebra.GetImage()) self.rtc.EndAlignment() self.rtc.Newline() self.rtc.Newline() self.rtc.WriteText("What can you do with this thing? ") self.rtc.WriteImage(images._rt_smiley.GetImage()) self.rtc.WriteText(" Well, you can change text ") self.rtc.BeginTextColour((255, 0, 0)) self.rtc.WriteText("colour, like this red bit.") self.rtc.EndTextColour() self.rtc.BeginTextColour((0, 0, 255)) self.rtc.WriteText(" And this blue bit.") self.rtc.EndTextColour() self.rtc.WriteText(" Naturally you can make things ") self.rtc.BeginBold() self.rtc.WriteText("bold ") self.rtc.EndBold() self.rtc.BeginItalic() self.rtc.WriteText("or italic ") self.rtc.EndItalic() self.rtc.BeginUnderline() self.rtc.WriteText("or underlined.") self.rtc.EndUnderline() self.rtc.BeginFontSize(14) self.rtc.WriteText( " Different font sizes on the same line is allowed, too.") self.rtc.EndFontSize() self.rtc.WriteText(" Next we'll show an indented paragraph.") self.rtc.BeginLeftIndent(60) self.rtc.Newline() self.rtc.WriteText( "It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable." ) self.rtc.EndLeftIndent() self.rtc.Newline() self.rtc.WriteText( "Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)." ) self.rtc.BeginLeftIndent(100, -40) self.rtc.Newline() self.rtc.WriteText( "It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable." ) self.rtc.EndLeftIndent() self.rtc.Newline() self.rtc.WriteText( "Numbered bullets are possible, again using sub-indents:") self.rtc.BeginNumberedBullet(1, 100, 60) self.rtc.Newline() self.rtc.WriteText( "This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later." ) self.rtc.EndNumberedBullet() self.rtc.BeginNumberedBullet(2, 100, 60) self.rtc.Newline() self.rtc.WriteText("This is my second item.") self.rtc.EndNumberedBullet() self.rtc.Newline() self.rtc.WriteText("The following paragraph is right-indented:") self.rtc.BeginRightIndent(200) self.rtc.Newline() self.rtc.WriteText( "It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable." ) self.rtc.EndRightIndent() self.rtc.Newline() self.rtc.WriteText( "The following paragraph is right-aligned with 1.5 line spacing:") self.rtc.BeginAlignment(rt.TEXT_ALIGNMENT_RIGHT) self.rtc.BeginLineSpacing(rt.TEXT_ATTR_LINE_SPACING_HALF) self.rtc.Newline() self.rtc.WriteText( "It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable." ) self.rtc.EndLineSpacing() self.rtc.EndAlignment() self.rtc.Newline() self.rtc.WriteText("Other notable features of wxRichTextCtrl include:") self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText("Compatibility with wxTextCtrl API") self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText( "Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()" ) self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText("XML loading and saving") self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText( "Undo/Redo, with batching option and Undo suppressing") self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText("Clipboard copy and paste") self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText( "wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles" ) self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() self.rtc.WriteText( "A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on" ) self.rtc.EndSymbolBullet() self.rtc.BeginSymbolBullet('*', 100, 60) self.rtc.Newline() # Make a style suitable for showing a URL urlStyle = rt.TextAttrEx() urlStyle.SetTextColour(wx.BLUE) urlStyle.SetFontUnderlined(True) self.rtc.WriteText( "RichTextCtrl can also display URLs, such as this one: ") self.rtc.BeginStyle(urlStyle) self.rtc.BeginURL("http://wxPython.org/") self.rtc.WriteText("The wxPython Web Site") self.rtc.EndURL() self.rtc.EndStyle() self.rtc.WriteText(". Click on the URL to generate an event.") self.rtc.Bind(wx.EVT_TEXT_URL, self.OnURL) self.rtc.Newline() self.rtc.WriteText( "Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!" ) self.rtc.EndParagraphSpacing() self.rtc.EndSuppressUndo() self.rtc.Thaw()
def display_colored_text(in_text, out_RichTextCtrl): # Apply coloring to text, then display it on a wx.RichTextCtrl try: # try coping with python changing function names between versions /abot low = rt.RichTextAttr() medium = rt.RichTextAttr() high = rt.RichTextAttr() happy = rt.RichTextAttr() hide = rt.RichTextAttr() url = rt.RichTextAttr() warning = rt.RichTextAttr() error = rt.RichTextAttr() except: low = rt.TextAttrEx() medium = rt.TextAttrEx() high = rt.TextAttrEx() happy = rt.TextAttrEx() hide = rt.TextAttrEx() url = rt.TextAttrEx() warning = rt.TextAttrEx() error = rt.TextAttrEx() low.SetBackgroundColour(wx.Colour(125,220,240)) medium.SetBackgroundColour(wx.Colour(255,255,180)) high.SetBackgroundColour(wx.Colour(255,180,180)) happy.SetBackgroundColour('GREEN') hide.SetBackgroundColour('BLACK'); hide.SetTextColour('BLACK') url.SetTextColour('BLUE') ; url.SetFontUnderlined(True) warning.SetBackgroundColour('YELLOW') error.SetBackgroundColour('RED') highlighters = { #Only highlights the text inside group 1 re.compile(r'Version[^\]]+\]\t+(.+)', re.MULTILINE): happy, re.compile(r'(http://\S+)', re.IGNORECASE): url, re.compile(r'^(\[CONFLICT\])', re.MULTILINE): medium, re.compile(r'(\[Plugins already in sorted order. No sorting needed!\])', re.IGNORECASE): happy, re.compile(r"^(\s*\|?\s*!{1}[^!].*)$", re.MULTILINE): low, #Handle '!' in mlox_base.txt re.compile(r"^(\s*\|?\s*!{2}[^!].*)$", re.MULTILINE): medium, #Handle '!!' in mlox_base.txt re.compile(r"^(\s*\|?\s*!{3}.*)$", re.MULTILINE): high, #Handle '!!!' in mlox_base.txt re.compile(r'^(WARNING:.*)', re.MULTILINE): warning, re.compile(r'^(ERROR:.*)', re.MULTILINE): error, re.compile(r'^(\*\d+\*\s\S*\.es[mp])', re.MULTILINE): medium #Changed mod order } # for hiding spoilers hidden = [] adjust = [0] # use a mutable entity for closure "hider" def hider(match): (p1, p2) = match.span(0) delta = len(match.group(0)) - len(match.group(1)) hidden.append((p1 - adjust[0], p2 - delta - adjust[0])) adjust[0] += delta return(match.group(1)) re_hide = re.compile(r'<hide>(.*)</hide>', re.IGNORECASE) in_text = re_hide.sub(hider, in_text) out_RichTextCtrl.SetValue(in_text) # for special highlighting for (re_pat, style) in highlighters.items(): for match in re.finditer(re_pat, in_text): (start, end) = match.span(1) if style == url: url.SetURL(in_text[start:end]) out_RichTextCtrl.SetStyle((start, end), style) for where in hidden: out_RichTextCtrl.SetStyle(where, hide)