ThemePageControl.cpp 26 KB

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