using System; using EventHandler = System.EventHandler; using System.Windows.Forms; using System.Collections.Generic; using System.Text; using System.Drawing; public class MyForm : Form { [System.STAThread] static void Main() { Application.Run(new MyForm()); } // ======================================================================== static public MyForm theMyForm; MenuStrip mainmenu; ToolStripMenuItem menu_File, menu_Edit, menu_File_SaveOver; ToolStripComboBox cb; ToolStripButton ch; public TextBox textBox; private const string titleprefix="TextEditor - "; private string filename=""; private byte[] bytes; const string GBH = "現在の文書は、保存しますか ?", GBNMH = "現在の文書に名前を付けて保存", NMH = "名前を付けて保存", SRH = "終了前に保存しますか ?"; public MyForm() { theMyForm=this; this.Text = titleprefix+filename; // ################## menu buiding block ###################### mainmenu = new MenuStrip(); this.Controls.Add(mainmenu); // new cb = new ToolStripComboBox(); menu_File=mainmenu.addItem("ファイル(&F)"); menu_Edit=mainmenu.addItem("編集(&E)"); mainmenu.Items.Add(cb); cb.Items.AddRange(new string[] { "Automatic", "ANSI", "UTF-7", "WordDump", "UTF-8NB", "UTF-8BOM", "UTF-16", "UTF-16BE", "UTF-32" }); cb.MaxDropDownItems=10; cb.SelectedIndex = 0; mainmenu.Items.Add(ch = new ToolStripButton("テキスト固定")); ch.Click += fixtext; // menu_File.addItem("新規(&N)", null, (o, e) => { if (menu_File_SaveOver.Enabled && textBox.Text != "") { DialogBoxCheckRetain commonDialog1=new DialogBoxCheckRetain(GBH); DialogResult dr= commonDialog1.ShowDialog(); if (dr == DialogResult.OK) {saveAsDialog(GBNMH); } else if (dr == DialogResult.Cancel) {return; } } textBox.Text=""; this.filename=""; this.Text = titleprefix+this.filename; cb.Items.Clear(); bytes = new byte[0]; cb.Items.AddRange(new string[] { "Automatic", "ANSI", "UTF-7", "WordDump", "UTF-8NB", "UTF-8BOM", "UTF-16", "UTF-16BE", "UTF-32" }); cb.SelectedIndex = 0; } ); menu_File.addItem("開く(コード; &O)", null, (o, e) => { if (menu_File_SaveOver.Enabled && textBox.Text != "") { DialogBoxCheckRetain commonDialog1=new DialogBoxCheckRetain(GBH); DialogResult dr= commonDialog1.ShowDialog(); if (dr == DialogResult.OK) {saveAsDialog(GBNMH); } else if (dr == DialogResult.Cancel) {return; } } this.loadAsDialog(); } ); menu_File.addItem("-"); menu_File_SaveOver=menu_File.addItem("上書き保存(コード; &A)", null, (o, e) => { if (this.filename == "") { this.saveAsDialog(NMH); } else { this.saveKernel(this.filename, iid); } }); menu_File.addItem("名前をつけて保存(コード; &S)", null, (o, e) => { this.saveAsDialog(NMH); } ); menu_File.addItem("-"); menu_File.addItem("終了(&E)", null, (o, e) => { Dispose(true); } ); menu_Edit.addItem("行(&L)", null, (o, e) => { DialogBoxLine commonDialog1=new DialogBoxLine(); commonDialog1.getstr = (int i) => { // === defining function over dialog's events if (i < 0) {return null; } if (textBox.TextLength == 0) {return null; } int Lmax=textBox.GetLineFromCharIndex(textBox.TextLength-1); if (i > Lmax+1) {return null; } return textBox.Lines[i]; }; commonDialog1.ShowDialog(); } ); // ############################################################ this.Controls.Add( textBox=new TextBox() ); textBox.Multiline=true; textBox.ScrollBars = ScrollBars.Both; textBox.Location = new Point(2, 28); this.menu_File_SaveOver.Enabled=false; // === ineffective first !!! cb.SelectedIndexChanged += (o, e) => { parseBytes(); }; textBox.KeyPress += fixtext; this.MaximumSize = new Size(maxwidth+iallowance, 1400); this.Size = new System.Drawing.Size(1200, 800); } // ===================================================================== void fixtext(object o, EventArgs e) { bytes = null; this.menu_File_SaveOver.Enabled=true; cb.BackColor = Color.White; cb.Items.Clear(); cb.Items.AddRange(new string[] { "Automatic", "ANSI", "UTF-7", "WordDump", "UTF-8NB", "UTF-8BOM", "UTF-16", "UTF-16BE", "UTF-32" }); cb.SelectedIndex = 0; } // ===================================================================== int maxwidth = 1600, iallowance=20; protected override void OnSizeChanged(System.EventArgs e) { textBox.Size=new Size(this.Width - iallowance, this.Height-70); } // ===================================================================== protected override void Dispose( bool disposing ) { if (disposing) { DialogBoxCheckRetain commonDialog1=new DialogBoxCheckRetain(SRH); DialogResult dr= commonDialog1.ShowDialog(); if (dr == DialogResult.OK) {saveAsDialog(NMH); } else if (dr == DialogResult.Cancel) {return; } } base.Dispose( disposing ); } // ===================================================================== private void loadAsDialog() { // Code version OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.FileName=this.filename; openFileDialog1.Filter = combinedFilter; openFileDialog1.FilterIndex=1; // == 1.. !!! openFileDialog1.RestoreDirectory=true; if(openFileDialog1.ShowDialog() == DialogResult.OK) { iid=openFileDialog1.FilterIndex; string ext=MyForm.getPostfix(openFileDialog1.FileName); // === might have been changed !!! System.IO.FileStream fst=new System.IO.FileStream( openFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None); int filelen=(int)fst.Length; bytes=new byte[filelen]; for (int i=0; i=3 && bytes[2] == 0) { cb.Items.Add("UTF-32"); } else if (bytes.Length>=2 && bytes[0]==255 && bytes[1]==254) { cb.Items.Add("UTF-16"); } else if (bytes.Length>=2 && bytes[0]==254 && bytes[1]==255) { // Unicode Big Endian (UTF16_BE) cb.Items.Add("UTF-16BE"); } else if (bytes.Length>=3 && bytes[0]==239 && bytes[1]==187 && bytes[2]==191) { // UTF-8 cb.Items.Add("UTF-8BOM"); } else if (bytes.Length>=1 && bytes[0] >= 0x90) { cb.Items.Add("UTF-8NB"); } if (cb.Items.Count >= 6) { cb.SelectedIndex = 5; } } // ===================================================================== private void parseBytes() { if (bytes == null) { return; } string strtop = cb.SelectedItem.ToString(); switch (strtop) { case "ANSI": // Encoding.Unicode.GetBytes( this.textBox.Text=Encoding.Default.GetString(bytes); break; case "UTF-7": this.textBox.Text=Encoding.UTF7.GetString(bytes); break; case "UTF-8NB": this.textBox.Text=Encoding.UTF8.GetString(bytes); break; case "UTF-8BOM": this.textBox.Text=Encoding.UTF8.GetString(bytes); break; case "UTF-16": // UTF-16 this.textBox.Text=Encoding.Unicode.GetString(bytes); break; case "UTF-16BE": // UTF-16BE this.textBox.Text=Encoding.BigEndianUnicode.GetString(bytes); break; case "UTF-32": this.textBox.Text=Encoding.UTF32.GetString(bytes); break; case "WordDump": int halfsize=bytes.Length/2; char[] chrs=new char[halfsize]; for (int i=0; i nlines = new List(); static private string getPostfix(string filename) { int ind=filename.IndexOf(".", 0); for ( ; ; ) { int indnw=filename.IndexOf(".", ind+1); if (indnw < 0) {break; } ind=indnw; } return filename.Substring(ind+1); } // ===================================================================== string[] filterContent = new string[] { "all (*.*)|*.*", "text (*.txt)|*.txt", "rich (*.rtf)|*.rtf", "csharp (*.cs)|*.cs" }; int iid; string combinedFilter { get { string str = filterContent[0]; for (int i=1; i lsti = new List(); byte[] bytes=null; string strtop; if (cb.SelectedIndex < 0) { cb.SelectedItem = strtop = "ANSI"; } else { strtop = cb.SelectedItem.ToString(); } System.IO.FileStream fst = new System.IO.FileStream( filename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); // "ANSI", "UTF-7", "WordDump", "UTF-8NB", "UTF-8BOM", "UTF-16", "UTF-16BE", "UTF-32" }); switch (strtop) { case "UTF-16": fst.WriteByte(255); fst.WriteByte(254); bytes = System.Text.Encoding.Unicode.GetBytes(chrs); break; case "UTF-16BE": fst.WriteByte(254); fst.WriteByte(255); bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(chrs); break; case "UTF-7": bytes = System.Text.Encoding.UTF7.GetBytes(chrs); break; case "UTF-8NB": bytes = System.Text.Encoding.UTF8.GetBytes(chrs); break; case "UTF-8BOM": bytes = System.Text.Encoding.UTF8.GetBytes(chrs); fst.WriteByte(0xef); fst.WriteByte(0xbb); fst.WriteByte(0xbf); break; case "UTF-32": bytes = System.Text.Encoding.UTF32.GetBytes(chrs); break; case "ANSI": bytes = System.Text.Encoding.Default.GetBytes(chrs); break; case "WordDump": bytes = new byte[chrs.Length * 2]; for (int i = 0; i < chrs.Length; i++) { ushort us = chrs[i]; byte usupper = (byte)(us / 256), uslower = (byte)(us % 256); bytes[i * 2] = uslower; bytes[i * 2 + 1] = usupper; } break; } foreach (byte b in bytes) { fst.WriteByte(b); } fst.Flush(); fst.Close(); this.filename=filename; this.menu_File_SaveOver.Enabled=false; // === indicates that overwritten !!! this.Text = titleprefix+this.filename; } } // ############################################################################ public class DialogBoxCheckRetain : Form { private Button button1, buttonc, button2; private Label label1; public DialogBoxCheckRetain(string strinquire) { button1 = new Button(); buttonc = new Button(); button2 = new Button(); label1 = new Label(); // button1.DialogResult = DialogResult.OK; button1.Location = new Point(48, 48); button1.Text = "保存する"; // buttonc.DialogResult = DialogResult.Cancel; buttonc.Location = new Point(168, 48); buttonc.Text = "キャンセル"; // button2.DialogResult = DialogResult.No; button2.Location = new Point(288, 48); button2.Text = "保存しない"; // label1.Location = new Point(72, 16); label1.Name = "label1"; label1.Size = new Size(216, 23); label1.TabIndex = 2; label1.Text = strinquire; // this.ClientSize = new Size(412, 85); this.Controls.AddRange(new Control[] { label1, button2, buttonc, button1, }); Text = "保存"; MinimizeBox=MaximizeBox=false; } // === as a dialog box !!! } // #################################################################################### public static class MenuExtention { // MenuStrip, ToolStripMenuItem 使うのに便利 // new public static ToolStripMenuItem addItem(this MenuStrip par, // new string str) { // new ToolStripMenuItem item=new ToolStripMenuItem(str); // new par.Items.Add(item); return item; } // new/ new public static ToolStripMenuItem addItem(this ToolStripMenuItem par, // new string str, Image img, EventHandler handler) { // new ToolStripMenuItem item=new ToolStripMenuItem(str, img, handler); // new par.DropDownItems.Add(item); return item; } // new public static ToolStripMenuItem addItem( // new this ToolStripMenuItem par, string str) { // new ToolStripMenuItem item = new ToolStripMenuItem(str); // new par.DropDownItems.Add(item); return item; } // new public static void addItemsRange(this ToolStripMenuItem par, // new string[] strs) { // new foreach (string str in strs) { ToolStripMenuItem item=new ToolStripMenuItem(str); // new par.DropDownItems.Add(item); } } // new } // #################################################### // new public delegate string stringByInt(int i); public class DialogBoxLine : Form { private Label label1; private TextBox textBoxLineNo; private Button buttonOK; private TextBox textBoxLineContent; private Button buttonFIND; // public DialogBoxLine() { label1 = new Label(); textBoxLineNo = new TextBox(); buttonFIND = new Button(); textBoxLineContent = new TextBox(); buttonOK = new Button(); // label1.Location = new Point(192, 32); label1.Size = new Size(100, 16); label1.Text = "行番号 (1 .. )"; // textBoxLineNo.Location = new Point(328, 32); textBoxLineNo.Text = ""; // buttonFIND.Location = new Point(304, 96);  buttonFIND.Text = "表示"; buttonFIND.Click += (o, e) => { int iline=0; string str=(textBoxLineNo).Text; // === as latter is private !!! try { int i=int.Parse(str); iline=i-1; } catch { // === (FormatException e) iline=-1; } if (iline < 0) { textBoxLineContent.BackColor=Color.Yellow; textBoxLineContent.Text="Assigned line number is out of search !!! "; return; } // ################# utilize delegate of the Form ################### string strhere = getstr(iline); // ################################################################## if (strhere == null) { textBoxLineContent.BackColor=Color.Yellow; textBoxLineContent.Text="Assigned line number is over the limit !!! "; } else { textBoxLineContent.BackColor=Color.White; textBoxLineContent.Text = strhere; } }; // textBoxLineContent.Location = new Point(24, 160);  textBoxLineContent.Size = new Size(664, 19);  textBoxLineContent.Text = ""; // buttonOK.Location = new Point(304, 192);  buttonOK.Text = "OK"; buttonOK.DialogResult = DialogResult.OK; // AutoScaleBaseSize = new Size(5, 12); ClientSize = new Size(720, 229); Controls.AddRange(new Control[] { label1, textBoxLineNo, buttonFIND, textBoxLineContent, buttonOK}); Text = "行表示"; MinimizeBox=MaximizeBox=false; } // === as a dialog box !!! public stringByInt getstr; } // ##################################################################################