summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Views/StaticPreviewView.xaml.cs
blob: 2289cfe45704e3a7f8005897f0b4b614b3b9b3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StaticPreviewView.xaml.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   Interaction logic for StaticPreviewView.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Views
{
    using System;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    using HandBrakeWPF.ViewModels.Interfaces;

    /// <summary>
    /// Interaction logic for StaticPreviewView.xaml
    /// </summary>
    public partial class StaticPreviewView : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StaticPreviewView"/> class.
        /// </summary>
        public StaticPreviewView()
        {
            this.InitializeComponent();

            this.SizeChanged += this.StaticPreviewView_SizeChanged;
            this.Title = Properties.Resources.Preview;
        }

        private void StaticPreviewView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // Prevent the Window Growing Past Screen Bounds
            Rect workArea = SystemParameters.WorkArea;
            if (e.NewSize.Width > workArea.Width)
            {
                this.Width = (int)Math.Round(workArea.Width, 0) - 50;
            }

            if (e.NewSize.Height > workArea.Height)
            {
                this.Height = (int)Math.Round(workArea.Height, 0) - 50;
            }

            // Update Window title scale factor.
            this.UpdateWindowTitle();
        }

        private void PreviewImage_OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 1)
            {
                ((IStaticPreviewViewModel)this.DataContext).NextPreview();
            }
            else
            {
                ((IStaticPreviewViewModel)this.DataContext).PreviousPreview();
            }
        }

        private void UpdateWindowTitle()
        {
            BitmapImage image = ((IStaticPreviewViewModel)this.DataContext).PreviewImage;
            if (image != null && this.previewImage != null && this.previewImage.ActualWidth > 0)
            {
                double origWidth = Math.Round(image.Width, 0);
                double origHeight = Math.Round(image.Height, 0);

                double actualWidth = Math.Round(this.previewImage.ActualWidth, 0);
                double actualHeight = Math.Round(this.previewImage.ActualHeight, 0);

                double scaleW = actualWidth / origWidth;
                double scaleH = actualHeight / origHeight;

                double scaleFactor = Math.Min(scaleW, scaleH);

                double scalePercentage = Math.Round(100 * scaleFactor, 0);

                this.Title = string.Format(Properties.Resources.StaticPreviewView_Title, scalePercentage);
            }
            else
            {
                this.Title = Properties.Resources.Preview;
            }
        }
    }
}