[dasher: 38/217] Win32: .Fix for dasher speaking garbage after using control box delete



commit 7893329b0ed4be42b40976583efcf9d7397d6caf
Author: ipomoena <amajorek google com>
Date:   Fri Sep 18 11:15:50 2015 -0700

    Win32: .Fix for  dasher speaking garbage after using control box delete
    
    fix for https://github.com/ipomoena/dasher/issues/12
    CEdit::get_context was not checking if iOffset isn't past length of
    text. This was causing dasher to speak garbage.
    Added GetAllContextLength to interface. It looks like best way to keep
    track of new text. TODO implement on other platforms.

 Src/DasherCore/DasherInterfaceBase.cpp |   11 +++++------
 Src/DasherCore/DasherInterfaceBase.h   |    4 ++++
 Src/Win32/Dasher.cpp                   |   17 ++++++++++-------
 Src/Win32/Dasher.h                     |    1 +
 Src/Win32/Widgets/Edit.cpp             |   14 --------------
 Src/Win32/Widgets/Edit.h               |    3 ---
 6 files changed, 20 insertions(+), 30 deletions(-)
---
diff --git a/Src/DasherCore/DasherInterfaceBase.cpp b/Src/DasherCore/DasherInterfaceBase.cpp
index db578a1..4bc4e45 100644
--- a/Src/DasherCore/DasherInterfaceBase.cpp
+++ b/Src/DasherCore/DasherInterfaceBase.cpp
@@ -412,7 +412,7 @@ void CDasherInterfaceBase::CreateNCManager() {
 }
 
 CDasherInterfaceBase::TextAction::TextAction(CDasherInterfaceBase *pIntf) : m_pIntf(pIntf) {
-  m_iStartOffset= (pIntf->m_pDasherModel) ? pIntf->m_pDasherModel->GetOffset() : 0;
+  m_iStartOffset= pIntf->GetAllContextLenght();
   pIntf->m_vTextActions.insert(this);
 }
 
@@ -422,13 +422,12 @@ CDasherInterfaceBase::TextAction::~TextAction() {
 
 void CDasherInterfaceBase::TextAction::executeOnAll() {
   (*this)(strLast = m_pIntf->GetAllContext());
-  m_iStartOffset = m_pIntf->m_pDasherModel->GetOffset();
+  m_iStartOffset = m_pIntf->GetAllContextLenght();
 }
 
 void CDasherInterfaceBase::TextAction::executeOnNew() {
-  int iNewOffset(m_pIntf->m_pDasherModel->GetOffset());
-  (*this)(strLast = m_pIntf->GetContext(m_iStartOffset, iNewOffset-m_iStartOffset));
-  m_iStartOffset=iNewOffset;
+  (*this)(strLast = m_pIntf->GetContext(m_iStartOffset, m_pIntf->GetAllContextLenght() - m_iStartOffset));
+  m_iStartOffset = m_pIntf->GetAllContextLenght();
 }
 
 void CDasherInterfaceBase::TextAction::executeLast() {
@@ -436,7 +435,7 @@ void CDasherInterfaceBase::TextAction::executeLast() {
 }
 
 void CDasherInterfaceBase::TextAction::NotifyOffset(int iOffset) {
-  m_iStartOffset = min(iOffset, m_iStartOffset);
+  m_iStartOffset = min(m_pIntf->GetAllContextLenght(), m_iStartOffset);
 }
 
 
diff --git a/Src/DasherCore/DasherInterfaceBase.h b/Src/DasherCore/DasherInterfaceBase.h
index 4bbee72..9af3644 100644
--- a/Src/DasherCore/DasherInterfaceBase.h
+++ b/Src/DasherCore/DasherInterfaceBase.h
@@ -347,6 +347,10 @@ public:
   virtual void ClearAllContext();
   virtual std::string GetAllContext()=0;
 
+  /// Subclasses should return the length of whole text. In letters, not bytes.
+  virtual int GetAllContextLenght() = 0;
+
+
   /// Set a key value pair by name - designed to allow operation from
   /// the command line.  Returns 0 on success, an error string on failure.
   ///
diff --git a/Src/Win32/Dasher.cpp b/Src/Win32/Dasher.cpp
index fbcbb1b..7cf2ce4 100644
--- a/Src/Win32/Dasher.cpp
+++ b/Src/Win32/Dasher.cpp
@@ -335,14 +335,17 @@ void CDasher::CopyToClipboard(const string &strText) {
 }
 
 std::string CDasher::GetAllContext() {
-       int speechlength = m_pEdit->GetWindowTextLength();
-       LPTSTR allspeech = new TCHAR[speechlength + 1];
-       m_pEdit->GetWindowText(allspeech, speechlength + 1);
-       string res;
-       wstring_to_UTF8string(wstring(allspeech),res);
-       return res;
+  CString wideText;
+  m_pEdit->GetWindowText(wideText);
+  return WinUTF8::wstring_to_UTF8string(wideText);
 }
 
 std::string CDasher::GetContext(unsigned int iStart, unsigned int iLength) {
-  return m_pEdit->get_context(iStart, iLength);
+  CString wideText;
+  m_pEdit->GetWindowText(wideText);
+  return WinUTF8::wstring_to_UTF8string(wideText.Mid(iStart, iLength));
+}
+
+int CDasher::GetAllContextLenght(){
+  return m_pEdit->GetWindowTextLength();
 }
diff --git a/Src/Win32/Dasher.h b/Src/Win32/Dasher.h
index 6f64b23..6afd0ea 100644
--- a/Src/Win32/Dasher.h
+++ b/Src/Win32/Dasher.h
@@ -52,6 +52,7 @@ public:
 
   virtual std::string GetAllContext();
   std::string GetContext(unsigned int iStart, unsigned int iLength);
+  int GetAllContextLenght();
 
 #ifdef WIN32_SPEECH
   bool SupportsSpeech();
diff --git a/Src/Win32/Widgets/Edit.cpp b/Src/Win32/Widgets/Edit.cpp
index 4535b22..d3dde9d 100644
--- a/Src/Win32/Widgets/Edit.cpp
+++ b/Src/Win32/Widgets/Edit.cpp
@@ -353,20 +353,6 @@ void CEdit::SetInterface(Dasher::CDasherInterfaceBase *DasherInterface) {
 #endif
 }
 
-std::string CEdit::get_context(int iOffset, int iLength) {
-  TCHAR *wszContent = new TCHAR[iOffset + iLength + 1];
-  
-  SendMessage(WM_GETTEXT, (LONG) (iOffset + iLength + 1), (LONG) wszContent);
-
-  std::string strReturn;
-  wstring_to_UTF8string(wszContent + iOffset, strReturn);
-
-  delete[] wszContent;
-
-  return strReturn;
-}
-
-
 void CEdit::output(const std::string &sText) {
   wstring String;
   WinUTF8::UTF8string_to_wstring(sText, String);
diff --git a/Src/Win32/Widgets/Edit.h b/Src/Win32/Widgets/Edit.h
index 4d81127..a097e2c 100644
--- a/Src/Win32/Widgets/Edit.h
+++ b/Src/Win32/Widgets/Edit.h
@@ -107,9 +107,6 @@ class CEdit : public ATL::CWindowImpl<CEdit> {
   
   void SetInterface(Dasher::CDasherInterfaceBase * DasherInterface);
   
-  // Get context (new version)
-  std::string get_context(int iOffset, int iLength);
-
   // called when a new character falls under the crosshair
   void output(const std::string & sText);
     


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]