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