fixed problem with calling SetFont() when (multiline) text control didn't have any value yet

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8598 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-10-20 14:11:42 +00:00
parent 69754f3253
commit 0104114590
4 changed files with 84 additions and 32 deletions

View File

@@ -28,7 +28,7 @@ class wxTextCtrl;
class wxTextCtrl: public wxTextCtrlBase class wxTextCtrl: public wxTextCtrlBase
{ {
public: public:
wxTextCtrl(); wxTextCtrl() { Init(); }
wxTextCtrl(wxWindow *parent, wxTextCtrl(wxWindow *parent,
wxWindowID id, wxWindowID id,
const wxString &value = wxEmptyString, const wxString &value = wxEmptyString,
@@ -139,17 +139,25 @@ public:
void ApplyWidgetStyle(); void ApplyWidgetStyle();
void CalculateScrollbar(); void CalculateScrollbar();
void OnInternalIdle(); void OnInternalIdle();
void UpdateFontIfNeeded();
void SetModified() { m_modified = TRUE; } void SetModified() { m_modified = TRUE; }
protected: protected:
virtual wxSize DoGetBestSize() const; virtual wxSize DoGetBestSize() const;
// common part of all ctors
void Init();
private: private:
// change the font for everything in this control
void ChangeFontGlobally();
bool m_modified; bool m_modified;
GtkWidget *m_text; GtkWidget *m_text;
GtkWidget *m_vScrollbar; GtkWidget *m_vScrollbar;
bool m_vScrollbarVisible; bool m_vScrollbarVisible;
bool m_updateFont;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
DECLARE_DYNAMIC_CLASS(wxTextCtrl); DECLARE_DYNAMIC_CLASS(wxTextCtrl);

View File

@@ -28,7 +28,7 @@ class wxTextCtrl;
class wxTextCtrl: public wxTextCtrlBase class wxTextCtrl: public wxTextCtrlBase
{ {
public: public:
wxTextCtrl(); wxTextCtrl() { Init(); }
wxTextCtrl(wxWindow *parent, wxTextCtrl(wxWindow *parent,
wxWindowID id, wxWindowID id,
const wxString &value = wxEmptyString, const wxString &value = wxEmptyString,
@@ -139,17 +139,25 @@ public:
void ApplyWidgetStyle(); void ApplyWidgetStyle();
void CalculateScrollbar(); void CalculateScrollbar();
void OnInternalIdle(); void OnInternalIdle();
void UpdateFontIfNeeded();
void SetModified() { m_modified = TRUE; } void SetModified() { m_modified = TRUE; }
protected: protected:
virtual wxSize DoGetBestSize() const; virtual wxSize DoGetBestSize() const;
// common part of all ctors
void Init();
private: private:
// change the font for everything in this control
void ChangeFontGlobally();
bool m_modified; bool m_modified;
GtkWidget *m_text; GtkWidget *m_text;
GtkWidget *m_vScrollbar; GtkWidget *m_vScrollbar;
bool m_vScrollbarVisible; bool m_vScrollbarVisible;
bool m_updateFont;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
DECLARE_DYNAMIC_CLASS(wxTextCtrl); DECLARE_DYNAMIC_CLASS(wxTextCtrl);

View File

@@ -51,6 +51,7 @@ gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
wxapp_install_idle_handler(); wxapp_install_idle_handler();
win->SetModified(); win->SetModified();
win->UpdateFontIfNeeded();
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
event.SetString( win->GetValue() ); event.SetString( win->GetValue() );
@@ -95,9 +96,10 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
END_EVENT_TABLE() END_EVENT_TABLE()
wxTextCtrl::wxTextCtrl() void wxTextCtrl::Init()
{ {
m_modified = FALSE; m_modified = FALSE;
m_updateFont = FALSE;
m_text = m_text =
m_vScrollbar = (GtkWidget *)NULL; m_vScrollbar = (GtkWidget *)NULL;
} }
@@ -111,7 +113,8 @@ wxTextCtrl::wxTextCtrl( wxWindow *parent,
const wxValidator& validator, const wxValidator& validator,
const wxString &name ) const wxString &name )
{ {
m_modified = FALSE; Init();
Create( parent, id, value, pos, size, style, validator, name ); Create( parent, id, value, pos, size, style, validator, name );
} }
@@ -321,23 +324,21 @@ void wxTextCtrl::SetValue( const wxString &value )
{ {
wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
wxString tmp = wxT("");
if (!value.IsNull()) tmp = value;
if (m_windowStyle & wxTE_MULTILINE) if (m_windowStyle & wxTE_MULTILINE)
{ {
gint len = gtk_text_get_length( GTK_TEXT(m_text) ); gint len = gtk_text_get_length( GTK_TEXT(m_text) );
gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
len = 0; len = 0;
#if wxUSE_UNICODE #if wxUSE_UNICODE
wxWX2MBbuf tmpbuf = tmp.mbc_str(); wxWX2MBbuf tmpbuf = value.mbc_str();
gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len ); gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
#else #else
gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp.mbc_str(), tmp.Length(), &len ); gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
#endif #endif
} }
else else
{ {
gtk_entry_set_text( GTK_ENTRY(m_text), tmp.mbc_str() ); gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
} }
// GRG, Jun/2000: Changed this after a lot of discussion in // GRG, Jun/2000: Changed this after a lot of discussion in
@@ -869,18 +870,35 @@ bool wxTextCtrl::SetFont( const wxFont &font )
if ( m_windowStyle & wxTE_MULTILINE ) if ( m_windowStyle & wxTE_MULTILINE )
{ {
// for compatibility with other ports: the font is a global controls m_updateFont = TRUE;
// characteristic, so change the font globally
ChangeFontGlobally();
}
return TRUE;
}
void wxTextCtrl::ChangeFontGlobally()
{
// this method is very inefficient and hence should be called as rarely as
// possible!
wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
_T("shouldn't be called for single line controls") );
wxString value = GetValue(); wxString value = GetValue();
if ( !value.IsEmpty() ) if ( !value.IsEmpty() )
{ {
Clear(); Clear();
AppendText(value); AppendText(value);
m_updateFont = FALSE;
} }
} }
return TRUE; void wxTextCtrl::UpdateFontIfNeeded()
{
if ( m_updateFont )
ChangeFontGlobally();
} }
bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) ) bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )

View File

@@ -51,6 +51,7 @@ gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
wxapp_install_idle_handler(); wxapp_install_idle_handler();
win->SetModified(); win->SetModified();
win->UpdateFontIfNeeded();
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
event.SetString( win->GetValue() ); event.SetString( win->GetValue() );
@@ -95,9 +96,10 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
END_EVENT_TABLE() END_EVENT_TABLE()
wxTextCtrl::wxTextCtrl() void wxTextCtrl::Init()
{ {
m_modified = FALSE; m_modified = FALSE;
m_updateFont = FALSE;
m_text = m_text =
m_vScrollbar = (GtkWidget *)NULL; m_vScrollbar = (GtkWidget *)NULL;
} }
@@ -111,7 +113,8 @@ wxTextCtrl::wxTextCtrl( wxWindow *parent,
const wxValidator& validator, const wxValidator& validator,
const wxString &name ) const wxString &name )
{ {
m_modified = FALSE; Init();
Create( parent, id, value, pos, size, style, validator, name ); Create( parent, id, value, pos, size, style, validator, name );
} }
@@ -321,23 +324,21 @@ void wxTextCtrl::SetValue( const wxString &value )
{ {
wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
wxString tmp = wxT("");
if (!value.IsNull()) tmp = value;
if (m_windowStyle & wxTE_MULTILINE) if (m_windowStyle & wxTE_MULTILINE)
{ {
gint len = gtk_text_get_length( GTK_TEXT(m_text) ); gint len = gtk_text_get_length( GTK_TEXT(m_text) );
gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
len = 0; len = 0;
#if wxUSE_UNICODE #if wxUSE_UNICODE
wxWX2MBbuf tmpbuf = tmp.mbc_str(); wxWX2MBbuf tmpbuf = value.mbc_str();
gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len ); gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
#else #else
gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp.mbc_str(), tmp.Length(), &len ); gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
#endif #endif
} }
else else
{ {
gtk_entry_set_text( GTK_ENTRY(m_text), tmp.mbc_str() ); gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
} }
// GRG, Jun/2000: Changed this after a lot of discussion in // GRG, Jun/2000: Changed this after a lot of discussion in
@@ -869,18 +870,35 @@ bool wxTextCtrl::SetFont( const wxFont &font )
if ( m_windowStyle & wxTE_MULTILINE ) if ( m_windowStyle & wxTE_MULTILINE )
{ {
// for compatibility with other ports: the font is a global controls m_updateFont = TRUE;
// characteristic, so change the font globally
ChangeFontGlobally();
}
return TRUE;
}
void wxTextCtrl::ChangeFontGlobally()
{
// this method is very inefficient and hence should be called as rarely as
// possible!
wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
_T("shouldn't be called for single line controls") );
wxString value = GetValue(); wxString value = GetValue();
if ( !value.IsEmpty() ) if ( !value.IsEmpty() )
{ {
Clear(); Clear();
AppendText(value); AppendText(value);
m_updateFont = FALSE;
} }
} }
return TRUE; void wxTextCtrl::UpdateFontIfNeeded()
{
if ( m_updateFont )
ChangeFontGlobally();
} }
bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) ) bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )