3
// Smuxi - Smart MUltipleXed Irc
5
// Copyright (c) 2010 David Paleino <dapal@debian.org>
7
// Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
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.
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.
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
24
using System.Collections.Generic;
28
namespace Smuxi.Frontend.Gnome
30
public class PangoTools
32
public static string ToMarkup(MessageModel msg)
34
return ToMarkup(msg, null);
37
public static string ToMarkup(MessageModel msg, Gdk.Color? bgColor)
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
48
* For this reason, for UrlMessagePartModels, we'll render them as
51
* Here we loop over the MessageModel to build up a proper Pango
54
* The colour codes/values have been taken from BuildTagTable(), in
57
* Documentation for Pango Markup is located at:
58
* http://library.gnome.org/devel/pango/unstable/PangoMarkupFormat.html
60
StringBuilder markup = new StringBuilder ();
61
foreach (MessagePartModel msgPart in msg.MessageParts) {
62
if (msgPart is UrlMessagePartModel) {
63
UrlMessagePartModel url = (UrlMessagePartModel) msgPart;
65
string str = GLib.Markup.EscapeText(url.Text);
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));
77
str = String.Format("<span color='#{0}'><u>{1}</u></span>",
81
} else if (msgPart is TextMessagePartModel) {
82
TextMessagePartModel text = (TextMessagePartModel) msgPart;
83
List<string> tags = new List<string>();
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));
90
// TODO: do contrast checks here like we do in MessageTextView?
101
if (tags.Count > 0) {
102
foreach (string tag in tags) {
103
str = String.Format("{0}{1}{2}",
104
"<"+tag+">", str, "</"+tag.Split(' ')[0]+">");
112
return markup.ToString();