Spell checking wxStyledTextCtrl

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Moonslate
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat Aug 24, 2019 10:09 am

Spell checking wxStyledTextCtrl

Post by Moonslate »

I'm wanting to use a spell checker for wxStyledTextCtrl. It's needs to wait the user to end typing, end then check the text typed. It looks like DSpellCheck for Notepad++. This is how I'm trying:

Code: Select all

const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
in the char added event:

Code: Select all

m_lastTyped = (std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count() * 0.001);

	size_t curPos = m_stc->GetCurrentPos();
	
	if (m_endTyping)
	{
		m_typedMinPos = curPos;
		m_typedMaxPos = curPos;
	}

	m_endTyping = false;

	if (m_typedMinPos > curPos)
		m_typedMinPos = curPos;

	if (m_typedMaxPos < curPos)
		m_typedMaxPos = curPos;

	CallAfter(&cScriptEditor::UpdateStatusText, m_stc);

	event.Skip();
and in the on style needed:

Code: Select all

	//from here
	int line = m_stc->GetCurrentLine();
	int start = m_stc->PositionFromLine(line);
	int end = m_stc->GetLineEndPosition(line);

	m_stc->StartStyling(start);
	m_stc->SetStyling(m_stc->LineLength(line), 0); //Clear old style

	FindAndHighlightAllVars(m_stc, start, end);

	VerifyCurLineLenght(m_stc);
	//to here is just styling. The next code is for the spell	
	
	double now = (std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count() * 0.001);

	double elapsed = now - m_lastTyped;

	if (elapsed > 500 && !m_endTyping)
	{
		m_endTyping = true;	

		DoSpelling(m_stc, m_stc->WordStartPosition(m_typedMinPos, false), m_stc->WordEndPosition(m_typedMaxPos, false));
	}
	event.Skip();
The DoSpelling function:

Code: Select all

	std::string s = stc->GetTextRange(start, end).ToStdString();

	size_t npos = std::string::npos;

	m_stc->IndicatorClearRange(start, end - start);

	for (size_t first = s.find_first_of(letters, 0); first != npos; first = s.find_first_of(letters, first))
	{
		size_t last = s.find_first_not_of(letters, first);

		std::string word = s.substr(first, last != npos ? last - first : s.size() - first);

		bool isGood = m_hunspell->spell(s);

		if (!isGood)
			m_stc->IndicatorFillRange(start + first, last != npos ? last - first : s.size() - first);

		first = last;
	}
This works, but, i'm clearing the old style, so, the OnStyleNeeded is not called any more when the users stops to type. When the user delete a char, the OnStyleNeeded is called again and (elapsed > 500) returns true. I tried using another thread with a while(true) to call DoSpelling, but, sometimes, when I close the frame, the another thread still trying to spell the StyledTextCtrl, and the program crash. I tried to compile wxSpellChecker, but I got erros. I think because is too old, well, I really need to use spell checking in my application. Any idea?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Spell checking wxStyledTextCtrl

Post by ONEEYEMAN »

Hi,
Do you really need wxSTC?
Maybe wxTextCtrl or wxRTC will suffice?

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Spell checking wxStyledTextCtrl

Post by evstevemd »

I have done spellcheck with wxSTC before. I used hunspell to do the checking. For showing spell errors wxSTC have indicators (like squiggled line). With some effort you can use Dwell start event to show error message when hovered or trigger autocompshow with suggestions.

For showing it when user finishes typing you will need to debounce the text change event of your choice. wx does not support that out of the box but check this thread for ideas
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
NeoFahrenheit
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Apr 25, 2022 12:35 pm

Re: Spell checking wxStyledTextCtrl

Post by NeoFahrenheit »

evstevemd wrote: Tue Dec 03, 2019 12:10 pm I have done spellcheck with wxSTC before. I used hunspell to do the checking. For showing spell errors wxSTC have indicators (like squiggled line). With some effort you can use Dwell start event to show error message when hovered or trigger autocompshow with suggestions.

For showing it when user finishes typing you will need to debounce the text change event of your choice. wx does not support that out of the box but check this thread for ideas
Can you give me some tips in how you achieved that, please?
I've got the richtext sample from wxWidgets and strip out some functionality I don't need and it would be awesome for my application to have spell checking.

Some of the topics I found about this are really old, so I don't know where we're at today.
If that is too hard, should I be looking for another framework, like Flutter?

Thank you.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Spell checking wxStyledTextCtrl

Post by ONEEYEMAN »

Hi,
What is your wxWidgets version? Under what platform you are testing?

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Spell checking wxStyledTextCtrl

Post by evstevemd »

NeoFahrenheit wrote: Mon Aug 22, 2022 2:05 pm Can you give me some tips in how you achieved that, please?
I've got the richtext sample from wxWidgets and strip out some functionality I don't need and it would be awesome for my application to have spell checking.
Richtext is different from wxSTC. However you can use Hunspell to do checking. Then you can use wxRTC styles to apply it to the text in question.
I have used wxRTC quiet long time and I cannot quickly recall how to style. But it should not be hard!

Hunspell: https://github.com/hunspell/hunspell
API: man page --> https://github.com/hunspell/hunspell/bl ... hunspell.3

Also see: https://blog.karatos.in/a?ID=00200-d1bc ... 3604e97f19
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
NeoFahrenheit
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Apr 25, 2022 12:35 pm

