Example #1
0
        public static String assembleOutput(String input, IndexTree tree)
        {
            if (string.IsNullOrEmpty(input) || tree == null)
            {
                return(null);
            }

            String           outStr = "";
            List <IndexTree> flat = IndexTree.flattenTree(tree);
            int idxA, idxB;
            int i = -1;
            int l = flat.Count;

            while (++i < l)
            {
                idxA = flat[i].indexA + flat[i].offsetA;
                idxB = flat[i].indexB + flat[i].offsetB;

                if (idxA > idxB)
                {
                    return(input);
                }

                outStr += input.Substring(idxA, idxB - idxA);
            }
            return(outStr);
        }
Example #2
0
        public static void applyStyleTreeToTextbox(RichTextBox tf, String input, IndexTree bbCodeTree)
        {
            if (tf == null || bbCodeTree == null || string.IsNullOrEmpty(input))
            {
                return;
            }

            tf.Text = "";

            BBCodeStyle rootStyle = new BBCodeStyle();

            rootStyle.isAbsFontSize = false;
            rootStyle.fontName      = tf.Font.Name;
            rootStyle.fontSize      = tf.Font.Size;
            rootStyle.isAbsFontSize = true;
            rootStyle.foreColor     = new BBCodeStyle.Color((uint)0xFF000000 | (uint)(tf.SelectionColor.ToArgb() & (int)0xFFFFFF), BBCodeStyle.Mode.NORMAL);
            rootStyle.backColor     = new BBCodeStyle.Color((uint)0xFF000000 | (uint)(tf.SelectionBackColor.ToArgb() & (int)0xFFFFFF), BBCodeStyle.Mode.NORMAL);
            rootStyle.isBold        = tf.Font.Bold ? StateMode.ON : StateMode.OFF;
            rootStyle.isItalic      = tf.Font.Italic ? StateMode.ON : StateMode.OFF;
            rootStyle.isStriked     = tf.Font.Strikeout ? StateMode.ON : StateMode.OFF;
            rootStyle.isUnderlined  = tf.Font.Underline ? StateMode.ON : StateMode.OFF;

            PairTag rootPair = new PairTag(new BBCodeTagMatch(true, 0, "", "", "", 0, 0, false), new VoidCloserTagMatch(input.Length));

            (rootPair.openerMatch as BBCodeTagMatch).bbCodeStyle = rootStyle;

            bbCodeTree      = IndexTree.cloneTree(bbCodeTree);
            bbCodeTree.data = rootPair;

            List <IndexTree> flatTree     = IndexTree.flattenTree(bbCodeTree);
            String           flatText     = assembleOutput(input, bbCodeTree);
            String           corrFlatText = _replaceEnclosures(flatText);

            IndexTree.normalizeTree(bbCodeTree);
            flatTree = IndexTree.flattenTree(bbCodeTree);

            tf.Text = corrFlatText /*flatText*/;

            if (flatText == input)
            {
                return;
            }

            String currText;
            String currCorrText;
            int    offsetA = 0;
            int    offsetB = 0;
            int    idxA, idxB;
            int    i = -1;
            int    l = flatTree.Count;

            while (++i < l)
            {
                idxA = flatTree[i].indexA;
                idxB = flatTree[i].indexB;

                currText     = flatText.Substring(idxA, idxB - idxA);
                currCorrText = _replaceEnclosures(currText);

                offsetB = currCorrText.Length + idxA - idxB;

                applyStyleToTextbox(getCascadedNodeStyle(flatTree[i]),
                                    tf,
                                    idxA + offsetA,
                                    idxB + offsetA + offsetB);

                offsetA += offsetB;
            }

            tf.Select(tf.Text.Length, tf.Text.Length);
        }