Beispiel #1
0
        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;
        }
Beispiel #2
0
 public UndoRedoInfo(string text, Win32.POINT scrollPos, int cursorLoc)
 {
     Text           = text;
     ScrollPos      = scrollPos;
     CursorLocation = cursorLoc;
 }
Beispiel #3
0
        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;
        }
Beispiel #4
0
 /// <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);
 }
Beispiel #5
0
 /// <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);
 }