diff --git a/include/wx/gtk/textctrl.h b/include/wx/gtk/textctrl.h index 0274a89154..390e27987f 100644 --- a/include/wx/gtk/textctrl.h +++ b/include/wx/gtk/textctrl.h @@ -98,10 +98,8 @@ public: #if wxUSE_SPELLCHECK && defined(__WXGTK3__) // Use native spelling and grammar checking functions. - virtual bool EnableProofCheck( - bool enable = true, - const wxTextProofOptions& options = wxTextProofOptions() - ) wxOVERRIDE; + virtual bool EnableProofCheck(const wxTextProofOptions& options + = wxTextProofOptions::Default()) wxOVERRIDE; virtual bool IsProofCheckEnabled() const wxOVERRIDE; #endif // wxUSE_SPELLCHECK && __WXGTK3__ diff --git a/include/wx/msw/textctrl.h b/include/wx/msw/textctrl.h index adbdfc40c5..1383120402 100644 --- a/include/wx/msw/textctrl.h +++ b/include/wx/msw/textctrl.h @@ -114,10 +114,8 @@ public: #if wxUSE_RICHEDIT && wxUSE_SPELLCHECK // Use native spelling and grammar checking functions. // This is only available in wxTE_RICH2 controls. - virtual bool EnableProofCheck( - bool enable = true, - const wxTextProofOptions& options = wxTextProofOptions() - ) wxOVERRIDE; + virtual bool EnableProofCheck(const wxTextProofOptions& options + = wxTextProofOptions::Default()) wxOVERRIDE; virtual bool IsProofCheckEnabled() const wxOVERRIDE; #endif // wxUSE_RICHEDIT && wxUSE_SPELLCHECK diff --git a/include/wx/osx/textctrl.h b/include/wx/osx/textctrl.h index 6adba7f4bb..0bc924713d 100644 --- a/include/wx/osx/textctrl.h +++ b/include/wx/osx/textctrl.h @@ -99,10 +99,8 @@ public: #if wxUSE_SPELLCHECK // Use native spelling and grammar checking functions (multiline only). - virtual bool EnableProofCheck( - bool enable = true, - const wxTextProofOptions& options = wxTextProofOptions() - ) wxOVERRIDE; + virtual bool EnableProofCheck(const wxTextProofOptions& options + = wxTextProofOptions::Default()) wxOVERRIDE; virtual bool IsProofCheckEnabled() const wxOVERRIDE; #endif // wxUSE_SPELLCHECK diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index 84695ec409..12553337c0 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -129,20 +129,29 @@ enum wxTextCtrlHitTestResult #if wxUSE_SPELLCHECK // ---------------------------------------------------------------------------- -// wxTextCtrl proof options object -// Passed to ::EnableProofCheck() to configure the proofing options for -// this control +// This object can be passed to wxTextCtrl::EnableProofCheck() to configure the +// proofing options for this control. // ---------------------------------------------------------------------------- class wxTextProofOptions { public: - wxTextProofOptions(const wxString& lang = wxEmptyString) - : m_lang(lang) + // Return the object corresponding to the default options: current + // language, spell checking enabled, grammar checking disabled. + static wxTextProofOptions Default() { - m_EnableSpellCheck = true; - m_EnableGrammarCheck = false; + wxTextProofOptions opts; + return opts.SpellCheck(true); } + // Return the object with all checks disabled. + static wxTextProofOptions Disable() + { + return wxTextProofOptions(); + } + + // Default copy ctor, assignment operator and dtor are ok + + // Methods that can be used to set the various options. wxTextProofOptions& SpellCheck(bool enable = true) { m_EnableSpellCheck = enable; @@ -155,9 +164,26 @@ public: return *this; } + wxTextProofOptions& Language(const wxString& lang) + { + m_lang = lang; + return *this; + } + + // And the corresponding accessors. + bool IsSpellCheckingEnabled() const { return m_EnableSpellCheck; } + bool IsGrammarCheckingEnabled() const { return m_EnableGrammarCheck; } const wxString& GetLang() const { return m_lang; } private: + // Ctor is private, use static factory methods to create objects of this + // class. + wxTextProofOptions() + { + m_EnableSpellCheck = + m_EnableGrammarCheck = false; + } + wxString m_lang; bool m_EnableSpellCheck; bool m_EnableGrammarCheck; @@ -799,10 +825,8 @@ public: #if wxUSE_SPELLCHECK // Use native spelling and grammar checking functions. - virtual bool EnableProofCheck( - bool WXUNUSED(enable) = true, - const wxTextProofOptions& WXUNUSED(options) = wxTextProofOptions() - ) + virtual bool EnableProofCheck(const wxTextProofOptions& WXUNUSED(options) + = wxTextProofOptions::Default()) { return false; } diff --git a/interface/wx/textctrl.h b/interface/wx/textctrl.h index 5e6378f6c4..73122f5444 100644 --- a/interface/wx/textctrl.h +++ b/interface/wx/textctrl.h @@ -982,24 +982,27 @@ public: @class wxTextProofOptions This class provides a convenient means of passing multiple parameters to - wxTextCtrl::EnableProofCheck(). Typical (and the default) usage is: + wxTextCtrl::EnableProofCheck(). + + By default, i.e. when calling EnableProofCheck() without any parameters, + Default() proof options are used, which enable spelling (but not grammar) + checks for the current language. + + However it is also possible to customize the options: @code - m_pText->EnableProofCheck(true, wxTextProofOptions(wxEmptyString).SpellCheck(true)); + textctrl->EnableProofCheck(wxTextProofOptions::Default().Language("fr").GrammarCheck()); @endcode - If this is the exact behaviour desired, then the parameters can be omitted. - The following code will have the same result: + or disable the all checks entirely: @code - m_pText->EnableProofCheck(); + textctrl->EnableProofCheck(wxTextProofOptions::Disable()); @endcode - Options can also be cascaded if required as follows: - - @code - m_pText->EnableProofCheck(true, wxTextProofOptions("fr_FR").SpellCheck(false).GrammarCheck(true)); - @endcode + Note that this class has no public constructor, except for the copy + constructor, so its objects can only be created using the static factory + methods Default() or Disable(). @see wxTextCtrl::EnableProofCheck(), wxTextCtrl::IsProofCheckEnabled(). @@ -1008,24 +1011,22 @@ public: class WXDLLIMPEXP_CORE wxTextProofOptions { /** - Default constructor. The proofing language is set to the current locale language, - spell checking is enabled and grammar checking is disabled. + Create an object corresponding to the default checks. - @param lang - The ISO 639 / ISO 3166 canonical language name of the dictionary - to be used. See wxLocale::GetCanonicalName() for examples. If an - empty string is passed (default), then the current locale will be - used. This parameter is ignored and only the current locale - language (default option) is supported at this time. + The returned object enables spelling checks and disables grammar checks. */ - wxTextProofOptions(const wxString& lang = wxEmptyString) + static wxTextProofOptions Default() + + /** + Create an object disabling all checks. + + The returned object can be passed to wxTextCtrl::EnableProofCheck() to + disable all checks in the text control. + */ + static wxTextProofOptions Disable() /** Enable / disable spell checking for this control. - - This option is currently hard coded to @true and is ignored. - Use the global enable parameter passed to - wxTextCtrl::EnableProofCheck() to enable / disable spell checking. */ wxTextProofOptions& SpellCheck(bool enable = true) @@ -1035,6 +1036,12 @@ class WXDLLIMPEXP_CORE wxTextProofOptions This option is not currently supported and is ignored. */ wxTextProofOptions& GrammarCheck(bool enable = true) + + /// Return true if spell checking is enabled. + bool IsSpellCheckingEnabled() const; + + /// Return true if grammar checking is enabled. + bool IsGrammarCheckingEnabled() const; }; /** @@ -1402,24 +1409,23 @@ public: that the text control has the wxTE_RICH2 style set. wxGTK3 and wxOSX require that the control has the wxTE_MULTILINE style. - @param enable - Enables native proof checking if true, disables it otherwise. - @param options A wxTextProofOptions object specifying the desired behaviour of the proof checker (e.g. language to use, spell check, grammar - check, etc.). This parameter is currently unused and the control is - initialised with the default setting of current locale language / - spell check only. + check, etc.) and whether the proof checks should be enabled at all. + By default, spelling checks for the current language are enabled. + Passing wxTextProofOptions::Disable() disables all the checks. @return - @true if proof checking has been successfully enabled (and true was - passed as the enable parameter), @false otherwise. + @true if proof checking has been successfully enabled or disabled, + @false otherwise (usually because the corresponding functionality + is not available under the current platform or for this type of + text control). @since 3.1.6 */ - virtual bool EnableProofCheck(bool enable = true, - const wxTextProofOptions& options = wxTextProofOptions()); + virtual bool EnableProofCheck(const wxTextProofOptions& options + = wxTextProofOptions::Default()); /** Returns the style currently used for the new text. diff --git a/samples/text/text.cpp b/samples/text/text.cpp index 120877f2ae..4a9fd1d33a 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -1209,7 +1209,11 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) m_enter->SetClientData(const_cast(static_cast(wxS("enter")))); #if wxUSE_SPELLCHECK - if ( !m_enter->EnableProofCheck(true, wxTextProofOptions("en_US").SpellCheck()) ) + // Enable grammar check just for demonstration purposes (note that it's + // only supported under Mac, but spell checking will be enabled under the + // other platforms too, if supported). If we didn't want to enable it, we + // could omit the EnableProofCheck() argument entirely. + if ( !m_enter->EnableProofCheck(wxTextProofOptions::Default().GrammarCheck()) ) { wxMessageDialog error(this, wxT("Spell checking is not available on this platform or control style."), diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index 7f7adab532..3c5f5e8427 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -1025,7 +1025,7 @@ void wxTextCtrl::GTKSetJustification() #if wxUSE_SPELLCHECK && defined(__WXGTK3__) -bool wxTextCtrl::EnableProofCheck(bool enable, const wxTextProofOptions& options) +bool wxTextCtrl::EnableProofCheck(const wxTextProofOptions& options) { wxCHECK_MSG( IsMultiLine(), false, "Unable to enable spell check on control " @@ -1036,7 +1036,7 @@ bool wxTextCtrl::EnableProofCheck(bool enable, const wxTextProofOptions& options GtkSpellChecker *spell = gtk_spell_checker_get_from_text_view(textview); - if ( enable ) + if ( options.IsSpellCheckingEnabled() ) { if ( !spell ) { diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 47f4bc553c..d2a9082a9d 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -834,7 +834,7 @@ WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const #if wxUSE_RICHEDIT && wxUSE_SPELLCHECK -bool wxTextCtrl::EnableProofCheck(bool enable, const wxTextProofOptions& WXUNUSED(options)) +bool wxTextCtrl::EnableProofCheck(const wxTextProofOptions& options) { wxCHECK_MSG((m_windowStyle & wxTE_RICH2), false, "Unable to enable proof checking on a control " @@ -846,7 +846,7 @@ bool wxTextCtrl::EnableProofCheck(bool enable, const wxTextProofOptions& WXUNUSE LRESULT langOptions = ::SendMessage(GetHwnd(), EM_GETLANGOPTIONS, 0, 0); - if ( enable ) + if ( options.IsSpellCheckingEnabled() ) langOptions |= IMF_SPELLCHECKING; else langOptions &= ~IMF_SPELLCHECKING; diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index ba21007e46..1af21da2ca 100644 --- a/src/osx/textctrl_osx.cpp +++ b/src/osx/textctrl_osx.cpp @@ -344,12 +344,9 @@ void wxTextCtrl::Paste() #if wxUSE_SPELLCHECK -bool wxTextCtrl::EnableProofCheck( - bool enable, - const wxTextProofOptions& WXUNUSED(options) - ) +bool wxTextCtrl::EnableProofCheck(const wxTextProofOptions& options) { - GetTextPeer()->CheckSpelling(enable); + GetTextPeer()->CheckSpelling(options.IsSpellCheckingEnabled()); return true; }