PageControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.Design;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using FastReport.Utils;
  8. #if DEBUG && !COREWIN && !(WPF || AVALONIA)
  9. using FastReport.VSDesign;
  10. #endif
  11. namespace FastReport.Controls
  12. {
  13. /// <summary>
  14. /// Represents a control that may contain several pages. It is similar to the TabControl
  15. /// but contains no tabs. This control is widely used in wizards.
  16. /// </summary>
  17. #if DEBUG && !COREWIN && !(WPF || AVALONIA)
  18. [Designer(typeof(PageControlDesigner))]
  19. #else
  20. [Designer("FastReport.VSDesign.PageControlDesigner, FastReport.VSDesign, Version=1.0.0.0, Culture=neutral, PublicKeyToken=db7e5ce63278458c, processorArchitecture=MSIL")]
  21. [ToolboxItem(false)]
  22. #endif
  23. public class PageControl : ContainerControl
  24. {
  25. #region Fields
  26. private int selectorWidth;
  27. private int selectorTabHeight;
  28. private int activePageIndex;
  29. private int highlightPageIndex;
  30. #endregion
  31. #region Properties
  32. /// <summary>
  33. /// Occurs when page is selected.
  34. /// </summary>
  35. [Category("Action")]
  36. [Description("Occurs when page is selected.")]
  37. public event EventHandler PageSelected;
  38. /// <summary>
  39. /// Gets or sets a value that determines whether the selector area is visible or not.
  40. /// </summary>
  41. [DefaultValue(false)]
  42. [Category("Appearance")]
  43. [Description("The width of selector area. Set to 0 if you don't want the selector.")]
  44. public int SelectorWidth
  45. {
  46. get { return selectorWidth; }
  47. set
  48. {
  49. selectorWidth = value;
  50. int padding = selectorWidth > 0 ? 1 : 0;
  51. if (RightToLeft == RightToLeft.Yes)
  52. Padding = new Padding(padding, padding, this.LogicalToDevice(selectorWidth), padding);
  53. else
  54. Padding = new Padding(selectorWidth, padding, padding, padding);
  55. Refresh();
  56. }
  57. }
  58. /// <summary>
  59. /// Gets or sets the height of selector tab.
  60. /// </summary>
  61. [DefaultValue(35)]
  62. [Category("Appearance")]
  63. [Description("The height of selector tab.")]
  64. public int SelectorTabHeight
  65. {
  66. get { return selectorTabHeight; }
  67. set
  68. {
  69. selectorTabHeight = value;
  70. Refresh();
  71. }
  72. }
  73. /// <summary>
  74. /// This property is not relevant to this class
  75. /// </summary>
  76. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  77. public new Padding Padding
  78. {
  79. get { return base.Padding; }
  80. set { base.Padding = value; }
  81. }
  82. /// <summary>
  83. /// Gets or sets the active page.
  84. /// </summary>
  85. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  86. public Panel ActivePage
  87. {
  88. get
  89. {
  90. if (ActivePageIndex >= 0)
  91. return Controls[ActivePageIndex] as Panel;
  92. else
  93. return null;
  94. }
  95. set { ActivePageIndex = Controls.IndexOf(value); }
  96. }
  97. /// <summary>
  98. /// Gets or sets the index of active page.
  99. /// </summary>
  100. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  101. public int ActivePageIndex
  102. {
  103. get { return Controls.Count == 0 ? -1 : activePageIndex; }
  104. set
  105. {
  106. if (value >= Controls.Count)
  107. value = Controls.Count - 1;
  108. if (value <= 0)
  109. value = 0;
  110. activePageIndex = value;
  111. // avoid page reordering
  112. SuspendLayout();
  113. foreach (Control c in Controls)
  114. {
  115. c.Visible = true;
  116. }
  117. for (int i = 0; i < Controls.Count; i++)
  118. {
  119. Control c = Controls[i];
  120. c.Visible = i == activePageIndex;
  121. if (DesignMode && c.Visible)
  122. {
  123. ISelectionService service = ((ISelectionService)GetService(typeof(ISelectionService)));
  124. if (service != null)
  125. service.SetSelectedComponents(new Control[] { c });
  126. }
  127. }
  128. ResumeLayout();
  129. Refresh();
  130. OnPageSelected();
  131. }
  132. }
  133. /// <summary>
  134. /// Gets or sets the highlighted page index.
  135. /// </summary>
  136. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  137. public int HighlightPageIndex
  138. {
  139. get { return highlightPageIndex; }
  140. set
  141. {
  142. if (highlightPageIndex != value)
  143. {
  144. highlightPageIndex = value;
  145. Refresh();
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// Gets the collection of pages.
  151. /// </summary>
  152. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  153. public ControlCollection Pages
  154. {
  155. get { return Controls; }
  156. }
  157. #endregion
  158. #region Private Methods
  159. private void DrawTab(Graphics g, int pageIndex)
  160. {
  161. if (pageIndex == -1)
  162. return;
  163. bool active = pageIndex == ActivePageIndex;
  164. bool highlight = pageIndex == HighlightPageIndex;
  165. int left = 4;
  166. int top = 2;
  167. int width = SelectorWidth - left - 1;
  168. int height = SelectorTabHeight;
  169. top += pageIndex * height;
  170. if (active)
  171. {
  172. left -= 4;
  173. top -= 2;
  174. height += 4;
  175. width += 6;
  176. }
  177. Point[] points = new Point[] {
  178. new Point(left + width, top + height),
  179. new Point(left, top + height),
  180. new Point(left, top),
  181. new Point(left + width, top)};
  182. Brush brush = null;
  183. if (active)
  184. brush = new SolidBrush(SystemColors.Window);
  185. else
  186. brush = new LinearGradientBrush(new Rectangle(left, top, width, height), SystemColors.Window, Color.FromArgb(240, 240, 240), 0f);
  187. g.FillPolygon(brush, points);
  188. if (active || highlight)
  189. g.FillRectangle(Brushes.Orange, left, top, 4, height);
  190. g.DrawLines(SystemPens.ControlDark, points);
  191. Font font = new Font(Font, active ? FontStyle.Bold : FontStyle.Regular);
  192. string text = Controls[pageIndex].Text;
  193. int textWidth = active ? width - 18 : width - 16;
  194. left += 12;
  195. if (RightToLeft == RightToLeft.Yes)
  196. g.ScaleTransform(-1, 1);
  197. TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
  198. if (RightToLeft == RightToLeft.Yes)
  199. {
  200. flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right | TextFormatFlags.PreserveGraphicsTranslateTransform;
  201. left = -left - textWidth;
  202. }
  203. TextRenderer.DrawText(g, text, font,
  204. this.LogicalToDevice(new Rectangle(left, top, textWidth, height)), SystemColors.ControlText, flags);
  205. if (RightToLeft == RightToLeft.Yes)
  206. g.ScaleTransform(-1, 1);
  207. brush.Dispose();
  208. font.Dispose();
  209. }
  210. private void OnPageSelected()
  211. {
  212. if (PageSelected != null)
  213. PageSelected(this, EventArgs.Empty);
  214. }
  215. #endregion
  216. #region Protected Methods
  217. /// <inheritdoc/>
  218. protected override void OnParentRightToLeftChanged(EventArgs e)
  219. {
  220. base.OnParentRightToLeftChanged(e);
  221. SelectorWidth = SelectorWidth;
  222. }
  223. /// <inheritdoc/>
  224. protected override void OnPaint(PaintEventArgs e)
  225. {
  226. Graphics g = e.Graphics;
  227. GraphicsState state = g.Save();
  228. if (SelectorWidth > 0)
  229. {
  230. int sw = this.LogicalToDevice(selectorWidth);
  231. g.DrawRectangle(SystemPens.ControlDark,
  232. new Rectangle(RightToLeft == RightToLeft.Yes ? 0 : sw - 1, 0, Width - sw, Height - 1));
  233. if (RightToLeft == RightToLeft.Yes)
  234. {
  235. g.TranslateTransform(Width, 0);
  236. g.ScaleTransform(-1, 1);
  237. }
  238. g.ScaleTransform(this.DpiMultiplier(), this.DpiMultiplier());
  239. // draw tabs other than active
  240. for (int i = 0; i < Controls.Count; i++)
  241. {
  242. if (i != ActivePageIndex)
  243. DrawTab(g, i);
  244. }
  245. // draw active tab
  246. DrawTab(g, ActivePageIndex);
  247. }
  248. g.Restore(state);
  249. }
  250. /// <inheritdoc/>
  251. protected override void OnMouseMove(MouseEventArgs e)
  252. {
  253. base.OnMouseMove(e);
  254. HighlightPageIndex = GetTabAt(e.Location);
  255. }
  256. /// <inheritdoc/>
  257. protected override void OnMouseUp(MouseEventArgs e)
  258. {
  259. base.OnMouseUp(e);
  260. int pageIndex = GetTabAt(e.Location);
  261. if (pageIndex != -1)
  262. ActivePageIndex = pageIndex;
  263. }
  264. /// <inheritdoc/>
  265. protected override void OnMouseLeave(EventArgs e)
  266. {
  267. base.OnMouseLeave(e);
  268. HighlightPageIndex = -1;
  269. }
  270. #endregion
  271. #region Public Methods
  272. /// <summary>
  273. /// Gets tab at specified mouse point.
  274. /// </summary>
  275. /// <param name="pt">The mouse point.</param>
  276. /// <returns>Index of tab under mouse; -1 if mouse is outside tab area.</returns>
  277. public int GetTabAt(Point pt)
  278. {
  279. for (int i = 0; i < Controls.Count; i++)
  280. {
  281. int left = 2;
  282. int top = 2;
  283. int width = this.LogicalToDevice(SelectorWidth) - left - 1;
  284. int height = this.LogicalToDevice(SelectorTabHeight);
  285. top += i * height;
  286. if (i == ActivePageIndex)
  287. {
  288. left -= 2;
  289. top -= 2;
  290. height += 4;
  291. width += 4;
  292. }
  293. if (RightToLeft == RightToLeft.Yes)
  294. {
  295. left = Width - left - width;
  296. }
  297. if (new Rectangle(left, top, width, height).Contains(pt))
  298. return i;
  299. }
  300. return -1;
  301. }
  302. /// <summary>
  303. /// Selects the next page.
  304. /// </summary>
  305. public void SelectNextPage()
  306. {
  307. if (ActivePageIndex < Pages.Count - 1)
  308. ActivePageIndex++;
  309. else
  310. ActivePageIndex = 0;
  311. }
  312. /// <summary>
  313. /// Selects the previous page.
  314. /// </summary>
  315. public void SelectPrevPage()
  316. {
  317. if (ActivePageIndex > 0)
  318. ActivePageIndex--;
  319. else
  320. ActivePageIndex = Pages.Count - 1;
  321. }
  322. #endregion
  323. /// <summary>
  324. /// Initializes a new instance of the <see cref="PageControl"/> class with default settings.
  325. /// </summary>
  326. public PageControl()
  327. {
  328. highlightPageIndex = -1;
  329. selectorTabHeight = 35;
  330. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
  331. }
  332. }
  333. /// <summary>
  334. /// This class represents a single page of the <see cref="PageControl"/> control.
  335. /// </summary>
  336. [ToolboxItem(false)]
  337. public class PageControlPage : Panel
  338. {
  339. private Image image;
  340. /// <summary>
  341. /// Gets or sets the image associated with this page.
  342. /// </summary>
  343. public Image Image
  344. {
  345. get { return image; }
  346. set
  347. {
  348. image = value;
  349. if (Parent is PageControl)
  350. Parent.Refresh();
  351. }
  352. }
  353. /// <summary>
  354. /// Gets or sets the page caption text.
  355. /// </summary>
  356. [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  357. [Category("Appearance")]
  358. public override string Text
  359. {
  360. get { return base.Text; }
  361. set
  362. {
  363. base.Text = value;
  364. if (Parent is PageControl)
  365. Parent.Refresh();
  366. }
  367. }
  368. }
  369. }