| 1 |
/********************************************************************** |
| 2 |
|
| 3 |
Audacity: A Digital Audio Editor |
| 4 |
|
| 5 |
NoteTrackButtonHandle.cpp |
| 6 |
|
| 7 |
Paul Licameli split from TrackPanel.cpp |
| 8 |
|
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifdef USE_MIDI |
| 14 |
#include "NoteTrackButtonHandle.h" |
| 15 |
|
| 16 |
#include "../../../../HitTestResult.h" |
| 17 |
#include "NoteTrackControls.h" |
| 18 |
#include "../../../../TrackPanelMouseEvent.h" |
| 19 |
#include "NoteTrack.h" |
| 20 |
#include "ProjectHistory.h" |
| 21 |
#include "../../../../RefreshCode.h" |
| 22 |
#include "../../../ui/CommonTrackInfo.h" |
| 23 |
|
| 24 |
#include <wx/event.h> |
| 25 |
|
| 26 |
NoteTrackButtonHandle::NoteTrackButtonHandle |
| 27 |
( const std::shared_ptr<NoteTrack> &pTrack, |
| 28 |
int channel, const wxRect &rect ) |
| 29 |
: mpTrack{ pTrack } |
| 30 |
, mChannel{ channel } |
| 31 |
, mRect{ rect } |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
void NoteTrackButtonHandle::Enter(bool, AudacityProject *) |
| 36 |
{ |
| 37 |
mChangeHighlight = RefreshCode::RefreshCell; |
| 38 |
} |
| 39 |
|
| 40 |
NoteTrackButtonHandle::~NoteTrackButtonHandle() |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
std::shared_ptr<const Track> NoteTrackButtonHandle::FindTrack() const |
| 45 |
{ |
| 46 |
return mpTrack.lock(); |
| 47 |
} |
| 48 |
|
| 49 |
UIHandle::Result NoteTrackButtonHandle::NeedChangeHighlight |
| 50 |
(const NoteTrackButtonHandle &oldState, const NoteTrackButtonHandle &newState) |
| 51 |
{ |
| 52 |
if (oldState.GetChannel() != newState.GetChannel()) |
| 53 |
// Repaint whenever the highlighted button is different |
| 54 |
return RefreshCode::RefreshCell; |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static int FindChannelNumber(const wxRect &rect, int mx, int my) |
| 59 |
{ |
| 60 |
wxASSERT_MSG(rect.width % 4 == 0, "Midi channel control rect width must be divisible by 4"); |
| 61 |
wxASSERT_MSG(rect.height % 4 == 0, "Midi channel control rect height must be divisible by 4"); |
| 62 |
|
| 63 |
auto cellWidth = rect.width / 4; |
| 64 |
auto cellHeight = rect.height / 4; |
| 65 |
|
| 66 |
int col = (mx - rect.x) / cellWidth; |
| 67 |
int row = (my - rect.y) / cellHeight; |
| 68 |
|
| 69 |
return row * 4 + col; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
// Handles clicking within the midi controls rect (same as DrawLabelControls). |
| 74 |
// This is somewhat oddly written, as these aren't real buttons - they act |
| 75 |
// when the mouse goes down; you can't hold it pressed and move off of it. |
| 76 |
// Left-clicking toggles a single channel; right-clicking turns off all other channels. |
| 77 |
static bool LabelClick( |
| 78 |
NoteTrack &track, const wxRect &rect, int mx, int my, bool right) |
| 79 |
{ |
| 80 |
auto channel = FindChannelNumber(rect, mx, my); |
| 81 |
if (right) |
| 82 |
track.SoloVisibleChan(channel); |
| 83 |
else |
| 84 |
track.ToggleVisibleChan(channel); |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
UIHandlePtr NoteTrackButtonHandle::HitTest |
| 90 |
(std::weak_ptr<NoteTrackButtonHandle> &holder, |
| 91 |
const wxMouseState &state, const wxRect &rect, |
| 92 |
const std::shared_ptr<NoteTrack> &pTrack) |
| 93 |
{ |
| 94 |
wxRect midiRect; |
| 95 |
NoteTrackControls::GetMidiControlsRect(rect, midiRect); |
| 96 |
if ( CommonTrackInfo::HideTopItem( rect, midiRect ) ) |
| 97 |
return {}; |
| 98 |
if (midiRect.Contains(state.m_x, state.m_y)) { |
| 99 |
auto channel = FindChannelNumber(midiRect, state.m_x, state.m_y); |
| 100 |
auto result = std::make_shared<NoteTrackButtonHandle>( |
| 101 |
pTrack, channel, midiRect ); |
| 102 |
result = AssignUIHandlePtr(holder, result); |
| 103 |
return result; |
| 104 |
} |
| 105 |
else |
| 106 |
return {}; |
| 107 |
} |
| 108 |
|
| 109 |
UIHandle::Result NoteTrackButtonHandle::Click |
| 110 |
(const TrackPanelMouseEvent &, AudacityProject *) |
| 111 |
{ |
| 112 |
return RefreshCode::RefreshNone; |
| 113 |
} |
| 114 |
|
| 115 |
UIHandle::Result NoteTrackButtonHandle::Drag |
| 116 |
(const TrackPanelMouseEvent &, AudacityProject *) |
| 117 |
{ |
| 118 |
return RefreshCode::RefreshNone; |
| 119 |
} |
| 120 |
|
| 121 |
HitTestPreview NoteTrackButtonHandle::Preview |
| 122 |
(const TrackPanelMouseState &, AudacityProject *) |
| 123 |
{ |
| 124 |
// auto pTrack = pProject->GetTracks()->Lock(mpTrack); |
| 125 |
auto pTrack = mpTrack.lock(); |
| 126 |
if ( !pTrack ) |
| 127 |
return {}; |
| 128 |
// No special message or cursor |
| 129 |
return {}; |
| 130 |
} |
| 131 |
|
| 132 |
UIHandle::Result NoteTrackButtonHandle::Release |
| 133 |
(const TrackPanelMouseEvent &evt, AudacityProject *pProject, wxWindow *) |
| 134 |
{ |
| 135 |
using namespace RefreshCode; |
| 136 |
|
| 137 |
auto pTrack = TrackList::Get( *pProject ).Lock(mpTrack); |
| 138 |
if (!pTrack) |
| 139 |
return Cancelled; |
| 140 |
|
| 141 |
const wxMouseEvent &event = evt.event; |
| 142 |
if (LabelClick(*pTrack, mRect, event.m_x, event.m_y, |
| 143 |
event.Button(wxMOUSE_BTN_RIGHT))) { |
| 144 |
// No undo items needed?? |
| 145 |
ProjectHistory::Get( *pProject ).ModifyState(false); |
| 146 |
return RefreshAll; |
| 147 |
} |
| 148 |
return RefreshNone; |
| 149 |
} |
| 150 |
|
| 151 |
UIHandle::Result NoteTrackButtonHandle::Cancel(AudacityProject *) |
| 152 |
{ |
| 153 |
return RefreshCode::RefreshNone; |
| 154 |
} |
| 155 |
|
| 156 |
#endif |