From 02f7f0d98e937d8ec2d527ac701daf783bb37f27 Mon Sep 17 00:00:00 2001
From: Vlad Voicu <vladv@rosedu.org>
Date: Thu, 8 Mar 2012 15:10:15 +0200
Subject: Gracefully degrade when hunspell is not present


diff --git a/SwifTools/HunspellChecker.cpp b/SwifTools/HunspellChecker.cpp
index 6c1b4bb..fa5917a 100644
--- a/SwifTools/HunspellChecker.cpp
+++ b/SwifTools/HunspellChecker.cpp
@@ -43,7 +43,8 @@ void HunspellChecker::checkFragment(const std::string& fragment, PositionPairLis
 		for (PositionPairList::iterator it = misspelledPositions.begin(); it != misspelledPositions.end();) {
 			if (isCorrect(fragment.substr(boost::get<0>(*it), boost::get<1>(*it) - boost::get<0>(*it)))) {
 				misspelledPositions.erase(it++);
-			} else {
+			}
+			else {
 				++it;
 			}
 		}
diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h
index d58a9ec..5519db5 100644
--- a/SwifTools/SpellCheckerFactory.h
+++ b/SwifTools/SpellCheckerFactory.h
@@ -6,6 +6,10 @@
 
 #pragma once
 
+#ifdef HAVE_HUNSPELL
+#define HAVE_SPELLCHECKER
+#endif
+
 namespace Swift {
 	class SpellChecker;
 	class SpellCheckerFactory {
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index 75bc292..513e6fd 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -27,7 +27,9 @@ QtTextEdit::QtTextEdit(SettingsProvider* settings, QWidget* parent) : QTextEdit(
 	connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged()));
 	checker_ = NULL;
 	settings_ = settings;
+#ifdef HAVE_SPELLCHECKER
 	setUpSpellChecker();
+#endif
 	handleTextChanged();
 };
 
@@ -60,9 +62,11 @@ void QtTextEdit::keyPressEvent(QKeyEvent* event) {
 	}
 	else {
 		QTextEdit::keyPressEvent(event);
-		if(settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
+#ifdef HAVE_SPELLCHECKER
+		if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
 			underlineMisspells();
 		}
+#endif
 	}
 }
 
@@ -82,12 +86,10 @@ void QtTextEdit::underlineMisspells() {
 	std::string fragment = Q2PSTRING(cursor.selectedText());
 	checker_->checkFragment(fragment, misspelledPositions_);
 	for (PositionPairList::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) {
-		if (textCursor().position() > boost::get<1>(*it)) {
-			cursor.setPosition(boost::get<0>(*it), QTextCursor::MoveAnchor);
-			cursor.setPosition(boost::get<1>(*it), QTextCursor::KeepAnchor);
-			cursor.setCharFormat(spellingErrorFormat);
-			cursor.clearSelection();
-		}
+		cursor.setPosition(boost::get<0>(*it), QTextCursor::MoveAnchor);
+		cursor.setPosition(boost::get<1>(*it), QTextCursor::KeepAnchor);
+		cursor.setCharFormat(spellingErrorFormat);
+		cursor.clearSelection();
 		cursor.setCharFormat(normalFormat);
 	}
 }
@@ -134,8 +136,8 @@ QSize QtTextEdit::sizeHint() const {
 void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) {
 	QMenu *menu = createStandardContextMenu();
 	QTextCursor cursor = cursorForPosition(event->pos());
+#ifdef HAVE_SPELLCHECKER
 	QAction *insertPoint = menu->actions().first();
-
 	QAction *settingsAction = new QAction(QApplication::translate("QtTextEdit", "Spell Checker Options", 0, QApplication::UnicodeUTF8), menu);
 	menu->insertAction(insertPoint, settingsAction);
 	menu->insertAction(insertPoint, menu->addSeparator());
@@ -149,6 +151,9 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) {
 			replaceMisspelledWord((*it)->text(), cursor.position());
 		}
 	}
+#else
+	menu->exec(event->globalPos());
+#endif
 	delete menu;
 }
 
@@ -193,6 +198,7 @@ void QtTextEdit::setUpSpellChecker()
 		std::string affixFile(dictFile);
 		boost::replace_all(affixFile, ".dic", ".aff");
 		if ((boost::filesystem::exists(dictPath + dictFile)) && (boost::filesystem::exists(dictPath + affixFile))) {
+			std::cout << dictPath + dictFile << std::endl;
 			checker_ = checkerFactory->createSpellChecker((dictPath + affixFile).c_str(), (dictPath + dictFile).c_str());
 		}
 		else {
@@ -220,8 +226,6 @@ void QtTextEdit::spellCheckerSettingsWindow() {
 }
 
 void QtTextEdit::handleModifiedSettings() {
-	delete checker_;
-	checker_ = NULL;
 	setUpSpellChecker();
 	underlineMisspells();
 }
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index 2c0b28b..3b49f4b 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -39,6 +39,8 @@ if myenv["swift_mobile"] :
 if myenv.get("HAVE_SNARL", False) :
 	myenv.UseFlags(myenv["SNARL_FLAGS"])
 	myenv.Append(CPPDEFINES = ["HAVE_SNARL"])
+if myenv.get("HAVE_HUNSPELL", True):
+	myenv.Append(CPPDEFINES = ["HAVE_HUNSPELL"])
 if env["PLATFORM"] == "win32" :
 	myenv.Append(LIBS = ["cryptui"])	
 myenv.UseFlags(myenv["PLATFORM_FLAGS"])
-- 
cgit v0.10.2-6-g49f6