From e894be201b9866e2f5dbf29050bd1aaa58d95cc4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 12 Mar 2006 12:46:58 +0000 Subject: [PATCH] GetNumberOfLines() now returns the number of physical, not logical, lines in the control in wxGTK2 as under the other platforms (patch 1438117) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38015 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/text.tex | 15 ++++++++++----- src/gtk/textctrl.cpp | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/latex/wx/text.tex b/docs/latex/wx/text.tex index a3413657ec..c95afcff68 100644 --- a/docs/latex/wx/text.tex +++ b/docs/latex/wx/text.tex @@ -731,12 +731,17 @@ Returns the number of lines in the text control buffer. \wxheading{Remarks} Note that even empty text controls have one line (where the insertion point -is), so GetNumberOfLines() never returns 0. +is), so GetNumberOfLines() never returns $0$. -For gtk\_text (multi-line) controls, the number of lines is -calculated by actually counting newline characters in the buffer. You -may wish to avoid using functions that work with line numbers if you are -working with controls that contain large amounts of text. +For wxGTK using GTK+ 1.2.x and earlier, the number of lines in a multi-line +text control is calculated by actually counting newline characters in the +buffer, i.e. this function returns the number of logical lines and doesn't +depend on whether any of them are wrapped. For all the other platforms, the +number of physical lines in the control is returned. + +Also note that you may wish to avoid using functions that work with line +numbers if you are working with controls that contain large amounts of text as +this function has $O(N)$ complexity for $N$ being the number of lines. \membersection{wxTextCtrl::GetRange}\label{wxtextctrlgetrange} diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index d4e61f72ef..5e852cc3a6 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -947,10 +947,38 @@ int wxTextCtrl::GetLineLength(long lineNo) const int wxTextCtrl::GetNumberOfLines() const { - if (m_windowStyle & wxTE_MULTILINE) - return gtk_text_buffer_get_line_count( m_buffer ); - else + if ( m_windowStyle & wxTE_MULTILINE ) + { + GtkTextIter iter; + gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, 0 ); + + // move forward by one display line until the end is reached + int lineCount = 1; + while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text), &iter) ) + { + lineCount++; + } + + // If the last character in the text buffer is a newline, + // gtk_text_view_forward_display_line() will return false without that + // line being counted. Must add one manually in that case. + GtkTextIter lastCharIter; + gtk_text_buffer_get_iter_at_offset + ( + m_buffer, + &lastCharIter, + gtk_text_buffer_get_char_count(m_buffer) - 1 + ); + gchar lastChar = gtk_text_iter_get_char( &lastCharIter ); + if ( lastChar == wxT('\n') ) + lineCount++; + + return lineCount; + } + else // single line + { return 1; + } } void wxTextCtrl::SetInsertionPoint( long pos ) -- 2.45.2