ThemePageControl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. // http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=6355
  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 (ALWAYS_TRUE(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. // On Windows 10 with 200% scaling, the first is 40, the second is 42.
  77. // The correct size is probably 41. Will wait for final release before settling on solution.
  78. if (ALWAYS_TRUE(PageCount >= 0))
  79. {
  80. TRect Rect = TabRect(0);
  81. int Result2 = Rect.Bottom + 1;
  82. if (ALWAYS_FALSE(Result != Result2))
  83. {
  84. Result = Result2;
  85. }
  86. }
  87. return Result;
  88. }
  89. //----------------------------------------------------------------------------------------------------------
  90. void __fastcall TThemePageControl::PaintWindow(HDC DC)
  91. {
  92. // Themes not enabled, give up
  93. if (!UseThemes())
  94. {
  95. TPageControl::PaintWindow(DC);
  96. return;
  97. }
  98. // TODO use GetClipBox
  99. TRect PageRect = GetClientRect();
  100. // 1st paint the tab body
  101. TRect ClientRect = PageRect;
  102. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&PageRect);
  103. ClientRect.Top = PageRect.Top - 2;
  104. DrawThemesXpTabItem(DC, -1, ClientRect, true, 0);
  105. // 2nd paint the inactive tabs
  106. TPoint Point = ScreenToClient(Mouse->CursorPos);
  107. int HotIndex = IndexOfTabAt(Point.X, Point.Y);
  108. int SelectedIndex = TabIndex;
  109. for (int Tab = 0; Tab < PageCount; Tab++)
  110. {
  111. if (Tab != SelectedIndex)
  112. {
  113. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(Pages[Tab]);
  114. bool Shadowed = (ThemeTabSheet != NULL) ? ThemeTabSheet->Shadowed : false;
  115. TRect Rect = TabRect(Tab);
  116. int State = (Tab == HotIndex ? TIS_HOT : (Shadowed ? TIS_DISABLED : TIS_NORMAL));
  117. DrawThemesXpTabItem(DC, Tab, Rect, false, State);
  118. }
  119. }
  120. if (SelectedIndex >= 0)
  121. {
  122. // 3rd paint the active selected tab
  123. TRect Rect = TabRect(SelectedIndex);
  124. Rect.Inflate(2, 2);
  125. Rect.Bottom--;
  126. DrawThemesXpTabItem(DC, SelectedIndex, Rect, false, TIS_SELECTED);
  127. }
  128. }
  129. //----------------------------------------------------------------------------------------------------------
  130. // This function draws Themes Tab control parts: a) Tab-Body and b) Tab-tabs
  131. void __fastcall TThemePageControl::DrawThemesXpTabItem(HDC DC, int Item,
  132. const TRect & Rect, bool Body, int State)
  133. {
  134. TSize Size = Rect.Size;
  135. // Draw background
  136. HDC DCMem = CreateCompatibleDC(DC);
  137. HBITMAP BitmapMem = CreateCompatibleBitmap(DC, Size.Width, Size.Height);
  138. HBITMAP BitmapOld = (HBITMAP)SelectObject(DCMem, BitmapMem);
  139. TRect RectMem(0, 0, Size.Width, Size.Height);
  140. if (!Body && (State == TIS_SELECTED))
  141. {
  142. RectMem.Bottom++;
  143. }
  144. if (Body)
  145. {
  146. DrawThemesPart(DCMem, TABP_PANE, State, IDS_UTIL_TAB, &RectMem);
  147. }
  148. else
  149. {
  150. DrawThemesPart(DCMem, TABP_TABITEM, State, IDS_UTIL_TAB, &RectMem);
  151. }
  152. // Init some extra parameters
  153. BITMAPINFO BitmapInfo;
  154. // Fill local pixel arrays
  155. ZeroMemory(&BitmapInfo, sizeof(BITMAPINFO));
  156. BITMAPINFOHEADER & BitmapInfoHeader = BitmapInfo.bmiHeader;
  157. BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
  158. BitmapInfoHeader.biCompression = BI_RGB;
  159. BitmapInfoHeader.biPlanes = 1;
  160. // force as RGB: 3 bytes,24 bits -> good for rotating bitmap in any resolution
  161. BitmapInfoHeader.biBitCount = 24;
  162. BitmapInfoHeader.biWidth = Size.Width;
  163. BitmapInfoHeader.biHeight = Size.Height;
  164. if (!Body && (Item >= 0))
  165. {
  166. if ((State == TIS_SELECTED))
  167. {
  168. RectMem.Bottom--;
  169. }
  170. DrawTabItem(DCMem, Item, RectMem, (State == TIS_SELECTED), (State == TIS_DISABLED));
  171. }
  172. // Blit image to the screen
  173. BitBlt(DC, Rect.Left, Rect.Top, Size.Width, Size.Height, DCMem, 0, 0, SRCCOPY);
  174. SelectObject(DCMem, BitmapOld);
  175. DeleteObject(BitmapMem);
  176. DeleteDC(DCMem);
  177. }
  178. //----------------------------------------------------------------------------------------------------------
  179. // draw tab item context: possible icon and text
  180. void __fastcall TThemePageControl::DrawTabItem(HDC DC, int Item, TRect Rect,
  181. bool Selected, bool Shadowed)
  182. {
  183. if (Selected)
  184. {
  185. Rect.Bottom -= 1;
  186. }
  187. else
  188. {
  189. Rect.Bottom += 2;
  190. }
  191. Rect.Left += 6;
  192. Rect.Top += 2 + (Selected ? 1 : 3);
  193. UnicodeString Text = Pages[Item]->Caption;
  194. if ((Images != NULL) && (Pages[Item]->ImageIndex >= 0))
  195. {
  196. int Left;
  197. if (!Text.IsEmpty())
  198. {
  199. Left = Rect.Left + (Selected ? 2 : 0);
  200. }
  201. else
  202. {
  203. Left = (Rect.Right - Images->Width - Rect.Left) / 2;
  204. }
  205. int Y = ((Rect.Top + Rect.Bottom - Images->Height) / 2) - 1 + (Selected ? 0 : -2);
  206. std::unique_ptr<TCanvas> Canvas(new TCanvas());
  207. Canvas->Handle = DC;
  208. Images->Draw(Canvas.get(), Left, Y, Pages[Item]->ImageIndex, !Shadowed);
  209. Rect.Left += Images->Width + 3;
  210. }
  211. else
  212. {
  213. Rect.Left -= 2;
  214. }
  215. int OldMode = SetBkMode(DC, TRANSPARENT);
  216. if (!Text.IsEmpty())
  217. {
  218. HFONT OldFont = (HFONT)SelectObject(DC, Font->Handle);
  219. Rect.Right -= 3;
  220. wchar_t * Buf = new wchar_t[Text.Length() + 1 + 4];
  221. wcscpy(Buf, Text.c_str());
  222. TRect TextRect(0, 0, Rect.Right - Rect.Left, 20);
  223. ::DrawText(DC, Buf, -1, &TextRect, DT_CALCRECT | DT_SINGLELINE | DT_MODIFYSTRING | DT_END_ELLIPSIS);
  224. OffsetRect(&Rect, 0, (Selected ? 0 : -2));
  225. DrawText(DC, Buf, -1, &Rect, DT_NOPREFIX | DT_CENTER);
  226. delete[] Buf;
  227. SelectObject(DC, OldFont);
  228. }
  229. SetBkMode(DC, OldMode);
  230. }
  231. //----------------------------------------------------------------------------------------------------------
  232. void __fastcall TThemePageControl::DrawThemesPart(HDC DC, int PartId,
  233. int StateId, LPCWSTR PartNameID, LPRECT Rect)
  234. {
  235. HTHEME Theme = OpenThemeData(NULL, PartNameID);
  236. if (Theme != 0)
  237. {
  238. DrawThemeBackground(Theme, DC, PartId, StateId, Rect, NULL);
  239. CloseThemeData(Theme);
  240. }
  241. }
  242. //==========================================================================================================
  243. // these two messages are necessary only to properly redraw deselected tab background, because
  244. bool __fastcall TThemePageControl::CanChange()
  245. {
  246. FOldTabIndex = ActivePageIndex;
  247. return TPageControl::CanChange();
  248. }
  249. //----------------------------------------------------------------------------------------------------------
  250. void __fastcall TThemePageControl::InvalidateTab(int Index)
  251. {
  252. if (HandleAllocated())
  253. {
  254. TRect Rect = TabRect(Index);
  255. if (Index == TabIndex)
  256. {
  257. Rect.Inflate(2, 2);
  258. }
  259. // Original code was invalidating range against parent window
  260. // (recalculating coordinates first)
  261. InvalidateRect(Handle, &Rect, true);
  262. }
  263. }
  264. //----------------------------------------------------------------------------------------------------------
  265. void __fastcall TThemePageControl::Change()
  266. {
  267. // note that TabIndex yields correct value already here,
  268. // while ActivePageIndex is not updated yet
  269. if ((FOldTabIndex >= 0) && (FOldTabIndex != TabIndex) && UseThemes())
  270. {
  271. InvalidateTab(FOldTabIndex);
  272. }
  273. TPageControl::Change();
  274. }
  275. //----------------------------------------------------------------------------------------------------------
  276. #ifdef _DEBUG
  277. void __fastcall TThemePageControl::RequestAlign()
  278. {
  279. TPageControl::RequestAlign();
  280. }
  281. #endif
  282. //----------------------------------------------------------------------------------------------------------