示例#1
0
文件: Image.cs 项目: Xbox-dev/XPF
        private Size GetScaledImageSize(Size givenSize)
        {
            ImageSource source = this.Source;

            if (source == null)
            {
                return(new Size());
            }

            Size   contentSize = source.Size;
            Vector scale       = Viewbox.ComputeScaleFactor(givenSize, contentSize, this.Stretch, this.StretchDirection);

            return(new Size(contentSize.Width * scale.X, contentSize.Height * scale.Y));
        }
示例#2
0
        /// <summary>
        /// Content arrangement.
        /// </summary>
        /// <param name="finalSize">The final size that element should use to arrange itself and its children.</param>
        protected override sealed Size ArrangeOverride(Size finalSize)
        {
            Transform      pageTransform;
            ScaleTransform pageScaleTransform;
            Visual         pageVisual;
            Size           pageSize, pageZoom;

            CheckDisposed();

            if (_pageVisualClone == null)
            {
                if (_pageHost == null)
                {
                    _pageHost = new DocumentPageHost();
                    this.AddVisualChild(_pageHost);
                }
                Invariant.Assert(_pageHost != null);

                pageVisual = (_documentPage == null) ? null : _documentPage.Visual;
                if (pageVisual == null)
                {
                    // Remove existing visiual children.
                    _pageHost.PageVisual = null;

                    // Reset offset and transform on the page host before Arrange
                    _pageHost.CachedOffset    = new Point();
                    _pageHost.RenderTransform = null;

                    // Size for the page host needs to be set to finalSize
                    _pageHost.Arrange(new Rect(_pageHost.CachedOffset, finalSize));
                }
                else
                {
                    // Add visual representing the page contents. For performance reasons
                    // first check if it is already insered there.
                    if (_pageHost.PageVisual != pageVisual)
                    {
                        // There might be a case where a visual associated with a page was
                        // inserted to a visual tree before. It got removed later, but GC did not
                        // destroy its parent yet. To workaround this case always check for the parent
                        // of page visual and disconnect it, when necessary.
                        DocumentPageHost.DisconnectPageVisual(pageVisual);

                        _pageHost.PageVisual = pageVisual;
                    }

                    // Compute transform to be applied to the page visual. First take into account
                    // mirroring transform, if necessary. Apply also scaling transform.
                    pageSize      = _documentPage.Size;
                    pageTransform = Transform.Identity;

                    // DocumentPage.Visual is always LeftToRight, so if the current
                    // FlowDirection is RightToLeft, need to unmirror the child visual.
                    if (FlowDirection == FlowDirection.RightToLeft)
                    {
                        pageTransform = new MatrixTransform(-1.0, 0.0, 0.0, 1.0, pageSize.Width, 0.0);
                    }

                    // Apply zooming
                    if (!DoubleUtil.IsOne(_pageZoom))
                    {
                        pageScaleTransform = new ScaleTransform(_pageZoom, _pageZoom);
                        if (pageTransform == Transform.Identity)
                        {
                            pageTransform = pageScaleTransform;
                        }
                        else
                        {
                            pageTransform = new MatrixTransform(pageTransform.Value * pageScaleTransform.Value);
                        }
                        pageSize = new Size(pageSize.Width * _pageZoom, pageSize.Height * _pageZoom);
                    }

                    // Apply stretch properties
                    pageZoom = Viewbox.ComputeScaleFactor(finalSize, pageSize, this.Stretch, this.StretchDirection);
                    if (!DoubleUtil.IsOne(pageZoom.Width) || !DoubleUtil.IsOne(pageZoom.Height))
                    {
                        pageScaleTransform = new ScaleTransform(pageZoom.Width, pageZoom.Height);
                        if (pageTransform == Transform.Identity)
                        {
                            pageTransform = pageScaleTransform;
                        }
                        else
                        {
                            pageTransform = new MatrixTransform(pageTransform.Value * pageScaleTransform.Value);
                        }
                        pageSize = new Size(pageSize.Width * pageZoom.Width, pageSize.Height * pageZoom.Height);
                    }

                    // Set offset and transform on the page host before Arrange
                    _pageHost.CachedOffset    = new Point((finalSize.Width - pageSize.Width) / 2, (finalSize.Height - pageSize.Height) / 2);
                    _pageHost.RenderTransform = pageTransform;

                    // Arrange pagehost to original size of the page.
                    _pageHost.Arrange(new Rect(_pageHost.CachedOffset, _documentPage.Size));
                }

                // Fire sync notification if new page was connected.
                if (_newPageConnected)
                {
                    OnPageConnected();
                }

                // Transform for the page has been changed, need to notify TextView about the changes.
                OnTransformChangedAsync();
            }
            else
            {
                if (_pageHost.PageVisual != _pageVisualClone)
                {
                    // Remove existing visiual children.
                    _pageHost.PageVisual = _pageVisualClone;
                    // Size for the page host needs to be set to finalSize

                    // Use previous offset and transform
                    _pageHost.Arrange(new Rect(_pageHost.CachedOffset, finalSize));
                }
            }

            return(base.ArrangeOverride(finalSize));
        }
 /// <summary>Arranges the content to fit a specified view size.</summary>
 /// <param name="finalSize">The maximum size that the page view should use to arrange itself and its children.</param>
 /// <returns>The actual size that the page view used to arrange itself and its children.</returns>
 // Token: 0x06005D78 RID: 23928 RVA: 0x001A4EA8 File Offset: 0x001A30A8
 protected sealed override Size ArrangeOverride(Size finalSize)
 {
     this.CheckDisposed();
     if (this._pageVisualClone == null)
     {
         if (this._pageHost == null)
         {
             this._pageHost = new DocumentPageHost();
             base.AddVisualChild(this._pageHost);
         }
         Invariant.Assert(this._pageHost != null);
         Visual visual = (this._documentPage == null) ? null : this._documentPage.Visual;
         if (visual == null)
         {
             this._pageHost.PageVisual      = null;
             this._pageHost.CachedOffset    = default(Point);
             this._pageHost.RenderTransform = null;
             this._pageHost.Arrange(new Rect(this._pageHost.CachedOffset, finalSize));
         }
         else
         {
             if (this._pageHost.PageVisual != visual)
             {
                 DocumentPageHost.DisconnectPageVisual(visual);
                 this._pageHost.PageVisual = visual;
             }
             Size      size      = this._documentPage.Size;
             Transform transform = Transform.Identity;
             if (base.FlowDirection == FlowDirection.RightToLeft)
             {
                 transform = new MatrixTransform(-1.0, 0.0, 0.0, 1.0, size.Width, 0.0);
             }
             if (!DoubleUtil.IsOne(this._pageZoom))
             {
                 ScaleTransform scaleTransform = new ScaleTransform(this._pageZoom, this._pageZoom);
                 if (transform == Transform.Identity)
                 {
                     transform = scaleTransform;
                 }
                 else
                 {
                     transform = new MatrixTransform(transform.Value * scaleTransform.Value);
                 }
                 size = new Size(size.Width * this._pageZoom, size.Height * this._pageZoom);
             }
             Size size2 = Viewbox.ComputeScaleFactor(finalSize, size, this.Stretch, this.StretchDirection);
             if (!DoubleUtil.IsOne(size2.Width) || !DoubleUtil.IsOne(size2.Height))
             {
                 ScaleTransform scaleTransform = new ScaleTransform(size2.Width, size2.Height);
                 if (transform == Transform.Identity)
                 {
                     transform = scaleTransform;
                 }
                 else
                 {
                     transform = new MatrixTransform(transform.Value * scaleTransform.Value);
                 }
                 size = new Size(size.Width * size2.Width, size.Height * size2.Height);
             }
             this._pageHost.CachedOffset    = new Point((finalSize.Width - size.Width) / 2.0, (finalSize.Height - size.Height) / 2.0);
             this._pageHost.RenderTransform = transform;
             this._pageHost.Arrange(new Rect(this._pageHost.CachedOffset, this._documentPage.Size));
         }
         if (this._newPageConnected)
         {
             this.OnPageConnected();
         }
         this.OnTransformChangedAsync();
     }
     else if (this._pageHost.PageVisual != this._pageVisualClone)
     {
         this._pageHost.PageVisual = this._pageVisualClone;
         this._pageHost.Arrange(new Rect(this._pageHost.CachedOffset, finalSize));
     }
     return(base.ArrangeOverride(finalSize));
 }
