~ubuntu-branches/ubuntu/natty/smuxi/natty

« back to all changes in this revision

Viewing changes to src/Frontend-GNOME/PangoTools.cs

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2010-01-11 22:47:12 UTC
  • mto: (4.2.5 sid) (1.1.4 upstream) (15.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100111224712-cdvx3vondz2g08vg
ImportĀ upstreamĀ versionĀ 0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id$
 
2
//
 
3
// Smuxi - Smart MUltipleXed Irc
 
4
//
 
5
// Copyright (c) 2010 David Paleino <dapal@debian.org>
 
6
//
 
7
// Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 
8
//
 
9
// This program is free software; you can redistribute it and/or modify
 
10
// it under the terms of the GNU General Public License as published by
 
11
// the Free Software Foundation; either version 2 of the License, or
 
12
// (at your option) any later version.
 
13
//
 
14
// This program is distributed in the hope that it will be useful,
 
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
// GNU General Public License for more details.
 
18
//
 
19
// You should have received a copy of the GNU General Public License
 
20
// along with this program; if not, write to the Free Software
 
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
22
 
 
23
using System;
 
24
using System.Collections.Generic;
 
25
using System.Text;
 
26
using Smuxi.Engine;
 
27
 
 
28
namespace Smuxi.Frontend.Gnome
 
29
{
 
30
    public class PangoTools
 
31
    {
 
32
        public static string ToMarkup(MessageModel msg)
 
33
        {
 
34
            return ToMarkup(msg, null);
 
35
        }
 
36
 
 
37
        public static string ToMarkup(MessageModel msg, Gdk.Color? bgColor)
 
38
        {
 
39
            if (msg == null) {
 
40
                return String.Empty;
 
41
            }
 
42
 
 
43
            /* Pango Markup doesn't support hyperlinks:
 
44
             *     (smuxi-frontend-gnome:9824): Gtk-WARNING **: Failed to set
 
45
             *     text from markup due to error parsing markup: Unknown tag
 
46
             *     'a' on line 1 char 59
 
47
             *
 
48
             * For this reason, for UrlMessagePartModels, we'll render them as
 
49
             * plaintext.
 
50
             *
 
51
             * Here we loop over the MessageModel to build up a proper Pango
 
52
             * Markup.
 
53
             *
 
54
             * The colour codes/values have been taken from BuildTagTable(), in
 
55
             * MessageTextView.cs.
 
56
             *
 
57
             * Documentation for Pango Markup is located at:
 
58
             *    http://library.gnome.org/devel/pango/unstable/PangoMarkupFormat.html
 
59
             */
 
60
            StringBuilder markup = new StringBuilder ();
 
61
            foreach (MessagePartModel msgPart in msg.MessageParts) {
 
62
                if (msgPart is UrlMessagePartModel) {
 
63
                    UrlMessagePartModel url = (UrlMessagePartModel) msgPart;
 
64
 
 
65
                    string str = GLib.Markup.EscapeText(url.Text);
 
66
                    
 
67
                    Gdk.Color gdkColor = Gdk.Color.Zero;
 
68
                    Gdk.Color.Parse("darkblue", ref gdkColor);
 
69
                    TextColor urlColor = ColorTools.GetTextColor(gdkColor);
 
70
                    if (bgColor != null) {
 
71
                        // we have a bg color so lets try to get a url color
 
72
                        // with a good contrast
 
73
                        urlColor = ColorTools.GetBestTextColor(
 
74
                            urlColor, ColorTools.GetTextColor(bgColor.Value));
 
75
                    }
 
76
 
 
77
                    str = String.Format("<span color='#{0}'><u>{1}</u></span>",
 
78
                                        urlColor.HexCode,
 
79
                                        str);
 
80
                    markup.Append(str);
 
81
                } else if (msgPart is TextMessagePartModel) {
 
82
                    TextMessagePartModel text = (TextMessagePartModel) msgPart;
 
83
                    List<string> tags = new List<string>();
 
84
 
 
85
                    string str = GLib.Markup.EscapeText(text.Text);
 
86
                    if (text.ForegroundColor != TextColor.None) {
 
87
                        tags.Add(String.Format("span color='#{0}'",
 
88
                                                text.ForegroundColor.HexCode));
 
89
                    }
 
90
                    // TODO: do contrast checks here like we do in MessageTextView?
 
91
                    if (text.Underline) {
 
92
                        tags.Add("u");
 
93
                    }
 
94
                    if (text.Bold) {
 
95
                        tags.Add("b");
 
96
                    }
 
97
                    if (text.Italic) {
 
98
                        tags.Add("i");
 
99
                    }
 
100
 
 
101
                    if (tags.Count > 0) {
 
102
                        foreach (string tag in tags) {
 
103
                            str = String.Format("{0}{1}{2}",
 
104
                                "<"+tag+">", str, "</"+tag.Split(' ')[0]+">");
 
105
                        }
 
106
                    }
 
107
 
 
108
                    markup.Append(str);
 
109
                }
 
110
            }
 
111
 
 
112
            return markup.ToString();
 
113
        }
 
114
    }
 
115
}