aboutsummaryrefslogtreecommitdiffstatsci
path: root/Samples/DebugLogAnalyzer/src/DebugInterface.cs
blob: c0615321c6318765ace8deee1cd0f8b3fa4e814e (plain)
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
// Copyright (c) 2013-2019  Bruyère Jean-Philippe <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)

using System;
using Crow.Cairo;
using System.Threading;

namespace Crow
{
	public class DebugInterface : SampleBase {
		static DebugInterface() {
			DbgLogger.IncludeEvents = DbgEvtType.None;
			DbgLogger.DiscardEvents = DbgEvtType.None;
			DbgLogger.ConsoleOutput = true;
		}
		public DebugInterface (IntPtr hWin) : base (hWin)
		{
			surf = new ImageSurface (Format.Argb32, 100, 100);
		}

		public override void Run()
		{
			Init();

			Thread t = new Thread (interfaceThread) {
				IsBackground = true
			};
			t.Start ();
		}
		public bool Terminate;
		string source;
		Action delRegisterForRepaint;//call RegisterForRepaint in the container widget (DebugInterfaceWidget)
		Action<Exception> delSetCurrentException;

		void interfaceThread () {
			while (!Terminate) {
				try
				{
					Update();	
				}
				catch (System.Exception ex)
				{
					if (Monitor.IsEntered(LayoutMutex))
						Monitor.Exit (LayoutMutex);
					if (Monitor.IsEntered(UpdateMutex))
						Monitor.Exit (UpdateMutex);
					if (Monitor.IsEntered(ClippingMutex))
						Monitor.Exit (ClippingMutex);
					delSetCurrentException (ex);					
					ClearInterface();
					Thread.Sleep(1000);	
				}
				
				if (IsDirty)
					delRegisterForRepaint();				
					
				Thread.Sleep (UPDATE_INTERVAL);
			}
		}
		public IntPtr SurfacePointer {
			get {
				lock(UpdateMutex)
					return surf.Handle;
			}
		}
		public void RegisterDebugInterfaceCallback (object w){
			Type t = w.GetType();
			delRegisterForRepaint = (Action)Delegate.CreateDelegate(typeof(Action), w, t.GetMethod("RegisterForRepaint"));
			delSetCurrentException = (Action<Exception>)Delegate.CreateDelegate(typeof(Action<Exception>), w, t.GetProperty("CurrentException").GetSetMethod());
		}
		public void ResetDirtyState () {
			IsDirty = false;
		}
		public string Source {
			set {
				if (source == value)
					return;
				source = value;
				if (string.IsNullOrEmpty(source))
					return;
				delSetCurrentException(null);
				try
				{
					lock (UpdateMutex) {
						Widget tmp = CreateITorFromIMLFragment (source).CreateInstance();
						ClearInterface();
						AddWidget (tmp);
						tmp.DataSource = this;
					}					
				}
				catch (IML.InstantiatorException iTorEx)
				{
					delSetCurrentException(iTorEx.InnerException);
				}
				catch (System.Exception ex)
				{
					delSetCurrentException(ex);
				}
			}
		}
		public void Resize (int width, int height) {
			
			lock (UpdateMutex) {
				clientRectangle = new Rectangle (0, 0, width, height);
				surf?.Dispose();
				surf = new ImageSurface (Format.Argb32, width, height);				
				foreach (Widget g in GraphicTree)
					g.RegisterForLayouting (LayoutingType.All);
				RegisterClip (clientRectangle);
			}				
		}				
	}
}