Esempio n. 1
0
 /// <summary>
 /// Создать сообщение с IRC-кодами на основе форматированного текста
 /// </summary>
 /// <param name="richTextBox"></param>
 /// <returns></returns>
 protected internal static string[] GenerateCodes(RichTextBox richTextBox, ref int relativeIndex, int start = 0, int end = -1, bool split = true)
 {
     return(RtfManager.RtfToIRC(richTextBox, ref relativeIndex, start, end, split));
 }
Esempio n. 2
0
        /// <summary>
        /// Помещает в текстовый редактор размеченный текст
        /// </summary>
        /// <param name="richTextBox">Текстовый редактор</param>
        /// <param name="original">Строка с разметкой в виде кодов IRC</param>
        /// <param name="defaultColor">Цвет для текста по умолчанию</param>
        internal static void SetText(RichTextBox richTextBox, string original, Color defaultColor, Font font)
        {
            int start     = richTextBox.Text.Length;
            int selStart  = richTextBox.SelectionStart;
            int selLength = richTextBox.SelectionLength;

            int index = richTextBox.GetCharIndexFromPosition(new Point(2, 2));

            #region Text

            var text = new FormattedText(original,
                                         RtfManager.ColorIndex(defaultColor),
                                         99);

            int  vpos             = GetVScrollPos(richTextBox);
            int  vmax             = VScrollPosMax(richTextBox);
            bool restoreSelection = richTextBox.HideSelection == false;
            if (restoreSelection)
            {
                richTextBox.HideSelection = true; // VERY IMPORTANT!!!
            }
            richTextBox.AppendText(text.Text);
            richTextBox.Select(start, text.Text.Length);
            richTextBox.SelectionColor = defaultColor;
            richTextBox.SelectionFont  = font;

            #endregion

            #region Style

            foreach (var pair in text.Colors)
            {
                richTextBox.Select(start + pair.Key, text.Text.Length - pair.Key);
                richTextBox.SelectionColor = pair.Value.ForegroundColorCode == -1 || pair.Value.ForegroundColorCode >= Settings.Default.Colors.Length ? Settings.Default.Colors.DefForeColor : Settings.Default.Colors[pair.Value.ForegroundColorCode];
                if (pair.Value.BackgroundColorCode != 99 && pair.Value.BackgroundColorCode != -1)
                {
                    richTextBox.SelectionBackColor = pair.Value.BackgroundColorCode == -1 || pair.Value.BackgroundColorCode >= Settings.Default.Colors.Length ? Settings.Default.Colors.DefBackColor : Settings.Default.Colors[pair.Value.BackgroundColorCode];
                }
                else
                {
                    richTextBox.SelectionBackColor = richTextBox.BackColor;
                }
            }

            bool b = false, u = false;
            int  bi = 0, ui = 0;
            int  j = -1;

            for (int i = 0; i < text.Text.Length + 1; i++)
            {
                if (bi < text.Bold.Count && text.Bold[bi] == i ||
                    ui < text.Under.Count && text.Under[ui] == i ||
                    i == text.Text.Length)
                {
                    if (j > -1)
                    {
                        richTextBox.Select(start + j, i - j);
                        FontStyle style = b ? FontStyle.Bold : FontStyle.Regular;
                        if (u)
                        {
                            style = style | FontStyle.Underline;
                        }
                        richTextBox.SelectionFont = new Font(richTextBox.SelectionFont ?? richTextBox.Font, style);
                    }

                    j = i;

                    bool changed = false;
                    if (bi < text.Bold.Count && text.Bold[bi] == i)
                    {
                        b = !b;
                        bi++;
                        changed = true;
                    }

                    if (ui < text.Under.Count && text.Under[ui] == i)
                    {
                        u = !u;
                        ui++;
                        changed = true;
                    }

                    if (i == text.Text.Length && changed)
                    {
                        richTextBox.Select(text.Text.Length, 0);
                        FontStyle style = b ? FontStyle.Bold : FontStyle.Regular;
                        if (u)
                        {
                            style = style | FontStyle.Underline;
                        }
                        richTextBox.SelectionFont = new Font(richTextBox.SelectionFont ?? richTextBox.Font, style);
                    }
                }
            }

            //for (int i = 0; i < text.Rev.Count; i += 2)
            //{
            //    int l = text.Rev[i];
            //    int r = i + 1 < text.Rev.Count ? text.Rev[i + 1] : text.Text.Length;
            //    richTextBox.Select(start + l, r - l);
            //    richTextBox.SelectionColor = Settings.Default.Colors.DefBackColor;
            //    richTextBox.SelectionBackColor = Settings.Default.Colors.DefForeColor;
            //}

            #endregion

            try
            {
                if (vpos > vmax - richTextBox.Height * 1.05 && selLength == 0)
                {
                    richTextBox.Select(richTextBox.TextLength, 0);
                    richTextBox.ScrollToCaret();
                }
                else
                {
                    richTextBox.SelectionLength = 0;
                    SetVScrollPos(richTextBox, vpos);
                    if (selLength > 0)
                    {
                        richTextBox.Select(index, 0);
                        richTextBox.Select(selStart, selLength);
                    }
                }
            }
            catch (Exception) { }
            if (restoreSelection)
            {
                richTextBox.HideSelection = false;
            }
        }