Use wxTextProofOptions itself to enable or disable the checks

Remove a separate "bool enable" argument of EnableProofCheck() and use
wxTextProofOptions::IsSpellCheckingEnabled() to decide whether the
checks should be enabled or disabled.

Also remove wxTextProofOptions ctor and provide named static factory
functions for creating the objects of this class with clearly defined
meaning.
This commit is contained in:
Vadim Zeitlin
2021-08-23 00:53:58 +02:00
parent 72ef834c70
commit 2bb3083a93
9 changed files with 91 additions and 66 deletions

View File

@@ -98,10 +98,8 @@ public:
#if wxUSE_SPELLCHECK && defined(__WXGTK3__) #if wxUSE_SPELLCHECK && defined(__WXGTK3__)
// Use native spelling and grammar checking functions. // Use native spelling and grammar checking functions.
virtual bool EnableProofCheck( virtual bool EnableProofCheck(const wxTextProofOptions& options
bool enable = true, = wxTextProofOptions::Default()) wxOVERRIDE;
const wxTextProofOptions& options = wxTextProofOptions()
) wxOVERRIDE;
virtual bool IsProofCheckEnabled() const wxOVERRIDE; virtual bool IsProofCheckEnabled() const wxOVERRIDE;
#endif // wxUSE_SPELLCHECK && __WXGTK3__ #endif // wxUSE_SPELLCHECK && __WXGTK3__

View File

@@ -114,10 +114,8 @@ public:
#if wxUSE_RICHEDIT && wxUSE_SPELLCHECK #if wxUSE_RICHEDIT && wxUSE_SPELLCHECK
// Use native spelling and grammar checking functions. // Use native spelling and grammar checking functions.
// This is only available in wxTE_RICH2 controls. // This is only available in wxTE_RICH2 controls.
virtual bool EnableProofCheck( virtual bool EnableProofCheck(const wxTextProofOptions& options
bool enable = true, = wxTextProofOptions::Default()) wxOVERRIDE;
const wxTextProofOptions& options = wxTextProofOptions()
) wxOVERRIDE;
virtual bool IsProofCheckEnabled() const wxOVERRIDE; virtual bool IsProofCheckEnabled() const wxOVERRIDE;
#endif // wxUSE_RICHEDIT && wxUSE_SPELLCHECK #endif // wxUSE_RICHEDIT && wxUSE_SPELLCHECK

View File

@@ -99,10 +99,8 @@ public:
#if wxUSE_SPELLCHECK #if wxUSE_SPELLCHECK
// Use native spelling and grammar checking functions (multiline only). // Use native spelling and grammar checking functions (multiline only).
virtual bool EnableProofCheck( virtual bool EnableProofCheck(const wxTextProofOptions& options
bool enable = true, = wxTextProofOptions::Default()) wxOVERRIDE;
const wxTextProofOptions& options = wxTextProofOptions()
) wxOVERRIDE;
virtual bool IsProofCheckEnabled() const wxOVERRIDE; virtual bool IsProofCheckEnabled() const wxOVERRIDE;
#endif // wxUSE_SPELLCHECK #endif // wxUSE_SPELLCHECK

View File

