29 |
{ |
{ |
30 |
public partial class ScratchPad : DockContent |
public partial class ScratchPad : DockContent |
31 |
{ |
{ |
32 |
|
List<ScratchPadDocument> docuemntList = new List<ScratchPadDocument>(); |
33 |
private UserControlPlugin plugin; |
private UserControlPlugin plugin; |
34 |
private bool PostInitDone = false; |
private bool PostInitDone = false; |
35 |
public ScratchPad(UserControlPlugin plugin) |
public ScratchPad(UserControlPlugin plugin) |
36 |
{ |
{ |
37 |
this.plugin = plugin; |
this.plugin = plugin; |
38 |
InitPluginFramework(); |
InitPluginFramework(); |
39 |
InitializeComponent(); |
InitializeComponent(); |
|
this.PerformPreInit(); |
|
40 |
} |
} |
41 |
|
|
42 |
|
|
75 |
} |
} |
76 |
|
|
77 |
|
|
|
private void PerformPreInit() |
|
|
{ |
|
|
txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Italic); |
|
|
txtScratchPad.ForeColor = SystemColors.GrayText; |
|
|
StringBuilder builder = new StringBuilder(); |
|
|
txtScratchPad.Clear(); |
|
|
|
|
|
builder.AppendFormat(System.Environment.NewLine); |
|
|
builder.AppendFormat("\tThis is a scratchpad"); |
|
|
builder.AppendFormat(System.Environment.NewLine); |
|
|
builder.AppendFormat("\tYou can type anything in here, and save it for later or load an existing file."); |
|
|
|
|
|
txtScratchPad.AppendText(builder.ToString()); |
|
|
} |
|
78 |
|
|
79 |
|
|
|
private void PerformSaveOperation() |
|
|
{ |
|
|
} |
|
|
|
|
|
private void mnuItemSave_Click(object sender, EventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
DialogResult result = ScracthPadSaver.ShowDialog(); |
|
|
if (result != DialogResult.OK) { return; } |
|
|
try |
|
|
{ |
|
|
using (FileStream fs = new FileStream(ScracthPadSaver.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
using (StreamWriter sw = new StreamWriter(fs)) |
|
|
{ |
|
|
string line = string.Empty; |
|
|
using (StringReader sr = new StringReader(txtScratchPad.Text)) |
|
|
{ |
|
|
while ((line = sr.ReadLine()) != null) |
|
|
{ |
|
|
sw.WriteLine(line); |
|
|
} |
|
|
} |
|
|
sw.Flush(); |
|
|
sw.Close(); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
logger.Error.WriteLine("Failed to save file: {0}", ScracthPadSaver.FileName); |
|
|
logger.VerboseError.WriteLine(ex.ToString()); |
|
|
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
|
|
return; |
|
|
} |
|
|
} |
|
|
MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
logger.Error.WriteLine("Failed to save file: {0}", ScracthPadSaver.FileName); |
|
|
logger.VerboseError.WriteLine(ex.ToString()); |
|
|
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
|
|
} |
|
|
} |
|
80 |
|
|
|
private void mnuItemOpen_Click(object sender, EventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
DialogResult result = ScracthPadLoader.ShowDialog(); |
|
|
if (result != DialogResult.OK) { return; } |
|
|
txtScratchPad.Clear(); |
|
81 |
|
|
|
try |
|
|
{ |
|
|
using (FileStream fs = new FileStream(ScracthPadLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
using (StreamReader sr = new StreamReader(fs)) |
|
|
{ |
|
|
txtScratchPad.AppendText(sr.ReadToEnd()); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
logger.Error.WriteLine("Failed to open file: {0}", ScracthPadLoader.FileName); |
|
|
logger.VerboseError.WriteLine(ex.ToString()); |
|
|
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
|
|
return; |
|
|
} |
|
|
//MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
logger.Error.WriteLine("Failed to open file: {0}", ScracthPadLoader.FileName); |
|
|
logger.VerboseError.WriteLine(ex.ToString()); |
|
|
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
|
|
} |
|
82 |
|
|
|
txtScratchPad.SelectionStart = 0; |
|
|
txtScratchPad.ScrollToCaret(); |
|
83 |
|
|
|
} |
|
84 |
|
|
85 |
|
|
|
private void PerformPostInit() |
|
|
{ |
|
|
if (!PostInitDone) |
|
|
{ |
|
|
txtScratchPad.Clear(); |
|
|
txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Regular); |
|
|
txtScratchPad.ForeColor = Color.Black; |
|
|
PostInitDone = true; |
|
|
} |
|
|
} |
|
|
private void txtScratchPad_MouseDown(object sender, MouseEventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
} |
|
|
|
|
|
private void txtScratchPad_KeyDown(object sender, KeyEventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
} |
|
|
|
|
|
private void mnuItemUndo_Click(object sender, EventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
txtScratchPad.Focus(); |
|
|
SendKeys.Send("^Z"); |
|
|
} |
|
|
|
|
|
private void mnuItemRedo_Click(object sender, EventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
txtScratchPad.Focus(); |
|
|
SendKeys.Send("^Y"); |
|
|
} |
|
|
|
|
|
private void mnuItemClear_Click(object sender, EventArgs e) |
|
|
{ |
|
|
txtScratchPad.Clear(); |
|
|
} |
|
|
|
|
|
private void mnuItemCopy_Click(object sender, EventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
string text = txtScratchPad.Text; |
|
|
text = text.Replace("\n", System.Environment.NewLine); |
|
|
Clipboard.SetText(text); |
|
|
} |
|
|
|
|
|
private void mnuItemPaste_Click(object sender, EventArgs e) |
|
|
{ |
|
|
PerformPostInit(); |
|
|
string text = Clipboard.GetText(); |
|
|
txtScratchPad.Text = text; |
|
|
} |
|
86 |
|
|
87 |
private void ScratchPad_FormClosing(object sender, FormClosingEventArgs e) |
private void ScratchPad_FormClosing(object sender, FormClosingEventArgs e) |
88 |
{ |
{ |
89 |
if (txtScratchPad.Text != string.Empty && PostInitDone) |
//if (txtScratchPad.Text != string.Empty && PostInitDone) |
90 |
|
//{ |
91 |
|
// DialogResult result = MessageBox.Show("Would you like to save the ScratchPad data before Closing?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); |
92 |
|
// if (result == DialogResult.Cancel) |
93 |
|
// { |
94 |
|
// e.Cancel = true; |
95 |
|
// return; |
96 |
|
// } |
97 |
|
// if (result == DialogResult.Yes) |
98 |
|
// { |
99 |
|
// mnuItemSave.PerformClick(); |
100 |
|
// } |
101 |
|
//} |
102 |
|
|
103 |
|
for (int i = 0; i < docuemntList.Count; i++) |
104 |
{ |
{ |
105 |
DialogResult result = MessageBox.Show("Would you like to save the ScratchPad data before Closing?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); |
var Document = docuemntList[i]; |
106 |
if (result == DialogResult.Cancel) |
Document.OnDocumentQuit(); |
|
{ |
|
|
e.Cancel = true; |
|
|
return; |
|
|
} |
|
|
if (result == DialogResult.Yes) |
|
|
{ |
|
|
mnuItemSave.PerformClick(); |
|
|
} |
|
107 |
} |
} |
108 |
|
|
109 |
} |
} |
110 |
|
|
111 |
private void ScratchPad_Deactivate<T>(object sender, T e) where T: EventArgs |
private void ScratchPad_Deactivate<T>(object sender, T e) where T: EventArgs |
118 |
catch { } |
catch { } |
119 |
} |
} |
120 |
|
|
121 |
private void txtScratchPad_LinkClicked(object sender, LinkClickedEventArgs e) |
private void mnuItemNew_Click(object sender, EventArgs e) |
122 |
{ |
{ |
123 |
//System.Diagnostics.Process.Start(e.LinkText); |
// create new document |
124 |
if (this.plugin != null) |
int tp_index = this.tb.TabPages.Count; |
125 |
{ |
ScratchPadDocument t = new ScratchPadDocument(tp_index); |
126 |
var p = this.plugin.WebBrowserInterface; |
docuemntList.Add(t); |
127 |
if (p != null) |
this.tb.TabPages.Add(t.DocumentTab); |
128 |
{ |
} |
129 |
p.Navigate(e.LinkText); |
|
130 |
} |
//private void txtScratchPad_LinkClicked(object sender, LinkClickedEventArgs e) |
131 |
else |
//{ |
132 |
{ |
// //System.Diagnostics.Process.Start(e.LinkText); |
133 |
logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); |
// if (this.plugin != null) |
134 |
logger.Debug.WriteLine("Could not obtain a handle to the WebBrowser Provider's Interface."); |
// { |
135 |
} |
// var p = this.plugin.WebBrowserInterface; |
136 |
} |
// if (p != null) |
137 |
else |
// { |
138 |
{ |
// p.Navigate(e.LinkText); |
139 |
logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); |
// } |
140 |
logger.Debug.WriteLine("Loaded plugin is null"); |
// else |
141 |
} |
// { |
142 |
} |
// logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); |
143 |
|
// logger.Debug.WriteLine("Could not obtain a handle to the WebBrowser Provider's Interface."); |
144 |
|
// } |
145 |
|
// } |
146 |
|
// else |
147 |
|
// { |
148 |
|
// logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); |
149 |
|
// logger.Debug.WriteLine("Loaded plugin is null"); |
150 |
|
// } |
151 |
|
//} |
152 |
} |
} |
153 |
} |
} |