Esempio n. 1
0
        private void RenderElementRecurse(Panel parent, ref Panel p, CaptionElement element, ref double offset, TextAlignment align)
        {
            if (element.IsActiveAtPosition(_mediaPosition) && element.CurrentStyle.Display == Visibility.Visible)
            {
                if (element.CaptionElementType == TimedTextElementType.Text)
                {
                    var text = element.Content != null?element.Content.ToString() : string.Empty;

                    offset = WrapElement(parent, ref p, offset, text, element, align);
                }
                else if (element.CaptionElementType == TimedTextElementType.Container)
                {
#if HACK_XAMLTYPEINFO
                    var children = element.Children as MediaMarkerCollection <TimedTextElement>;
#else
                    var children = element.Children;
#endif

                    foreach (CaptionElement child in children)
                    {
                        RenderElementRecurse(parent, ref p, child, ref offset, align);
                    }
                }
                else if (element.CaptionElementType == TimedTextElementType.LineBreak)
                {
                    p = NewPanel(parent, ref offset, element, align);
                }
            }
        }
Esempio n. 2
0
        private Panel NewPanel(Panel parent, ref double offset, CaptionElement element, TextAlignment align)
        {
            var p = new StackPanel
            {
                Orientation = Orientation.Horizontal
            };

            switch (align)
            {
            case TextAlignment.Center:
                p.HorizontalAlignment = HorizontalAlignment.Center;
                break;

            case TextAlignment.Right:
                p.HorizontalAlignment = HorizontalAlignment.Right;
                break;

            case TextAlignment.Left:
                p.HorizontalAlignment = HorizontalAlignment.Left;
                break;

            case TextAlignment.Justify:
                p.HorizontalAlignment = HorizontalAlignment.Stretch;
                break;
            }
            parent.Children.Add(p);
            offset = 0;
            return(p);
        }
        private Panel NewPanel(Panel parent, ref double offset, CaptionElement element, TextAlignment align)
        {
            var p = new StackPanel
            {
                Orientation = Orientation.Horizontal
            };

            switch (align)
            {
                case TextAlignment.Center:
                    p.HorizontalAlignment = HorizontalAlignment.Center;
                    break;
                case TextAlignment.Right:
                    p.HorizontalAlignment = HorizontalAlignment.Right;
                    break;
                case TextAlignment.Left:
                    p.HorizontalAlignment = HorizontalAlignment.Left;
                    break;
                case TextAlignment.Justify:
                    p.HorizontalAlignment = HorizontalAlignment.Stretch;
                    break;
            }
            parent.Children.Add(p);
            offset = 0;
            return p;
        }
        private double WrapElement(Panel parent, ref Panel p, double offset, string text, CaptionElement element, TextAlignment align, bool directionApplied = false)
        {
            if (text == null || text == "") return offset;

            var effectiveSize = this.GetEffectiveSize();
            var style = element.CurrentStyle;
            var panelSize = style.Extent.ToPixelSize(effectiveSize);
            double panelWidth = panelSize.Width;
            double panelHeight = panelSize.Height;

            if (style.Direction == FlowDirection.RightToLeft && !directionApplied)
            {
                text = new string(text.ToCharArray().Reverse().ToArray());
            }

            double height = style.FontSize.Unit == LengthUnit.PixelProportional || style.FontSize.Unit == LengthUnit.Cell ? effectiveSize.Height : panelHeight;
            TextBlock textblock = GetStyledTextblock(style, panelWidth, height, false);
            SetContent(textblock, text);

            Border border = new Border();
            border.Background = GetCachedBrush(style.BackgroundColor);
            FrameworkElement contentElement;

            double outlineWidth = style.OutlineWidth.ToPixelLength(effectiveSize.Height);

            if (outlineWidth > 0)
            {
                switch (style.TextStyle)
                {
                    case TextStyle.Default:
                        contentElement = AddOutlineTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                        break;

                    case TextStyle.DepressedEdge:
                        contentElement = AddDepressedEdgeTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                        break;

                    case TextStyle.DropShadow:
                        contentElement = AddDropShadowTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                        break;

                    case TextStyle.None:
                        contentElement = textblock;
                        break;

                    case TextStyle.Outline:
                        contentElement = AddOutlineTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                        break;

                    case TextStyle.RaisedEdge:
                        contentElement = AddRaisedEdgeTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                        break;

                    default:
                        contentElement = AddOutlineTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                        break;
                }

            }
            else
            {
                contentElement = textblock;
            }

            contentElement.Opacity = System.Convert.ToDouble(style.Color.A) / 255.0;
            border.Opacity = System.Convert.ToDouble(style.Color.A) / 255.0;
            border.Child = contentElement;
            p.Children.Add(border);

            string head = text;
            string tail = string.Empty;
            double elementWidth = textblock.GetEffectiveWidth();
            if (offset + elementWidth > panelSize.Width && style.WrapOption == TextWrapping.Wrap)
            {
                if (text.Length > 0 && text.IndexOf(' ') < 0)
                {
                    if (offset != 0 && elementWidth < panelSize.Width)
                    {
                        p.Children.Remove(border);
                        p = NewPanel(parent, ref offset, element, align);
                        return WrapElement(parent, ref p, 0, text, element, align, true);
                    }
                    int idx = text.Length - 1;
                    head = text.Substring(0, idx);
                    tail = text.Substring(idx);
                    SetAllContent(contentElement, head);
                    while (offset + textblock.GetEffectiveWidth() > panelSize.Width)
                    {
                        idx--;
                        head = text.Substring(0, idx);
                        tail = text.Substring(idx);
                        SetAllContent(contentElement, head);
                        p.UpdateLayout();
                    }
                    p = NewPanel(parent, ref offset, element, align);
                    return WrapElement(parent, ref p, offset, tail, element, align, true);
                }
                while (offset + textblock.GetEffectiveWidth() > panelSize.Width)
                {
                    int idx = head.LastIndexOf(' ');
                    if (idx < 0)
                    {
                        SetAllContent(contentElement, text);
                        return 0;
                    }
                    else
                    {
                        tail = text.Substring(idx + 1);
                        head = text.Substring(0, idx);
                    }
                    SetAllContent(contentElement, head);
                }
                p = NewPanel(parent, ref offset, element, align);
                return WrapElement(parent, ref p, offset, tail, element, align, true);
            }
            else
            {
                offset += elementWidth;
                return offset;
            }
        }
        private void RenderElementRecurse(Panel parent, ref Panel p, CaptionElement element, ref double offset, TextAlignment align)
        {
            if (element.IsActiveAtPosition(_mediaPosition) && element.CurrentStyle.Display == Visibility.Visible)
            {
                if (element.CaptionElementType == TimedTextElementType.Text)
                {
                    var text = element.Content != null ? element.Content.ToString() : string.Empty;
                    offset = WrapElement(parent, ref p, offset, text, element, align);
                }
                else if (element.CaptionElementType == TimedTextElementType.Container)
                {
#if HACK_XAMLTYPEINFO
                    var children = element.Children as MediaMarkerCollection<TimedTextElement>;
#else
                    var children = element.Children;
#endif

                    foreach (CaptionElement child in children)
                    {
                        RenderElementRecurse(parent, ref p, child, ref offset, align);
                    }
                }
                else if (element.CaptionElementType == TimedTextElementType.LineBreak)
                {
                    p = NewPanel(parent, ref offset, element, align);
                }
            }
        }
        private UIElement RenderElement(CaptionElement element)
        {
            Panel parent = null;
            UIElement result = null;

            try
            {
                parent = new StackPanel()
                {
                    HorizontalAlignment = HorizontalAlignment.Center
                };

                var bgImage = element.CurrentStyle.BackgroundImage;
                if (!string.IsNullOrEmpty(bgImage))
                {
                    var border = new Border() { Background = GetCachedBrush(element.CurrentStyle.BackgroundColor) };
                    border.Child = parent;
                    result = border;
                    SetBackgroundImageAsync(bgImage, parent, element.CurrentStyle.BackgroundImageHorizontal, element.CurrentStyle.BackgroundImageVertical);
                }
                else
                {
                    result = parent;
                    parent.Background = GetCachedBrush(element.CurrentStyle.BackgroundColor);
                }

                if (element.CurrentStyle.IsOriginSpecified || element.CurrentStyle.IsExtentSpecified)
                {
                    // set position and size (origin and extent)
                    var width = this.GetEffectiveWidth();
                    var height = this.GetEffectiveHeight();
                    if (element.CurrentStyle.IsOriginSpecified)
                    {
                        var origin = element.CurrentStyle.Origin;
                        parent.Margin = new Thickness
                        {
                            Left = origin.Left.ToPixelLength(width),
                            Top = origin.Top.ToPixelLength(height)
                        };
                    }
                    if (element.CurrentStyle.IsExtentSpecified)
                    {
                        var extent = CaptionRegion.CurrentStyle.Extent;
                        var extentWidth = extent.Width.ToPixelLength(width);
                        var extentHeight = extent.Height.ToPixelLength(height);
                        if (extentWidth > 0) parent.Width = extentWidth;
                        if (extentHeight > 0) parent.Height = extentHeight;
                    }
                }

                var textAlignment = element.CurrentStyle.TextAlign;

                double offset = 0;
                Panel p = NewPanel(parent, ref offset, element, textAlignment);

                RenderElementRecurse(parent, ref p, element, ref offset, textAlignment);

#if !WINDOWS_PHONE && !SILVERLIGHT3 && NOTUSED
                foreach (StackPanel stack in parent.Children)
                {
                    double baseline = 0;
                    foreach (Border b in stack.Children)
                    {
                        TextBlock tb = b.Child as TextBlock;
                        if (tb != null && tb.BaselineOffset > baseline)
                        {
                            baseline = tb.ActualHeight - tb.BaselineOffset;
                        }
                    }
                    foreach (Border b in stack.Children)
                    {
                        TextBlock tb = b.Child as TextBlock;
                        if (tb != null)
                        {
                            tb.Margin = new Thickness(0, 0, 0, baseline - (tb.ActualHeight - tb.BaselineOffset));
                        }
                    }
                }
#endif
            }
            catch (Exception)
            {
                //TODO: Respond to errors
            }

            return result;
        }
