now cache GetCharWidth() too (and not only height)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/wxUNIVERSAL@8856 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-11-28 17:19:11 +00:00
parent be18e65726
commit 8c27f612e5
2 changed files with 22 additions and 11 deletions

View File

@@ -394,8 +394,11 @@ protected:
return m_heightLine; return m_heightLine;
} }
// recalc the line height (to call when the font changes) // get the average char width
void RecalcLineHeight(); wxCoord GetAverageWidth() const { return m_widthAvg; }
// recalc the line height and char width (to call when the font changes)
void RecalcFontMetrics();
// event handlers // event handlers
void OnIdle(wxIdleEvent& event); void OnIdle(wxIdleEvent& event);
@@ -460,7 +463,10 @@ private:
// the height of one line (cached value of GetCharHeight) // the height of one line (cached value of GetCharHeight)
wxCoord m_heightLine; wxCoord m_heightLine;
// and the average char width (cached value of GetCharWidth)
wxCoord m_widthAvg;
// we have some data which depends on the kind of control (single or multi // we have some data which depends on the kind of control (single or multi
// line) // line)
union union

View File

@@ -392,7 +392,8 @@ void wxTextCtrl::Init()
m_curCol = m_curCol =
m_curRow = 0; m_curRow = 0;
m_heightLine = -1; m_heightLine =
m_widthAvg = -1;
// init wxScrollHelper // init wxScrollHelper
SetWindow(this); SetWindow(this);
@@ -463,7 +464,7 @@ bool wxTextCtrl::Create(wxWindow *parent,
_T("wxTE_PASSWORD can't be used with multiline ctrls") ); _T("wxTE_PASSWORD can't be used with multiline ctrls") );
} }
RecalcLineHeight(); RecalcFontMetrics();
SetValue(value); SetValue(value);
SetBestSize(size); SetBestSize(size);
@@ -1520,8 +1521,8 @@ void wxTextCtrl::ShowPosition(wxTextPos pos)
// (i.e. the current character) // (i.e. the current character)
// make xStart the first visible pixel (and not position) // make xStart the first visible pixel (and not position)
int wChar = GetCharWidth(); int wChar = GetAverageWidth();
xStart *= GetCharWidth(); xStart *= wChar;
if ( x < xStart ) if ( x < xStart )
{ {
@@ -1859,7 +1860,7 @@ wxSize wxTextCtrl::DoGetBestClientSize() const
wxCoord w, h; wxCoord w, h;
GetTextExtent(GetTextToShow(GetLineText(0)), &w, &h); GetTextExtent(GetTextToShow(GetLineText(0)), &w, &h);
int wChar = GetCharWidth(), int wChar = GetAverageWidth(),
hChar = GetLineHeight(); hChar = GetLineHeight();
int widthMin = wxMax(10*wChar, 100); int widthMin = wxMax(10*wChar, 100);
@@ -2180,7 +2181,10 @@ wxTextCtrlHitTestResult wxTextCtrl::HitTestLine(const wxString& line,
// we use the first character of the line instead of (average) // we use the first character of the line instead of (average)
// GetCharWidth(): it is common to have lines of dashes, for example, // GetCharWidth(): it is common to have lines of dashes, for example,
// and this should give us much better approximation in such case // and this should give us much better approximation in such case
//
// OPT: maybe using (cache) m_widthAvg would be still faster? profile!
dc.GetTextExtent(line[0], &width, NULL); dc.GetTextExtent(line[0], &width, NULL);
col = x / width; col = x / width;
if ( col < 0 ) if ( col < 0 )
{ {
@@ -2712,9 +2716,10 @@ void wxTextCtrl::UpdateMaxWidth(wxTextCoord line)
MData().m_updateScrollbarX = MData().m_widthMax != widthMaxOld; MData().m_updateScrollbarX = MData().m_widthMax != widthMaxOld;
} }
void wxTextCtrl::RecalcLineHeight() void wxTextCtrl::RecalcFontMetrics()
{ {
m_heightLine = GetCharHeight(); m_heightLine = GetCharHeight();
m_widthAvg = GetCharWidth();
} }
void wxTextCtrl::RecalcMaxWidth() void wxTextCtrl::RecalcMaxWidth()
@@ -2770,7 +2775,7 @@ void wxTextCtrl::UpdateScrollbars()
bool showScrollbarX; bool showScrollbarX;
if ( !WrapLines() ) if ( !WrapLines() )
{ {
charWidth = GetCharWidth(); charWidth = GetAverageWidth();
maxWidth = GetMaxWidth(); maxWidth = GetMaxWidth();
showScrollbarX = maxWidth > size.x; showScrollbarX = maxWidth > size.x;
} }
@@ -3326,7 +3331,7 @@ bool wxTextCtrl::SetFont(const wxFont& font)
// update geometry parameters // update geometry parameters
UpdateTextRect(); UpdateTextRect();
UpdateScrollbars(); UpdateScrollbars();
RecalcLineHeight(); RecalcFontMetrics();
RecalcMaxWidth(); RecalcMaxWidth();
Refresh(); Refresh();