How To Implement an EditWordBreak Procedure in a RichEdit Control (273139)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0

This article was previously published under Q273139

SUMMARY

This article provides some sample code that illustrates how to implement an EditWordBreak procedure in RichEdit controls. An EditWordBreak procedure is used by the control to determine whether a given character is to be considered a breakable character, and to find the beginning or end of a word in different situations.

MORE INFORMATION

EditWordBreak procedures respond to a set of commands to provide very straightforward word-break information. The following is a example of how to implement an EditWordBreak procedure:

#define WB_CLASS_WHITESPACE 0x00
#define WB_CLASS_LINEBREAK 0x01
#define WB_CLASS_DELIMITER 0x02
#define WB_CLASS_NORMALCHAR 0x03


TCHAR whitespace[]=TEXT(" \t") ;
TCHAR linebreakers[]=TEXT("\r\n") ;
TCHAR delimiters[]=TEXT("[]();:,.\"'") ;

BOOL isInDelimiterList(TCHAR c)
{
  return (_tcschr((LPCTSTR)delimiters,c) != NULL || _tcschr((LPCTSTR)whitespace,c) != NULL || _tcschr((LPCTSTR)linebreakers,c) != NULL) ;
}



int CALLBACK EditWordBreakProcEx(LPTSTR pchText, int ichCurrent, int cch, int code)
{
  int lRet = 0 ;
  int i=0 ;
  BOOL found = FALSE ;
  int initial_pos = ichCurrent ;
  
  switch (code)
  {
  case WB_LEFT:
    {//Finds the beginning of a word to the left of the specified position.
      while(!isInDelimiterList(pchText[ichCurrent]) && ichCurrent>0) ichCurrent-- ;
      if(ichCurrent>0)
         lRet = ichCurrent +1 ;
      else 
         lRet = -1 ;
    }
    break ;
  case WB_RIGHT:
    {
      //Finds the beginning of a word to the right of the specified position. 
      while(!isInDelimiterList(pchText[ichCurrent]) && ichCurrent<cch) ichCurrent ;
      if(ichCurrent>0)
         lRet = ichCurrent -1 ;
      else 
         lRet = -1 ;
    }
    break ;
  case WB_ISDELIMITER:
    {//Checks whether the character at the specified position is a delimiter.
      if(_tcschr((LPCTSTR)delimiters,*pchText) != NULL)
      {
        lRet = TRUE ;
      }
    }
    break ;
  case WB_CLASSIFY:
    { //Retrieves the character class and word break flags of the character at the specified position.  
      if(_tcschr((LPCTSTR)whitespace,*pchText) != NULL)
      {
        lRet = WBF_ISWHITE | WB_CLASS_WHITESPACE ;
      }
      else if(_tcschr((LPCTSTR)delimiters,*pchText) != NULL)
      {
        lRet = WBF_BREAKAFTER | WB_CLASS_DELIMITER ;
      }
      else if(_tcschr((LPCTSTR)linebreakers,*pchText) != NULL)
      {
        lRet = WBF_BREAKLINE | WB_CLASS_DELIMITER ;
      }
      else
      {
        lRet = WB_CLASS_NORMALCHAR ;
      }
    }
    break ;
  case WB_MOVEWORDLEFT:
    // Finds the next character that begins a word before the specified position.
    {
      ichCurrent-- ;
      while(isInDelimiterList(pchText[ichCurrent])) ichCurrent-- ;      
      while(ichCurrent<cch && !found && ichCurrent>=0)
      {
         found = isInDelimiterList(pchText[ichCurrent]) ;
         if(found && ichCurrent>=0)
         {
            lRet = ichCurrent+1 ;
         }
         else
         {
            lRet = -1 ;//This ensures failure unless something is found.                           //Thus, if the supplied string, is exhausted,
                       //we will be called with the next bunch                                    //of characters.
            ichCurrent -- ;
         }
      }
      return lRet ;
    }
    break ;
  case WB_MOVEWORDRIGHT:
    //Finds the next character that begins a word after the specified position. 
    {
      if(!isInDelimiterList(pchText[ichCurrent])) 
         while(!isInDelimiterList(pchText[ichCurrent])&&ichCurrent <cch) ichCurrent++ ;
      while(isInDelimiterList(pchText[ichCurrent]&&ichCurrent <cch)) ichCurrent++ ;
      if(ichCurrent<cch)
         lRet = ichCurrent +1 ;
      else
         lRet = -1 ;
      return lRet ;

    }
    break ;
  case WB_LEFTBREAK:
    //Finds the end-of-word delimiter to the left of the specified position.
    {
      while(!isInDelimiterList(pchText[ichCurrent]) && ichCurrent>0) ichCurrent-- ;
      if(ichCurrent>0)
         lRet = ichCurrent ;
      else 
         lRet = -1 ;
    }
    break ;
  case WB_RIGHTBREAK:
    {//Finds the end-of-word delimiter to the left of the specified position.
      while(!isInDelimiterList(pchText[ichCurrent]) && ichCurrent<cch) ichCurrent ;
      if(ichCurrent>0)
         lRet = ichCurrent ;
      else 
         lRet = -1 ;
    }
    break ;
  }
  return lRet ;
}

				

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbhowto kbRichEdit KB273139 kbAudDeveloper