void OnChatReceived(object _sender, ChatReceivedEventArgs e)
        {
            App.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                const int FontSize = 14;

                var message = e.Message.Text;
                var font = e.Message.Font;
                var sender = e.Message.Sender;

                // Add this color to the brush cache if it's not there already
                var brush = App.GetBrush(font.Color);

                Paragraph paragraph;
                if (sender != lastSender)
                {
                    lastSender = sender;
                    paragraph = new Paragraph()
                    {
                        FontSize = FontSize + 2,
                        Foreground = Brushes.Black,
                        FontWeight = FontWeights.Light,
                        Margin = new Thickness(0, 4.0, 0, 2.0),
                        Padding = new Thickness(0, 0, 0, 2.0),
                        BorderBrush = Brushes.LightGray,
                        BorderThickness = new Thickness(0, 0, 0, 1),
                    };
                    paragraph.Inlines.Add(new Run(sender.DisplayName));
                    ChatHistory.Add(paragraph);
                }

                paragraph = new Paragraph()
                {
                    FontFamily = new FontFamily(font.Family),
                    FontSize = FontSize,
                    LineHeight = 3 * FontSize / 2,
                    Foreground = brush,
                    Margin = new Thickness(16.0, 0, 0, 0),
                    TextIndent = 0,
                };

            /*				var path = new System.Windows.Shapes.Path()
                {
                    Stroke = Brushes.Black,
                    StrokeThickness = 2,
                    Data = App.Current.FindResource("CrossGeometry") as Geometry,
                    Width = 12,
                    Height = paragraph.FontSize,
                    Margin = new Thickness(0, 0, 4, 0),
                };

                paragraph.Inlines.Add(new InlineUIContainer(path));*/

                var stops = new List<ChatStop>();

                foreach (var emoteStop in App.EmoticonManager.SearchForEmoticons(message))
                {
                    stops.Add(new ChatStop()
                        {
                            Index = emoteStop.Key,
                            Length = emoteStop.Value.Shortcut.Length,
                            Type = StopType.Emoticon,
                            Data = emoteStop.Value,
                        });
                }

                string[] urls = new string[] {"http://", "https://"};
                foreach (var url in urls)
                {
                    int index = message.IndexOf(url);
                    while (index != -1)
                    {
                        int endIndex = message.IndexOf(' ', index);
                        if (endIndex == -1)
                            endIndex = message.Length - 1;
                        stops.Add(new ChatStop()
                            {
                                Index = index,
                                Length = endIndex - index + 1,
                                Type = StopType.Url,
                            });

                        index = message.IndexOf(url, endIndex);
                    }
                }

                var orderedStops = stops.OrderBy(_ => _.Index);

                int startIndex = 0;
                foreach (var stop in orderedStops)
                {
                    if (stop.Index < startIndex)
                        continue;
                    if (stop.Index != startIndex)
                        paragraph.Inlines.Add(new Run(message.Substring(startIndex, stop.Index - startIndex)));

                    switch (stop.Type)
                    {
                        case StopType.Emoticon:
                            var picture = new EmoteImage()
                            {
                                SnapsToDevicePixels = true,
                                Emote = stop.Data as Emoticon,
                            };

                            paragraph.Inlines.Add(new InlineUIContainer(picture));
                            break;

                        case StopType.Url:
                            var text = message.Substring(stop.Index, stop.Length);
                            var link = new Hyperlink(new Run(text));
                            link.Tag = text;
                            link.Click += OnChatLinkClicked;
                            paragraph.Inlines.Add(link);
                            break;
                    }

                    startIndex = stop.Index + stop.Length;
                }

                paragraph.Inlines.Add(new Run(message.Substring(startIndex)));

                if (font.Style.HasFlag(Protocol.FontStyle.Bold))
                    paragraph.FontWeight = FontWeights.Bold;
                if (font.Style.HasFlag(Protocol.FontStyle.Italic))
                    paragraph.FontStyle = FontStyles.Italic;
                if (font.Style.HasFlag(Protocol.FontStyle.Underline))
                {
                    paragraph.TextDecorations = new TextDecorationCollection();
                    paragraph.TextDecorations.Add(System.Windows.TextDecorations.Underline);
                }

                ChatHistory.Add(paragraph);
            }));
        }
