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
|
# Copyright (C) 2009, Tomeu Vizoso
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import gobject
import gtk
from sugar import dispatch
class Canvas(gtk.DrawingArea):
__gtype_name__ = 'MindMapCanvas'
def __init__(self):
self._elements = []
self._pressed_button = None
self._press_start_x = None
self._press_start_y = None
self._last_clicked_element = None
self._dragging = False
gobject.GObject.__init__(self)
self.dragging_started = dispatch.Signal()
self.dragging_finished = dispatch.Signal()
self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.POINTER_MOTION_HINT_MASK)
self.connect('expose-event', self.__expose_event_cb)
self.connect('button-press-event', self.__button_press_event_cb)
self.connect('button-release-event', self.__button_release_event_cb)
self.connect('motion-notify-event', self.__motion_notify_event_cb)
def __expose_event_cb(self, widget, event):
context = self.window.cairo_create()
rect = self.get_allocation()
rect.x = 0
rect.y = 0
context.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
context.clip()
context.set_source_rgb(1.0, 1.0, 1.0)
context.paint()
for element in self._elements:
rect = element.get_rect()
if rect.intersect(event.area):
context.save()
context.rectangle(rect.x, rect.y, rect.width, rect.height)
context.clip()
element.draw(context)
context.restore()
return False
def get_element_at(self, x, y):
for element in self._elements:
rect = element.get_rect()
if (x >= rect.x and x <= rect.x + rect.width) and \
(y >= rect.y and y <= rect.y + rect.height):
return element
return None
def _invalidate(self, x, y, width, height):
if self.window is None:
return
rect = gtk.gdk.Rectangle(x, y, width, height)
self.window.invalidate_rect(rect, True)
self.window.process_updates(True)
def add_element(self, element):
element.invalidated.connect(self.__element_invalidated_cb)
self._elements.append(element)
rect = element.get_rect()
self._invalidate(rect.x, rect.y, rect.width, rect.height)
def remove_all_elements(self):
rect = gtk.gdk.Rectangle(0, 0, 0, 0)
for element in self._elements:
rect.union(element.get_rect())
self._invalidate(rect.x, rect.y, rect.width, rect.height)
self._elements = []
def __element_invalidated_cb(self, **kwargs):
element = kwargs['sender']
rect = element.previous_rect
self._invalidate(rect.x, rect.y, rect.width, rect.height)
rect = element.get_rect()
self._invalidate(rect.x, rect.y, rect.width, rect.height)
def __button_press_event_cb(self, widget, event):
logging.debug('button_press_event_cb')
if event.button == 1 and event.type == gtk.gdk.BUTTON_PRESS:
self._last_clicked_element = self.get_element_at(event.x, event.y)
logging.debug('button_press_event_cb %r' % self._last_clicked_element)
if self._last_clicked_element is not None:
self._pressed_button = event.button
self._press_start_x = event.x
self._press_start_y = event.y
return False
def __button_release_event_cb(self, widget, event):
logging.debug('button_release_event_cb')
if self._dragging:
self.dragging_finished.send(self, position=(event.x, event.y),
element=self._last_clicked_element)
self._cleanup_drag()
return True
return False
def _cleanup_drag(self):
self._last_clicked_element = None
self._pressed_button = None
self._press_start_x = None
self._press_start_y = None
self._dragging = False
def __motion_notify_event_cb(self, widget, event):
if self._dragging or self._last_clicked_element is None:
return True
# if the mouse button is not pressed, no drag should occurr
if not event.state & gtk.gdk.BUTTON1_MASK:
self._cleanup_drag()
return True
logging.debug('motion_notify_event_cb')
if event.is_hint:
x, y, state_ = event.window.get_pointer()
else:
x = event.x
y = event.y
if self.drag_check_threshold(int(self._press_start_x),
int(self._press_start_y),
int(x),
int(y)):
self._dragging = True
self.dragging_started.send(self, position=(x, y),
element=self._last_clicked_element)
return True
class CanvasElement(object):
def __init__(self):
self.invalidated = dispatch.Signal()
self.previous_rect = (0, 0, 0, 0)
def draw(self, context):
self.previous_rect = self.get_rect()
def invalidate(self):
self.invalidated.send(self)
def get_rect(self):
pass
|