From c3cc1f3417f4c78a5d6a33f9a5209bd28744915b Mon Sep 17 00:00:00 2001 From: Rich Date: Fri, 11 Feb 2011 21:14:41 +0000 Subject: [PATCH] Dispose RTF in OwnerDraw Added Properties and Methods for UnEven Selection --- PROMS/Volian.Controls.Library/VlnFlexGrid.cs | 204 ++++++++++++++++--- 1 file changed, 174 insertions(+), 30 deletions(-) diff --git a/PROMS/Volian.Controls.Library/VlnFlexGrid.cs b/PROMS/Volian.Controls.Library/VlnFlexGrid.cs index 207f95a2..e35c4387 100644 --- a/PROMS/Volian.Controls.Library/VlnFlexGrid.cs +++ b/PROMS/Volian.Controls.Library/VlnFlexGrid.cs @@ -311,39 +311,41 @@ namespace Volian.Controls.Library private void Grid_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e) { - 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); - if (e.Style.BackColor != solid) - e.Style.BackColor = solid; - - // check whether the cell contains RTF - string rtfText = this.GetDataDisplay(e.Row, e.Col); - if (rtfText.StartsWith(@"{\rtf")) + using (RTF _rtf = new RTF()) { - // it does, so draw background - e.DrawCell(DrawCellFlags.Background); + // use nearest solid color + // (the RTF control doesn't dither, and doesn't support transparent backgrounds) + Color solid = e.Graphics.GetNearestColor(e.Style.BackColor); + if (e.Style.BackColor != solid) + e.Style.BackColor = solid; - // draw the RTF text - if (e.Bounds.Width > 0 && e.Bounds.Height > 0) + // check whether the cell contains RTF + string rtfText = this.GetDataDisplay(e.Row, e.Col); + if (rtfText.StartsWith(@"{\rtf")) { - _rtf.Width = Cols[e.Col].Width; - _rtf.Rtf = rtfText; - _rtf.ForeColor = e.Style.ForeColor; - _rtf.BackColor = e.Style.BackColor; - _rtf.Render(e.Graphics, e.Bounds); - //CellRange cr = GetCellRange(e.Row, e.Col); - //Console.WriteLine("ownerDraw UserData [{0},{1}] = {2}", cr.r1, cr.c1, _rtf.ContentsRectangle.Height); - //cr.UserData = _rtf.ContentsRectangle.Height; - } + // it does, so draw background + e.DrawCell(DrawCellFlags.Background); - // and draw border last - e.DrawCell(DrawCellFlags.Border); - // This can be used to draw more specific borders - // DrawCellBorder(e.Graphics, e.Bounds,e.Row,e.Col); - // we're done with this cell - e.Handled = true; + // draw the RTF text + if (e.Bounds.Width > 0 && e.Bounds.Height > 0) + { + _rtf.Width = Cols[e.Col].Width; + _rtf.Rtf = rtfText; + _rtf.ForeColor = e.Style.ForeColor; + _rtf.BackColor = e.Style.BackColor; + _rtf.Render(e.Graphics, e.Bounds); + //CellRange cr = GetCellRange(e.Row, e.Col); + //Console.WriteLine("ownerDraw UserData [{0},{1}] = {2}", cr.r1, cr.c1, _rtf.ContentsRectangle.Height); + //cr.UserData = _rtf.ContentsRectangle.Height; + } + + // and draw border last + e.DrawCell(DrawCellFlags.Border); + // This can be used to draw more specific borders + // DrawCellBorder(e.Graphics, e.Bounds,e.Row,e.Col); + // we're done with this cell + e.Handled = true; + } } } private void DrawCellBorder(Graphics graphics, Rectangle rectangle, int row, int col) @@ -1897,7 +1899,149 @@ namespace Volian.Controls.Library // } //} - } + #region Uneven Selections + /// + /// True if the top and bottom row of the selection is the same for every column + /// + public bool EvenSelection + { + get + { + if (Selection.c1 < 0 || Selection.c2 < 0 || Selection.r1 < 0 || Selection.r2 < 0) return true; // Even if there is no selection + if (Selection.IsSingleCell) return true; // One cell by definition is Even + int cMin = MinimumColumn(Selection.r1, Selection.c1); // Get the left column of the top row + int cMax = MaximumColumn(Selection.r1, Selection.c2); // Get the right column of the top row + for (int r = Selection.r1 + 1; r <= Selection.r2; r++) + { + // Uneven if the top row in this column doesn't match the first column + if (MinimumColumn(r, Selection.c1) != cMin) + return false; + // Uneven if the bottom row in this column doesn't match the first column + if (MaximumColumn(r, Selection.c2) != cMax) + return false; + } + int rMin = MinimumRow(Selection.r1, Selection.c1); // Get the top row of the left column + int rMax = MaximumRow(Selection.r2, Selection.c1); // Get the bottom row of the left column + for (int c = Selection.c1 + 1; c <= Selection.c2; c++) + { + // Uneven if the top row in this column doesn't match the first column + if (MinimumRow(Selection.r1, c) != rMin) + return false; + // Uneven if the bottom row in this column doesn't match the first column + if (MaximumRow(Selection.r2, c) != rMax) + return false; + } + return true; + } + } + public void MakeSelectionEven() + { + if (Selection.IsSingleCell) return; // One cell by definition is Even + MakeSelectionEven(Selection.r1, Selection.c1, Selection.r2, Selection.c2); + } + public void SelectRow() + { + MakeSelectionEven(Selection.r1, 0, Selection.r2, Cols.Count - 1); + } + public void SelectCol() + { + MakeSelectionEven(0, Selection.c1, Rows.Count - 1, Selection.c2); + } + public void MakeSelectionEven(int r1,int c1, int r2, int c2) + { + if (c1 < 0 || c2 < 0 || r1 < 0 || r2 < 0) return; // if there is no selection don't bother + int cMinNew = MinimumColumn(r1, c1); // Get the left column of the top row + int cMaxNew = MaximumColumn(r1, c2); // Get the right column of the top row + int rMinNew = MinimumRow(r1, c1); // Get the top row of the left column + int rMaxNew = MaximumRow(r2, c1); // Get the bottom row of the left column + int cMin = cMinNew; + int cMax = cMaxNew; + int rMin = rMinNew; + int rMax = rMaxNew; + do + { + cMin = cMinNew; + cMax = cMaxNew; + rMin = rMinNew; + rMax = rMaxNew; + for (int r = rMin; r <= rMax; r++) + { + // Uneven if the top row in this column doesn't match the first column + int myMin = MinimumColumn(r, cMin); + if (myMin < cMinNew) + cMinNew = myMin; + // Uneven if the bottom row in this column doesn't match the first column + int myMax = MaximumColumn(r, cMax); + if (myMax > cMaxNew) + cMaxNew = myMax; + } + for (int c = cMinNew; c <= cMaxNew; c++) + { + // if the top row in this column is less than the minimum save it + int myMin = MinimumRow(rMin, c); + if (myMin < rMinNew) + rMinNew = myMin; + // Uneven if the bottom row in this column doesn't match the first column + int myMax = MaximumRow(rMax, c); + if (myMax > rMax) + rMaxNew = myMax; + } + } + while (cMinNew < cMin || cMaxNew > cMax || rMinNew < rMin || rMaxNew > rMax); + Select(rMinNew, cMinNew, rMaxNew, cMaxNew); + } + private int MinimumColumn(int r, int c) + { + return GetMergedRange(r, c).c1; + } + private int MaximumColumn(int r, int c) + { + return GetMergedRange(r, c).c2; + } + private int MinimumRow(int r, int c) + { + return GetMergedRange(r, c).r1; + } + private int MaximumRow(int r, int c) + { + return GetMergedRange(r, c).r2; + } + /// + /// Returns a list of cells or merged cells within the selection + /// + public List MySelection + { + get + { + List ranges = new List(); + for (int c = Selection.c1; c <= Selection.c2; c++) + for (int r = Selection.r1; r <= Selection.r2; r++) + { + CellRange cr = GetMergedRange(r, c); + if (!ranges.Contains(cr)) + ranges.Add(cr); + } + //} + return ranges; + } + } + /// + /// This is a test to see if MySelection works. + /// + public void BoldMySelection() + { + 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; + } + } + + #endregion + } #region RTF Class for Cell rendering class RTF : StepRTB //RichTextBox {