/My Notepad/MainWindow.cs

http://mynotepad.codeplex.com · C# · 302 lines · 262 code · 36 blank · 4 comment · 48 complexity · 5b6d41ae25a973674cecefda0488515f MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using WeifenLuo.WinFormsUI.Docking;
  5. using System.Collections.Generic;
  6. using System.Drawing.Printing;
  7. namespace MyNotepad
  8. {
  9. public partial class MainWindow : Form
  10. {
  11. public string[] Args;
  12. TabbedNote activeNote = null;
  13. public MainWindow()
  14. {
  15. InitializeComponent();
  16. }
  17. public MainWindow(string file)
  18. {
  19. InitializeComponent();
  20. }
  21. public delegate void ProcessParametersDelegate(object sender, string[] args);
  22. public void ProcessParameters(object sender, string[] args)
  23. {
  24. // The form has loaded, and initialization will have been be done.
  25. // Add the command-line arguments to our textbox, just to confirm that
  26. // it reached here.
  27. if (args.Length == 0)
  28. {
  29. TabbedNote note = new TabbedNote();
  30. note.Show(mainDockPanel);
  31. }
  32. else
  33. {
  34. foreach (string file in args)
  35. {
  36. FileInfo f = new FileInfo(file);
  37. TabbedNote note = new TabbedNote(f);
  38. note.Show(this.mainDockPanel);
  39. this.Focus();
  40. }
  41. }
  42. }
  43. private void NewTabbedNote(object sender, EventArgs e)
  44. {
  45. TabbedNote note = new TabbedNote();
  46. note.Show(this.mainDockPanel);
  47. }
  48. private void OpenTabbedNote(object sender, EventArgs e)
  49. {
  50. OpenFileDialog open = new OpenFileDialog();
  51. open.Title = "Open...";
  52. string filter = "";
  53. filter += "All Files (*.*)|*.*|";
  54. filter += "C Files (*.c)|*.c|";
  55. filter += "C++ Files (*.cpp)|*.cpp|";
  56. filter += "C# Files (*.cs)|*.cs|";
  57. filter += "Java Files (*.java)|*.java|";
  58. filter += "HTML Files (*.html)|*.html|";
  59. filter += "Text Files (*.txt)|*.txt";
  60. open.Filter = filter;
  61. open.DefaultExt = ".txt";
  62. open.Multiselect = true;
  63. if (open.ShowDialog() == DialogResult.OK)
  64. {
  65. FileInfo f;
  66. foreach (String s in open.FileNames)
  67. {
  68. f = new FileInfo(s);
  69. TabbedNote note = new TabbedNote(f);
  70. note.Show(this.mainDockPanel);
  71. }
  72. }
  73. }
  74. private void SaveCurrentNote(object sender, EventArgs e)
  75. {
  76. if (activeNote != null)
  77. activeNote.Save();
  78. }
  79. private void SaveFileAs(object sender, EventArgs e)
  80. {
  81. if (activeNote != null)
  82. activeNote.SaveFileAs();
  83. }
  84. private void MyNotepadExit(object sender, EventArgs e)
  85. {
  86. this.Dispose();
  87. }
  88. private void Exiting()
  89. {
  90. DockContentCollection openContents = mainDockPanel.Contents;
  91. foreach (DockContent c in openContents)
  92. {
  93. TabbedNote note = (TabbedNote)c;
  94. note.Close();
  95. }
  96. }
  97. private void MyNotepadMainFormClosed(object sender, FormClosingEventArgs e)
  98. {
  99. //Exiting();
  100. }
  101. private void MainWindowOpened(object sender, EventArgs e)
  102. {
  103. if (this.Args != null)
  104. {
  105. ProcessParameters(null, this.Args);
  106. this.Args = null;
  107. }
  108. }
  109. private void UndoLastAction(object sender, EventArgs e)
  110. {
  111. if (activeNote != null)
  112. activeNote.ScintillaContent.UndoRedo.Undo();
  113. }
  114. private void RedoLastUndone(object sender, EventArgs e)
  115. {
  116. if (activeNote != null)
  117. activeNote.ScintillaContent.UndoRedo.Redo();
  118. }
  119. private void ContentChanged(object sender, EventArgs e)
  120. {
  121. try
  122. {
  123. activeNote = (TabbedNote)mainDockPanel.ActiveContent;
  124. if (activeNote.Info == null)
  125. {
  126. lblActiveFileInfo.Text = "Document Path: Unsaved";
  127. this.Text = "My Notepad";
  128. }
  129. else
  130. {
  131. lblActiveFileInfo.Text = "Document Path: " + activeNote.Info.FullName;
  132. this.Text = activeNote.Info.Name + " - My Notepad";
  133. }
  134. }
  135. catch (Exception) { }
  136. }
  137. private void CloseTab(object sender, EventArgs e)
  138. {
  139. if (activeNote != null)
  140. activeNote.Close();
  141. }
  142. private void CutText(object sender, EventArgs e)
  143. {
  144. if (activeNote != null)
  145. activeNote.ScintillaContent.Clipboard.Cut();
  146. }
  147. private void CopyText(object sender, EventArgs e)
  148. {
  149. if (activeNote != null)
  150. activeNote.ScintillaContent.Clipboard.Copy();
  151. }
  152. private void PasteText(object sender, EventArgs e)
  153. {
  154. if (activeNote != null)
  155. activeNote.ScintillaContent.Clipboard.Paste();
  156. }
  157. private void SelectAllText(object sender, EventArgs e)
  158. {
  159. if (activeNote != null)
  160. activeNote.ScintillaContent.Selection.SelectAll();
  161. }
  162. private void ShowAboutBox(object sender, EventArgs e)
  163. {
  164. AboutWindow abt = new AboutWindow();
  165. abt.ShowDialog();
  166. }
  167. private void PrintCurrentDocument(object sender, EventArgs e)
  168. {
  169. try
  170. {
  171. if (activeNote != null)
  172. activeNote.ScintillaContent.Printing.Print(true);
  173. }
  174. catch (InvalidPrinterException)
  175. {
  176. MessageBox.Show("You have no printers installed. Please install one.", "My Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
  177. }
  178. }
  179. private void PreviewCurrentDocumentPrinting(object sender, EventArgs e)
  180. {
  181. try
  182. {
  183. if (activeNote != null)
  184. activeNote.ScintillaContent.Printing.PrintPreview();
  185. }
  186. catch (InvalidPrinterException)
  187. {
  188. MessageBox.Show("You have no printers installed. Please install one.", "My Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
  189. }
  190. }
  191. private void Replace(object sender, EventArgs e)
  192. {
  193. if (activeNote != null)
  194. activeNote.ScintillaContent.FindReplace.ShowReplace();
  195. }
  196. private void Find(object sender, EventArgs e)
  197. {
  198. if (activeNote != null)
  199. activeNote.ScintillaContent.FindReplace.ShowFind();
  200. }
  201. private void ImmediatePrint(object sender, EventArgs e)
  202. {
  203. try
  204. {
  205. if (activeNote != null)
  206. activeNote.ScintillaContent.Printing.Print(false);
  207. }
  208. catch (InvalidPrinterException)
  209. {
  210. MessageBox.Show("You have no printers installed. Please install one.", "My Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
  211. }
  212. }
  213. private void ZoomIn(object sender, EventArgs e)
  214. {
  215. if (activeNote != null)
  216. activeNote.ScintillaContent.Zoom++;
  217. }
  218. private void ZoomOut(object sender, EventArgs e)
  219. {
  220. if (activeNote != null)
  221. activeNote.ScintillaContent.Zoom--;
  222. }
  223. private void ResetZoom(object sender, EventArgs e)
  224. {
  225. if (activeNote != null)
  226. activeNote.ScintillaContent.Zoom = 0;
  227. }
  228. private void ToggleLineNumbers(object sender, EventArgs e)
  229. {
  230. if (activeNote != null)
  231. {
  232. if (activeNote.ScintillaContent.Margins[0].Width == 50)
  233. {
  234. activeNote.ScintillaContent.Margins[0].Width = 0;
  235. lineNumbersToolStripMenuItem.Checked = false;
  236. }
  237. else
  238. {
  239. activeNote.ScintillaContent.Margins[0].Width = 50;
  240. lineNumbersToolStripMenuItem.Checked = true;
  241. }
  242. }
  243. }
  244. private void CloseOthers(object sender, EventArgs e)
  245. {
  246. foreach (Form frm in this.MdiChildren)
  247. {
  248. if (frm != activeNote)
  249. frm.Close();
  250. }
  251. }
  252. private void SaveAll(object sender, EventArgs e)
  253. {
  254. DockContentCollection open = mainDockPanel.Contents;
  255. foreach (TabbedNote note in open)
  256. {
  257. note.Save();
  258. }
  259. }
  260. private void CloseAll(object sender, EventArgs e)
  261. {
  262. foreach (Form frm in this.MdiChildren)
  263. frm.Close();
  264. }
  265. }
  266. }