using FastReport.Utils;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace FastReport.Preview
{
///
/// Represents a WPF control used to preview a report.
///
///
/// To use this control, place it on a form and link it to a report using the report's
/// property. To show a report, call
/// the Report.Show method:
///
/// report1.WpfPreview = previewControl1;
/// report1.Show();
///
/// Use this control's methods such as , etc. to
/// handle the preview. Call method to clear the preview.
/// You can specify whether the standard toolbar is visible in the
/// property. The property allows you to hide/show the statusbar.
///
///
public partial class WpfPreviewControl : UserControl, INotifyPropertyChanged
{
///
/// Gets inner preview control.
///
public PreviewControl InnerPreview { get; }
///
/// Specifies the set of buttons available in the toolbar.
///
public PreviewButtons Buttons
{
get => InnerPreview.Buttons;
set => InnerPreview.Buttons = value;
}
///
/// Gets or sets a value indicating whether the toolbar is visible.
///
public bool ToolbarVisible
{
get => InnerPreview.ToolbarVisible;
set => InnerPreview.ToolbarVisible = value;
}
///
/// Gets or sets a value indicating whether the statusbar is visible.
///
public bool StatusbarVisible
{
get => InnerPreview.StatusbarVisible;
set => InnerPreview.StatusbarVisible = value;
}
///
/// Gets or sets the visual style.
///
public UIStyle UIStyle
{
get => InnerPreview.UIStyle;
set => InnerPreview.UIStyle = value;
}
///
/// Gets or sets a color used to draw the background area.
///
///
/// In order to use this property, you must also set the property to true.
///
public Color BackColor
{
get => System.Windows.Forms.Helper.GetColor(InnerPreview.BackColor);
set => InnerPreview.BackColor = System.Windows.Forms.Helper.GetColor(value);
}
///
/// Gets or sets a value indicating that the BackColor property must be used to draw the background area.
///
///
/// By default, the background area is drawn using the color defined in the current UIStyle.
///
public bool UseBackColor
{
get => InnerPreview.UseBackColor;
set => InnerPreview.UseBackColor = value;
}
///
/// Gets or sets the color of active page border.
///
public Color ActivePageBorderColor
{
get => System.Windows.Forms.Helper.GetColor(InnerPreview.ActivePageBorderColor);
set => InnerPreview.ActivePageBorderColor = System.Windows.Forms.Helper.GetColor(value);
}
///
/// Gets a reference to the report.
///
public Report Report => InnerPreview.Report;
///
/// Gets the value indicating that async report is running.
///
///
/// This value can be used to abort the report when you close the form that contains the report preview control.
///
public bool IsAsyncReportRunning => InnerPreview.IsAsyncReportRunning;
///
/// Occurs when the current page is changed.
///
public event EventHandler PageChanged
{
add => InnerPreview.PageChanged += value;
remove => InnerPreview.PageChanged -= value;
}
#region ICommands
public PreviewCommand cmdPrint { get; }
public PreviewCommand cmdSave { get; }
public PreviewCommand cmdLoad { get; }
public PreviewCommand cmdSendEmail { get; }
public PreviewCommand cmdFind { get; }
public PreviewCommand cmdFindNext { get; }
public PreviewCommand cmdFirst { get; }
public PreviewCommand cmdPrior { get; }
public PreviewCommand cmdNext { get; }
public PreviewCommand cmdLast { get; }
public PreviewCommand cmdZoomIn { get; }
public PreviewCommand cmdZoomOut { get; }
public PreviewCommand cmdZoomPageWidth { get; }
public PreviewCommand cmdZoomWholePage { get; }
public PreviewCommand cmdEditPage { get; }
public PreviewCommand cmdEditWatermark { get; }
public PreviewCommand cmdPageSetup { get; }
public PreviewCommand cmdClear { get; }
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Preview commands
///
/// Prints the current report.
///
/// true if report was printed; false if user cancels the "Print" dialog.
public bool Print() => InnerPreview.Print();
///
/// Saves the current report to a .fpx file using the "Save FIle" dialog.
///
public void Save() => InnerPreview.Save();
///
/// Saves the current report to a specified .fpx file.
///
public void Save(string fileName) => InnerPreview.Save(fileName);
///
/// Saves the current report to a stream.
///
public void Save(Stream stream) => InnerPreview.Save(stream);
///
/// Loads the report from a .fpx file using the "Open File" dialog.
///
public void Load() => InnerPreview.Load();
///
/// Loads the report from a specified .fpx file.
///
public void Load(string fileName) => InnerPreview.Load(fileName);
///
/// Load the report from a stream.
///
/// The stream to load from.
public void Load(Stream stream) => InnerPreview.Load(stream);
///
/// Sends an email.
///
public void SendEmail() => InnerPreview.SendEmail();
///
/// Finds the text in the current report using the "Find Text" dialog.
///
public void Find() => InnerPreview.Find();
///
/// Finds the specified text in the current report.
///
/// Text to find.
/// A value indicating whether the search is case-sensitive.
/// A value indicating whether the search matches whole words only.
/// true if text found.
public bool Find(string text, bool matchCase, bool wholeWord) => InnerPreview.Find(text, matchCase, wholeWord);
///
/// Finds the next occurence of text specified in the Find method.
///
/// true if text found.
public bool FindNext() => InnerPreview.FindNext();
///
/// Navigates to the first page.
///
public void First() => InnerPreview.First();
///
/// Navigates to the previuos page.
///
public void Prior() => InnerPreview.Prior();
///
/// Navigates to the next page.
///
public void Next() => InnerPreview.Next();
///
/// Navigates to the last page.
///
public void Last() => InnerPreview.Last();
///
/// Gets or sets the current page number.
///
///
/// This value is 1-based.
///
public int PageNo
{
get => InnerPreview.PageNo;
set => InnerPreview.PageNo = value;
}
///
/// Gets the pages count in the current report.
///
public int PageCount => InnerPreview.PageCount;
///
/// Gets or sets the zoom factor.
///
///
/// 1 corresponds to 100% zoom.
///
public float Zoom
{
get => InnerPreview.Zoom;
set => InnerPreview.Zoom = value;
}
///
/// Zooms in.
///
public void ZoomIn() => InnerPreview.ZoomIn();
///
/// Zooms out.
///
public void ZoomOut() => InnerPreview.ZoomOut();
///
/// Zooms to fit the page width.
///
public void ZoomPageWidth() => InnerPreview.ZoomPageWidth();
///
/// Zooms to fit the whole page.
///
public void ZoomWholePage() => InnerPreview.ZoomWholePage();
///
/// Edits the current page in the designer.
///
public void EditPage() => InnerPreview.EditPage();
///
/// Edits the watermark.
///
public void EditWatermark() => InnerPreview.EditWatermark();
///
/// Edits the page settings.
///
public void PageSetup() => InnerPreview.PageSetup();
///
/// Navigates to the specified position inside a specified page.
///
/// The page number (1-based).
/// The position inside a page, in pixels.
public void PositionTo(int pageNo, Point point) => InnerPreview.PositionTo(pageNo, new System.Drawing.PointF((float)point.X, (float)point.Y));
///
/// Clears the preview.
///
public void Clear() => InnerPreview.Clear();
///
/// Refresh the report.
///
public void RefreshReport() => InnerPreview.RefreshReport();
///
/// Displays the text in the status bar.
///
/// Text to display.
public void ShowStatus(string text) => InnerPreview.ShowStatus(text);
#endregion
private PreviewCommand CreateCmd(Action action) => new PreviewCommand(action);
///
/// Initializes a new instance of the class.
///
public WpfPreviewControl()
{
InnerPreview = new PreviewControl();
var control = InnerPreview.control;
control.Width = double.NaN;
control.Height = double.NaN;
control.HorizontalAlignment = HorizontalAlignment.Stretch;
control.VerticalAlignment = VerticalAlignment.Stretch;
Content = control;
UseLayoutRounding = true;
Loaded += (s, e) =>
{
System.Windows.Forms.DpiRescaler.Install(Window.GetWindow(this), InnerPreview, this, (s, e) => InnerPreview.UpdateDpiDependencies());
};
InnerPreview.PageChanged += (s, e) =>
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PageNo)));
};
cmdPrint = CreateCmd(() => InnerPreview.Print());
cmdSave = CreateCmd(InnerPreview.Save);
cmdLoad = CreateCmd(InnerPreview.Load);
cmdSendEmail = CreateCmd(InnerPreview.SendEmail);
cmdFind = CreateCmd(InnerPreview.Find);
cmdFindNext = CreateCmd(() => InnerPreview.FindNext());
cmdFirst = CreateCmd(InnerPreview.First);
cmdPrior = CreateCmd(InnerPreview.Prior);
cmdNext = CreateCmd(InnerPreview.Next);
cmdLast = CreateCmd(InnerPreview.Last);
cmdZoomIn = CreateCmd(InnerPreview.ZoomIn);
cmdZoomOut = CreateCmd(InnerPreview.ZoomOut);
cmdZoomPageWidth = CreateCmd(InnerPreview.ZoomPageWidth);
cmdZoomWholePage = CreateCmd(InnerPreview.ZoomWholePage);
cmdEditPage = CreateCmd(InnerPreview.EditPage);
cmdEditWatermark = CreateCmd(InnerPreview.EditWatermark);
cmdPageSetup = CreateCmd(InnerPreview.PageSetup);
cmdClear = CreateCmd(InnerPreview.Clear);
}
}
}