123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using System;
- using System.ComponentModel;
- using System.ComponentModel.Design;
- using System.Windows.Forms;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using FastReport.Utils;
- #if DEBUG && !COREWIN && !(WPF || AVALONIA)
- using FastReport.VSDesign;
- #endif
- namespace FastReport.Controls
- {
- /// <summary>
- /// Represents a control that may contain several pages. It is similar to the TabControl
- /// but contains no tabs. This control is widely used in wizards.
- /// </summary>
- #if DEBUG && !COREWIN && !(WPF || AVALONIA)
- [Designer(typeof(PageControlDesigner))]
- #else
- [Designer("FastReport.VSDesign.PageControlDesigner, FastReport.VSDesign, Version=1.0.0.0, Culture=neutral, PublicKeyToken=db7e5ce63278458c, processorArchitecture=MSIL")]
- [ToolboxItem(false)]
- #endif
- public class PageControl : ContainerControl
- {
- #region Fields
- private int selectorWidth;
- private int selectorTabHeight;
- private int activePageIndex;
- private int highlightPageIndex;
- #endregion
- #region Properties
- /// <summary>
- /// Occurs when page is selected.
- /// </summary>
- [Category("Action")]
- [Description("Occurs when page is selected.")]
- public event EventHandler PageSelected;
- /// <summary>
- /// Gets or sets a value that determines whether the selector area is visible or not.
- /// </summary>
- [DefaultValue(false)]
- [Category("Appearance")]
- [Description("The width of selector area. Set to 0 if you don't want the selector.")]
- public int SelectorWidth
- {
- get { return selectorWidth; }
- set
- {
- selectorWidth = value;
- int padding = selectorWidth > 0 ? 1 : 0;
- if (RightToLeft == RightToLeft.Yes)
- Padding = new Padding(padding, padding, this.LogicalToDevice(selectorWidth), padding);
- else
- Padding = new Padding(selectorWidth, padding, padding, padding);
- Refresh();
- }
- }
- /// <summary>
- /// Gets or sets the height of selector tab.
- /// </summary>
- [DefaultValue(35)]
- [Category("Appearance")]
- [Description("The height of selector tab.")]
- public int SelectorTabHeight
- {
- get { return selectorTabHeight; }
- set
- {
- selectorTabHeight = value;
- Refresh();
- }
- }
- /// <summary>
- /// This property is not relevant to this class
- /// </summary>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public new Padding Padding
- {
- get { return base.Padding; }
- set { base.Padding = value; }
- }
- /// <summary>
- /// Gets or sets the active page.
- /// </summary>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public Panel ActivePage
- {
- get
- {
- if (ActivePageIndex >= 0)
- return Controls[ActivePageIndex] as Panel;
- else
- return null;
- }
- set { ActivePageIndex = Controls.IndexOf(value); }
- }
- /// <summary>
- /// Gets or sets the index of active page.
- /// </summary>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public int ActivePageIndex
- {
- get { return Controls.Count == 0 ? -1 : activePageIndex; }
- set
- {
- if (value >= Controls.Count)
- value = Controls.Count - 1;
- if (value <= 0)
- value = 0;
- activePageIndex = value;
- // avoid page reordering
- SuspendLayout();
- foreach (Control c in Controls)
- {
- c.Visible = true;
- }
- for (int i = 0; i < Controls.Count; i++)
- {
- Control c = Controls[i];
- c.Visible = i == activePageIndex;
- if (DesignMode && c.Visible)
- {
- ISelectionService service = ((ISelectionService)GetService(typeof(ISelectionService)));
- if (service != null)
- service.SetSelectedComponents(new Control[] { c });
- }
- }
- ResumeLayout();
- Refresh();
- OnPageSelected();
- }
- }
- /// <summary>
- /// Gets or sets the highlighted page index.
- /// </summary>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public int HighlightPageIndex
- {
- get { return highlightPageIndex; }
- set
- {
- if (highlightPageIndex != value)
- {
- highlightPageIndex = value;
- Refresh();
- }
- }
- }
- /// <summary>
- /// Gets the collection of pages.
- /// </summary>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public ControlCollection Pages
- {
- get { return Controls; }
- }
- #endregion
- #region Private Methods
- private void DrawTab(Graphics g, int pageIndex)
- {
- if (pageIndex == -1)
- return;
- bool active = pageIndex == ActivePageIndex;
- bool highlight = pageIndex == HighlightPageIndex;
- int left = 4;
- int top = 2;
- int width = SelectorWidth - left - 1;
- int height = SelectorTabHeight;
- top += pageIndex * height;
- if (active)
- {
- left -= 4;
- top -= 2;
- height += 4;
- width += 6;
- }
- Point[] points = new Point[] {
- new Point(left + width, top + height),
- new Point(left, top + height),
- new Point(left, top),
- new Point(left + width, top)};
- Brush brush = null;
- if (active)
- brush = new SolidBrush(SystemColors.Window);
- else
- brush = new LinearGradientBrush(new Rectangle(left, top, width, height), SystemColors.Window, Color.FromArgb(240, 240, 240), 0f);
- g.FillPolygon(brush, points);
- if (active || highlight)
- g.FillRectangle(Brushes.Orange, left, top, 4, height);
- g.DrawLines(SystemPens.ControlDark, points);
- Font font = new Font(Font, active ? FontStyle.Bold : FontStyle.Regular);
- string text = Controls[pageIndex].Text;
- int textWidth = active ? width - 18 : width - 16;
- left += 12;
- if (RightToLeft == RightToLeft.Yes)
- g.ScaleTransform(-1, 1);
- TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
- if (RightToLeft == RightToLeft.Yes)
- {
- flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right | TextFormatFlags.PreserveGraphicsTranslateTransform;
- left = -left - textWidth;
- }
- TextRenderer.DrawText(g, text, font,
- this.LogicalToDevice(new Rectangle(left, top, textWidth, height)), SystemColors.ControlText, flags);
- if (RightToLeft == RightToLeft.Yes)
- g.ScaleTransform(-1, 1);
- brush.Dispose();
- font.Dispose();
- }
- private void OnPageSelected()
- {
- if (PageSelected != null)
- PageSelected(this, EventArgs.Empty);
- }
- #endregion
- #region Protected Methods
- /// <inheritdoc/>
- protected override void OnParentRightToLeftChanged(EventArgs e)
- {
- base.OnParentRightToLeftChanged(e);
- SelectorWidth = SelectorWidth;
- }
- /// <inheritdoc/>
- protected override void OnPaint(PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- GraphicsState state = g.Save();
- if (SelectorWidth > 0)
- {
- int sw = this.LogicalToDevice(selectorWidth);
- g.DrawRectangle(SystemPens.ControlDark,
- new Rectangle(RightToLeft == RightToLeft.Yes ? 0 : sw - 1, 0, Width - sw, Height - 1));
- if (RightToLeft == RightToLeft.Yes)
- {
- g.TranslateTransform(Width, 0);
- g.ScaleTransform(-1, 1);
- }
- g.ScaleTransform(this.DpiMultiplier(), this.DpiMultiplier());
- // draw tabs other than active
- for (int i = 0; i < Controls.Count; i++)
- {
- if (i != ActivePageIndex)
- DrawTab(g, i);
- }
- // draw active tab
- DrawTab(g, ActivePageIndex);
- }
- g.Restore(state);
- }
- /// <inheritdoc/>
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- HighlightPageIndex = GetTabAt(e.Location);
- }
- /// <inheritdoc/>
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- int pageIndex = GetTabAt(e.Location);
- if (pageIndex != -1)
- ActivePageIndex = pageIndex;
- }
- /// <inheritdoc/>
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- HighlightPageIndex = -1;
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Gets tab at specified mouse point.
- /// </summary>
- /// <param name="pt">The mouse point.</param>
- /// <returns>Index of tab under mouse; -1 if mouse is outside tab area.</returns>
- public int GetTabAt(Point pt)
- {
- for (int i = 0; i < Controls.Count; i++)
- {
- int left = 2;
- int top = 2;
- int width = this.LogicalToDevice(SelectorWidth) - left - 1;
- int height = this.LogicalToDevice(SelectorTabHeight);
- top += i * height;
- if (i == ActivePageIndex)
- {
- left -= 2;
- top -= 2;
- height += 4;
- width += 4;
- }
- if (RightToLeft == RightToLeft.Yes)
- {
- left = Width - left - width;
- }
- if (new Rectangle(left, top, width, height).Contains(pt))
- return i;
- }
- return -1;
- }
- /// <summary>
- /// Selects the next page.
- /// </summary>
- public void SelectNextPage()
- {
- if (ActivePageIndex < Pages.Count - 1)
- ActivePageIndex++;
- else
- ActivePageIndex = 0;
- }
- /// <summary>
- /// Selects the previous page.
- /// </summary>
- public void SelectPrevPage()
- {
- if (ActivePageIndex > 0)
- ActivePageIndex--;
- else
- ActivePageIndex = Pages.Count - 1;
- }
- #endregion
- /// <summary>
- /// Initializes a new instance of the <see cref="PageControl"/> class with default settings.
- /// </summary>
- public PageControl()
- {
- highlightPageIndex = -1;
- selectorTabHeight = 35;
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
- }
- }
- /// <summary>
- /// This class represents a single page of the <see cref="PageControl"/> control.
- /// </summary>
- [ToolboxItem(false)]
- public class PageControlPage : Panel
- {
- private Image image;
- /// <summary>
- /// Gets or sets the image associated with this page.
- /// </summary>
- public Image Image
- {
- get { return image; }
- set
- {
- image = value;
- if (Parent is PageControl)
- Parent.Refresh();
- }
- }
- /// <summary>
- /// Gets or sets the page caption text.
- /// </summary>
- [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
- [Category("Appearance")]
- public override string Text
- {
- get { return base.Text; }
- set
- {
- base.Text = value;
- if (Parent is PageControl)
- Parent.Refresh();
- }
- }
- }
- }
|