1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
|
/**********************************************************************
Audacity: A Digital Audio Editor
CellularPanel.cpp
Dominic Mazzoni
and lots of other contributors
Implements CellularPanel.
********************************************************************//*!
\class CellularPanel
\brief
Formerly part of TrackPanel, this abstract base class has no special knowledge
of Track objects and is intended for reuse with other windows.
Manages a division of a panel's area into disjoint rectangles, each with
an associated Cell object. Details of that partition and association, and
the choice of the cell with keyboard focus, are subclass responsibilities.
Handling of keyboard events is delegated to the focused cell. The cell under
the mouse position is queried for hit-test candidate objects, which handle
click-drag-release (and ESC key abort) sequences.
*//*****************************************************************/
#include "CellularPanel.h"
#include <wx/eventfilter.h>
#include <wx/setup.h> // for wxUSE_* macros
#include "KeyboardCapture.h"
#include "UIHandle.h"
#include "TrackPanelMouseEvent.h"
#include "HitTestResult.h"
#include "RefreshCode.h"
#include "TrackPanelCell.h"
// A singleton class that intercepts escape key presses when some cellular
// panel is dragging
struct CellularPanel::Filter : wxEventFilter
{
Filter()
{
wxEvtHandler::AddFilter( this );
}
~Filter()
{
wxEvtHandler::RemoveFilter( this );
}
static void Create()
{
static Filter instance;
}
int FilterEvent( wxEvent &event ) override
{
const auto type = event.GetEventType();
if (type == wxEVT_KEY_DOWN &&
static_cast< wxKeyEvent& >( event ).GetKeyCode() == WXK_ESCAPE ) {
bool eatEvent = false;
for (const auto &pPanel: {spClickedPanel, spEnteredPanel}) {
if (pPanel) {
eatEvent = true;
// Handle escape either in the clicked panel to abort a drag, or
// to switch among hit test candidates before button down in the
// entered (but not yet clicked) panel
pPanel->HandleEscapeKey( true );
}
}
if (eatEvent)
return Event_Processed;
}
else if ((type == wxEVT_LEFT_DOWN ||
type == wxEVT_RIGHT_DOWN ||
type == wxEVT_MIDDLE_DOWN)) {
if ( spClickedPanel &&
spClickedPanel != event.GetEventObject() ) {
// Clicking away from the panel doesn't necessarily change wxWidgets
// focus, so we use this global filter instead
spClickedPanel->DoKillFocus();
// Don't eat the event
}
}
return Event_Skip;
}
static wxWeakRef< CellularPanel > spClickedPanel;
static wxWeakRef< CellularPanel > spEnteredPanel;
};
wxWeakRef< CellularPanel > CellularPanel::Filter::spClickedPanel = nullptr;
wxWeakRef< CellularPanel > CellularPanel::Filter::spEnteredPanel = nullptr;
struct CellularPanel::State
{
UIHandlePtr mUIHandle;
std::weak_ptr<TrackPanelCell> mLastCell;
std::vector<UIHandlePtr> mTargets;
size_t mTarget {};
unsigned mMouseOverUpdateFlags{};
int mMouseMostRecentX;
int mMouseMostRecentY;
std::weak_ptr<TrackPanelCell> mpClickedCell;
bool mEnableTab{};
};
BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel)
EVT_MOUSE_EVENTS(CellularPanel::OnMouseEvent)
EVT_MOUSE_CAPTURE_LOST(CellularPanel::OnCaptureLost)
EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, CellularPanel::OnCaptureKey)
EVT_KEY_DOWN(CellularPanel::OnKeyDown)
EVT_KEY_UP(CellularPanel::OnKeyUp)
EVT_CHAR(CellularPanel::OnChar)
EVT_SET_FOCUS(CellularPanel::OnSetFocus)
EVT_KILL_FOCUS(CellularPanel::OnKillFocus)
EVT_CONTEXT_MENU(CellularPanel::OnContextMenu)
END_EVENT_TABLE()
CellularPanel::CellularPanel(
wxWindow * parent, wxWindowID id,
const wxPoint & pos, const wxSize & size,
ViewInfo *viewInfo,
long style)
: OverlayPanel(parent, id, pos, size, style)
, mViewInfo( viewInfo )
, mState{ std::make_unique<State>() }
{
// Create the global event filter instance for CellularPanels only when the
// first CellularPanel is created, not sooner, so that the filter will be
// invoked before that for the application.
Filter::Create();
}
CellularPanel::~CellularPanel() = default;
void CellularPanel::HandleInterruptedDrag()
{
auto &state = *mState;
if (state.mUIHandle &&
state.mUIHandle->StopsOnKeystroke() ) {
// The bogus id isn't used anywhere, but may help with debugging.
// as this is sending a bogus mouse up. The mouse button is still actually down
// and may go up again.
const int idBogusUp = 2;
wxMouseEvent evt { wxEVT_LEFT_UP };
evt.SetId( idBogusUp );
evt.SetPosition(this->ScreenToClient(::wxGetMousePosition()));
this->ProcessEvent(evt);
}
}
void CellularPanel::Uncapture(bool escaping, wxMouseState *pState)
{
auto state = ::wxGetMouseState();
if (!pState) {
// Remap the position
state.SetPosition(this->ScreenToClient(state.GetPosition()));
pState = &state;
}
if (HasCapture())
ReleaseMouse();
HandleMotion( *pState );
if ( escaping || !AcceptsFocus() )
Filter::spClickedPanel = nullptr;
}
bool CellularPanel::CancelDragging( bool escaping )
{
auto &state = *mState;
if (state.mUIHandle) {
// copy shared_ptr for safety, as in HandleClick
auto handle = state.mUIHandle;
// UIHANDLE CANCEL
UIHandle::Result refreshResult = handle->Cancel(GetProject());
auto pClickedCell = state.mpClickedCell.lock();
if (pClickedCell)
ProcessUIHandleResult(
pClickedCell.get(), {},
refreshResult | state.mMouseOverUpdateFlags );
state.mpClickedCell.reset();
state.mUIHandle.reset(), handle.reset(), ClearTargets();
Uncapture( escaping );
return true;
}
return false;
}
bool CellularPanel::HandleEscapeKey(bool down)
{
if (!down)
return false;
{
auto target = Target();
const auto pProject = GetProject();
if (target && target->HasEscape(pProject) && target->Escape(pProject)) {
HandleCursorForPresentMouseState(false);
return true;
}
}
auto &state = *mState;
if (state.mUIHandle) {
CancelDragging( true );
return true;
}
if (ChangeTarget(true, false)) {
HandleCursorForPresentMouseState(false);
return true;
}
return false;
}
void CellularPanel::UpdateMouseState(const wxMouseState &state)
{
mLastMouseState = state;
// Simulate a down button if none, so hit test routines can anticipate
// which button will be clicked
if (!state.ButtonIsDown(wxMOUSE_BTN_ANY)) {
#ifdef __WXOSX__
if (state.RawControlDown())
// On Mac we can distinctly anticipate "right" click (as Control+click)
mLastMouseState.SetRightDown( true ),
mLastMouseState.SetLeftDown( false );
else
#endif
// Anticipate a left click by default
mLastMouseState.SetRightDown( false ),
mLastMouseState.SetLeftDown( true );
}
}
void CellularPanel::HandleModifierKey()
{
HandleCursorForPresentMouseState();
}
void CellularPanel::HandleCursorForPresentMouseState(bool doHit)
{
// Come here on modifier key or mouse button transitions,
// or on starting or stopping of play or record,
// or change of toolbar button,
// and change the cursor appropriately.
// Get the button and key states
auto state = ::wxGetMouseState();
// Remap the position
state.SetPosition(this->ScreenToClient(state.GetPosition()));
HandleMotion( state, doHit );
}
/// CellularPanel::HandleMotion( ) sets the cursor drawn at the mouse location,
/// and updates the status bar message.
/// We treat certain other changes of mouse button and key state as "motions"
/// too, and also starting and stopping of playback or recording, all of which
/// may cause the appropriate cursor and message to change.
/// As this procedure checks which region the mouse is over, it is
/// appropriate to establish the message in the status bar.
void CellularPanel::HandleMotion( wxMouseState &inState, bool doHit )
{
UpdateMouseState( inState );
const auto foundCell = FindCell( inState.m_x, inState.m_y );
auto &rect = foundCell.rect;
auto &pCell = foundCell.pCell;
const TrackPanelMouseState tpmState{ mLastMouseState, rect, pCell };
HandleMotion( tpmState, doHit );
}
void CellularPanel::HandleMotion
( const TrackPanelMouseState &tpmState, bool doHit )
{
auto &state = *mState;
auto handle = state.mUIHandle;
auto newCell = tpmState.pCell;
auto oldCell = state.mLastCell.lock();
auto oldHandle = Target();
TranslatableString status, tooltip;
wxCursor *pCursor{};
unsigned refreshCode = 0;
if ( ! doHit ) {
// Dragging or not
handle = Target();
// Assume cell does not change but target does
refreshCode = state.mMouseOverUpdateFlags;
state.mMouseOverUpdateFlags = 0;
}
else if ( !state.mUIHandle ) {
// Not yet dragging.
unsigned updateFlags = state.mMouseOverUpdateFlags;
// First check whether crossing cell to cell
if ( newCell == oldCell )
oldCell.reset();
else {
// Forget old targets
ClearTargets();
// Re-draw any highlighting
if (oldCell) {
ProcessUIHandleResult(
oldCell.get(), oldCell.get(), updateFlags);
}
}
auto oldPosition = state.mTarget;
// Now do the
// UIHANDLE HIT TEST !
state.mTargets.clear();
if (newCell)
state.mTargets = newCell->HitTest(tpmState, GetProject());
state.mTarget = 0;
// Find the old target's NEW place if we can
if (oldHandle) {
auto begin = state.mTargets.begin(), end = state.mTargets.end(),
iter = std::find(begin, end, oldHandle);
if (iter != end) {
size_t newPosition = iter - begin;
if (newPosition <= oldPosition)
state.mTarget = newPosition;
// else, some NEW hit at this position takes priority
}
}
handle = Target();
state.mLastCell = newCell;
// These lines caused P2 Bug 2617, repeated refreshing using all CPU.
// Disabling them might be causing something to not refresh,
// but so far I have not found a downside to disabling them. JKC
// VS: https://github.com/audacity/audacity/issues/1363
// Extensive refresh request fixed by using std::move on
// new envelope handle instance
if (!oldCell && oldHandle != handle)
// Did not move cell to cell, but did change the target
refreshCode = updateFlags;
if (handle && handle != oldHandle)
handle->Enter(true, GetProject());
if (oldHandle == handle)
oldHandle.reset();
}
// UIHANDLE PREVIEW
// Update status message and cursor, whether dragging or not
if (handle) {
auto preview = handle->Preview( tpmState, GetProject() );
status = preview.message;
tooltip = preview.tooltip;
pCursor = preview.cursor;
auto code = handle->GetChangeHighlight();
handle->SetChangeHighlight(RefreshCode::RefreshNone);
refreshCode |= code;
state.mMouseOverUpdateFlags |= code;
}
if (newCell &&
(!pCursor || status.empty() || tooltip.empty())) {
// Defaulting of cursor, tooltip, and status if there is no handle,
// or if the handle does not specify them
const auto preview = newCell->DefaultPreview( tpmState, GetProject() );
if (!pCursor)
pCursor = preview.cursor;
if (status.empty())
status = preview.message;
if (tooltip.empty())
tooltip = preview.tooltip;
}
if (!pCursor) {
// Ultimate default cursor
static wxCursor defaultCursor{ wxCURSOR_DEFAULT };
pCursor = &defaultCursor;
}
// Update status, tooltip, and cursor only if we're dragging, or the mouse
// was in one of our cells and nobody else is dragging
if (handle || (newCell && !wxWindow::GetCapture())) {
UpdateStatusMessage(status);
#if wxUSE_TOOLTIPS
if (tooltip.Translation() != GetToolTipText()) {
// Unset first, by analogy with AButton
UnsetToolTip();
if (handle != oldHandle)
SetToolTip(tooltip);
}
#endif
if (pCursor)
SetCursor( *pCursor );
}
else if ( oldCell || oldHandle )
// Leaving a cell or hit test target with no replacement
UpdateStatusMessage( {} );
if (newCell)
ProcessUIHandleResult(newCell.get(), newCell.get(), refreshCode);
}
void CellularPanel::Leave()
{
// Make transition into an empty CellularPanel state
auto state = ::wxGetMouseState();
const wxRect rect;
std::shared_ptr<TrackPanelCell> pCell;
TrackPanelMouseState tpmState{ state, rect, pCell };
HandleMotion( tpmState );
}
bool CellularPanel::HasRotation()
{
auto &state = *mState;
// Is there a nontrivial TAB key rotation?
if ( state.mTargets.size() > 1 )
return true;
auto target = Target();
return target && target->HasRotation();
}
bool CellularPanel::HasEscape()
{
if (IsMouseCaptured())
return true;
auto &state = *mState;
if (state.mTarget + 1 == state.mTargets.size() &&
Target() &&
!Target()->HasEscape(GetProject()))
return false;
return state.mTargets.size() > 0;
}
bool CellularPanel::ChangeTarget(bool forward, bool cycle)
{
auto &state = *mState;
auto size = state.mTargets.size();
auto target = Target();
if (target && target->HasRotation()) {
if(target->Rotate(forward))
return true;
else if (cycle && (size == 1 || IsMouseCaptured())) {
// Rotate through the states of this target only.
target->Enter(forward, GetProject());
return true;
}
}
if (!cycle &&
((forward && state.mTarget + 1 == size) ||
(!forward && state.mTarget == 0)))
return false;
if (size > 1) {
if (forward)
++state.mTarget;
else
state.mTarget += size - 1;
state.mTarget %= size;
if (Target())
Target()->Enter(forward, GetProject());
return true;
}
return false;
}
/// Determines if a modal tool is active
bool CellularPanel::IsMouseCaptured()
{
auto &state = *mState;
return state.mUIHandle != NULL;
}
void CellularPanel::OnContextMenu(wxContextMenuEvent & WXUNUSED(event))
{
DoContextMenu();
}
/// Handle mouse wheel rotation (for zoom in/out, vertical and horizontal scrolling)
void CellularPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent )
{
auto pCell = tpmEvent.pCell;
if (!pCell)
return;
auto &event = tpmEvent.event;
double steps {};
#if defined(__WXMAC__) && defined(EVT_MAGNIFY)
// PRL:
// Pinch and spread implemented in wxWidgets 3.1.0, or cherry-picked from
// the future in custom build of 3.0.2
if (event.Magnify()) {
event.SetControlDown(true);
steps = 2 * event.GetMagnification();
}
else
#endif
{
steps = event.m_wheelRotation /
(event.m_wheelDelta > 0 ? (double)event.m_wheelDelta : 120.0);
}
if(event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL) {
// Two-fingered horizontal swipe on mac is treated like shift-mousewheel
event.SetShiftDown(true);
// This makes the wave move in the same direction as the fingers, and the scrollbar
// thumb moves oppositely
steps *= -1;
}
tpmEvent.steps = steps;
if(!event.HasAnyModifiers()) {
// We will later un-skip if we do anything, but if we don't,
// propagate the event up for the sake of the scrubber
event.Skip();
event.ResumePropagation(wxEVENT_PROPAGATE_MAX);
}
unsigned result =
pCell->HandleWheelRotation( tpmEvent, GetProject() );
ProcessUIHandleResult(
pCell.get(), pCell.get(), result);
}
void CellularPanel::OnCaptureKey(wxCommandEvent & event)
{
auto &state = *mState;
state.mEnableTab = false;
wxKeyEvent *kevent = static_cast<wxKeyEvent *>(event.GetEventObject());
const auto code = kevent->GetKeyCode();
if ( WXK_ESCAPE != code )
HandleInterruptedDrag();
// Give focused cell precedence
const auto t = GetFocusedCell();
if (t) {
const unsigned refreshResult =
t->CaptureKey(*kevent, *mViewInfo, this, GetProject());
ProcessUIHandleResult(t, t, refreshResult);
event.Skip(kevent->GetSkipped());
}
#if 0
// Special TAB key handling, but only if the cell didn't capture it
if ( !(t && !kevent->GetSkipped()) &&
WXK_TAB == code && HasRotation() ) {
// Override TAB navigation in wxWidgets, by not skipping
event.Skip(false);
mEnableTab = true;
return;
}
else
#endif
if (!t)
event.Skip();
}
void CellularPanel::OnKeyDown(wxKeyEvent & event)
{
switch (event.GetKeyCode())
{
case WXK_ESCAPE:
// This switch case is now redundant with the global filter
if(HandleEscapeKey(true))
// Don't skip the event, eat it so that
// AudacityApp does not also stop any playback.
return;
else
break;
case WXK_ALT:
case WXK_SHIFT:
case WXK_CONTROL:
#ifdef __WXOSX__
case WXK_RAW_CONTROL:
#endif
HandleModifierKey();
break;
#if 0
case WXK_TAB:
if ( mEnableTab && HasRotation() ) {
ChangeTarget( !event.ShiftDown(), true );
HandleCursorForPresentMouseState(false);
return;
}
else
break;
#endif
}
const auto t = GetFocusedCell();
if (t) {
const unsigned refreshResult =
t->KeyDown(event, *mViewInfo, this, GetProject());
ProcessUIHandleResult(t, t, refreshResult);
}
else
event.Skip();
}
void CellularPanel::OnChar(wxKeyEvent & event)
{
switch (event.GetKeyCode())
{
case WXK_ESCAPE:
case WXK_ALT:
case WXK_SHIFT:
case WXK_CONTROL:
case WXK_PAGEUP:
case WXK_PAGEDOWN:
return;
}
const auto t = GetFocusedCell();
if (t) {
const unsigned refreshResult =
t->Char(event, *mViewInfo, this, GetProject());
ProcessUIHandleResult(t, t, refreshResult);
}
else
event.Skip();
}
void CellularPanel::OnKeyUp(wxKeyEvent & event)
{
bool didSomething = false;
switch (event.GetKeyCode())
{
case WXK_ESCAPE:
didSomething = HandleEscapeKey(false);
break;
case WXK_ALT:
case WXK_SHIFT:
case WXK_CONTROL:
#ifdef __WXOSX__
case WXK_RAW_CONTROL:
#endif
HandleModifierKey();
break;
}
if (didSomething)
return;
const auto t = GetFocusedCell();
if (t) {
const unsigned refreshResult =
t->KeyUp(event, *mViewInfo, this, GetProject());
ProcessUIHandleResult(t, t, refreshResult);
return;
}
event.Skip();
}
/// Should handle the case when the mouse capture is lost. (MSW only)
void CellularPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event))
{
auto &state = *mState;
state.mUIHandle.reset();
Leave();
// This is bad. We are lying abou the event by saying it is a mouse up.
wxMouseEvent e(wxEVT_LEFT_UP);
e.SetId( kCaptureLostEventId );
e.m_x = state.mMouseMostRecentX;
e.m_y = state.mMouseMostRecentY;
OnMouseEvent(e);
}
/// This handles just generic mouse events. Then, based
/// on our current state, we forward the mouse events to
/// various interested parties.
void CellularPanel::OnMouseEvent(wxMouseEvent & event)
try
{
const auto foundCell = FindCell( event.m_x, event.m_y );
auto &rect = foundCell.rect;
auto &pCell = foundCell.pCell;
const auto size = GetSize();
TrackPanelMouseEvent tpmEvent{ event, rect, size, pCell };
#if defined(__WXMAC__) && defined(EVT_MAGNIFY)
// PRL:
// Pinch and spread implemented in wxWidgets 3.1.0, or cherry-picked from
// the future in custom build of 3.0.2
if (event.Magnify()) {
HandleWheelRotation( tpmEvent );
}
#endif
// If a mouse event originates from a keyboard context menu event then
// event.GetPosition() == wxDefaultPosition. wxContextMenu events are handled in
// CellularPanel::OnContextMenu(), and therefore associated mouse events are ignored here.
// Not ignoring them was causing bug 613: the mouse events were interpreted as clicking
// outside the tracks.
if (event.GetPosition() == wxDefaultPosition && (event.RightDown() || event.RightUp())) {
event.Skip();
return;
}
if (event.m_wheelRotation != 0)
HandleWheelRotation( tpmEvent );
if (event.LeftDown() || event.LeftIsDown() || event.Moving()) {
// Skip, even if we do something, so that the left click or drag
// may have an additional effect in the scrubber.
event.Skip();
event.ResumePropagation(wxEVENT_PROPAGATE_MAX);
}
auto &state = *mState;
state.mMouseMostRecentX = event.m_x;
state.mMouseMostRecentY = event.m_y;
if (event.LeftDown()) {
// The activate event is used to make the
// parent window 'come alive' if it didn't have focus.
wxActivateEvent e;
GetParent()->GetEventHandler()->ProcessEvent(e);
}
if (event.Entering())
{
Filter::spEnteredPanel = this;
}
else if (event.Leaving())
{
if (Filter::spEnteredPanel == this)
Filter::spEnteredPanel = nullptr;
Leave();
auto buttons =
// Bug 1325: button state in Leaving events is unreliable on Mac.
// Poll the global state instead.
// event.ButtonIsDown(wxMOUSE_BTN_ANY);
::wxGetMouseState().ButtonIsDown(wxMOUSE_BTN_ANY);
if (!buttons) {
CancelDragging( false );
#if defined(__WXMAC__)
// We must install the cursor ourselves since the window under
// the mouse is no longer this one and wx2.8.12 makes that check.
// Should re-evaluate with wx3.
wxSTANDARD_CURSOR->MacInstall();
#endif
}
}
if (state.mUIHandle) {
auto pClickedCell = state.mpClickedCell.lock();
if (event.Dragging()) {
// UIHANDLE DRAG
// copy shared_ptr for safety, as in HandleClick
auto handle = state.mUIHandle;
const UIHandle::Result refreshResult =
handle->Drag( tpmEvent, GetProject() );
ProcessUIHandleResult
(pClickedCell.get(), pCell.get(), refreshResult);
state.mMouseOverUpdateFlags |= refreshResult;
if (refreshResult & RefreshCode::Cancelled) {
// Drag decided to abort itself
state.mUIHandle.reset(), handle.reset(), ClearTargets();
state.mpClickedCell.reset();
Uncapture( false, &event );
}
else {
UpdateMouseState(event);
TrackPanelMouseState tpmState{ mLastMouseState, rect, pCell };
HandleMotion( tpmState );
}
}
else if (event.ButtonUp()) {
// UIHANDLE RELEASE
// copy shared_ptr for safety, as in HandleClick
auto handle = state.mUIHandle;
unsigned moreFlags = state.mMouseOverUpdateFlags;
UIHandle::Result refreshResult =
handle->Release( tpmEvent, GetProject(), this );
ProcessUIHandleResult
(pClickedCell.get(), pCell.get(),
refreshResult | moreFlags);
state.mUIHandle.reset(), handle.reset(), ClearTargets();
state.mpClickedCell.reset();
// will also Uncapture() below
}
}
else if ( event.GetEventType() == wxEVT_MOTION )
// Update status message and cursor, not during drag
// consider it not a drag, even if button is down during motion, if
// mUIHandle is null, as it becomes during interrupted drag
// (e.g. by hitting space to play while dragging an envelope point)
HandleMotion( event );
else if ( event.ButtonDown() || event.ButtonDClick() )
HandleClick( tpmEvent );
if (event.ButtonDown() && IsMouseCaptured()) {
if (!HasCapture())
CaptureMouse();
}
if (event.ButtonUp())
Uncapture( false );
}
catch( ... )
{
// Abort any dragging, as if by hitting Esc
if ( CancelDragging( true ) )
;
else {
Uncapture( true );
Refresh(false);
}
throw;
}
namespace {
class DefaultRightButtonHandler : public UIHandle {
public:
explicit DefaultRightButtonHandler(
const std::shared_ptr<TrackPanelCell> &pCell )
: mwCell{ pCell }
{}
~DefaultRightButtonHandler() override;
virtual Result Click
(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
{
return RefreshCode::RefreshNone;
}
virtual Result Drag
(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
{
return RefreshCode::RefreshNone;
}
virtual HitTestPreview Preview
(const TrackPanelMouseState &state, AudacityProject *pProject) override
{
return {};
}
virtual Result Release
(const TrackPanelMouseEvent &event, AudacityProject *pProject,
wxWindow *pParent) override
{
if (auto pCell = mwCell.lock()) {
auto point = event.event.GetPosition();
return pCell->DoContextMenu(event.rect, pParent, &point, pProject);
}
return RefreshCode::RefreshNone;
}
virtual Result Cancel(AudacityProject *pProject) override
{
return RefreshCode::RefreshNone;
}
private:
std::weak_ptr<TrackPanelCell> mwCell;
};
DefaultRightButtonHandler::~DefaultRightButtonHandler()
{
}
}
void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent )
{
auto pCell = tpmEvent.pCell;
// Do hit test once more, in case the button really pressed was not the
// one "anticipated."
{
TrackPanelMouseState tpmState{
tpmEvent.event,
tpmEvent.rect,
tpmEvent.pCell
};
HandleMotion( tpmState );
}
auto &state = *mState;
state.mUIHandle = Target();
if (tpmEvent.event.RightDown() &&
!(state.mUIHandle && state.mUIHandle->HandlesRightClick())) {
if (auto pCell = state.mLastCell.lock())
state.mUIHandle = std::make_shared<DefaultRightButtonHandler>(pCell);
}
if (state.mUIHandle) {
// UIHANDLE CLICK
// Make another shared pointer to the handle, in case recursive
// event dispatching otherwise tries to delete the handle.
auto handle = state.mUIHandle;
UIHandle::Result refreshResult =
handle->Click( tpmEvent, GetProject() );
if (refreshResult & RefreshCode::Cancelled)
state.mUIHandle.reset(), handle.reset(), ClearTargets();
else {
Filter::spClickedPanel = this;
#if wxUSE_TOOLTIPS
// Remove any outstanding tooltip
UnsetToolTip();
#endif
if( !HasFocus() && AcceptsFocus() )
SetFocusIgnoringChildren();
state.mpClickedCell = pCell;
// Perhaps the clicked handle wants to update cursor and state message
// after a click.
TrackPanelMouseState tpmState{
tpmEvent.event,
tpmEvent.rect,
tpmEvent.pCell
};
HandleMotion( tpmState );
}
ProcessUIHandleResult(
pCell.get(), pCell.get(), refreshResult);
state.mMouseOverUpdateFlags |= refreshResult;
}
}
void CellularPanel::DoContextMenu( TrackPanelCell *pCell )
{
if( !pCell ) {
pCell = GetFocusedCell();
if( !pCell )
return;
}
const auto delegate = pCell->ContextMenuDelegate();
if (!delegate)
return;
auto rect = FindRect( *delegate );
const UIHandle::Result refreshResult =
delegate->DoContextMenu(rect, this, nullptr, GetProject());
// To do: use safer shared_ptr to pCell
ProcessUIHandleResult(pCell, pCell, refreshResult);
}
void CellularPanel::OnSetFocus(wxFocusEvent &event)
{
SetFocusedCell();
Refresh( false);
}
void CellularPanel::DoKillFocus()
{
if (auto pCell = GetFocusedCell()) {
auto refreshResult = pCell->LoseFocus(GetProject());
auto &state = *mState;
auto pClickedCell = state.mpClickedCell.lock();
if (pClickedCell)
ProcessUIHandleResult( pClickedCell.get(), {}, refreshResult );
}
Refresh( false);
}
void CellularPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event))
{
DoKillFocus();
if (KeyboardCapture::IsHandler(this))
{
KeyboardCapture::Release(this);
}
}
// Empty out-of-line default functions to fill Visitor's vtable
CellularPanel::Visitor::~Visitor() {}
void CellularPanel::Visitor::VisitCell(
const wxRect &rect, TrackPanelCell &cell ) {}
void CellularPanel::Visitor::BeginGroup(
const wxRect &rect, TrackPanelGroup &group ) {}
void CellularPanel::Visitor::EndGroup(
const wxRect &rect, TrackPanelGroup &group ) {}
// Public, top-level entry for generalized Visit
void CellularPanel::Visit( Visitor &visitor )
{
Visit( GetClientRect(), Root(), visitor );
}
// Common utility class for the functions that follow
namespace {
struct Adaptor : CellularPanel::Visitor {
using SimpleCellVisitor = CellularPanel::SimpleCellVisitor;
using SimpleNodeVisitor = CellularPanel::SimpleNodeVisitor;
// Visit cells only
Adaptor( const SimpleCellVisitor& function_ )
: function{ [&](const wxRect &rect, TrackPanelNode &cell) {
return function_( rect, static_cast<TrackPanelCell&>(cell) );
} }
{}
// Visit cells and groups, each once only, choosing pre- or post- ordering
// for the groups
Adaptor( const SimpleNodeVisitor &function_, bool pre_ )
: function{ function_ }, pre{ pre_ }, post{ ! pre_ } {}
void VisitCell( const wxRect &rect, TrackPanelCell &cell ) override
{ return function( rect, cell ); }
void BeginGroup( const wxRect &rect, TrackPanelGroup &group ) override
{ if (pre) return function( rect, group ); }
void EndGroup( const wxRect &rect, TrackPanelGroup &group ) override
{ if (post) return function( rect, group ); }
SimpleNodeVisitor function;
const bool pre{ false }, post{ false };
};
}
// Simplified entry points for visits that don't need all the generality of
// CellularPanel::Visitor
void CellularPanel::VisitCells( const SimpleCellVisitor &visitor )
{
Adaptor adaptor{ visitor };
Visit( adaptor );
}
void CellularPanel::VisitPreorder( const SimpleNodeVisitor &visitor )
{
Adaptor adaptor{ visitor, true };
Visit( adaptor );
}
void CellularPanel::VisitPostorder( const SimpleNodeVisitor &visitor )
{
Adaptor adaptor{ visitor, false };
Visit( adaptor );
}
namespace {
wxRect Subdivide(
const wxRect &rect, bool divideX,
const TrackPanelGroup::Refinement &children,
const TrackPanelGroup::Refinement::const_iterator iter)
{
const auto next = iter + 1;
const auto end = children.end();
wxCoord nextCoord;
if (next == end)
nextCoord = std::max( iter->first,
divideX ? rect.GetRight() : rect.GetBottom() );
else
nextCoord = next->first - 1;
auto lesser = iter->first;
auto greater = nextCoord;
auto result = rect;
if (divideX)
result.SetLeft(lesser), result.SetRight(greater);
else
result.SetTop(lesser), result.SetBottom(greater);
return result;
};
}
// Private, recursive implementation function of Visit
void CellularPanel::Visit(
const wxRect &rect, const std::shared_ptr<TrackPanelNode> &node,
Visitor &visitor )
{
if (auto pCell = dynamic_cast<TrackPanelCell*>(node.get()))
visitor.VisitCell( rect, *pCell );
else if (auto pGroup = dynamic_cast<TrackPanelGroup*>(node.get())) {
visitor.BeginGroup( rect, *pGroup );
// Recur on children
const auto results = pGroup->Children( rect );
const bool divideX = results.first == TrackPanelGroup::Axis::X;
const auto &children = results.second;
const auto begin = children.begin(), end = children.end();
for (auto iter = begin; iter != end; ++iter)
Visit(
Subdivide(rect, divideX, children, iter), iter->second, visitor );
visitor.EndGroup( rect, *pGroup );
}
else
return;
}
auto CellularPanel::FindCell(int mouseX, int mouseY) -> FoundCell
{
auto rect = this->GetClientRect();
auto node = Root();
while (node) {
if ( auto pCell = std::dynamic_pointer_cast< TrackPanelCell >( node ) )
// Found the bottom of the hierarchy
return { pCell, rect };
else if ( auto pGroup = dynamic_cast< TrackPanelGroup* >( node.get() ) ) {
// Ask node for its subdivision
const auto results = pGroup->Children( rect );
const bool divideX = results.first == TrackPanelGroup::Axis::X;
const auto &children = results.second;
// Find the correct child
const auto begin = children.begin(), end = children.end();
auto iter = std::upper_bound( begin, end,
(divideX ? mouseX : mouseY),
[&]( wxCoord coord, const TrackPanelGroup::Child &child ) {
return coord < child.first;
}
);
if (iter == begin)
break;
--iter;
// Descend the hierarchy of nodes
rect = Subdivide(rect, divideX, children, iter);
node = iter->second;
}
else
// Nulls in the array of children are allowed, to define a void with
// no cell
break;
}
return { {}, {} };
}
wxRect CellularPanel::FindRect( const TrackPanelCell &cell )
{
wxRect result;
struct Stop{};
try { VisitCells( [&]( const wxRect &rect, TrackPanelCell &visited ) {
if ( &visited == &cell )
result = rect, throw Stop{};
} ); }
catch ( const Stop& ) {}
return result;
}
wxRect CellularPanel::FindRect(
const std::function< bool( TrackPanelNode& ) > &pred)
{
wxRect result;
struct Stop{};
try { VisitPreorder( [&]( const wxRect &rect, TrackPanelNode &visited ) {
if ( pred( visited ) )
result = rect, throw Stop{};
} ); }
catch ( const Stop& ) {}
return result;
}
UIHandlePtr CellularPanel::Target()
{
auto &state = *mState;
if (state.mTargets.size())
return state.mTargets[state.mTarget];
else
return {};
}
wxCoord CellularPanel::MostRecentXCoord() const
{
auto &state = *mState;
return state.mMouseMostRecentX;
}
void CellularPanel::ClearTargets()
{
auto &state = *mState;
// Forget the rotation of hit test candidates when the mouse moves from
// cell to cell or outside of the panel entirely.
state.mLastCell.reset();
state.mTargets.clear();
state.mTarget = 0;
state.mMouseOverUpdateFlags = 0;
}
std::shared_ptr<TrackPanelCell> CellularPanel::LastCell() const
{
auto &state = *mState;
return state.mLastCell.lock();
}
void CellularPanel::Draw( TrackPanelDrawingContext &context, unsigned nPasses )
{
const auto panelRect = GetClientRect();
auto lastCell = LastCell();
for ( unsigned iPass = 0; iPass < nPasses; ++iPass ) {
VisitPostorder( [&]( const wxRect &rect, TrackPanelNode &node ) {
// Draw the node
const auto newRect = node.DrawingArea(
context, rect, panelRect, iPass );
if ( newRect.Intersects( panelRect ) )
node.Draw( context, newRect, iPass );
// Draw the current handle if it is associated with the node
if ( &node == lastCell.get() ) {
auto target = Target();
if ( target ) {
const auto targetRect =
target->DrawingArea( context, rect, panelRect, iPass );
if ( targetRect.Intersects( panelRect ) )
target->Draw( context, targetRect, iPass );
}
}
} ); // nodes
} // passes
}
|