ThemePageControl.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <vsstyle.h>
  6. #include <memory>
  7. #include <PasTools.hpp>
  8. #include <TBXOfficeXPTheme.hpp>
  9. #include <TBX.hpp>
  10. #include <StrUtils.hpp>
  11. #include <CustomWinConfiguration.h>
  12. #include "ThemePageControl.h"
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. //---------------------------------------------------------------------------
  16. // Based on
  17. // https://www.codeproject.com/Articles/6355/XP-Themes-Tab-Control-in-any-orientation
  18. //---------------------------------------------------------------------------
  19. #define IDS_UTIL_TAB L"TAB"
  20. //---------------------------------------------------------------------------
  21. static inline void ValidCtrCheck(TThemePageControl *)
  22. {
  23. new TThemePageControl(NULL);
  24. }
  25. //---------------------------------------------------------------------------
  26. namespace Themepagecontrol
  27. {
  28. void __fastcall PACKAGE Register()
  29. {
  30. TComponentClass classes[2] = {__classid(TThemePageControl), __classid(TThemeTabSheet)};
  31. RegisterComponents(L"Scp", classes, 1);
  32. }
  33. }
  34. //----------------------------------------------------------------------------------------------------------
  35. __fastcall TThemeTabSheet::TThemeTabSheet(TComponent * Owner) :
  36. TTabSheet(Owner)
  37. {
  38. FShadowed = false;
  39. FButton = ttbNone;
  40. FCaptionTruncation = tttNone;
  41. }
  42. //----------------------------------------------------------------------------------------------------------
  43. TThemePageControl * TThemeTabSheet::GetParentPageControl()
  44. {
  45. return DebugNotNull(dynamic_cast<TThemePageControl *>(Parent));
  46. }
  47. //----------------------------------------------------------------------------------------------------------
  48. void __fastcall TThemeTabSheet::Invalidate()
  49. {
  50. TThemePageControl * ThemePageControl = GetParentPageControl();
  51. if (DebugAlwaysTrue(ThemePageControl != NULL))
  52. {
  53. ThemePageControl->InvalidateTab(TabIndex);
  54. }
  55. else
  56. {
  57. Parent->Invalidate();
  58. }
  59. }
  60. //----------------------------------------------------------------------------------------------------------
  61. void __fastcall TThemeTabSheet::SetShadowed(bool Value)
  62. {
  63. if (Shadowed != Value)
  64. {
  65. FShadowed = Value;
  66. Invalidate();
  67. }
  68. }
  69. //----------------------------------------------------------------------------------------------------------
  70. void __fastcall TThemeTabSheet::SetButton(TThemeTabSheetButtons Value)
  71. {
  72. if (Button != Value)
  73. {
  74. FButton = Value;
  75. Invalidate();
  76. }
  77. }
  78. //----------------------------------------------------------------------------------------------------------
  79. void TThemeTabSheet::SetBaseCaption(const UnicodeString & value)
  80. {
  81. if (FBaseCaption != value)
  82. {
  83. FBaseCaption = value;
  84. UpdateCaption();
  85. }
  86. }
  87. //----------------------------------------------------------------------------------------------------------
  88. UnicodeString TThemeTabSheet::TruncatedCaption()
  89. {
  90. UnicodeString Result = FBaseCaption;
  91. TThemePageControl * ParentPageControl = GetParentPageControl();
  92. if (ParentPageControl->FSessionTabShrink > 0)
  93. {
  94. if (FCaptionTruncation == tttNone)
  95. {
  96. // noop
  97. }
  98. else if (FCaptionTruncation == tttEllipsis)
  99. {
  100. if (ParentPageControl->FSessionTabShrink == 1)
  101. {
  102. Result = Result.SubString(1, 1);
  103. }
  104. else if (ParentPageControl->FSessionTabShrink < Result.Length())
  105. {
  106. Result = Result.SubString(1, ParentPageControl->FSessionTabShrink - 1) + Ellipsis;
  107. }
  108. }
  109. else if (DebugAlwaysTrue(FCaptionTruncation == tttNoText))
  110. {
  111. Result = EmptyStr;
  112. }
  113. }
  114. return Result;
  115. }
  116. //----------------------------------------------------------------------------------------------------------
  117. void TThemeTabSheet::UpdateCaption()
  118. {
  119. UnicodeString ACaption = TruncatedCaption();
  120. TThemePageControl * ParentPageControl = GetParentPageControl();
  121. if (UseThemes() && (Button != ttbNone))
  122. {
  123. int OrigWidth = ParentPageControl->Canvas->TextWidth(ACaption);
  124. int TabButtonWidth = ParentPageControl->TabButtonSize();
  125. while (ParentPageControl->Canvas->TextWidth(ACaption) < OrigWidth + TabButtonWidth)
  126. {
  127. ACaption += L" ";
  128. }
  129. }
  130. Caption = ACaption;
  131. ParentPageControl->TabChanged(TabIndex);
  132. }
  133. //----------------------------------------------------------------------------------------------------------
  134. UnicodeString TThemeTabSheet::GetBaseCaption()
  135. {
  136. DebugAssert(StartsStr(FBaseCaption, Caption));
  137. DebugAssert(RightStr(Caption, Caption.Length() - FBaseCaption.Length()).Trim().Length() == 0);
  138. return FBaseCaption;
  139. }
  140. //----------------------------------------------------------------------------------------------------------
  141. void TThemeTabSheet::SetCaptionTruncation(TThemeTabCaptionTruncation Value)
  142. {
  143. if (FCaptionTruncation != Value)
  144. {
  145. FCaptionTruncation = Value;
  146. UpdateCaption();
  147. }
  148. }
  149. //----------------------------------------------------------------------------------------------------------
  150. //----------------------------------------------------------------------------------------------------------
  151. __fastcall TThemePageControl::TThemePageControl(TComponent * Owner) :
  152. TPageControl(Owner)
  153. {
  154. FOldTabIndex = -1;
  155. FHotTabButton = -1;
  156. FClickedButton = -1;
  157. FSessionTabShrink = 0;
  158. FOnTabButtonClick = NULL;
  159. FOnTabHint = NULL;
  160. FTabTheme = NULL;
  161. FActiveTabTheme = NULL;
  162. }
  163. //----------------------------------------------------------------------------------------------------------
  164. int __fastcall TThemePageControl::GetTabsHeight()
  165. {
  166. // The Calculated height includes tab/contents separator line on Windows 7/8,
  167. // but not on Windows XP
  168. TRect Rect = GetClientRect();
  169. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&Rect);
  170. int Result = Rect.Top - 1;
  171. // Two different ways to calculate the same, not sure which one is more reliable,
  172. // so we want to know in case they differ.
  173. if (DebugAlwaysTrue(PageCount >= 0))
  174. {
  175. TRect Rect = TabRect(0);
  176. int Result2 = Rect.Bottom + 1;
  177. // On Windows 10 with 200% scaling, the first is 40, the second is 42.
  178. // With 250% scaling it's 50 vs 53.
  179. // Using the larger.
  180. if (Result2 > Result)
  181. {
  182. DebugAssert(IsWin10());
  183. Result = Result2;
  184. }
  185. }
  186. return Result;
  187. }
  188. //----------------------------------------------------------------------------------------------------------
  189. void __fastcall TThemePageControl::PaintWindow(HDC DC)
  190. {
  191. // Themes not enabled, give up
  192. if (!UseThemes())
  193. {
  194. TPageControl::PaintWindow(DC);
  195. return;
  196. }
  197. HTHEME Theme = OpenThemeData(NULL, IDS_UTIL_TAB);
  198. // TODO use GetClipBox
  199. TRect PageRect = GetClientRect();
  200. // 1st paint the tab body
  201. TRect ClientRect = PageRect;
  202. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&PageRect);
  203. ClientRect.Top = PageRect.Top - 2;
  204. DrawThemeBackground(Theme, DC, TABP_PANE, 0, &ClientRect, NULL);
  205. // 2nd paint the inactive tabs
  206. int SelectedIndex = TabIndex; // optimization
  207. for (int Tab = 0; Tab < PageCount; Tab++)
  208. {
  209. if (Tab != SelectedIndex)
  210. {
  211. DrawThemesXpTab(DC, Theme, Tab);
  212. }
  213. }
  214. if (SelectedIndex >= 0)
  215. {
  216. DrawThemesXpTab(DC, Theme, SelectedIndex);
  217. }
  218. CloseThemeData(Theme);
  219. }
  220. //----------------------------------------------------------------------------------------------------------
  221. TThemeTabSheetButtons __fastcall TThemePageControl::GetTabButton(int Index)
  222. {
  223. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(Pages[Index]);
  224. return (UseThemes() && (ThemeTabSheet != NULL)) ? ThemeTabSheet->Button : ttbNone;
  225. }
  226. //----------------------------------------------------------------------------------------------------------
  227. void __fastcall TThemePageControl::DrawThemesXpTab(HDC DC, HTHEME Theme, int Tab)
  228. {
  229. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(Pages[Tab]);
  230. bool Shadowed = (ThemeTabSheet != NULL) ? ThemeTabSheet->Shadowed : false;
  231. TRect Rect = TabRect(Tab);
  232. ItemTabRect(Tab, Rect);
  233. int State;
  234. TTBXTheme * ATabTheme;
  235. if (Tab != TabIndex)
  236. {
  237. TPoint Point = ScreenToClient(Mouse->CursorPos);
  238. int HotIndex = IndexOfTabAt(Point.X, Point.Y);
  239. State = (Tab == HotIndex ? TIS_HOT : (Shadowed ? TIS_DISABLED : TIS_NORMAL));
  240. ATabTheme = TabTheme;
  241. }
  242. else
  243. {
  244. State = TIS_SELECTED;
  245. ATabTheme = (ActiveTabTheme != NULL) ? ActiveTabTheme : TabTheme;
  246. }
  247. DrawThemesXpTabItem(DC, Theme, Tab, Rect, State, Shadowed, ATabTheme);
  248. }
  249. //----------------------------------------------------------------------------------------------------------
  250. static TTBXItemInfo GetItemInfo(int State)
  251. {
  252. TTBXItemInfo ItemInfo;
  253. memset(&ItemInfo, 0, sizeof(ItemInfo));
  254. ItemInfo.Enabled = true;
  255. ItemInfo.ViewType =
  256. VT_TOOLBAR | TVT_EMBEDDED |
  257. FLAGMASK(State == TIS_SELECTED, ISF_SELECTED);
  258. return ItemInfo;
  259. }
  260. //----------------------------------------------------------------------------------------------------------
  261. void __fastcall TThemePageControl::DrawThemesXpTabItem(
  262. HDC DC, HTHEME Theme, int Item, const TRect & Rect, int State, bool Shadowed, TTBXTheme * ATabTheme)
  263. {
  264. TRect PaintRect = Rect;
  265. if ((State == TIS_SELECTED) || (ATabTheme != NULL))
  266. {
  267. PaintRect.Bottom++;
  268. }
  269. if (ATabTheme != NULL)
  270. {
  271. std::unique_ptr<TCanvas> CanvasMem(new TCanvas());
  272. CanvasMem->Handle = DC;
  273. ATabTheme->PaintFrame(CanvasMem.get(), PaintRect, GetItemInfo(State));
  274. }
  275. else
  276. {
  277. int PartID = (Item == 0) ? TABP_TABITEMLEFTEDGE : TABP_TABITEM;
  278. DrawThemeBackground(Theme, DC, PartID, State, &PaintRect, NULL);
  279. }
  280. if (Item >= 0)
  281. {
  282. DrawTabItem(DC, Item, Rect, State, Shadowed, ATabTheme);
  283. }
  284. }
  285. //----------------------------------------------------------------------------------------------------------
  286. void __fastcall TThemePageControl::ItemTabRect(int Item, TRect & Rect)
  287. {
  288. if (Item == TabIndex)
  289. {
  290. // Countered in TabButtonRect
  291. Rect.Inflate(2, 2);
  292. Rect.Bottom--;
  293. }
  294. }
  295. //----------------------------------------------------------------------------------------------------------
  296. void __fastcall TThemePageControl::ItemContentsRect(int Item, TRect & Rect)
  297. {
  298. bool Selected = (Item == TabIndex);
  299. Rect.Left += 6;
  300. Rect.Top += 2;
  301. if (Selected)
  302. {
  303. Rect.Bottom -= 2;
  304. Rect.Top += 1;
  305. }
  306. else
  307. {
  308. Rect.Bottom += 2;
  309. Rect.Top += 3;
  310. }
  311. }
  312. //----------------------------------------------------------------------------------------------------------
  313. bool __fastcall TThemePageControl::HasItemImage(int Item)
  314. {
  315. return (Images != NULL) && (Pages[Item]->ImageIndex >= 0);
  316. }
  317. //----------------------------------------------------------------------------------------------------------
  318. void __fastcall TThemePageControl::ItemTextRect(int Item, TRect & Rect)
  319. {
  320. if (HasItemImage(Item))
  321. {
  322. Rect.Left += Images->Width + 3;
  323. }
  324. else
  325. {
  326. Rect.Left -= 2;
  327. }
  328. Rect.Right -= 3;
  329. OffsetRect(&Rect, 0, ((Item == TabIndex) ? 0 : -2));
  330. }
  331. //----------------------------------------------------------------------------------------------------------
  332. void TThemePageControl::DrawCross(HDC DC, int Width, COLORREF Color, const TRect & Rect)
  333. {
  334. HPEN Pen = CreatePen(PS_SOLID, Width, Color);
  335. HPEN OldPen = static_cast<HPEN>(SelectObject(DC, Pen));
  336. // To-and-back - to make both ends look the same
  337. MoveToEx(DC, Rect.Left, Rect.Bottom - 1, NULL);
  338. LineTo(DC, Rect.Right - 1, Rect.Top);
  339. LineTo(DC, Rect.Left, Rect.Bottom - 1);
  340. MoveToEx(DC, Rect.Left, Rect.Top, NULL);
  341. LineTo(DC, Rect.Right - 1, Rect.Bottom - 1);
  342. LineTo(DC, Rect.Left, Rect.Top);
  343. SelectObject(DC, OldPen);
  344. DeleteObject(Pen);
  345. }
  346. //----------------------------------------------------------------------------------------------------------
  347. void TThemePageControl::DrawDropDown(HDC DC, int Radius, int X, int Y, COLORREF Color, int Grow)
  348. {
  349. // Optimized for even-sized Rect (100% scaling), may need adjustments for even-sized to correctly center
  350. TPoint Points[] = {
  351. Point(X - Radius - 1 - Grow, Y), Point(X + Radius + Grow, Y),
  352. Point(X, Y + Radius + Grow), Point(X - 1, Y + Radius + Grow)
  353. };
  354. HBRUSH Brush = CreateSolidBrush(Color);
  355. HPEN Pen = CreatePen(PS_SOLID, 1, Color);
  356. HGDIOBJ OldBrush = SelectObject(DC, Brush);
  357. HGDIOBJ OldPen = SelectObject(DC, Pen);
  358. Polygon(DC, Points, LENOF(Points));
  359. SelectObject(DC, OldPen);
  360. SelectObject(DC, OldBrush);
  361. DeleteObject(Brush);
  362. DeleteObject(Pen);
  363. }
  364. //----------------------------------------------------------------------------------------------------------
  365. // Draw tab item context: possible icon and text
  366. void __fastcall TThemePageControl::DrawTabItem(HDC DC, int Item, TRect Rect, int State, bool Shadowed, TTBXTheme * ATabTheme)
  367. {
  368. TRect OrigRect = Rect;
  369. ItemContentsRect(Item, Rect);
  370. UnicodeString Text = Pages[Item]->Caption;
  371. bool Selected = (State == TIS_SELECTED);
  372. if (HasItemImage(Item))
  373. {
  374. int Left;
  375. if (!Text.IsEmpty())
  376. {
  377. Left = Rect.Left + (Selected ? 2 : 0);
  378. }
  379. else
  380. {
  381. Left = OrigRect.Left + (OrigRect.Right - Images->Width - OrigRect.Left) / 2;
  382. }
  383. int Y = ((Rect.Top + Rect.Bottom - Images->Height) / 2) - 1 + (Selected ? 0 : -2);
  384. std::unique_ptr<TCanvas> Canvas(new TCanvas());
  385. Canvas->Handle = DC;
  386. Images->Draw(Canvas.get(), Left, Y, Pages[Item]->ImageIndex, !Shadowed);
  387. }
  388. int TextHeight = 20;
  389. int OldMode = SetBkMode(DC, TRANSPARENT);
  390. if (!Text.IsEmpty())
  391. {
  392. ItemTextRect(Item, Rect);
  393. if (ATabTheme != NULL)
  394. {
  395. SetTextColor(DC, ATabTheme->GetItemTextColor(GetItemInfo(State)));
  396. }
  397. HFONT OldFont = (HFONT)SelectObject(DC, Font->Handle);
  398. wchar_t * Buf = new wchar_t[Text.Length() + 1 + 4];
  399. wcscpy(Buf, Text.c_str());
  400. TRect TextRect(0, 0, Rect.Right - Rect.Left, TextHeight);
  401. // Truncates too long texts with ellipsis
  402. ::DrawText(DC, Buf, -1, &TextRect, DT_CALCRECT | DT_SINGLELINE | DT_MODIFYSTRING | DT_END_ELLIPSIS);
  403. DrawText(DC, Buf, -1, &Rect, DT_NOPREFIX | DT_CENTER);
  404. delete[] Buf;
  405. TThemeTabSheetButtons Button = GetTabButton(Item);
  406. if (Button != ttbNone)
  407. {
  408. Rect = TabButtonRect(Item);
  409. TTBXItemInfo ButtonItemInfo = GetItemInfo(State);
  410. if (IsHotButton(Item))
  411. {
  412. ButtonItemInfo.HoverKind = hkMouseHover;
  413. std::unique_ptr<TCanvas> CanvasMem(new TCanvas());
  414. CanvasMem->Handle = DC;
  415. CurrentTheme->PaintFrame(CanvasMem.get(), Rect, ButtonItemInfo);
  416. }
  417. COLORREF BackColor = GetPixel(DC, Rect.Left + (Rect.Width() / 2), Rect.Top + (Rect.Height() / 2));
  418. COLORREF ShapeColor;
  419. if (ATabTheme != NULL)
  420. {
  421. ShapeColor = ColorToRGB(ATabTheme->GetItemTextColor(ButtonItemInfo));
  422. }
  423. else
  424. {
  425. ShapeColor = ColorToRGB(Font->Color);
  426. }
  427. #define BlendValue(FN) (((4 * static_cast<int>(FN(BackColor))) + static_cast<int>(FN(ShapeColor))) / 5)
  428. COLORREF BlendColor = RGB(BlendValue(GetRValue), BlendValue(GetGValue), BlendValue(GetBValue));
  429. #undef BlendValue
  430. if (Button == ttbClose)
  431. {
  432. int CrossPadding = GetCrossPadding();
  433. TRect CrossRect(Rect);
  434. CrossRect.Inflate(-CrossPadding, -CrossPadding);
  435. int CrossWidth = ScaleByTextHeight(this, 1);
  436. DrawCross(DC, CrossWidth + 1, BlendColor, CrossRect);
  437. DrawCross(DC, CrossWidth, ShapeColor, CrossRect);
  438. }
  439. else if (DebugAlwaysTrue(Button == ttbDropDown))
  440. {
  441. // See TTBXOfficeXPTheme.PaintDropDownArrow
  442. int Radius = ScaleByTextHeight(this, 2);
  443. int X = ((Rect.Left + Rect.Right)) / 2;
  444. int Y = ((Rect.Top + Rect.Bottom) / 2) - (Radius * 2 / 3);
  445. DrawDropDown(DC, Radius, X, Y, BlendColor, 1);
  446. DrawDropDown(DC, Radius, X, Y, ShapeColor, 0);
  447. }
  448. }
  449. SelectObject(DC, OldFont);
  450. }
  451. SetBkMode(DC, OldMode);
  452. }
  453. //----------------------------------------------------------------------------------------------------------
  454. int __fastcall TThemePageControl::TabButtonSize()
  455. {
  456. return ScaleByTextHeight(this, 16);
  457. }
  458. //----------------------------------------------------------------------------------------------------------
  459. int __fastcall TThemePageControl::GetCrossPadding()
  460. {
  461. return ScaleByTextHeight(this, 4);
  462. }
  463. //----------------------------------------------------------------------------------------------------------
  464. TRect __fastcall TThemePageControl::TabButtonRect(int Index)
  465. {
  466. TRect Rect = TabRect(Index);
  467. ItemTabRect(Index, Rect);
  468. ItemContentsRect(Index, Rect);
  469. ItemTextRect(Index, Rect);
  470. int ATabButtonSize = TabButtonSize();
  471. int CrossPadding = GetCrossPadding();
  472. TEXTMETRIC TextMetric;
  473. Canvas->Font = Font;
  474. GetTextMetrics(Canvas->Handle, &TextMetric);
  475. Rect.Top += TextMetric.tmAscent - ATabButtonSize + CrossPadding;
  476. Rect.Left = Rect.Right - ATabButtonSize - ScaleByTextHeight(this, 1);
  477. if (Index == TabIndex)
  478. {
  479. // To counter Inflate(2, 2) in ItemTabRect
  480. Rect.Left -= 2;
  481. }
  482. Rect.Right = Rect.Left + ATabButtonSize;
  483. Rect.Bottom = Rect.Top + ATabButtonSize;
  484. return Rect;
  485. }
  486. //----------------------------------------------------------------------------------------------------------
  487. bool TThemePageControl::IsHotButton(int Index)
  488. {
  489. // This was an attempt to allow tracking close buttons, even while drop down button menu is popped,
  490. // but MouseMove does not trigger then.
  491. return (Index == FClickedButton) || (Index == FHotTabButton);
  492. }
  493. //----------------------------------------------------------------------------------------------------------
  494. void TThemePageControl::TabChanged(int Index)
  495. {
  496. // When the "clicked" tab changes, it's probably not anymore the tab that was actually clicked.
  497. // For example, when the last tab is closed, it's replaced with either local-local tab (without the X button),
  498. // or removed altogether. The Login dialog pops up and when new session is opened, its tab's X button is rendered clicked,
  499. // until connection openning finishes (and WMLButtonDown finishes).
  500. if (Index == FClickedButton)
  501. {
  502. UpdateHotButton(FClickedButton, -1);
  503. }
  504. }
  505. //----------------------------------------------------------------------------------------------------------
  506. void TThemePageControl::UpdateHotButton(int & Ref, int Index)
  507. {
  508. if (Ref != Index)
  509. {
  510. bool WasHot = (Index >= 0) && IsHotButton(Index);
  511. int Prev = Ref;
  512. Ref = Index;
  513. if ((Prev >= 0) && !IsHotButton(Prev))
  514. {
  515. InvalidateTab(Prev);
  516. }
  517. if ((Index >= 0) && !WasHot)
  518. {
  519. InvalidateTab(Index);
  520. }
  521. }
  522. }
  523. //----------------------------------------------------------------------------------------------------------
  524. void __fastcall TThemePageControl::MouseMove(TShiftState Shift, int X, int Y)
  525. {
  526. TPageControl::MouseMove(Shift, X, Y);
  527. UpdateHotButton(FHotTabButton, IndexOfTabButtonAt(X, Y));
  528. }
  529. //----------------------------------------------------------------------------------------------------------
  530. int __fastcall TThemePageControl::IndexOfTabButtonAt(int X, int Y)
  531. {
  532. int Result = IndexOfTabAt(X, Y);
  533. if ((Result < 0) ||
  534. !GetTabButton(Result) ||
  535. !TabButtonRect(Result).Contains(TPoint(X, Y)))
  536. {
  537. Result = -1;
  538. }
  539. return Result;
  540. }
  541. //----------------------------------------------------------------------------------------------------------
  542. bool __fastcall TThemePageControl::CanChange()
  543. {
  544. FOldTabIndex = ActivePageIndex;
  545. return TPageControl::CanChange();
  546. }
  547. //----------------------------------------------------------------------------------------------------------
  548. void __fastcall TThemePageControl::InvalidateTab(int Index)
  549. {
  550. if (HandleAllocated())
  551. {
  552. TRect Rect = TabRect(Index);
  553. if (Index == TabIndex)
  554. {
  555. Rect.Inflate(2, 2);
  556. }
  557. // Original code was invalidating range against parent window
  558. // (recalculating coordinates first)
  559. InvalidateRect(Handle, &Rect, true);
  560. }
  561. }
  562. //----------------------------------------------------------------------------------------------------------
  563. void __fastcall TThemePageControl::Change()
  564. {
  565. // note that TabIndex yields correct value already here,
  566. // while ActivePageIndex is not updated yet
  567. if ((FOldTabIndex >= 0) && (FOldTabIndex != TabIndex) && UseThemes())
  568. {
  569. InvalidateTab(FOldTabIndex);
  570. }
  571. TPageControl::Change();
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TThemePageControl::WMLButtonDown(TWMLButtonDown & Message)
  575. {
  576. int Index = IndexOfTabButtonAt(Message.XPos, Message.YPos);
  577. if (Index >= 0)
  578. {
  579. Message.Result = 1;
  580. if (FOnTabButtonClick != NULL)
  581. {
  582. UpdateHotButton(FClickedButton, Index);
  583. try
  584. {
  585. FOnTabButtonClick(this, Index);
  586. }
  587. __finally
  588. {
  589. UpdateHotButton(FClickedButton, -1);
  590. }
  591. }
  592. }
  593. else
  594. {
  595. TPageControl::Dispatch(&Message);
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void TThemePageControl::CMHintShow(TCMHintShow & HintShow)
  600. {
  601. TPageControl::Dispatch(&HintShow);
  602. if (OnTabHint != NULL)
  603. {
  604. int Tab = IndexOfTabAt(HintShow.HintInfo->CursorPos.x, HintShow.HintInfo->CursorPos.y);
  605. OnTabHint(this, Tab, HintShow.HintInfo->HintStr);
  606. HintShow.HintInfo->CursorRect = TabRect(Tab);
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TThemePageControl::Dispatch(void * Message)
  611. {
  612. TMessage * M = reinterpret_cast<TMessage*>(Message);
  613. if (M->Msg == CM_MOUSELEAVE)
  614. {
  615. UpdateHotButton(FHotTabButton, -1);
  616. TPageControl::Dispatch(Message);
  617. }
  618. else if (M->Msg == WM_LBUTTONDOWN)
  619. {
  620. WMLButtonDown(*reinterpret_cast<TWMLButtonDown *>(M));
  621. }
  622. else if (M->Msg == WM_WANTS_SCREEN_TIPS)
  623. {
  624. M->Result = 1;
  625. }
  626. else if (M->Msg == CM_HINTSHOW)
  627. {
  628. CMHintShow(*reinterpret_cast<TCMHintShow *>(M));
  629. }
  630. else
  631. {
  632. TPageControl::Dispatch(Message);
  633. }
  634. }
  635. //----------------------------------------------------------------------------------------------------------
  636. TThemeTabSheet * TThemePageControl::GetPage(int Index)
  637. {
  638. return DebugNotNull(dynamic_cast<TThemeTabSheet *>(TPageControl::Pages[Index]));
  639. }
  640. //----------------------------------------------------------------------------------------------------------
  641. TThemeTabSheet * TThemePageControl::GetActivePage()
  642. {
  643. TTabSheet * TabSheet = TPageControl::ActivePage;
  644. TThemeTabSheet * Result = NULL;
  645. if (TabSheet != NULL)
  646. {
  647. Result = DebugNotNull(dynamic_cast<TThemeTabSheet *>(TabSheet));
  648. }
  649. return Result;
  650. }
  651. //----------------------------------------------------------------------------------------------------------
  652. int TThemePageControl::TotalTabsWidth()
  653. {
  654. TRect FirstTabRect = TabRect(0);
  655. TRect LastTabRect = TabRect(PageCount - 1);
  656. return -FirstTabRect.Left + LastTabRect.Right;
  657. }
  658. //----------------------------------------------------------------------------------------------------------
  659. void TThemePageControl::UpdateTabsCaptionTruncation()
  660. {
  661. DisableAlign();
  662. Tabs->BeginUpdate();
  663. try
  664. {
  665. FSessionTabShrink = 0;
  666. for (int Index = 0; Index < PageCount; Index++)
  667. {
  668. Pages[Index]->UpdateCaption();
  669. }
  670. int TabsWidth = TotalTabsWidth();
  671. int MaxWidth = ClientWidth - ScaleByTextHeight(this, 8); // arbitrary margin to avoid left/right buttons flicker
  672. if (TabsWidth > MaxWidth)
  673. {
  674. int NeedWidth = (TabsWidth - MaxWidth);
  675. int MaxLen = 0;
  676. int CaptionsWidth = 0;
  677. for (int Index = 0; Index < PageCount; Index++)
  678. {
  679. UnicodeString TabCaption = Pages[Index]->BaseCaption;
  680. MaxLen = std::max(MaxLen, TabCaption.Length());
  681. CaptionsWidth += Canvas->TextWidth(TabCaption);
  682. }
  683. bool Repeat;
  684. do
  685. {
  686. int NewShrink;
  687. if (FSessionTabShrink == 0)
  688. {
  689. NewShrink = MaxLen; // remove only new tab caption
  690. }
  691. else
  692. {
  693. NewShrink = FSessionTabShrink - 1;
  694. }
  695. if (NewShrink < 1)
  696. {
  697. Repeat = false;
  698. }
  699. else
  700. {
  701. FSessionTabShrink = NewShrink;
  702. int NewCaptionsWidth = 0;
  703. for (int Index = 0; Index < PageCount; Index++)
  704. {
  705. UnicodeString TabCaption = Pages[Index]->TruncatedCaption();
  706. NewCaptionsWidth += Canvas->TextWidth(TabCaption);
  707. }
  708. int GainedWidth = (CaptionsWidth - NewCaptionsWidth);
  709. Repeat = (GainedWidth < NeedWidth);
  710. }
  711. }
  712. while (Repeat);
  713. for (int Index = 0; Index < PageCount; Index++)
  714. {
  715. Pages[Index]->UpdateCaption();
  716. }
  717. }
  718. }
  719. __finally
  720. {
  721. Tabs->BeginUpdate();
  722. EnableAlign();
  723. }
  724. }
  725. //----------------------------------------------------------------------------------------------------------
  726. void TThemePageControl::SetActiveTabTheme(TTBXTheme * value)
  727. {
  728. if (FActiveTabTheme != value)
  729. {
  730. FActiveTabTheme = value;
  731. if (ActivePage != NULL)
  732. {
  733. ActivePage->Invalidate();
  734. }
  735. }
  736. }
  737. //----------------------------------------------------------------------------------------------------------
  738. void TThemePageControl::SetTabTheme(TTBXTheme * value)
  739. {
  740. if (FTabTheme != value)
  741. {
  742. FTabTheme = value;
  743. Invalidate();
  744. }
  745. }
  746. //----------------------------------------------------------------------------------------------------------
  747. #ifdef _DEBUG
  748. void __fastcall TThemePageControl::RequestAlign()
  749. {
  750. TPageControl::RequestAlign();
  751. }
  752. #endif
  753. //----------------------------------------------------------------------------------------------------------