File: GtkHtmlHtmlRender.cs

package info (click to toggle)
mono-tools 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,868 kB
  • sloc: cs: 100,909; sh: 3,845; makefile: 2,040; xml: 126
file content (143 lines) | stat: -rw-r--r-- 2,717 bytes parent folder | download | duplicates (7)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
//
// GtkHtmlHtmlRender.cs: Implementation of IHtmlRender that uses Gtk.HTML
//
// Author: Mario Sopena
// Author:	Rafael Ferreira <raf@ophion.org>
//
using System;
using Gtk;
using System.IO;
using System.Reflection;

namespace Monodoc {
class GtkHtmlHtmlRender : IHtmlRender {
	
	HTML html_panel;
	public Widget HtmlPanel {
		get { return (Widget) html_panel; }
	}

	string url;
	public string Url {
		get { return url; }
	}

	RootTree help_tree;
	public event EventHandler OnUrl;
	public event EventHandler UrlClicked;

	
	public GtkHtmlHtmlRender (RootTree help_tree) 
	{
		this.help_tree = help_tree;
	}
	
	protected void LinkClicked (object o, LinkClickedArgs args)
	{
		url = FixUrl (args.Url);
		if (UrlClicked != null)
			UrlClicked (this, new EventArgs());
	}

	static string FixUrl (string url)
	{
		if (url == null)
			return url;
		return url.Replace ("&lt;", "<").Replace ("&gt;", ">");
	}
	
	protected void OnUrlMouseOver (object o, OnUrlArgs args)
	{
		url = FixUrl (args.Url);
		if (OnUrl != null)
			OnUrl (this, args);
	}

	public void JumpToAnchor (string anchor)
	{
		html_panel.JumpToAnchor(anchor);
	}

	public void Copy () 
	{
		html_panel.Copy();	
	}

	public void SelectAll () 
	{
		html_panel.SelectAll();	
	}

	public void Render (string html_code) 
	{
		Gtk.HTMLStream stream = html_panel.Begin ("text/html");
		stream.Write(html_code);
		html_panel.End (stream, HTMLStreamStatus.Ok);
	}

	static Stream GetBrowserResourceImage (string name)
	{
		Assembly assembly = typeof (RootTree).Assembly;
		System.IO.Stream s = assembly.GetManifestResourceStream (name);
		
		return s;
	}

	protected void UrlRequested (object sender, UrlRequestedArgs args)
	{
		Stream s = help_tree.GetImage (args.Url);
		
		if (s == null)
			s = GetBrowserResourceImage ("monodoc.png");
		byte [] buffer = new byte [8192];
		int n, m;
		m=0;
		while ((n = s.Read (buffer, 0, 8192)) != 0) {
			args.Handle.Write (buffer, n);
			m += n;
		}
		args.Handle.Close (HTMLStreamStatus.Ok);
	}
	
	public void Print (string Html) {
#if !MACOS
		if (Html == null) {
			Console.WriteLine ("empty print");
			return;
		}

		PrintManager.Print (Html);
#endif
	}

	public bool Initialize ()
	{
		try {
			html_panel = new HTML ();
		}
		catch (Exception ex) {
			Console.WriteLine (ex.Message);
			Console.WriteLine (ex.StackTrace);
			return false;
		}
		html_panel.Show ();
		html_panel.LinkClicked += new LinkClickedHandler (LinkClicked);
		html_panel.OnUrl += new OnUrlHandler (OnUrlMouseOver);
		html_panel.UrlRequested += new UrlRequestedHandler (UrlRequested);
		return true;

	}

	public Capabilities Capabilities
	{
		get { return Capabilities.None; }
	}

	public string Name
	{
		get { return "GtkHtml"; }
	}

}
}