![]() |
General Programming »
Algorithms & Recipes »
Spell Checkers
Intermediate
License: A Public Domain dedication
Spell Checking Edit Control (Using HunSpell)By Curtis JA CEdit control with an up-to-date, internationalizable spell checker. |
VC7, VC7.1, VC8.0, WindowsVS.NET2003, VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I was inspired to write this control after looking at both Matthew Gullett's spell checking engine and Steve King's CSpellEdit control.
I liked CSpellEdit's simplicity, but the integration with the CEdit control felt a little better in CFPSSpellingEditCtrl, so I combined the two, updated the spell checking engine used to Hunspell instead of MySpell, added user dictionary code, and generalized it as much as possible.
To use this edit box, there are three steps:
To satisfy the LGPL, these directions build hunspell as a DLL, just to be safe.
You'll also need to get a dictionary from the same site (unless Mozilla Firefox or OpenOffice.org is already installed on your machine, and even then, you may wish to provide a dictionary with your program) and put the en_US.dic and en_US.aff (or the equivalent files for the language you chose) in one of the directories mentioned below. Note that Hunspell can use MySpell-compatible dictionaries.
(Steps 1 and 2 only need to be done if you choose to get updated source for Hunspell. If you choose not to, then copy the hunspell and win_api directories from the demo project into a convienent place in your solution.)
Choose either 2a or 2b at this point for the next 3 steps.
WINVER, _WIN32_WINNT, and _WIN32_IE as 0x0500, and _WIN32_WINDOWS as 0x0410, at a minimum.
If you do not do this step, HSpellEdit.h will stop with a C1189 error. (it hits a #error line.) This is to prevent other compilation errors later.
The code takes advantage of capabilities of Windows 98, Windows 2000, and/or Windows ME when they are available, but does not *require* them. However, these define needs to be set in order to use these capabilities at all.
#include "HSpellEdit.h" to the .cpp file that contains your CWinApp descendant's InitInstance (and ExitInstance) routines, and in any .h files that define dialog box classes that will use CHSpellEdit. CHSpellEdit::Initialize(this) in the InitInstance member of your CWinApp descendant after calling CWinApp::SetRegistryKey(). CHSpellEdit::Terminate() in the ExitInstance member of your CWinApp descendant. (In a dialog box application, you can call it in InitInstance after DoModal has been called, but before InitInstance exits.) DoDataExchange routine, there should be a DDX_Control call referring to your edit box ID and your CHSpellEdit variable. If there was already a CEdit variable, just change it to a CHSpellEdit variable.) /* LICENSE:
* This code is absolutely free to use and modify. The code is provided
* "as is" with no expressed or implied warranty. The author accepts no
* liability if it causes any damage to your computer, causes your pet to
* fall ill, increases baldness or makes your car start emitting strange
* noises when you start it up.
* This code has no bugs, just undocumented features!
*/
The only requirement that HSpellEdit actually places on your users is that if they run Windows 95 or Windows NT 4.0, that they also have an updated version (better than 4.71) of SHFolder.dll installed. The URL to get the US English version is in the comments at the appropriate point in the code. HSpellEdit.cpp detects the Windows version of the user and degrades accordingly. No code that requires Windows 98, Me, or 2000 is relied upon, but code that uses the capabilities of Windows 98, Me, or 2000 is used when these operating systems are available.
You'll want to create IDS_HSPELLEDIT_SHFOLDER_ERROR in your string table to define the message to use if the check for SHFolder.dll fails. If there is none, a default message will be used.
If you use the DLL, please make sure that you use the same number for any IDS_HSPELLEDIT_* string that the DLL uses. The DLL provides strings in US English, but the strings can be overridden or translated by your application. You may wish to specify a string table in another language for translated strings, and I'd appreciate the translations being sent to me.
Three preprocessor definitions affect finding the dictionary files:
The preprocessor definition HSPELLEDIT_NO_REGISTRY_CHECK turns off loading and saving the path information to the registry.
The preprocessor definition HSPELLEDIT_NO_EXTERNAL_PROGRAMS_CHECK turns off loading the path information from the registry entries for OpenOffice.org 2.3, 2.2, 2.0, or Mozilla Firefox 2.0.0.0 or greater. (They are tried in that order.)
The preprocessor definition HSPELLEDIT_NO_SELFREF_CHECK turns off checking the executable directory (and the "dic" subdirectory of the same) for the dictionary files.
Here are the order the directories are checked:
CSIDL_COMMON_APPDATA directory *CSIDL_APPDATA directory Locations marked with * are not used to find the user dictionary, which will be written to CSIDL_APPDATA if it does not exist.
If no regular dictionaries exist, no spell checking will happen, but your program will otherwise work.
The directories that will be searched in if: A) Program is in C:\Program Files\HSpellEditTest
B) Username is "Curtis", running on a US English version of Windows XP
C) OpenOffice 2.3 and Mozilla Firefox 2.0 are installed
are these:
Both a .dic and .aff file are required for your language in one of these directories to be a usable dictionary for HSpellEdit.
If your language is not available in a particular directory, HSpellEdit will check for other sublanguages of your language in descending order, (Windows specifies the order, HSpellEdit does not) then defaults to US English (en_US) before going on to the next directory.
Once a dictionary is found, its location is saved to the registry, unless HSPELLEDIT_NO_REGISTRY_CHECK was defined.
If you are writing an installer for a program using HSpellEdit, it is probably best to put your dictionaries in locations 4 and 5, rather than locations 2 or 3.
To internationalize the menus, put these constants in the string table:
IDS_HSPELLEDIT_SUGGESTIONS IDS_HSPELLEDIT_NO_SUGGESTIONS IDS_HSPELLEDIT_ADD IDS_HSPELLEDIT_IGNORE
You only need to add these constants to your string table if MFC does not already define them for your language. (They will be in MFC71xxx.dll, where xxx is a 3-letter language code, and in the MFC source at atlmfc\src\mfc\l.xxx\prompts.rc if MFC DOES define them for your language.)
ID_EDIT_CUT ID_EDIT_COPY ID_EDIT_PASTE ID_EDIT_SELECT_ALL ID_EDIT_UNDO
I tried to allow for use in "least-priviledge-required" situations, which is why CSIDL_APPDATA (which should be writable except in extremely limited circumstances) is used as one place to store the dictionaries.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 20 Nov 2007 Editor: Sean Ewington |
Copyright 2007 by Curtis J Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |