]> git.saurik.com Git - wxWidgets.git/commitdiff
Fixed GetLineLength and GetLineText for MLTE text controls - Fixed bug where it could...
authorRyan Norton <wxprojects@comcast.net>
Sat, 25 Sep 2004 05:50:36 +0000 (05:50 +0000)
committerRyan Norton <wxprojects@comcast.net>
Sat, 25 Sep 2004 05:50:36 +0000 (05:50 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29339 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/text/text.cpp
src/mac/carbon/textctrl.cpp

index 5bdce24e2a649e0ab8e8d3f1e39cc827a5658f6b..ce57da1f3f0dc3dca8357af0bd9a2cf224b1f8b8 100644 (file)
@@ -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)
index 7129086a115085308d4ff57d0cf4e1e4f688e1be..953ffd15c45853becfb43cae3392a80bd90e0c55 100644 (file)
@@ -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, &currentPoint);
         }
     }
     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, &currentPoint);
         }
     }
-    return 0 ;
+    return theLength ;
 }