ui/wpf/UIcore/Application.cs

Sun, 01 Feb 2015 12:37:31 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Feb 2015 12:37:31 +0100
changeset 85
91f45354d1e2
parent 81
5eb765a7a793
child 89
9a7e4a335b2b
permissions
-rw-r--r--

added ui_layout_fill (WPF)

78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 using System;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 using System.Collections.Generic;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 using System.Linq;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 using System.Text;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 using System.Threading;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 using System.Threading.Tasks;
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
7 using System.Windows;
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 namespace UI
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 public class Application
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 private static Application instance;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 private System.Windows.Application application;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 private Thread thread;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 private Queue<Action> queue = new Queue<Action>();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 private object sync = new object();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 private object result = new object();
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
21 private Boolean main = false;
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 public String Name;
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
24 public List<Window> Windows = new List<Window>();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
25 public ApplicationMenu AppMenu = new ApplicationMenu();
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 private Application() : base()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 thread = new Thread(() => Run());
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 thread.SetApartmentState(ApartmentState.STA);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 thread.Start();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 public static Application GetInstance()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 if (instance == null)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 instance = new Application();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 GC.KeepAlive(instance);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 return instance;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 public Thread Start()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 lock(sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 queue.Enqueue(() => RunApplication());
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 Monitor.Pulse(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 return thread;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 private void RunApplication()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 application = new System.Windows.Application();
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
57 main = true;
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 application.Run();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 public void Run()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 lock(sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 for (;;)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 Monitor.Wait(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 Action action = queue.Dequeue();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 action.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 lock (result)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 Monitor.Pulse(result);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
74 if (main)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
75 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
76 // end loop after shutdown
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
77 break;
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
78 }
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 public T Exec<T>(Func<T> func)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 ResultExec<T> e = new ResultExec<T>();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 e.Func = func;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 e.Exec();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 return e.Result;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 else
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 lock (sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 queue.Enqueue(() => e.Exec());
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 Monitor.Pulse(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 lock (result)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 Monitor.Wait(result);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 return e.Result;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 public void Exec(Action action)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 action.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 else
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 lock (sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 queue.Enqueue(action);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 Monitor.Pulse(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 lock (result)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 Monitor.Wait(result);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 }
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
128
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
129 public void AddWindow(Window window)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
130 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
131 Windows.Add(window);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
132 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
133
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
134 public void RemoveWindow(Window window)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
135 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
136 Windows.Remove(window);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
137 if (Windows.Count == 0)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
138 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
139 application.Shutdown();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
140 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
141 }
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 public class ResultExec<T>
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 public T Result;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 public Func<T> Func;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 public void Exec()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 Result = Func.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 public class VoidExec
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 public Action Action;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 public void Exec()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 Action.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 }

mercurial