Fix up confusion with relayering after movement drags in
[ardour.git] / gtk2_ardour / canvas-note-event.cc
index 0e1afcc01774bcd6379d35e3209419a6b2b6b8fc..75f9ed0d066b6756d929d41c55edaa6dbaa7afe9 100644 (file)
@@ -1,6 +1,6 @@
 /*
     Copyright (C) 2007 Paul Davis
-    Author: Dave Robillard
+    Author: David Robillard
 
     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
@@ -55,6 +55,7 @@ CanvasNoteEvent::CanvasNoteEvent(MidiRegionView& region, Item* item, const boost
        , _valid (true)
        , _mouse_x_fraction (-1.0)
        , _mouse_y_fraction (-1.0)
+       , _channel_selection (0xffff)
 {
 }
 
@@ -113,14 +114,11 @@ CanvasNoteEvent::hide_velocity()
 void
 CanvasNoteEvent::on_channel_selection_change(uint16_t selection)
 {
-       // make note change its color if its channel is not marked active
-       if ( (selection & (1 << _note->channel())) == 0 ) {
-               set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get());
-               set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get()));
-       } else {
-               // set the color according to the notes selection state
-               set_selected(_selected);
-       }
+       _channel_selection = selection;
+       
+       /* this takes into account whether or not the note should be drawn as inactive */
+       set_selected (_selected);
+
        // this forces the item to update..... maybe slow...
        _item->hide();
        _item->show();
@@ -138,7 +136,7 @@ void
 CanvasNoteEvent::show_channel_selector(void)
 {
        if (_channel_selector_widget == 0) {
-         
+
                if(_region.channel_selector_scoped_note() != 0){
                    _region.channel_selector_scoped_note()->hide_channel_selector();
                    _region.set_channel_selector_scoped_note(0);
@@ -162,7 +160,7 @@ CanvasNoteEvent::show_channel_selector(void)
                _channel_selector_widget->property_width() = 100;
                _channel_selector_widget->raise_to_top();
                _channel_selector_widget->show();
-               
+
                _region.set_channel_selector_scoped_note(this);
        } else {
                hide_channel_selector();
@@ -187,20 +185,31 @@ CanvasNoteEvent::set_selected(bool selected)
        }
 
        _selected = selected;
-       set_fill_color (base_color ());
-        
-       if (_selected) {
+
+       bool const active = (_channel_selection & (1 << _note->channel())) != 0;
+
+       if (_selected && active) {
                set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get()));
-               
+
                if(_region.channel_selector_scoped_note() != 0){
                    _region.channel_selector_scoped_note()->hide_channel_selector();
                    _region.set_channel_selector_scoped_note(0);
                }
+
+               set_fill_color (base_color ());
+
        } else {
-               set_outline_color(calculate_outline(base_color()));
+
+               if (active) {
+                       set_fill_color(base_color());
+                       set_outline_color(calculate_outline(base_color()));
+               } else {
+                       set_fill_color(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get());
+                       set_outline_color(calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteInactiveChannel.get()));
+               }
+               
                hide_channel_selector();
        }
-
 }
 
 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
@@ -223,13 +232,13 @@ CanvasNoteEvent::base_color()
                                                 SCALE_USHORT_TO_UINT8_T(color.get_red()),
                                                 SCALE_USHORT_TO_UINT8_T(color.get_green()),
                                                 SCALE_USHORT_TO_UINT8_T(color.get_blue()),
-                                                opacity), 
+                                                opacity),
                                         ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
        }
 
        case ChannelColors:
                return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (CanvasNoteEvent::midi_channel_colors[_note->channel()],
-                                                            opacity), 
+                                                            opacity),
                                         ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(), 0.5);
 
        default:
@@ -292,7 +301,14 @@ CanvasNoteEvent::set_mouse_fractions (GdkEvent* ev)
        _mouse_y_fraction = yf;
 
        if (notify) {
-               _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
+                if (big_enough_to_trim()) {
+                        _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
+                } else {
+                        /* pretend the mouse is in the middle, because this is not big enough
+                           to trim right now.
+                        */
+                        _region.note_mouse_position (0.5, 0.5, set_cursor);
+                }
        }
 }
 
@@ -347,6 +363,12 @@ CanvasNoteEvent::mouse_near_ends () const
                (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
 }
 
+bool
+CanvasNoteEvent::big_enough_to_trim () const
+{
+        return (x2() - x1()) > 10; /* canvas units, really pixels */
+}
+
 } // namespace Canvas
 } // namespace Gnome