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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#! /usr/bin/env python
# LabelThoughts.py
# This file is part of Labyrinth
#
# Copyright (C) 2010 - Jorge Saldivar <jsaldivar@paraguayeduca.org>
# Martin Abente <mabente@paraguayeduca.org>
#
# Labyrinth is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Labyrinth is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Labyrinth; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
#
import gtk
import pango
import utils
import xml.dom
from BaseThought import *
from TextThought import TextThought
class LabelThought (TextThought):
def __init__ (self, coords, pango_context, thought_number, save, undo, loading, background_color, foreground_color, name="label_thought"):
super (LabelThought, self).__init__(coords, pango_context, thought_number, save, undo, loading, background_color, foreground_color, name)
self.edge = True
def can_be_parent(self):
return False
def draw (self, context):
self.recalc_edges ()
if self.edge:
ResizableThought.draw(self, context)
if self.creating:
return
(textx, texty) = (self.min_x, self.min_y)
if self.am_primary:
r, g, b = utils.primary_colors["text"]
elif (self.foreground_color):
r, g, b = utils.gtk_to_cairo_color(self.foreground_color)
else:
r, g ,b = utils.gtk_to_cairo_color(utils.default_colors["text"])
context.set_source_rgb (r, g, b)
self.layout.set_alignment(pango.ALIGN_CENTER)
context.move_to (textx, texty)
context.show_layout (self.layout)
if self.editing:
if self.preedit:
(strong, weak) = self.layout.get_cursor_pos (self.index + self.preedit[2])
else:
(strong, weak) = self.layout.get_cursor_pos (self.index)
(startx, starty, curx,cury) = strong
startx /= pango.SCALE
starty /= pango.SCALE
curx /= pango.SCALE
cury /= pango.SCALE
context.move_to (textx + startx, texty + starty)
context.line_to (textx + startx, texty + starty + cury)
context.stroke ()
context.set_source_rgb (0,0,0)
context.stroke ()
def update_save (self):
next = self.element.firstChild
while next:
m = next.nextSibling
if next.nodeName == "attribute":
self.element.removeChild (next)
next.unlink ()
next = m
if self.text_element.parentNode is not None:
self.text_element.replaceWholeText (self.text)
text = self.extended_buffer.get_text ()
if text:
self.extended_buffer.update_save()
else:
try:
self.element.removeChild(self.extended_buffer.element)
except xml.dom.NotFoundErr:
pass
self.element.setAttribute ("cursor", str(self.index))
self.element.setAttribute ("ul-coords", str(self.ul))
self.element.setAttribute ("lr-coords", str(self.lr))
self.element.setAttribute ("identity", str(self.identity))
self.element.setAttribute ("background-color", utils.color_to_string(self.background_color))
self.element.setAttribute ("foreground-color", utils.color_to_string(self.foreground_color))
self.element.setAttribute ("edge", str(self.edge))
if self.am_selected:
self.element.setAttribute ("current_root", "true")
else:
try:
self.element.removeAttribute ("current_root")
except xml.dom.NotFoundErr:
pass
if self.am_primary:
self.element.setAttribute ("primary_root", "true");
else:
try:
self.element.removeAttribute ("primary_root")
except xml.dom.NotFoundErr:
pass
doc = self.element.ownerDocument
it = self.attributes.get_iterator()
while (1):
r = it.range()
for x in it.get_attrs():
if x.type == pango.ATTR_WEIGHT and x.value == pango.WEIGHT_BOLD:
elem = doc.createElement ("attribute")
self.element.appendChild (elem)
elem.setAttribute("start", str(r[0]))
elem.setAttribute("end", str(r[1]))
elem.setAttribute("type", "bold")
elif x.type == pango.ATTR_STYLE and x.value == pango.STYLE_ITALIC:
elem = doc.createElement ("attribute")
self.element.appendChild (elem)
elem.setAttribute("start", str(r[0]))
elem.setAttribute("end", str(r[1]))
elem.setAttribute("type", "italics")
elif x.type == pango.ATTR_UNDERLINE and x.value == pango.UNDERLINE_SINGLE:
elem = doc.createElement ("attribute")
self.element.appendChild (elem)
elem.setAttribute("start", str(r[0]))
elem.setAttribute("end", str(r[1]))
elem.setAttribute("type", "underline")
elif x.type == pango.ATTR_FONT_DESC:
elem = doc.createElement ("attribute")
self.element.appendChild (elem)
elem.setAttribute("start", str(r[0]))
elem.setAttribute("end", str(r[1]))
elem.setAttribute("type", "font")
elem.setAttribute("value", x.desc.to_string ())
if not it.next():
break
def load (self, node, tar):
self.index = int (node.getAttribute ("cursor"))
self.end_index = self.index
tmp = node.getAttribute ("ul-coords")
self.ul = utils.parse_coords (tmp)
tmp = node.getAttribute ("lr-coords")
self.lr = utils.parse_coords (tmp)
self.width = self.lr[0] - self.ul[0]
self.height = self.lr[1] - self.ul[1]
self.identity = int (node.getAttribute ("identity"))
try:
tmp = node.getAttribute ("background-color")
self.background_color = gtk.gdk.color_parse(tmp)
tmp = node.getAttribute ("foreground-color")
self.foreground_color = gtk.gdk.color_parse(tmp)
except ValueError:
pass
self.am_selected = node.hasAttribute ("current_root")
self.am_primary = node.hasAttribute ("primary_root")
if node.getAttribute ("edge") == "True":
self.edge = True
else:
self.edge = False
for n in node.childNodes:
if n.nodeType == n.TEXT_NODE:
self.text = n.data
elif n.nodeName == "Extended":
self.extended_buffer.load(n)
elif n.nodeName == "attribute":
attrType = n.getAttribute("type")
start = int(n.getAttribute("start"))
end = int(n.getAttribute("end"))
if attrType == "bold":
attr = pango.AttrWeight(pango.WEIGHT_BOLD, start, end)
elif attrType == "italics":
attr = pango.AttrStyle(pango.STYLE_ITALIC, start, end)
elif attrType == "underline":
attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE, start, end)
elif attrType == "font":
font_name = str(n.getAttribute("value"))
pango_font = pango.FontDescription (font_name)
attr = pango.AttrFontDesc (pango_font, start, end)
self.attributes.change(attr)
else:
print "Unknown: "+n.nodeName
self.rebuild_byte_table ()
self.recalc_edges()
def enter(self):
if self.editing:
return
self.orig_text = self.text
self.editing = True
self.edge = True
def leave(self):
if not self.editing:
return
ResizableThought.leave(self)
self.editing = False
self.end_index = self.index
self.emit ("update_links")
self.edge = False
self.recalc_edges ()
|