FRTabStrip.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing.Drawing2D;
  6. using FastReport.Utils;
  7. namespace FastReport.Controls
  8. {
  9. internal class Tab
  10. {
  11. public FRTabStrip TabControl { get; set; }
  12. public string Text { get; set; }
  13. public object Tag { get; set; }
  14. public Image Image { get; set; }
  15. public bool Visible { get; set; }
  16. public bool CloseButton { get; set; }
  17. public bool AllowMove { get; set; }
  18. public Rectangle DisplayRectangle { get; set; }
  19. public int CalcWidth()
  20. {
  21. if (!Visible)
  22. return 0;
  23. int _2 = TabControl.LogicalToDevice(2);
  24. int _16 = TabControl.LogicalToDevice(16);
  25. int result = _2;
  26. if (Image != null)
  27. result += _16;
  28. #if AVALONIA
  29. TextRenderer.FontScale = (float)TabControl.DpiScale;
  30. #endif
  31. result += _2 + TextRenderer.MeasureText(Text, TabControl.Font).Width + _2;
  32. if (CloseButton)
  33. result += _16 + _2;
  34. return result;
  35. }
  36. public Tab()
  37. {
  38. AllowMove = true;
  39. CloseButton = true;
  40. Visible = true;
  41. }
  42. public Tab(string text) : this()
  43. {
  44. Text = text;
  45. }
  46. public Tab(string text, Image image) : this(text)
  47. {
  48. Image = image;
  49. }
  50. }
  51. internal class TabCollection : CollectionBase
  52. {
  53. private FRTabStrip FOwner;
  54. public Tab this[int index]
  55. {
  56. get { return List[index] as Tab; }
  57. set { List[index] = value; }
  58. }
  59. public void AddRange(Tab[] range)
  60. {
  61. foreach (Tab t in range)
  62. {
  63. Add(t);
  64. }
  65. }
  66. public int Add(Tab value)
  67. {
  68. return List.Add(value);
  69. }
  70. public void Insert(int index, Tab value)
  71. {
  72. List.Insert(index, value);
  73. }
  74. public void Remove(Tab value)
  75. {
  76. List.Remove(value);
  77. }
  78. public int IndexOf(Tab value)
  79. {
  80. return List.IndexOf(value);
  81. }
  82. public bool Contains(Tab value)
  83. {
  84. return List.Contains(value);
  85. }
  86. protected override void OnInsert(int index, Object value)
  87. {
  88. if (FOwner != null)
  89. {
  90. (value as Tab).TabControl = FOwner;
  91. }
  92. }
  93. public TabCollection(FRTabStrip owner)
  94. {
  95. FOwner = owner;
  96. }
  97. }
  98. internal delegate void TabMovedEventHandler(object sender, TabMovedEventArgs e);
  99. internal class TabMovedEventArgs
  100. {
  101. public int OldIndex;
  102. public int NewIndex;
  103. public TabMovedEventArgs(int oldIndex, int newIndex)
  104. {
  105. OldIndex = oldIndex;
  106. NewIndex = newIndex;
  107. }
  108. }
  109. internal enum TabOrientation
  110. {
  111. Top,
  112. Bottom
  113. }
  114. internal class FRTabStrip : Control
  115. {
  116. private int selectedTabIndex;
  117. private bool scrollButtonsVisible;
  118. private int highlightTabIndex;
  119. private bool highlightLeftBtn;
  120. private bool highlightRightBtn;
  121. private bool highlightCloseButton;
  122. private int offset;
  123. private int saveTabIndex;
  124. private bool movingTab;
  125. private bool tabMoved;
  126. private int mouseOffset;
  127. private TabOrientation tabOrientation;
  128. private TabStripColors style = new TabStripColors();
  129. public TabCollection Tabs { get; }
  130. public int SelectedTabIndex
  131. {
  132. get { return selectedTabIndex; }
  133. set
  134. {
  135. if (value < 0)
  136. value = -1;
  137. if (value > Tabs.Count - 1)
  138. value = Tabs.Count - 1;
  139. selectedTabIndex = value;
  140. EnsureTabVisible();
  141. Refresh();
  142. SelectedTabChanged?.Invoke(this, null);
  143. }
  144. }
  145. public Tab SelectedTab
  146. {
  147. get
  148. {
  149. if (SelectedTabIndex == -1 || SelectedTabIndex >= Tabs.Count)
  150. return null;
  151. return Tabs[SelectedTabIndex];
  152. }
  153. set
  154. {
  155. SelectedTabIndex = Tabs.IndexOf(value);
  156. }
  157. }
  158. public bool AllowTabReorder { get; set; }
  159. public TabOrientation TabOrientation
  160. {
  161. get { return tabOrientation; }
  162. set
  163. {
  164. tabOrientation = value;
  165. Refresh();
  166. }
  167. }
  168. public TabStripColors Style
  169. {
  170. get { return style; }
  171. set
  172. {
  173. style = value;
  174. Refresh();
  175. }
  176. }
  177. public event EventHandler SelectedTabChanged;
  178. public event EventHandler TabClosed;
  179. public event TabMovedEventHandler TabMoved;
  180. private void EnsureTabVisible()
  181. {
  182. if (Width == 0)
  183. return;
  184. Tab tab = SelectedTab;
  185. if (tab == null)
  186. return;
  187. int tabsWidth = CalcItems();
  188. int _4 = this.LogicalToDevice(4);
  189. int add = scrollButtonsVisible ? this.LogicalToDevice(40) : 0;
  190. if (tab.DisplayRectangle.Left < 0)
  191. offset += -tab.DisplayRectangle.Left + _4;
  192. else if (tab.DisplayRectangle.Right > Width - add)
  193. offset -= tab.DisplayRectangle.Right - (Width - add) + _4;
  194. if (offset > 0)
  195. offset = 0;
  196. if (tabsWidth < DisplayRectangle.Width)
  197. offset = 0;
  198. Refresh();
  199. }
  200. private int CalcItems()
  201. {
  202. int _3 = this.LogicalToDevice(3);
  203. int _4 = this.LogicalToDevice(4);
  204. int x = _4 + offset;
  205. int tabsWidth = 0;
  206. foreach (Tab tab in Tabs)
  207. {
  208. if (!tab.Visible)
  209. continue;
  210. int width = tab.CalcWidth();
  211. if (TabOrientation == TabOrientation.Top)
  212. tab.DisplayRectangle = new Rectangle(x, _3, width, Height - _3);
  213. else
  214. tab.DisplayRectangle = new Rectangle(x, 0, width, Height - _4);
  215. x += width;
  216. tabsWidth += width;
  217. }
  218. return tabsWidth;
  219. }
  220. private void HighlightButton(Graphics g, int x, int y)
  221. {
  222. if (!scrollButtonsVisible)
  223. return;
  224. int _16 = this.LogicalToDevice(16);
  225. using (var brush = new SolidBrush(Color.FromArgb(193, 210, 238)))
  226. using (var pen = new Pen(Color.FromArgb(49, 106, 197), this.LogicalToDevice(1f)))
  227. {
  228. g.FillRectangle(brush, x, y, _16, _16);
  229. g.DrawRectangle(pen, x, y, _16, _16);
  230. }
  231. }
  232. private void ScrollLeft()
  233. {
  234. if (offset < 0)
  235. {
  236. offset += SelectedTab.DisplayRectangle.Width;
  237. if (offset > 0)
  238. offset = 0;
  239. }
  240. Refresh();
  241. }
  242. private void ScrollRight()
  243. {
  244. int tabsWidth = 0;
  245. for (int i = 0; i < Tabs.Count - 1; i++)
  246. tabsWidth += Tabs[i].DisplayRectangle.Width;
  247. if (-offset < tabsWidth)
  248. offset -= SelectedTab.DisplayRectangle.Width;
  249. Refresh();
  250. }
  251. protected override void OnPaint(PaintEventArgs e)
  252. {
  253. CalcItems();
  254. Graphics g = e.Graphics;
  255. Pen pen = new Pen(style.Border, this.LogicalToDevice(1f));
  256. LinearGradientBrush brush = new LinearGradientBrush(DisplayRectangle,
  257. style.GradientBegin,
  258. style.GradientEnd,
  259. LinearGradientMode.Vertical);
  260. g.FillRectangle(brush, DisplayRectangle);
  261. Brush activeBrush = new SolidBrush(style.ActiveTabBackground);
  262. // draw all tabs except selected one
  263. int tabsWidth = 0;
  264. for (int i = 0; i < Tabs.Count; i++)
  265. {
  266. Tab tab = Tabs[i];
  267. if (!tab.Visible)
  268. continue;
  269. Rectangle rect = tab.DisplayRectangle;
  270. tabsWidth += rect.Width;
  271. if (i != SelectedTabIndex)
  272. DrawTab(g, i, pen, activeBrush);
  273. }
  274. // draw top/bottom line
  275. using (Brush lineBrush = new SolidBrush(style.Border))
  276. {
  277. int h = this.LogicalToDevice(style.BorderWidth);
  278. if (TabOrientation == TabOrientation.Top)
  279. g.FillRectangle(lineBrush, 0, Height - h, Width, h);
  280. else
  281. g.FillRectangle(lineBrush, 0, 0, Width, h);
  282. }
  283. // draw active tab
  284. if (SelectedTabIndex != -1)
  285. DrawTab(g, SelectedTabIndex, pen, activeBrush);
  286. scrollButtonsVisible = tabsWidth > Width;
  287. if (scrollButtonsVisible)
  288. DrawScrollButtons(g, brush);
  289. pen.Dispose();
  290. brush.Dispose();
  291. activeBrush.Dispose();
  292. }
  293. private void DrawTab(Graphics g, int i, Pen pen, Brush brush)
  294. {
  295. int _1 = this.LogicalToDevice(1);
  296. int _2 = this.LogicalToDevice(2);
  297. int _3 = this.LogicalToDevice(3);
  298. int _4 = this.LogicalToDevice(4);
  299. int _5 = this.LogicalToDevice(5);
  300. int _16 = this.LogicalToDevice(16);
  301. Tab tab = Tabs[i];
  302. Rectangle rect = tab.DisplayRectangle;
  303. int x = rect.Left;
  304. if (i == SelectedTabIndex || i == highlightTabIndex)
  305. {
  306. g.FillRectangle(brush, rect.Left, rect.Top, rect.Width, rect.Height);
  307. if (TabOrientation == TabOrientation.Top)
  308. {
  309. g.DrawLines(pen, new Point[]
  310. {
  311. new Point(rect.Left, rect.Bottom),
  312. new Point(rect.Left, rect.Top),
  313. new Point(rect.Right, rect.Top),
  314. new Point(rect.Right, rect.Bottom)
  315. });
  316. }
  317. else
  318. {
  319. g.DrawLines(pen, new Point[]
  320. {
  321. new Point(rect.Left, rect.Top),
  322. new Point(rect.Left, rect.Bottom),
  323. new Point(rect.Right, rect.Bottom),
  324. new Point(rect.Right, rect.Top)
  325. });
  326. }
  327. }
  328. else if (i < Tabs.Count - 1)
  329. {
  330. g.DrawLine(pen, rect.Right, rect.Top + _3, rect.Right, rect.Bottom - _3);
  331. }
  332. if (tab.Image != null)
  333. {
  334. g.DrawImage(tab.Image, rect.Left + _3, rect.Top + _3);
  335. x += _16;
  336. }
  337. TextRenderer.DrawText(g, tab.Text, Font, new Point(x + _4, rect.Top + _2 + 1),
  338. i == SelectedTabIndex || i == highlightTabIndex ? style.ActiveTabForeground : style.InactiveTabForeground);
  339. if (tab.CloseButton)
  340. DrawCloseButton(g, rect.Right - _16, rect.Top + _4, i);
  341. }
  342. private void DrawCloseButton(Graphics g, int x, int y, int i)
  343. {
  344. int btnSize = this.LogicalToDevice(13);
  345. int _3 = this.LogicalToDevice(3);
  346. bool isHighlight = highlightCloseButton && i == highlightTabIndex;
  347. if (isHighlight)
  348. {
  349. using (var brush = new SolidBrush(Color.FromArgb(196, 43, 28)))
  350. g.FillRectangle(brush, new Rectangle(x, y, btnSize, btnSize));
  351. }
  352. using (var p = new Pen(
  353. isHighlight ? Color.White :
  354. i == SelectedTabIndex || i == highlightTabIndex ? style.ActiveTabForeground : style.InactiveTabForeground, this.LogicalToDevice(1f)))
  355. {
  356. g.DrawLine(p, x + _3, y + _3, x + btnSize - _3 - 1, y + btnSize - _3 - 1);
  357. g.DrawLine(p, x + _3, y + btnSize - _3 - 1, x + btnSize - _3 - 1, y + _3);
  358. }
  359. }
  360. private void DrawScrollButtons(Graphics g, Brush brush)
  361. {
  362. int _4 = this.LogicalToDevice(4);
  363. int _20 = this.LogicalToDevice(20);
  364. int _22 = this.LogicalToDevice(22);
  365. int _38 = this.LogicalToDevice(38);
  366. int _40 = this.LogicalToDevice(40);
  367. g.FillRectangle(brush, Width - _40, 1, _40, Height - 2);
  368. if (highlightLeftBtn)
  369. HighlightButton(g, Width - _38, _4);
  370. g.DrawImage(this.GetImage(186), Width - _38, _4);
  371. if (highlightRightBtn)
  372. HighlightButton(g, Width - _20, _4);
  373. g.DrawImage(this.GetImage(187), Width - _22, _4);
  374. }
  375. protected override void OnMouseDown(MouseEventArgs e)
  376. {
  377. base.OnMouseDown(e);
  378. int _6 = this.LogicalToDevice(6);
  379. int _16 = this.LogicalToDevice(16);
  380. int _20 = this.LogicalToDevice(20);
  381. int _24 = this.LogicalToDevice(24);
  382. int _38 = this.LogicalToDevice(38);
  383. movingTab = false;
  384. if (scrollButtonsVisible && e.X > Width - _38 && e.X < Width - _24)
  385. {
  386. ScrollLeft();
  387. }
  388. else if (scrollButtonsVisible && e.X > Width - _20 && e.X < Width - _6)
  389. {
  390. ScrollRight();
  391. }
  392. else
  393. {
  394. Tab tab = TabAt(e.X);
  395. if (tab != null)
  396. {
  397. if (tab.CloseButton && e.X > tab.DisplayRectangle.Right - _16 && e.X < tab.DisplayRectangle.Right)
  398. {
  399. // close button, handle in mouseup
  400. return;
  401. }
  402. SelectedTab = tab;
  403. movingTab = true;
  404. }
  405. }
  406. saveTabIndex = SelectedTabIndex;
  407. tabMoved = false;
  408. mouseOffset = 0;
  409. }
  410. private void CloseTab(Tab tab)
  411. {
  412. TabClosed?.Invoke(tab, null);
  413. }
  414. protected override void OnMouseMove(MouseEventArgs e)
  415. {
  416. base.OnMouseMove(e);
  417. int _6 = this.LogicalToDevice(6);
  418. int _16 = this.LogicalToDevice(16);
  419. int _20 = this.LogicalToDevice(20);
  420. int _24 = this.LogicalToDevice(24);
  421. int _38 = this.LogicalToDevice(38);
  422. if (e.Button == MouseButtons.None)
  423. {
  424. bool needRefresh = false;
  425. if (scrollButtonsVisible)
  426. {
  427. bool b = e.X > Width - _38 && e.X < Width - _24;
  428. if (b != highlightLeftBtn)
  429. {
  430. highlightLeftBtn = b;
  431. needRefresh = true;
  432. }
  433. b = e.X > Width - _20 && e.X < Width - _6;
  434. if (highlightRightBtn != b)
  435. {
  436. highlightRightBtn = b;
  437. needRefresh = true;
  438. }
  439. }
  440. int tabIndex = -1;
  441. Tab tab = TabAt(e.X);
  442. if (tab != null)
  443. {
  444. tabIndex = Tabs.IndexOf(tab);
  445. bool b = e.X > tab.DisplayRectangle.Right - _16 && e.X < tab.DisplayRectangle.Right;
  446. if (b != highlightCloseButton)
  447. {
  448. highlightCloseButton = b;
  449. needRefresh = true;
  450. }
  451. }
  452. if (highlightTabIndex != tabIndex)
  453. {
  454. highlightTabIndex = tabIndex;
  455. needRefresh = true;
  456. }
  457. if (needRefresh)
  458. Refresh();
  459. }
  460. else if (e.Button == MouseButtons.Left && movingTab && AllowTabReorder)
  461. {
  462. Tab oldTab = SelectedTab;
  463. Tab newTab = TabAt(e.X + mouseOffset);
  464. if (oldTab != null && newTab != null && newTab != oldTab && newTab.AllowMove && oldTab.AllowMove)
  465. {
  466. int oldIndex = Tabs.IndexOf(oldTab);
  467. int newIndex = Tabs.IndexOf(newTab);
  468. Tabs.Remove(oldTab);
  469. Tabs.Insert(newIndex, oldTab);
  470. Refresh();
  471. SelectedTabIndex = newIndex;
  472. tabMoved = true;
  473. #if !(WPF || AVALONIA)
  474. if (oldIndex < newIndex)
  475. mouseOffset = oldTab.DisplayRectangle.Left - e.X;
  476. else
  477. mouseOffset = e.X - oldTab.DisplayRectangle.Right;
  478. #endif
  479. }
  480. }
  481. }
  482. protected override void OnMouseUp(MouseEventArgs e)
  483. {
  484. if (tabMoved)
  485. {
  486. TabMoved?.Invoke(this, new TabMovedEventArgs(saveTabIndex, SelectedTabIndex));
  487. }
  488. else
  489. {
  490. int _16 = this.LogicalToDevice(16);
  491. Tab tab = TabAt(e.X);
  492. if (tab != null)
  493. {
  494. if (tab.CloseButton && e.X > tab.DisplayRectangle.Right - _16 && e.X < tab.DisplayRectangle.Right)
  495. CloseTab(tab);
  496. }
  497. }
  498. base.OnMouseUp(e);
  499. }
  500. protected override void OnMouseLeave(EventArgs e)
  501. {
  502. base.OnMouseLeave(e);
  503. if (highlightLeftBtn || highlightRightBtn || highlightTabIndex != -1)
  504. {
  505. highlightLeftBtn = false;
  506. highlightRightBtn = false;
  507. highlightCloseButton = false;
  508. highlightTabIndex = -1;
  509. Refresh();
  510. }
  511. }
  512. public Tab TabAt(int x)
  513. {
  514. foreach (Tab tab in Tabs)
  515. {
  516. if (!tab.Visible)
  517. continue;
  518. if (x >= tab.DisplayRectangle.Left && x <= tab.DisplayRectangle.Right)
  519. return tab;
  520. }
  521. return null;
  522. }
  523. public FRTabStrip()
  524. {
  525. Tabs = new TabCollection(this);
  526. scrollButtonsVisible = true;
  527. AllowTabReorder = true;
  528. highlightTabIndex = -1;
  529. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
  530. Height = 24;
  531. }
  532. }
  533. }