Skip to content

Commit e36e8be

Browse files
author
ArthurHub
committed
WPF - handle border
1 parent c79a7d9 commit e36e8be

8 files changed

Lines changed: 46 additions & 27 deletions

File tree

Source/Demo/WPF/MainControl.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public string GetHtml()
119119

120120
public void SetHtml(string html)
121121
{
122-
_htmlPanel.Html = html;
122+
_htmlPanel.Text = html;
123123
}
124124

125125

@@ -205,7 +205,7 @@ private void OnTreeView_SelectedItemChanged(object sender, RoutedPropertyChanged
205205
try
206206
{
207207
_htmlPanel.AvoidImagesLateLoading = !sample.FullName.Contains("Many images");
208-
_htmlPanel.Html = sample.Html;
208+
_htmlPanel.Text = sample.Html;
209209
}
210210
catch (Exception ex)
211211
{
@@ -241,7 +241,7 @@ private void OnUpdateHtmlTimerTick(object state)
241241

242242
try
243243
{
244-
_htmlPanel.Html = GetHtmlEditorText();
244+
_htmlPanel.Text = GetHtmlEditorText();
245245
}
246246
catch (Exception ex)
247247
{

Source/Demo/WPF/SampleWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public SampleWindow()
2626
{
2727
InitializeComponent();
2828

29-
_htmlLabel.Html = DemoUtils.SampleHtmlLabelText;
30-
_htmlPanel.Html = DemoUtils.SampleHtmlPanelText;
29+
_htmlLabel.Text = DemoUtils.SampleHtmlLabelText;
30+
_htmlPanel.Text = DemoUtils.SampleHtmlPanelText;
3131

3232
_propertyGrid.SelectedObject = _htmlLabel;
3333
}

Source/HtmlRenderer.WPF/HtmlControlBase.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public abstract class HtmlControlBase : Control
7373
/// <summary>
7474
/// the current html text set in the control
7575
/// </summary>
76-
protected string _html;
76+
protected string _text;
7777

7878
#endregion
7979

@@ -84,6 +84,7 @@ public abstract class HtmlControlBase : Control
8484
protected HtmlControlBase()
8585
{
8686
Background = SystemColors.WindowBrush;
87+
SnapsToDevicePixels = true;
8788

8889
_htmlContainer = new HtmlContainer();
8990
_htmlContainer.LinkClicked += OnLinkClicked;
@@ -183,7 +184,7 @@ public virtual string BaseStylesheet
183184
{
184185
_baseRawCssData = value;
185186
_baseCssData = HtmlRender.ParseStyleSheet(value);
186-
_htmlContainer.SetHtml(_html, _baseCssData);
187+
_htmlContainer.SetHtml(_text, _baseCssData);
187188
}
188189
}
189190

@@ -192,14 +193,14 @@ public virtual string BaseStylesheet
192193
/// </summary>
193194
[Browsable(true)]
194195
[Description("Sets the html of this control.")]
195-
public virtual string Html
196+
public virtual string Text
196197
{
197-
get { return _html; }
198+
get { return _text; }
198199
set
199200
{
200-
_html = value;
201+
_text = value;
201202
_htmlContainer.ScrollOffset = new Point(0, 0);
202-
_htmlContainer.SetHtml(_html, _baseCssData);
203+
_htmlContainer.SetHtml(_text, _baseCssData);
203204
InvalidateMeasure();
204205
InvalidateVisual();
205206
}
@@ -255,11 +256,25 @@ protected override void OnRender(DrawingContext context)
255256
if (Background != null && Background.Opacity > 0)
256257
context.DrawRectangle(Background, null, new Rect(RenderSize));
257258

259+
if (BorderThickness != new Thickness(0))
260+
{
261+
var brush = BorderBrush ?? SystemColors.ControlDarkBrush;
262+
if (BorderThickness.Top > 0)
263+
context.DrawRectangle(brush, null, new Rect(0, 0, RenderSize.Width, BorderThickness.Top));
264+
if (BorderThickness.Bottom > 0)
265+
context.DrawRectangle(brush, null, new Rect(0, RenderSize.Height - BorderThickness.Bottom, RenderSize.Width, BorderThickness.Bottom));
266+
if (BorderThickness.Left > 0)
267+
context.DrawRectangle(brush, null, new Rect(0, 0, BorderThickness.Left, RenderSize.Height));
268+
if (BorderThickness.Right > 0)
269+
context.DrawRectangle(brush, null, new Rect(RenderSize.Width - BorderThickness.Right, 0, BorderThickness.Right, RenderSize.Height));
270+
}
271+
258272
var htmlWidth = HtmlWidth(RenderSize);
259273
var htmlHeight = HtmlHeight(RenderSize);
260274
if (_htmlContainer != null && htmlWidth > 0 && htmlHeight > 0)
261275
{
262-
context.PushClip(new RectangleGeometry(new Rect(new Size(htmlWidth, htmlHeight))));
276+
context.PushClip(new RectangleGeometry(new Rect(BorderThickness.Left, BorderThickness.Top, htmlWidth, htmlHeight)));
277+
_htmlContainer.Location = new Point(BorderThickness.Left, BorderThickness.Top);
263278
_htmlContainer.PerformPaint(context, new Rect(new Size(htmlWidth, htmlHeight)));
264279
context.Pop();
265280

@@ -395,15 +410,15 @@ protected override void OnVisualParentChanged(DependencyObject oldParent)
395410
/// </summary>
396411
protected virtual double HtmlWidth(Size size)
397412
{
398-
return size.Width;
413+
return size.Width - BorderThickness.Left - BorderThickness.Right;
399414
}
400415

401416
/// <summary>
402417
/// Get the width the HTML has to render in (not including vertical scroll iff it is visible)
403418
/// </summary>
404419
protected virtual double HtmlHeight(Size size)
405420
{
406-
return size.Height;
421+
return size.Height - BorderThickness.Top - BorderThickness.Bottom;
407422
}
408423

409424

Source/HtmlRenderer.WPF/HtmlLabel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using HtmlRenderer.Adapters.Entities;
1818
using HtmlRenderer.Core;
1919
using HtmlRenderer.WPF.Adapters;
20-
using HtmlRenderer.WPF.Utilities;
2120

2221
namespace HtmlRenderer.WPF
2322
{
@@ -116,10 +115,11 @@ protected override Size MeasureOverride(Size constraint)
116115
{
117116
using (var ig = new GraphicsAdapter())
118117
{
118+
var size = new RSize(constraint.Width - BorderThickness.Left - BorderThickness.Right, constraint.Height - BorderThickness.Top - BorderThickness.Bottom);
119119
var minSize = new RSize(MinWidth < Double.PositiveInfinity ? MinWidth : 0, MinHeight < Double.PositiveInfinity ? MinHeight : 0);
120120
var maxSize = new RSize(MaxWidth < Double.PositiveInfinity ? MaxWidth : 0, MaxHeight < Double.PositiveInfinity ? MaxHeight : 0);
121-
var newSize = HtmlRendererUtils.Layout(ig, _htmlContainer.HtmlContainerInt, Utils.Convert(constraint), minSize, maxSize, AutoSize, AutoSizeHeightOnly);
122-
constraint = Utils.ConvertRound(newSize);
121+
var newSize = HtmlRendererUtils.Layout(ig, _htmlContainer.HtmlContainerInt, size, minSize, maxSize, AutoSize, AutoSizeHeightOnly);
122+
constraint = new Size(newSize.Width + BorderThickness.Left + BorderThickness.Right, newSize.Height + BorderThickness.Top + BorderThickness.Bottom);
123123
}
124124
}
125125

Source/HtmlRenderer.WPF/HtmlPanel.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ public HtmlPanel()
6868
_htmlContainer.ScrollChange += OnScrollChange;
6969
}
7070

71-
public override string Html
71+
public override string Text
7272
{
73+
get { return base.Text; }
7374
set
7475
{
7576
_horizontalScrollBar.Value = _verticalScrollBar.Value = 0;
76-
base.Html = value;
77+
base.Text = value;
7778
}
7879
}
7980

@@ -152,12 +153,12 @@ protected override Size MeasureOverride(Size constraint)
152153
/// </summary>
153154
protected override Size ArrangeOverride(Size bounds)
154155
{
155-
var scrollHeight = bounds.Height - (_horizontalScrollBar.Visibility == Visibility.Visible ? _horizontalScrollBar.Height : 0);
156+
var scrollHeight = HtmlHeight(bounds);
156157
scrollHeight = scrollHeight > 1 ? scrollHeight : 1;
157-
var scrollWidth = bounds.Width - (_verticalScrollBar.Visibility == Visibility.Visible ? _verticalScrollBar.Width : 0);
158+
var scrollWidth = HtmlWidth(bounds);
158159
scrollWidth = scrollWidth > 1 ? scrollWidth : 1;
159-
_verticalScrollBar.Arrange(new Rect(System.Math.Max(bounds.Width - _verticalScrollBar.Width, 0), 0, _verticalScrollBar.Width, scrollHeight));
160-
_horizontalScrollBar.Arrange(new Rect(0, System.Math.Max(bounds.Height - _horizontalScrollBar.Height, 0), scrollWidth, _horizontalScrollBar.Height));
160+
_verticalScrollBar.Arrange(new Rect(System.Math.Max(bounds.Width - _verticalScrollBar.Width - BorderThickness.Right, 0), BorderThickness.Top, _verticalScrollBar.Width, scrollHeight));
161+
_horizontalScrollBar.Arrange(new Rect(BorderThickness.Left, System.Math.Max(bounds.Height - _horizontalScrollBar.Height - BorderThickness.Bottom, 0), scrollWidth, _horizontalScrollBar.Height));
161162

162163
if (_htmlContainer != null)
163164
{
@@ -207,7 +208,7 @@ protected override void OnRender(DrawingContext context)
207208

208209
// render rectangle in right bottom corner where both scrolls meet
209210
if (_horizontalScrollBar.Visibility == Visibility.Visible && _verticalScrollBar.Visibility == Visibility.Visible)
210-
context.DrawRectangle(SystemColors.ControlBrush, null, new Rect(HtmlWidth(RenderSize), HtmlHeight(RenderSize), _verticalScrollBar.Width, _horizontalScrollBar.Height));
211+
context.DrawRectangle(SystemColors.ControlBrush, null, new Rect(BorderThickness.Left + HtmlWidth(RenderSize), BorderThickness.Top + HtmlHeight(RenderSize), _verticalScrollBar.Width, _horizontalScrollBar.Height));
211212
}
212213

213214
/// <summary>
@@ -302,7 +303,7 @@ protected override void OnKeyDown(KeyEventArgs e)
302303
/// </summary>
303304
protected override double HtmlWidth(Size size)
304305
{
305-
var width = size.Width - (_verticalScrollBar.Visibility == Visibility.Visible ? _verticalScrollBar.Width : 0);
306+
var width = base.HtmlWidth(size) - (_verticalScrollBar.Visibility == Visibility.Visible ? _verticalScrollBar.Width : 0);
306307
return width > 1 ? width : 1;
307308
}
308309

@@ -311,7 +312,7 @@ protected override double HtmlWidth(Size size)
311312
/// </summary>
312313
protected override double HtmlHeight(Size size)
313314
{
314-
var height = size.Height - (_horizontalScrollBar.Visibility == Visibility.Visible ? _horizontalScrollBar.Height : 0);
315+
var height = base.HtmlHeight(size) - (_horizontalScrollBar.Visibility == Visibility.Visible ? _horizontalScrollBar.Height : 0);
315316
return height > 1 ? height : 1;
316317
}
317318

Source/HtmlRenderer.WPF/HtmlRenderer.WPF.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<ItemGroup>
3737
<Reference Include="System" />
3838
<Reference Include="System.Data" />
39+
<Reference Include="System.Design" />
3940
<Reference Include="System.Xml" />
4041
<Reference Include="WindowsBase" />
4142
<Reference Include="PresentationCore" />

Source/HtmlRenderer/Core/Dom/CssBox.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ public void Paint(RGraphics g)
439439
var rect = ContainingBlock.ClientRectangle;
440440
rect.X -= 2;
441441
rect.Width += 2;
442+
rect.Offset(new RPoint(-HtmlContainer.Location.X, -HtmlContainer.Location.Y));
442443
rect.Offset(HtmlContainer.ScrollOffset);
443444
clip.Intersect(rect);
444445

@@ -636,7 +637,7 @@ protected virtual void PerformLayoutImp(RGraphics g)
636637

637638
CreateListItemBox(g);
638639

639-
var actualWidth = Math.Max(GetMinimumWidth() + GetWidthMarginDeep(this), Size.Width < 90999 ? ActualRight : 0);
640+
var actualWidth = Math.Max(GetMinimumWidth() + GetWidthMarginDeep(this), Size.Width < 90999 ? ActualRight - HtmlContainer.Root.Location.X : 0);
640641
HtmlContainer.ActualSize = CommonUtils.Max(HtmlContainer.ActualSize, new RSize(actualWidth, ActualBottom - HtmlContainer.Root.Location.Y));
641642
}
642643

Source/HtmlRenderer/Core/Utils/RenderUtils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static bool ClipGraphicsByOverflow(RGraphics g, CssBox box)
5050
var rect = box.ContainingBlock.ClientRectangle;
5151
rect.X -= 2; // TODO:a find better way to fix it
5252
rect.Width += 2;
53+
rect.Offset(new RPoint(-box.HtmlContainer.Location.X, -box.HtmlContainer.Location.Y));
5354
rect.Offset(box.HtmlContainer.ScrollOffset);
5455
rect.Intersect(prevClip);
5556
g.PushClip(rect);

0 commit comments

Comments
 (0)