ThemePageControl.cpp 26 KB

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