Skip to content

Commit d41a173

Browse files
author
maxim.vakhrushev
committed
AR-25175: Use ExpressionInfo type for RTF property in C# RtfControl
1 parent 0b5967c commit d41a173

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

Advanced/PageAndRDL/RtfControl/C#/Rtf/Control/RichTextBoxFixed.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23
using System.Windows.Forms;
34

45
using GrapeCity.ActiveReports.Samples.Rtf.Native;
@@ -52,4 +53,19 @@ private int GetTextLength(int gtl)
5253
return UnsafeNativeMethods.SendMessage(Handle, NativeMethods.EM_GETTEXTLENGTHEX, ref getLength, 0);
5354
}
5455
}
56+
57+
public static class RichTextBoxExtensions
58+
{
59+
public static void SetRtfOrText(this RichTextBox rtb, string rtf)
60+
{
61+
try
62+
{
63+
rtb.Rtf = rtf;
64+
}
65+
catch (ArgumentException)
66+
{
67+
rtb.Text = rtf;
68+
}
69+
}
70+
}
5571
}

Advanced/PageAndRDL/RtfControl/C#/Rtf/Rendering/Layout/RtfControlLayoutManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ private void ResizeBoxToContent(object sender, ContentsResizedEventArgs e)
8787

8888
private SizeF GetNeededSize()
8989
{
90-
var rtf = _control.Rtf;
9190
var width = TwipsToPixels(_computedSize.Width);
9291

9392
using (var box = new RichTextBoxFixed())
9493
{
9594
box.ContentsResized += ResizeBoxToContent;
9695
box.Width = width;
97-
box.Rtf = rtf;
96+
box.SetRtfOrText(_control.Rtf);
9897

9998
return new SizeF(PixelsToTwips(box.Size.Width), PixelsToTwips(box.Size.Height));
10099
}
@@ -132,7 +131,7 @@ private string GetNextRtf(float to)
132131
{
133132
rtb.ContentsResized += ResizeBoxToContent;
134133
rtb.Width = TwipsToPixels(_computedSize.Width);
135-
rtb.Rtf = _control.Rtf;
134+
rtb.SetRtfOrText(_control.Rtf ?? string.Empty);
136135

137136
_lastChar = rtb.GetCharIndexFromPosition(new Point(0, bottom + rtb.PreferredHeight));
138137
rtb.Select(_firstChar, _lastChar - _firstChar);

Advanced/PageAndRDL/RtfControl/C#/Rtf/Rendering/RtfRenderer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Drawing.Imaging;
55
using System.Windows.Forms;
66
using System.Runtime.InteropServices;
7+
78
using GrapeCity.ActiveReports.Samples.Rtf.Native;
9+
using GrapeCity.ActiveReports.Samples.Rtf.Control;
810

911
namespace GrapeCity.ActiveReports.Samples.Rtf.Rendering
1012
{
@@ -29,7 +31,7 @@ private static Image RenderImage(string rtfContent, SizeF sizeInInches, Func<Int
2931
{
3032
using (var richText = new RichTextBox())
3133
{
32-
richText.Rtf = rtfContent;
34+
richText.SetRtfOrText(rtfContent);
3335
richText.CreateControl();
3436

3537
using (var g = richText.CreateGraphics())

Advanced/PageAndRDL/RtfControl/C#/RtfDesigner/RtfDesigner.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using GrapeCity.ActiveReports.PageReportModel;
1313
using GrapeCity.ActiveReports.Design.DdrDesigner.Behavior;
1414
using GrapeCity.ActiveReports.Design.DdrDesigner.Designers;
15+
using GrapeCity.ActiveReports.Design.DdrDesigner.Tools;
1516
using GrapeCity.ActiveReports.Samples.Rtf.Rendering;
1617
using GrapeCity.Enterprise.Data.DataEngine.Expressions;
1718

@@ -39,8 +40,7 @@ public RtfDesigner()
3940
CategoryAttribute.Data,
4041
new DisplayNameAttribute(Properties.Resources.PropertyRtf),
4142
new DescriptionAttribute(Properties.Resources.PropertyRtfDescription),
42-
new EditorAttribute(typeof(MultilineStringEditor),
43-
typeof(UITypeEditor))
43+
new EditorAttribute(typeof(ExpressionInfoUITypeEditor), typeof(UITypeEditor))
4444
);
4545

4646
AddProperty(this, x => x.CanGrow,
@@ -118,17 +118,14 @@ protected override void OnComponentChanged(object sender, ComponentChangedEventA
118118

119119
#region Public properties
120120

121-
public string Rtf { get; set; }
121+
public ExpressionInfo Rtf { get; set; } = ExpressionInfo.EmptyString;
122122

123123
public string GetRtf()
124124
{
125125
var prop = TypeDescriptor.GetProperties(Component)[RTF_FIELD_NAME];
126-
var rtf = prop.GetValue(Component);
126+
var rtf = (ExpressionInfo) prop.GetValue(Component);
127127

128-
if (rtf != null)
129-
return rtf.ToString();
130-
131-
return string.Empty;
128+
return rtf.IsEmptyString ? string.Empty : rtf.ToString();
132129
}
133130

134131
public void SetRtf(string rtf)
@@ -138,7 +135,7 @@ public void SetRtf(string rtf)
138135
var prop = TypeDescriptor.GetProperties(Component)[RTF_FIELD_NAME];
139136
var result = string.Equals(rtf, ExpressionInfo.EmptyString.ToString()) ? string.Empty : rtf;
140137

141-
prop.SetValue(Component, result);
138+
prop.SetValue(Component, ExpressionInfo.FromString(result));
142139
ReportItem.CustomProperties[RTF_FIELD_NAME].Value = result;
143140
}
144141

@@ -167,7 +164,7 @@ internal System.Drawing.Image RenderedRtf
167164
{
168165
get
169166
{
170-
if (string.IsNullOrWhiteSpace(Rtf))
167+
if (Rtf.IsEmptyString)
171168
return null;
172169

173170
if (_metafile != null)

Advanced/PageAndRDL/RtfControl/C#/RtfDesigner/RtfEditor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Drawing;
33
using System.Windows.Forms;
44

5-
using GrapeCity.ActiveReports.Samples.Rtf.Control;
65
using GrapeCity.ActiveReports.Samples.Rtf.Native;
6+
using GrapeCity.ActiveReports.Samples.Rtf.Control;
77

88
namespace GrapeCity.ActiveReports.Samples.Rtf
99
{
@@ -42,12 +42,10 @@ public void Deactivate(bool save)
4242

4343
if (save)
4444
{
45-
_designer.SetRtf(Rtf);
45+
_designer.SetRtf(Text.TrimStart().StartsWith("=") ? Text : Rtf);
4646
}
4747
else
4848
{
49-
_designer.SetRtf(_designer.GetRtf()); // force redraw without saving
50-
5149
while (CanUndo)
5250
Undo();
5351
}
@@ -119,7 +117,8 @@ private void EditorLostFocus(object sender, EventArgs e)
119117

120118
private void SyncWithSource()
121119
{
122-
Rtf = _designer.GetRtf();
120+
this.SetRtfOrText(_designer.GetRtf());
121+
123122
Size = _designer.Size;
124123
Location = Point.Empty;
125124
Margin = Padding.Empty;

0 commit comments

Comments
 (0)