Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
13 KiB

  1. //---------------------------------------------------------------------------
  2. // Loader.h - loads the theme data into shared memory
  3. //---------------------------------------------------------------------------
  4. #pragma once
  5. //---------------------------------------------------------------------------
  6. #include "Parser.h"
  7. #include "TmSchema.h"
  8. #include "ThemeFile.h"
  9. //---------------------------------------------------------------------------
  10. #define THEMEDATA_VERSION 0x00010006
  11. //---------------------------------------------------------------------------
  12. #define TM_FONTCOUNT (TMT_LASTFONT - TMT_FIRSTFONT + 1)
  13. #define TM_SIZECOUNT (TMT_LASTSIZE - TMT_FIRSTSIZE + 1)
  14. #define TM_BOOLCOUNT (TMT_LASTBOOL - TMT_FIRSTBOOL + 1)
  15. #define TM_STRINGCOUNT (TMT_LASTSTRING - TMT_FIRSTSTRING + 1)
  16. #define TM_INTCOUNT (TMT_LASTINT - TMT_FIRSTINT + 1)
  17. //---------------------------------------------------------------------------
  18. class CRenderObj; // forward
  19. class CImageFile; // forward
  20. struct DIBINFO; // forward
  21. //---------------------------------------------------------------------------
  22. struct THEMEMETRICS
  23. {
  24. //---- subset of system metrics ----
  25. LOGFONT lfFonts[TM_FONTCOUNT];
  26. COLORREF crColors[TM_COLORCOUNT];
  27. int iSizes[TM_SIZECOUNT];
  28. BOOL fBools[TM_BOOLCOUNT];
  29. //---- special theme metrics ----
  30. int iStringOffsets[TM_STRINGCOUNT];
  31. int iInts[TM_INTCOUNT];
  32. };
  33. //---------------------------------------------------------------------------
  34. struct LOADTHEMEMETRICS : THEMEMETRICS
  35. {
  36. CWideString wsStrings[TM_STRINGCOUNT];
  37. };
  38. //---------------------------------------------------------------------------
  39. // Signatures for quick cache file validation
  40. const CHAR kszBeginCacheFileSignature[] = "BEGINTHM";
  41. const CHAR kszEndCacheFileSignature[] = "ENDTHEME";
  42. const UINT kcbBeginSignature = sizeof kszBeginCacheFileSignature - 1;
  43. const UINT kcbEndSignature = sizeof kszEndCacheFileSignature - 1;
  44. //---------------------------------------------------------------------------
  45. // Theme section flags
  46. #define SECTION_READY 1
  47. #define SECTION_GLOBAL 2
  48. #define SECTION_HASSTOCKOBJECTS 4
  49. //---------------------------------------------------------------------------
  50. struct THEMEHDR
  51. {
  52. //---- theme validity ----
  53. CHAR szSignature[kcbBeginSignature]; // "BEGINTHM"
  54. DWORD dwVersion; // THEMEDATA_VERSION
  55. DWORD dwFlags; // must have SECTION_READY to be usable
  56. DWORD dwCheckSum; // byte-additive total of all bytes following THEMEHDR
  57. FILETIME ftModifTimeStamp; // Last modification time of the .msstyles file
  58. DWORD dwTotalLength; // total number of bytes of all data (incl. header & begin/end sigs)
  59. //---- theme id ----
  60. int iDllNameOffset; // dll filename of this theme
  61. int iColorParamOffset; // color param theme was loaded with
  62. int iSizeParamOffset; // size param theme was loaded with
  63. DWORD dwLangID; // User lang ID theme was loaded with
  64. int iLoadId; // sequential number for each loaded file (workstation local)
  65. //---- main sections ----
  66. DWORD iStringsOffset; // offset to strings
  67. DWORD iStringsLength; // total bytes in string section
  68. DWORD iSectionIndexOffset; // offset to Section Index
  69. DWORD iSectionIndexLength; // length of section indexes
  70. DWORD iGlobalsOffset; // offset to [globals] section (for globals parts)
  71. DWORD iGlobalsTextObjOffset; // offset to text obj for [globals] section
  72. DWORD iGlobalsDrawObjOffset; // offset to draw obj for [globals] section
  73. DWORD iSysMetricsOffset; // offset to [SysMetrics] section (for theme metrics API support)
  74. };
  75. //---------------------------------------------------------------------------
  76. struct DRAWOBJHDR // preceeds each draw obj
  77. {
  78. int iPartNum;
  79. int iStateNum;
  80. };
  81. //---------------------------------------------------------------------------
  82. struct RGNDATAHDR // preceeds each draw obj
  83. {
  84. int iPartNum;
  85. int iStateNum;
  86. int iFileIndex; // for multiple image selection (HDC scaling)
  87. };
  88. //---------------------------------------------------------------------------
  89. // Shared Theme Data layout:
  90. //
  91. // // ----- header ----
  92. // THEMEHDR ThemeHdr;
  93. //
  94. // // ----- string section ----
  95. // DWORD dwStringsLength; // length of string section
  96. // WCHAR []; // strings
  97. //
  98. // // ----- index section ----
  99. // DWORD dwIndexLengh; // length of index section
  100. // DWORD dwIndexCount; // count of APPCLASSLIVE entries
  101. // APPCLASSLIVE [];
  102. //
  103. // // ----- theme data section ----
  104. // DWORD dwDataLength; // length of theme data section
  105. // BYTE []; // actual theme data
  106. //
  107. // // ----- end signature
  108. // CHAR[8]; // ENDTHEME signature
  109. //---------------------------------------------------------------------------
  110. // A class section within the "theme data section" consists of the
  111. // following ENTRYs:
  112. //
  113. // <part jump table>
  114. //
  115. // <optional state jump table>
  116. // <property/value entries>
  117. //
  118. // for each packed drawobject:
  119. //
  120. // <TMT_RGNLIST entries> (associated with each DIB)
  121. // <TMT_DRAWOBJ entry>
  122. //
  123. // <TMT_TEXTOBJ entries>
  124. //
  125. // <end of class marker>
  126. //---------------------------------------------------------------------------
  127. // an ENTRY consists of (all 1-byte aligned):
  128. //
  129. // WORD usTypeNum; // declared type id
  130. // BYTE ePrimVal; // equiv. primitive type
  131. // BYTE bFiller; // # of bytes added after data to align it
  132. // DWORD dwDataLen; // includes filler bytes
  133. // //---- entry data follows ----
  134. //
  135. //---------------------------------------------------------------------------
  136. // The data for a part jump table ENTRY (TMT_PARTJUMPTABLE) consists of:
  137. //
  138. // <offset of first drawobj: long>
  139. // <PartCount (1 + MaxPart): BYTE>
  140. // <offset to each part's entries: long[]>
  141. //---------------------------------------------------------------------------
  142. // The data for a state jump table ENTRY (TMT_STATEJUMPTABLE) consists of:
  143. //
  144. // <StateCount (1 + MaxState): BYTE>
  145. // <offset to each state's entries: long[]>
  146. //---------------------------------------------------------------------------
  147. // The data for a rgn list ENTRY (TMT_RGNLIST) consists of:
  148. //
  149. // <StateCount (1 + MaxState): BYTE>
  150. // <offset to each state's custom rgn data: long[]>
  151. //---------------------------------------------------------------------------
  152. // The custom rgn data ENTRY (TMT_RGNDATA) consists of:
  153. //
  154. // RGNDATAHDR RgnDataHdr;
  155. // BYTE Data[];
  156. //---------------------------------------------------------------------------
  157. #define MAX_SHAREDMEM_SIZE (3000*1000) // 1.5 meg (yikes!)
  158. //---------------------------------------------------------------------------
  159. #ifdef _WIN64
  160. #define ALIGN_FACTOR 8
  161. #else
  162. #define ALIGN_FACTOR 4
  163. #endif
  164. //---------------------------------------------------------------------------
  165. #define MAX_ENTRY_NESTING 5 // max # of nested entry levels
  166. //---------------------------------------------------------------------------
  167. #define ENTRYHDR_SIZE (sizeof(SHORT) + sizeof(BYTE) + sizeof(BYTE) + sizeof(int))
  168. //---------------------------------------------------------------------------
  169. struct UNPACKED_ENTRYHDR // (hdr's in theme are PACKED)
  170. {
  171. WORD usTypeNum; // declared type id
  172. BYTE ePrimVal; // equiv. primitive type
  173. BYTE bFiller; // # of bytes added after data to align it
  174. DWORD dwDataLen; // includes filler bytes
  175. };
  176. //---------------------------------------------------------------------------
  177. inline void FillAndSkipHdr(MIXEDPTRS &u, UNPACKED_ENTRYHDR *pHdr)
  178. {
  179. pHdr->usTypeNum = *u.ps++;
  180. pHdr->ePrimVal = *u.pb++;
  181. pHdr->bFiller = *u.pb++;
  182. pHdr->dwDataLen = *u.pi++;
  183. }
  184. //---------------------------------------------------------------------------
  185. struct PART_STATE_INDEX
  186. {
  187. int iPartNum; // 0=parent part
  188. int iStateNum;
  189. int iIndex;
  190. int iLen;
  191. };
  192. //---------------------------------------------------------------------------
  193. struct APPCLASSLIVE
  194. {
  195. //---- note: cannot use ptrs since image shared by diff addr-mapping processes ----
  196. DWORD dwAppNameIndex;
  197. DWORD dwClassNameIndex;
  198. int iIndex;
  199. int iLen;
  200. };
  201. //---------------------------------------------------------------------------
  202. struct APPCLASSLOCAL
  203. {
  204. CWideString csAppName;
  205. CWideString csClassName;
  206. int iMaxPartNum;
  207. CSimpleArray<PART_STATE_INDEX> PartStateIndexes;
  208. int iPackedSize; // total size of section (incl strings) if packed
  209. APPCLASSLIVE LiveIndex; // updated during copy to live
  210. };
  211. //---------------------------------------------------------------------------
  212. HRESULT InitThemeMetrics(LOADTHEMEMETRICS *tm);
  213. void SetSystemMetrics(THEMEMETRICS *tm, BOOL fSyncLoad);
  214. HRESULT PersistSystemColors(THEMEMETRICS *tm);
  215. //---------------------------------------------------------------------------
  216. class CThemeLoader : IParserCallBack
  217. {
  218. public:
  219. CThemeLoader();
  220. ~CThemeLoader();
  221. HRESULT LoadTheme(LPCWSTR pszThemeName, LPCWSTR pszColorParam,
  222. LPCWSTR pszSizeParam, OUT HANDLE *pHandle, BOOL fGlobalTheme);
  223. HRESULT SetWindowThemeInfo(HWND hwnd, LPCWSTR pszThemeIdList);
  224. HRESULT LoadClassDataIni(HINSTANCE hInst, LPCWSTR pszColorName,
  225. LPCWSTR pszSizeName, LPWSTR pszFoundIniName, DWORD dwMaxIniNameChars, LPWSTR *ppIniData);
  226. //---- IParserCallBack ----
  227. HRESULT AddIndex(LPCWSTR pszAppName, LPCWSTR pszClassName,
  228. int iPartNum, int iStateNum, int iIndex, int iLen);
  229. HRESULT AddData(SHORT sTypeNum, PRIMVAL ePrimVal, const void *pData, DWORD dwLen);
  230. int GetNextDataIndex();
  231. protected:
  232. //---- helpers ----
  233. HRESULT PackAndLoadTheme(LPCWSTR pszThemeName, LPCWSTR pszColorParam,
  234. LPCWSTR pszSizeParam, HINSTANCE hInst);
  235. HRESULT CopyLocalThemeToLive(int iTotalLength, LPCWSTR pszThemeName,
  236. LPCWSTR pszColorParam, LPCWSTR pszSizeParam);
  237. void FreeLocalTheme();
  238. HRESULT PackMetrics();
  239. HRESULT PackThemeStructs();
  240. BOOL KeyDrawPropertyFound(int iStateDataOffset);
  241. BOOL KeyTextPropertyFound(int iStateDataOffset);
  242. HRESULT PackDrawObject(MIXEDPTRS &u, CRenderObj *pRender, int iPartId, int iStateId);
  243. HRESULT PackTextObject(MIXEDPTRS &u, CRenderObj *pRender, int iPartId, int iStateId);
  244. HRESULT PackDrawObjects(MIXEDPTRS &u, CRenderObj *pRender, int iMaxPart, BOOL fGlobals);
  245. HRESULT PackTextObjects(MIXEDPTRS &u, CRenderObj *pRender, int iMaxPart, BOOL fGlobals);
  246. HRESULT CopyPartGroup(APPCLASSLOCAL *ac, MIXEDPTRS &u, int iPartNum,
  247. int *piPartJumpTable, int iPartZeroIndex, int iGlobalsIndex, BOOL fGlobalsGroup);
  248. int GetPartOffset(CRenderObj *pRender, int iPartNum);
  249. HRESULT CopyClassGroup(APPCLASSLOCAL *ac, MIXEDPTRS &u, int iGlobalsIndex, int iClassNameOffset);
  250. int GetMaxState(APPCLASSLOCAL *ac, int iPartNum);
  251. HRESULT AddIndexInternal(LPCWSTR pszAppName, LPCWSTR pszClassName,
  252. int iPartNum, int iStateNum, int iIndex, int iLen);
  253. BOOL IndexExists(LPCWSTR pszAppName, LPCWSTR pszClassName,
  254. int iPartNum, int iStateNum);
  255. HRESULT AddMissingParent(LPCWSTR pszAppName, LPCWSTR pszClassName,
  256. int iPartNum, int iStateNum);
  257. HRESULT EmitEntryHdr(MIXEDPTRS &u, SHORT propnum, BYTE privnum);
  258. int EndEntry(MIXEDPTRS &u);
  259. HRESULT PackImageFileInfo(DIBINFO *pdi, CImageFile *pImageObj, MIXEDPTRS &u,
  260. CRenderObj *pRender, int iPartId, int iStateId);
  261. HRESULT AllocateThemeFileBytes(BYTE *upb, DWORD dwAdditionalLen);
  262. // Helper functions to alloc and emit sized data
  263. HRESULT EmitAndCopyBlock(MIXEDPTRS &u, void *pSrc, DWORD dwLen);
  264. HRESULT EmitString(MIXEDPTRS &u, LPCWSTR pSrc, DWORD dwLen, int *piOffSet);
  265. HRESULT EmitObject(MIXEDPTRS &u, SHORT propnum, BYTE privnum, void *pHdr, DWORD dwHdrLen, void *pObj, DWORD dwObjLen);
  266. //---- private data ----
  267. CWideString _wsThemeFileName;
  268. int _iGlobalsOffset;
  269. int _iSysMetricsOffset;
  270. //---- ptrs to packed objs for [globals] section ----
  271. int _iGlobalsTextObj; // we always create this obj
  272. int _iGlobalsDrawObj; // we always create this obj
  273. //---- local copy of theme data while being built ----
  274. BYTE *_pbLocalData;
  275. int _iLocalLen;
  276. CSimpleArray<APPCLASSLOCAL> _LocalIndexes;
  277. //---- used for updating entry hdrs ----
  278. BYTE *_pbEntryHdrs[MAX_ENTRY_NESTING]; // points to current hdr
  279. int _iEntryHdrLevel;
  280. //---- shared memory copy of theme data ----
  281. CUxThemeFile _LoadingThemeFile;
  282. //---- theme metrics ----
  283. LOADTHEMEMETRICS _LoadThemeMetrics;
  284. //---- Global creation flag
  285. BOOL _fGlobalTheme;
  286. //---- Machine page size for VirtualAlloc optimization
  287. DWORD _dwPageSize;
  288. };
  289. //---------------------------------------------------------------------------