/// <summary> /// Reload the typing area /// </summary> public new void Refresh() { BeginUpdate(); //Save the current information string currentText = this.Text; int currentCaretPos = this.SelectionStart; Win32.POINT scrollPos = GetScrollPos(); this.Rtf = ""; //Return the information this.Text = currentText; this.SelectionStart = currentCaretPos; SetScrollPos(scrollPos); //Hide the ListBox if it is showing autoCompleteListBox.Visible = false; FinishUpdate(); }
/// <summary> /// Sends a win32 message to set scrollbars position. /// </summary> /// <param name="point">a POINT contains horizaoltal and vertical position to set to the scrollbar.</param> private void SetScrollPos(Win32.POINT point) { Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ref point); }
/// <summary> /// Sends a win32 message to get the scrollbars' position. /// </summary> /// <returns>a POINT structure contains horizontal and vertical scrollbar position.</returns> private Win32.POINT GetScrollPos() { Win32.POINT scrollPos = new Win32.POINT(); Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ref scrollPos); return(scrollPos); }
public void Highlight() { parsing = true; rtfHeader.Length = 0; fontStyles.Clear(); //Save cursor and scrollbars position Win32.LockWindowUpdate(Handle); Win32.POINT scrollPosition = GetScrollPos(); int cursorPosition = SelectionStart; StringBuilder rtfBody = new StringBuilder(); //Dictionary that saves the index of font/color in font/color table. Dictionary <string, int> fonts = new Dictionary <string, int>(); Dictionary <Color, int> colors = new Dictionary <Color, int>(); //Add RTF header rtfHeader.Append(@"{\rtf1\ansi\deff0"); //Create color table, loaded from color's descriptors. colors = AddColorTable(rtfHeader); //Create font table rtfHeader.Append(@"\fonttbl "); //Add default font of the textbox. AddFontToTable(rtfHeader, Font, fonts); //Add the defaults font tags to fontStyles.GetFontStyle(Font); //Parsing text in the rtf body rtfBody.Append(@"\viewkind4\uc1\pard\ltrpar").Append("\n"); //Set default color and font for the text SetDefaultSetting(rtfBody, colors, fonts); separatorArray = separators.ToArray(); //Replace some specified symbols that has meaning in RTF file. string inputText = Text; string[] lines = inputText.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Split('\n'); StringBuilder rtfText = new StringBuilder(); //Scan every line of input text. for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) { if (lineCounter != 0) { AddNewLine(rtfBody); } string line = lines[lineCounter]; //Put every word of line to an array. string[] tokens = caseSensitive ? line.Split(separatorArray) : line.ToUpper().Split(separatorArray); int tokenCounter = 0; for (int i = 0; i < line.Length;) { char currentChar = line[i]; if (separators.Contains(currentChar)) { rtfBody.Append(currentChar); i++; } else { if (tokenCounter >= tokens.Length) { break; } string currentToken = tokens[tokenCounter]; tokenCounter++; bool isPlainTextToken = true; foreach (var item in descriptors) { string stringToCompare = caseSensitive ? item.token : item.token.ToUpper(); bool match = false; //Check if the current token matches any of descriptors according to the DescriptorRecognition property. switch (item.descriptorRecognition) { case DescriptorRecognition.WholeWord: if (stringToCompare == currentToken) { match = true; } break; case DescriptorRecognition.StartsWith: if (currentToken.StartsWith(stringToCompare)) { match = true; } break; case DescriptorRecognition.Contains: if (currentToken.Contains(stringToCompare)) { match = true; } break; case DescriptorRecognition.IsNumber: double number = 0; if (double.TryParse(currentToken, out number)) { match = true; } continue; default: break; } if (!match) { //If doesn't match, continue to check another item in descriptors. continue; } //Found the item of descriptors that matches. isPlainTextToken = false; //Open a "block" that contains the text we are going to add //and its style tags rtfBody.Append("{"); //Apply the font and color for the text. SetDescriptorSetting(rtfBody, item, colors, fonts); string textToFormat = ""; switch (item.descriptorType) { case DescriptorType.Word: textToFormat = line.Substring(i, currentToken.Length); i += currentToken.Length; break; //case DescriptorType.ToEOW: // textToFormat = line.Substring(i, currentToken.Length); // i += currentToken.Length; // break; case DescriptorType.ToEOL: textToFormat = line.Substring(i, line.Length); i = line.Length; break; case DescriptorType.ToCloseToken: { StringBuilder sbOfTextToFormat = new StringBuilder(); //int closeStart = i + item.token.Length; while ((!line.Contains(item.closeToken)) && (lineCounter < lines.Length)) { sbOfTextToFormat.Append(line.Remove(0, i)); lineCounter++; if (lineCounter < lines.Length) //Not the last line. { AddNewLine(sbOfTextToFormat); line = lines[lineCounter]; i = 0; } else { i = line.Length; } } bool hasCloseToken = line.Contains(item.closeToken); if (hasCloseToken) { int closeTokenIndex = line.IndexOf(item.closeToken); sbOfTextToFormat.Append(line.Substring(i, closeTokenIndex + item.closeToken.Length - i)); //Because we might skip some token since add it to text to format. //--> We need to generate the list of tokens in line again. line = line.Remove(0, closeTokenIndex + item.closeToken.Length); tokenCounter = 0; tokens = caseSensitive ? line.Split(separatorArray) : line.ToUpper().Split(separatorArray); i = 0; } textToFormat = sbOfTextToFormat.ToString(); } break; default: break; } AddUnicode(rtfBody, textToFormat); //Close the block contains the text formated. rtfBody.Append("}"); break; } //End of foreach(var item in descriptors). if (isPlainTextToken) { AddUnicode(rtfBody, line.Substring(i, currentToken.Length)); i += currentToken.Length; } } } //End for(int i = 0; i < line.Length;). } //End of for(int lineCounter = 0; lineCounter < lines.Length; lineCounter++) //Close rtf header. rtfHeader.Append("\n}\n"); //Join the rtf header with the rtf body. //Then show it to the text box. this.Rtf = rtfHeader.ToString() + rtfBody; //Restore cursor and scrollbars location. SelectionStart = cursorPosition; SelectionLength = 0; SetScrollPos(scrollPosition); Win32.LockWindowUpdate((IntPtr)0); Invalidate(); parsing = false; }
/// <summary> /// Create a new instance of UndoRedoInfo. /// </summary> /// <param name="text">The current text in textbox.</param> /// <param name="caretPos">the current</param> /// <param name="scrollPos"></param> public UndoRedoInfo(string text, int caretPos, Win32.POINT scrollPos) { this.caretPosition = caretPos; this.scrollPosition = scrollPos; this.text = text; }