Move frmErrors when frmLoader is moved

Shutoff SpellCheck for StepRTB
Use using for temporary StepRTB
Use using for vlnFlexGrid
Added debug output to track StepRTBs and vlnFlexGrids in memory
Limit bottom margin to 0 or above
Dispose of roImage after it is done being used
Dispose of Figure after it is done being used.
Use GetJustRODB so that images are not loaded.
Added ErrorHandler if annotation is deleted after a search
Track create, dispose and finalize
Add static variable to control if SpellCheck is used
Use using for temporary StepRTB
Lazy Load SelectionStack
Clean-up on Dispose
Track create, dispose and finalize
Make MyCopyInfo Lazy Loaded
Use using for temporary StepRTB
Add Dispose method for TableCellEditor
Cleanup on Dispose
Only kill MSWord instances that are invisible
This commit is contained in:
Rich
2012-07-13 18:34:57 +00:00
parent ce21565a15
commit 7bcfafcc8b
12 changed files with 520 additions and 387 deletions

View File

@@ -25,8 +25,26 @@ namespace Volian.Controls.Library
public partial class VlnFlexGrid : C1.Win.C1FlexGrid.C1FlexGrid
{
private bool _Disposed = false;
private static int _CountCreated = 0;
private static int _CountDisposed = 0;
private static int _CountFinalized = 0;
private static int IncrementCountCreated
{ get { return ++_CountCreated; } }
private int _CountWhenCreated = IncrementCountCreated;
public static int CountCreated
{ get { return _CountCreated; } }
public static int CountNotDisposed
{ get { return _CountCreated - _CountDisposed; } }
public static int CountNotFinalized
{ get { return _CountCreated - _CountFinalized; } }
private bool _Finalized = false;
~VlnFlexGrid()
{
if(!_Finalized) _CountFinalized++;
_Finalized = true;
}
private C1SpellChecker _SpellChecker;
public C1SpellChecker SpellChecker
{
get { return _SpellChecker; }
@@ -185,7 +203,16 @@ namespace Volian.Controls.Library
return string.Empty;
return xd.SelectSingleNode("C1FlexGrid/MergedRanges").InnerXml;
}
public static GridCopyInfo MyCopyInfo = new GridCopyInfo();
private static GridCopyInfo _MyCopyInfo;
public static GridCopyInfo MyCopyInfo
{
get
{
if (_MyCopyInfo == null)
_MyCopyInfo = new GridCopyInfo();
return _MyCopyInfo;
}
}
private Color _DefaultCellBackgroundcolor;
public Color DefaultCellBackgroundcolor
@@ -646,26 +673,28 @@ namespace Volian.Controls.Library
//int curRowHeightNLines = curRowHeight / (Rows.DefaultSize - 3);
if (curColWidth <= Cols.DefaultSize - 3) return 0; // never have col less than default width
int blankColSpace = curColWidth;
for (int r = 0; r < Rows.Count; r++)
{
StepRTB srtb = new StepRTB();
CellRange cr = GetMergedRange(r, Col);
srtb.Rtf = GetCellRTFString(cr.r1, cr.c1);
if (Col >= cr.c1 && Col <= cr.c2)
{
int cellWidth = GetCellWidth(r, Col);
//srtb.Width = cellWidth;
srtb.AdjustWidthForContent();
//Application.DoEvents();
//int mergeCellHeightNLines = cellHeight / (Rows.DefaultSize - 3);
//int dataHeight = (cr.UserData == null) ? cellHeight : (int)cr.UserData;
//int ud = dataHeight / (Rows.DefaultSize - 3);
//if (cellHeight < dataHeight)
// Console.WriteLine("r {0}, c {1}, cell{2}, data{3}", Row, c, cellHeight, dataHeight);
//blankColSpace = Math.Min(blankColSpace, Math.Max(0, cellWidth - srtb.MaxTextWidth));
blankColSpace = Math.Min(blankColSpace, Math.Max(0, cellWidth - srtb.Width));
}
}
for (int r = 0; r < Rows.Count; r++)
{
using (StepRTB srtb = new StepRTB())
{
CellRange cr = GetMergedRange(r, Col);
srtb.Rtf = GetCellRTFString(cr.r1, cr.c1);
if (Col >= cr.c1 && Col <= cr.c2)
{
int cellWidth = GetCellWidth(r, Col);
//srtb.Width = cellWidth;
srtb.AdjustWidthForContent();
//Application.DoEvents();
//int mergeCellHeightNLines = cellHeight / (Rows.DefaultSize - 3);
//int dataHeight = (cr.UserData == null) ? cellHeight : (int)cr.UserData;
//int ud = dataHeight / (Rows.DefaultSize - 3);
//if (cellHeight < dataHeight)
// Console.WriteLine("r {0}, c {1}, cell{2}, data{3}", Row, c, cellHeight, dataHeight);
//blankColSpace = Math.Min(blankColSpace, Math.Max(0, cellWidth - srtb.MaxTextWidth));
blankColSpace = Math.Min(blankColSpace, Math.Max(0, cellWidth - srtb.Width));
}
}
}
//Console.WriteLine("BlankRowSpace {0}", blankRowSpace);
return blankColSpace;
}
@@ -981,116 +1010,116 @@ namespace Volian.Controls.Library
// //Console.WriteLine("LeaveEdit Style = {0}", this.GetCellRange(e.Row, e.Col).Style.Name);
// this.GetCellRange(e.Row, e.Col).Style.ForeColor = Color.Black;
//}
private void Grid_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{
private void Grid_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{
using (RTF _rtf = new RTF())
{
// use nearest solid color
// (the RTF control doesn't dither, and doesn't support transparent backgrounds)
Color solid = e.Graphics.GetNearestColor(e.Style.BackColor);
DPI = e.Graphics.DpiX;
if (e.Style.BackColor != solid)
e.Style.BackColor = solid;
// use nearest solid color
// (the RTF control doesn't dither, and doesn't support transparent backgrounds)
Color solid = e.Graphics.GetNearestColor(e.Style.BackColor);
DPI = e.Graphics.DpiX;
if (e.Style.BackColor != solid)
e.Style.BackColor = solid;
// check whether the cell contains RTF
string rtfText = this.GetDataDisplay(e.Row, e.Col).Replace(@"\~", @"\u160?");
GridItem gi = Parent as GridItem;
if (gi != null)
{
DisplayText dt = new DisplayText(gi.MyItemInfo, rtfText, true);
rtfText = dt.StartText;
}
// it does, so draw background
e.DrawCell(DrawCellFlags.Background);
if (rtfText.StartsWith(@"{\rtf"))
{
// Please add a comment if this value needs to be changed
_rtf.Rtf = rtfText;
// draw the RTF text
if (e.Bounds.Width > 0 && e.Bounds.Height > 0)
{
_rtf.Width = e.Bounds.Width - 1; // This has also been -3 which matchs the rener command
CellRange cr = GetMergedRange(e.Row, e.Col);
cr.UserData = _rtf.Height;
int hAdjust = 0;
int hDiff = e.Bounds.Height - _rtf.Height;
if (hDiff < 0)
{
Rows[e.Row].Height = _rtf.Height + 4;
AdjustGridControlSize();
hDiff = 0;
}
if (e.Style != null)
{
switch (e.Style.TextAlign)
{
case TextAlignEnum.CenterBottom:
case TextAlignEnum.GeneralBottom:
case TextAlignEnum.LeftBottom:
case TextAlignEnum.RightBottom:
hAdjust = hDiff;
break;
case TextAlignEnum.CenterCenter:
case TextAlignEnum.GeneralCenter:
case TextAlignEnum.LeftCenter:
case TextAlignEnum.RightCenter:
hAdjust = hDiff / 2;
break;
default:
break;
}
}
if (IsRoTable)
{
_rtf.ForeColor = Color.Red;
}
else
{
_rtf.ForeColor = e.Style.ForeColor;
}
_rtf.BackColor = e.Style.BackColor;
_rtf.Render(e.Graphics, new Rectangle(e.Bounds.X + 1, e.Bounds.Y + hAdjust, e.Bounds.Width - 3, e.Bounds.Height));
}
}
else
_rtf.Text = rtfText;
// check whether the cell contains RTF
string rtfText = this.GetDataDisplay(e.Row, e.Col).Replace(@"\~", @"\u160?");
GridItem gi = Parent as GridItem;
if (gi != null)
{
DisplayText dt = new DisplayText(gi.MyItemInfo, rtfText, true);
rtfText = dt.StartText;
}
// it does, so draw background
e.DrawCell(DrawCellFlags.Background);
if (rtfText.StartsWith(@"{\rtf"))
{
// Please add a comment if this value needs to be changed
_rtf.Rtf = rtfText;
// draw the RTF text
if (e.Bounds.Width > 0 && e.Bounds.Height > 0)
{
_rtf.Width = e.Bounds.Width - 1; // This has also been -3 which matchs the rener command
CellRange cr = GetMergedRange(e.Row, e.Col);
cr.UserData = _rtf.Height;
int hAdjust = 0;
int hDiff = e.Bounds.Height - _rtf.Height;
if (hDiff < 0)
{
Rows[e.Row].Height = _rtf.Height + 4;
AdjustGridControlSize();
hDiff = 0;
}
if (e.Style != null)
{
switch (e.Style.TextAlign)
{
case TextAlignEnum.CenterBottom:
case TextAlignEnum.GeneralBottom:
case TextAlignEnum.LeftBottom:
case TextAlignEnum.RightBottom:
hAdjust = hDiff;
break;
case TextAlignEnum.CenterCenter:
case TextAlignEnum.GeneralCenter:
case TextAlignEnum.LeftCenter:
case TextAlignEnum.RightCenter:
hAdjust = hDiff / 2;
break;
default:
break;
}
}
if (IsRoTable)
{
_rtf.ForeColor = Color.Red;
}
else
{
_rtf.ForeColor = e.Style.ForeColor;
}
_rtf.BackColor = e.Style.BackColor;
_rtf.Render(e.Graphics, new Rectangle(e.Bounds.X + 1, e.Bounds.Y + hAdjust, e.Bounds.Width - 3, e.Bounds.Height));
}
}
else
_rtf.Text = rtfText;
// spell check unless (we're just measuring)
if (!e.Measuring)
{
CharRangeList errors = SpellChecker.CheckText(_rtf.Text);//(text);
// underline errors
if (errors.Count > 0)
{
CharacterRange[] ranges = new CharacterRange[1];
ranges[0] = new CharacterRange(0, 1);
Pen pn = new Pen(Color.Green, 2);
StringFormat sf = new StringFormat(e.Style.StringFormat);
sf.SetMeasurableCharacterRanges(ranges);
Rectangle rc = e.Style.GetTextRectangle(e.Bounds, null);
Region[] rgns = e.Graphics.MeasureCharacterRanges(_rtf.Text, e.Style.Font, rc, sf);//(text, e.Style.Font, rc, sf);
float btm = rgns[0].GetBounds(e.Graphics).Bottom;
float top = rgns[0].GetBounds(e.Graphics).Top;
int adj = (int)(btm - top);
for (int i = 0; i < errors.Count; i++)
{
// The positioning of the the squigle line does not account for mixed font sizes
// should we support having mixed fonts in the table cells, we will need to add
// logic to support it.
Point ptStart = _rtf.GetPositionFromCharIndex(errors[i].Start);
Point ptEnd = _rtf.GetPositionFromCharIndex(errors[i].Start + errors[i].Length);
SquigleLine(e.Graphics, e.Bounds.Left + ptStart.X, e.Bounds.Top + ptStart.Y + adj, e.Bounds.Left + ptEnd.X);
}
}
}
// and draw border last
//e.DrawCell(DrawCellFlags.Border);
// This can be used to draw more specific borders
DrawCellBorder(e);
// we're done with this cell
e.Handled = true;
// spell check unless (we're just measuring)
if (!e.Measuring)
{
CharRangeList errors = SpellChecker.CheckText(_rtf.Text);//(text);
// underline errors
if (errors.Count > 0)
{
CharacterRange[] ranges = new CharacterRange[1];
ranges[0] = new CharacterRange(0, 1);
Pen pn = new Pen(Color.Green, 2);
StringFormat sf = new StringFormat(e.Style.StringFormat);
sf.SetMeasurableCharacterRanges(ranges);
Rectangle rc = e.Style.GetTextRectangle(e.Bounds, null);
Region[] rgns = e.Graphics.MeasureCharacterRanges(_rtf.Text, e.Style.Font, rc, sf);//(text, e.Style.Font, rc, sf);
float btm = rgns[0].GetBounds(e.Graphics).Bottom;
float top = rgns[0].GetBounds(e.Graphics).Top;
int adj = (int)(btm - top);
for (int i = 0; i < errors.Count; i++)
{
// The positioning of the the squigle line does not account for mixed font sizes
// should we support having mixed fonts in the table cells, we will need to add
// logic to support it.
Point ptStart = _rtf.GetPositionFromCharIndex(errors[i].Start);
Point ptEnd = _rtf.GetPositionFromCharIndex(errors[i].Start + errors[i].Length);
SquigleLine(e.Graphics, e.Bounds.Left + ptStart.X, e.Bounds.Top + ptStart.Y + adj, e.Bounds.Left + ptEnd.X);
}
}
}
// and draw border last
//e.DrawCell(DrawCellFlags.Border);
// This can be used to draw more specific borders
DrawCellBorder(e);
// we're done with this cell
e.Handled = true;
}
}
}
private void SquigleLine(Graphics graphics, int x1, int y1, int x2)
{
@@ -1386,28 +1415,32 @@ namespace Volian.Controls.Library
{
// only used in test program
CellRange cr = this.Selection;
StepRTB srtb = new StepRTB();
srtb.Rtf = this.GetCellRTFString(cr.r1, cr.c1);
using (StepRTB srtb = new StepRTB())
{
srtb.Rtf = this.GetCellRTFString(cr.r1, cr.c1);
srtb.SelectAll();
int align = (int)srtb.SelectionAlignment;
align = (align + 1) % 3;
srtb.SelectAll();
int align = (int)srtb.SelectionAlignment;
align = (align + 1) % 3;
srtb.SelectionAlignment = (HorizontalAlignment)align;
this.PutCellRTFString(cr.r1, cr.c1, srtb.Rtf);
srtb.SelectionAlignment = (HorizontalAlignment)align;
this.PutCellRTFString(cr.r1, cr.c1, srtb.Rtf);
}
}
public void RTFTextAlignment(CellRange cr, HorizontalAlignment ha)
{
StepRTB srtb = new StepRTB();
for (int rw = cr.r1; rw <= cr.r2; rw++)
for (int cl = cr.c1; cl <= cr.c2; cl++)
{
srtb.Rtf = this.GetCellRTFString(rw, cl);
srtb.SelectAll();
srtb.SelectionAlignment = ha;
this.PutCellRTFString(rw, cl, srtb.Rtf);
}
using (StepRTB srtb = new StepRTB())
{
for (int rw = cr.r1; rw <= cr.r2; rw++)
for (int cl = cr.c1; cl <= cr.c2; cl++)
{
srtb.Rtf = this.GetCellRTFString(rw, cl);
srtb.SelectAll();
srtb.SelectionAlignment = ha;
this.PutCellRTFString(rw, cl, srtb.Rtf);
}
}
}
@@ -1578,52 +1611,53 @@ namespace Volian.Controls.Library
public void ConvertTextCellToRTF(int r, int c)
{
StepRTB trtb = new StepRTB();
string tstr = null;
bool dummyCharWidth = false;
bool AllowWidthShrink = false;
trtb.SetTableGridCellRTFPrefix(this.Font);
trtb.Clear();
tstr = (string)this[r, c];
trtb.Font = this.Font;
if (tstr != null && tstr.Length > 0)
using (StepRTB trtb = new StepRTB())
{
string tsave = tstr;
// regular text has special characters to toggle Bold, Underline, and Italics
// we need to subtract the width of these characters (allow column/row to shrink)
//AllowWidthShrink = RemoveBoldUlineItalicChars(trtb.Rtf);
AllowWidthShrink = tstr.Contains("#Link:");
// this will convert the special characters for Bold, Underline, and Italics
// into RTF commands
//trtb.Rtf = trtb.RtfPrefix + ConvertTableText(trtb.Rtf) + "}";
//string fromRTF = trtb.Rtf;
//string prefix = trtb.RtfPrefix;
//if (tstr.Contains("#Link:")) prefix += @"{\colortbl ;\red255\green0\blue0;}";
//if (tstr.Contains("#Link:"))
// Console.WriteLine("here");
//string jText = trtb.RtfPrefix + ConvertTableText(tstr) + @"\par}";
//trtb.Rtf = jText;
trtb.Rtf = trtb.RtfPrefix + ConvertTableText(tstr) + @"\par}";
//string fromStr = trtb.Rtf;
//if (fromRTF.Contains("SimSun"))
// Console.WriteLine("SimSun");
//else
// Compare(fromRTF, fromStr, tsave);
}
else
{
trtb.Text = "X"; // this is to trick steprtf in giving a char width to fit one character
// note that a space character was too small.
dummyCharWidth = true;
}
// find the needed cell width
trtb.AdjustWidthForContent();
if (dummyCharWidth)
{
trtb.Text = ""; // clear out the dummy character before saving
dummyCharWidth = false;
}
string tstr = null;
bool dummyCharWidth = false;
bool AllowWidthShrink = false;
trtb.SetTableGridCellRTFPrefix(this.Font);
trtb.Clear();
tstr = (string)this[r, c];
trtb.Font = this.Font;
if (tstr != null && tstr.Length > 0)
{
string tsave = tstr;
// regular text has special characters to toggle Bold, Underline, and Italics
// we need to subtract the width of these characters (allow column/row to shrink)
//AllowWidthShrink = RemoveBoldUlineItalicChars(trtb.Rtf);
AllowWidthShrink = tstr.Contains("#Link:");
// this will convert the special characters for Bold, Underline, and Italics
// into RTF commands
//trtb.Rtf = trtb.RtfPrefix + ConvertTableText(trtb.Rtf) + "}";
//string fromRTF = trtb.Rtf;
//string prefix = trtb.RtfPrefix;
//if (tstr.Contains("#Link:")) prefix += @"{\colortbl ;\red255\green0\blue0;}";
//if (tstr.Contains("#Link:"))
// Console.WriteLine("here");
//string jText = trtb.RtfPrefix + ConvertTableText(tstr) + @"\par}";
//trtb.Rtf = jText;
trtb.Rtf = trtb.RtfPrefix + ConvertTableText(tstr) + @"\par}";
//string fromStr = trtb.Rtf;
//if (fromRTF.Contains("SimSun"))
// Console.WriteLine("SimSun");
//else
// Compare(fromRTF, fromStr, tsave);
}
else
{
trtb.Text = "X"; // this is to trick steprtf in giving a char width to fit one character
// note that a space character was too small.
dummyCharWidth = true;
}
// find the needed cell width
trtb.AdjustWidthForContent();
if (dummyCharWidth)
{
trtb.Text = ""; // clear out the dummy character before saving
dummyCharWidth = false;
}
this[r, c] = trtb.Rtf; // save the cleaned up and processed cell text as RTF
this.Select(r, c, false);
CellRange sel = this.Selection;
@@ -1701,41 +1735,44 @@ namespace Volian.Controls.Library
// }
// }
//}
}
}
private void AdjustCellHeightWidth(int r, int c)
{
StepRTB trtb = new StepRTB();
string tstr = null;
bool dummyCharWidth = false;
bool AllowWidthShrink = false;
trtb.SetTableGridCellRTFPrefix(this.Font);
trtb.Clear();
trtb.Font = this.Font;
trtb.Rtf = (string)this[r, c];
tstr = trtb.Text;
if (tstr != null && tstr.Length > 0)
AllowWidthShrink = tstr.Contains("#Link:");
else
{
trtb.Text = "X"; // this is to trick steprtf in giving a char width to fit one character
// note that a space character was too small.
dummyCharWidth = true;
}
// find the needed cell width
trtb.AdjustWidthForContent();
private void AdjustCellHeightWidth(int r, int c)
{
using (StepRTB trtb = new StepRTB())
{
string tstr = null;
bool dummyCharWidth = false;
bool AllowWidthShrink = false;
trtb.SetTableGridCellRTFPrefix(this.Font);
trtb.Clear();
trtb.Font = this.Font;
trtb.Rtf = (string)this[r, c];
tstr = trtb.Text;
if (tstr != null && tstr.Length > 0)
AllowWidthShrink = tstr.Contains("#Link:");
else
{
trtb.Text = "X"; // this is to trick steprtf in giving a char width to fit one character
// note that a space character was too small.
dummyCharWidth = true;
}
// find the needed cell width
trtb.AdjustWidthForContent();
if (dummyCharWidth)
{
trtb.Text = ""; // clear out the dummy character before saving
dummyCharWidth = false;
}
if (dummyCharWidth)
{
trtb.Text = ""; // clear out the dummy character before saving
dummyCharWidth = false;
}
this.Select(r, c, false);
CellRange sel = this.Selection;
this.Select(r, c, false);
CellRange sel = this.Selection;
// Now adjust the Height and Width in the defined merge ranges
AdjustMergeRangeHeightWidth(r, c, trtb, tstr, AllowWidthShrink);
}
// Now adjust the Height and Width in the defined merge ranges
AdjustMergeRangeHeightWidth(r, c, trtb, tstr, AllowWidthShrink);
}
}
private void AdjustMergeRangeHeightWidth(int r, int c, StepRTB trtb, string tstr, bool AllowWidthShrink)
{
@@ -2043,13 +2080,15 @@ namespace Volian.Controls.Library
{
CellRange cr = GetMergedRange(r, Col);
// find the needed cell width
if (Col == cr.c1)
{
StepRTB srtb = new StepRTB();
srtb.Rtf = GetCellRTFString(cr.r1, cr.c1);
srtb.AdjustWidthForContent();
curColWidth = Math.Max(curColWidth, srtb.Width + 3);
}
if (Col == cr.c1)
{
using (StepRTB srtb = new StepRTB())
{
srtb.Rtf = GetCellRTFString(cr.r1, cr.c1);
srtb.AdjustWidthForContent();
curColWidth = Math.Max(curColWidth, srtb.Width + 3);
}
}
}
return curColWidth;
}
@@ -2230,11 +2269,13 @@ namespace Volian.Controls.Library
string rtfText = GetDataDisplay(r, c);
if (rtfText.StartsWith(@"{\rtf"))
{
RTF _rtf = new RTF();
_rtf.Width = Cols[c].Width;
_rtf.Rtf = rtfText;
CellRange cr = GetCellRange(r, c);
cr.UserData = _rtf.ContentsRectangle.Height;
using (StepRTB _rtf = new StepRTB())
{
_rtf.Width = Cols[c].Width;
_rtf.Rtf = rtfText;
CellRange cr = GetCellRange(r, c);
cr.UserData = _rtf.ContentsRectangle.Height;
}
}
}
}
@@ -4156,11 +4197,13 @@ namespace Volian.Controls.Library
{
foreach (CellRange cr in MySelection)
{
StepRTB rtb = new StepRTB();
rtb.Rtf = GetCellRTFString(cr.r1, cr.c1);
rtb.SelectAll();
rtb.SelectionFont = new Font(rtb.SelectionFont, FontStyle.Bold);
this[cr.r1, cr.c1] = rtb.Rtf;
using (StepRTB rtb = new StepRTB())
{
rtb.Rtf = GetCellRTFString(cr.r1, cr.c1);
rtb.SelectAll();
rtb.SelectionFont = new Font(rtb.SelectionFont, FontStyle.Bold);
this[cr.r1, cr.c1] = rtb.Rtf;
}
}
}
@@ -4205,7 +4248,10 @@ namespace Volian.Controls.Library
public enum GridCopyOption { Row, Column, Selection };
#region RTF Class for Cell rendering
class RTF : StepRTB //RichTextBox
/// <summary>
/// RTF Should only be used if the contents of the richtextbox are to be converted to an image.
/// </summary>
class RTF : StepRTB
{
// messages used by RichEd20.dll
internal const int
@@ -4351,7 +4397,10 @@ namespace Volian.Controls.Library
this.Leave += new EventHandler(_editor_Leave);
//this.HeightChanged += new StepRTBEvent(_HeightChanged);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
// start editing: move to cell and activate
public void StartEditing(int row, int col)
{