Esempio n. 7
0
        private double WrapElement(Panel parent, ref Panel p, double offset, string text, CaptionElement element, TextAlignment align, bool directionApplied = false)
        {
            if (text == null || text == "")
            {
                return(offset);
            }

            var    effectiveSize = this.GetEffectiveSize();
            var    style         = element.CurrentStyle;
            var    panelSize     = style.Extent.ToPixelSize(effectiveSize);
            double panelWidth    = panelSize.Width;
            double panelHeight   = panelSize.Height;

            if (style.Direction == FlowDirection.RightToLeft && !directionApplied)
            {
                text = new string(text.ToCharArray().Reverse().ToArray());
            }

            double    height    = style.FontSize.Unit == LengthUnit.PixelProportional || style.FontSize.Unit == LengthUnit.Cell ? effectiveSize.Height : panelHeight;
            TextBlock textblock = GetStyledTextblock(style, panelWidth, height, false);

            SetContent(textblock, text);

            Border border = new Border();

            border.Background = GetCachedBrush(style.BackgroundColor);
            FrameworkElement contentElement;

            double outlineWidth = style.OutlineWidth.ToPixelLength(effectiveSize.Height);

            if (outlineWidth > 0)
            {
                switch (style.TextStyle)
                {
                case TextStyle.Default:
                    contentElement = AddOutlineTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                    break;

                case TextStyle.DepressedEdge:
                    contentElement = AddDepressedEdgeTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                    break;

                case TextStyle.DropShadow:
                    contentElement = AddDropShadowTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                    break;

                case TextStyle.None:
                    contentElement = textblock;
                    break;

                case TextStyle.Outline:
                    contentElement = AddOutlineTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                    break;

                case TextStyle.RaisedEdge:
                    contentElement = AddRaisedEdgeTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                    break;

                default:
                    contentElement = AddOutlineTextStyle(text, style, panelWidth, height, textblock, outlineWidth);
                    break;
                }
            }
            else
            {
                contentElement = textblock;
            }

            contentElement.Opacity = System.Convert.ToDouble(style.Color.A) / 255.0;
            border.Opacity         = System.Convert.ToDouble(style.Color.A) / 255.0;
            border.Child           = contentElement;
            p.Children.Add(border);

            string head         = text;
            string tail         = string.Empty;
            double elementWidth = textblock.GetEffectiveWidth();

            if (offset + elementWidth > panelSize.Width && style.WrapOption == TextWrapping.Wrap)
            {
                if (text.Length > 0 && text.IndexOf(' ') < 0)
                {
                    if (offset != 0 && elementWidth < panelSize.Width)
                    {
                        p.Children.Remove(border);
                        p = NewPanel(parent, ref offset, element, align);
                        return(WrapElement(parent, ref p, 0, text, element, align, true));
                    }
                    int idx = text.Length - 1;
                    head = text.Substring(0, idx);
                    tail = text.Substring(idx);
                    SetAllContent(contentElement, head);
                    while (offset + textblock.GetEffectiveWidth() > panelSize.Width)
                    {
                        idx--;
                        head = text.Substring(0, idx);
                        tail = text.Substring(idx);
                        SetAllContent(contentElement, head);
                        p.UpdateLayout();
                    }
                    p = NewPanel(parent, ref offset, element, align);
                    return(WrapElement(parent, ref p, offset, tail, element, align, true));
                }
                while (offset + textblock.GetEffectiveWidth() > panelSize.Width)
                {
                    int idx = head.LastIndexOf(' ');
                    if (idx < 0)
                    {
                        SetAllContent(contentElement, text);
                        return(0);
                    }
                    else
                    {
                        tail = text.Substring(idx + 1);
                        head = text.Substring(0, idx);
                    }
                    SetAllContent(contentElement, head);
                }
                p = NewPanel(parent, ref offset, element, align);
                return(WrapElement(parent, ref p, offset, tail, element, align, true));
            }
            else
            {
                offset += elementWidth;
                return(offset);
            }
        }
