fix some aspects of midi region view event handling
[ardour.git] / gtk2_ardour / canvas-midi-event.cc
1 /*
2     Copyright (C) 2007 Paul Davis 
3     Author: Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <iostream>
21 #include "canvas-midi-event.h"
22 #include "midi_region_view.h"
23 #include "public_editor.h"
24 #include "editing_syms.h"
25 #include "keyboard.h"
26
27 using namespace std;
28
29 namespace Gnome {
30 namespace Canvas {
31
32
33 CanvasMidiEvent::CanvasMidiEvent(MidiRegionView& region, Item* item)
34         : _region(region)
35         , _item(item)
36         , _state(None)
37 {       
38 }
39
40
41 bool
42 CanvasMidiEvent::on_event(GdkEvent* ev)
43 {
44         static double last_x, last_y;
45         double event_x, event_y;
46
47         if (_region.get_time_axis_view().editor.current_mouse_mode() != Editing::MouseNote)
48                 return false;
49
50         switch (ev->type) {
51         case GDK_ENTER_NOTIFY:
52                 cerr << "ENTERED: " << ev->crossing.state << endl;
53                 Keyboard::magic_widget_grab_focus();
54                 _item->grab_focus();
55                 /*if ( (ev->crossing.state & GDK_BUTTON2_MASK) ) {
56
57                 }*/
58                 break;
59
60         case GDK_LEAVE_NOTIFY:
61                 cerr << "LEAVE: " << ev->crossing.state << endl;
62                 Keyboard::magic_widget_drop_focus();
63                 //_item->drop_focus();
64                 break;
65         
66         case GDK_KEY_PRESS:
67                 cerr << "EVENT KEY PRESS\n"; // doesn't work :/
68                 break;
69
70         case GDK_BUTTON_PRESS:
71                 _state = Pressed;
72                 return true;
73
74         case GDK_MOTION_NOTIFY:
75                 event_x = ev->motion.x;
76                 event_y = ev->motion.y;
77                 cerr << "MOTION @ " << event_x << ", " << event_y << endl;
78                 _item->property_parent().get_value()->w2i(event_x, event_y);
79
80                 switch (_state) {
81                 case Pressed:
82                         _item->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
83                                         Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
84                         _state = Dragging;
85                         last_x = event_x;
86                         last_y = event_y;
87                         return true;
88                 case Dragging:
89                         if (ev->motion.is_hint) {
90                                 int t_x;
91                                 int t_y;
92                                 GdkModifierType state;
93                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
94                                 event_x = t_x;
95                                 event_y = t_y;
96                         }
97
98                         _item->move(event_x - last_x, event_y - last_y);
99
100                         last_x = event_x;
101                         last_y = event_y;
102
103                         return true;
104                 default:
105                         break;
106                 }
107                 break;
108         
109         case GDK_BUTTON_RELEASE:
110                 switch (_state) {
111                 case Pressed: // Clicked
112                         _state = None;
113                         return true;
114                 case Dragging: // Dropped
115                         _item->ungrab(ev->button.time);
116                         _state = None;
117                         return true;
118                 default:
119                         break;
120                 }
121
122         default:
123                 break;
124         }
125
126         return false;
127 }
128
129 } // namespace Canvas
130 } // namespace Gnome
131