| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/*!******************************************************************** |
| 3 |
|
| 4 |
Audacity: A Digital Audio Editor |
| 5 |
|
| 6 |
HighlitClipButtonHandle.cpp |
| 7 |
|
| 8 |
Matthieu Hodgkinson |
| 9 |
|
| 10 |
**********************************************************************/ |
| 11 |
#include "HighlitClipButtonHandle.h" |
| 12 |
#include "AllThemeResources.h" |
| 13 |
#include "ClipParameters.h" |
| 14 |
#include "HitTestResult.h" |
| 15 |
#include "LowlitClipButton.h" |
| 16 |
#include "ProjectHistory.h" |
| 17 |
#include "RefreshCode.h" |
| 18 |
#include "SelectionState.h" |
| 19 |
#include "Theme.h" |
| 20 |
#include "TrackArt.h" |
| 21 |
#include "TrackArtist.h" |
| 22 |
#include "TrackPanelDrawingContext.h" |
| 23 |
#include "TrackPanelMouseEvent.h" |
| 24 |
#include "ViewInfo.h" |
| 25 |
#include "WaveClip.h" |
| 26 |
#include "WaveTrack.h" |
| 27 |
#include <wx/dc.h> |
| 28 |
#include <wx/window.h> |
| 29 |
|
| 30 |
HighlitClipButtonHandle::HighlitClipButtonHandle( |
| 31 |
ClipButtonId id, std::shared_ptr<WaveTrack> track, |
| 32 |
std::shared_ptr<WaveTrack::Interval> clip) |
| 33 |
: mButtonId { id } |
| 34 |
, mTrack { std::move(track) } |
| 35 |
, mClip { std::move(clip) } |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
void HighlitClipButtonHandle::Draw( |
| 40 |
TrackPanelDrawingContext& context, const wxRect& affordanceRect, |
| 41 |
unsigned iPass) |
| 42 |
{ |
| 43 |
if (iPass != TrackArtist::PassSnapping) |
| 44 |
return; |
| 45 |
const auto artist = TrackArtist::Get(context); |
| 46 |
const auto& zoomInfo = *artist->pZoomInfo; |
| 47 |
const auto rect = LowlitClipButton::Detail::GetButtonInnerRectangle( |
| 48 |
mButtonId, { *mClip, zoomInfo, affordanceRect }); |
| 49 |
if (!rect) |
| 50 |
return; |
| 51 |
Highlight(*rect, context.dc); |
| 52 |
DoDraw(*rect, context.dc); |
| 53 |
} |
| 54 |
|
| 55 |
UIHandle::Result HighlitClipButtonHandle::Click( |
| 56 |
const TrackPanelMouseEvent& event, AudacityProject* pProject) |
| 57 |
{ |
| 58 |
mwCell = event.pCell; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
UIHandle::Result HighlitClipButtonHandle::Drag( |
| 63 |
const TrackPanelMouseEvent& event, AudacityProject* pProject) |
| 64 |
{ |
| 65 |
const auto cancelCode = RefreshCode::RefreshCell | RefreshCode::Cancelled; |
| 66 |
if (event.pCell != mwCell.lock()) |
| 67 |
return cancelCode; |
| 68 |
|
| 69 |
// It's the right cell; check that the mouse is still over the button. |
| 70 |
const auto buttonRect = LowlitClipButton::Detail::GetButtonRectangle( |
| 71 |
mButtonId, { *mClip, ViewInfo::Get(*pProject), event.rect }); |
| 72 |
if (!buttonRect.has_value()) |
| 73 |
return cancelCode; |
| 74 |
|
| 75 |
const auto mousePos = event.event.GetPosition(); |
| 76 |
if (!buttonRect->Contains(mousePos)) |
| 77 |
return cancelCode; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
UIHandle::Result HighlitClipButtonHandle::Release( |
| 83 |
const TrackPanelMouseEvent& event, AudacityProject* pProject, |
| 84 |
wxWindow* pParent) |
| 85 |
{ |
| 86 |
mChangeHighlight = RefreshCode::RefreshNone; |
| 87 |
// Select before calling `DoRelease`. |
| 88 |
const auto result = UpdateTrackSelection(event, pProject); |
| 89 |
return result | DoRelease(event, pProject, pParent); |
| 90 |
} |
| 91 |
|
| 92 |
UIHandle::Result HighlitClipButtonHandle::Cancel(AudacityProject* pProject) |
| 93 |
{ |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
UIHandle::Result HighlitClipButtonHandle::UpdateTrackSelection( |
| 98 |
const TrackPanelMouseEvent& event, AudacityProject* pProject) |
| 99 |
{ |
| 100 |
auto& trackList = TrackList::Get(*pProject); |
| 101 |
|
| 102 |
if (const auto track = trackList.Lock<Track>(mTrack)) |
| 103 |
{ |
| 104 |
auto& selectionState = SelectionState::Get(*pProject); |
| 105 |
selectionState.SelectNone(trackList); |
| 106 |
selectionState.SelectTrack(*track, true, true); |
| 107 |
|
| 108 |
auto& viewInfo = ViewInfo::Get(*pProject); |
| 109 |
viewInfo.selectedRegion.setTimes( |
| 110 |
mClip->GetPlayStartTime(), mClip->GetPlayEndTime()); |
| 111 |
|
| 112 |
ProjectHistory::Get(*pProject).ModifyState(false); |
| 113 |
|
| 114 |
return RefreshCode::RefreshAll | RefreshCode::Cancelled; |
| 115 |
} |
| 116 |
|
| 117 |
return RefreshCode::RefreshNone; |
| 118 |
} |
| 119 |
|
| 120 |
std::shared_ptr<const Track> HighlitClipButtonHandle::FindTrack() const |
| 121 |
{ |
| 122 |
return mTrack; |
| 123 |
} |
| 124 |
|
| 125 |
void HighlitClipButtonHandle::Enter(bool forward, AudacityProject* pProject) |
| 126 |
{ |
| 127 |
mChangeHighlight = RefreshCode::RefreshCell; |
| 128 |
} |
| 129 |
|
| 130 |
void HighlitClipButtonHandle::Highlight(const wxRect& rect, wxDC& dc) |
| 131 |
{ |
| 132 |
constexpr auto penWidth = 0; |
| 133 |
const auto col = theTheme.Colour(clrClipAffordanceButton); |
| 134 |
wxDCBrushChanger brush { dc, col }; |
| 135 |
wxDCPenChanger pen { dc, { wxPenInfo { col, penWidth } } }; |
| 136 |
constexpr auto radius = 3; |
| 137 |
dc.DrawRoundedRectangle(rect, radius); |
| 138 |
} |