Esempio n. 8
0
        private UIElement RenderElement(CaptionElement element)
        {
            Panel     parent = null;
            UIElement result = null;

            try
            {
                parent = new StackPanel()
                {
                    HorizontalAlignment = HorizontalAlignment.Center
                };

                var bgImage = element.CurrentStyle.BackgroundImage;
                if (!string.IsNullOrEmpty(bgImage))
                {
                    var border = new Border()
                    {
                        Background = GetCachedBrush(element.CurrentStyle.BackgroundColor)
                    };
                    border.Child = parent;
                    result       = border;
                    SetBackgroundImageAsync(bgImage, parent, element.CurrentStyle.BackgroundImageHorizontal, element.CurrentStyle.BackgroundImageVertical);
                }
                else
                {
                    result            = parent;
                    parent.Background = GetCachedBrush(element.CurrentStyle.BackgroundColor);
                }

                if (element.CurrentStyle.IsOriginSpecified || element.CurrentStyle.IsExtentSpecified)
                {
                    // set position and size (origin and extent)
                    var width  = this.GetEffectiveWidth();
                    var height = this.GetEffectiveHeight();
                    if (element.CurrentStyle.IsOriginSpecified)
                    {
                        var origin = element.CurrentStyle.Origin;
                        parent.Margin = new Thickness
                        {
                            Left = origin.Left.ToPixelLength(width),
                            Top  = origin.Top.ToPixelLength(height)
                        };
                    }
                    if (element.CurrentStyle.IsExtentSpecified)
                    {
                        var extent       = CaptionRegion.CurrentStyle.Extent;
                        var extentWidth  = extent.Width.ToPixelLength(width);
                        var extentHeight = extent.Height.ToPixelLength(height);
                        if (extentWidth > 0)
                        {
                            parent.Width = extentWidth;
                        }
                        if (extentHeight > 0)
                        {
                            parent.Height = extentHeight;
                        }
                    }
                }

                var textAlignment = element.CurrentStyle.TextAlign;

                double offset = 0;
                Panel  p      = NewPanel(parent, ref offset, element, textAlignment);

                RenderElementRecurse(parent, ref p, element, ref offset, textAlignment);

#if !WINDOWS_PHONE && !SILVERLIGHT3 && NOTUSED
                foreach (StackPanel stack in parent.Children)
                {
                    double baseline = 0;
                    foreach (Border b in stack.Children)
                    {
                        TextBlock tb = b.Child as TextBlock;
                        if (tb != null && tb.BaselineOffset > baseline)
                        {
                            baseline = tb.ActualHeight - tb.BaselineOffset;
                        }
                    }
                    foreach (Border b in stack.Children)
                    {
                        TextBlock tb = b.Child as TextBlock;
                        if (tb != null)
                        {
                            tb.Margin = new Thickness(0, 0, 0, baseline - (tb.ActualHeight - tb.BaselineOffset));
                        }
                    }
                }
#endif
            }
            catch (Exception)
            {
                //TODO: Respond to errors
            }

            return(result);
        }