public void UnHighlight() { if (Text.Length > maxHighlightTextLength) { return; } mParsing = true; //Store cursor and scrollbars location Win32.LockWindowUpdate(Handle); Win32.POINT scrollPos = GetScrollPos(); int cursorLoc = SelectionStart; //Clear formatting string txt = Text; Clear(); SelectionColor = ForeColor; SelectionFont = Font; Text = txt; //Restore cursor and scrollbars location SelectionStart = cursorLoc; SetScrollPos(scrollPos); Win32.LockWindowUpdate((IntPtr)0); Invalidate(); mParsing = false; }
/// <summary> /// Sends a win32 message to get the scrollbars' position. /// </summary> /// <returns>a POINT structore containing horizontal and vertical scrollbar position.</returns> private unsafe Win32.POINT GetScrollPos() { Win32.POINT res = new Win32.POINT(); IntPtr ptr = new IntPtr(&res); Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ptr); return(res); }
/// <summary> /// Sends a win32 message to set scrollbars position. /// </summary> /// <param name="point">a POINT conatining H/Vscrollbar scrollpos.</param> private unsafe void SetScrollPos(Win32.POINT point) { IntPtr ptr = new IntPtr(&point); Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ptr); }
public UndoRedoInfo(string text, Win32.POINT scrollPos, int cursorLoc) { Text = text; ScrollPos = scrollPos; CursorLocation = cursorLoc; }
/// <summary> /// The on text changed overrided. Here we parse the text into RTF for the highlighting. /// </summary> /// <param name="e"></param> protected override void OnTextChanged(EventArgs e) { if (mParsing) { return; } mParsing = true; Win32.LockWindowUpdate(Handle); base.OnTextChanged(e); if (!mIsUndo) { mRedoStack.Clear(); mUndoList.Insert(0, mLastInfo); this.LimitUndo(); mLastInfo = new UndoRedoInfo(Text, GetScrollPos(), SelectionStart); } //Save scroll bar an cursor position, changeing the RTF moves the cursor and scrollbars to top positin Win32.POINT scrollPos = GetScrollPos(); int cursorLoc = SelectionStart; //Created with an estimate of how big the stringbuilder has to be... StringBuilder sb = new StringBuilder((int)(Text.Length * 1.5 + 150)); //Adding RTF header sb.Append(@"{\rtf1\fbidis\ansi\ansicpg1255\deff0\deflang1037{\fonttbl{"); //Font table creation int fontCounter = 0; Hashtable fonts = new Hashtable(); AddFontToTable(sb, Font, ref fontCounter, fonts); foreach (HighlightDescriptor hd in mHighlightDescriptors) { if ((hd.Font != null) && !fonts.ContainsKey(hd.Font.Name)) { AddFontToTable(sb, hd.Font, ref fontCounter, fonts); } } sb.Append("}\n"); //ColorTable sb.Append(@"{\colortbl ;"); Hashtable colors = new Hashtable(); int colorCounter = 1; AddColorToTable(sb, ForeColor, ref colorCounter, colors); AddColorToTable(sb, BackColor, ref colorCounter, colors); foreach (HighlightDescriptor hd in mHighlightDescriptors) { if (!colors.ContainsKey(hd.Color)) { AddColorToTable(sb, hd.Color, ref colorCounter, colors); } } //Parsing text sb.Append("}\n").Append(@"\viewkind4\uc1\pard\ltrpar"); SetDefaultSettings(sb, colors, fonts); char[] sperators = mSeperators.GetAsCharArray(); //Replacing "\" to "\\" for RTF... string[] lines = Text.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Split('\n'); for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) { if (lineCounter != 0) { AddNewLine(sb); } string line = lines[lineCounter]; string[] tokens = mCaseSesitive ? line.Split(sperators) : line.ToUpper().Split(sperators); if (tokens.Length == 0) { sb.Append(line); AddNewLine(sb); continue; } int tokenCounter = 0; for (int i = 0; i < line.Length;) { char curChar = line[i]; if (mSeperators.Contains(curChar)) { sb.Append(curChar); i++; } else { string curToken = tokens[tokenCounter++]; bool bAddToken = true; foreach (HighlightDescriptor hd in mHighlightDescriptors) { string compareStr = mCaseSesitive ? hd.Token : hd.Token.ToUpper(); bool match = false; //Check if the highlight descriptor matches the current toker according to the DescriptoRecognision property. switch (hd.DescriptorRecognition) { case DescriptorRecognition.WholeWord: if (curToken == compareStr) { match = true; } break; case DescriptorRecognition.StartsWith: if (curToken.StartsWith(compareStr)) { match = true; } break; case DescriptorRecognition.Contains: if (curToken.IndexOf(compareStr) != -1) { match = true; } break; } if (!match) { //If this token doesn't match chech the next one. continue; } //printing this token will be handled by the inner code, don't apply default settings... bAddToken = false; //Set colors to current descriptor settings. SetDescriptorSettings(sb, hd, colors, fonts); //Print text affected by this descriptor. switch (hd.DescriptorType) { case DescriptorType.Word: sb.Append(line.Substring(i, curToken.Length)); SetDefaultSettings(sb, colors, fonts); i += curToken.Length; break; case DescriptorType.ToEOL: sb.Append(line.Remove(0, i)); i = line.Length; SetDefaultSettings(sb, colors, fonts); break; case DescriptorType.ToCloseToken: while ((line.IndexOf(hd.CloseToken, i) == -1) && (lineCounter < lines.Length)) { sb.Append(line.Remove(0, i)); lineCounter++; if (lineCounter < lines.Length) { AddNewLine(sb); line = lines[lineCounter]; i = 0; } else { i = line.Length; } } if (line.IndexOf(hd.CloseToken, i) != -1) { sb.Append(line.Substring(i, line.IndexOf(hd.CloseToken, i) + hd.CloseToken.Length - i)); line = line.Remove(0, line.IndexOf(hd.CloseToken, i) + hd.CloseToken.Length); tokenCounter = 0; tokens = mCaseSesitive ? line.Split(sperators) : line.ToUpper().Split(sperators); SetDefaultSettings(sb, colors, fonts); i = 0; } break; } break; } if (bAddToken) { //Print text with default settings... sb.Append(line.Substring(i, curToken.Length)); i += curToken.Length; } } } } // System.Diagnostics.Debug.WriteLine(sb.ToString()); Rtf = sb.ToString(); //Restore cursor and scrollbars location. SelectionStart = cursorLoc; mParsing = false; SetScrollPos(scrollPos); Win32.LockWindowUpdate((IntPtr)0); Invalidate(); if (mAutoCompleteShown) { if (mFilterAutoComplete) { SetAutoCompleteItems(); SetAutoCompleteSize(); SetAutoCompleteLocation(false); } SetBestSelectedAutoCompleteItem(); } }
/// <summary> /// Sends a win32 message to get the scrollbars' position. /// </summary> /// <returns>a POINT structore containing horizontal and vertical scrollbar position.</returns> private unsafe Win32.POINT GetScrollPos() { Win32.POINT res = new Win32.POINT(); IntPtr ptr = new IntPtr(&res); Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ptr); return res; }
public void Highlight() { if (Text.Length > maxHighlightTextLength) { return; } mParsing = true; m_FontHeader.Length = 0; m_FontStyles.Clear(); //Store cursor and scrollbars location Win32.LockWindowUpdate(Handle); Win32.POINT scrollPos = GetScrollPos(); int cursorLoc = SelectionStart; StringBuilder sbBody = new StringBuilder(); try // nenecham to kvuli blbemu zvyraznovani spadnut, neee? { //Font table creation FontNameIndex fonts = new FontNameIndex(); //Adding RTF header - FIX m_FontHeader.Append(@"{\rtf1\fbidis\ansi\ansicpg1255\deff0\deflang1037"); Hashtable colors = AddColorTable(m_FontHeader); m_FontHeader.Append("\n{\\fonttbl"); //sb.Append(@"{\rtf1\fbidis\ansi\ansicpg1255\deff0\deflang1037{\fonttbl{"); //we do not need the counter, the Count-property of Font-index will work. //int fontCounter = 0; //AddFontToTable(sb, Font, ref fontCounter, fonts); //Add default font. AddFontToTable(m_FontHeader, Font, fonts); //this adds the defaultfont to the style definitions m_FontStyles.GetFontStyle(Font); //Tweak: Only load fonts that are used... //foreach (HighlightDescriptor hd in mHighlightDescriptors) //{ // if (hd.Font != null) // { // if (!fonts.ContainsKey(hd.Font)) // { // AddFontToTable(sb, hd.Font, ref fontCounter, fonts); // } // } //} //Do not close the header, we'll do that after all the fonts are added. // sbHeader.Append("}\n"); //Parsing text sbBody.Append(@"\viewkind4\uc1\pard\ltrpar").Append('\n'); //this is why we added the default font allready SetDefaultSettings(sbBody, colors, fonts); m_SeperatorCharArray = mSeperators.GetAsCharArray(); //Replacing "\" to "\\" for RTF... string sCurrentText = Text; string[] lines = sCurrentText.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Split('\n'); //will be used to determine the text to be formatted StringBuilder sbSubText = new StringBuilder(); #region RtfBodyGeneration #region for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) { if (lineCounter != 0) { AddNewLine(sbBody); } string line = lines[lineCounter]; string[] tokens = mCaseSensitive ? line.Split(m_SeperatorCharArray) : line.ToUpper().Split(m_SeperatorCharArray); if (tokens.Length == 0) { AddUnicode(sbBody, line); AddNewLine(sbBody); continue; } int tokenCounter = 0; #region for (int i = 0; i < line.Length; ) for (int i = 0; i < line.Length;) { char curChar = line[i]; if (mSeperators.Contains(curChar)) { sbBody.Append(curChar); i++; } else { if (tokenCounter >= tokens.Length) { break; } string curToken = tokens[tokenCounter++]; bool bAddToken = true; #region foreach (HighlightDescriptor hd in mHighlightDescriptors) foreach (HighlightDescriptor hd in mHighlightDescriptors) { string compareStr = mCaseSensitive ? hd.Token : hd.Token.ToUpper(); bool match = false; //Check if the highlight descriptor matches the current toker according to the DescriptoRecognision property. #region switch (hd.DescriptorType) switch (hd.DescriptorRecognition) { case DescriptorRecognition.RegEx: continue; case DescriptorRecognition.WholeWord: if (curToken == compareStr) { match = true; } break; case DescriptorRecognition.StartsWith: if (curToken.StartsWith(compareStr)) { match = true; } break; case DescriptorRecognition.Contains: if (curToken.IndexOf(compareStr) != -1) { match = true; } break; } if (!match) { //If this token doesn't match chech the next one. continue; } #endregion switch (hd.DescriptorType) //printing this token will be handled by the inner code, don't apply default settings... bAddToken = false; //Set colors to current descriptor settings. //Open a "block", this we will close after adding the text to the body sbBody.Append('{'); SetDescriptorSettings(sbBody, hd, colors, fonts); //Improvement for readability instead of formatting the text in the //switch, just determine the text to be formatted, and format it after the swich. //the result is just one call to SetDefaultSettings, which is encsulated in FormatText //for better font support (another improvement). string sSubText = ""; //Print text affected by this descriptor. #region switch (hd.DescriptorType) switch (hd.DescriptorType) { case DescriptorType.Word: //Will be added to the rtf after the switch sSubText = line.Substring(i, curToken.Length); i += curToken.Length; break; case DescriptorType.ToEOW: int dummy; sSubText = GetWordBounsOnCharIndex(line, i, out dummy, out i); break; case DescriptorType.ToEOL: //Will be added to the rtf after the switch sSubText = line.Remove(0, i); i = line.Length; break; case DescriptorType.ToCloseToken: { //we have multiple itterations, so clear it first: sbSubText.Length = 0; //determine endtoken, add all encountered text to subtext int closeStart = i + hd.Token.Length; while ((line.IndexOf(hd.CloseToken, closeStart) == -1) && (lineCounter < lines.Length)) { //Will be added to the rtf after the switch sbSubText.Append(line.Remove(0, i)); lineCounter++; if (lineCounter < lines.Length) { AddNewLine(sbSubText); line = lines[lineCounter]; i = closeStart = 0; } else { i = closeStart = line.Length; } } int closeIndex = line.IndexOf(hd.CloseToken, closeStart); if (closeIndex != -1) { //Will be added to the rtf after the switch sbSubText.Append(line.Substring(i, closeIndex + hd.CloseToken.Length - i)); line = line.Remove(0, closeIndex + hd.CloseToken.Length); tokenCounter = 0; tokens = mCaseSensitive ? line.Split(m_SeperatorCharArray) : line.ToUpper().Split(m_SeperatorCharArray); i = 0; } sSubText = sbSubText.ToString(); } break; } #endregion switch (hd.DescriptorType) //now that sSubText has a value (what do we want to format), format it. //Add the text to the RTF AddUnicode(sbBody, sSubText); //Close the "block" since the text we wanted to format, is now in the body. sbBody.Append('}'); //this ends all the styles that were applied in the SetDescriptorSettings, //returning all formatting to the default SetDefaultSettings(sbBody, colors, fonts); break; } #endregion foreach (HighlightDescriptor hd in mHighlightDescriptors) if (bAddToken) { //Print text with default settings... AddUnicode(sbBody, (line.Substring(i, curToken.Length))); i += curToken.Length; } } } #endregion for (int i = 0; i < line.Length; ) } #endregion for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) #endregion //we might have added fonts to the header, which means the header is not closed yet. close it: m_FontHeader.Append("\n}\n"); ApplyRTF(m_FontHeader, sbBody); PerformRegEx(sCurrentText); } #if DEBUG catch (Exception exc) { System.Diagnostics.Debug.Fail(exc.Message, exc.StackTrace); } #else catch { } #endif //Restore cursor and scrollbars location. SelectionStart = cursorLoc; SelectionLength = 0; SetScrollPos(scrollPos); Win32.LockWindowUpdate((IntPtr)0); Invalidate(); mParsing = false; }
/// <summary> /// Sends a win32 message to set scrollbars position. /// </summary> /// <param name="point">a POINT conatining H/Vscrollbar scrollpos.</param> private void SetScrollPos(Win32.POINT point) { Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ref point); }
/// <summary> /// replace the current word of the cursor with the one from the AutoComplete form and closes it. /// </summary> /// <returns>If the operation was succesful</returns> //private bool AcceptAutoCompleteItem() //{ // //if (mAutoCompleteForm.SelectedItem == null) // { // return false; // } // int curTokenStartIndex; // int curTokenEndIndex; // string curTokenString = GetSelectedWordBounds(Text, out curTokenStartIndex, out curTokenEndIndex); // ReplaceText(curTokenStartIndex, curTokenEndIndex, mAutoCompleteForm.SelectedItem); // HideAutoCompleteForm(); // return true; //} /// <summary> /// Finds the and sets the best matching token as the selected item in the AutoCompleteForm. /// </summary> //private void SetBestSelectedAutoCompleteItem() //{ // int curTokenStartIndex; // int curTokenEndIndex; // string curTokenString = GetSelectedWordBounds(Text, out curTokenStartIndex, out curTokenEndIndex); // if ((mAutoCompleteForm.SelectedItem != null) && // mAutoCompleteForm.SelectedItem.ToUpper().StartsWith(curTokenString)) // { // return; // } // int matchingChars = -1; // string bestMatchingToken = null; // foreach (string item in mAutoCompleteForm.Items) // { // bool isWholeItemMatching = true; // for (int i = 0; i < Math.Min(item.Length, curTokenString.Length); i++) // { // if (char.ToUpper(item[i]) != char.ToUpper(curTokenString[i])) // { // isWholeItemMatching = false; // if (i - 1 > matchingChars) // { // matchingChars = i; // bestMatchingToken = item; // break; // } // } // } // if (isWholeItemMatching && // (Math.Min(item.Length, curTokenString.Length) > matchingChars)) // { // matchingChars = Math.Min(item.Length, curTokenString.Length); // bestMatchingToken = item; // } // } // if (bestMatchingToken != null) // { // mAutoCompleteForm.SelectedIndex = mAutoCompleteForm.Items.IndexOf(bestMatchingToken); // } //} /// <summary> /// Sets the items for the AutoComplete form. /// </summary> //private void SetAutoCompleteItems() //{ // mAutoCompleteForm.Items.Clear(); // string filterString = ""; // if (mFilterAutoComplete) // { // int curTokenStartIndex; // int curTokenEndIndex; // filterString = GetSelectedWordBounds(Text, out curTokenStartIndex, out curTokenEndIndex); // } // foreach (HighlightDescriptor hd in mHighlightDescriptors) // { // if (hd.Token.ToUpper().StartsWith(filterString) && hd.UseForAutoComplete) // { // mAutoCompleteForm.Items.Add(hd.Token); // } // } // mAutoCompleteForm.UpdateView(); //} /// <summary> /// Sets the size. the size is limited by the MaxSize property in the form itself. /// </summary> //private void SetAutoCompleteSize() //{ // mAutoCompleteForm.Height = Math.Min( // Math.Max(mAutoCompleteForm.Items.Count, 1) * mAutoCompleteForm.ItemHeight + 4, // mAutoCompleteForm.MaximumSize.Height); //} /// <summary> /// closes the AutoCompleteForm. /// </summary> //private void HideAutoCompleteForm() //{ // mAutoCompleteForm.Visible = false; // mAutoCompleteShown = false; //} /// <summary> /// Sets the location of the AutoComplete form, maiking sure it's on the screen where the cursor is. /// </summary> /// <param name="moveHorizontly">determines wheather or not to move the form horizontly.</param> //private void SetAutoCompleteLocation(bool moveHorizontly) //{ // Point cursorLocation = GetPositionFromCharIndex(SelectionStart); // //Screen screen = Screen.FromPoint(cursorLocation); // //make it show op on the proper monitor. // Screen screen = Screen.FromPoint(PointToScreen(cursorLocation)); // FIX // Point optimalLocation = new Point(PointToScreen(cursorLocation).X - 15, (int)(PointToScreen(cursorLocation).Y + Font.Size * 2 + 2)); // Rectangle desiredPlace = new Rectangle(optimalLocation, mAutoCompleteForm.Size); // desiredPlace.Width = 152; // if (desiredPlace.Left < screen.Bounds.Left) // { // desiredPlace.X = screen.Bounds.Left; // } // if (desiredPlace.Right > screen.Bounds.Right) // { // desiredPlace.X -= (desiredPlace.Right - screen.Bounds.Right); // } // if (desiredPlace.Bottom > screen.Bounds.Bottom) // { // desiredPlace.Y = cursorLocation.Y - 2 - desiredPlace.Height; // } // if (!moveHorizontly) // { // desiredPlace.X = mAutoCompleteForm.Left; // } // mAutoCompleteForm.Bounds = desiredPlace; //} /// <summary> /// Shows the Autocomplete form. /// </summary> //public void ShowAutoComplete() //{ // SetAutoCompleteItems(); // SetAutoCompleteSize(); // SetAutoCompleteLocation(true); // mIgnoreLostFocus = true; // mAutoCompleteForm.Visible = true; // SetBestSelectedAutoCompleteItem(); // mAutoCompleteShown = true; // Focus(); // mIgnoreLostFocus = false; //} #endregion #region Scrollbar positions functions /// <summary> /// Sends a win32 message to get the scrollbars' position. /// </summary> /// <returns>a POINT structore containing horizontal and vertical scrollbar position.</returns> private Win32.POINT GetScrollPos() { Win32.POINT res = new Win32.POINT(); Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ref res); return(res); }