Skip to content

Instantly share code, notes, and snippets.

@jyr
Created July 2, 2009 18:59
Show Gist options
  • Save jyr/139645 to your computer and use it in GitHub Desktop.
Save jyr/139645 to your computer and use it in GitHub Desktop.
Rich Text Toolbar for html
#!/usr/bin/env python
# -*- coding: us-ascii -*-
# generated by wxGlade 0.6.3 on Wed Jun 24 01:20:43 2009
import wx
import wx.richtext as rt
import wx.html as html
import urllib
from cStringIO import StringIO
import os
import sys
# begin wxGlade: extracode
# end wxGlade
class MyToolBar(wx.ToolBar):
def __init__(self, *args, **kwds):
# begin wxGlade: MyToolBar.__init__
self.text_ctrl = kwds.pop('text_ctrl')
self.html = kwds.pop('htm')
wx.ToolBar.__init__(self, *args, **kwds)
self.path_images = '/usr/share/pixmaps/opentumblr/toolbar'
if not os.path.isdir(self.path_images):
if sys.platform == "win32":
self.path_images = os.path.abspath(os.path.dirname(__file__)) + '\\..\\images\\toolbar\\'
else:
self.path_images = os.path.abspath('images') + '/toolbar/'
self.AddLabelTool(wx.ID_BOLD, "bold", wx.Bitmap(self.path_images + "bold.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddLabelTool(wx.ID_ITALIC, "Italic", wx.Bitmap(self.path_images + "italic.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddLabelTool(wx.NewId(), "strikethrough", wx.Bitmap(self.path_images + "strikethrough.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddSeparator()
self.AddLabelTool(14, "Unordered list", wx.Bitmap(self.path_images + "unordered_list.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddLabelTool(15, "Ordered list", wx.Bitmap(self.path_images + "ordered_list.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddSeparator()
self.AddLabelTool(11, "Indent", wx.Bitmap(self.path_images + "indent.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddLabelTool(12, "Outdent", wx.Bitmap(self.path_images + "outdent.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddSeparator()
self.AddLabelTool(10, "Insert/Edit image", wx.Bitmap(self.path_images + "insert_image.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddLabelTool(13, "Insert/Edit link", wx.Bitmap(self.path_images + "insert_link.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddLabelTool(16, "Unlink", wx.Bitmap(self.path_images + "unlink.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddSeparator()
self.AddLabelTool(wx.NewId(), "Spellchecker", wx.Bitmap(self.path_images + "spellchecker.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.AddSeparator()
self.AddLabelTool(wx.NewId(), "Edit Html", wx.Bitmap(self.path_images + "insert_html.png", wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_NORMAL, "", "")
self.Bind(wx.EVT_TOOL, self.OnUpdateBold, id = wx.ID_BOLD)
self.Bind(wx.EVT_TOOL, self.OnUpdateItalic, id = wx.ID_ITALIC)
self.Bind(wx.EVT_TOOL, self.OnIndent, id = 11)
self.Bind(wx.EVT_TOOL, self.OnOutdent, id = 12)
self.Bind(wx.EVT_TOOL, self.OnInsertImage, id = 10)
self.Bind(wx.EVT_TOOL, self.OnInsertLink, id = 13)
self.Bind(wx.EVT_TOOL, self.OnBulletList, id = 14)
self.Bind(wx.EVT_TOOL, self.OnNumberList, id = 15)
self.Bind(wx.EVT_TOOL, self.OnUnLink, id = 16)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyToolBar.__set_properties
self.Realize()
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyToolBar.__do_layout
pass
# end wxGlade
def OnUpdateBold(self, event):
self.text_ctrl.ApplyBoldToSelection()
def OnUpdateItalic(self, event):
self.text_ctrl.ApplyItalicToSelection()
def OnBulletList(self, event):
self.attr = rt.TextAttrEx()
self.ip = self.text_ctrl.GetInsertionPoint()
print self.ip
self.text_ctrl.GetStyle(self.ip, self.attr)
self.attr.SetFlags(rt.TEXT_ATTR_BULLET_STYLE)
self.text_ctrl.MoveToParagraphStart()
#self.text_ctrl.MoveLeft()
self.start = self.text_ctrl.GetInsertionPoint()
self.text_ctrl.SetSelection(self.start, self.start + 1)
self.text_ctrl.DeleteSelection()
self.text_ctrl.BeginSymbolBullet(unichr(9679), 100, 60)
self.text_ctrl.Newline()
self.text_ctrl.MoveToParagraphEnd()
self.text_ctrl.EndSymbolBullet()
self.text_ctrl.MoveCaret(self.ip)
def OnNumberList(self, event):
self.attr = rt.TextAttrEx()
self.ip = self.text_ctrl.GetInsertionPoint()
print self.ip
if self.text_ctrl.GetStyle(self.ip, self.attr) and self.attr.BulletNumber > 0:
self.text_ctrl.MoveToParagraphStart()
self.start = self.text_ctrl.GetInsertionPoint()
self.text_ctrl.MoveToParagraphEnd()
self.end = self.text_ctrl.GetInsertionPoint()
r = rt.RichTextRange(self.start, self.end)
self.attr.SetFlags(rt.TEXT_ATTR_BULLET_NUMBER)
self.attr.BulletNumber += 1
self.text_ctrl.SetStyle(r,self.attr)
else:
self.text_ctrl.MoveToParagraphStart()
self.text_ctrl.MoveLeft()
self.start = self.text_ctrl.GetInsertionPoint()
self.text_ctrl.SetSelection(self.start, self.start + 1)
self.text_ctrl.DeleteSelection()
self.attr.BulletNumber += 1
self.text_ctrl.BeginNumberedBullet(self.attr.BulletNumber, 100, 60)
print " number: %i" % self.attr.BulletNumber
self.text_ctrl.Newline()
self.text_ctrl.MoveToParagraphEnd()
self.text_ctrl.EndNumberedBullet()
#self.text_ctrl.MoveCaret(self.ip)
def OnIndent(self, event):
self.attr = rt.TextAttrEx()
self.attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT)
self.ip = self.text_ctrl.GetInsertionPoint()
if self.text_ctrl.GetStyle(self.ip, self.attr):
r = rt.RichTextRange(self.ip, self.ip)
if self.text_ctrl.HasSelection():
r = self.text_ctrl.GetSelectionRange()
if self.attr.GetLeftIndent() >= 100:
self.attr.SetLeftIndent(self.attr.GetLeftIndent() - 100)
self.attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT)
self.text_ctrl.SetStyle(r, self.attr)
def OnOutdent(self, event):
self.attr = rt.TextAttrEx()
self.attr.SetLeftIndent(self.attr.GetLeftIndent()+100)
self.attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT)
self.ip = self.text_ctrl.GetInsertionPoint()
if self.text_ctrl.GetStyle(self.ip, self.attr):
r = rt.RichTextRange(self.ip, self.ip)
self.attr.SetLeftIndent(self.attr.GetLeftIndent() + 100)
self.attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT)
self.text_ctrl.SetStyle(r, self.attr)
def OnInsertLink(self, event):
self.attr = rt.TextAttrEx()
self.attr.SetTextColour(wx.BLUE)
self.attr.SetFontUnderlined(True)
self.dlg = wx.TextEntryDialog(self, "Link Url")
if self.dlg.ShowModal():
self.link = self.dlg.GetValue()
self.attr.SetURL(self.link)
if self.text_ctrl.HasSelection():
r = self.text_ctrl.GetSelectionRange()
self.text_ctrl.SetInsertionPointEnd()
self.text_ctrl.SetStyleEx(r, self.attr)
self.dlg.Destroy()
def OnUnLink(self, event):
self.attr = rt.TextAttrEx()
self.attr.SetTextColour(wx.BLACK)
self.attr.SetFontUnderlined(False)
self.attr.SetURL('')
r = self.text_ctrl.GetSelectionRange()
self.text_ctrl.SetStyleEx(r, self.attr, flags = rt.RICHTEXT_SETSTYLE_REMOVE)
self.text_ctrl.SetInsertionPointEnd()
def OnInsertImage(self, event):
self.dlg = wx.TextEntryDialog(self, "image URL")
if self.dlg.ShowModal():
url = self.dlg.GetValue()
fp = urllib.urlopen(url)
data = fp.read()
fp.close()
self.image = wx.ImageFromStream(StringIO(data)).ConvertToBitmap()
self.text_ctrl.WriteBitmap(self.image)
self.dlg.Destroy()
# end of class MyToolBar
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
#self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
self.text_ctrl_1 = rt.RichTextCtrl(self, -1, "")
self.html = html.HtmlWindow(self, -1)
self.toolbar = MyToolBar(self, text_ctrl = self.text_ctrl_1, htm = self.html)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
self.text_ctrl_1.SetMinSize((300,200))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.toolbar, 0, wx.EXPAND|wx.ALL, 6)
sizer_1.Add(self.text_ctrl_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
# end wxGlade
# end of class MyFrame
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment