using System; using System.IO; using System.Drawing; // Security Change (added using statements) using System.Security; using System.Security.Permissions; using System.Windows.Forms; using System.ComponentModel; class App{ public static void Main(){ Application.Run(new Pad()); } } public class Pad : System.Windows.Forms.Form{ System.Windows.Forms.TextBox text; System.Windows.Forms.MainMenu mainMenu; System.Windows.Forms.MenuItem menuFile; System.Windows.Forms.MenuItem menuFileSave; System.Windows.Forms.MenuItem menuFileNew; System.Windows.Forms.MenuItem menuFileOpen; System.Windows.Forms.MenuItem menuFileSaveAs; System.Windows.Forms.MenuItem menuFileExit; System.Windows.Forms.MenuItem menuEdit; System.Windows.Forms.MenuItem menuEditUndo; System.Windows.Forms.MenuItem menuEditCut; System.Windows.Forms.MenuItem menuEditCopy; System.Windows.Forms.MenuItem menuEditPaste; System.Windows.Forms.MenuItem menuEditDelete; System.Windows.Forms.MenuItem menuEditSelectAll; System.Windows.Forms.MenuItem menuFormat; System.Windows.Forms.MenuItem menuFormatWordWrap; System.Windows.Forms.MenuItem menuFormatFont; System.Windows.Forms.MenuItem menuBar1; System.Windows.Forms.MenuItem menuBar2; System.Windows.Forms.MenuItem menuBar3; System.Windows.Forms.StatusBar status; // Keep track of the file name and whether or not the data has changed String fileName; Boolean isDirty; public Pad(){ // Setup the controls, and setup a handler for the Format menu's popup InitializeComponent(); menuFormat.Popup += new EventHandler(menuFormat_Popup); ResetForm(); // Security Change INTERNET ZONE // Added a try catch and a demand for clipboard access // if exception is thrown, then the paste menu is disabled try{ UIPermission perm = new UIPermission(UIPermissionClipboard.AllClipboard); perm.Demand(); }catch(SecurityException){ menuEditPaste.Enabled = false; } } void menuFileNew_Click(object sender, System.EventArgs e){ Boolean cancel; CheckToSave(out cancel); if(!cancel){ ResetForm(); } } void menuFileOpen_Click(object sender, System.EventArgs e){ OpenFile(); } void menuFileSave_Click(object sender, System.EventArgs e){ SaveFile(); } void menuFileSaveAs_Click(object sender, System.EventArgs e){ SaveFileAs(); } void menuFileExit_Click(object sender, System.EventArgs e){ Close(); } protected override void OnClosing(CancelEventArgs e){ Boolean cancel; CheckToSave(out cancel); if(cancel){ e.Cancel = true; }else{ base.OnClosing(e); } } void menuEditCut_Click(object sender, System.EventArgs e){ text.Cut(); } void menuEditCopy_Click(object sender, System.EventArgs e){ text.Copy(); } void menuEditPaste_Click(object sender, System.EventArgs e){ text.Paste(); } void menuEditUndo_Click(object sender, System.EventArgs e){ text.Undo(); } void menuEditDelete_Click(object sender, System.EventArgs e){ text.SelectedText = String.Empty; } void menuEditSelectAll_Click(object sender, System.EventArgs e){ text.SelectAll(); } void menuFormat_Popup(object sender, System.EventArgs e){ this.menuFormatWordWrap.Checked = text.WordWrap; } void menuFormatWordWrap_Click(object sender, System.EventArgs e){ text.WordWrap = !text.WordWrap; } void menuFormatFont_Click(object sender, System.EventArgs e){ SelectFont(); } void text_TextChanged(object sender, System.EventArgs e){ isDirty = true; } // Set everything back to square-one void ResetForm(){ FileName = null; text.Text = String.Empty; isDirty = false; } // Property for getting and setting FileName, sets up other odds and ends String FileName{ get{return fileName;} set{ if(value == null){ menuFileSave.Enabled = false; Text = "Pad"; }else{ menuFileSave.Enabled = true; Text = Path.GetFileName(value)+" - Pad"; } fileName = value; } } // Check to see if we should ask the user if they would like to save void CheckToSave(out Boolean cancelOperation){ cancelOperation = false; if(isDirty){ DialogResult answer = MessageBox.Show( "The text in the document has changed.\n\n"+ "Do you want to save changes?", "Pad", MessageBoxButtons.YesNoCancel); switch(answer){ case DialogResult.Yes: if(FileName == null){ SaveFileAs(); }else{ SaveFile(); } break; case DialogResult.No: break; default: cancelOperation = true; break; } } } // Open a file selected by the user void OpenFile(){ Stream stream; OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; ofd.FilterIndex = 1 ; // Security Change // The following try catch block was added. No real handling is // done since both of these lines implement non-critical features. try{ ofd.InitialDirectory = "." ; ofd.RestoreDirectory = false ; }catch(SecurityException){} if(ofd.ShowDialog() == DialogResult.OK){ if((stream = ofd.OpenFile())!= null){ Boolean cancel; CheckToSave(out cancel); if(!cancel){ StreamReader reader = new StreamReader(stream, true); text.Text = reader.ReadToEnd(); isDirty = false; reader.Close(); // Security Change // try catch block added. The restricted version never // knows a filename try{FileName = ofd.FileName;}catch(SecurityException){} } } } } // Let the user select a filename to save the file as void SaveFileAs(){ SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; sfd.FilterIndex = 1 ; // Security Change // The following try catch block was added. No real handling is // done since this line implements a non-critical feature. try{sfd.RestoreDirectory = true ;}catch(SecurityException){} if(sfd.ShowDialog() == DialogResult.OK){ // Security Change // This try catch block was added, the handler makes a call // to a new function added to support the secured functionality. // The original flow is shown in the try block. try{ FileName = sfd.FileName; SaveFile(); }catch(SecurityException){ SaveFile(sfd.OpenFile()); } } } // Security Change // The original SaveFile function was broken up into two. One that // opens a stream from a filename, and another that saves the stream // and performs some maintenance. This keeps the bulk of the logic // in one place, and nicely blends with the full-trust version which // needs to be able to save a file based on its name. void SaveFile(Stream stream){ StreamWriter writer = new StreamWriter(stream); writer.Write(text.Text); writer.Close(); isDirty = false; } // Security Change // Save the current file (if fails, SaveAs) void SaveFile(){ try{ SaveFile(new FileStream(FileName, FileMode.Create)); }catch(IOException){ SaveFileAs(); } } // Let the user select the font that they work with void SelectFont(){ FontDialog fd = new FontDialog(); fd.Font = text.Font; fd.Color = text.ForeColor; fd.ShowColor = true; if(fd.ShowDialog() != DialogResult.Cancel ){ text.Font = fd.Font ; text.ForeColor = fd.Color; } } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.menuFormatFont = new System.Windows.Forms.MenuItem(); this.mainMenu = new System.Windows.Forms.MainMenu(); this.menuFile = new System.Windows.Forms.MenuItem(); this.menuFileNew = new System.Windows.Forms.MenuItem(); this.menuFileOpen = new System.Windows.Forms.MenuItem(); this.menuFileSave = new System.Windows.Forms.MenuItem(); this.menuFileSaveAs = new System.Windows.Forms.MenuItem(); this.menuBar1 = new System.Windows.Forms.MenuItem(); this.menuFileExit = new System.Windows.Forms.MenuItem(); this.menuEdit = new System.Windows.Forms.MenuItem(); this.menuEditUndo = new System.Windows.Forms.MenuItem(); this.menuBar2 = new System.Windows.Forms.MenuItem(); this.menuEditCut = new System.Windows.Forms.MenuItem(); this.menuEditCopy = new System.Windows.Forms.MenuItem(); this.menuEditPaste = new System.Windows.Forms.MenuItem(); this.menuEditDelete = new System.Windows.Forms.MenuItem(); this.menuBar3 = new System.Windows.Forms.MenuItem(); this.menuEditSelectAll = new System.Windows.Forms.MenuItem(); this.menuFormat = new System.Windows.Forms.MenuItem(); this.menuFormatWordWrap = new System.Windows.Forms.MenuItem(); this.text = new System.Windows.Forms.TextBox(); this.status = new System.Windows.Forms.StatusBar(); this.SuspendLayout(); // // menuFormatFont // this.menuFormatFont.Index = 1; this.menuFormatFont.Text = "&Font..."; this.menuFormatFont.Click += new System.EventHandler(this.menuFormatFont_Click); // // mainMenu // this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuFile, this.menuEdit, this.menuFormat}); // // menuFile // this.menuFile.Index = 0; this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuFileNew, this.menuFileOpen, this.menuFileSave, this.menuFileSaveAs, this.menuBar1, this.menuFileExit}); this.menuFile.Text = "&File"; // // menuFileNew // this.menuFileNew.Index = 0; this.menuFileNew.Text = "&New"; this.menuFileNew.Click += new System.EventHandler(this.menuFileNew_Click); // // menuFileOpen // this.menuFileOpen.Index = 1; this.menuFileOpen.Text = "&Open..."; this.menuFileOpen.Click += new System.EventHandler(this.menuFileOpen_Click); // // menuFileSave // this.menuFileSave.Index = 2; this.menuFileSave.Text = "&Save"; this.menuFileSave.Click += new System.EventHandler(this.menuFileSave_Click); // // menuFileSaveAs // this.menuFileSaveAs.Index = 3; this.menuFileSaveAs.Text = "Save &As..."; this.menuFileSaveAs.Click += new System.EventHandler(this.menuFileSaveAs_Click); // // menuBar1 // this.menuBar1.Index = 4; this.menuBar1.Text = "-"; // // menuFileExit // this.menuFileExit.Index = 5; this.menuFileExit.Text = "E&xit"; this.menuFileExit.Click += new System.EventHandler(this.menuFileExit_Click); // // menuEdit // this.menuEdit.Index = 1; this.menuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuEditUndo, this.menuBar2, this.menuEditCut, this.menuEditCopy, this.menuEditPaste, this.menuEditDelete, this.menuBar3, this.menuEditSelectAll}); this.menuEdit.Text = "&Edit"; // // menuEditUndo // this.menuEditUndo.Index = 0; this.menuEditUndo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ; this.menuEditUndo.Text = "&Undo"; this.menuEditUndo.Click += new System.EventHandler(this.menuEditUndo_Click); // // menuBar2 // this.menuBar2.Index = 1; this.menuBar2.Text = "-"; // // menuEditCut // this.menuEditCut.Index = 2; this.menuEditCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX; this.menuEditCut.Text = "Cu&t"; this.menuEditCut.Click += new System.EventHandler(this.menuEditCut_Click); // // menuEditCopy // this.menuEditCopy.Index = 3; this.menuEditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC; this.menuEditCopy.Text = "&Copy"; this.menuEditCopy.Click += new System.EventHandler(this.menuEditCopy_Click); // // menuEditPaste // this.menuEditPaste.Index = 4; this.menuEditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV; this.menuEditPaste.Text = "&Paste"; this.menuEditPaste.Click += new System.EventHandler(this.menuEditPaste_Click); // // menuEditDelete // this.menuEditDelete.Index = 5; this.menuEditDelete.Shortcut = System.Windows.Forms.Shortcut.Del; this.menuEditDelete.Text = "De&lete"; this.menuEditDelete.Click += new System.EventHandler(this.menuEditDelete_Click); // // menuBar3 // this.menuBar3.Index = 6; this.menuBar3.Text = "-"; // // menuEditSelectAll // this.menuEditSelectAll.Index = 7; this.menuEditSelectAll.Shortcut = System.Windows.Forms.Shortcut.CtrlA; this.menuEditSelectAll.Text = "Select &All"; this.menuEditSelectAll.Click += new System.EventHandler(this.menuEditSelectAll_Click); // // menuFormat // this.menuFormat.Index = 2; this.menuFormat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuFormatWordWrap, this.menuFormatFont}); this.menuFormat.Text = "F&ormat"; // // menuFormatWordWrap // this.menuFormatWordWrap.Index = 0; this.menuFormatWordWrap.Text = "&Word Wrap"; this.menuFormatWordWrap.Click += new System.EventHandler(this.menuFormatWordWrap_Click); // // text // this.text.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.text.Font = new System.Drawing.Font("Lucida Console", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.text.MaxLength = 0; this.text.Multiline = true; this.text.Name = "text"; this.text.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.text.Size = new System.Drawing.Size(592, 552); this.text.TabIndex = 0; this.text.Text = ""; this.text.WordWrap = false; this.text.TextChanged += new System.EventHandler(this.text_TextChanged); // // status // this.status.Location = new System.Drawing.Point(0, 556); this.status.Name = "status"; this.status.Size = new System.Drawing.Size(592, 20); this.status.TabIndex = 1; // // CSPad // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(592, 576); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.status, this.text}); this.Menu = this.mainMenu; this.MinimumSize = new System.Drawing.Size(10, 10); this.Name = "CSPad"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "CSPad"; this.ResumeLayout(false); } #endregion }