1
0

ThemePageControl.cpp 25 KB

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