Widow – Blame information for rev 21
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.ComponentModel; |
||
3 | using System.Configuration; |
||
8 | office | 4 | using System.Globalization; |
1 | office | 5 | using System.Threading.Tasks; |
6 | using System.Windows.Forms; |
||
15 | office | 7 | using AutoUpdaterDotNET; |
1 | office | 8 | using Widow.Properties; |
9 | using Widow.Serialization; |
||
10 | |||
11 | namespace Widow |
||
12 | { |
||
6 | office | 13 | public partial class MainForm : Form |
1 | office | 14 | { |
15 | #region Public Events & Delegates |
||
16 | |||
17 | public event EventHandler<WindowCreatedEventArgs> WindowCreated; |
||
18 | |||
19 | public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed; |
||
20 | |||
21 | #endregion |
||
22 | |||
23 | #region Public Enums, Properties and Fields |
||
24 | |||
25 | public Windows.Windows Windows { get; set; } |
||
26 | |||
27 | public RuleEditForm RuleEditForm { get; set; } |
||
28 | |||
9 | office | 29 | public WindowManipulation WindowManipulation { get; set; } |
1 | office | 30 | |
5 | office | 31 | public AboutForm AboutForm { get; set; } |
21 | office | 32 | public LogForm LogForm { get; private set; } |
5 | office | 33 | |
1 | office | 34 | #endregion |
35 | |||
36 | #region Constructors, Destructors and Finalizers |
||
37 | |||
6 | office | 38 | public MainForm() |
1 | office | 39 | { |
40 | InitializeComponent(); |
||
15 | office | 41 | AutoUpdater.Start("http://widow.grimore.org/update/update.xml"); |
6 | office | 42 | |
1 | office | 43 | // Bind to settings changed event. |
44 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
||
45 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
||
46 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
||
47 | |||
48 | Natives.RegisterShellHookWindow(Handle); |
||
49 | |||
18 | office | 50 | WindowManipulation = new WindowManipulation(Settings.Default, this); |
51 | |||
6 | office | 52 | LoadWindows().ContinueWith(task => |
53 | { |
||
18 | office | 54 | WindowManipulation.AddWindows(Windows); |
6 | office | 55 | }); |
1 | office | 56 | } |
57 | |||
58 | /// <summary> |
||
59 | /// Clean up any resources being used. |
||
60 | /// </summary> |
||
61 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
62 | protected override void Dispose(bool disposing) |
||
63 | { |
||
64 | if (disposing && components != null) |
||
65 | { |
||
66 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
||
67 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
||
68 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
||
69 | |||
70 | components.Dispose(); |
||
71 | } |
||
72 | |||
73 | base.Dispose(disposing); |
||
74 | } |
||
75 | |||
76 | #endregion |
||
77 | |||
78 | #region Private Overrides |
||
79 | |||
80 | protected override void WndProc(ref Message m) |
||
81 | { |
||
82 | var handle = m.LParam; |
||
83 | var windowName = Helpers.GetWindowTitle(handle); |
||
84 | |||
85 | switch (m.WParam.ToInt32()) |
||
86 | { |
||
87 | case (int) Natives.WM.HSHELL_WINDOWCREATED: |
||
88 | WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle)); |
||
89 | break; |
||
90 | case (int) Natives.WM.HSHELL_WINDOWDESTROYED: |
||
91 | WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle)); |
||
92 | break; |
||
93 | } |
||
94 | |||
95 | base.WndProc(ref m); |
||
96 | } |
||
97 | |||
98 | #endregion |
||
99 | |||
100 | #region Event Handlers |
||
101 | |||
8 | office | 102 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
1 | office | 103 | { |
104 | Settings.Default.Save(); |
||
105 | } |
||
106 | |||
8 | office | 107 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
1 | office | 108 | { |
109 | } |
||
110 | |||
8 | office | 111 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
1 | office | 112 | { |
113 | } |
||
114 | |||
115 | private void NewToolStripMenuItem_Click(object sender, EventArgs e) |
||
116 | { |
||
11 | office | 117 | if (RuleEditForm != null) |
118 | { |
||
119 | return; |
||
120 | } |
||
121 | |||
1 | office | 122 | RuleEditForm = new RuleEditForm(this, Windows); |
123 | RuleEditForm.Closed += RuleEditForm_Closed; |
||
124 | RuleEditForm.Show(); |
||
125 | } |
||
126 | |||
127 | private async void RuleEditForm_Closed(object sender, EventArgs e) |
||
128 | { |
||
129 | RuleEditForm.Closed -= RuleEditForm_Closed; |
||
130 | RuleEditForm.Dispose(); |
||
131 | RuleEditForm = null; |
||
132 | |||
133 | switch (await WindowsSerialization.Serialize(Windows, "Windows.xml")) |
||
134 | { |
||
135 | case SerializationSuccess serializationSuccess: |
||
21 | office | 136 | if (LogForm != null) |
137 | { |
||
138 | LogForm.InvokeIfRequired(form => |
||
139 | { |
||
140 | form.logTextBox.Text += "Windows saved." + Environment.NewLine; |
||
141 | }); |
||
142 | } |
||
1 | office | 143 | break; |
144 | case SerializationFailure serializationFailure: |
||
21 | office | 145 | if (LogForm != null) |
146 | { |
||
147 | LogForm.InvokeIfRequired(form => |
||
148 | { |
||
149 | form.logTextBox.Text += "Failed to save windows: " + serializationFailure.Exception.Message + Environment.NewLine; |
||
150 | }); |
||
151 | } |
||
1 | office | 152 | break; |
153 | } |
||
154 | } |
||
155 | |||
156 | private void ExitToolStripMenuItem_Click(object sender, EventArgs e) |
||
157 | { |
||
158 | Application.Exit(); |
||
159 | } |
||
160 | |||
161 | private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
||
162 | { |
||
163 | Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
||
164 | |||
165 | LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); |
||
166 | } |
||
167 | |||
5 | office | 168 | private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
169 | { |
||
170 | Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked; |
||
171 | |||
9 | office | 172 | WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate; |
5 | office | 173 | } |
174 | |||
175 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
176 | { |
||
177 | // Show the about form. |
||
178 | AboutForm = new AboutForm(); |
||
179 | AboutForm.Show(); |
||
180 | } |
||
181 | |||
11 | office | 182 | private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e) |
7 | office | 183 | { |
11 | office | 184 | await WindowManipulation.Apply(); |
7 | office | 185 | } |
186 | |||
8 | office | 187 | private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
188 | { |
||
189 | var toolStripMenuItem = (ToolStripMenuItem) sender; |
||
190 | |||
191 | if (!int.TryParse(toolStripTextBox1.Text, out var time)) |
||
192 | { |
||
193 | return; |
||
194 | } |
||
195 | |||
196 | Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
||
197 | Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture); |
||
198 | |||
9 | office | 199 | WindowManipulation.ApplyEveryTime = time; |
200 | WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
||
8 | office | 201 | } |
202 | |||
13 | office | 203 | private void NotifyIcon1_DoubleClick(object sender, EventArgs e) |
204 | { |
||
205 | if (RuleEditForm != null) |
||
206 | { |
||
207 | return; |
||
208 | } |
||
209 | |||
210 | RuleEditForm = new RuleEditForm(this, Windows); |
||
211 | RuleEditForm.Closed += RuleEditForm_Closed; |
||
212 | RuleEditForm.Show(); |
||
213 | } |
||
214 | |||
215 | private async void NotifyIcon1_Click(object sender, EventArgs e) |
||
216 | { |
||
217 | await WindowManipulation.Apply(); |
||
218 | } |
||
219 | |||
1 | office | 220 | #endregion |
221 | |||
222 | #region Private Methods |
||
223 | |||
224 | private async Task LoadWindows() |
||
225 | { |
||
226 | switch (await WindowsSerialization.Deserialize("Windows.xml")) |
||
227 | { |
||
228 | case SerializationSuccess serializationSuccess: |
||
229 | Windows = serializationSuccess.Windows; |
||
21 | office | 230 | if (LogForm != null) |
231 | { |
||
232 | LogForm.InvokeIfRequired(form => |
||
233 | { |
||
234 | form.logTextBox.Text += "Windows loaded." + Environment.NewLine; |
||
235 | }); |
||
236 | } |
||
1 | office | 237 | break; |
238 | case SerializationFailure serializationFailure: |
||
239 | Windows = new Windows.Windows(); |
||
21 | office | 240 | if (LogForm != null) |
241 | { |
||
242 | LogForm.InvokeIfRequired(form => |
||
243 | { |
||
244 | form.logTextBox.Text += "Failed to load windows: " + serializationFailure.Exception.Message + Environment.NewLine; |
||
245 | }); |
||
246 | } |
||
1 | office | 247 | break; |
248 | } |
||
249 | } |
||
250 | |||
251 | #endregion |
||
21 | office | 252 | |
253 | private void ShowLogToolStripMenuItem_Click(object sender, EventArgs e) |
||
254 | { |
||
255 | if (LogForm != null) |
||
256 | { |
||
257 | return; |
||
258 | } |
||
259 | |||
260 | LogForm = new LogForm(); |
||
261 | LogForm.Closed += LogForm_Closed; |
||
262 | LogForm.Show(); |
||
263 | } |
||
264 | |||
265 | private void LogForm_Closed(object sender, EventArgs e) |
||
266 | { |
||
267 | LogForm.Closed -= LogForm_Closed; |
||
268 | LogForm.Dispose(); |
||
269 | LogForm = null; |
||
270 | } |
||
1 | office | 271 | } |
272 | } |