File: DemoCssBasics.cs

package info (click to toggle)
gtk-sharp3 2.99.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,488 kB
  • sloc: xml: 308,885; cs: 38,796; sh: 11,336; perl: 1,295; makefile: 1,099; ansic: 903
file content (99 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download | duplicates (5)
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
/* CSS Theming/CSS Basics
 *
 * Gtk themes are written using CSS. Every widget is build of multiple items
 * that you can style very similarly to a regular website.
 *
 */

using System;
using System.IO;
using System.Reflection;
using Gtk;

namespace GtkDemo
{
	[Demo ("CSS Basics", "DemoCssBasics.cs", "CSS Theming")]
	public class DemoCssBasics : Window
	{
		TextBuffer buffer;
		CssProvider provider;
		CssProvider provider_reset;

		public DemoCssBasics () : base ("CSS Basics")
		{
			SetDefaultSize (600, 500);

			buffer = new TextBuffer (null);

			var warning = new TextTag ("warning");
			warning.Underline = Pango.Underline.Single;
			buffer.TagTable.Add (warning);

			var error = new TextTag ("error");
			error.Underline = Pango.Underline.Error;
			buffer.TagTable.Add (error);

			provider = new CssProvider ();
			provider_reset = new CssProvider ();

			var container = new ScrolledWindow ();
			Add (container);
			var view = new TextView (buffer);
			container.Add (view);
			buffer.Changed += OnCssTextChanged;

			using (Stream stream = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("reset.css"))
			using (StreamReader reader = new StreamReader(stream))
			{
				provider_reset.LoadFromData (reader.ReadToEnd());
			}

			using (Stream stream = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("css_basics.css"))
			using (StreamReader reader = new StreamReader(stream))
			{
				buffer.Text = reader.ReadToEnd();
			}

			// TODO: Connect to "parsing-error" signal in CssProvider, added in GTK+ 3.2

			ApplyCss (this, provider_reset, 800);
			ApplyCss (this, provider, UInt32.MaxValue);

			ShowAll ();
		}

		private void ApplyCss (Widget widget, CssProvider provider, uint priority)
		{
			widget.StyleContext.AddProvider (provider, priority);
			var container = widget as Container;
			if (container != null) {
				foreach (var child in container.Children) {
					ApplyCss (child, provider, priority);
				}
			}
		}

		private void OnCssTextChanged (object sender, EventArgs e)
		{
			var start = buffer.StartIter;
			var end = buffer.EndIter;
			buffer.RemoveAllTags (start, end);

			string text = buffer.Text;
			try {
				provider.LoadFromData (text);
			} catch (GLib.GException) {
				// Ignore parsing errors
			}

			StyleContext.ResetWidgets (Gdk.Screen.Default);
		}

		protected override bool OnDeleteEvent (Gdk.Event evt)
		{
			Destroy ();
			return true;
		}
	}
}