summaryrefslogtreecommitdiff
path: root/Juick/Classes/RichTextConverter.cs
blob: ee6ffa7484ff811fe2b3dc229047d2b43c9561da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Tasks;

namespace Juick.Classes
{
    public class RichTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var message = value as string;
            var runs = new List<Inline>();

            foreach (var word in message.Split(new char[] {' ', '\n'}))
            {
                Uri uri;

                if (Uri.TryCreate(word, UriKind.Absolute, out uri) ||
                   (word.StartsWith("www.") && Uri.TryCreate("http://" + word, UriKind.Absolute, out uri)))
                {
                    var link = new Hyperlink();
                    link.Inlines.Add(new Run() { Text = word });
                    link.Click += (sender, e) =>
                    {
                        var hyperLink = (sender as Hyperlink);
                        new WebBrowserTask() { Uri = uri }.Show();
                    };

                    runs.Add(link);
                }
                else
                {
                    runs.Add(new Run() { Text = word });
                }

                runs.Add(new Run() { Text = " " });
            }

            return runs;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}