/[RomCheater]/trunk/RomCheater.PluginFramework/Core/ScratchPad.cs
ViewVC logotype

Contents of /trunk/RomCheater.PluginFramework/Core/ScratchPad.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 734 - (show annotations) (download)
Wed Jun 19 15:40:06 2013 UTC (7 years, 1 month ago) by william
File size: 11092 byte(s)
ScratchPad plugin:
* when opening a file, always scroll to the first line

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 txtScratchPad.SelectionStart = 0;
177 txtScratchPad.ScrollToCaret();
178
179 }
180
181
182 private void PerformPostInit()
183 {
184 if (!PostInitDone)
185 {
186 txtScratchPad.Clear();
187 txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Regular);
188 txtScratchPad.ForeColor = Color.Black;
189 PostInitDone = true;
190 }
191 }
192 private void txtScratchPad_MouseDown(object sender, MouseEventArgs e)
193 {
194 PerformPostInit();
195 }
196
197 private void txtScratchPad_KeyDown(object sender, KeyEventArgs e)
198 {
199 PerformPostInit();
200 }
201
202 private void mnuItemUndo_Click(object sender, EventArgs e)
203 {
204 PerformPostInit();
205 txtScratchPad.Focus();
206 SendKeys.Send("^Z");
207 }
208
209 private void mnuItemRedo_Click(object sender, EventArgs e)
210 {
211 PerformPostInit();
212 txtScratchPad.Focus();
213 SendKeys.Send("^Y");
214 }
215
216 private void mnuItemClear_Click(object sender, EventArgs e)
217 {
218 txtScratchPad.Clear();
219 }
220
221 private void mnuItemCopy_Click(object sender, EventArgs e)
222 {
223 PerformPostInit();
224 string text = txtScratchPad.Text;
225 text = text.Replace("\n", System.Environment.NewLine);
226 Clipboard.SetText(text);
227 }
228
229 private void mnuItemPaste_Click(object sender, EventArgs e)
230 {
231 PerformPostInit();
232 string text = Clipboard.GetText();
233 txtScratchPad.Text = text;
234 }
235
236 private void ScratchPad_FormClosing(object sender, FormClosingEventArgs e)
237 {
238 if (txtScratchPad.Text != string.Empty && PostInitDone)
239 {
240 DialogResult result = MessageBox.Show("Would you like to save the ScratchPad data before Closing?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
241 if (result == DialogResult.Cancel)
242 {
243 e.Cancel = true;
244 return;
245 }
246 if (result == DialogResult.Yes)
247 {
248 mnuItemSave.PerformClick();
249 }
250 }
251 }
252
253 private void ScratchPad_Deactivate<T>(object sender, T e) where T: EventArgs
254 {
255 try
256 {
257 FormClosingEventArgs args = (e as FormClosingEventArgs);
258 ScratchPad_FormClosing(sender, args);
259 }
260 catch { }
261 }
262
263 private void txtScratchPad_LinkClicked(object sender, LinkClickedEventArgs e)
264 {
265 //System.Diagnostics.Process.Start(e.LinkText);
266 if (this.plugin != null)
267 {
268 var p = this.plugin.WebBrowserInterface;
269 if (p != null)
270 {
271 p.Navigate(e.LinkText);
272 }
273 else
274 {
275 logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText);
276 logger.Debug.WriteLine("Could not obtain a handle to the WebBrowser Provider's Interface.");
277 }
278 }
279 else
280 {
281 logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText);
282 logger.Debug.WriteLine("Loaded plugin is null");
283 }
284 }
285 }
286 }

  ViewVC Help
Powered by ViewVC 1.1.22