| 1 |
/*!******************************************************************** |
| 2 |
* |
| 3 |
Audacity: A Digital Audio Editor |
| 4 |
|
| 5 |
WaveTrackAffordanceControls.cpp |
| 6 |
|
| 7 |
Vitaly Sverchinsky |
| 8 |
|
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
#include "WaveTrackAffordanceControls.h" |
| 12 |
|
| 13 |
#include <wx/dc.h> |
| 14 |
#include <wx/frame.h> |
| 15 |
|
| 16 |
#include "AllThemeResources.h" |
| 17 |
#include "CommandContext.h" |
| 18 |
#include "CommandFlag.h" |
| 19 |
#include "CommandFunctors.h" |
| 20 |
#include "MenuRegistry.h" |
| 21 |
#include "PendingTracks.h" |
| 22 |
#include "../../../../TrackPanelMouseEvent.h" |
| 23 |
#include "../../../../TrackArt.h" |
| 24 |
#include "../../../../TrackArtist.h" |
| 25 |
#include "../../../../TrackPanelDrawingContext.h" |
| 26 |
#include "ViewInfo.h" |
| 27 |
#include "WaveTrack.h" |
| 28 |
#include "WaveClip.h" |
| 29 |
#include "TimeStretching.h" |
| 30 |
#include "UndoManager.h" |
| 31 |
#include "ShuttleGui.h" |
| 32 |
#include "../../../../ProjectWindows.h" |
| 33 |
#include "../../../../commands/AudacityCommand.h" |
| 34 |
|
| 35 |
#include "../../../ui/SelectHandle.h" |
| 36 |
#include "../../../ui/TextEditHelper.h" |
| 37 |
#include "ClipParameters.h" |
| 38 |
#include "WaveChannelView.h" |
| 39 |
#include "WaveTrackAffordanceHandle.h" |
| 40 |
|
| 41 |
#include "ProjectHistory.h" |
| 42 |
#include "../../../../ProjectSettings.h" |
| 43 |
#include "SelectionState.h" |
| 44 |
#include "../../../../RefreshCode.h" |
| 45 |
#include "Theme.h" |
| 46 |
#include "../../../../../images/Cursors.h" |
| 47 |
#include "../../../../HitTestResult.h" |
| 48 |
#include "../../../../TrackPanel.h" |
| 49 |
#include "TrackFocus.h" |
| 50 |
|
| 51 |
#include "ClipOverflowButtonHandle.h" |
| 52 |
#include "ClipPitchAndSpeedButtonHandle.h" |
| 53 |
#include "LowlitClipButton.h" |
| 54 |
#include "PitchAndSpeedDialog.h" |
| 55 |
#include "WaveClipAdjustBorderHandle.h" |
| 56 |
#include "WaveClipUIUtilities.h" |
| 57 |
|
| 58 |
#include "BasicUI.h" |
| 59 |
#include "UserException.h" |
| 60 |
|
| 61 |
|
| 62 |
class SetWaveClipNameCommand : public AudacityCommand |
| 63 |
{ |
| 64 |
public: |
| 65 |
static const ComponentInterfaceSymbol Symbol; |
| 66 |
|
| 67 |
ComponentInterfaceSymbol GetSymbol() const override |
| 68 |
{ |
| 69 |
return Symbol; |
| 70 |
} |
| 71 |
void PopulateOrExchange(ShuttleGui& S) override |
| 72 |
{ |
| 73 |
S.AddSpace(0, 5); |
| 74 |
|
| 75 |
S.StartMultiColumn(2, wxALIGN_CENTER); |
| 76 |
{ |
| 77 |
S.TieTextBox(XXO("Name:"), mName, 60); |
| 78 |
} |
| 79 |
S.EndMultiColumn(); |
| 80 |
} |
| 81 |
public: |
| 82 |
wxString mName; |
| 83 |
}; |
| 84 |
|
| 85 |
const ComponentInterfaceSymbol SetWaveClipNameCommand::Symbol |
| 86 |
{ XO("Set Wave Clip Name") }; |
| 87 |
|
| 88 |
//Handle which is used to send mouse events to TextEditHelper |
| 89 |
class WaveClipTitleEditHandle final : public UIHandle |
| 90 |
{ |
| 91 |
std::shared_ptr<TextEditHelper> mHelper; |
| 92 |
std::shared_ptr<WaveTrack> mpTrack; |
| 93 |
public: |
| 94 |
|
| 95 |
WaveClipTitleEditHandle(const std::shared_ptr<TextEditHelper>& helper, |
| 96 |
const std::shared_ptr<WaveTrack> pTrack) |
| 97 |
: mHelper(helper) |
| 98 |
, mpTrack{ move(pTrack) } |
| 99 |
{ } |
| 100 |
|
| 101 |
~WaveClipTitleEditHandle() |
| 102 |
{ |
| 103 |
} |
| 104 |
|
| 105 |
std::shared_ptr<const Track> FindTrack() const override |
| 106 |
{ |
| 107 |
return mpTrack; |
| 108 |
} |
| 109 |
|
| 110 |
Result Click(const TrackPanelMouseEvent& event, AudacityProject* project) override |
| 111 |
{ |
| 112 |
if (mHelper->OnClick(event.event, project)) |
| 113 |
return RefreshCode::RefreshCell; |
| 114 |
return RefreshCode::RefreshNone; |
| 115 |
} |
| 116 |
|
| 117 |
Result Drag(const TrackPanelMouseEvent& event, AudacityProject* project) override |
| 118 |
{ |
| 119 |
if (mHelper->OnDrag(event.event, project)) |
| 120 |
return RefreshCode::RefreshCell; |
| 121 |
return RefreshCode::RefreshNone; |
| 122 |
} |
| 123 |
|
| 124 |
HitTestPreview Preview(const TrackPanelMouseState& state, AudacityProject* pProject) override |
| 125 |
{ |
| 126 |
static auto ibeamCursor = |
| 127 |
::MakeCursor(wxCURSOR_IBEAM, IBeamCursorXpm, 17, 16); |
| 128 |
return { |
| 129 |
XO("Click and drag to select text"), |
| 130 |
ibeamCursor.get() |
| 131 |
}; |
| 132 |
} |
| 133 |
|
| 134 |
Result Release(const TrackPanelMouseEvent& event, AudacityProject* project, wxWindow*) override |
| 135 |
{ |
| 136 |
if (mHelper->OnRelease(event.event, project)) |
| 137 |
return RefreshCode::RefreshCell; |
| 138 |
return RefreshCode::RefreshNone; |
| 139 |
} |
| 140 |
|
| 141 |
Result Cancel(AudacityProject* project) override |
| 142 |
{ |
| 143 |
if (mHelper) |
| 144 |
{ |
| 145 |
mHelper->Cancel(project); |
| 146 |
mHelper.reset(); |
| 147 |
} |
| 148 |
return RefreshCode::RefreshAll; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
WaveTrackAffordanceControls::WaveTrackAffordanceControls(const std::shared_ptr<Track>& pTrack) |
| 153 |
: CommonTrackCell{ pTrack } |
| 154 |
, mClipNameFont{ wxFontInfo{} } |
| 155 |
{ |
| 156 |
if (auto trackList = pTrack->GetOwner()) { |
| 157 |
if (auto pProject = trackList->GetOwner()) { |
| 158 |
mTrackListEventSubscription = PendingTracks(*pProject).Subscribe( |
| 159 |
*this, &WaveTrackAffordanceControls::OnTrackListEvent); |
| 160 |
if(auto project = trackList->GetOwner()) |
| 161 |
{ |
| 162 |
auto& viewInfo = ViewInfo::Get(*project); |
| 163 |
mSelectionChangeSubscription = |
| 164 |
viewInfo.selectedRegion.Subscribe( |
| 165 |
*this, |
| 166 |
&WaveTrackAffordanceControls::OnSelectionChange); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
std::vector<UIHandlePtr> WaveTrackAffordanceControls::HitTest(const TrackPanelMouseState& state, const AudacityProject* pProject) |
| 173 |
{ |
| 174 |
std::vector<UIHandlePtr> results; |
| 175 |
|
| 176 |
const auto px = state.state.m_x; |
| 177 |
const auto py = state.state.m_y; |
| 178 |
const wxPoint mousePoint { px, py }; |
| 179 |
|
| 180 |
const auto rect = state.rect; |
| 181 |
|
| 182 |
auto track = std::static_pointer_cast<WaveTrack>(FindTrack()); |
| 183 |
if (!track) |
| 184 |
return {}; |
| 185 |
|
| 186 |
{ |
| 187 |
auto handle = WaveClipAdjustBorderHandle::HitAnywhere( |
| 188 |
mClipBorderAdjustHandle, |
| 189 |
track, |
| 190 |
pProject, |
| 191 |
state); |
| 192 |
|
| 193 |
if (handle) |
| 194 |
results.push_back(handle); |
| 195 |
} |
| 196 |
|
| 197 |
if (mTextEditHelper && mTextEditHelper->GetBBox().Contains(px, py)) |
| 198 |
{ |
| 199 |
results.push_back( |
| 200 |
AssignUIHandlePtr( |
| 201 |
mTitleEditHandle, |
| 202 |
std::make_shared<WaveClipTitleEditHandle>( |
| 203 |
mTextEditHelper, track) |
| 204 |
) |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
auto &waveTrack = static_cast<WaveTrack&>( |
| 209 |
PendingTracks::Get(*pProject).SubstitutePendingChangedTrack(*track)); |
| 210 |
auto& zoomInfo = ViewInfo::Get(*pProject); |
| 211 |
const auto &intervals = waveTrack.Intervals(); |
| 212 |
for(auto it = intervals.begin(); it != intervals.end(); ++it) |
| 213 |
{ |
| 214 |
if (it == mEditedInterval) |
| 215 |
continue; |
| 216 |
|
| 217 |
const auto clip = (*it); |
| 218 |
if (LowlitClipButton::HitTest<ClipButtonId::Overflow>( |
| 219 |
{ *clip, zoomInfo, rect }, mousePoint)) |
| 220 |
{ |
| 221 |
results.push_back(AssignUIHandlePtr( |
| 222 |
mOverflowButtonHandle, std::make_shared<ClipOverflowButtonHandle>( |
| 223 |
track, *it, weak_from_this()))); |
| 224 |
mFocusInterval = it; |
| 225 |
break; |
| 226 |
} |
| 227 |
else if (LowlitClipButton::HitTest<ClipButtonId::Pitch>( |
| 228 |
{ *clip, zoomInfo, rect }, mousePoint)) |
| 229 |
{ |
| 230 |
results.push_back(AssignUIHandlePtr( |
| 231 |
mPitchButtonHandle, |
| 232 |
std::make_shared<ClipPitchAndSpeedButtonHandle>( |
| 233 |
ClipPitchAndSpeedButtonHandle::Type::Pitch, track, *it))); |
| 234 |
mFocusInterval = it; |
| 235 |
break; |
| 236 |
} |
| 237 |
else if (LowlitClipButton::HitTest<ClipButtonId::Speed>( |
| 238 |
{ *clip, zoomInfo, rect }, mousePoint)) |
| 239 |
{ |
| 240 |
results.push_back(AssignUIHandlePtr( |
| 241 |
mSpeedButtonHandle, |
| 242 |
std::make_shared<ClipPitchAndSpeedButtonHandle>( |
| 243 |
ClipPitchAndSpeedButtonHandle::Type::Speed, track, *it))); |
| 244 |
mFocusInterval = it; |
| 245 |
break; |
| 246 |
} |
| 247 |
else if (WaveChannelView::HitTest( |
| 248 |
*clip, zoomInfo, state.rect, mousePoint)) |
| 249 |
{ |
| 250 |
results.push_back(AssignUIHandlePtr( |
| 251 |
mAffordanceHandle, |
| 252 |
std::make_shared<WaveTrackAffordanceHandle>(track, clip))); |
| 253 |
mFocusInterval = it; |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
const auto& settings = ProjectSettings::Get(*pProject); |
| 259 |
const auto currentTool = settings.GetTool(); |
| 260 |
if (currentTool == ToolCodes::multiTool || currentTool == ToolCodes::selectTool) |
| 261 |
{ |
| 262 |
results.push_back( |
| 263 |
SelectHandle::HitTest( |
| 264 |
mSelectHandle, state, pProject, |
| 265 |
WaveChannelView::GetFirst(*track).shared_from_this() |
| 266 |
) |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
return results; |
| 271 |
} |
| 272 |
|
| 273 |
void WaveTrackAffordanceControls::Draw(TrackPanelDrawingContext& context, const wxRect& rect, unsigned iPass) |
| 274 |
{ |
| 275 |
if (iPass == TrackArtist::PassBackground) { |
| 276 |
const auto track = FindTrack().get(); |
| 277 |
if (!track) |
| 278 |
return; |
| 279 |
const auto artist = TrackArtist::Get(context); |
| 280 |
const auto &pendingTracks = *artist->pPendingTracks; |
| 281 |
|
| 282 |
// Color the background of the affordance rectangle (only one per track) |
| 283 |
// as for the topmost channel |
| 284 |
TrackArt::DrawBackgroundWithSelection(context, |
| 285 |
rect, **track->Channels().begin(), |
| 286 |
artist->clipAffordanceBackgroundSelBrush, |
| 287 |
artist->clipAffordanceBackgroundBrush, |
| 288 |
true, true); |
| 289 |
|
| 290 |
mVisibleIntervals.clear(); |
| 291 |
|
| 292 |
auto &waveTrack = static_cast<WaveTrack&>( |
| 293 |
pendingTracks.SubstitutePendingChangedTrack(*track)); |
| 294 |
const auto& zoomInfo = *artist->pZoomInfo; |
| 295 |
{ |
| 296 |
wxDCClipper dcClipper(context.dc, rect); |
| 297 |
|
| 298 |
context.dc.SetTextBackground(wxTransparentColor); |
| 299 |
context.dc.SetTextForeground(theTheme.Colour(clrClipNameText)); |
| 300 |
context.dc.SetFont(mClipNameFont); |
| 301 |
|
| 302 |
auto px = context.lastState.m_x; |
| 303 |
auto py = context.lastState.m_y; |
| 304 |
|
| 305 |
const auto overflowHandle = mOverflowButtonHandle.lock(); |
| 306 |
const auto &intervals = waveTrack.Intervals(); |
| 307 |
for(auto it = intervals.begin(); it != intervals.end(); ++it) |
| 308 |
{ |
| 309 |
auto interval = *it; |
| 310 |
const auto& clip = *interval; |
| 311 |
const auto clipRect = ClipParameters::GetClipRect( |
| 312 |
clip, zoomInfo, rect); |
| 313 |
|
| 314 |
if(!WaveChannelView::ClipDetailsVisible(clip, zoomInfo, rect)) |
| 315 |
{ |
| 316 |
TrackArt::DrawClipFolded(context.dc, clipRect); |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
const auto selected = GetSelectedInterval() == it; |
| 321 |
const auto highlightAffordance = |
| 322 |
!overflowHandle && (selected || clipRect.Contains(px, py)); |
| 323 |
auto affordanceRect = TrackArt::DrawClipAffordance( |
| 324 |
context.dc, clipRect, highlightAffordance, selected); |
| 325 |
|
| 326 |
if ( |
| 327 |
const auto overflowButtonRect = |
| 328 |
LowlitClipButton::DrawOnClip<ClipButtonId::Overflow>( |
| 329 |
{ clip, zoomInfo, rect }, context.dc)) |
| 330 |
affordanceRect.width -= overflowButtonRect->width; |
| 331 |
if ( |
| 332 |
const auto speedButtonRect = |
| 333 |
LowlitClipButton::DrawOnClip<ClipButtonId::Speed>( |
| 334 |
{ clip, zoomInfo, rect }, context.dc)) |
| 335 |
affordanceRect.width -= speedButtonRect->width; |
| 336 |
if ( |
| 337 |
const auto pitchButtonRect = |
| 338 |
LowlitClipButton::DrawOnClip<ClipButtonId::Pitch>( |
| 339 |
{ clip, zoomInfo, rect }, context.dc)) |
| 340 |
affordanceRect.width -= pitchButtonRect->width; |
| 341 |
|
| 342 |
if (mTextEditHelper && mEditedInterval == it) |
| 343 |
{ |
| 344 |
if (!mTextEditHelper->Draw(context.dc, affordanceRect)) |
| 345 |
{ |
| 346 |
mTextEditHelper->Cancel(nullptr); |
| 347 |
TrackArt::DrawClipTitle( |
| 348 |
context.dc, affordanceRect, interval->GetName()); |
| 349 |
} |
| 350 |
} |
| 351 |
else if (TrackArt::DrawClipTitle( |
| 352 |
context.dc, affordanceRect, interval->GetName())) |
| 353 |
mVisibleIntervals.push_back(it); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
bool WaveTrackAffordanceControls::IsIntervalVisible(const IntervalIterator& it) const noexcept |
| 360 |
{ |
| 361 |
return std::find(mVisibleIntervals.begin(), |
| 362 |
mVisibleIntervals.end(), |
| 363 |
it) != mVisibleIntervals.end(); |
| 364 |
} |
| 365 |
|
| 366 |
bool WaveTrackAffordanceControls::StartEditClipName(AudacityProject& project, IntervalIterator it) |
| 367 |
{ |
| 368 |
bool useDialog{ false }; |
| 369 |
gPrefs->Read(wxT("/GUI/DialogForNameNewLabel"), &useDialog, false); |
| 370 |
|
| 371 |
auto interval = *it; |
| 372 |
if(!interval) |
| 373 |
return false; |
| 374 |
|
| 375 |
if (useDialog) |
| 376 |
{ |
| 377 |
SetWaveClipNameCommand Command; |
| 378 |
auto oldName = interval->GetName(); |
| 379 |
Command.mName = oldName; |
| 380 |
auto result = Command.PromptUser(project); |
| 381 |
if (result && Command.mName != oldName) |
| 382 |
{ |
| 383 |
interval->SetName(Command.mName); |
| 384 |
ProjectHistory::Get(project).PushState(XO("Modified Clip Name"), |
| 385 |
XO("Clip Name Edit")); |
| 386 |
} |
| 387 |
} |
| 388 |
else if(it != mEditedInterval) |
| 389 |
{ |
| 390 |
if(!IsIntervalVisible(it)) |
| 391 |
return false; |
| 392 |
|
| 393 |
if (mTextEditHelper) |
| 394 |
mTextEditHelper->Finish(&project); |
| 395 |
|
| 396 |
mEditedInterval = it; |
| 397 |
mTextEditHelper = MakeTextEditHelper(interval->GetName()); |
| 398 |
} |
| 399 |
|
| 400 |
return true; |
| 401 |
} |
| 402 |
|
| 403 |
auto WaveTrackAffordanceControls::GetSelectedInterval() const -> IntervalIterator |
| 404 |
{ |
| 405 |
if (auto handle = mAffordanceHandle.lock()) |
| 406 |
{ |
| 407 |
return handle->Clicked() ? mFocusInterval : IntervalIterator{}; |
| 408 |
} |
| 409 |
return {}; |
| 410 |
} |
| 411 |
|
| 412 |
namespace { |
| 413 |
|
| 414 |
auto FindAffordance(WaveTrack &track) |
| 415 |
{ |
| 416 |
auto &view = WaveChannelView::GetFirst(track); |
| 417 |
auto pAffordance = view.GetAffordanceControls(); |
| 418 |
return std::dynamic_pointer_cast<WaveTrackAffordanceControls>( |
| 419 |
pAffordance ); |
| 420 |
} |
| 421 |
|
| 422 |
std::pair<std::shared_ptr<WaveTrack>, ChannelGroup::IntervalIterator<WaveTrack::Interval>> |
| 423 |
SelectedIntervalOfFocusedTrack(AudacityProject &project, bool wholeInterval = true) |
| 424 |
{ |
| 425 |
// Note that TrackFocus may change its state as a side effect, defining |
| 426 |
// a track focus if there was none |
| 427 |
auto track = TrackFocus::Get(project).Get(); |
| 428 |
if (!track) |
| 429 |
return {}; |
| 430 |
if ( |
| 431 |
auto pWaveTrack = |
| 432 |
std::dynamic_pointer_cast<WaveTrack>(track->shared_from_this())) |
| 433 |
{ |
| 434 |
if (FindAffordance(*pWaveTrack)) { |
| 435 |
auto &viewInfo = ViewInfo::Get(project); |
| 436 |
const auto &intervals = pWaveTrack->Intervals(); |
| 437 |
|
| 438 |
auto it = std::find_if( |
| 439 |
intervals.begin(), intervals.end(), [&](const auto& interval) { |
| 440 |
if (wholeInterval) |
| 441 |
return interval->Start() == viewInfo.selectedRegion.t0() && |
| 442 |
interval->End() == viewInfo.selectedRegion.t1(); |
| 443 |
else |
| 444 |
return interval->Start() <= viewInfo.selectedRegion.t0() && |
| 445 |
interval->End() > viewInfo.selectedRegion.t0(); |
| 446 |
}); |
| 447 |
|
| 448 |
if(it != intervals.end()) |
| 449 |
return { pWaveTrack, it }; |
| 450 |
} |
| 451 |
} |
| 452 |
return {}; |
| 453 |
} |
| 454 |
|
| 455 |
// condition for enabling the command |
| 456 |
const ReservedCommandFlag &SomeClipIsSelectedFlag() |
| 457 |
{ |
| 458 |
static ReservedCommandFlag flag{ |
| 459 |
[](const AudacityProject &project){ |
| 460 |
// const_cast isn't pretty but not harmful in this case |
| 461 |
return SelectedIntervalOfFocusedTrack(const_cast<AudacityProject&>(project)).first != nullptr; |
| 462 |
} |
| 463 |
}; |
| 464 |
return flag; |
| 465 |
} |
| 466 |
|
| 467 |
const ReservedCommandFlag &StretchedClipIsSelectedFlag() |
| 468 |
{ |
| 469 |
static ReservedCommandFlag flag{ |
| 470 |
[](const AudacityProject &project){ |
| 471 |
// const_cast isn't pretty but not harmful in this case |
| 472 |
auto result = SelectedIntervalOfFocusedTrack( |
| 473 |
const_cast<AudacityProject&>(project)); |
| 474 |
|
| 475 |
auto interval = *result.second; |
| 476 |
return interval != nullptr && interval->HasPitchOrSpeed(); |
| 477 |
} |
| 478 |
}; |
| 479 |
return flag; |
| 480 |
} |
| 481 |
|
| 482 |
} |
| 483 |
|
| 484 |
unsigned WaveTrackAffordanceControls::CaptureKey(wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent, AudacityProject* project) |
| 485 |
{ |
| 486 |
if (!mTextEditHelper |
| 487 |
|| !mTextEditHelper->CaptureKey(event.GetKeyCode(), event.GetModifiers())) |
| 488 |
// Handle the event if it can be processed by the text editor (if any) |
| 489 |
event.Skip(); |
| 490 |
return RefreshCode::RefreshNone; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
unsigned WaveTrackAffordanceControls::KeyDown(wxKeyEvent& event, ViewInfo& viewInfo, wxWindow*, AudacityProject* project) |
| 495 |
{ |
| 496 |
auto keyCode = event.GetKeyCode(); |
| 497 |
|
| 498 |
if (mTextEditHelper) |
| 499 |
{ |
| 500 |
if (!mTextEditHelper->OnKeyDown(keyCode, event.GetModifiers(), project) |
| 501 |
&& !TextEditHelper::IsGoodEditKeyCode(keyCode)) |
| 502 |
event.Skip(); |
| 503 |
|
| 504 |
return RefreshCode::RefreshCell; |
| 505 |
} |
| 506 |
return RefreshCode::RefreshNone; |
| 507 |
} |
| 508 |
|
| 509 |
unsigned WaveTrackAffordanceControls::Char(wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent, AudacityProject* project) |
| 510 |
{ |
| 511 |
if (mTextEditHelper && mTextEditHelper->OnChar(event.GetUnicodeKey(), project)) |
| 512 |
return RefreshCode::RefreshCell; |
| 513 |
return RefreshCode::RefreshNone; |
| 514 |
} |
| 515 |
|
| 516 |
unsigned WaveTrackAffordanceControls::LoseFocus(AudacityProject *) |
| 517 |
{ |
| 518 |
return ExitTextEditing(); |
| 519 |
} |
| 520 |
|
| 521 |
void WaveTrackAffordanceControls::OnTextEditFinished(AudacityProject* project, const wxString& text) |
| 522 |
{ |
| 523 |
if (auto interval = *mEditedInterval) |
| 524 |
{ |
| 525 |
if (text != interval->GetName()) { |
| 526 |
interval->SetName(text); |
| 527 |
|
| 528 |
ProjectHistory::Get(*project).PushState(XO("Modified Clip Name"), |
| 529 |
XO("Clip Name Edit")); |
| 530 |
} |
| 531 |
} |
| 532 |
ResetClipNameEdit(); |
| 533 |
} |
| 534 |
|
| 535 |
void WaveTrackAffordanceControls::OnTextEditCancelled(AudacityProject* project) |
| 536 |
{ |
| 537 |
ResetClipNameEdit(); |
| 538 |
} |
| 539 |
|
| 540 |
void WaveTrackAffordanceControls::OnTextModified(AudacityProject* project, const wxString& text) |
| 541 |
{ |
| 542 |
//Nothing to do |
| 543 |
} |
| 544 |
|
| 545 |
void WaveTrackAffordanceControls::OnTextContextMenu(AudacityProject* project, const wxPoint& position) |
| 546 |
{ |
| 547 |
} |
| 548 |
|
| 549 |
void WaveTrackAffordanceControls::ResetClipNameEdit() |
| 550 |
{ |
| 551 |
mTextEditHelper.reset(); |
| 552 |
mEditedInterval = {}; |
| 553 |
} |
| 554 |
|
| 555 |
void WaveTrackAffordanceControls::OnTrackListEvent(const TrackListEvent& evt) |
| 556 |
{ |
| 557 |
if (evt.mType == TrackListEvent::SELECTION_CHANGE) |
| 558 |
ExitTextEditing(); |
| 559 |
} |
| 560 |
|
| 561 |
void WaveTrackAffordanceControls::OnSelectionChange(NotifyingSelectedRegionMessage) |
| 562 |
{ |
| 563 |
ExitTextEditing(); |
| 564 |
} |
| 565 |
|
| 566 |
unsigned WaveTrackAffordanceControls::ExitTextEditing() |
| 567 |
{ |
| 568 |
using namespace RefreshCode; |
| 569 |
if (mTextEditHelper) |
| 570 |
{ |
| 571 |
if (auto trackList = FindTrack()->GetOwner()) |
| 572 |
{ |
| 573 |
mTextEditHelper->Finish(trackList->GetOwner()); |
| 574 |
} |
| 575 |
ResetClipNameEdit(); |
| 576 |
return RefreshCell; |
| 577 |
} |
| 578 |
return RefreshNone; |
| 579 |
} |
| 580 |
|
| 581 |
bool WaveTrackAffordanceControls::OnTextCopy(AudacityProject& project) |
| 582 |
{ |
| 583 |
if (mTextEditHelper) |
| 584 |
{ |
| 585 |
mTextEditHelper->CopySelectedText(project); |
| 586 |
return true; |
| 587 |
} |
| 588 |
return false; |
| 589 |
} |
| 590 |
|
| 591 |
bool WaveTrackAffordanceControls::OnTextCut(AudacityProject& project) |
| 592 |
{ |
| 593 |
if (mTextEditHelper) |
| 594 |
{ |
| 595 |
mTextEditHelper->CutSelectedText(project); |
| 596 |
return true; |
| 597 |
} |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
bool WaveTrackAffordanceControls::OnTextPaste(AudacityProject& project) |
| 602 |
{ |
| 603 |
if (mTextEditHelper) |
| 604 |
{ |
| 605 |
mTextEditHelper->PasteSelectedText(project); |
| 606 |
return true; |
| 607 |
} |
| 608 |
return false; |
| 609 |
} |
| 610 |
|
| 611 |
bool WaveTrackAffordanceControls::OnTextSelect(AudacityProject& project) |
| 612 |
{ |
| 613 |
if (mTextEditHelper) |
| 614 |
{ |
| 615 |
mTextEditHelper->SelectAll(); |
| 616 |
return true; |
| 617 |
} |
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
unsigned WaveTrackAffordanceControls::OnAffordanceClick(const TrackPanelMouseEvent& event, AudacityProject* project) |
| 622 |
{ |
| 623 |
auto& viewInfo = ViewInfo::Get(*project); |
| 624 |
if (mTextEditHelper) |
| 625 |
{ |
| 626 |
if (auto interval = *mEditedInterval) |
| 627 |
{ |
| 628 |
auto affordanceRect = ClipParameters::GetClipRect(*interval, viewInfo, event.rect); |
| 629 |
if (!affordanceRect.Contains(event.event.GetPosition())) |
| 630 |
return ExitTextEditing(); |
| 631 |
} |
| 632 |
} |
| 633 |
else if (auto interval = *mFocusInterval) |
| 634 |
{ |
| 635 |
if (event.event.LeftDClick()) |
| 636 |
{ |
| 637 |
auto affordanceRect = ClipParameters::GetClipRect(*interval, viewInfo, event.rect); |
| 638 |
if (affordanceRect.Contains(event.event.GetPosition()) && |
| 639 |
StartEditClipName(*project, mFocusInterval)) |
| 640 |
{ |
| 641 |
event.event.Skip(false); |
| 642 |
return RefreshCode::RefreshCell; |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
return RefreshCode::RefreshNone; |
| 647 |
} |
| 648 |
|
| 649 |
void WaveTrackAffordanceControls::StartEditSelectedClipName(AudacityProject& project) |
| 650 |
{ |
| 651 |
auto [track, it] = SelectedIntervalOfFocusedTrack(project); |
| 652 |
if(track != FindTrack()) |
| 653 |
return; |
| 654 |
StartEditClipName(project, it); |
| 655 |
} |
| 656 |
|
| 657 |
void WaveTrackAffordanceControls::StartEditSelectedClipSpeed( |
| 658 |
AudacityProject& project) |
| 659 |
{ |
| 660 |
constexpr auto wholeInterval = false; |
| 661 |
auto [track, it] = SelectedIntervalOfFocusedTrack(project, wholeInterval); |
| 662 |
|
| 663 |
if (track != FindTrack()) |
| 664 |
return; |
| 665 |
|
| 666 |
auto interval = *it; |
| 667 |
|
| 668 |
if (!interval) |
| 669 |
return; |
| 670 |
|
| 671 |
PitchAndSpeedDialog::Get(project).Retarget(track, interval).SetFocus({}); |
| 672 |
} |
| 673 |
|
| 674 |
void WaveTrackAffordanceControls::OnRenderClipStretching( |
| 675 |
AudacityProject& project) const |
| 676 |
{ |
| 677 |
const auto [track, it] = SelectedIntervalOfFocusedTrack(project); |
| 678 |
|
| 679 |
if (track != FindTrack()) |
| 680 |
return; |
| 681 |
|
| 682 |
auto interval = *it; |
| 683 |
|
| 684 |
if (!interval || !interval->HasPitchOrSpeed()) |
| 685 |
return; |
| 686 |
|
| 687 |
TimeStretching::WithClipRenderingProgress( |
| 688 |
[track = track, interval = interval](const ProgressReporter& progress) { |
| 689 |
track->ApplyPitchAndSpeed( |
| 690 |
{ { interval->GetPlayStartTime(), interval->GetPlayEndTime() } }, |
| 691 |
progress); |
| 692 |
}, |
| 693 |
XO("Applying...")); |
| 694 |
|
| 695 |
ProjectHistory::Get(project).PushState( |
| 696 |
XO("Rendered time-stretched audio"), XO("Render")); |
| 697 |
|
| 698 |
WaveClipUIUtilities::SelectClip(project, *interval); |
| 699 |
} |
| 700 |
|
| 701 |
std::shared_ptr<TextEditHelper> WaveTrackAffordanceControls::MakeTextEditHelper(const wxString& text) |
| 702 |
{ |
| 703 |
auto helper = std::make_shared<TextEditHelper>(shared_from_this(), text, mClipNameFont); |
| 704 |
helper->SetTextColor(theTheme.Colour(clrClipNameText)); |
| 705 |
helper->SetTextSelectionColor(theTheme.Colour(clrClipNameTextSelection)); |
| 706 |
return helper; |
| 707 |
} |
| 708 |
|
| 709 |
auto WaveTrackAffordanceControls::GetMenuItems( |
| 710 |
const wxRect &rect, const wxPoint *pPosition, AudacityProject *pProject) |
| 711 |
-> std::vector<MenuItem> |
| 712 |
{ |
| 713 |
return WaveClipUIUtilities::GetWaveClipMenuItems(); |
| 714 |
} |
| 715 |
|
| 716 |
// Register a menu item |
| 717 |
|
| 718 |
namespace { |
| 719 |
|
| 720 |
// Menu handler functions |
| 721 |
|
| 722 |
void OnEditClipName(const CommandContext &context) |
| 723 |
{ |
| 724 |
auto &project = context.project; |
| 725 |
|
| 726 |
if(auto pWaveTrack = dynamic_cast<WaveTrack *>(TrackFocus::Get(project).Get())) |
| 727 |
{ |
| 728 |
if(auto pAffordance = FindAffordance(*pWaveTrack)) |
| 729 |
{ |
| 730 |
pAffordance->StartEditSelectedClipName(project); |
| 731 |
// Refresh so the cursor appears |
| 732 |
TrackPanel::Get(project).RefreshTrack(pWaveTrack); |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
void OnChangePitchAndSpeed(const CommandContext& context) |
| 738 |
{ |
| 739 |
auto& project = context.project; |
| 740 |
|
| 741 |
if ( |
| 742 |
auto pWaveTrack = |
| 743 |
dynamic_cast<WaveTrack*>(TrackFocus::Get(project).Get())) |
| 744 |
{ |
| 745 |
if (auto pAffordance = FindAffordance(*pWaveTrack)) |
| 746 |
pAffordance->StartEditSelectedClipSpeed(project); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
void OnRenderClipStretching(const CommandContext& context) |
| 751 |
{ |
| 752 |
auto& project = context.project; |
| 753 |
|
| 754 |
if ( |
| 755 |
auto pWaveTrack = |
| 756 |
dynamic_cast<WaveTrack*>(TrackFocus::Get(project).Get())) |
| 757 |
{ |
| 758 |
if (auto pAffordance = FindAffordance(*pWaveTrack)) |
| 759 |
pAffordance->OnRenderClipStretching(project); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
void OnPitchShift( |
| 764 |
const CommandContext& context, bool up) |
| 765 |
{ |
| 766 |
auto [track, it] = SelectedIntervalOfFocusedTrack(context.project); |
| 767 |
if (!track) |
| 768 |
return; |
| 769 |
const auto interval = *it; |
| 770 |
if (interval->SetCentShift(interval->GetCentShift() + (up ? 100 : -100))) |
| 771 |
ProjectHistory::Get(context.project) |
| 772 |
.PushState( |
| 773 |
XO("Pitch Shift"), XO("Changed Pitch Shift"), |
| 774 |
UndoPush::CONSOLIDATE); |
| 775 |
} |
| 776 |
|
| 777 |
void OnPitchUp(const CommandContext& context) |
| 778 |
{ |
| 779 |
OnPitchShift(context, true); |
| 780 |
} |
| 781 |
|
| 782 |
void OnPitchDown(const CommandContext& context) |
| 783 |
{ |
| 784 |
OnPitchShift(context, false); |
| 785 |
} |
| 786 |
|
| 787 |
using namespace MenuRegistry; |
| 788 |
|
| 789 |
// Register menu items |
| 790 |
|
| 791 |
AttachedItem sAttachment{ |
| 792 |
Command( L"RenameClip", XXO("&Rename Clip..."), |
| 793 |
OnEditClipName, SomeClipIsSelectedFlag(), wxT("Ctrl+F2") ), |
| 794 |
wxT("Edit/Other/Clip") |
| 795 |
}; |
| 796 |
|
| 797 |
AttachedItem sAttachment2 { |
| 798 |
Command( |
| 799 |
L"ChangePitchAndSpeed", XXO("&Pitch and Speed..."), OnChangePitchAndSpeed, |
| 800 |
AlwaysEnabledFlag, Options { wxT("Ctrl+Shift+P") }), |
| 801 |
wxT("Edit/Other/Clip") |
| 802 |
}; |
| 803 |
|
| 804 |
AttachedItem sAttachment3{ |
| 805 |
Command( L"RenderPitchAndSpeed", XXO("Render Pitch and &Speed"), |
| 806 |
OnRenderClipStretching, StretchedClipIsSelectedFlag()), |
| 807 |
wxT("Edit/Other/Clip") |
| 808 |
}; |
| 809 |
|
| 810 |
AttachedItem sAttachment4{ |
| 811 |
Command( L"PitchUp", XXO("Pitch &Up"), |
| 812 |
OnPitchUp, SomeClipIsSelectedFlag(), Options{ wxT("Alt+Up") }), |
| 813 |
wxT("Edit/Other/Clip") |
| 814 |
}; |
| 815 |
|
| 816 |
AttachedItem sAttachment5{ |
| 817 |
Command( L"PitchDown", XXO("Pitch &Down"), |
| 818 |
OnPitchDown, SomeClipIsSelectedFlag(), Options{ wxT("Alt+Down") }), |
| 819 |
wxT("Edit/Other/Clip") |
| 820 |
}; |
| 821 |
} |