private void InsertToBuffer(Gtk.TextBuffer buffer, ref Gtk.TextIter iter, UrlMessagePartModel urlPart) { var linkText = urlPart.Text ?? urlPart.Url; Uri uri; try { uri = new Uri(urlPart.Url); } catch (UriFormatException ex) { #if LOG4NET _Logger.Error("AddMessage(): Invalid URL: " + urlPart.Url, ex); #endif buffer.Insert(ref iter, linkText); return; } var tags = new List <Gtk.TextTag>(); // link URI tag var linkTag = new LinkTag(uri); linkTag.TextEvent += OnLinkTagTextEvent; _MessageTextTagTable.Add(linkTag); tags.Add(linkTag); // link style tag tags.Add(LinkTag); buffer.InsertWithTags(ref iter, linkText, tags.ToArray()); }
public MessageModel ToMessage() { var msg = new MessageModel() { MessageType = this.MessageType, TimeStamp = this.TimeStamp }; foreach (var msgPart in MessageParts) { MessagePartModel part = null; switch (msgPart.Type) { case "Text": var textPart = new TextMessagePartModel() { ForegroundColor = msgPart.ForegroundColor, BackgroundColor = msgPart.BackgroundColor, Underline = msgPart.Underline, Bold = msgPart.Bold, Italic = msgPart.Italic, Text = msgPart.Text }; part = textPart; break; case "URL": var urlPart = new UrlMessagePartModel() { Url = msgPart.Url, Text = msgPart.Text }; part = urlPart; break; case "Image": var imagePart = new ImageMessagePartModel() { ImageFileName = msgPart.ImageFileName, AlternativeText = msgPart.AlternativeText }; part = imagePart; break; } if (part == null) { continue; } part.IsHighlight = msgPart.IsHighlight; msg.MessageParts.Add(part); } return(msg); }
public void ToMarkup() { MessageModel testmodel = new MessageModel(); testmodel.IsCompactable = false; TextMessagePartModel textmodel; UrlMessagePartModel urlmodel; textmodel = new TextMessagePartModel("normal"); testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("blue"); textmodel.ForegroundColor = TextColor.Parse("0000FF"); testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("bold"); textmodel.Bold = true; testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("bold2"); textmodel.Bold = true; testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("normal"); testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("underline"); textmodel.Underline = true; testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("combined"); textmodel.Underline = true; textmodel.Bold = true; textmodel.Italic = true; textmodel.ForegroundColor = TextColor.Parse("00FF00"); textmodel.BackgroundColor = TextColor.Parse("0000FF"); testmodel.MessageParts.Add(textmodel); urlmodel = new UrlMessagePartModel("http://www.smuxi.org"); testmodel.MessageParts.Add(urlmodel); textmodel = new TextMessagePartModel("normal"); testmodel.MessageParts.Add(textmodel); string expected = "normal<span color='#0000FF'>blue</span>" + "<b>bold</b><b>bold2</b>normal<u>underline</u>" + "<span color='#00FF00'><u><b><i>combined</i></b></u></span>" + "<span color='#00008B'><u>http://www.smuxi.org</u></span>normal"; string tested = PangoTools.ToMarkup(testmodel); Assert.AreEqual(expected, tested); }
public virtual void AddMessage(MessageModel msg) { Trace.Call(msg); string timestamp; try { string format = (string)Frontend.UserConfig["Interface/Notebook/TimestampFormat"]; timestamp = msg.TimeStamp.ToLocalTime().ToString(format); } catch (FormatException e) { timestamp = "Timestamp Format ERROR: " + e.Message; } timestamp += " "; _OutputTextView.SelectionHangingIndent = TextRenderer.MeasureText(timestamp, _Font).Width; _OutputTextView.AppendText(timestamp); bool hasHighlight = false; foreach (MessagePartModel msgPart in msg.MessageParts) { #if LOG4NET _Logger.Debug("AddMessage(): msgPart.GetType(): " + msgPart.GetType()); #endif if (msgPart.IsHighlight) { hasHighlight = true; } // TODO: implement all types if (msgPart is UrlMessagePartModel) { UrlMessagePartModel fmsgui = (UrlMessagePartModel)msgPart; /*TODO: Create a link in the TextView (possibly requiring WinAPI hacks...)*/ _OutputTextView.SelectionColor = Color.Blue; _OutputTextView.SelectionFont = new Font(_Font, FontStyle.Underline); _OutputTextView.AppendText(fmsgui.Url); } else if (msgPart is TextMessagePartModel) { /*TODO: Add required formatting to the TextView (possibly requiring WinAPI hacks...)*/ TextMessagePartModel fmsgti = (TextMessagePartModel)msgPart; #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Text: '" + fmsgti.Text + "'"); #endif FontStyle fstyle = FontStyle.Regular; if (fmsgti.ForegroundColor == TextColor.None) { _OutputTextView.SelectionColor = _ForegroundColor ?? Color.White; } else { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.ForegroundColor: '" + fmsgti.ForegroundColor.ToString() + "'"); #endif _OutputTextView.SelectionColor = ColorTools.GetColor(fmsgti.ForegroundColor); } if (fmsgti.BackgroundColor == TextColor.None) { _OutputTextView.SelectionBackColor = _BackgroundColor ?? Color.Black; } else { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.BackgroundColor: '" + fmsgti.BackgroundColor.ToString() + "'"); #endif _OutputTextView.SelectionBackColor = ColorTools.GetColor(fmsgti.BackgroundColor); } if (fmsgti.Underline) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Underline is true"); #endif fstyle |= FontStyle.Underline; } if (fmsgti.Bold) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Bold is true"); #endif fstyle |= FontStyle.Bold; } if (fmsgti.Italic) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Italic is true"); #endif fstyle |= FontStyle.Italic; } _OutputTextView.SelectionFont = new Font(Font, fstyle); _OutputTextView.AppendText(fmsgti.Text); } } _OutputTextView.AppendText("\n"); // HACK: out of scope? if (hasHighlight /*&& !Frontend.MainWindow.HasToplevelFocus*/) { /*TODO: Flash the main window*/ if (Frontend.UserConfig["Sound/BeepOnHighlight"] != null && (bool)Frontend.UserConfig["Sound/BeepOnHighlight"]) { System.Media.SystemSounds.Beep.Play(); } } // HACK: out of scope? if (((TabControl)this.Parent).SelectedTab != this) { string color = null; if (hasHighlight) { _HasHighlight = hasHighlight; color = (string)Frontend.UserConfig["Interface/Notebook/Tab/HighlightColor"]; } else if (!_HasHighlight) { color = (string)Frontend.UserConfig["Interface/Notebook/Tab/ActivityColor"]; } if (color != null) { /*TODO: Color the associated Tab*/ } } }
public static string ToMarkup(MessageModel msg, Gdk.Color?bgColor) { if (msg == null) { return(String.Empty); } /* Pango Markup doesn't support hyperlinks: * (smuxi-frontend-gnome:9824): Gtk-WARNING **: Failed to set * text from markup due to error parsing markup: Unknown tag * 'a' on line 1 char 59 * * For this reason, for UrlMessagePartModels, we'll render them as * plaintext. * * Here we loop over the MessageModel to build up a proper Pango * Markup. * * The colour codes/values have been taken from BuildTagTable(), in * MessageTextView.cs. * * Documentation for Pango Markup is located at: * http://library.gnome.org/devel/pango/unstable/PangoMarkupFormat.html */ StringBuilder markup = new StringBuilder(); foreach (MessagePartModel msgPart in msg.MessageParts) { if (msgPart is UrlMessagePartModel) { UrlMessagePartModel url = (UrlMessagePartModel)msgPart; string str = GLib.Markup.EscapeText(url.Text); Gdk.Color gdkColor = Gdk.Color.Zero; Gdk.Color.Parse("darkblue", ref gdkColor); TextColor urlColor = ColorConverter.GetTextColor(gdkColor); if (bgColor != null) { // we have a bg color so lets try to get a url color // with a good contrast urlColor = TextColorTools.GetBestTextColor( urlColor, ColorConverter.GetTextColor(bgColor.Value) ); } str = String.Format("<span color='#{0}'><u>{1}</u></span>", urlColor.HexCode, str); markup.Append(str); } else if (msgPart is TextMessagePartModel) { TextMessagePartModel text = (TextMessagePartModel)msgPart; List <string> tags = new List <string>(); string str = GLib.Markup.EscapeText(text.Text); if (text.ForegroundColor != TextColor.None) { TextColor fgColor; if (bgColor == null) { fgColor = text.ForegroundColor; } else { var bgTextColor = ColorConverter.GetTextColor( bgColor.Value ); fgColor = TextColorTools.GetBestTextColor( text.ForegroundColor, bgTextColor ); } tags.Add(String.Format("span color='#{0}'", fgColor.HexCode)); } if (text.Underline) { tags.Add("u"); } if (text.Bold) { tags.Add("b"); } if (text.Italic) { tags.Add("i"); } if (tags.Count > 0) { tags.Reverse(); foreach (string tag in tags) { string endTag; if (tag.Contains(" ")) { // tag contains attributes, only get tag name endTag = tag.Split(' ')[0]; } else { endTag = tag; } str = String.Format("<{0}>{1}</{2}>", tag, str, endTag); } } markup.Append(str); } } return(markup.ToString()); }
public virtual MessageModel ToMessage() { var msg = new MessageModel() { MessageType = this.MessageType, TimeStamp = this.TimeStamp }; foreach (var msgPart in MessageParts) { MessagePartModel part = null; switch (msgPart.Type) { case "Text": var textPart = new TextMessagePartModel() { ForegroundColor = msgPart.ForegroundColor, BackgroundColor = msgPart.BackgroundColor, Underline = msgPart.Underline, Bold = msgPart.Bold, Italic = msgPart.Italic, Text = msgPart.Text }; part = textPart; break; case "URL": var urlPart = new UrlMessagePartModel() { Url = msgPart.Url, Text = msgPart.Text }; part = urlPart; break; case "Image": var imagePart = new ImageMessagePartModel() { ImageFileName = msgPart.ImageFileName, AlternativeText = msgPart.AlternativeText }; part = imagePart; break; } if (part == null) { continue; } part.IsHighlight = msgPart.IsHighlight; msg.MessageParts.Add(part); } return msg; }