1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Data; |
5 |
using System.Drawing; |
6 |
using System.Text; |
7 |
using System.Windows.Forms; |
8 |
using RomCheater.Logging; |
9 |
using RomCheater.Docking; |
10 |
using WeifenLuo.WinFormsUI.Docking; |
11 |
using System.IO; |
12 |
using RomCheater.PluginFramework.Core; |
13 |
using RomCheater.Core; |
14 |
using RomCheater.UserSettingsSupport; |
15 |
|
16 |
namespace RomCheater.RVAScratchPad |
17 |
{ |
18 |
public partial class Form1 : Form |
19 |
{ |
20 |
private SettingSubscriber SettingsSubscriber = null; |
21 |
private bool m_bSaveLayout = true; |
22 |
PluginLoader loader = null; |
23 |
IConfigPlugin ConfigPlugin = null; |
24 |
|
25 |
IUserControlPlugin RVACalcPlugin = null; |
26 |
IUserControlPlugin ScratchPadPlugin = null; |
27 |
|
28 |
private DeserializeDockContent m_deserializeDockContent; |
29 |
private FloatingLogWindow m_LogWindow = new FloatingLogWindow(); |
30 |
private FloatingWebBrowser m_wb = new FloatingWebBrowser(); |
31 |
|
32 |
#region LogWriterSupport |
33 |
static LogWriter _LoggerInstance; |
34 |
static LogWriter LoggerInstance |
35 |
{ |
36 |
get { return _LoggerInstance; } |
37 |
set { _LoggerInstance = value; } |
38 |
} |
39 |
#endregion |
40 |
|
41 |
public Form1() : this(false) { } |
42 |
public Form1(bool no_console_redirect) |
43 |
{ |
44 |
InitializeComponent(); |
45 |
SettingsSubscriber = new SettingSubscriber(); |
46 |
SettingsSubscriber.AddSubscriber(this, RomCheater.Properties.Settings.Default); |
47 |
load_loggerflags(); |
48 |
SetupDocks(); |
49 |
LoggerInstance = m_LogWindow.Logwriter; |
50 |
LoggerInstance.CreateNewLog(false); |
51 |
logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name); |
52 |
load_plugins(); |
53 |
if (no_console_redirect) |
54 |
LoggerInstance.RedirectConsoleOutput = false; |
55 |
} |
56 |
private void load_loggerflags() |
57 |
{ |
58 |
logger.SetLoggingFlags(Logging.Properties.Settings.Default.LoggingFlags); |
59 |
#if FORCE_ALL_LOGGING_FLAGS |
60 |
logger.SetLoggingFlags(loggerflags.ALL); |
61 |
#endif |
62 |
} |
63 |
private void load_plugins() { load_plugins(false); } |
64 |
private void load_plugins_silent() { load_plugins(true); } |
65 |
private void load_plugins(bool silent) |
66 |
{ |
67 |
loader = new PluginLoader(); |
68 |
loader.LoadPlugins(silent); |
69 |
|
70 |
var LastConfigPlugin = SettingsSubscriber.GetValue("LastConfigPlugin").ToString(); |
71 |
if (LastConfigPlugin != null) |
72 |
{ |
73 |
ConfigPlugin = loader.GetConfigPlugin(LastConfigPlugin.ToString()); |
74 |
} |
75 |
else |
76 |
{ |
77 |
var config = PluginCollection.GetPluginByName(PluginNames.GenericConfig); |
78 |
ConfigPlugin = loader.GetPluginByGuid<IConfigPlugin>(config.ID.ToString()); |
79 |
} |
80 |
var scratchpad = PluginCollection.GetPluginByName(PluginNames.ScratchPadPlugin); |
81 |
var rvacalc = PluginCollection.GetPluginByName(PluginNames.RVACalculatorPlugin); |
82 |
ScratchPadPlugin = loader.GetPluginByGuid<IUserControlPlugin>(scratchpad.ID.ToString()); |
83 |
RVACalcPlugin = loader.GetPluginByGuid<IUserControlPlugin>(rvacalc.ID.ToString()); |
84 |
} |
85 |
|
86 |
|
87 |
|
88 |
#region Dock Support |
89 |
private IDockContent GetContentFromPersistString(string persistString) |
90 |
{ |
91 |
if (persistString == typeof(FloatingLogWindow).ToString()) { return m_LogWindow; } |
92 |
if (persistString == typeof(FloatingWebBrowser).ToString()) { return m_wb; } |
93 |
if (RVACalcPlugin != null) { if (persistString == RVACalcPlugin.IDockContentTypeName) { return RVACalcPlugin.DockContent; } } |
94 |
if (ScratchPadPlugin != null) { if (persistString == ScratchPadPlugin.IDockContentTypeName) { return ScratchPadPlugin.DockContent; } } |
95 |
return null; |
96 |
} |
97 |
public void SetupDocks() |
98 |
{ |
99 |
m_LogWindow = new FloatingLogWindow(); |
100 |
m_LogWindow.CloseButton = false; |
101 |
m_LogWindow.CloseButtonVisible = false; |
102 |
|
103 |
m_wb = new FloatingWebBrowser(); |
104 |
m_wb.CloseButton = false; |
105 |
m_wb.CloseButtonVisible = false; |
106 |
|
107 |
m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString); |
108 |
} |
109 |
#region SetupDockWindowHandler support |
110 |
public void SetupDockWindowHandler() |
111 |
{ |
112 |
SetupLogWindowHandler(); |
113 |
SetupWebBrowserWindowHandler(); |
114 |
SetupPluginWindowHandlers(); |
115 |
} |
116 |
private void SetupPluginWindowHandlers() |
117 |
{ |
118 |
if (RVACalcPlugin != null) |
119 |
{ |
120 |
RVACalcPlugin.Activate(); |
121 |
} |
122 |
if (ScratchPadPlugin != null) |
123 |
{ |
124 |
ScratchPadPlugin.Activate(); |
125 |
} |
126 |
} |
127 |
private void SetupLogWindowHandler() |
128 |
{ |
129 |
if (m_LogWindow == null) return; |
130 |
//m_LogWindow.Shown += new EventHandler(AddDockToWindowList); |
131 |
//m_LogWindow.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
132 |
m_LogWindow.Activate(); |
133 |
} |
134 |
private void SetupWebBrowserWindowHandler() |
135 |
{ |
136 |
if (m_wb == null) return; |
137 |
//m_wb.Shown += new EventHandler(AddDockToWindowList); |
138 |
//m_wb.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
139 |
m_wb.Activate(); |
140 |
} |
141 |
#endregion |
142 |
public void ShowDocks() |
143 |
{ |
144 |
ShowLogWindow(); |
145 |
SetupLogWindowHandler(); |
146 |
ShowWebBrowser(); |
147 |
SetupWebBrowserWindowHandler(); |
148 |
ShowPluginWindows(); |
149 |
SetupPluginWindowHandlers(); |
150 |
|
151 |
if (RVACalcPlugin != null) |
152 |
RVACalcPlugin.Activate(); |
153 |
} |
154 |
public void ShowLogWindow() |
155 |
{ |
156 |
if (m_LogWindow == null || m_LogWindow.IsDisposed) { m_LogWindow = new FloatingLogWindow(); } |
157 |
LoggerInstance = m_LogWindow.Logwriter; |
158 |
LoggerInstance.CreateNewLog(false); |
159 |
m_LogWindow.CloseButton = false; |
160 |
m_LogWindow.CloseButtonVisible = false; |
161 |
m_LogWindow.Show(dockPanel, DockState.DockBottom); |
162 |
} |
163 |
|
164 |
|
165 |
public void ShowWebBrowser() |
166 |
{ |
167 |
//load_plugins_silent(); |
168 |
m_wb = new FloatingWebBrowser(); |
169 |
m_wb.CloseButton = false; |
170 |
m_wb.CloseButtonVisible = false; |
171 |
m_wb.Show(dockPanel); |
172 |
} |
173 |
public void ShowPluginWindows() |
174 |
{ |
175 |
// RVA Calc |
176 |
RVACalcPlugin.Show(dockPanel); |
177 |
// ScratchPad |
178 |
ScratchPadPlugin.Show(dockPanel); |
179 |
} |
180 |
#endregion |
181 |
|
182 |
private void Form1_FormClosing(object sender, FormClosingEventArgs e) |
183 |
{ |
184 |
SettingsSubscriber.SaveSettings(); |
185 |
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "RVAScratchPad-DockPanel.config"); |
186 |
if (m_bSaveLayout) |
187 |
dockPanel.SaveAsXml(configFile); |
188 |
else if (File.Exists(configFile)) |
189 |
File.Delete(configFile); |
190 |
// notify any docked windows of formclosing |
191 |
foreach (var t in this.dockPanel.Contents) |
192 |
{ |
193 |
t.OnDeactivate<FormClosingEventArgs>(e); |
194 |
} |
195 |
} |
196 |
|
197 |
private void Form1_Shown(object sender, EventArgs e) |
198 |
{ |
199 |
//dockPanel.SuspendLayout(true); |
200 |
//ShowDocks(); |
201 |
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "RVAScratchPad-DockPanel.config"); |
202 |
if (File.Exists(configFile)) |
203 |
{ |
204 |
try |
205 |
{ |
206 |
dockPanel.LoadFromXml(configFile, m_deserializeDockContent); |
207 |
SetupDockWindowHandler(); |
208 |
} |
209 |
catch (Exception) |
210 |
{ |
211 |
this.Controls.Remove(dockPanel); |
212 |
dockPanel = new DockPanel(); |
213 |
dockPanel.Dock = DockStyle.Fill; |
214 |
dockPanel.DocumentStyle = DocumentStyle.DockingWindow; |
215 |
this.Controls.Add(dockPanel); |
216 |
ShowDocks(); |
217 |
} |
218 |
} |
219 |
else |
220 |
{ |
221 |
ShowDocks(); |
222 |
} |
223 |
|
224 |
if (RVACalcPlugin != null) |
225 |
RVACalcPlugin.Activate(); |
226 |
//dockPanel.ResumeLayout(true, true); |
227 |
} |
228 |
|
229 |
private void mnuItemExit_Click(object sender, EventArgs e) |
230 |
{ |
231 |
this.Close(); |
232 |
} |
233 |
|
234 |
private void Form1_Load(object sender, EventArgs e) |
235 |
{ |
236 |
SettingsSubscriber.SaveSettings(); |
237 |
} |
238 |
} |
239 |
} |