From: Ryan Norton Date: Sat, 25 Sep 2004 05:50:36 +0000 (+0000) Subject: Fixed GetLineLength and GetLineText for MLTE text controls - Fixed bug where it could... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ae25e5cc8c7d538190079056b1778b7edc5e04ec?ds=sidebyside Fixed GetLineLength and GetLineText for MLTE text controls - Fixed bug where it couldn't get the length or text of the first line (0) and sf bug 1030042 where subsequent lines to 0 would not include the first character. Added those tests to the text sample git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29339 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/samples/text/text.cpp b/samples/text/text.cpp index 5bdce24e2a..ce57da1f3f 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -200,6 +200,27 @@ public: if ( !m_panel->m_textrich->PageUp() ) wxLogMessage(_T("Already at the top")); } + + void OnGetLine(wxCommandEvent& WXUNUSED(event)) + { + long nLine = wxGetNumberFromUser(wxT("Which line would you like to get?"), + wxT("Enter which line you would like to get"), + wxT("Get a line from the tabbed multiline text control") ); + + wxMessageBox(m_panel->m_tab->GetLineText(nLine)); + } + + void OnGetLineLength(wxCommandEvent& WXUNUSED(event)) + { + long nLine = wxGetNumberFromUser(wxT("Which line would you like to get?"), + wxT("Enter which line you would like to get"), + wxT("Get length of a line from the tabbed multiline text control") ); + + wxMessageBox(wxString::Format(wxT("Length of line %i is:%i"), + (int) nLine, + m_panel->m_tab->GetLineLength(nLine)) + ); + } #if wxUSE_LOG void OnLogClear(wxCommandEvent& event); @@ -336,6 +357,10 @@ enum TEXT_LINE_UP, TEXT_PAGE_DOWN, TEXT_PAGE_UP, + + TEXT_GET_LINE, + TEXT_GET_LINELENGTH, + TEXT_REMOVE, TEXT_REPLACE, TEXT_SELECT, @@ -411,6 +436,9 @@ bool MyApp::OnInit() menuText->Append(TEXT_LINE_UP, _T("Scroll text one line up")); menuText->Append(TEXT_PAGE_DOWN, _T("Scroll text one page down")); menuText->Append(TEXT_PAGE_UP, _T("Scroll text one page up")); + menuText->AppendSeparator(); + menuText->Append(TEXT_GET_LINE, _T("Get the text of a line of the tabbed multiline")); + menuText->Append(TEXT_GET_LINELENGTH, _T("Get the length of a line of the tabbed multiline")); menu_bar->Append(menuText, _T("Te&xt")); #if wxUSE_LOG @@ -1212,6 +1240,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(TEXT_PAGE_DOWN, MyFrame::OnScrollPageDown) EVT_MENU(TEXT_PAGE_UP, MyFrame::OnScrollPageUp) + EVT_MENU(TEXT_GET_LINE, MyFrame::OnGetLine) + EVT_MENU(TEXT_GET_LINELENGTH, MyFrame::OnGetLineLength) + EVT_MENU(TEXT_SET, MyFrame::OnSetText) EVT_IDLE(MyFrame::OnIdle) diff --git a/src/mac/carbon/textctrl.cpp b/src/mac/carbon/textctrl.cpp index 7129086a11..953ffd15c4 100644 --- a/src/mac/carbon/textctrl.cpp +++ b/src/mac/carbon/textctrl.cpp @@ -1804,41 +1804,37 @@ void wxMacMLTEControl::SetTXNData( const wxString& st , TXNOffset start , TXNOff wxString wxMacMLTEControl::GetLineText(long lineNo) const { wxString line ; - Point curpt ; - wxString content = GetStringValue() ; if ( lineNo < GetNumberOfLines() ) { - // TODO find a better implementation : while we can get the - // line metrics of a certain line, we don't get its starting - // position, so it would probably be rather a binary search - // for the start position - long xpos = 0 ; long ypos = 0 ; - int lastHeight = 0 ; - long lastpos = GetLastPosition() ; - - ItemCount n ; - for ( n = 0 ; n <= (ItemCount)lastpos ; ++n ) + + Fixed lineWidth, + lineHeight, + currentHeight = 0; + + // get the first possible position in the control + Point firstPoint; + TXNOffsetToPoint(m_txn, 0, &firstPoint); + + // Iterate through the lines until we reach the one we want, + // adding to our current y pixel point position + while (ypos < lineNo) { - TXNOffsetToPoint( m_txn, n , &curpt); - - if ( curpt.v > lastHeight ) - { - if ( ypos == lineNo ) - return line ; + TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight); + currentHeight += lineHeight; + } + + Point thePoint = { firstPoint.v + Fix2Long(currentHeight), firstPoint.h + Fix2Long(0) }; + TXNOffset theOffset; + TXNPointToOffset(m_txn, thePoint, &theOffset); - xpos = 0 ; - if ( n > 0 ) - ++ypos ; - lastHeight = curpt.v ; - } - else - { - if ( ypos == lineNo ) - line += content[n] ; - ++xpos ; - } + wxString content = GetStringValue() ; + Point currentPoint = thePoint; + while(thePoint.v == currentPoint.v && theOffset < content.length()) + { + line += content[theOffset]; + TXNOffsetToPoint(m_txn, ++theOffset, ¤tPoint); } } return line ; @@ -1846,38 +1842,41 @@ wxString wxMacMLTEControl::GetLineText(long lineNo) const int wxMacMLTEControl::GetLineLength(long lineNo) const { - Point curpt ; + int theLength = 0; + if ( lineNo < GetNumberOfLines() ) { - // TODO find a better implementation : while we can get the - // line metrics of a certain line, we don't get its starting - // position, so it would probably be rather a binary search - // for the start position - long xpos = 0 ; long ypos = 0 ; - int lastHeight = 0 ; - long lastpos = GetLastPosition() ; - - ItemCount n ; - for ( n = 0 ; n <= (ItemCount) lastpos ; ++n ) + + Fixed lineWidth, + lineHeight, + currentHeight = 0; + + // get the first possible position in the control + Point firstPoint; + TXNOffsetToPoint(m_txn, 0, &firstPoint); + + // Iterate through the lines until we reach the one we want, + // adding to our current y pixel point position + while (ypos < lineNo) { - TXNOffsetToPoint( m_txn , n , &curpt); - - if ( curpt.v > lastHeight ) - { - if ( ypos == lineNo ) - return xpos ; + TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight); + currentHeight += lineHeight; + } + + Point thePoint = { firstPoint.v + Fix2Long(currentHeight), firstPoint.h + Fix2Long(0) }; + TXNOffset theOffset; + TXNPointToOffset(m_txn, thePoint, &theOffset); - xpos = 0 ; - if ( n > 0 ) - ++ypos ; - lastHeight = curpt.v ; - } - else - ++xpos ; + wxString content = GetStringValue() ; + Point currentPoint = thePoint; + while(thePoint.v == currentPoint.v && theOffset < content.length()) + { + ++theLength; + TXNOffsetToPoint(m_txn, ++theOffset, ¤tPoint); } } - return 0 ; + return theLength ; }