ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.ScratchPad/ScratchPad.cs
Revision: 762
Committed: Thu Jun 20 20:25:55 2013 UTC (9 years, 9 months ago) by william
File size: 11122 byte(s)
Log Message:
move Scratchpad to it's own plugin

File Contents

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