// one word (in edit) coloring void RichTextBox2KeyUp(object sender, KeyEventArgs e) { char c = (char)e.KeyValue; //32 , 8, 46 if (((e.KeyValue == 32) || (e.KeyValue == 8) || (e.KeyValue == 46)) && (this.richTextBox2.Lines.Length > 0)) { c = 'a'; } #region go to encloset position if (Math.Abs(pevOpenBr.Current - prevClosBr.Current) > 150) { if (pevOpenBr.Current == this.richTextBox2.SelectionStart) { selChangedProgr = true; this.richTextBox2.SelectionStart = prevClosBr.Current + 1; return; } if (prevClosBr.Current == this.richTextBox2.SelectionStart) { selChangedProgr = true; this.richTextBox2.SelectionStart = pevOpenBr.Current + 1; return; } } #endregion if ((e.KeyCode == Keys.Z) && (e.Control)) { this.richTextBox2.Undo(); } if ((Char.IsLetterOrDigit(c) == false) || (e.Control)) { return; } if ((this.richTextBox2.ReadOnly) || (this.richTextBox2.Text.Length < 3)) { return; } if (!brUnmarked) { UnmarkBr(); } this.richTextBox2.SelectionColor = Color.Black; segmCoord rez; rez = GetSelectionRowCol(this.richTextBox2, this.richTextBox2.SelectionStart); int oldSelStart = this.richTextBox2.SelectionStart; segmCoord substrToUpdate; string procRow = this.richTextBox2.Lines[ROW]; substrToUpdate = QueryParser.GetSubstrSegmByCharNum(procRow, COL); if ((substrToUpdate.fin == 0) && (substrToUpdate.start == 0)) { return; } selChangedProgr = true; this.richTextBox2.SelectionStart = rez.start + substrToUpdate.start; selChangedProgr = true; int len = (substrToUpdate.fin - substrToUpdate.start + 1); len = (procRow.Length > len) ? len : len - 1; this.richTextBox2.SelectionLength = len; string parseSubstr = procRow.Substring(substrToUpdate.start, len); string[] t; string exprType = QueryParser.GetExpresionStruct(QueryParser.TrimBrackets(parseSubstr), out t); this.richTextBox2.SelectionColor = GetColorByExpr(exprType); this.richTextBox2.SelectionBackColor = Color.White; selChangedProgr = true; this.richTextBox2.SelectedText = parseSubstr; selChangedProgr = true; this.richTextBox2.SelectionStart = oldSelStart; }
// whole text coloring void RichTextBox2TextChanged(object sender, EventArgs e) { char[] variousOp = QueryParser.variousOpGet; if ((((prevTextLen + 100) < this.richTextBox2.Text.Length) || ((prevTextLen - 500) > this.richTextBox2.Text.Length)) && (!notTextChanged)) { string rftHeader = @"{\rtf1\ansi\ansicpg1251\deff0{\fonttbl{\f0\fnil\fcharset204 Microsoft Sans Serif;}}"; //128 // 114 // 41 143 44 128 rftHeader += @"{\colortbl ;\red138\green43\blue226;\red139\green0\blue0;\red0\green139\blue139;\red0\green0\blue205;\red0\green0\blue0;\red250\green90\blue160;\red250\green10\blue27;}"; rftHeader += @"\viewkind4\uc1\pard\cf1\lang1049\f0\fs21"; StringBuilder SB = new StringBuilder(); notTextChanged = true; string RTF = ""; #region init environment string[] uncoloredLines = new string[this.richTextBox2.Lines.Length + 1]; this.richTextBox2.Lines.CopyTo(uncoloredLines, 0); int SelStart = this.richTextBox2.SelectionStart; // string inserted = ""; // if ((prevTextLen < this.richTextBox2.Text.Length) && !firstshow) // { // prevTextLen = this.richTextBox2.Text.Length - prevTextLen; // this.richTextBox2.SelectionStart = SelStart - prevTextLen; // this.richTextBox2.SelectionLength = prevTextLen; // inserted = this.richTextBox2.SelectedText; // uncoloredLines = new string[1]; // uncoloredLines[0] = inserted; // this.richTextBox2.SelectedText ="zzxx"; // } // firstshow = false; #endregion #region var bool comment = false; bool NextLinecomment = false; char prevSpec = ' '; string[] t; int wordCount = 0; #endregion foreach (string uncoloredText in uncoloredLines) { int opCou = -1; string exprMap = QueryParser.GetExpresionStruct(uncoloredText, out t, true); // third parameter save all space formatting of original text (if omitted spaces not including to exp map) string spaceBuffer = ""; // wordCount += t.Length; for (int i = 0; i < exprMap.Length; i++) { int index = Array.IndexOf(variousOp, exprMap[i]); #region output expr character or operand from array if (index != -1) { opCou++; #region coloring if (!comment) { SB.Append(GetStrColorByExpr(exprMap[i].ToString())); } else { SB.Append(GetStrColorByExpr("G")); } #endregion SB.Append(spaceBuffer + t[opCou]); spaceBuffer = ""; } else { if (exprMap[i] == ' ') // optimization: not drawing each space as separate colored substring { spaceBuffer += exprMap[i]; } else { #region coloring if (!comment) { SB.Append(GetStrColorByExpr("")); } else { SB.Append(GetStrColorByExpr("G")); } #endregion SB.Append(spaceBuffer + exprMap[i].ToString()); spaceBuffer = ""; #region comment parsing if (!comment) { if ((prevSpec.ToString() + exprMap[i].ToString()) == "/*") { NextLinecomment = true; comment = true; } if ((prevSpec.ToString() + exprMap[i].ToString()) == "--") { NextLinecomment = false; comment = true; } } if (((prevSpec.ToString() + exprMap[i].ToString()) == "*/") && (NextLinecomment != false)) { NextLinecomment = false; comment = false; } prevSpec = exprMap[i]; #endregion } } #endregion } comment = NextLinecomment; SB.Append(@"\par "); wordCount += opCou + 1; } RTF = SB.ToString(); RTF = RTF.Substring(0, RTF.Length - 5); // delete last "\par " // if (inserted != "") // { // int idx =this.richTextBox2.Rtf.IndexOf("zzxx"); // this.richTextBox2.SelectedText =""; // this.richTextBox2.Rtf = this.richTextBox2.Rtf.Insert(idx, RTF); // } else // { this.richTextBox2.Rtf = rftHeader + RTF + "}"; // } prevTextLen = this.richTextBox2.Text.Length; selChangedProgr = true; this.richTextBox2.SelectionStart = SelStart; notTextChanged = false; this.label1.Text = "Word count: " + wordCount.ToString(); } prevTextLen = this.richTextBox2.Text.Length; }