Audacity 3.2.0
TrackSelectHandle.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5TrackSelectHandle.cpp
6
7Paul Licameli split from TrackPanel.cpp
8
9**********************************************************************/
10
11
12#include "TrackSelectHandle.h"
13
14#include "ChannelView.h"
15#include "../../HitTestResult.h"
16#include "Project.h"
17#include "ProjectAudioIO.h"
18#include "ProjectHistory.h"
19#include "../../RefreshCode.h"
20#include "../../SelectUtilities.h"
21#include "../../TrackPanelMouseEvent.h"
22#include "Track.h"
23
24#include <wx/cursor.h>
25#include <wx/translation.h>
26
27#include "../../../images/Cursors.h"
28
29#if defined(__WXMAC__)
30/* i18n-hint: Command names a modifier key on Macintosh keyboards */
31#define CTRL_CLICK XO("Command+Click")
32#else
33/* i18n-hint: Ctrl names a modifier key on Windows or Linux keyboards */
34#define CTRL_CLICK XO("Ctrl+Click")
35#endif
36
37namespace {
38 TranslatableString Message(unsigned trackCount) {
39 if (trackCount > 1)
40 return XO(
41// i18n-hint: %s is replaced by (translation of) 'Ctrl+Click' on windows, 'Command+Click' on Mac
42"%s to select or deselect track. Drag up or down to change track order.")
43 .Format( CTRL_CLICK );
44 else
45 // i18n-hint: %s is replaced by (translation of) 'Ctrl+Click' on windows, 'Command+Click' on Mac
46 return XO("%s to select or deselect track.")
47 .Format( CTRL_CLICK );
48 }
49}
50
51TrackSelectHandle::TrackSelectHandle( const std::shared_ptr<Track> &pTrack )
52 : mpTrack( pTrack )
53{}
54
56(std::weak_ptr<TrackSelectHandle> &holder,
57 const std::shared_ptr<Track> &pTrack)
58{
59 auto result = std::make_shared<TrackSelectHandle>(pTrack);
60 result = AssignUIHandlePtr(holder, result);
61 return result;
62}
63
65{
66}
67
68std::shared_ptr<const Channel> TrackSelectHandle::FindChannel() const
69{
70 return std::dynamic_pointer_cast<const Channel>(mpTrack);
71}
72
74(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
75{
76 // If unsafe to drag, still, it does harmlessly change the selected track
77 // set on button down.
78
79 using namespace RefreshCode;
80 Result result = RefreshNone;
81
82 const wxMouseEvent &event = evt.event;
83
84 // AS: If not a click, ignore the mouse event.
85 if (!event.ButtonDown() && !event.ButtonDClick())
86 return Cancelled;
87 if (!event.Button(wxMOUSE_BTN_LEFT))
88 return Cancelled;
89
90 const auto pTrack = mpTrack;
91 if (!pTrack)
92 return Cancelled;
93 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
94
95 // DM: If they weren't clicking on a particular part of a track label,
96 // deselect other tracks and select this one.
97
98 // JH: also, capture the current track for rearranging, so the user
99 // can drag the track up or down to swap it with others
100 if (unsafe)
101 result |= Cancelled;
102 else {
103 mRearrangeCount = 0;
104 CalculateRearrangingThresholds(event, pProject);
105 }
106
108 *pTrack, event.ShiftDown(), event.ControlDown(), !unsafe);
109
110 mClicked = true;
111 return result;
112}
113
115(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
116{
117 using namespace RefreshCode;
118 Result result = RefreshNone;
119
120 const wxMouseEvent &event = evt.event;
121
122 auto &tracks = TrackList::Get( *pProject );
123
124 // probably harmless during play? However, we do disallow the click, so check this too.
125 bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
126 if (unsafe)
127 return result;
128
129 if (event.m_y < mMoveUpThreshold || event.m_y < 0) {
130 tracks.MoveUp(mpTrack.get());
132 }
133 else if ( event.m_y > mMoveDownThreshold
134 || event.m_y > evt.whole.GetHeight() ) {
135 tracks.MoveDown(mpTrack.get());
137 }
138 else
139 return result;
140
141 // JH: if we moved up or down, recalculate the thresholds and make sure the
142 // track is fully on-screen.
143 CalculateRearrangingThresholds(event, pProject);
144
145 result |= EnsureVisible | RefreshAll;
146 return result;
147}
148
151{
152 static auto disabledCursor =
153 ::MakeCursor(wxCURSOR_NO_ENTRY, DisabledCursorXpm, 16, 16);
154 //static wxCursor rearrangeCursor{ wxCURSOR_HAND };
155 static auto rearrangingCursor =
156 ::MakeCursor(wxCURSOR_HAND, RearrangingCursorXpm, 16, 16);
157 static wxCursor arrowCursor{ wxCURSOR_ARROW };
158
159 //static auto hoverCursor =
160 // ::MakeCursor(wxCURSOR_HAND, RearrangeCursorXpm, 16, 16);
161 //static auto clickedCursor =
162 // ::MakeCursor(wxCURSOR_HAND, RearrangingCursorXpm, 16, 16);
163
164 const auto trackCount = TrackList::Get( *project ).Any().size();
165 auto message = Message(trackCount);
166 if (mClicked) {
167 const bool unsafe =
169 const bool canMove = TrackList::Get( *project ).Any().size() > 1;
170 return {
171 message,
172 (unsafe
173 ? &*disabledCursor
174 : canMove
175 ? &*rearrangingCursor
176 : &arrowCursor)
177 // , message // Stop showing the tooltip after the click
178 };
179 }
180 else {
181 // Only mouse-over
182 // Don't test safety, because the click to change selection is allowed
183 return {
184 message,
185 &arrowCursor,
186 message
187 };
188 }
189}
190
192(const TrackPanelMouseEvent &, AudacityProject *project, wxWindow *)
193{
194 // If we're releasing, surely we are dragging a track?
195 wxASSERT( mpTrack );
196 if (mRearrangeCount != 0) {
197 ProjectHistory::Get( *project ).PushState(
198 /* i18n-hint: will substitute name of track for %s */
199 ( mRearrangeCount < 0 ? XO("Moved '%s' up") : XO("Moved '%s' down") )
200 .Format( mpTrack->GetName() ),
201 XO("Move Track"));
202 }
203 // Bug 1677
204 // Holding on to the reference to the track was causing it to be released far later
205 // than necessary, on shutdown, and so causing a crash as a dialog about cleaning
206 // out files could not show at that time.
207 mpTrack.reset();
208 // No need to redraw, that was done when drag moved the track
210}
211
213{
214 ProjectHistory::Get( *pProject ).RollbackState();
215 // Bug 1677
216 mpTrack.reset();
218}
219
223 const wxMouseEvent & event, AudacityProject *project)
224{
225 // JH: this will probably need to be tweaked a bit, I'm just
226 // not sure what formula will have the best feel for the
227 // user.
228
229 auto &tracks = TrackList::Get( *project );
230
231 if (tracks.CanMoveUp(mpTrack.get()))
233 event.m_y -
235 * -- tracks.Find(mpTrack.get()));
236 else
237 mMoveUpThreshold = INT_MIN;
238
239 if (tracks.CanMoveDown(mpTrack.get()))
241 event.m_y +
243 * ++ tracks.Find(mpTrack.get()));
244 else
245 mMoveDownThreshold = INT_MAX;
246}
std::shared_ptr< UIHandle > UIHandlePtr
Definition: CellularPanel.h:28
XO("Cut/Copy/Paste")
const auto tracks
const auto project
declares abstract base class Track, TrackList, and iterators over TrackList
std::unique_ptr< wxCursor > MakeCursor(int WXUNUSED(CursorId), const char *const pXpm[36], int HotX, int HotY)
Definition: TrackPanel.cpp:188
#define CTRL_CLICK
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:159
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
static int GetChannelGroupHeight(const Track *pTrack)
Definition: ChannelView.cpp:32
Abstract base class used in importing a file.
bool IsAudioActive() const
static ProjectAudioIO & Get(AudacityProject &project)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
static ProjectHistory & Get(AudacityProject &project)
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1079
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:347
static UIHandlePtr HitAnywhere(std::weak_ptr< TrackSelectHandle > &holder, const std::shared_ptr< Track > &pTrack)
std::shared_ptr< Track > mpTrack
Result Release(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent) override
std::shared_ptr< const Channel > FindChannel() const override
void CalculateRearrangingThresholds(const wxMouseEvent &event, AudacityProject *project)
Result Drag(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
HitTestPreview Preview(const TrackPanelMouseState &state, AudacityProject *pProject) override
Result Click(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
Result Cancel(AudacityProject *) override
TrackSelectHandle(const TrackSelectHandle &)=delete
Holds a msgid for the translation catalog; may also bind format arguments.
unsigned Result
Definition: UIHandle.h:39
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
void DoListSelection(AudacityProject &project, Track &t, bool shift, bool ctrl, bool modifyState)
TranslatableString Message(unsigned trackCount)