Beispiel #2
0
        void OnChatReceived(object _sender, ChatReceivedEventArgs e)
        {
            App.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                const int FontSize = 14;

                var message = e.Message.Text;
                var font    = e.Message.Font;
                var sender  = e.Message.Sender;

                // Add this color to the brush cache if it's not there already
                var brush = App.GetBrush(font.Color);

                Paragraph paragraph;
                if (sender != lastSender)
                {
                    lastSender = sender;
                    paragraph  = new Paragraph()
                    {
                        FontSize        = FontSize + 2,
                        Foreground      = Brushes.Black,
                        FontWeight      = FontWeights.Light,
                        Margin          = new Thickness(0, 4.0, 0, 2.0),
                        Padding         = new Thickness(0, 0, 0, 2.0),
                        BorderBrush     = Brushes.LightGray,
                        BorderThickness = new Thickness(0, 0, 0, 1),
                    };
                    paragraph.Inlines.Add(new Run(sender.DisplayName));
                    ChatHistory.Add(paragraph);
                }

                paragraph = new Paragraph()
                {
                    FontFamily = new FontFamily(font.Family),
                    FontSize   = FontSize,
                    LineHeight = 3 * FontSize / 2,
                    Foreground = brush,
                    Margin     = new Thickness(16.0, 0, 0, 0),
                    TextIndent = 0,
                };

/*				var path = new System.Windows.Shapes.Path()
 *                              {
 *                                      Stroke = Brushes.Black,
 *                                      StrokeThickness = 2,
 *                                      Data = App.Current.FindResource("CrossGeometry") as Geometry,
 *                                      Width = 12,
 *                                      Height = paragraph.FontSize,
 *                                      Margin = new Thickness(0, 0, 4, 0),
 *                              };
 *
 *                              paragraph.Inlines.Add(new InlineUIContainer(path));*/

                var stops = new List <ChatStop>();

                foreach (var emoteStop in App.EmoticonManager.SearchForEmoticons(message))
                {
                    stops.Add(new ChatStop()
                    {
                        Index  = emoteStop.Key,
                        Length = emoteStop.Value.Shortcut.Length,
                        Type   = StopType.Emoticon,
                        Data   = emoteStop.Value,
                    });
                }

                string[] urls = new string[] { "http://", "https://" };
                foreach (var url in urls)
                {
                    int index = message.IndexOf(url);
                    while (index != -1)
                    {
                        int endIndex = message.IndexOf(' ', index);
                        if (endIndex == -1)
                        {
                            endIndex = message.Length - 1;
                        }
                        stops.Add(new ChatStop()
                        {
                            Index  = index,
                            Length = endIndex - index + 1,
                            Type   = StopType.Url,
                        });

                        index = message.IndexOf(url, endIndex);
                    }
                }

                var orderedStops = stops.OrderBy(_ => _.Index);

                int startIndex = 0;
                foreach (var stop in orderedStops)
                {
                    if (stop.Index < startIndex)
                    {
                        continue;
                    }
                    if (stop.Index != startIndex)
                    {
                        paragraph.Inlines.Add(new Run(message.Substring(startIndex, stop.Index - startIndex)));
                    }

                    switch (stop.Type)
                    {
                    case StopType.Emoticon:
                        var picture = new EmoteImage()
                        {
                            SnapsToDevicePixels = true,
                            Emote = stop.Data as Emoticon,
                        };

                        paragraph.Inlines.Add(new InlineUIContainer(picture));
                        break;

                    case StopType.Url:
                        var text    = message.Substring(stop.Index, stop.Length);
                        var link    = new Hyperlink(new Run(text));
                        link.Tag    = text;
                        link.Click += OnChatLinkClicked;
                        paragraph.Inlines.Add(link);
                        break;
                    }

                    startIndex = stop.Index + stop.Length;
                }

                paragraph.Inlines.Add(new Run(message.Substring(startIndex)));

                if (font.Style.HasFlag(Protocol.FontStyle.Bold))
                {
                    paragraph.FontWeight = FontWeights.Bold;
                }
                if (font.Style.HasFlag(Protocol.FontStyle.Italic))
                {
                    paragraph.FontStyle = FontStyles.Italic;
                }
                if (font.Style.HasFlag(Protocol.FontStyle.Underline))
                {
                    paragraph.TextDecorations = new TextDecorationCollection();
                    paragraph.TextDecorations.Add(System.Windows.TextDecorations.Underline);
                }

                ChatHistory.Add(paragraph);
            }));
        }