/*
i-net software provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties
of merchantability and/or fitness for a particular purpose. This programming example
assumes that you are familiar with the programming language being demonstrated and
the tools used to create and debug procedures. i-net software support professionals
can help explain the functionality of a particular procedure, but they will not modify
these examples to provide added functionality or construct procedures to meet your
specific needs.
© i-net software 1998-2013
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using Inet.Viewer.Data;
using Inet.Viewer.Resources;
using Inet.Viewer.WinForms.Export;
namespace Inet.Viewer.WinForms
{
///
/// An implemenation of the interface . This Toolbar changes its behaviour according to the
/// currently added .
///
[ToolboxItem(false)]
public partial class ViewerToolbar : UserControl, IToolBar
{
private const string ZoomFitToPage = "fit to page";
private const string ZoomFitToWidth = "fit to width";
private const string ZoomFitToHeight = "fit to height";
///
/// int array that holds the different zoom levels that are shown
///
private static readonly int[] ZoomStep = new int[] { 10, 25, 50, 75, 100, 150, 200, 400, };
///
/// int array that holds the different zoom levels that are used for Zoom + and - feature
///
private static readonly int[] ZoomStepsDetail = new int[] { 10, 18, 25, 33, 50, 66, 75, 88, 100, 125, 150, 175, 200, 300, 400, 600 };
///
/// Used for the Property ReportView
///
private ReportView reportView;
private DataChanged dataInitedHandler;
private PageChanged pageChangedHandler;
private EventHandler zoomChangedHandler;
///
/// Constructor sets the zoom steps and disables all buttons by default
///
public ViewerToolbar()
{
InitializeComponent();
dataInitedHandler = new DataChanged((x, y) => {
if (InvokeRequired)
{
Invoke(new Action(UpdateButtonStates));
}
else
{
UpdateButtonStates();
}
});
pageChangedHandler = new PageChanged(ReportViewPageChanged);
zoomChangedHandler = new EventHandler(ViewZoomChanged);
UpdateButtonStates();
SetupZoomLevel();
SetLabels();
}
///
/// Sets the visiblity of the buttons in the toolbar, depending on the availability
/// of the representing functions
///
private void UpdateButtonStates()
{
btnRefresh.Enabled = reportView != null;
bool state = reportView != null && reportView.ReportInfo != null;
toolStripSeparatorRefresh.Visible = state;
btnExport.Enabled = state;
btnPrint.Enabled = state && reportView.ReportInfo.IsPrintingEnabled;
btnZoomLevel.Enabled = state;
btnZoomMinus.Enabled = state;
btnZoomPlus.Enabled = state;
btnSearch.Enabled = state;
btnTool.Enabled = state;
btnPageSingle.Enabled = state;
btnPageSingleCont.Enabled = state;
btnPageDouble.Enabled = state;
btnPageDoubleCont.Enabled = state;
DisplayNavigationButtons();
}
///
/// Updates the label for PageCount and CurrentPage
///
private void UpdatePageLabelAndText()
{
if (ReportView != null)
{
this.txtCurrentPage.Enabled = true;
string pageLimit = string.Empty;
if (ReportView.PageLimitExceeded)
{
pageLimit = "+";
}
string pageCount = ReportView.PageCount < 1 ? "?" : ReportView.PageCount + string.Empty;
this.txtCurrentPage.Text = ReportView.CurrentPage + " / " + pageCount + pageLimit;
this.Refresh();
}
else
{
// no report shown
this.txtCurrentPage.Enabled = false;
this.txtCurrentPage.Text = string.Empty;
}
}
///
/// This methods enables or disables the navigation button according to the .CurrentPage of the ReportView.
/// The needs to be set for this.
///
private void DisplayNavigationButtons()
{
// Disable Navigation Buttons
btnPreviousPage.Enabled = false;
btnLastPage.Enabled = false;
btnNextPage.Enabled = false;
btnFirstPage.Enabled = false;
txtCurrentPage.Enabled = false;
txtCurrentPage.Enabled = false;
if (ReportView != null && ReportView.ReportInfo != null)
{
bool isFirstPage = ReportView.CurrentPage == 1;
// If last page is unknown it stil has the Default value 0
bool isLastPage = ReportView.CurrentPage == ReportView.PageCount;
bool isInBetweenPage = !isFirstPage && !isLastPage;
bool hasOnlyOnePage = ReportView.PageCount == 1;
txtCurrentPage.Enabled = true;
if ((isFirstPage || isInBetweenPage) && !hasOnlyOnePage)
{
btnNextPage.Enabled = true;
btnLastPage.Enabled = ReportView.PageCount > 0;
}
if ((isLastPage || isInBetweenPage) && !hasOnlyOnePage)
{
btnPreviousPage.Enabled = true;
btnFirstPage.Enabled = true;
}
}
}
///
/// Updates the labels and the Navigation buttons
///
/// the sender
/// the page
private void ReportViewPageChanged(object sender, int page)
{
UpdatePageLabelAndText();
DisplayNavigationButtons();
}
///
/// Update CurrentPage when the 'Enter' key is pressed in the CurrentPage textfield
///
/// the sender
/// the arguments
private void txtCurrentPage_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return && ReportView != null)
{
string page = txtCurrentPage.Text;
// if has page separator
if (page != null && page.Contains("/"))
{
int index = page.IndexOf("/");
page = page.Substring(0, index);
}
int pageNumber;
if (int.TryParse(page, out pageNumber))
{
ReportView.CurrentPage = pageNumber;
}
UpdatePageLabelAndText();
}
}
///
/// Next Page Button clicked
///
/// the sender
/// the arguments
private void btnNextPage_Click(object sender, System.EventArgs e)
{
if (this.ReportView != null)
{
ReportView.NextPage();
UpdatePageLabelAndText();
}
}
///
/// Print button clicked
///
/// the sender
/// the arguments
private void btnPrint_Click(object sender, System.EventArgs e)
{
// open the dialog async due to an issue with the printer dialog.
// otherwise the dialog doesn't get the input focus when opened from toolstrip click event.
BeginInvoke((MethodInvoker)delegate {
ReportView.Print();
});
}
///
/// Last Page button clicked
///
/// the sender
/// the arguments
private void btnLastPage_Click(object sender, System.EventArgs e)
{
if (this.ReportView != null)
{
ReportView.LastPage();
}
}
///
/// Previous Page button clicked
///
/// the sender
/// the arguments
private void btnPreviousPage_Click(object sender, System.EventArgs e)
{
if (this.ReportView != null)
{
ReportView.PreviousPage();
}
}
///
/// First Page button clicked
///
/// the sender
/// the arguments
private void btnFirstPage_Click(object sender, System.EventArgs e)
{
if (this.ReportView != null)
{
ReportView.FirstPage();
}
}
///
///
///
public void SetButtonsVisible(ToolbarButtonType buttonType, bool visible)
{
switch (buttonType)
{
case ToolbarButtonType.General:
break;
case ToolbarButtonType.Navigation:
break;
}
}
///
///
///
public bool IsButtonsVisible(ToolbarButtonType buttonType)
{
switch (buttonType)
{
case ToolbarButtonType.General:
break;
case ToolbarButtonType.Navigation:
break;
}
return false;
}
///
///
///
public IReportView ReportView
{
get
{
return reportView;
}
set
{
// remove events
if (reportView != null)
{
// remove events from previous ReportView
reportView.DataInited -= dataInitedHandler;
reportView.PageChanged -= pageChangedHandler;
reportView.ZoomChanged -= zoomChangedHandler;
}
reportView = (ReportView)value;
// setup up the toolbar
if (reportView != null)
{
UpdatePageLabelAndText();
value.DataInited += dataInitedHandler;
value.PageChanged += pageChangedHandler;
this.ShowZoomLevel();
value.ZoomChanged += zoomChangedHandler;
this.ShowMouseMode();
switch(reportView.PageViewMode) {
case PageViewMode.SinglePage:
MarkPageViewModeButtonChecked(btnPageSingle);
break;
case PageViewMode.SingleContinuous:
MarkPageViewModeButtonChecked(btnPageSingleCont);
break;
case PageViewMode.DoublePage:
MarkPageViewModeButtonChecked(btnPageDouble);
break;
case PageViewMode.DoubleContinuous:
MarkPageViewModeButtonChecked(btnPageDoubleCont);
break;
}
}
UpdateButtonStates();
}
}
///
/// Called when the zoom level was changed.
///
/// the sender
/// the arguments
private void ViewZoomChanged(object sender, EventArgs e)
{
ShowZoomLevel();
}
///
/// Adds the zoom levels to the Zoom level combobox
///
private void SetupZoomLevel()
{
for (int i = 0; i < ZoomStep.Length; i++)
{
btnZoomLevel.Items.Add(ZoomStep[i] + " %");
}
btnZoomLevel.Items.Add(strings.FitToPage);
btnZoomLevel.Items.Add(strings.FitToHeight);
btnZoomLevel.Items.Add(strings.FitToWidth);
}
///
/// Decreases the zoom level
///
/// the sender
/// the arguments
private void btnZoomMinus_Click(object sender, EventArgs e)
{
ZoomOut();
}
///
/// Increases the zoom level
///
/// the sender
/// the arguments
private void btnZoomPlus_Click(object sender, EventArgs e)
{
ZoomIn();
}
///
/// This method calculates the next smallest step of the zoom level.
/// Uses the ZOOM_STEPS array for the information. With inbetween steps.
/// Implementation for float values
///
/// The zoom value in percent in float (1.0 = 100%)
/// zoom float value in percent (1.0 = 100%)
public static float NextHigherZoomLevel(float currentZoom)
{
float lastStep = 0;
foreach (int step in ZoomStepsDetail)
{
float fStep = (float) step / 100f;
if (currentZoom < fStep)
{
return fStep;
}
lastStep = step;
}
return lastStep;
}
///
/// This method calculates the next bigger step of the zoom level
/// Uses the ZOOM_STEPS array for the information
///
/// The zoom value in percent
/// zoom float value in percent (1.0 = 100%)
public static float NextLowerZoomLevel(float currentZoom)
{
float smallestStep = ZoomStepsDetail[0] / 100f;
for (int i = ZoomStepsDetail.Length - 1; i > 0; i--)
{
float step = (float) ZoomStepsDetail[i] / 100f;
if (currentZoom > step)
{
return step;
}
}
return smallestStep;
}
///
/// This method displays the current Zoom factor in the ZoomLevel Combobox
///
private void ShowZoomLevel()
{
if (ReportView != null)
{
switch (ReportView.ZoomMode)
{
case ZoomMode.Manual:
this.btnZoomLevel.Text = ReportView.ZoomFactor * 100 + " %";
break;
case ZoomMode.FullPage:
this.btnZoomLevel.Text = strings.FitToPage;
break;
case ZoomMode.PageHeight:
this.btnZoomLevel.Text = strings.FitToHeight;
break;
case ZoomMode.PageWidth:
this.btnZoomLevel.Text = strings.FitToWidth;
break;
}
}
}
///
/// Updates the shown mouse mode with the current mouse
/// mode from the report view.
///
private void ShowMouseMode()
{
if (ReportView != null)
{
switch (ReportView.MouseActionMode)
{
case MouseMode.Panning:
this.btnTool.Image = global::Inet.Viewer.Images.hand;
break;
case MouseMode.SelectText:
this.btnTool.Image = global::Inet.Viewer.Images.textselect;
break;
}
}
}
///
/// Parses the Export format that is save in the Tag of the ToolStripDropDownItem
///
///
/// Returns the parsed export format
private static ExportFormat GetExportFormat(object sender)
{
ExportFormat selectedFormat = ExportFormat.None;
// Get the export format
ToolStripDropDownItem item = sender as ToolStripDropDownItem;
int intExportFormat = -1;
if (item != null && item.Tag != null && int.TryParse(item.Tag.ToString(), out intExportFormat))
{
selectedFormat = (ExportFormat)intExportFormat;
}
return selectedFormat;
}
///
/// Does the localisation of all the strings with the current CultureInfo
///
private void SetLabels()
{
btnFirstPage.ToolTipText = strings.FirstPage;
btnLastPage.ToolTipText = strings.LastPage;
btnPreviousPage.ToolTipText = strings.PreviousPage;
btnNextPage.ToolTipText = strings.NextPage;
txtCurrentPage.ToolTipText = strings.CurrentPage;
btnZoomLevel.ToolTipText = strings.ZoomLevel;
}
///
/// Adds a progress to the toolbar.
///
/// the progress to add
public void AddProgress(Progress progress)
{
ToolStripProgress toolStripProgress = new ToolStripProgress(progress);
toolStripProgress.Finished += ToolStripProgressFinished;
toolStrip1.Items.Add(toolStripProgress);
}
///
public IEnumerable