Sfall-ScriptEditor
238 строк · 8.0 Кб
1using System;2using System.Collections.Generic;3using System.Windows.Forms;4
5using ICSharpCode.TextEditor.Document;6
7namespace ScriptEditor8{
9internal enum InsertAt {10End = 0,11After = 1,12Caret = 213}14
15public partial class ProcForm : Form16{17private bool isCreateProcedure;18private bool isSetName;19
20public string CheckName { get; private set; }21
22public string ProcedureName23{24get { return tbName.Text; }25}26
27internal InsertAt PlaceAt28{29get {30if (!groupBoxProcedure.Enabled) return InsertAt.Caret;31return (rbPasteAtEnd.Checked) ? InsertAt.End : InsertAt.After;32}33}34
35public bool SetInsertAtArter36{37set { rbAfterSelProcedure.Checked = value; }38}39
40/// <summary>41/// Получает установленное значение копировать ли тело процедуры.42/// Включает или Выключает элемент управления.43/// </summary>44public bool CopyProcedure45{46get { return cbCopyBodyProc.Checked; }47set { cbCopyBodyProc.Enabled = value; }48}49
50public ProcForm(string name, bool readOnly = false, bool proc = false)51{52InitializeComponent();53
54this.isCreateProcedure = proc;55
56if (proc && name != null)57IncrementNumber(ref name);58
59tbName.Text = name;60tbName.ReadOnly = readOnly;61}62
63private void IncrementNumber(ref string name)64{65int lenName = name.Length - 1;66if (Char.IsDigit(name[lenName])) {67int i;68for (i = lenName; i > 0; i--) {69if (!Char.IsDigit(name[i]))70break;71}72int numZero = lenName - i;73int numb = int.Parse(name.Substring(++i));74numb++;75name = name.Remove(i) + numb.ToString(new string('0', numZero));76}77}78
79private void ProcForm_Shown(object sender, EventArgs e)80{81if (tbName.Text.Length == 082|| tbName.Text.IndexOf(' ') > -183|| tbName.Text == "procedure"84|| tbName.Text == "begin"85|| tbName.Text == "end"86|| tbName.Text == "variable")87{88tbName.Text = "Example(arg0, arg1)";89tbName.ForeColor = System.Drawing.Color.Gray;90tbName.SelectionStart = 0;91} else {92isSetName = true;93tbName.SelectionStart = tbName.Text.Length;94}95tbName.Focus();96if (isCreateProcedure) tbName.Enter += tbName_Enter;97
98}99
100private void ProcForm_FormClosing(object sender, FormClosingEventArgs e)101{102if (!isSetName || DialogResult != DialogResult.OK || tbName.Text.Length == 0) return;103
104tbName.Text = tbName.Text.Trim();105string name = tbName.Text;106
107int z = name.IndexOf('(');108if (z > 0) {109while (true)110{111z = name.IndexOf("variable ", z, StringComparison.OrdinalIgnoreCase);112if (z == -1) break;113name = name.Remove(z, 9);114}115}116// удаление пробелов117name = name.Replace(" ", "");118
119CheckName = name;120z = CheckName.IndexOf('(');121if (z > -1) CheckName = CheckName.Remove(z);122
123// проверка корректности имени124for (int i = 0; i < CheckName.Length; i++)125{126char ch = CheckName[i];127if (!TextUtilities.IsLetterDigitOrUnderscore(ch)) {128e.Cancel = true;129break;130}131}132
133if (isCreateProcedure && z > 0) {134int pairCount = 0, pair = 0;135// проверка корректности аргументов136for (int i = z; i < name.Length; i++)137{138char ch = name[i];139if (ch == ',') continue;140if (pair > 0 && ch == ')') pairCount++;141if (ch == '(') pair++;142if (ch == ')') pair--;143if ( ch == '(' || ch == ')') continue;144
145if (!TextUtilities.IsLetterDigitOrUnderscore(ch)) {146e.Cancel = true;147break;148}149}150if (pair != 0 || pairCount > 1) e.Cancel = true;151}152
153if (e.Cancel)154MessageBox.Show("Was used incorrect name.\nThe name can only contain alphanumeric characters and the underscore character.", "Incorrect name");155else {156// вставляем ключевые слова 'variable' для аргументов процедуры157if (z != -1) {158z++;159int y = name.LastIndexOf(')');160if (z == y) return; // no args161
162string pName = name.Substring(0, z - 1);163
164List<byte> args = new List<byte>();165for (byte i = (byte)z; i < y; i++) {166if (name[i] == ',') args.Add(i);167}168args.Add((byte)y);169
170// извлекаем имена аргументов171string argNames = string.Empty;172for (byte i = 0; i < args.Count; i++)173{174int x = args[i];175string argName = name.Substring(z, x - z).Trim();176z = x + 1;177
178if (argName.Length == 0) continue;179
180if (!argName.StartsWith("variable ")) argName = argName.Insert(0, "variable ");181if (argNames != string.Empty) argNames += ", ";182argNames += argName;183}184tbName.Text = string.Format("{0}({1})", pName, argNames);185}186}187}188
189internal static bool CreateRenameForm(ref string name, string tile = "")190{191ProcForm RenameFrm = new ProcForm(name);192RenameFrm.groupBoxProcedure.Enabled = false;193RenameFrm.Text = "Rename " + tile;194RenameFrm.Create.Text = "OK";195if (RenameFrm.ShowDialog() == DialogResult.Cancel || RenameFrm.ProcedureName.Length == 0) {196return false;197}198name = RenameFrm.ProcedureName.Trim();199RenameFrm.Dispose();200return true;201}202
203private void tbName_Leave(object sender, EventArgs e)204{205if (isCreateProcedure && (!isSetName || tbName.Text.Length == 0)) {206tbName.Text = "Example(arg0, arg1)";207tbName.ForeColor = System.Drawing.Color.Gray;208isSetName = false;209}210}211
212private void tbName_Enter(object sender, EventArgs e)213{214if (!isSetName && tbName.Text.Length > 0) {215tbName.ResetText();216tbName.ForeColor = System.Drawing.SystemColors.ControlText;217}218}219
220private void tbName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)221{222if (!isSetName) {223tbName.ResetText();224tbName.ForeColor = System.Drawing.SystemColors.ControlText;225isSetName = true;226}227}228
229private void tbName_MouseClick(object sender, MouseEventArgs e)230{231if (!isSetName) {232tbName.ResetText();233tbName.ForeColor = System.Drawing.SystemColors.ControlText;234isSetName = true;235}236}237}238}
239