[tomboy] Copy rich text to apps that support it (#431014)



commit c829483cfd96ae30eef2496110d2c42f4254c2ef
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Mon Nov 16 15:23:18 2009 -0800

    Copy rich text to apps that support it (#431014)

 Tomboy/Makefile.am                  |    1 +
 Tomboy/NoteBuffer.cs                |   52 ++++++++++++++
 data/Makefile.am                    |    1 +
 data/tomboy-note-clipboard-html.xsl |  127 +++++++++++++++++++++++++++++++++++
 4 files changed, 181 insertions(+), 0 deletions(-)
---
diff --git a/Tomboy/Makefile.am b/Tomboy/Makefile.am
index 4371b3a..ff19c6c 100644
--- a/Tomboy/Makefile.am
+++ b/Tomboy/Makefile.am
@@ -154,6 +154,7 @@ ASSEMBLIES = 		\
 RESOURCES = 										\
 	-resource:$(top_srcdir)/data/GNOME_TomboyApplet.xml,GNOME_TomboyApplet.xml	\
 	-resource:$(top_srcdir)/data/UIManagerLayout.xml	\
+	-resource:$(top_srcdir)/data/tomboy-note-clipboard-html.xsl,tomboy-note-clipboard-html.xsl	\
 	-resource:$(top_srcdir)/data/icons/hicolor_apps_48x48_tomboy.png,tomboy.png			\
 	-resource:$(top_srcdir)/data/icons/hicolor_places_22x22_note.png,note.png		\
 	-resource:$(top_srcdir)/data/icons/hicolor_actions_16x16_note-new.png,note-new.png		\
diff --git a/Tomboy/NoteBuffer.cs b/Tomboy/NoteBuffer.cs
index 938ae57..201f8e6 100644
--- a/Tomboy/NoteBuffer.cs
+++ b/Tomboy/NoteBuffer.cs
@@ -1,8 +1,11 @@
 
 using System;
 using System.Collections.Generic;
+using System.Reflection;
 using System.IO;
 using System.Xml;
+using System.Xml.XPath;
+using System.Xml.Xsl;
 
 namespace Tomboy
 {
@@ -48,9 +51,16 @@ namespace Tomboy
 			}
 		}
 
+		private static bool text_buffer_serialize_func_fixed = typeof(Gtk.TextBufferSerializeFunc).GetMethod ("Invoke").ReturnType == typeof(byte[]);
+
 		public NoteBuffer (Gtk.TextTagTable tags, Note note)
 : base (tags)
 		{
+			// Ensure Gtk# has the fix for BNC #555495
+			if (text_buffer_serialize_func_fixed) {
+				RegisterSerializeFormat ("text/html", (Gtk.TextBufferSerializeFunc) Delegate.CreateDelegate (typeof(Gtk.TextBufferSerializeFunc), this, "SerializeToHtml"));
+			}
+
 			active_tags = new List<Gtk.TextTag> ();
 			undo_manager = new UndoManager (this);
 
@@ -68,6 +78,48 @@ namespace Tomboy
 			this.note = note;
 		}
 
+		private static XslTransform html_transform;
+		private static XslTransform HtmlTransform {
+			get {
+				if (html_transform == null) {
+					html_transform = new XslTransform ();
+					var resource = typeof(NoteBuffer).Assembly.GetManifestResourceStream ("tomboy-note-clipboard-html.xsl");
+					var reader = new XmlTextReader (resource);
+					html_transform.Load (reader, null, null);
+					reader.Close ();
+				}
+				return html_transform;
+			}
+		}
+
+		private byte [] SerializeToHtml (Gtk.TextBuffer register_buffer, Gtk.TextBuffer content_buffer, Gtk.TextIter start, Gtk.TextIter end, out ulong length)
+		{
+			if (start.Equals (end) || start.Equals (Gtk.TextIter.Zero) || end.Equals (Gtk.TextIter.Zero) || HtmlTransform == null) {
+				length = 0;
+				return new byte [0];
+			}
+
+			Logger.Debug ("Handling text/html Clipboard copy/cut request");
+			var xsl = HtmlTransform;
+
+			string xml = String.Format (
+				"<note version=\"0.3\" xmlns:link=\"http://beatniksoftware.com/tomboy/link\"; xmlns:size=\"http://beatniksoftware.com/tomboy/size\";>{0}</note>",
+				NoteBufferArchiver.Serialize (register_buffer, start, end)
+			);
+
+			var reader = new StringReader (xml);
+			var doc = new XPathDocument (reader);
+			var args = new XsltArgumentList ();
+
+			var writer = new StringWriter ();
+			xsl.Transform (doc, args, writer);
+
+			string html = writer.ToString ();
+			byte [] bytes = System.Text.Encoding.UTF8.GetBytes (html);
+			length = (ulong)bytes.Length;
+			return bytes;
+		}
+
 		// Signal that text has been inserted, and any active tags have
 		// been applied to the text.  This allows undo to pull any
 		// active tags from the inserted text.
diff --git a/data/Makefile.am b/data/Makefile.am
index 0b6d587..5971a4d 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -67,6 +67,7 @@ EXTRA_DIST = 					\
 	$(noinst_DATA)				\
 	GNOME_TomboyApplet.server.in.in		\
 	$(dbusservice_in_files)				\
+	tomboy-note-clipboard-html.xsl		\
 	UIManagerLayout.xml
 
 DISTCLEANFILES = 				\
diff --git a/data/tomboy-note-clipboard-html.xsl b/data/tomboy-note-clipboard-html.xsl
new file mode 100644
index 0000000..310cfc1
--- /dev/null
+++ b/data/tomboy-note-clipboard-html.xsl
@@ -0,0 +1,127 @@
+<?xml version='1.0'?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+		xmlns:tomboy="http://beatniksoftware.com/tomboy";
+		xmlns:size="http://beatniksoftware.com/tomboy/size";
+		xmlns:link="http://beatniksoftware.com/tomboy/link";
+                version='1.0'>
+
+<xsl:output method="html" indent="no" />
+<xsl:preserve-space elements="*" />
+
+<xsl:param name="font" />
+<xsl:param name="newline" select="'&#xA;'" />
+
+<xsl:template match="/">
+	<html><body>
+
+    <xsl:apply-templates select="note"/>
+
+	</body></html>
+</xsl:template>
+
+<xsl:template match="text()">
+   <xsl:call-template name="softbreak"/>
+</xsl:template>
+
+<xsl:template name="softbreak">
+	<xsl:param name="text" select="."/>
+	<xsl:choose>
+		<xsl:when test="contains($text, $newline)">
+			<xsl:value-of select="substring-before($text, $newline)"/>
+			<br/>
+			<xsl:call-template name="softbreak">
+				<xsl:with-param name="text" select="substring-after($text, $newline)"/>
+			</xsl:call-template>
+		</xsl:when>
+
+		<xsl:otherwise>
+			<xsl:value-of select="$text"/>
+		</xsl:otherwise>
+	</xsl:choose>
+</xsl:template>
+
+<xsl:template match="note-content">
+    <xsl:apply-templates select="node()" />
+</xsl:template>
+
+<xsl:template match="bold">
+	<b><xsl:apply-templates select="node()"/></b>
+</xsl:template>
+
+<xsl:template match="italic">
+	<i><xsl:apply-templates select="node()"/></i>
+</xsl:template>
+
+<xsl:template match="strikethrough">
+	<strike><xsl:apply-templates select="node()"/></strike>
+</xsl:template>
+
+<xsl:template match="highlight">
+	<span style="background:yellow"><xsl:apply-templates select="node()"/></span>
+</xsl:template>
+
+<xsl:template match="datetime">
+	<span style="font-style:italic;font-size:small;color:#888A85">
+		<xsl:apply-templates select="node()"/>
+	</span>
+</xsl:template>
+
+<xsl:template match="size:small">
+	<span style="font-size:small"><xsl:apply-templates select="node()"/></span>
+</xsl:template>
+
+<xsl:template match="size:large">
+	<span style="font-size:large"><xsl:apply-templates select="node()"/></span>
+</xsl:template>
+
+<xsl:template match="size:huge">
+	<span style="font-size:xx-large"><xsl:apply-templates select="node()"/></span>
+</xsl:template>
+
+<xsl:template match="link:broken">
+    <xsl:value-of select="node()"/>
+</xsl:template>
+
+<xsl:template match="link:internal">
+    <xsl:value-of select="node()"/>
+</xsl:template>
+
+<xsl:template match="link:url">
+	<a style="color:#3465A4" href="{node()}"><xsl:value-of select="node()"/></a>
+</xsl:template>
+
+<xsl:template match="list">
+	<ul>
+		<xsl:apply-templates select="list-item" />
+	</ul>
+</xsl:template>
+
+<xsl:template match="list-item">
+	<li>
+		<xsl:if test="normalize-space(text()) = '' and count(list) = 1 and count(*) = 1">
+			<xsl:attribute name="style">list-style-type: none</xsl:attribute>
+		</xsl:if>
+		<xsl:attribute name="dir">
+			<xsl:value-of select="@dir"/>
+		</xsl:attribute>
+		<xsl:apply-templates select="node()" />
+	</li>
+</xsl:template>
+
+<!-- Evolution.dll Plugin -->
+<xsl:template match="link:evo-mail">
+    <xsl:value-of select="node()"/>
+</xsl:template>
+
+<!-- FixedWidth.dll Plugin -->
+<xsl:template match="monospace">
+	<span style="font-family:monospace"><xsl:apply-templates select="node()"/></span>
+</xsl:template>
+
+<!-- Bugzilla.dll Plugin -->
+<xsl:template match="link:bugzilla">
+	<a href="{ uri}"><xsl:value-of select="node()" /></a>
+</xsl:template>
+
+</xsl:stylesheet>
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]