using Gestor.Application; using Gestor.Application.Helpers; using Gestor.Application.Model; using Gestor.Application.ViewModels.Generic; using Gestor.Application.ViewModels.Relatorios; using Gestor.Model.Domain.Relatorios; using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Shell; namespace Gestor.Application.Views.Relatorios { public class SinteticoView : Window, IComponentConnector, IStyleConnector { internal SinteticoViewModel ViewModel; private bool _buttonClickable; private ListSortDirection _ascending; private string _member; internal System.Windows.Shell.WindowChrome WindowChrome; internal TextBlock SubTitulo; internal Grid MinimizeButton; internal Grid MaximizeButton; internal Grid CloseButton; internal System.Windows.Controls.TabControl Tab; private bool _contentLoaded; public DateTime? DateFinal { get; set; } public DateTime? DateStart { get; set; } public SinteticoView(List sintetico, string relatorio, DateTime? inicio, DateTime? fim) { DateTime valueOrDefault; string str; string str1; this.ViewModel = new SinteticoViewModel(sintetico, relatorio); base.DataContext = this.ViewModel; this.InitializeComponent(); TextBlock subTitulo = this.SubTitulo; if (inicio.HasValue) { valueOrDefault = inicio.GetValueOrDefault(); str = valueOrDefault.ToString("dd/MM/yyyy"); } else { str = null; } if (fim.HasValue) { valueOrDefault = fim.GetValueOrDefault(); str1 = valueOrDefault.ToString("dd/MM/yyyy"); } else { str1 = null; } subTitulo.Text = string.Concat("VALORES BASEADOS NA RELAÇÃO EMITIDA ANTERIORMENTE - INICIO: ", str, " FIM: ", str1); DateTime? nullable = inicio; this.DateStart = new DateTime?((nullable.HasValue ? nullable.GetValueOrDefault() : DateTime.MinValue)); nullable = fim; this.DateFinal = new DateTime?((nullable.HasValue ? nullable.GetValueOrDefault() : DateTime.MinValue)); this.MinimizeButton.MouseEnter += new System.Windows.Input.MouseEventHandler(SinteticoView.TopControls_OnMouseEnter); this.MinimizeButton.MouseLeave += new System.Windows.Input.MouseEventHandler(this.TopControls_OnMouseLeave); this.MaximizeButton.MouseEnter += new System.Windows.Input.MouseEventHandler(SinteticoView.TopControls_OnMouseEnter); this.MaximizeButton.MouseLeave += new System.Windows.Input.MouseEventHandler(this.TopControls_OnMouseLeave); this.CloseButton.MouseEnter += new System.Windows.Input.MouseEventHandler(SinteticoView.TopControls_OnMouseEnter); this.CloseButton.MouseLeave += new System.Windows.Input.MouseEventHandler(this.TopControls_OnMouseLeave); } private void AjustaNome(SinteticoSource source) { foreach (ValorSintetico listum in source.Lista) { if (string.IsNullOrEmpty(listum.get_Porcentagem()) || listum.get_Indice().Contains(listum.get_Porcentagem())) { continue; } ValorSintetico valorSintetico = listum; valorSintetico.set_Indice(string.Concat(valorSintetico.get_Indice(), " - ", listum.get_Porcentagem(), "%")); } } public void CloseButton_Click() { base.Close(); } private void DataGrid_Sorting(object sender, DataGridSortingEventArgs e) { System.Windows.Controls.DataGrid dataGrid = (System.Windows.Controls.DataGrid)sender; if (dataGrid == null || dataGrid.DataContext == null) { return; } if (this._member != e.Column.SortMemberPath) { this._ascending = ListSortDirection.Ascending; } else { this._ascending = (this._ascending == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending); } ObservableCollection itemsSource = dataGrid.ItemsSource as ObservableCollection; if (itemsSource == null) { return; } IQueryable valorSinteticos = itemsSource.AsQueryable(); List list = ( from x in (this._ascending == ListSortDirection.Ascending ? valorSinteticos.OrderBy(e.Column.SortMemberPath).ToList() : valorSinteticos.OrderByDescending(e.Column.SortMemberPath).ToList()) orderby x.get_Indice() == "TOTAL" select x).ToList(); itemsSource.Clear(); foreach (ValorSintetico valorSintetico in list) { itemsSource.Add(valorSintetico); } this.ViewModel.OnPropertyChanged("DataGrid_Sorting"); this._member = e.Column.SortMemberPath; e.Handled = true; } private async void ExportarExcel_OnClick(object sender, RoutedEventArgs e) { System.Windows.Controls.MenuItem menuItem = (System.Windows.Controls.MenuItem)sender; if (menuItem != null && menuItem.DataContext != null) { SinteticoSource dataContext = (SinteticoSource)menuItem.DataContext; await this.ViewModel.GerarExcel(dataContext); } } private async void ExportarPdf_OnClick(object sender, RoutedEventArgs e) { System.Windows.Controls.MenuItem menuItem = (System.Windows.Controls.MenuItem)sender; if (menuItem != null && menuItem.DataContext != null) { SinteticoSource dataContext = (SinteticoSource)menuItem.DataContext; await this.ViewModel.GerarPdf(dataContext); } } [DllImport("user32.dll", CharSet=CharSet.None, ExactSpelling=false)] private static extern bool GetCursorPos(out MainWindow.Point lpPoint); [DllImport("user32.dll", CharSet=CharSet.None, ExactSpelling=false)] private static extern bool GetMonitorInfo(IntPtr hMonitor, SinteticoView.Monitorinfo lpmi); private async void Imprimir_OnClick(object sender, RoutedEventArgs e) { bool flag; System.Windows.Controls.MenuItem menuItem = (System.Windows.Controls.MenuItem)sender; if (menuItem != null && menuItem.DataContext != null) { SinteticoSource dataContext = (SinteticoSource)menuItem.DataContext; DateTime? dateStart = this.DateStart; DateTime minValue = DateTime.MinValue; flag = (dateStart.HasValue ? dateStart.GetValueOrDefault() != minValue : true); if (flag) { dataContext.DateStart = this.DateStart; dataContext.DateFinal = this.DateFinal; } await this.ViewModel.Print(dataContext); } } private async void ImprimirUnicaPagina_OnClick(object sender, RoutedEventArgs e) { bool flag; System.Windows.Controls.MenuItem menuItem = (System.Windows.Controls.MenuItem)sender; if (menuItem != null && menuItem.DataContext != null) { SinteticoSource dataContext = (SinteticoSource)menuItem.DataContext; DateTime? dateStart = this.DateStart; DateTime minValue = DateTime.MinValue; flag = (dateStart.HasValue ? dateStart.GetValueOrDefault() != minValue : true); if (flag) { dataContext.DateStart = this.DateStart; dataContext.DateFinal = this.DateFinal; } await this.ViewModel.PrintUnica(dataContext); } } [DebuggerNonUserCode] [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] public void InitializeComponent() { if (this._contentLoaded) { return; } this._contentLoaded = true; System.Windows.Application.LoadComponent(this, new Uri("/Gestor.Application;component/views/relatorios/sinteticoview.xaml", UriKind.Relative)); } public void MaximizeButton_Click() { base.WindowState = (base.WindowState == System.Windows.WindowState.Normal ? System.Windows.WindowState.Maximized : System.Windows.WindowState.Normal); } public void MinimizeButton_Click() { base.WindowState = System.Windows.WindowState.Minimized; } [DllImport("user32.dll", CharSet=CharSet.None, ExactSpelling=false, SetLastError=true)] private static extern IntPtr MonitorFromPoint(MainWindow.Point pt, SinteticoView.MonitorOptions dwFlags); protected sealed override void OnStateChanged(EventArgs e) { object obj; base.BorderThickness = new Thickness((double)(base.WindowState != System.Windows.WindowState.Maximized)); System.Windows.Shell.WindowChrome windowChrome = this.WindowChrome; if (base.WindowState == System.Windows.WindowState.Maximized) { obj = null; } else { obj = 4; } windowChrome.ResizeBorderThickness = new Thickness((double)obj); this.WindowChrome.CaptionHeight = (double)((base.WindowState == System.Windows.WindowState.Maximized ? 33 : 30)); this.ViewModel.MaximizeRestore = (base.WindowState == System.Windows.WindowState.Maximized ? Geometry.Parse((string)System.Windows.Application.Current.Resources["Restore"]) : Geometry.Parse((string)System.Windows.Application.Current.Resources["Maximize"])); base.OnStateChanged(e); } [DebuggerNonUserCode] [EditorBrowsable(EditorBrowsableState.Never)] [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: { ((SinteticoView)target).Initialized += new EventHandler(this.Window_OnInitialized); return; } case 2: { this.WindowChrome = (System.Windows.Shell.WindowChrome)target; return; } case 3: { this.SubTitulo = (TextBlock)target; return; } case 4: { this.MinimizeButton = (Grid)target; this.MinimizeButton.MouseLeftButtonDown += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonDown); this.MinimizeButton.MouseLeftButtonUp += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonUp); return; } case 5: { this.MaximizeButton = (Grid)target; this.MaximizeButton.MouseLeftButtonDown += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonDown); this.MaximizeButton.MouseLeftButtonUp += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonUp); return; } case 6: { this.CloseButton = (Grid)target; this.CloseButton.MouseLeftButtonDown += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonDown); this.CloseButton.MouseLeftButtonUp += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonUp); return; } case 7: { this.Tab = (System.Windows.Controls.TabControl)target; return; } } this._contentLoaded = true; } [DebuggerNonUserCode] [EditorBrowsable(EditorBrowsableState.Never)] [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] void System.Windows.Markup.IStyleConnector.Connect(int connectionId, object target) { switch (connectionId) { case 8: { ((System.Windows.Controls.MenuItem)target).Click += new RoutedEventHandler(this.Imprimir_OnClick); return; } case 9: { ((System.Windows.Controls.MenuItem)target).Click += new RoutedEventHandler(this.ExportarPdf_OnClick); return; } case 10: { ((System.Windows.Controls.MenuItem)target).Click += new RoutedEventHandler(this.ExportarExcel_OnClick); return; } case 11: { ((System.Windows.Controls.MenuItem)target).Click += new RoutedEventHandler(this.ImprimirUnicaPagina_OnClick); return; } case 12: { ((System.Windows.Controls.DataGrid)target).Sorting += new DataGridSortingEventHandler(this.DataGrid_Sorting); return; } default: { return; } } } private static void TopControls_OnMouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { ((Grid)sender).Background = (((Grid)sender).Name == "CloseButton" ? new SolidColorBrush(Colors.IndianRed) : new SolidColorBrush(Colors.Gray)); Path child = VisualTreeHelper.GetChild((Grid)sender, 0) as Path; if (child != null) { child.Stroke = new SolidColorBrush(Colors.White); } } private void TopControls_OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e) { this._buttonClickable = false; ((Grid)sender).Background = new SolidColorBrush(Colors.Transparent); Path child = VisualTreeHelper.GetChild((Grid)sender, 0) as Path; if (child != null) { child.Stroke = new SolidColorBrush(Colors.White); } } private void TopControls_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this._buttonClickable = true; ((Grid)sender).Background = (((Grid)sender).Name == "CloseButton" ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.DimGray)); } private void TopControls_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this._buttonClickable) { MethodInfo method = base.GetType().GetMethod(string.Concat(((Grid)sender).Name, "_Click")); if (method == null) { return; } method.Invoke(this, null); } } private void Window_OnInitialized(object sender, EventArgs e) { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this); windowInteropHelper.EnsureHandle(); HwndSource hwndSource = HwndSource.FromHwnd(windowInteropHelper.Handle); if (hwndSource == null) { return; } hwndSource.AddHook(new HwndSourceHook(this.WindowProc)); } private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == 36) { this.WmGetMinMaxInfo(lParam); } return IntPtr.Zero; } private void WmGetMinMaxInfo(IntPtr lParam) { MainWindow.Point point; System.Drawing.Rectangle workingArea = Screen.FromHandle((new WindowInteropHelper(this)).Handle).WorkingArea; base.MaxHeight = (double)workingArea.Height; SinteticoView.GetCursorPos(out point); IntPtr intPtr = SinteticoView.MonitorFromPoint(new MainWindow.Point(0, 0), SinteticoView.MonitorOptions.MonitorDefaulttoprimary); SinteticoView.Monitorinfo monitorinfo = new SinteticoView.Monitorinfo(); if (!SinteticoView.GetMonitorInfo(intPtr, monitorinfo)) { return; } IntPtr intPtr1 = SinteticoView.MonitorFromPoint(point, SinteticoView.MonitorOptions.MonitorDefaulttonearest); SinteticoView.Minmaxinfo structure = (SinteticoView.Minmaxinfo)Marshal.PtrToStructure(lParam, typeof(SinteticoView.Minmaxinfo)); if (!intPtr.Equals(intPtr1)) { structure.ptMaxPosition.X = monitorinfo.rcMonitor.Left; structure.ptMaxPosition.Y = monitorinfo.rcMonitor.Top; structure.ptMaxSize.X = monitorinfo.rcMonitor.Right - monitorinfo.rcMonitor.Left; structure.ptMaxSize.Y = monitorinfo.rcMonitor.Bottom - monitorinfo.rcMonitor.Top; } else { structure.ptMaxPosition.X = monitorinfo.rcWork.Left; structure.ptMaxPosition.Y = monitorinfo.rcWork.Top; structure.ptMaxSize.X = monitorinfo.rcWork.Right - monitorinfo.rcWork.Left; structure.ptMaxSize.Y = monitorinfo.rcWork.Bottom - monitorinfo.rcWork.Top; } Marshal.StructureToPtr(structure, lParam, true); } private struct Minmaxinfo { private readonly MainWindow.Point ptReserved; public SinteticoView.Point ptMaxSize; public SinteticoView.Point ptMaxPosition; private readonly MainWindow.Point ptMinTrackSize; private readonly MainWindow.Point ptMaxTrackSize; } private class Monitorinfo { private readonly int cbSize; public readonly SinteticoView.Rect rcMonitor; public readonly SinteticoView.Rect rcWork; private readonly int dwFlags; public Monitorinfo() { } } private enum MonitorOptions : uint { MonitorDefaulttoprimary = 1, MonitorDefaulttonearest = 2 } public struct Point { public int X; public int Y; public Point(int x, int y) { this.X = x; this.Y = y; } } public struct Rect { public int Left; public int Top; public int Right; public int Bottom; } } }