1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-20 14:47:49 +02:00

Don't need to delay certain state changes of LabelTrack until drawing

This commit is contained in:
Paul Licameli 2016-06-23 15:12:46 -04:00
parent 7a7379c6fc
commit 06fd481815
3 changed files with 29 additions and 46 deletions

View File

@ -276,13 +276,11 @@ void LabelTrack::WarpLabels(const TimeWarper &warper) {
void LabelTrack::ResetFlags() void LabelTrack::ResetFlags()
{ {
mMouseXPos = -1;
mXPos1 = -1; mXPos1 = -1;
mXPos2 = -1; mXPos2 = -1;
mDragXPos = -1; mDragXPos = -1;
mInitialCursorPos = 1; mInitialCursorPos = 1;
mCurrentCursorPos = 1; mCurrentCursorPos = 1;
mResetCursorPos = false;
mRightDragging = false; mRightDragging = false;
mDrawCursor = false; mDrawCursor = false;
} }
@ -826,12 +824,6 @@ void LabelTrack::Draw(wxDC & dc, const wxRect & r,
{ {
// find the left X pos of highlighted area // find the left X pos of highlighted area
mLabels[mSelIndex]->getXPos(dc, &mXPos1, mInitialCursorPos); mLabels[mSelIndex]->getXPos(dc, &mXPos1, mInitialCursorPos);
// for preventing dragging glygh from changing current cursor position
if (mResetCursorPos) {
// set end dragging position to current cursor position
SetCurrentCursorPosition(mDragXPos);
mResetCursorPos = false;
}
// find the right X pos of highlighted area // find the right X pos of highlighted area
mLabels[mSelIndex]->getXPos(dc, &mXPos2, mCurrentCursorPos); mLabels[mSelIndex]->getXPos(dc, &mXPos2, mCurrentCursorPos);
mLabels[mSelIndex]->DrawHighlight(dc, mXPos1, mXPos2, mFontHeight); mLabels[mSelIndex]->DrawHighlight(dc, mXPos1, mXPos2, mFontHeight);
@ -851,19 +843,6 @@ void LabelTrack::Draw(wxDC & dc, const wxRect & r,
i = mSelIndex; i = mSelIndex;
int xPos = mLabels[i]->xText; int xPos = mLabels[i]->xText;
// if mouse is clicked in text box
if (mMouseXPos != -1)
{
// set current cursor position
SetCurrentCursorPosition((int) mMouseXPos);
// for preventing from resetting by shift+mouse left button
// set initialCursorPos equal to currentCursorPos
if (mLabels[mSelIndex]->changeInitialMouseXPos)
mInitialCursorPos = mCurrentCursorPos;
mDrawCursor = true;
mMouseXPos = -1;
}
if( mCurrentCursorPos > 0) if( mCurrentCursorPos > 0)
{ {
// Calculate the width of the substring and add it to Xpos // Calculate the width of the substring and add it to Xpos
@ -888,7 +867,7 @@ void LabelTrack::Draw(wxDC & dc, const wxRect & r,
/// Set the cursor position according to x position of mouse /// Set the cursor position according to x position of mouse
/// uses GetTextExtent to find the character position /// uses GetTextExtent to find the character position
/// corresponding to the x pixel position. /// corresponding to the x pixel position.
void LabelTrack::SetCurrentCursorPosition(int xPos) const void LabelTrack::SetCurrentCursorPosition(int xPos)
{ {
wxMemoryDC dc; wxMemoryDC dc;
if(msFont.Ok()) if(msFont.Ok())
@ -1489,7 +1468,10 @@ void LabelTrack::HandleTextDragRelease(const wxMouseEvent & evt)
// end dragging x position in pixels // end dragging x position in pixels
// set flag to update current cursor position // set flag to update current cursor position
mDragXPos = evt.m_x; mDragXPos = evt.m_x;
mResetCursorPos = true;
// for preventing dragging glygh from changing current cursor position
// set end dragging position to current cursor position
SetCurrentCursorPosition(mDragXPos);
// if it's an invalid dragging, disable displaying // if it's an invalid dragging, disable displaying
if (mRightDragging) { if (mRightDragging) {
@ -1521,9 +1503,6 @@ void LabelTrack::HandleClick(const wxMouseEvent & evt,
mIsAdjustingLabel = evt.Button(wxMOUSE_BTN_LEFT) && mIsAdjustingLabel = evt.Button(wxMOUSE_BTN_LEFT) &&
iGlyph != 0; iGlyph != 0;
// reset mouseXPos if the mouse is pressed in the text box
mMouseXPos = -1;
if (mIsAdjustingLabel) if (mIsAdjustingLabel)
{ {
float t = 0.0; float t = 0.0;
@ -1572,8 +1551,13 @@ void LabelTrack::HandleClick(const wxMouseEvent & evt,
mSelIndex = OverATextBox(evt.m_x, evt.m_y); mSelIndex = OverATextBox(evt.m_x, evt.m_y);
if (mSelIndex != -1) { if (mSelIndex != -1) {
*newSel = mLabels[mSelIndex]->selectedRegion; *newSel = mLabels[mSelIndex]->selectedRegion;
// set mouseXPos to set current cursor position SetCurrentCursorPosition(evt.m_x);
mMouseXPos = evt.m_x;
// for preventing from resetting by shift+mouse left button
// set initialCursorPos equal to currentCursorPos
if (mLabels[mSelIndex]->changeInitialMouseXPos)
mInitialCursorPos = mCurrentCursorPos;
mDrawCursor = true;
} }
// reset the highlight indicator // reset the highlight indicator
@ -2031,6 +2015,7 @@ void LabelTrack::ShowContextMenu()
menu.Enable(OnDeleteSelectedLabelID, true); menu.Enable(OnDeleteSelectedLabelID, true);
menu.Enable(OnEditSelectedLabelID, true); menu.Enable(OnEditSelectedLabelID, true);
wxASSERT(mSelIndex >= 0);
const LabelStruct *ls = GetLabel(mSelIndex); const LabelStruct *ls = GetLabel(mSelIndex);
wxClientDC dc(parent); wxClientDC dc(parent);
@ -2040,16 +2025,9 @@ void LabelTrack::ShowContextMenu()
dc.SetFont(msFont); dc.SetFont(msFont);
} }
int x; int x = 0;
if (mMouseXPos != -1) bool success = CalcCursorX(&x);
{ wxASSERT(success);
x = mMouseXPos;
}
else
{
dc.GetTextExtent(ls->title.Left(mCurrentCursorPos), &x, NULL);
x += ls->xText;
}
parent->PopupMenu(&menu, x, ls->y + (mIconHeight / 2) - 1); parent->PopupMenu(&menu, x, ls->y + (mIconHeight / 2) - 1);
} }

View File

@ -183,7 +183,6 @@ class AUDACITY_DLL_API LabelTrack final : public Track
// methods to set flags // methods to set flags
void SetDragXPos(const int d) { mDragXPos = d; } void SetDragXPos(const int d) { mDragXPos = d; }
void SetResetCursorPos(bool resetFlag) { mResetCursorPos = resetFlag; }
void SetWrongDragging(bool rightFlag) { mRightDragging = rightFlag; } void SetWrongDragging(bool rightFlag) { mRightDragging = rightFlag; }
void HandleClick(const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo, void HandleClick(const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
@ -264,15 +263,14 @@ class AUDACITY_DLL_API LabelTrack final : public Track
static int mFontHeight; static int mFontHeight;
mutable int mXPos1; /// left X pos of highlighted area mutable int mXPos1; /// left X pos of highlighted area
mutable int mXPos2; /// right X pos of highlighted area mutable int mXPos2; /// right X pos of highlighted area
mutable int mCurrentCursorPos; /// current cursor position int mCurrentCursorPos; /// current cursor position
mutable int mInitialCursorPos; /// initial cursor position int mInitialCursorPos; /// initial cursor position
mutable double mMouseXPos; /// mouse X pos
int mDragXPos; /// end X pos of dragging int mDragXPos; /// end X pos of dragging
/// in text box /// in text box
mutable bool mResetCursorPos; /// flag to reset cursor position(used in the dragging the glygh)
bool mRightDragging; /// flag to tell if it's a valid dragging bool mRightDragging; /// flag to tell if it's a valid dragging
mutable bool mDrawCursor; /// flag to tell if drawing the cursor or not bool mDrawCursor; /// flag to tell if drawing the
/// cursor or not
int mRestoreFocus; /// Restore focus to this track int mRestoreFocus; /// Restore focus to this track
/// when done editing /// when done editing
@ -281,8 +279,11 @@ class AUDACITY_DLL_API LabelTrack final : public Track
void ComputeLayout(const wxRect & r, const ZoomInfo &zoomInfo) const; void ComputeLayout(const wxRect & r, const ZoomInfo &zoomInfo) const;
void ComputeTextPosition(const wxRect & r, int index) const; void ComputeTextPosition(const wxRect & r, int index) const;
void SetCurrentCursorPosition(int xPos) const;
public:
void SetCurrentCursorPosition(int xPos);
private:
void calculateFontHeight(wxDC & dc) const; void calculateFontHeight(wxDC & dc) const;
void RemoveSelectedText(); void RemoveSelectedText();

View File

@ -6318,7 +6318,11 @@ bool TrackPanel::HandleLabelTrackClick(LabelTrack * lTrack, wxRect &rect, wxMous
// if the mouse is clicked in text box, set flags // if the mouse is clicked in text box, set flags
if (lTrack->OverTextBox(lTrack->GetLabel(lTrack->getSelectedIndex()), event.m_x, event.m_y)) { if (lTrack->OverTextBox(lTrack->GetLabel(lTrack->getSelectedIndex()), event.m_x, event.m_y)) {
lTrack->SetDragXPos(event.m_x); lTrack->SetDragXPos(event.m_x);
lTrack->SetResetCursorPos(true);
// for preventing dragging glygh from changing current cursor position
// set end dragging position to current cursor position
lTrack->SetCurrentCursorPosition(event.m_x);
RefreshTrack(lTrack); RefreshTrack(lTrack);
return true; return true;
} }