@@ -129,20 +129,29 @@ enum wxTextCtrlHitTestResult
#if wxUSE_SPELLCHECK #if wxUSE_SPELLCHECK
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxTextCtrl proof options object // This object can be passed to wxTextCtrl::EnableProofCheck() to configure the
// Passed to ::EnableProofCheck() to configure the proofing options for // proofing options for this control.
// this control
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class wxTextProofOptions class wxTextProofOptions
{ {
public: public:
wxTextProofOptions(const wxString& lang = wxEmptyString) // Return the object corresponding to the default options: current
: m_lang(lang) // language, spell checking enabled, grammar checking disabled.
static wxTextProofOptions Default()
{ {
m_EnableSpellCheck = true; wxTextProofOptions opts;
m_EnableGrammarCheck = false; 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) wxTextProofOptions& SpellCheck(bool enable = true)
{ {
m_EnableSpellCheck = enable; m_EnableSpellCheck = enable;
@@ -155,9 +164,26 @@ public:
return *this; 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; } const wxString& GetLang() const { return m_lang; }
private: private:
// Ctor is private, use static factory methods to create objects of this
// class.
wxTextProofOptions()
{
m_EnableSpellCheck =
m_EnableGrammarCheck = false;
}
wxString m_lang; wxString m_lang;
bool m_EnableSpellCheck; bool m_EnableSpellCheck;
bool m_EnableGrammarCheck; bool m_EnableGrammarCheck;
@@ -799,10 +825,8 @@ public:
#if wxUSE_SPELLCHECK #if wxUSE_SPELLCHECK
// Use native spelling and grammar checking functions. // Use native spelling and grammar checking functions.
virtual bool EnableProofCheck( virtual bool EnableProofCheck(const wxTextProofOptions& WXUNUSED(options)
bool WXUNUSED(enable) = true, = wxTextProofOptions::Default())
const wxTextProofOptions& WXUNUSED(options) = wxTextProofOptions()
)
{ {
return false; return false;
} }

View File

@@ -982,24 +982,27 @@ public:
@class wxTextProofOptions @class wxTextProofOptions
This class provides a convenient means of passing multiple parameters to 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 @code
m_pText->EnableProofCheck(true, wxTextProofOptions(wxEmptyString).SpellCheck(true)); textctrl->EnableProofCheck(wxTextProofOptions::Default().Language("fr").GrammarCheck());
@endcode @endcode
If this is the exact behaviour desired, then the parameters can be omitted. or disable the all checks entirely:
The following code will have the same result:
@code @code
m_pText->EnableProofCheck(); textctrl->EnableProofCheck(wxTextProofOptions::Disable());
@endcode @endcode
Options can also be cascaded if required as follows: Note that this class has no public constructor, except for the copy
constructor, so its objects can only be created using the static factory
@code methods Default() or Disable().
m_pText->EnableProofCheck(true, wxTextProofOptions("fr_FR").SpellCheck(false).GrammarCheck(true));
@endcode
@see wxTextCtrl::EnableProofCheck(), wxTextCtrl::IsProofCheckEnabled(). @see wxTextCtrl::EnableProofCheck(), wxTextCtrl::IsProofCheckEnabled().
@@ -1008,24 +1011,22 @@ public:
class WXDLLIMPEXP_CORE wxTextProofOptions class WXDLLIMPEXP_CORE wxTextProofOptions
{ {
/** /**
Default constructor. The proofing language is set to the current locale language, Create an object corresponding to the default checks.
spell checking is enabled and grammar checking is disabled.
@param lang The returned object enables spelling checks and disables grammar checks.
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.
*/ */
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. 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) wxTextProofOptions& SpellCheck(bool enable = true)
@@ -1035,6 +1036,12 @@ class WXDLLIMPEXP_CORE wxTextProofOptions
This option is not currently supported and is ignored. This option is not currently supported and is ignored.
*/ */
wxTextProofOptions& GrammarCheck(bool enable = true) 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 that the text control has the wxTE_RICH2 style set. wxGTK3 and wxOSX
require that the control has the wxTE_MULTILINE style. require that the control has the wxTE_MULTILINE style.
@param enable
Enables native proof checking if true, disables it otherwise.
@param options @param options
A wxTextProofOptions object specifying the desired behaviour A wxTextProofOptions object specifying the desired behaviour
of the proof checker (e.g. language to use, spell check, grammar of the proof checker (e.g. language to use, spell check, grammar
check, etc.). This parameter is currently unused and the control is check, etc.) and whether the proof checks should be enabled at all.
initialised with the default setting of current locale language / By default, spelling checks for the current language are enabled.
spell check only. Passing wxTextProofOptions::Disable() disables all the checks.
@return @return
@true if proof checking has been successfully enabled (and true was @true if proof checking has been successfully enabled or disabled,
passed as the enable parameter), @false otherwise. @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 @since 3.1.6
*/ */
virtual bool EnableProofCheck(bool enable = true, virtual bool EnableProofCheck(const wxTextProofOptions& options
const wxTextProofOptions& options = wxTextProofOptions()); = wxTextProofOptions::Default());
/** /**
Returns the style currently used for the new text. Returns the style currently used for the new text.

View File

@@ -1209,7 +1209,11 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
m_enter->SetClientData(const_cast<void*>(static_cast<const void*>(wxS("enter")))); m_enter->SetClientData(const_cast<void*>(static_cast<const void*>(wxS("enter"))));
#if wxUSE_SPELLCHECK #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, wxMessageDialog error(this,
wxT("Spell checking is not available on this platform or control style."), wxT("Spell checking is not available on this platform or control style."),

View File

@@ -1025,7 +1025,7 @@ void wxTextCtrl::GTKSetJustification()
#if wxUSE_SPELLCHECK && defined(__WXGTK3__) #if wxUSE_SPELLCHECK && defined(__WXGTK3__)
bool wxTextCtrl::EnableProofCheck(bool enable, const wxTextProofOptions& options) bool wxTextCtrl::EnableProofCheck(const wxTextProofOptions& options)
{ {
wxCHECK_MSG( IsMultiLine(), false, wxCHECK_MSG( IsMultiLine(), false,
"Unable to enable spell check on control " "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); GtkSpellChecker *spell = gtk_spell_checker_get_from_text_view(textview);
if ( enable ) if ( options.IsSpellCheckingEnabled() )
{ {
if ( !spell ) if ( !spell )
{ {

View File

@@ -834,7 +834,7 @@ WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
#if wxUSE_RICHEDIT && wxUSE_SPELLCHECK #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, wxCHECK_MSG((m_windowStyle & wxTE_RICH2), false,
"Unable to enable proof checking on a control " "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); LRESULT langOptions = ::SendMessage(GetHwnd(), EM_GETLANGOPTIONS, 0, 0);
if ( enable ) if ( options.IsSpellCheckingEnabled() )
langOptions |= IMF_SPELLCHECKING; langOptions |= IMF_SPELLCHECKING;
else else
langOptions &= ~IMF_SPELLCHECKING; langOptions &= ~IMF_SPELLCHECKING;

View File

@@ -344,12 +344,9 @@ void wxTextCtrl::Paste()
#if wxUSE_SPELLCHECK #if wxUSE_SPELLCHECK
bool wxTextCtrl::EnableProofCheck( bool wxTextCtrl::EnableProofCheck(const wxTextProofOptions& options)
bool enable,
const wxTextProofOptions& WXUNUSED(options)
)
{ {
GetTextPeer()->CheckSpelling(enable); GetTextPeer()->CheckSpelling(options.IsSpellCheckingEnabled());
return true; return true;
} }