ThemePageControl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <vsstyle.h>
  6. #include <memory>
  7. #include "ThemePageControl.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. // Based on
  12. // https://www.codeproject.com/Articles/6355/XP-Themes-Tab-Control-in-any-orientation
  13. //---------------------------------------------------------------------------
  14. //#define USE_DEFAULT_XP_TOPTAB // XP top tab is drawn only for test purpose. To use default, uncoment this line
  15. //---------------------------------------------------------------------------
  16. // constant string definitions here (or you can put it into resource string table)
  17. #define IDS_UTIL_TAB L"TAB"
  18. #define IDS_UTIL_UXTHEME L"UxTheme.dll"
  19. #define IDS_UTIL_THEMEACT "IsThemeActive"
  20. #define IDS_UTIL_THEMEOPN "OpenThemeData"
  21. #define IDS_UTIL_THEMEBCKG "DrawThemeBackground"
  22. //---------------------------------------------------------------------------
  23. static inline void ValidCtrCheck(TThemePageControl *)
  24. {
  25. new TThemePageControl(NULL);
  26. }
  27. //---------------------------------------------------------------------------
  28. namespace Themepagecontrol
  29. {
  30. void __fastcall PACKAGE Register()
  31. {
  32. TComponentClass classes[1] = {__classid(TThemePageControl)};
  33. RegisterComponents(L"Scp", classes, 0);
  34. }
  35. }
  36. //----------------------------------------------------------------------------------------------------------
  37. __fastcall TThemeTabSheet::TThemeTabSheet(TComponent * Owner) :
  38. TTabSheet(Owner)
  39. {
  40. FShadowed = false;
  41. }
  42. //----------------------------------------------------------------------------------------------------------
  43. void __fastcall TThemeTabSheet::SetShadowed(bool Value)
  44. {
  45. if (Shadowed != Value)
  46. {
  47. FShadowed = Value;
  48. TThemePageControl * ThemePageControl = dynamic_cast<TThemePageControl *>(Parent);
  49. if (DebugAlwaysTrue(ThemePageControl != NULL))
  50. {
  51. ThemePageControl->InvalidateTab(TabIndex);
  52. }
  53. else
  54. {
  55. Parent->Invalidate();
  56. }
  57. }
  58. }
  59. //----------------------------------------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------------------------------------
  61. __fastcall TThemePageControl::TThemePageControl(TComponent * Owner) :
  62. TPageControl(Owner)
  63. {
  64. FOldTabIndex = -1;
  65. }
  66. //----------------------------------------------------------------------------------------------------------
  67. int __fastcall TThemePageControl::GetTabsHeight()
  68. {
  69. // Calculated height includes tab/contents separator line on Windows 7/8,
  70. // but not on Windows XP
  71. TRect Rect = GetClientRect();
  72. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&Rect);
  73. int Result = Rect.Top - 1;
  74. // Two different ways to calculate the same, not sure which one is more reliable,
  75. // so we want to know in case they differ.
  76. if (DebugAlwaysTrue(PageCount >= 0))
  77. {
  78. TRect Rect = TabRect(0);
  79. int Result2 = Rect.Bottom + 1;
  80. // On Windows 10 with 200% scaling, the first is 40, the second is 42.
  81. // With 250% scaling its 50 vs 53.
  82. // Using the larger.
  83. if (Result2 > Result)
  84. {
  85. DebugAssert(IsWin10());
  86. Result = Result2;
  87. }
  88. }
  89. return Result;
  90. }
  91. //----------------------------------------------------------------------------------------------------------
  92. void __fastcall TThemePageControl::PaintWindow(HDC DC)
  93. {
  94. // Themes not enabled, give up
  95. if (!UseThemes())
  96. {
  97. TPageControl::PaintWindow(DC);
  98. return;
  99. }
  100. // TODO use GetClipBox
  101. TRect PageRect = GetClientRect();
  102. // 1st paint the tab body
  103. TRect ClientRect = PageRect;
  104. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&PageRect);
  105. ClientRect.Top = PageRect.Top - 2;
  106. DrawThemesXpTabItem(DC, -1, ClientRect, true, 0);
  107. // 2nd paint the inactive tabs
  108. TPoint Point = ScreenToClient(Mouse->CursorPos);
  109. int HotIndex = IndexOfTabAt(Point.X, Point.Y);
  110. int SelectedIndex = TabIndex;
  111. for (int Tab = 0; Tab < PageCount; Tab++)
  112. {
  113. if (Tab != SelectedIndex)
  114. {
  115. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(Pages[Tab]);
  116. bool Shadowed = (ThemeTabSheet != NULL) ? ThemeTabSheet->Shadowed : false;
  117. TRect Rect = TabRect(Tab);
  118. int State = (Tab == HotIndex ? TIS_HOT : (Shadowed ? TIS_DISABLED : TIS_NORMAL));
  119. DrawThemesXpTabItem(DC, Tab, Rect, false, State);
  120. }
  121. }
  122. if (SelectedIndex >= 0)
  123. {
  124. // 3rd paint the active selected tab
  125. TRect Rect = TabRect(SelectedIndex);
  126. Rect.Inflate(2, 2);
  127. Rect.Bottom--;
  128. DrawThemesXpTabItem(DC, SelectedIndex, Rect, false, TIS_SELECTED);
  129. }
  130. }
  131. //----------------------------------------------------------------------------------------------------------
  132. // This function draws Themes Tab control parts: a) Tab-Body and b) Tab-tabs
  133. void __fastcall TThemePageControl::DrawThemesXpTabItem(HDC DC, int Item,
  134. const TRect & Rect, bool Body, int State)
  135. {
  136. TSize Size = Rect.Size;
  137. // Draw background
  138. HDC DCMem = CreateCompatibleDC(DC);
  139. HBITMAP BitmapMem = CreateCompatibleBitmap(DC, Size.Width, Size.Height);
  140. HBITMAP BitmapOld = (HBITMAP)SelectObject(DCMem, BitmapMem);
  141. TRect RectMem(0, 0, Size.Width, Size.Height);
  142. if (!Body && (State == TIS_SELECTED))
  143. {
  144. RectMem.Bottom++;
  145. }
  146. if (Body)
  147. {
  148. DrawThemesPart(DCMem, TABP_PANE, State, IDS_UTIL_TAB, &RectMem);
  149. }
  150. else
  151. {
  152. DrawThemesPart(DCMem, TABP_TABITEM, State, IDS_UTIL_TAB, &RectMem);
  153. }
  154. // Init some extra parameters
  155. BITMAPINFO BitmapInfo;
  156. // Fill local pixel arrays
  157. ZeroMemory(&BitmapInfo, sizeof(BITMAPINFO));
  158. BITMAPINFOHEADER & BitmapInfoHeader = BitmapInfo.bmiHeader;
  159. BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
  160. BitmapInfoHeader.biCompression = BI_RGB;
  161. BitmapInfoHeader.biPlanes = 1;
  162. // force as RGB: 3 bytes,24 bits -> good for rotating bitmap in any resolution
  163. BitmapInfoHeader.biBitCount = 24;
  164. BitmapInfoHeader.biWidth = Size.Width;
  165. BitmapInfoHeader.biHeight = Size.Height;
  166. if (!Body && (Item >= 0))
  167. {
  168. if ((State == TIS_SELECTED))
  169. {
  170. RectMem.Bottom--;
  171. }
  172. DrawTabItem(DCMem, Item, RectMem, (State == TIS_SELECTED), (State == TIS_DISABLED));
  173. }
  174. // Blit image to the screen
  175. BitBlt(DC, Rect.Left, Rect.Top, Size.Width, Size.Height, DCMem, 0, 0, SRCCOPY);
  176. SelectObject(DCMem, BitmapOld);
  177. DeleteObject(BitmapMem);
  178. DeleteDC(DCMem);
  179. }
  180. //----------------------------------------------------------------------------------------------------------
  181. // draw tab item context: possible icon and text
  182. void __fastcall TThemePageControl::DrawTabItem(HDC DC, int Item, TRect Rect,
  183. bool Selected, bool Shadowed)
  184. {
  185. if (Selected)
  186. {
  187. Rect.Bottom -= 1;
  188. }
  189. else
  190. {
  191. Rect.Bottom += 2;
  192. }
  193. Rect.Left += 6;
  194. Rect.Top += 2 + (Selected ? 1 : 3);
  195. UnicodeString Text = Pages[Item]->Caption;
  196. if ((Images != NULL) && (Pages[Item]->ImageIndex >= 0))
  197. {
  198. int Left;
  199. if (!Text.IsEmpty())
  200. {
  201. Left = Rect.Left + (Selected ? 2 : 0);
  202. }
  203. else
  204. {
  205. Left = (Rect.Right - Images->Width - Rect.Left) / 2;
  206. }
  207. int Y = ((Rect.Top + Rect.Bottom - Images->Height) / 2) - 1 + (Selected ? 0 : -2);
  208. std::unique_ptr<TCanvas> Canvas(new TCanvas());
  209. Canvas->Handle = DC;
  210. Images->Draw(Canvas.get(), Left, Y, Pages[Item]->ImageIndex, !Shadowed);
  211. Rect.Left += Images->Width + 3;
  212. }
  213. else
  214. {
  215. Rect.Left -= 2;
  216. }
  217. int OldMode = SetBkMode(DC, TRANSPARENT);
  218. if (!Text.IsEmpty())
  219. {
  220. HFONT OldFont = (HFONT)SelectObject(DC, Font->Handle);
  221. Rect.Right -= 3;
  222. wchar_t * Buf = new wchar_t[Text.Length() + 1 + 4];
  223. wcscpy(Buf, Text.c_str());
  224. TRect TextRect(0, 0, Rect.Right - Rect.Left, 20);
  225. ::DrawText(DC, Buf, -1, &TextRect, DT_CALCRECT | DT_SINGLELINE | DT_MODIFYSTRING | DT_END_ELLIPSIS);
  226. OffsetRect(&Rect, 0, (Selected ? 0 : -2));
  227. DrawText(DC, Buf, -1, &Rect, DT_NOPREFIX | DT_CENTER);
  228. delete[] Buf;
  229. SelectObject(DC, OldFont);
  230. }
  231. SetBkMode(DC, OldMode);
  232. }
  233. //----------------------------------------------------------------------------------------------------------
  234. void __fastcall TThemePageControl::DrawThemesPart(HDC DC, int PartId,
  235. int StateId, LPCWSTR PartNameID, LPRECT Rect)
  236. {
  237. HTHEME Theme = OpenThemeData(NULL, PartNameID);
  238. if (Theme != 0)
  239. {
  240. DrawThemeBackground(Theme, DC, PartId, StateId, Rect, NULL);
  241. CloseThemeData(Theme);
  242. }
  243. }
  244. //==========================================================================================================
  245. // these two messages are necessary only to properly redraw deselected tab background, because
  246. bool __fastcall TThemePageControl::CanChange()
  247. {
  248. FOldTabIndex = ActivePageIndex;
  249. return TPageControl::CanChange();
  250. }
  251. //----------------------------------------------------------------------------------------------------------
  252. void __fastcall TThemePageControl::InvalidateTab(int Index)
  253. {
  254. if (HandleAllocated())
  255. {
  256. TRect Rect = TabRect(Index);
  257. if (Index == TabIndex)
  258. {
  259. Rect.Inflate(2, 2);
  260. }
  261. // Original code was invalidating range against parent window
  262. // (recalculating coordinates first)
  263. InvalidateRect(Handle, &Rect, true);
  264. }
  265. }
  266. //----------------------------------------------------------------------------------------------------------
  267. void __fastcall TThemePageControl::Change()
  268. {
  269. // note that TabIndex yields correct value already here,
  270. // while ActivePageIndex is not updated yet
  271. if ((FOldTabIndex >= 0) && (FOldTabIndex != TabIndex) && UseThemes())
  272. {
  273. InvalidateTab(FOldTabIndex);
  274. }
  275. TPageControl::Change();
  276. }
  277. //----------------------------------------------------------------------------------------------------------
  278. #ifdef _DEBUG
  279. void __fastcall TThemePageControl::RequestAlign()
  280. {
  281. TPageControl::RequestAlign();
  282. }
  283. #endif
  284. //----------------------------------------------------------------------------------------------------------