ThemePageControl.cpp 26 KB

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