Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/chat/richtext.py
blob: 6c6c59287cd14305112270062a3a1e23152ea8f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import pango
import xml.sax

class RichTextBuffer(gtk.TextBuffer):
	def __init__(self):
		gtk.TextBuffer.__init__(self)

		self.connect_after("insert-text", self.__insert_text_cb)
		
		self.__create_tags()
		self.active_tags = []
		
	def apply_tag(self, tag_name):
		self.active_tags.append(tag_name)
		
		bounds = self.get_selection_bounds()
		if bounds:
			[start, end] = bounds
			self.apply_tag_by_name(tag_name, start, end)

	def unapply_tag(self, tag_name):
		self.active_tags.remove(tag_name)

		[start, end] = self.get_selection_bounds()
		self.remove_tag_by_name(tag_name, start, end)
	
	def __create_tags(self):
		tag = self.create_tag("bold")
		tag.set_property("weight", pango.WEIGHT_BOLD)
		
		tag = self.create_tag("italic")
		tag.set_property("style", pango.STYLE_ITALIC)
	
	def __insert_text_cb(self, widget, pos, text, length):
		for tag in self.active_tags:
				pos_end = pos.copy()
				pos_end.backward_chars(length)
				self.apply_tag_by_name(tag, pos, pos_end)
		
class RichTextToolbar(gtk.Toolbar):
	def __init__(self, buf):
		gtk.Toolbar.__init__(self)
		
		self.buf = buf
		
		self.set_style(gtk.TOOLBAR_ICONS)
		
		item = gtk.ToggleToolButton(gtk.STOCK_BOLD)
		item.connect("toggled", self.__toggle_style_cb, "bold")
		self.insert(item, -1)
		item.show()

		item = gtk.ToggleToolButton(gtk.STOCK_ITALIC)
		item.connect("toggled", self.__toggle_style_cb, "italic")
		self.insert(item, -1)
		item.show()
	
	def __toggle_style_cb(self, toggle, tag_name):
		if toggle.get_active():
			self.buf.apply_tag(tag_name)
		else:
			self.buf.unapply_tag(tag_name)
 
class RichTextHandler(xml.sax.handler.ContentHandler):
	def __init__(self, serializer, buf):
		self.buf = buf
		self.serializer = serializer
		self.tags = []

	def startElement(self, name, attributes):
		tag = self.serializer.deserialize_element(name)
		if tag:
			self.tags.append(tag)
 
	def characters(self, data):
		start_it = it = self.buf.get_end_iter()
		mark = self.buf.create_mark(None, start_it, True)
		self.buf.insert(it, data)
		start_it = self.buf.get_iter_at_mark(mark)

		for tag in self.tags:
			self.buf.apply_tag_by_name(tag, start_it, it)
 
	def endElement(self, name):
		tag = self.serializer.deserialize_element(name)
		if tag:
			self.tags.remove(tag)

class RichTextSerializer:
	def __init__(self):
		self._open_tags = []

	def deserialize_element(self, el_name):
		if el_name == "bold":
			return "bold"
		elif el_name == "italic":
			return "italic"
		else:
			return None
	
	def serialize_tag_start(self, tag):
		if tag.get_property("name") == "bold":
			return "<bold>"
		elif tag.get_property("name") == "italic":
			return "<italic>"
		else:
			return "<unknown>"

	def serialize_tag_end(self, tag):
		if tag.get_property("name") == "bold":
			return "</bold>"
		elif tag.get_property("name") == "italic":
			return "</italic>"
		else:
			return "</unknown>"
			
	def serialize(self, buf):
		xml = "<richtext>"

		next_it = buf.get_start_iter()
		while not next_it.is_end():
			it = next_it.copy()
			if not next_it.forward_to_tag_toggle(None):
				next_it = buf.get_end_iter()

			tags_to_reopen = []

			for tag in it.get_toggled_tags(False):
				while 1:
					open_tag = self._open_tags.pop()
					xml += self.serialize_tag_end(tag)
					if open_tag == tag:
						break						
					tags_to_reopen.append(open_tag)
					
			for tag in tags_to_reopen + it.get_toggled_tags(True):
				self._open_tags.append(tag)
				xml += self.serialize_tag_start(tag)
			
			xml += buf.get_text(it, next_it)

		if next_it.is_end():
			self._open_tags.reverse()
			for tag in self._open_tags:
				xml += self.serialize_tag_end(tag)
		
		xml += "</richtext>"
		
		return xml

	def deserialize(self, xml_string, buf):
		parser = xml.sax.make_parser()
		handler = RichTextHandler(self, buf)
		parser.setContentHandler(handler)
		parser.feed(xml_string)
		parser.close()

def test_quit(window, rich_buf):
	print RichTextSerializer().serialize(rich_buf)
	gtk.main_quit()

if __name__ == "__main__":
	window = gtk.Window()
	window.set_default_size(400, 300)
	
	vbox = gtk.VBox()
	
	rich_buf = RichTextBuffer()

	xml_string = "<richtext>"	

	#xml_string += "<bold><italic>Test</italic>one</bold>\n"
	xml_string += "<bold><italic>Test two</italic></bold>"

	xml_string += "</richtext>"


	RichTextSerializer().deserialize(xml_string, rich_buf)
	
	view = gtk.TextView(rich_buf)
	vbox.pack_start(view)
	view.show()
	
	toolbar = RichTextToolbar(rich_buf)
	vbox.pack_start(toolbar, False)
	toolbar.show()
	
	window.add(vbox)
	vbox.show()
	
	window.show()
	
	window.connect("destroy", test_quit, rich_buf)

	gtk.main()