Re: Spell checking wxStyledTextCtrl

Post by NeoFahrenheit »

ONEEYEMAN wrote: Mon Aug 22, 2022 2:36 pm Hi,
What is your wxWidgets version? Under what platform you are testing?

Thank you.
I'm on Windows and wxWidgets 3.2.0.
evstevemd wrote: Mon Aug 22, 2022 4:25 pm
NeoFahrenheit wrote: Mon Aug 22, 2022 2:05 pm Can you give me some tips in how you achieved that, please?
I've got the richtext sample from wxWidgets and strip out some functionality I don't need and it would be awesome for my application to have spell checking.
Richtext is different from wxSTC. However you can use Hunspell to do checking. Then you can use wxRTC styles to apply it to the text in question.
I have used wxRTC quiet long time and I cannot quickly recall how to style. But it should not be hard!

Hunspell: https://github.com/hunspell/hunspell
API: man page --> https://github.com/hunspell/hunspell/bl ... hunspell.3

Also see: https://blog.karatos.in/a?ID=00200-d1bc ... 3604e97f19
I can switch to wxSTC with no problem, if that is going to make my life easier.
Is the procedure with wxSTC the same?

Thank you for the links!
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Spell checking wxStyledTextCtrl

Post by evstevemd »

You cannot just arbitrary switch. I thought you already use wxRTC. If not sorry.
Just know that wxRTC is for richtext editing and wxSTC is for code editing.
Which one are you using?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
NeoFahrenheit
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Apr 25, 2022 12:35 pm

Re: Spell checking wxStyledTextCtrl

Post by NeoFahrenheit »

evstevemd wrote: Tue Aug 23, 2022 6:51 am You cannot just arbitrary switch. I thought you already use wxRTC. If not sorry.
Just know that wxRTC is for richtext editing and wxSTC is for code editing.
Which one are you using?
I'm using wxRichTextCtrl and yes, I need the editing capabilities of it. :(
I guess doing the red squiggle beneath the words and the context menu will be the hardest.
NeoFahrenheit
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Apr 25, 2022 12:35 pm

Re: Spell checking wxStyledTextCtrl

Post by NeoFahrenheit »

evstevemd wrote: Tue Aug 23, 2022 6:51 am You cannot just arbitrary switch. I thought you already use wxRTC. If not sorry.
Just know that wxRTC is for richtext editing and wxSTC is for code editing.
Which one are you using?
I finally managed to compile hunspell and link it to my Visual Studio 2022 project. I'm ready for next move!
Could you share your expertise in how you implemented your spell checker, please? :)
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Spell checking wxStyledTextCtrl

Post by evstevemd »

The app is complex and I did few years back but basically all you do is you get text in a range and send it to spell checker. If it not okay apply squiggles on that range. When that range is clicked, show box to select correct word and replace it in the same range. Spell checker will give you the list too.
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
NeoFahrenheit
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Apr 25, 2022 12:35 pm

Re: Spell checking wxStyledTextCtrl

Post by NeoFahrenheit »

evstevemd wrote: Fri Sep 02, 2022 10:20 am The app is complex and I did few years back but basically all you do is you get text in a range and send it to spell checker. If it not okay apply squiggles on that range. When that range is clicked, show box to select correct word and replace it in the same range. Spell checker will give you the list too.
Thanks so much for your answer.
I have some questions, if you don't mind answering (if you know) them. :)

1) As I will be saving in .xml format, how do I apply the red squiggle in wxRichTextCtrl and how do I make to not be saved into the file (only for editor)?
2) How can I edit the Right Click Context Menu to add the words the user can choose?
3) Encoding seems to be a problem. In my language, puctuation like ~ and ç are comming all messed up. Do you how can I fix it? hunspell said the enconding of dictionary is ISO 8859-1, but the VS2022 doens't offer this encoding for the file (don't know if this is related).

Thank you.
I guess with those problems solved, I can figure it out the rest myself.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Spell checking wxStyledTextCtrl

Post by evstevemd »

NeoFahrenheit wrote: Fri Sep 02, 2022 12:58 pm 1) As I will be saving in .xml format, how do I apply the red squiggle in wxRichTextCtrl and how do I make to not be saved into the file (only for editor)?
I don't remember having something like that in wxRTC. However it is long time since I played with it and so I don't know how.
As of saving, I suggest save just a file and do check while or after loading
NeoFahrenheit wrote: Fri Sep 02, 2022 12:58 pm 2) How can I edit the Right Click Context Menu to add the words the user can choose?
https://docs.wxwidgets.org/trunk/classw ... event.html

Load it from Hunspell results
NeoFahrenheit wrote: Fri Sep 02, 2022 12:58 pm 3) Encoding seems to be a problem. In my language, puctuation like ~ and ç are comming all messed up. Do you how can I fix it? hunspell said the enconding of dictionary is ISO 8859-1, but the VS2022 doens't offer this encoding for the file (don't know if this is related).
If you can open as a different thread, with details, someone more knowledgeable can pick it up!
I have never had a need for encoding other than UTF8
NeoFahrenheit wrote: Fri Sep 02, 2022 12:58 pm I guess with those problems solved, I can figure it out the rest myself.
You can always ask here!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply