Widow – Blame information for rev 18

Subversion Repositories:
Rev:
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; }
32  
1 office 33 #endregion
34  
35 #region Constructors, Destructors and Finalizers
36  
6 office 37 public MainForm()
1 office 38 {
39 InitializeComponent();
15 office 40 AutoUpdater.Start("http://widow.grimore.org/update/update.xml");
6 office 41  
1 office 42 // Bind to settings changed event.
43 Settings.Default.SettingsLoaded += Default_SettingsLoaded;
44 Settings.Default.SettingsSaving += Default_SettingsSaving;
45 Settings.Default.PropertyChanged += Default_PropertyChanged;
46  
47 Natives.RegisterShellHookWindow(Handle);
48  
18 office 49 WindowManipulation = new WindowManipulation(Settings.Default, this);
50  
6 office 51 LoadWindows().ContinueWith(task =>
52 {
18 office 53 WindowManipulation.AddWindows(Windows);
6 office 54 });
1 office 55 }
56  
57 /// <summary>
58 /// Clean up any resources being used.
59 /// </summary>
60 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
61 protected override void Dispose(bool disposing)
62 {
63 if (disposing && components != null)
64 {
65 Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
66 Settings.Default.SettingsSaving -= Default_SettingsSaving;
67 Settings.Default.PropertyChanged -= Default_PropertyChanged;
68  
69 components.Dispose();
70 }
71  
72 base.Dispose(disposing);
73 }
74  
75 #endregion
76  
77 #region Private Overrides
78  
79 protected override void WndProc(ref Message m)
80 {
81 var handle = m.LParam;
82 var windowName = Helpers.GetWindowTitle(handle);
83  
84 switch (m.WParam.ToInt32())
85 {
86 case (int) Natives.WM.HSHELL_WINDOWCREATED:
87 WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle));
88 break;
89 case (int) Natives.WM.HSHELL_WINDOWDESTROYED:
90 WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle));
91 break;
92 }
93  
94 base.WndProc(ref m);
95 }
96  
97 #endregion
98  
99 #region Event Handlers
100  
8 office 101 private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
1 office 102 {
103 Settings.Default.Save();
104 }
105  
8 office 106 private static void Default_SettingsSaving(object sender, CancelEventArgs e)
1 office 107 {
108 }
109  
8 office 110 private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
1 office 111 {
112 }
113  
114 private void NewToolStripMenuItem_Click(object sender, EventArgs e)
115 {
11 office 116 if (RuleEditForm != null)
117 {
118 return;
119 }
120  
1 office 121 RuleEditForm = new RuleEditForm(this, Windows);
122 RuleEditForm.Closed += RuleEditForm_Closed;
123 RuleEditForm.Show();
124 }
125  
126 private async void RuleEditForm_Closed(object sender, EventArgs e)
127 {
128 RuleEditForm.Closed -= RuleEditForm_Closed;
129 RuleEditForm.Dispose();
130 RuleEditForm = null;
131  
132 switch (await WindowsSerialization.Serialize(Windows, "Windows.xml"))
133 {
134 case SerializationSuccess serializationSuccess:
135 break;
136 case SerializationFailure serializationFailure:
137 break;
138 }
139 }
140  
141 private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
142 {
143 Application.Exit();
144 }
145  
146 private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
147 {
148 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
149  
150 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
151 }
152  
5 office 153 private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
154 {
155 Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked;
156  
9 office 157 WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate;
5 office 158 }
159  
160 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
161 {
162 // Show the about form.
163 AboutForm = new AboutForm();
164 AboutForm.Show();
165 }
166  
11 office 167 private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e)
7 office 168 {
11 office 169 await WindowManipulation.Apply();
7 office 170 }
171  
8 office 172 private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
173 {
174 var toolStripMenuItem = (ToolStripMenuItem) sender;
175  
176 if (!int.TryParse(toolStripTextBox1.Text, out var time))
177 {
178 return;
179 }
180  
181 Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
182 Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture);
183  
9 office 184 WindowManipulation.ApplyEveryTime = time;
185 WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
8 office 186 }
187  
13 office 188 private void NotifyIcon1_DoubleClick(object sender, EventArgs e)
189 {
190 if (RuleEditForm != null)
191 {
192 return;
193 }
194  
195 RuleEditForm = new RuleEditForm(this, Windows);
196 RuleEditForm.Closed += RuleEditForm_Closed;
197 RuleEditForm.Show();
198 }
199  
200 private async void NotifyIcon1_Click(object sender, EventArgs e)
201 {
202 await WindowManipulation.Apply();
203 }
204  
1 office 205 #endregion
206  
207 #region Private Methods
208  
209 private async Task LoadWindows()
210 {
211 switch (await WindowsSerialization.Deserialize("Windows.xml"))
212 {
213 case SerializationSuccess serializationSuccess:
214 Windows = serializationSuccess.Windows;
215 break;
216 case SerializationFailure serializationFailure:
217 Windows = new Windows.Windows();
218 break;
219 }
220 }
221  
222 #endregion
223 }
224 }