src/tracks/labeltrack/ui/LabelTrackView.cpp
/// uses GetTextExtent to find the character position
/// corresponding to the x pixel position.
uses GetTextExtent to find the character position
corresponding to the x pixel position.
uses GetTextExtent to find the character position corresponding to the x pixel position .
int LabelTrackView::FindCursorPosition(int labelIndex, wxCoord xPos)
{
   int result = -1;
   wxMemoryDC dc;
   if(msFont.Ok())
      dc.SetFont(msFont);
   bool finished = false;
   int charIndex = 1;
   int partWidth;
   int oneWidth;
   double bound;
   wxString subString;
   const auto pTrack = FindLabelTrack();
   const auto &mLabels = pTrack->GetLabels();
   const auto &labelStruct = mLabels[labelIndex];
   const auto &title = labelStruct.title;
   const int length = title.length();
   while (!finished && (charIndex < length + 1))
   {
      int unichar = (int)title.at( charIndex-1 );
      if( (0xDC00 <= unichar) && (unichar <= 0xDFFF)){
         charIndex++;
         continue;
      }
      subString = title.Left(charIndex);
      dc.GetTextExtent(subString, &partWidth, NULL);
      dc.GetTextExtent(subString.Right(1), &oneWidth, NULL);
      bound = labelStruct.xText + partWidth - oneWidth * 0.5;
      if (xPos <= bound)
      {
         result = charIndex - 1;
         finished = true;
      }
      else
      {
         charIndex++;
      }
   }
   if (!finished)
      result = length;
   return result;
}
int LabelTrackView :: FindCursorPosition ( int labelIndex , wxCoord xPos ) { int result = -1 ; wxMemoryDC dc ; if ( msFont . Ok ( ) ) dc . SetFont ( msFont ) ; bool finished = false ; int charIndex = 1 ; int partWidth ; int oneWidth ; double bound ; wxString subString ; const auto pTrack = FindLabelTrack ( ) ; const auto & mLabels = pTrack -> GetLabels ( ) ; const auto & labelStruct = mLabels [ labelIndex ] ; const auto & title = labelStruct . title ; const int length = title . length ( ) ; while ( ! finished && ( charIndex < length + 1 ) ) { int unichar = ( int ) title . at ( charIndex - 1 ) ; if ( ( 0xDC00 <= unichar ) && ( unichar <= 0xDFFF ) ) { charIndex ++ ; continue ; } subString = title . Left ( charIndex ) ; dc . GetTextExtent ( subString , & partWidth , NULL ) ; dc . GetTextExtent ( subString . Right ( 1 ) , & oneWidth , NULL ) ; bound = labelStruct . xText + partWidth - oneWidth * 0.5 ; if ( xPos <= bound ) { result = charIndex - 1 ; finished = true ; } else { charIndex ++ ; } } if ( ! finished ) result = length ; return result ; }
uses GetTextExtent to find the character position
corresponding to the x pixel position.
uses GetTextExtent to find the character position corresponding to the x pixel position .
// A bool indicator to see if set the cursor position or not // Get the width of substring // Get the width of the last character // Found // Advance // Cursor should be in the last position