123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- 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
- {
- /// <summary>
- /// Represents a WPF control used to preview a report.
- /// </summary>
- /// <remarks>
- /// To use this control, place it on a form and link it to a report using the report's
- /// <see cref="FastReport.Report.WpfPreview"/> property. To show a report, call
- /// the <b>Report.Show</b> method:
- /// <code>
- /// report1.WpfPreview = previewControl1;
- /// report1.Show();
- /// </code>
- /// <para>Use this control's methods such as <see cref="Print"/>, <see cref="Save()"/> etc. to
- /// handle the preview. Call <see cref="Clear"/> method to clear the preview.</para>
- /// <para>You can specify whether the standard toolbar is visible in the <see cref="ToolbarVisible"/>
- /// property. The <see cref="StatusbarVisible"/> property allows you to hide/show the statusbar.
- /// </para>
- /// </remarks>
- public partial class WpfPreviewControl : UserControl, INotifyPropertyChanged
- {
- /// <summary>
- /// Gets inner preview control.
- /// </summary>
- public PreviewControl InnerPreview { get; }
- /// <summary>
- /// Specifies the set of buttons available in the toolbar.
- /// </summary>
- public PreviewButtons Buttons
- {
- get => InnerPreview.Buttons;
- set => InnerPreview.Buttons = value;
- }
- /// <summary>
- /// Gets or sets a value indicating whether the toolbar is visible.
- /// </summary>
- public bool ToolbarVisible
- {
- get => InnerPreview.ToolbarVisible;
- set => InnerPreview.ToolbarVisible = value;
- }
- /// <summary>
- /// Gets or sets a value indicating whether the statusbar is visible.
- /// </summary>
- public bool StatusbarVisible
- {
- get => InnerPreview.StatusbarVisible;
- set => InnerPreview.StatusbarVisible = value;
- }
- /// <summary>
- /// Gets or sets the visual style.
- /// </summary>
- public UIStyle UIStyle
- {
- get => InnerPreview.UIStyle;
- set => InnerPreview.UIStyle = value;
- }
- /// <summary>
- /// Gets or sets a color used to draw the background area.
- /// </summary>
- /// <remarks>
- /// In order to use this property, you must also set the <see cref="UseBackColor"/> property to true.
- /// </remarks>
- public Color BackColor
- {
- get => System.Windows.Forms.Helper.GetColor(InnerPreview.BackColor);
- set => InnerPreview.BackColor = System.Windows.Forms.Helper.GetColor(value);
- }
- /// <summary>
- /// Gets or sets a value indicating that the BackColor property must be used to draw the background area.
- /// </summary>
- /// <remarks>
- /// By default, the background area is drawn using the color defined in the current <b>UIStyle</b>.
- /// </remarks>
- public bool UseBackColor
- {
- get => InnerPreview.UseBackColor;
- set => InnerPreview.UseBackColor = value;
- }
- /// <summary>
- /// Gets or sets the color of active page border.
- /// </summary>
- public Color ActivePageBorderColor
- {
- get => System.Windows.Forms.Helper.GetColor(InnerPreview.ActivePageBorderColor);
- set => InnerPreview.ActivePageBorderColor = System.Windows.Forms.Helper.GetColor(value);
- }
- /// <summary>
- /// Gets a reference to the report.
- /// </summary>
- public Report Report => InnerPreview.Report;
- /// <summary>
- /// Gets the value indicating that async report is running.
- /// </summary>
- /// <remarks>
- /// This value can be used to abort the report when you close the form that contains the report preview control.
- /// </remarks>
- public bool IsAsyncReportRunning => InnerPreview.IsAsyncReportRunning;
- /// <summary>
- /// Occurs when the current page is changed.
- /// </summary>
- 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
- /// <summary>
- /// Prints the current report.
- /// </summary>
- /// <returns><b>true</b> if report was printed; <b>false</b> if user cancels the "Print" dialog.</returns>
- public bool Print() => InnerPreview.Print();
- /// <summary>
- /// Saves the current report to a .fpx file using the "Save FIle" dialog.
- /// </summary>
- public void Save() => InnerPreview.Save();
- /// <summary>
- /// Saves the current report to a specified .fpx file.
- /// </summary>
- public void Save(string fileName) => InnerPreview.Save(fileName);
- /// <summary>
- /// Saves the current report to a stream.
- /// </summary>
- public void Save(Stream stream) => InnerPreview.Save(stream);
- /// <summary>
- /// Loads the report from a .fpx file using the "Open File" dialog.
- /// </summary>
- public void Load() => InnerPreview.Load();
- /// <summary>
- /// Loads the report from a specified .fpx file.
- /// </summary>
- public void Load(string fileName) => InnerPreview.Load(fileName);
- /// <summary>
- /// Load the report from a stream.
- /// </summary>
- /// <param name="stream">The stream to load from.</param>
- public void Load(Stream stream) => InnerPreview.Load(stream);
- /// <summary>
- /// Sends an email.
- /// </summary>
- public void SendEmail() => InnerPreview.SendEmail();
- /// <summary>
- /// Finds the text in the current report using the "Find Text" dialog.
- /// </summary>
- public void Find() => InnerPreview.Find();
- /// <summary>
- /// Finds the specified text in the current report.
- /// </summary>
- /// <param name="text">Text to find.</param>
- /// <param name="matchCase">A value indicating whether the search is case-sensitive.</param>
- /// <param name="wholeWord">A value indicating whether the search matches whole words only.</param>
- /// <returns><b>true</b> if text found.</returns>
- public bool Find(string text, bool matchCase, bool wholeWord) => InnerPreview.Find(text, matchCase, wholeWord);
- /// <summary>
- /// Finds the next occurence of text specified in the <b>Find</b> method.
- /// </summary>
- /// <returns><b>true</b> if text found.</returns>
- public bool FindNext() => InnerPreview.FindNext();
- /// <summary>
- /// Navigates to the first page.
- /// </summary>
- public void First() => InnerPreview.First();
- /// <summary>
- /// Navigates to the previuos page.
- /// </summary>
- public void Prior() => InnerPreview.Prior();
- /// <summary>
- /// Navigates to the next page.
- /// </summary>
- public void Next() => InnerPreview.Next();
- /// <summary>
- /// Navigates to the last page.
- /// </summary>
- public void Last() => InnerPreview.Last();
- /// <summary>
- /// Gets or sets the current page number.
- /// </summary>
- /// <remarks>
- /// This value is 1-based.
- /// </remarks>
- public int PageNo
- {
- get => InnerPreview.PageNo;
- set => InnerPreview.PageNo = value;
- }
- /// <summary>
- /// Gets the pages count in the current report.
- /// </summary>
- public int PageCount => InnerPreview.PageCount;
- /// <summary>
- /// Gets or sets the zoom factor.
- /// </summary>
- /// <remarks>
- /// <b>1</b> corresponds to 100% zoom.
- /// </remarks>
- public float Zoom
- {
- get => InnerPreview.Zoom;
- set => InnerPreview.Zoom = value;
- }
- /// <summary>
- /// Zooms in.
- /// </summary>
- public void ZoomIn() => InnerPreview.ZoomIn();
- /// <summary>
- /// Zooms out.
- /// </summary>
- public void ZoomOut() => InnerPreview.ZoomOut();
- /// <summary>
- /// Zooms to fit the page width.
- /// </summary>
- public void ZoomPageWidth() => InnerPreview.ZoomPageWidth();
- /// <summary>
- /// Zooms to fit the whole page.
- /// </summary>
- public void ZoomWholePage() => InnerPreview.ZoomWholePage();
- /// <summary>
- /// Edits the current page in the designer.
- /// </summary>
- public void EditPage() => InnerPreview.EditPage();
- /// <summary>
- /// Edits the watermark.
- /// </summary>
- public void EditWatermark() => InnerPreview.EditWatermark();
- /// <summary>
- /// Edits the page settings.
- /// </summary>
- public void PageSetup() => InnerPreview.PageSetup();
- /// <summary>
- /// Navigates to the specified position inside a specified page.
- /// </summary>
- /// <param name="pageNo">The page number (1-based).</param>
- /// <param name="point">The position inside a page, in pixels.</param>
- public void PositionTo(int pageNo, Point point) => InnerPreview.PositionTo(pageNo, new System.Drawing.PointF((float)point.X, (float)point.Y));
- /// <summary>
- /// Clears the preview.
- /// </summary>
- public void Clear() => InnerPreview.Clear();
- /// <summary>
- /// Refresh the report.
- /// </summary>
- public void RefreshReport() => InnerPreview.RefreshReport();
- /// <summary>
- /// Displays the text in the status bar.
- /// </summary>
- /// <param name="text">Text to display.</param>
- public void ShowStatus(string text) => InnerPreview.ShowStatus(text);
- #endregion
- private PreviewCommand CreateCmd(Action action) => new PreviewCommand(action);
- /// <summary>
- /// Initializes a new instance of the <see cref="WpfPreviewControl"/> class.
- /// </summary>
- 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);
- }
- }
- }
|