示例#4
0
        /// <summary>
        /// Content measurement.
        /// </summary>
        /// <param name="availableSize">Available size that parent can give to the child. This is soft constraint.</param>
        /// <returns>The DocumentPageView's desired size.</returns>
        protected override sealed Size MeasureOverride(Size availableSize)
        {
            Size newPageSize, pageZoom;
            Size pageSize;
            Size desiredSize = new Size(); // If no page is available, return (0,0) as size.

            CheckDisposed();

            if (_suspendLayout)
            {
                desiredSize = this.DesiredSize;
            }
            else if (_documentPaginator != null)
            {
                // Reflow content if needed.
                if (ShouldReflowContent())
                {
                    // Reflow is disabled when dealing with infinite size in both directions.
                    // If only one dimention is infinte, calculate value based on PageSize of the
                    // document and Stretching properties.
                    if (!Double.IsInfinity(availableSize.Width) || !Double.IsInfinity(availableSize.Height))
                    {
                        pageSize = _documentPaginator.PageSize;
                        if (Double.IsInfinity(availableSize.Width))
                        {
                            newPageSize        = new Size();
                            newPageSize.Height = availableSize.Height / _pageZoom;
                            newPageSize.Width  = newPageSize.Height * (pageSize.Width / pageSize.Height); // Keep aspect ratio.
                        }
                        else if (Double.IsInfinity(availableSize.Height))
                        {
                            newPageSize        = new Size();
                            newPageSize.Width  = availableSize.Width / _pageZoom;
                            newPageSize.Height = newPageSize.Width * (pageSize.Height / pageSize.Width); // Keep aspect ratio.
                        }
                        else
                        {
                            newPageSize = new Size(availableSize.Width / _pageZoom, availableSize.Height / _pageZoom);
                        }
                        if (!DoubleUtil.AreClose(pageSize, newPageSize))
                        {
                            _documentPaginator.PageSize = newPageSize;
                        }
                    }
                }

                // If the main page or pending async page are not available yet,
                // asynchronously request new page from Paginator.
                if (_documentPage == null && _documentPageAsync == null)
                {
                    if (PageNumber >= 0)
                    {
                        if (_useAsynchronous)
                        {
                            _documentPaginator.GetPageAsync(PageNumber, this);
                        }
                        else
                        {
                            _documentPageAsync = _documentPaginator.GetPage(PageNumber);
                            if (_documentPageAsync == null)
                            {
                                _documentPageAsync = DocumentPage.Missing;
                            }
                        }
                    }
                    else
                    {
                        _documentPage = DocumentPage.Missing;
                    }
                }

                // If pending async page is available, discard the main page and
                // set _documentPage to _documentPageAsync.
                if (_documentPageAsync != null)
                {
                    // Do cleanup for currently used page, because it gets replaced.
                    DisposeCurrentPage();
                    // DisposeCurrentPage raises PageDisposed and DocumentPage.PageDestroyed events.
                    // Handlers for those events may dispose _documentPageAsync. Treat this situation
                    // as missing page.
                    if (_documentPageAsync == null)
                    {
                        _documentPageAsync = DocumentPage.Missing;
                    }
                    if (_pageVisualClone != null)
                    {
                        RemoveDuplicateVisual();
                    }

                    // Replace the main page with cached async page.
                    _documentPage = _documentPageAsync;
                    if (_documentPage != DocumentPage.Missing)
                    {
                        _documentPage.PageDestroyed      += new EventHandler(HandlePageDestroyed);
                        _documentPageAsync.PageDestroyed -= new EventHandler(HandleAsyncPageDestroyed);
                    }
                    _documentPageAsync = null;

                    // Set a flag that will indicate that a PageConnected must be fired in
                    // ArrangeOverride
                    _newPageConnected = true;
                }

                // If page is available, return its size as desired size.
                if (_documentPage != null && _documentPage != DocumentPage.Missing)
                {
                    pageSize    = new Size(_documentPage.Size.Width * _pageZoom, _documentPage.Size.Height * _pageZoom);
                    pageZoom    = Viewbox.ComputeScaleFactor(availableSize, pageSize, this.Stretch, this.StretchDirection);
                    desiredSize = new Size(pageSize.Width * pageZoom.Width, pageSize.Height * pageZoom.Height);
                }

                if (_pageVisualClone != null)
                {
                    desiredSize = _visualCloneSize;
                }
            }

            return(desiredSize);
        }
        /// <summary>Returns the available viewport size that can be given to display the current <see cref="P:System.Windows.Controls.Primitives.DocumentPageView.DocumentPage" />.</summary>
        /// <param name="availableSize">The maximum available size.</param>
        /// <returns>The actual desired size.</returns>
        // Token: 0x06005D77 RID: 23927 RVA: 0x001A4BDC File Offset: 0x001A2DDC
        protected sealed override Size MeasureOverride(Size availableSize)
        {
            Size result = default(Size);

            this.CheckDisposed();
            if (this._suspendLayout)
            {
                result = base.DesiredSize;
            }
            else if (this._documentPaginator != null)
            {
                if (this.ShouldReflowContent() && (!double.IsInfinity(availableSize.Width) || !double.IsInfinity(availableSize.Height)))
                {
                    Size pageSize = this._documentPaginator.PageSize;
                    Size size;
                    if (double.IsInfinity(availableSize.Width))
                    {
                        size        = default(Size);
                        size.Height = availableSize.Height / this._pageZoom;
                        size.Width  = size.Height * (pageSize.Width / pageSize.Height);
                    }
                    else if (double.IsInfinity(availableSize.Height))
                    {
                        size        = default(Size);
                        size.Width  = availableSize.Width / this._pageZoom;
                        size.Height = size.Width * (pageSize.Height / pageSize.Width);
                    }
                    else
                    {
                        size = new Size(availableSize.Width / this._pageZoom, availableSize.Height / this._pageZoom);
                    }
                    if (!DoubleUtil.AreClose(pageSize, size))
                    {
                        this._documentPaginator.PageSize = size;
                    }
                }
                if (this._documentPage == null && this._documentPageAsync == null)
                {
                    if (this.PageNumber >= 0)
                    {
                        if (this._useAsynchronous)
                        {
                            this._documentPaginator.GetPageAsync(this.PageNumber, this);
                        }
                        else
                        {
                            this._documentPageAsync = this._documentPaginator.GetPage(this.PageNumber);
                            if (this._documentPageAsync == null)
                            {
                                this._documentPageAsync = DocumentPage.Missing;
                            }
                        }
                    }
                    else
                    {
                        this._documentPage = DocumentPage.Missing;
                    }
                }
                if (this._documentPageAsync != null)
                {
                    this.DisposeCurrentPage();
                    if (this._documentPageAsync == null)
                    {
                        this._documentPageAsync = DocumentPage.Missing;
                    }
                    if (this._pageVisualClone != null)
                    {
                        this.RemoveDuplicateVisual();
                    }
                    this._documentPage = this._documentPageAsync;
                    if (this._documentPage != DocumentPage.Missing)
                    {
                        this._documentPage.PageDestroyed      += this.HandlePageDestroyed;
                        this._documentPageAsync.PageDestroyed -= this.HandleAsyncPageDestroyed;
                    }
                    this._documentPageAsync = null;
                    this._newPageConnected  = true;
                }
                if (this._documentPage != null && this._documentPage != DocumentPage.Missing)
                {
                    Size pageSize = new Size(this._documentPage.Size.Width * this._pageZoom, this._documentPage.Size.Height * this._pageZoom);
                    Size size2    = Viewbox.ComputeScaleFactor(availableSize, pageSize, this.Stretch, this.StretchDirection);
                    result = new Size(pageSize.Width * size2.Width, pageSize.Height * size2.Height);
                }
                if (this._pageVisualClone != null)
                {
                    result = this._visualCloneSize;
                }
            }
            return(result);
        }