Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 31da8822170b3c5e69018479f62355fd17ec3dcb

  • avatar
  • Marco Pesenti Gritti <mpg @red…at.com>
  • Thu Apr 20 20:55:59 EDT 2006
Initial rich text code
  
1#!/usr/bin/env python
2
3import pygtk
4pygtk.require('2.0')
5import gtk
6import pango
7
8class RichTextBuffer(gtk.TextBuffer):
9 def __init__(self):
10 gtk.TextBuffer.__init__(self)
11
12 self.connect_after("insert-text", self.__insert_text_cb)
13
14 self.__create_tags()
15 self.active_tags = []
16
17 def apply_tag(self, tag_name):
18 self.active_tags.append(tag_name)
19
20 def unapply_tag(self, tag_name):
21 self.active_tags.remove(tag_name)
22
23 def __create_tags(self):
24 tag = self.create_tag("bold")
25 tag.set_property("weight", pango.WEIGHT_BOLD)
26
27 tag = self.create_tag("italic")
28 tag.set_property("style", pango.STYLE_ITALIC)
29
30 def __insert_text_cb(self, widget, pos, text, length):
31 for tag in self.active_tags:
32 pos_end = pos.copy()
33 pos_end.backward_chars(length)
34 self.apply_tag_by_name(tag, pos, pos_end)
35
36 def to_xml():
37 next_iter = buffer.get_start_iter()
38 while not next_iter.is_end():
39
40
41
42class RichTextToolbar(gtk.Toolbar):
43 def __init__(self, buf):
44 gtk.Toolbar.__init__(self)
45
46 self.buf = buf
47
48 item = gtk.ToggleToolButton(gtk.STOCK_BOLD)
49 item.connect("toggled", self.__toggle_style_cb, "bold")
50 self.insert(item, -1)
51 item.show()
52
53 item = gtk.ToggleToolButton(gtk.STOCK_ITALIC)
54 item.connect("toggled", self.__toggle_style_cb, "italic")
55 self.insert(item, -1)
56 item.show()
57
58 def __toggle_style_cb(self, toggle, tag_name):
59 if toggle.get_active():
60 self.buf.apply_tag(tag_name)
61 else:
62 self.buf.unapply_tag(tag_name)
63
64def test_quit(window, rich_buf):
65 print rich_buf.to_xml()
66 gtk.main_quit()
67
68if __name__ == "__main__":
69 window = gtk.Window()
70 window.set_default_size(400, 300)
71
72 vbox = gtk.VBox()
73
74 rich_buf = RichTextBuffer()
75
76 view = gtk.TextView(rich_buf)
77 vbox.pack_start(view)
78 view.show()
79
80 toolbar = RichTextToolbar(rich_buf)
81 vbox.pack_start(toolbar, False)
82 toolbar.show()
83
84 window.add(vbox)
85 vbox.show()
86
87 window.show()
88
89 window.connect("destroy", test_quit, rich_buf)
90
91 gtk.main()