1 |
#region Logging Defines |
2 |
// include this any class or method that required logging, and comment-out what is not needed |
3 |
#define LOGGING_ENABLED // this is only valid within the logger class |
4 |
#region Enabled logging levels |
5 |
#define LOGGING_ENABLE_INFO |
6 |
#define LOGGING_ENABLE_WARN |
7 |
#define LOGGING_ENABLE_DEBUG |
8 |
#define LOGGING_ENABLE_VERBOSEDEBUG |
9 |
#define LOGGING_ENABLE_ERROR |
10 |
#define LOGGING_ENABLE_VERBOSEERROR |
11 |
#define LOGGING_ENABLE_PROFILER |
12 |
#endregion |
13 |
#endregion |
14 |
using System; |
15 |
using System.Collections.Generic; |
16 |
using System.ComponentModel; |
17 |
using System.Data; |
18 |
using System.Drawing; |
19 |
using System.Linq; |
20 |
using System.Text; |
21 |
using System.Windows.Forms; |
22 |
using WeifenLuo.WinFormsUI.Docking; |
23 |
using System.IO; |
24 |
using RomCheater.Logging; |
25 |
using RomCheater.Core; |
26 |
|
27 |
namespace RomCheater.PluginFramework.Core |
28 |
{ |
29 |
public partial class ScratchPad : DockContent |
30 |
{ |
31 |
|
32 |
private UserControlPlugin plugin; |
33 |
private bool PostInitDone = false; |
34 |
public ScratchPad(UserControlPlugin plugin) |
35 |
{ |
36 |
this.plugin = plugin; |
37 |
InitPluginFramework(); |
38 |
InitializeComponent(); |
39 |
this.PerformPreInit(); |
40 |
} |
41 |
|
42 |
|
43 |
private void InitPluginFramework() |
44 |
{ |
45 |
if (this.plugin == null) { return; } |
46 |
this.plugin.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(plugin_OnSelectedProcessChanged); |
47 |
this.plugin.OnSelectedConfigChanged += new BaseEventHandler<ConfigChangedEventArgs>(plugin_OnSelectedConfigChanged); |
48 |
this.plugin.OnPEDataUpdated += new BaseEventHandler<PEViewerDataUpdatedEventArgs>(plugin_OnPEDataUpdated); |
49 |
RaisePluginFrameworkEvents(); |
50 |
} |
51 |
|
52 |
bool EventsRaised = false; |
53 |
private void RaisePluginFrameworkEvents() |
54 |
{ |
55 |
|
56 |
if (this.plugin == null) { EventsRaised = true; return; } |
57 |
if (!EventsRaised) |
58 |
{ |
59 |
this.plugin.RaisePluginFrameworkEvents(); |
60 |
EventsRaised = true; |
61 |
} |
62 |
} |
63 |
|
64 |
void plugin_OnPEDataUpdated(PEViewerDataUpdatedEventArgs e) |
65 |
{ |
66 |
//logger.Warn.WriteLine("plugin_OnPEDataUpdated::has not been implemented!"); |
67 |
} |
68 |
void plugin_OnSelectedConfigChanged(ConfigChangedEventArgs e) |
69 |
{ |
70 |
//logger.Warn.WriteLine("plugin_OnSelectedConfigChanged::has not been implemented!"); |
71 |
} |
72 |
void plugin_OnSelectedProcessChanged(ProcessChangedEventArgs e) |
73 |
{ |
74 |
//logger.Warn.WriteLine("plugin_OnSelectedProcessChanged::has not been implemented!"); |
75 |
} |
76 |
|
77 |
|
78 |
private void PerformPreInit() |
79 |
{ |
80 |
txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Italic); |
81 |
txtScratchPad.ForeColor = SystemColors.GrayText; |
82 |
StringBuilder builder = new StringBuilder(); |
83 |
txtScratchPad.Clear(); |
84 |
|
85 |
builder.AppendFormat(System.Environment.NewLine); |
86 |
builder.AppendFormat("\tThis is a scratchpad"); |
87 |
builder.AppendFormat(System.Environment.NewLine); |
88 |
builder.AppendFormat("\tYou can type anything in here, and save it for later or load an existing file."); |
89 |
|
90 |
txtScratchPad.AppendText(builder.ToString()); |
91 |
} |
92 |
|
93 |
|
94 |
private void PerformSaveOperation() |
95 |
{ |
96 |
} |
97 |
|
98 |
private void mnuItemSave_Click(object sender, EventArgs e) |
99 |
{ |
100 |
PerformPostInit(); |
101 |
DialogResult result = ScracthPadSaver.ShowDialog(); |
102 |
if (result != DialogResult.OK) { return; } |
103 |
try |
104 |
{ |
105 |
using (FileStream fs = new FileStream(ScracthPadSaver.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) |
106 |
{ |
107 |
try |
108 |
{ |
109 |
using (StreamWriter sw = new StreamWriter(fs)) |
110 |
{ |
111 |
string line = string.Empty; |
112 |
using (StringReader sr = new StringReader(txtScratchPad.Text)) |
113 |
{ |
114 |
while ((line = sr.ReadLine()) != null) |
115 |
{ |
116 |
sw.WriteLine(line); |
117 |
} |
118 |
} |
119 |
sw.Flush(); |
120 |
sw.Close(); |
121 |
} |
122 |
} |
123 |
catch (Exception ex) |
124 |
{ |
125 |
logger.Error.WriteLine("Failed to save file: {0}", ScracthPadSaver.FileName); |
126 |
logger.VerboseError.WriteLine(ex.ToString()); |
127 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
128 |
return; |
129 |
} |
130 |
} |
131 |
MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
132 |
} |
133 |
catch (Exception ex) |
134 |
{ |
135 |
logger.Error.WriteLine("Failed to save file: {0}", ScracthPadSaver.FileName); |
136 |
logger.VerboseError.WriteLine(ex.ToString()); |
137 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
138 |
} |
139 |
} |
140 |
|
141 |
private void mnuItemOpen_Click(object sender, EventArgs e) |
142 |
{ |
143 |
PerformPostInit(); |
144 |
DialogResult result = ScracthPadLoader.ShowDialog(); |
145 |
if (result != DialogResult.OK) { return; } |
146 |
txtScratchPad.Clear(); |
147 |
|
148 |
try |
149 |
{ |
150 |
using (FileStream fs = new FileStream(ScracthPadLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) |
151 |
{ |
152 |
try |
153 |
{ |
154 |
using (StreamReader sr = new StreamReader(fs)) |
155 |
{ |
156 |
txtScratchPad.AppendText(sr.ReadToEnd()); |
157 |
} |
158 |
} |
159 |
catch (Exception ex) |
160 |
{ |
161 |
logger.Error.WriteLine("Failed to open file: {0}", ScracthPadLoader.FileName); |
162 |
logger.VerboseError.WriteLine(ex.ToString()); |
163 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
164 |
return; |
165 |
} |
166 |
MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
167 |
} |
168 |
} |
169 |
catch (Exception ex) |
170 |
{ |
171 |
logger.Error.WriteLine("Failed to open file: {0}", ScracthPadLoader.FileName); |
172 |
logger.VerboseError.WriteLine(ex.ToString()); |
173 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
174 |
} |
175 |
} |
176 |
|
177 |
|
178 |
private void PerformPostInit() |
179 |
{ |
180 |
if (!PostInitDone) |
181 |
{ |
182 |
txtScratchPad.Clear(); |
183 |
txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Regular); |
184 |
txtScratchPad.ForeColor = Color.Black; |
185 |
PostInitDone = true; |
186 |
} |
187 |
} |
188 |
private void txtScratchPad_MouseDown(object sender, MouseEventArgs e) |
189 |
{ |
190 |
PerformPostInit(); |
191 |
} |
192 |
|
193 |
private void txtScratchPad_KeyDown(object sender, KeyEventArgs e) |
194 |
{ |
195 |
PerformPostInit(); |
196 |
} |
197 |
|
198 |
private void mnuItemUndo_Click(object sender, EventArgs e) |
199 |
{ |
200 |
PerformPostInit(); |
201 |
txtScratchPad.Focus(); |
202 |
SendKeys.Send("^Z"); |
203 |
} |
204 |
|
205 |
private void mnuItemRedo_Click(object sender, EventArgs e) |
206 |
{ |
207 |
PerformPostInit(); |
208 |
txtScratchPad.Focus(); |
209 |
SendKeys.Send("^Y"); |
210 |
} |
211 |
|
212 |
private void mnuItemClear_Click(object sender, EventArgs e) |
213 |
{ |
214 |
txtScratchPad.Clear(); |
215 |
} |
216 |
|
217 |
private void mnuItemCopy_Click(object sender, EventArgs e) |
218 |
{ |
219 |
PerformPostInit(); |
220 |
string text = txtScratchPad.Text; |
221 |
text = text.Replace("\n", System.Environment.NewLine); |
222 |
Clipboard.SetText(text); |
223 |
} |
224 |
|
225 |
private void mnuItemPaste_Click(object sender, EventArgs e) |
226 |
{ |
227 |
PerformPostInit(); |
228 |
string text = Clipboard.GetText(); |
229 |
txtScratchPad.Text = text; |
230 |
} |
231 |
|
232 |
private void ScratchPad_FormClosing(object sender, FormClosingEventArgs e) |
233 |
{ |
234 |
if (txtScratchPad.Text != string.Empty && PostInitDone) |
235 |
{ |
236 |
DialogResult result = MessageBox.Show("Would you like to save the ScratchPad data before Closing?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); |
237 |
if (result == DialogResult.Cancel) |
238 |
{ |
239 |
e.Cancel = true; |
240 |
return; |
241 |
} |
242 |
if (result == DialogResult.Yes) |
243 |
{ |
244 |
mnuItemSave.PerformClick(); |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
private void ScratchPad_Deactivate<T>(object sender, T e) where T: EventArgs |
250 |
{ |
251 |
try |
252 |
{ |
253 |
FormClosingEventArgs args = (e as FormClosingEventArgs); |
254 |
ScratchPad_FormClosing(sender, args); |
255 |
} |
256 |
catch { } |
257 |
} |
258 |
|
259 |
private void txtScratchPad_LinkClicked(object sender, LinkClickedEventArgs e) |
260 |
{ |
261 |
//System.Diagnostics.Process.Start(e.LinkText); |
262 |
if (this.plugin != null) |
263 |
{ |
264 |
var p = this.plugin.WebBrowserInterface; |
265 |
if (p != null) |
266 |
{ |
267 |
p.Navigate(e.LinkText); |
268 |
} |
269 |
else |
270 |
{ |
271 |
logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); |
272 |
logger.Debug.WriteLine("Could not obtain a handle to the WebBrowser Provider's Interface."); |
273 |
} |
274 |
} |
275 |
else |
276 |
{ |
277 |
logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); |
278 |
logger.Debug.WriteLine("Loaded plugin is null"); |
279 |
} |
280 |
} |
281 |
} |
282 |
} |