Audacity 3.2.0
MouseWheelHandler.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5MouseWheelHandler.cpp
6
7Paul Licameli split from ProjectWindow.cpp
8
9**********************************************************************/
11#include "RefreshCode.h"
12#include "Track.h"
14#include "ViewInfo.h"
15#include "Viewport.h"
16#include "tracks/ui/Scrubbing.h"
17
18// Common mouse wheel handling in track panel cells, moved here to avoid
19// compilation dependencies on Track, TrackPanel, and Scrubbing at low levels
20// which made cycles
22// Need a bit of memory from one call to the next
23[mVertScrollRemainder = 0.0](
24 const TrackPanelMouseEvent &evt, AudacityProject *pProject )
25mutable -> unsigned {
26 using namespace RefreshCode;
27
28 if ( TrackList::Get( *pProject ).empty() )
29 // Scrolling and Zoom in and out commands are disabled when there are no tracks.
30 // This should be disabled too for consistency. Otherwise
31 // you do see changes in the time ruler.
32 return Cancelled;
33
34 unsigned result = RefreshAll;
35 const wxMouseEvent &event = evt.event;
36 auto &viewInfo = ViewInfo::Get( *pProject );
37 Scrubber &scrubber = Scrubber::Get( *pProject );
38 auto &viewport = Viewport::Get(*pProject);
39 const auto steps = evt.steps;
40
41 if (event.ShiftDown()
42 // Don't pan during smooth scrolling. That would conflict with keeping
43 // the play indicator centered.
44 && !scrubber.IsScrollScrubbing()
45 )
46 {
47 // MM: Scroll left/right when used with Shift key down
48 viewport.SetHorizontalThumb(
49 viewInfo.OffsetTimeByPixels(
50 viewInfo.PositionToTime(0), 50.0 * -steps));
51 }
52 else if (event.CmdDown())
53 {
54#if 0
55 // JKC: Alternative scroll wheel zooming code
56 // using AudacityProject zooming, which is smarter,
57 // it keeps selections on screen and centred if it can,
58 // also this ensures mousewheel and zoom buttons give same result.
59 double ZoomFactor = pow(2.0, steps);
61 if( steps > 0 )
62 // PRL: Track panel refresh may be needed if you reenable this
63 // code, but we don't want this file dependent on TrackPanel.cpp
64 p->ZoomAboutSelection( ZoomFactor );
65 else
66 p->ZoomAboutCenter( ZoomFactor );
67#endif
68 // MM: Zoom in/out when used with Control key down
69 // We're converting pixel positions to times,
70 // counting pixels from the left edge of the track.
71 int trackLeftEdge = viewInfo.GetLeftOffset();
72
73 // Time corresponding to mouse position
74 wxCoord xx;
75 double center_h;
76 double mouse_h = viewInfo.PositionToTime(event.m_x, trackLeftEdge);
77
78 // Scrubbing? Expand or contract about the center, ignoring mouse position
79 if (scrubber.IsScrollScrubbing())
80 center_h = viewInfo.hpos +
81 (viewInfo.GetScreenEndTime() - viewInfo.hpos) / 2.0;
82 // Zooming out? Focus on mouse.
83 else if( steps <= 0 )
84 center_h = mouse_h;
85 // No Selection? Focus on mouse.
86 else if((viewInfo.selectedRegion.t1() - viewInfo.selectedRegion.t0() ) < 0.00001 )
87 center_h = mouse_h;
88 // Before Selection? Focus on left
89 else if( mouse_h < viewInfo.selectedRegion.t0() )
90 center_h = viewInfo.selectedRegion.t0();
91 // After Selection? Focus on right
92 else if( mouse_h > viewInfo.selectedRegion.t1() )
93 center_h = viewInfo.selectedRegion.t1();
94 // Inside Selection? Focus on mouse
95 else
96 center_h = mouse_h;
97
98 xx = viewInfo.TimeToPosition(center_h, trackLeftEdge);
99
100 // Time corresponding to last (most far right) audio.
101 double audioEndTime = TrackList::Get(*pProject).GetEndTime();
102
103// Disabled this code to fix Bug 1923 (tricky to wheel-zoom right of waveform).
104#if 0
105 // When zooming in empty space, it's easy to 'lose' the waveform.
106 // This prevents it.
107 // IF zooming in
108 if (steps > 0)
109 {
110 // IF mouse is to right of audio
111 if (center_h > audioEndTime)
112 // Zooming brings far right of audio to mouse.
113 center_h = audioEndTime;
114 }
115#endif
116
117 wxCoord xTrackEnd = viewInfo.TimeToPosition( audioEndTime );
118 viewInfo.ZoomBy(pow(2.0, steps/4.0));
119
120 double new_center_h = viewInfo.PositionToTime(xx, trackLeftEdge);
121 viewInfo.hpos += (center_h - new_center_h);
122
123 // If wave has gone off screen, bring it back.
124 // This means that the end of the track stays where it was.
125 if( viewInfo.hpos > audioEndTime )
126 viewInfo.hpos += audioEndTime - viewInfo.PositionToTime( xTrackEnd );
127
128
129 result |= FixScrollbars;
130 }
131 else
132 {
133 if (scrubber.IsScrubbing()) {
134 scrubber.HandleScrollWheel(steps);
135 evt.event.Skip(false);
136 }
137 else
138 {
139 // MM: Scroll up/down when used without modifier keys
140 double lines = steps * 4 + mVertScrollRemainder;
141 mVertScrollRemainder = lines - floor(lines);
142 lines = floor(lines);
143 auto didSomething = viewport.ScrollUpDown((int)-lines);
144 if (!didSomething)
145 result |= Cancelled;
146 }
147 }
148
149 return result;
150} };
static CommonTrackPanelCell::MouseWheelHook::Scope scope
declares abstract base class Track, TrackList, and iterators over TrackList
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
typename GlobalVariable< MouseWheelHook, const std::function< unsigned(const TrackPanelMouseEvent &evt, AudacityProject *pProject) >, nullptr, Options... >::Scope Scope
void HandleScrollWheel(int steps)
Definition: Scrubbing.cpp:832
bool IsScrollScrubbing() const
Definition: Scrubbing.h:93
bool IsScrubbing() const
Definition: Scrubbing.cpp:768
static Scrubber & Get(AudacityProject &project)
Definition: Scrubbing.cpp:186
bool empty() const
Definition: Track.cpp:758
double GetEndTime() const
Return the greatest end time of the tracks, or 0 when no tracks.
Definition: Track.cpp:784
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
static Viewport & Get(AudacityProject &project)
Definition: Viewport.cpp:33
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
const AudacityProject & GetProject(const Track &track)
Definition: TrackArt.cpp:479