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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 696 - (hide annotations) (download)
Mon Jun 17 11:47:23 2013 UTC (7 years, 10 months ago) by william
File size: 10270 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22