Ejemplo n.º 1
0
        public static void ProcessInlines(RichTextBox textBox, InlineCollection inlines, bool findLinks)
        {
            for (int inlineIndex = 0; inlineIndex < inlines.Count; inlineIndex++)
            {
                Inline i = inlines.ElementAt(inlineIndex);
                if (i is Run)
                {
                    Run    r    = i as Run;
                    string text = r.Text;

                    if (findLinks)
                    {
                        ProcessParagraphLink(textBox, text, i);
                    }

                    string emoticonFound = string.Empty;

                    int index = FindFirstEmoticon(text, 0, out emoticonFound);
                    if (index >= 0)
                    {
                        TextPointer tp = i.ContentStart;

                        bool reposition = false;
                        while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonFound))
                        {
                            tp = tp.GetNextInsertionPosition(LogicalDirection.Forward);
                        }

                        TextPointer end = tp;
                        for (int j = 0; j < emoticonFound.Length; j++)
                        {
                            end = end.GetNextInsertionPosition(LogicalDirection.Forward);
                        }

                        TextRange tr = new TextRange(tp, end);
                        if (textBox != null)
                        {
                            reposition = textBox.CaretPosition.CompareTo(tr.End) == 0;
                        }

                        tr.Text = string.Empty;

                        Image image = new Image();

                        image.SnapsToDevicePixels = true;
                        image.Source  = LoadResource.GetEmoticon(emoticonFound.ToLower());
                        image.Width   = 19;
                        image.Height  = 19;
                        image.Stretch = Stretch.Fill;
                        image.Tag     = emoticonFound.ToLower();

                        RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);
                        RenderOptions.SetEdgeMode(image, EdgeMode.Aliased);

                        InlineUIContainer iui = new InlineUIContainer(image, tp);
                        iui.BaselineAlignment = BaselineAlignment.TextBottom;

                        if (textBox != null && reposition)
                        {
                            textBox.CaretPosition = tp.GetNextInsertionPosition(LogicalDirection.Forward);
                        }
                    }
                }
            }
        }