PageControl.cs 14 KB

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