PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/WinRT XAML Toolkit - 1.3.6.0 - Source.zip/WinRTXamlToolkit.StylesBrowser/Common/RichTextColumns.cs

https://gitlab.com/prabaprakash/Real_Metro_Player
C# | 210 lines | 117 code | 15 blank | 78 comment | 17 complexity | e8bd9f86953b674e0249ab2c1f5a9bb8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Windows.Foundation;
  6. using Windows.UI.Xaml;
  7. using Windows.UI.Xaml.Controls;
  8. using Windows.UI.Xaml.Data;
  9. using Windows.UI.Xaml.Documents;
  10. namespace WinRTXamlToolkit.StylesBrowser.Common
  11. {
  12. /// <summary>
  13. /// Wrapper for <see cref="RichTextBlock"/> that creates as many additional overflow
  14. /// columns as needed to fit the available content.
  15. /// </summary>
  16. /// <example>
  17. /// The following creates a collection of 400-pixel wide columns spaced 50 pixels apart
  18. /// to contain arbitrary data-bound content:
  19. /// <code>
  20. /// <RichTextColumns>
  21. /// <RichTextColumns.ColumnTemplate>
  22. /// <DataTemplate>
  23. /// <RichTextBlockOverflow Width="400" Margin="50,0,0,0"/>
  24. /// </DataTemplate>
  25. /// </RichTextColumns.ColumnTemplate>
  26. ///
  27. /// <RichTextBlock Width="400">
  28. /// <Paragraph>
  29. /// <Run Text="{Binding Content}"/>
  30. /// </Paragraph>
  31. /// </RichTextBlock>
  32. /// </RichTextColumns>
  33. /// </code>
  34. /// </example>
  35. /// <remarks>Typically used in a horizontally scrolling region where an unbounded amount of
  36. /// space allows for all needed columns to be created. When used in a vertically scrolling
  37. /// space there will never be any additional columns.</remarks>
  38. [Windows.UI.Xaml.Markup.ContentProperty(Name = "RichTextContent")]
  39. public sealed class RichTextColumns : Panel
  40. {
  41. /// <summary>
  42. /// Identifies the <see cref="RichTextContent"/> dependency property.
  43. /// </summary>
  44. public static readonly DependencyProperty RichTextContentProperty =
  45. DependencyProperty.Register("RichTextContent", typeof(RichTextBlock),
  46. typeof(RichTextColumns), new PropertyMetadata(null, ResetOverflowLayout));
  47. /// <summary>
  48. /// Identifies the <see cref="ColumnTemplate"/> dependency property.
  49. /// </summary>
  50. public static readonly DependencyProperty ColumnTemplateProperty =
  51. DependencyProperty.Register("ColumnTemplate", typeof(DataTemplate),
  52. typeof(RichTextColumns), new PropertyMetadata(null, ResetOverflowLayout));
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="RichTextColumns"/> class.
  55. /// </summary>
  56. public RichTextColumns()
  57. {
  58. this.HorizontalAlignment = HorizontalAlignment.Left;
  59. }
  60. /// <summary>
  61. /// Gets or sets the initial rich text content to be used as the first column.
  62. /// </summary>
  63. public RichTextBlock RichTextContent
  64. {
  65. get { return (RichTextBlock)GetValue(RichTextContentProperty); }
  66. set { SetValue(RichTextContentProperty, value); }
  67. }
  68. /// <summary>
  69. /// Gets or sets the template used to create additional
  70. /// <see cref="RichTextBlockOverflow"/> instances.
  71. /// </summary>
  72. public DataTemplate ColumnTemplate
  73. {
  74. get { return (DataTemplate)GetValue(ColumnTemplateProperty); }
  75. set { SetValue(ColumnTemplateProperty, value); }
  76. }
  77. /// <summary>
  78. /// Invoked when the content or overflow template is changed to recreate the column layout.
  79. /// </summary>
  80. /// <param name="d">Instance of <see cref="RichTextColumns"/> where the change
  81. /// occurred.</param>
  82. /// <param name="e">Event data describing the specific change.</param>
  83. private static void ResetOverflowLayout(DependencyObject d, DependencyPropertyChangedEventArgs e)
  84. {
  85. // When dramatic changes occur, rebuild the column layout from scratch
  86. var target = d as RichTextColumns;
  87. if (target != null)
  88. {
  89. target._overflowColumns = null;
  90. target.Children.Clear();
  91. target.InvalidateMeasure();
  92. }
  93. }
  94. /// <summary>
  95. /// Lists overflow columns already created. Must maintain a 1:1 relationship with
  96. /// instances in the <see cref="Panel.Children"/> collection following the initial
  97. /// RichTextBlock child.
  98. /// </summary>
  99. private List<RichTextBlockOverflow> _overflowColumns = null;
  100. /// <summary>
  101. /// Determines whether additional overflow columns are needed and if existing columns can
  102. /// be removed.
  103. /// </summary>
  104. /// <param name="availableSize">The size of the space available, used to constrain the
  105. /// number of additional columns that can be created.</param>
  106. /// <returns>The resulting size of the original content plus any extra columns.</returns>
  107. protected override Size MeasureOverride(Size availableSize)
  108. {
  109. if (this.RichTextContent == null) return new Size(0, 0);
  110. // Make sure the RichTextBlock is a child, using the lack of
  111. // a list of additional columns as a sign that this hasn't been
  112. // done yet
  113. if (_overflowColumns == null)
  114. {
  115. Children.Add(this.RichTextContent);
  116. _overflowColumns = new List<RichTextBlockOverflow>();
  117. }
  118. // Start by measuring the original RichTextBlock content
  119. this.RichTextContent.Measure(availableSize);
  120. var maxWidth = this.RichTextContent.DesiredSize.Width;
  121. var maxHeight = this.RichTextContent.DesiredSize.Height;
  122. var hasOverflow = this.RichTextContent.HasOverflowContent;
  123. // Make sure there are enough overflow columns
  124. int overflowIndex = 0;
  125. while (hasOverflow && maxWidth < availableSize.Width && this.ColumnTemplate != null)
  126. {
  127. // Use existing overflow columns until we run out, then create
  128. // more from the supplied template
  129. RichTextBlockOverflow overflow;
  130. if (_overflowColumns.Count > overflowIndex)
  131. {
  132. overflow = _overflowColumns[overflowIndex];
  133. }
  134. else
  135. {
  136. overflow = (RichTextBlockOverflow)this.ColumnTemplate.LoadContent();
  137. _overflowColumns.Add(overflow);
  138. this.Children.Add(overflow);
  139. if (overflowIndex == 0)
  140. {
  141. this.RichTextContent.OverflowContentTarget = overflow;
  142. }
  143. else
  144. {
  145. _overflowColumns[overflowIndex - 1].OverflowContentTarget = overflow;
  146. }
  147. }
  148. // Measure the new column and prepare to repeat as necessary
  149. overflow.Measure(new Size(availableSize.Width - maxWidth, availableSize.Height));
  150. maxWidth += overflow.DesiredSize.Width;
  151. maxHeight = Math.Max(maxHeight, overflow.DesiredSize.Height);
  152. hasOverflow = overflow.HasOverflowContent;
  153. overflowIndex++;
  154. }
  155. // Disconnect extra columns from the overflow chain, remove them from our private list
  156. // of columns, and remove them as children
  157. if (_overflowColumns.Count > overflowIndex)
  158. {
  159. if (overflowIndex == 0)
  160. {
  161. this.RichTextContent.OverflowContentTarget = null;
  162. }
  163. else
  164. {
  165. _overflowColumns[overflowIndex - 1].OverflowContentTarget = null;
  166. }
  167. while (_overflowColumns.Count > overflowIndex)
  168. {
  169. _overflowColumns.RemoveAt(overflowIndex);
  170. this.Children.RemoveAt(overflowIndex + 1);
  171. }
  172. }
  173. // Report final determined size
  174. return new Size(maxWidth, maxHeight);
  175. }
  176. /// <summary>
  177. /// Arranges the original content and all extra columns.
  178. /// </summary>
  179. /// <param name="finalSize">Defines the size of the area the children must be arranged
  180. /// within.</param>
  181. /// <returns>The size of the area the children actually required.</returns>
  182. protected override Size ArrangeOverride(Size finalSize)
  183. {
  184. double maxWidth = 0;
  185. double maxHeight = 0;
  186. foreach (var child in Children)
  187. {
  188. child.Arrange(new Rect(maxWidth, 0, child.DesiredSize.Width, finalSize.Height));
  189. maxWidth += child.DesiredSize.Width;
  190. maxHeight = Math.Max(maxHeight, child.DesiredSize.Height);
  191. }
  192. return new Size(maxWidth, maxHeight);
  193. }
  194. }
  195. }