6 class Notation_toolbar (gtk.VBox):
7 def __init__ (self, notation, check_refresh_callback):
8 gtk.VBox.__init__ (self)
11 self.notation = notation
14 self.check_refresh_callback = check_refresh_callback
16 def get_row (self, row):
17 if not self.rows.has_key (row):
19 self.pack_start (r, expand = False)
26 def click_callback (self, widget):
27 if not self.button_dict.has_key (widget):
28 print 'no such widget?'
31 cb = self.button_dict[widget]
33 self.check_refresh_callback()
36 def keypress_callback (self, widget, event):
38 name = gtk.gdk.keyval_name (key)
40 if event.get_state () & gtk.gdk.SHIFT_MASK:
41 name = 'Shift+' + name
42 if event.get_state () & gtk.gdk.CONTROL_MASK:
44 if not self.key_dict.has_key (name):
45 print 'no such key?', name
48 button = self.key_dict[name]
49 button.do_activate (button)
52 def add_button (self, text, key, callback, row_id):
54 row = self.get_row (row_id)
55 row.pack_start (b, expand=True)
57 b.connect ('clicked', self.click_callback)
58 b.set_focus_on_click (False)
59 self.key_dict[key] = b
60 self.button_dict[b] = callback
63 def add_buttons (self):
64 for (key_name, text, func, row) in \
66 lambda: self.notation.print_score(), 0),
68 lambda: gtk.main_quit(), 0),
70 lambda: self.notation.cursor_move (-1), 0),
72 lambda: self.notation.cursor_move (1), 0),
74 lambda: self.notation.add_note (), 0),
75 ('BackSpace', 'backspace',
76 lambda: self.notation.backspace (), 0),
78 lambda: self.notation.change_alteration (2), 1),
80 lambda: self.notation.change_alteration (-2), 1),
82 lambda: self.notation.change_step (1), 1),
84 lambda: self.notation.change_step (-1), 1),
85 ('apostrophe', 'oct up',
86 lambda: self.notation.change_octave (1), 1),
88 lambda: self.notation.change_octave (-1), 1),
90 lambda: self.notation.change_dots (), 1),
92 lambda: self.notation.change_duration_log (1), 1),
93 ('Shift+asterisk', 'longer',
94 lambda: self.notation.change_duration_log (-1), 1),
96 lambda: self.notation.ensure_rest (), 1),
98 lambda: self.notation.add_step (0), 2),
100 lambda: self.notation.add_step (1), 2),
102 lambda: self.notation.add_step (2), 2),
104 lambda: self.notation.add_step (3), 2),
106 lambda: self.notation.add_step (4), 2),
108 lambda: self.notation.add_step (5), 2),
110 lambda: self.notation.add_step (6), 2),
112 lambda: self.notation.set_step (0), 3),
114 lambda: self.notation.set_step (1), 3),
116 lambda: self.notation.set_step (2), 3),
118 lambda: self.notation.set_step (3), 3),
120 lambda: self.notation.set_step (4), 3),
122 lambda: self.notation.set_step (5), 3),
124 lambda: self.notation.set_step (6), 3)]:
126 self.add_button (text, key_name, func, row)
129 class Notation_canvas (gnomecanvas.Canvas):
130 """The canvas for drawing notation symbols."""
132 def __init__ (self, canvas_controller):
133 gnomecanvas.Canvas.__init__ (self,
137 self.set_size_request (w, h)
138 self.set_scroll_region (0, 0, w, h)
140 root.affine_relative ((1,0,0,-1,0, 5))
141 self.pixel_scale = 10
142 self.set_pixels_per_unit (self.pixel_scale)
143 i = root.add (gnomecanvas.CanvasRect,
146 fill_color = 'white', outline_color = 'white')
147 i.notation_item = None
148 self.notation_canvas_controller = canvas_controller
149 self.create_cursor ()
151 def create_cursor (self):
152 type = gnomecanvas.CanvasRect
153 w = self.root ().add (type,
154 fill_color = 'lightblue',
155 outline_color = 'lightblue')
156 w.notation_item = None
158 self.cursor_widget = w
160 def set_cursor (self, notation_item):
161 if not notation_item.bbox:
165 (x1, y1, x2, y2) = notation_item.bbox
166 self.cursor_widget.set (x1 = x1,
172 def item_set_active_state (self, item, active):
177 item.set (fill_color = color)
179 def click_event (self, widget, event = None):
180 if event <> None and event.type == gtk.gdk.BUTTON_PRESS:
181 if event.button == 1 and widget.notation_item.name in ('Rest', 'NoteHead'):
183 notat = self.notation_canvas_controller.notation
184 notat.set_cursor (widget.notation_item.music_expression)
185 self.notation_canvas_controller.check_update()
190 def register_notation_canvas_item (self, citem):
191 if citem.notation_item and citem.notation_item.music_expression:
192 citem.connect ("event", self.click_event)
194 def set_cursor_to_music (self, music_expression):
195 c_items = [it for it in self.root().item_list if
197 and it.notation_item.music_expression
198 == music_expression)]
200 c_items = filter (lambda it: it.notation_item.name in ('NoteHead', 'Rest'),
203 self.set_cursor (c_items[0].notation_item)
205 def zoom (self, delta):
206 fact = pow (1.25, delta)
207 self.pixel_scale *= fact
208 self.set_pixels_per_unit (self.pixel_scale)
210 class Notation_canvas_controller:
211 """The connection between canvas and the abstract notation graphics."""
213 def __init__ (self, notation):
214 self.canvas = Notation_canvas (self)
215 self.notation = notation
217 def update_cursor (self):
218 self.canvas.set_cursor_to_music (self.notation.music_cursor)
220 def update_canvas (self):
221 self.notation.paint_on_canvas (self.canvas)
223 def check_update (self):
224 self.notation.check_update ()
225 if self.notation.touched:
226 self.update_canvas ()
227 elif self.notation.cursor_touched:
228 self.update_cursor ()