From 927637fdafd754b45494fb06701aa20f8ef7ef7a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 4 Dec 2006 12:39:52 +0000 Subject: [PATCH] allow dynamically changing most of text control styles in wxGTK git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43799 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 4 + docs/latex/wx/text.tex | 8 ++ include/wx/gtk/textctrl.h | 10 +++ src/gtk/textctrl.cpp | 169 ++++++++++++++++++++++++++------------ 4 files changed, 138 insertions(+), 53 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 3ed29d5a65..9ea86c3eed 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -106,6 +106,10 @@ wxMSW: - Use system default paper size for printing instead of A4. - Fix colours when converting wxBitmap with alpha to wxImage (nusi). +wxGTK: + +- Allow dynamically changing most of text control styles + 2.7.2 ----- diff --git a/docs/latex/wx/text.tex b/docs/latex/wx/text.tex index b19864deb8..8f6cbb2e94 100644 --- a/docs/latex/wx/text.tex +++ b/docs/latex/wx/text.tex @@ -57,6 +57,14 @@ used, so that text won't be wrapped. No effect under wxGTK1.} See also \helpref{window styles overview}{windowstyles} and \helpref{wxTextCtrl::wxTextCtrl}{wxtextctrlctor}. +Note that alignment styles (\windowstyle{wxTE\_LEFT}, +\windowstyle{wxTE\_CENTRE} and \windowstyle{wxTE\_RIGHT}) can be changed +dynamically after control creation on wxMSW and wxGTK. +\windowstyle{wxTE\_READONLY}, \windowstyle{wxTE\_PASSWORD} and wrapping styles +can be dynamically changed under wxGTK but not wxMSW. The other styles can be +only set during control creation. + + \wxheading{wxTextCtrl text format} The multiline text controls always store the text as a sequence of lines diff --git a/include/wx/gtk/textctrl.h b/include/wx/gtk/textctrl.h index fd5fb376c0..1cb6cf64bd 100644 --- a/include/wx/gtk/textctrl.h +++ b/include/wx/gtk/textctrl.h @@ -115,6 +115,8 @@ public: virtual void SetSelection(long from, long to); virtual void SetEditable(bool editable); + // Overridden wxWindow methods + virtual void SetWindowStyleFlag( long style ); virtual bool Enable( bool enable = true ); // Implementation from now on @@ -190,6 +192,14 @@ protected: virtual void DoSetValue(const wxString &value, int flags = 0); + // wrappers hiding the differences between functions doing the same thing + // for GtkTextView and GtkEntry (all of them use current window style to + // set the given characteristic) + void GTKSetEditable(); + void GTKSetVisibility(); + void GTKSetWrapMode(); + void GTKSetJustification(); + private: // change the font for everything in this control void ChangeFontGlobally(); diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index 705c6868a7..9a2bc0502f 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -712,28 +712,7 @@ bool wxTextCtrl::Create( wxWindow *parent, // Insert view into scrolled window gtk_container_add( GTK_CONTAINER(m_widget), m_text ); - // translate wx wrapping style to GTK+ - GtkWrapMode wrap; - if ( HasFlag( wxTE_DONTWRAP ) ) - wrap = GTK_WRAP_NONE; - else if ( HasFlag( wxTE_CHARWRAP ) ) - wrap = GTK_WRAP_CHAR; - else if ( HasFlag( wxTE_WORDWRAP ) ) - wrap = GTK_WRAP_WORD; - else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 - { - // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 -#ifdef __WXGTK24__ - if ( !gtk_check_version(2,4,0) ) - { - wrap = GTK_WRAP_WORD_CHAR; - } - else -#endif - wrap = GTK_WRAP_WORD; - } - - gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); + GTKSetWrapMode(); GtkScrolledWindowSetBorder(m_widget, style); @@ -768,40 +747,14 @@ bool wxTextCtrl::Create( wxWindow *parent, } if (style & wxTE_PASSWORD) - { - if (!multi_line) - gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); - } + GTKSetVisibility(); if (style & wxTE_READONLY) - { - if (!multi_line) - gtk_editable_set_editable( GTK_EDITABLE(m_text), FALSE ); - else - gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE); - } + GTKSetEditable(); - if (multi_line) - { - if (style & wxTE_RIGHT) - gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT ); - else if (style & wxTE_CENTRE) - gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER ); - // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT - } - else - { -#ifdef __WXGTK24__ - // gtk_entry_set_alignment was introduced in gtk+-2.3.5 - if (!gtk_check_version(2,4,0)) - { - if (style & wxTE_RIGHT) - gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 ); - else if (style & wxTE_CENTRE) - gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 ); - } -#endif - } + // left justification (alignment) is the default anyhow + if ( style & (wxTE_RIGHT | wxTE_CENTRE) ) + GTKSetJustification(); // We want to be notified about text changes. if (multi_line) @@ -866,6 +819,116 @@ bool wxTextCtrl::Create( wxWindow *parent, return true; } +// ---------------------------------------------------------------------------- +// flags handling +// ---------------------------------------------------------------------------- + +void wxTextCtrl::GTKSetEditable() +{ + gboolean editable = !HasFlag(wxTE_READONLY); + if ( IsSingleLine() ) + gtk_editable_set_editable(GTK_EDITABLE(m_text), editable); + else + gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text), editable); +} + +void wxTextCtrl::GTKSetVisibility() +{ + // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control? + if ( IsSingleLine() ) + gtk_entry_set_visibility(GTK_ENTRY(m_text), !HasFlag(wxTE_PASSWORD)); +} + +void wxTextCtrl::GTKSetWrapMode() +{ + // no wrapping in single line controls + if ( !IsMultiLine() ) + return; + + // translate wx wrapping style to GTK+ + GtkWrapMode wrap; + if ( HasFlag( wxTE_DONTWRAP ) ) + wrap = GTK_WRAP_NONE; + else if ( HasFlag( wxTE_CHARWRAP ) ) + wrap = GTK_WRAP_CHAR; + else if ( HasFlag( wxTE_WORDWRAP ) ) + wrap = GTK_WRAP_WORD; + else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 + { + // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 +#ifdef __WXGTK24__ + if ( !gtk_check_version(2,4,0) ) + { + wrap = GTK_WRAP_WORD_CHAR; + } + else +#endif // __WXGTK24__ + wrap = GTK_WRAP_WORD; + } + + gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); +} + +void wxTextCtrl::GTKSetJustification() +{ + if ( IsMultiLine() ) + { + GtkJustification just; + if ( HasFlag(wxTE_RIGHT) ) + just = GTK_JUSTIFY_RIGHT; + else if ( HasFlag(wxTE_CENTRE) ) + just = GTK_JUSTIFY_CENTER; + else // wxTE_LEFT == 0 + just = GTK_JUSTIFY_LEFT; + + gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER ); + } + else // single line + { +#ifdef __WXGTK24__ + // gtk_entry_set_alignment was introduced in gtk+-2.3.5 + if (!gtk_check_version(2,4,0)) + { + gfloat align; + if ( HasFlag(wxTE_RIGHT) ) + align = 1.0; + else if ( HasFlag(wxTE_CENTRE) ) + align = 0.5; + else // single line + align = 0.0; + + gtk_entry_set_alignment(GTK_ENTRY(m_text), align); + } +#endif // __WXGTK24__ + } + +} + +void wxTextCtrl::SetWindowStyleFlag(long style) +{ + long styleOld = GetWindowStyleFlag(); + + wxTextCtrlBase::SetWindowStyleFlag(style); + + if ( (style & wxTE_READONLY) != (styleOld & wxTE_READONLY) ) + GTKSetEditable(); + + if ( (style & wxTE_PASSWORD) != (styleOld & wxTE_PASSWORD) ) + GTKSetVisibility(); + + static const long flagsWrap = wxTE_WORDWRAP | wxTE_CHARWRAP | wxTE_DONTWRAP; + if ( (style & flagsWrap) != (styleOld & flagsWrap) ) + GTKSetWrapMode(); + + static const long flagsAlign = wxTE_LEFT | wxTE_CENTRE | wxTE_RIGHT; + if ( (style & flagsAlign) != (styleOld & flagsAlign) ) + GTKSetJustification(); +} + +// ---------------------------------------------------------------------------- +// control value +// ---------------------------------------------------------------------------- + wxString wxTextCtrl::GetValue() const { wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") ); -- 2.45.2