asciipumper- mspaint style program to create irc ascii art |
git clone git://git.acid.vegas/asciipumper.git |
Log | Files | Refs | Archive | README |
PumpForm.cs (14372B)
1 #region Copyright (c) 2007, PP4L Software 2 /************************************************************************************ 3 4 Copyright 2007, PP4L Software 5 Author: Lampiasis <lampiasis@dvolker.com> 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 21 '***********************************************************************************/ 22 #endregion 23 24 using System; 25 using System.Collections.Generic; 26 using System.ComponentModel; 27 using System.Data; 28 using System.Drawing; 29 using System.Text; 30 using System.Windows.Forms; 31 using System.IO; 32 using System.Net; 33 using System.Text.RegularExpressions; 34 using System.Reflection; 35 using System.Diagnostics; 36 using System.Threading; 37 using System.Drawing.Printing; 38 39 namespace AsciiPumper 40 { 41 public partial class PumpForm : Form 42 { 43 private int childFormNumber = 0; 44 45 public PumpForm() 46 { 47 InitializeComponent(); 48 } 49 50 private void ShowNewForm(object sender, EventArgs e) 51 { 52 // Create a new instance of the child form. 53 MainForm childForm = new MainForm(); 54 // Make it a child of this MDI form before showing it. 55 childForm.MdiParent = this; 56 //childForm.Text = "(Untitled)"; 57 childForm.FileName = "(Untitled " + ++childFormNumber + ")"; 58 childForm.FileModified = false; 59 childForm.UndoChanged += new EventHandler<MainForm.UndoChangedEventArgs>(childForm_UndoChanged); 60 childForm.RedoChanged += new EventHandler<MainForm.RedoChangedEventArgs>(childForm_RedoChanged); 61 childForm.Show(); 62 } 63 64 void childForm_RedoChanged(object sender, MainForm.RedoChangedEventArgs e) 65 { 66 if (sender is MainForm) 67 { 68 MainForm form = (MainForm)sender; 69 70 if (form.RedoList.Count > 0) 71 redoToolStripMenuItem.Enabled = true; 72 else 73 redoToolStripMenuItem.Enabled = false; 74 } 75 } 76 77 void childForm_UndoChanged(object sender, MainForm.UndoChangedEventArgs e) 78 { 79 if (sender is MainForm) 80 { 81 MainForm form = (MainForm)sender; 82 83 if (form.UndoList.Count > 0) 84 undoToolStripMenuItem.Enabled = true; 85 else 86 undoToolStripMenuItem.Enabled = false; 87 } 88 } 89 90 private void OpenFile(object sender, EventArgs e) 91 { 92 OpenFileDialog openFileDialog = new OpenFileDialog(); 93 openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 94 openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; 95 if (openFileDialog.ShowDialog(this) == DialogResult.OK) 96 { 97 OpenFileByName(openFileDialog.FileName); 98 } 99 } 100 101 private void OpenFileByName(string fn) 102 { 103 104 Cursor cur = Cursor.Current; 105 Cursor.Current = Cursors.WaitCursor; 106 try 107 { 108 Stream stream = File.OpenRead(fn); 109 MainForm form = new MainForm(); 110 111 form.LoadStream(stream); 112 113 stream.Close(); 114 form.FileName = fn; 115 form.MdiParent = this; 116 form.FileModified = false; 117 form.UndoChanged += new EventHandler<MainForm.UndoChangedEventArgs>(childForm_UndoChanged); 118 form.RedoChanged += new EventHandler<MainForm.RedoChangedEventArgs>(childForm_RedoChanged); 119 form.Show(); 120 121 } 122 finally 123 { 124 Cursor.Current = cur; 125 } 126 AddFileToRecentFiles(fn); 127 } 128 129 private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e) 130 { 131 if (this.ActiveMdiChild == null) 132 { 133 MessageBox.Show(this, "No files are loaded."); 134 return; 135 } 136 SaveFileDialog saveFileDialog = new SaveFileDialog(); 137 saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 138 saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; 139 if (saveFileDialog.ShowDialog(this) == DialogResult.OK) 140 { 141 Stream stream = saveFileDialog.OpenFile(); 142 StreamWriter writer = new StreamWriter(stream); 143 if (this.ActiveMdiChild is MainForm) 144 { 145 ((MainForm)this.ActiveMdiChild).FileName = saveFileDialog.FileName; 146 ((MainForm)this.ActiveMdiChild).FileModified = false; 147 writer.Write(((MainForm)this.ActiveMdiChild).GetIRCString(false)); 148 writer.Close(); 149 } 150 151 AddFileToRecentFiles(saveFileDialog.FileName); 152 } 153 } 154 155 private void ExitToolsStripMenuItem_Click(object sender, EventArgs e) 156 { 157 Application.Exit(); 158 } 159 160 private void CutToolStripMenuItem_Click(object sender, EventArgs e) 161 { 162 // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard 163 } 164 165 private void CopyToolStripMenuItem_Click(object sender, EventArgs e) 166 { 167 // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard 168 } 169 170 private void PasteToolStripMenuItem_Click(object sender, EventArgs e) 171 { 172 // TODO: Use System.Windows.Forms.Clipboard.GetText() or System.Windows.Forms.GetData to retrieve information from the clipboard. 173 } 174 175 private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e) 176 { 177 toolStrip.Visible = toolBarToolStripMenuItem.Checked; 178 } 179 180 private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e) 181 { 182 statusStrip.Visible = statusBarToolStripMenuItem.Checked; 183 } 184 185 private void CascadeToolStripMenuItem_Click(object sender, EventArgs e) 186 { 187 LayoutMdi(MdiLayout.Cascade); 188 } 189 190 private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e) 191 { 192 LayoutMdi(MdiLayout.TileVertical); 193 } 194 195 private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e) 196 { 197 LayoutMdi(MdiLayout.TileHorizontal); 198 } 199 200 private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e) 201 { 202 LayoutMdi(MdiLayout.ArrangeIcons); 203 } 204 205 private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e) 206 { 207 foreach (Form childForm in MdiChildren) 208 { 209 childForm.Close(); 210 } 211 } 212 213 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) 214 { 215 (new AboutBox1()).ShowDialog(this); 216 } 217 218 private void optionsToolStripMenuItem_Click(object sender, EventArgs e) 219 { 220 OptionsForm optionsform = new OptionsForm(); 221 optionsform.SettingsSaved += new EventHandler<OptionsForm.SettingsSavedEventArgs>(optionsform_SettingsSaved); 222 optionsform.ShowDialog(this); 223 224 } 225 226 private void AddFileToRecentFiles(string fn) 227 { 228 Program.Settings.RecentFiles.Remove(fn); 229 Program.Settings.RecentFiles.Add(fn); 230 while (Program.Settings.RecentFiles.Count > 10) 231 { 232 Program.Settings.RecentFiles.RemoveAt(0); 233 } 234 ReloadRecentFiles(); 235 } 236 237 void optionsform_SettingsSaved(object sender, OptionsForm.SettingsSavedEventArgs e) 238 { 239 foreach (Form form in this.MdiChildren) 240 { 241 if (form is MainForm) 242 { 243 ((MainForm)form).RepaintCanvas(); 244 } 245 246 } 247 } 248 249 private void ReloadRecentFiles() 250 { 251 openRecentToolStripMenuItem.DropDownItems.Clear(); 252 if (Program.Settings.RecentFiles == null) 253 { 254 Program.Settings.RecentFiles = new System.Collections.Specialized.StringCollection(); 255 } 256 for (int i = Program.Settings.RecentFiles.Count - 1; i >= 0; i--) 257 { 258 openRecentToolStripMenuItem.DropDownItems.Add(Program.Settings.RecentFiles[i], null, new EventHandler(OpenRecentHandler)); 259 } 260 261 } 262 263 private void PumpForm_Load(object sender, EventArgs e) 264 { 265 new Thread(new ThreadStart(this.CheckForNewVersion)).Start(); 266 267 ReloadRecentFiles(); 268 269 string[] args = Environment.GetCommandLineArgs(); 270 if ( args.Length > 1 ) 271 OpenFileByName(args[1]); 272 else 273 ShowNewForm(this, EventArgs.Empty); 274 } 275 276 private void OpenRecentHandler(object sender, EventArgs e) 277 { 278 ToolStripMenuItem menu = (ToolStripMenuItem) sender; 279 280 // see if file is open already 281 foreach (Form form in this.MdiChildren) 282 { 283 if (form is MainForm) 284 { 285 if (((MainForm)form).FileName == menu.Text) 286 { 287 form.Activate(); 288 return; 289 } 290 } 291 } 292 293 // open file 294 OpenFileByName(menu.Text); 295 } 296 297 private void CheckForNewVersion() 298 { 299 // this is really non-critical code so let's discard all exceptions and go on with life ok? 300 try 301 { 302 WebRequest request = WebRequest.Create("http://code.google.com/p/asciipumper"); 303 304 // Get the response. 305 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 306 // Get the stream containing content returned by the server. 307 Stream dataStream = response.GetResponseStream(); 308 // Open the stream using a StreamReader for easy access. 309 StreamReader reader = new StreamReader(dataStream); 310 // Read the content. 311 string responseFromServer = reader.ReadToEnd(); 312 Regex versionregex = new Regex(@"Latest release version: (\d+)\.(\d+)\.(\d+)\.(\d+)", RegexOptions.Multiline); 313 MatchCollection matches = versionregex.Matches(responseFromServer); 314 if (matches.Count >= 1 && matches[0].Captures.Count >= 1) 315 { 316 int v1, v2, v3, v4; 317 v1 = int.Parse(matches[0].Groups[1].Value); 318 v2 = int.Parse(matches[0].Groups[2].Value); 319 v3 = int.Parse(matches[0].Groups[3].Value); 320 v4 = int.Parse(matches[0].Groups[4].Value); 321 322 Version v = Assembly.GetExecutingAssembly().GetName().Version; 323 bool old = false; 324 if (v.Major < v1) 325 old = true; 326 if (v.Major == v1 && v.Minor < v2) 327 old = true; 328 if (v.Major == v1 && v.Minor == v2 && v.Build < v3) 329 old = true; 330 if (v.Major == v1 && v.Minor == v2 && v.Build == v3 && v.Revision < v4) 331 old = true; 332 333 if (old) 334 { 335 DialogResult res = MessageBox.Show(string.Format("A new version of Ascii Pumper is available.\r\nYou have: {0}.\r\nLatest release: {1}.{2}.{3}.{4}.\r\n\r\nVisit homepage?", v.ToString(), v1, v2, v3, v4), "New Ascii Pumper version", MessageBoxButtons.YesNo); 336 if (res == DialogResult.Yes) 337 { 338 ProcessStartInfo pi = new ProcessStartInfo("http://code.google.com/p/asciipumper/"); 339 Process.Start(pi); 340 } 341 } 342 } 343 344 } 345 catch (Exception e) 346 { 347 // this is really non-critical code so let's discard all exceptions and go on with life ok? 348 } 349 350 351 } 352 353 private void saveToolStripButton_Click(object sender, EventArgs e) 354 { 355 if (this.ActiveMdiChild == null) 356 { 357 MessageBox.Show(this, "No files are loaded."); 358 return; 359 } 360 361 if (this.ActiveMdiChild is MainForm) 362 { 363 ((MainForm)this.ActiveMdiChild).SaveDocument(); 364 AddFileToRecentFiles(((MainForm)this.ActiveMdiChild).FileName ); 365 } 366 367 } 368 369 private void saveToolStripMenuItem_Click(object sender, EventArgs e) 370 { 371 saveToolStripButton_Click(sender, e); 372 } 373 374 private void imageImporterToolStripMenuItem_Click(object sender, EventArgs e) 375 { 376 OpenFileDialog ofd = new OpenFileDialog(); 377 ofd.Filter = "Supported images (*.bmp, *.jpeg, *.jpg, *.png, *.gif)|*.bmp;*.jpeg;*.jpg;*.png;*.gif|Bitmaps (*.bmp)|*.bmp|GIFs (*.gif)|*.gif|JPEGs (*.jpg,*.jpeg)|*.jpeg;*.jpg|PNGs (*.png)|*.png|All Files (*.*)|*.*"; 378 DialogResult res = ofd.ShowDialog(this); 379 if (res == DialogResult.OK) 380 { 381 382 // Create a new instance of the child form. 383 MainForm childForm = new MainForm(); 384 // Make it a child of this MDI form before showing it. 385 childForm.MdiParent = this; 386 //childForm.Text = "(Untitled)"; 387 childForm.FileName = "(Imported image " + ++childFormNumber + ")"; 388 childForm.FileModified = false; 389 childForm.Show(); 390 childForm.ImportImage(ofd.FileName); 391 392 } 393 } 394 395 private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e) 396 { 397 PrintPreviewDialog previewdlg = new PrintPreviewDialog(); 398 previewdlg.Document = new System.Drawing.Printing.PrintDocument(); 399 previewdlg.Document.PrintPage += new PrintPageEventHandler(Document_PrintPage); 400 previewdlg.ShowDialog(this); 401 } 402 403 void Document_PrintPage(object sender, PrintPageEventArgs e) 404 { 405 if (this.ActiveMdiChild is MainForm) 406 { 407 ((MainForm)this.ActiveMdiChild).PrintToGraphicDevice(e.Graphics, e); 408 } 409 } 410 411 private void printToolStripButton_Click(object sender, EventArgs e) 412 { 413 PrintDialog printdlg = new PrintDialog(); 414 printdlg.Document = new PrintDocument(); 415 printdlg.Document.PrintPage += new PrintPageEventHandler(Document_PrintPage); 416 if (printdlg.ShowDialog(this) == DialogResult.OK) 417 printdlg.Document.Print(); 418 419 } 420 421 private void printPreviewToolStripButton_Click(object sender, EventArgs e) 422 { 423 printPreviewToolStripMenuItem_Click(sender, e); 424 } 425 426 private void printSetupToolStripMenuItem_Click(object sender, EventArgs e) 427 { 428 429 } 430 431 private void printToolStripMenuItem_Click(object sender, EventArgs e) 432 { 433 printToolStripButton_Click(sender, e); 434 } 435 436 private void redoToolStripMenuItem_Click(object sender, EventArgs e) 437 { 438 if (this.ActiveMdiChild is MainForm) 439 { 440 MainForm form = (MainForm)this.ActiveMdiChild; 441 442 form.Redo(); 443 } 444 } 445 446 private void undoToolStripMenuItem_Click(object sender, EventArgs e) 447 { 448 if (this.ActiveMdiChild is MainForm) 449 { 450 MainForm form = (MainForm)this.ActiveMdiChild; 451 452 form.Undo(); 453 } 454 } 455 456 private void PumpForm_FormClosing(object sender, FormClosingEventArgs e) 457 { 458 Program.Settings.Save(); 459 } 460 461 private void rEADMETXTToolStripMenuItem_Click(object sender, EventArgs e) 462 { 463 ProcessStartInfo pi = new ProcessStartInfo( /*Assembly.GetExecutingAssembly().Location*/ "README.TXT"); 464 Process.Start(pi); 465 } 466 467 private void PumpForm_MdiChildActivate(object sender, EventArgs e) 468 { 469 childForm_UndoChanged(this.ActiveMdiChild, MainForm.UndoChangedEventArgs.Empty); 470 childForm_RedoChanged(this.ActiveMdiChild, MainForm.RedoChangedEventArgs.Empty); 471 472 } 473 } 474 }