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.

443 lines
14 KiB

  1. /*
  2. * Value
  3. */
  4. #ifndef DUI_CORE_VALUE_H_INCLUDED
  5. #define DUI_CORE_VALUE_H_INCLUDED
  6. #pragma once
  7. namespace DirectUI
  8. {
  9. ////////////////////////////////////////////////////////
  10. // Value
  11. /*
  12. * Value Multithreading
  13. *
  14. * Values are immutable and are a process-wide resource. A value created in one thread can be
  15. * used in any thread. Access to them are synchronized (thread-safe) and they do not have
  16. * thread-affinity.
  17. *
  18. * TODO: Impl thread-safety for Values
  19. */
  20. // Forward declarations
  21. class Element;
  22. class Layout;
  23. class PropertySheet;
  24. class Expression;
  25. typedef DynamicArray<Element*> ElementList;
  26. // Value will maintain the lifetime of ElementLists, Layouts, PropertySheets, and Expressions after
  27. // a new Value is created with a pointer to the object (these objects are created externally). When
  28. // the reference count goes to zero, these objects will be deleted.
  29. #define DUIV_UNAVAILABLE -2
  30. #define DUIV_UNSET -1
  31. #define DUIV_NULL 0
  32. #define DUIV_INT 1
  33. #define DUIV_BOOL 2
  34. #define DUIV_ELEMENTREF 3
  35. #define DUIV_ELLIST 4 // List deleted on Value destruction and made immutable on create (Value only object holding external created ElementList)
  36. #define DUIV_STRING 5 // String duplicated on creation and freed on destruction (Value creates new internal instance)
  37. #define DUIV_POINT 6
  38. #define DUIV_SIZE 7
  39. #define DUIV_RECT 8
  40. #define DUIV_FILL 9
  41. #define DUIV_LAYOUT 10 // Layout object destroyed on Value destruction (Value only object holding external created Layout)
  42. #define DUIV_GRAPHIC 11 // Bitmap handle freed on Value destruction (Value creates new internal instance)
  43. #define DUIV_SHEET 12 // PropertySheet object destroyed on Value destruction and made immutable on create (Value only object holding external created PropertySheet)
  44. #define DUIV_EXPR 13 // Expression object destroyed on Value destruction (Value only object holding external created Expression)
  45. #define DUIV_ATOM 14
  46. #define DUIV_CURSOR 15 // Will not destroy cursor handle upon value destruction
  47. // Value structures and macros
  48. #define FILLTYPE_HGradient ((BYTE)0)
  49. #define FILLTYPE_VGradient ((BYTE)1)
  50. #define FILLTYPE_Solid ((BYTE)2)
  51. #define FILLTYPE_TriHGradient ((BYTE)3)
  52. #define FILLTYPE_TriVGradient ((BYTE)4)
  53. #define FILLTYPE_DrawFrameControl ((BYTE)5) // DrawFrameControl fill
  54. #define FILLTYPE_DrawThemeBackground ((BYTE)6) // DrawThemeBackground fill
  55. struct Fill // Solid colors and fills
  56. {
  57. BYTE dType;
  58. union
  59. {
  60. struct
  61. {
  62. COLORREF cr;
  63. COLORREF cr2;
  64. COLORREF cr3;
  65. } ref;
  66. struct
  67. {
  68. UINT uType;
  69. UINT uState;
  70. } fillDFC;
  71. struct
  72. {
  73. HTHEME hTheme;
  74. int iPartId;
  75. int iStateId;
  76. } fillDTB;
  77. };
  78. };
  79. // Graphic
  80. // Graphic objects may either have an Alpha channel applied to the entire bitmap,
  81. // may have full alpha transparency of a particular color in the bitmap (with the
  82. // option of an auto-color pick (upper left corner)), or neither
  83. #define GRAPHICTYPE_Bitmap ((BYTE)0)
  84. #define GRAPHICTYPE_Icon ((BYTE)1)
  85. #define GRAPHICTYPE_EnhMetaFile ((BYTE)2)
  86. #ifdef GADGET_ENABLE_GDIPLUS
  87. #define GRAPHICTYPE_GpBitmap ((BYTE)3)
  88. #endif
  89. // Valid modes for Bitmaps (Alpha or RGB used depending on mode), meaning based on context of use
  90. #define GRAPHIC_NoBlend ((BYTE)0)
  91. #define GRAPHIC_AlphaConst ((BYTE)1)
  92. #define GRAPHIC_AlphaConstPerPix ((BYTE)2)
  93. #define GRAPHIC_TransColor ((BYTE)3)
  94. #define GRAPHIC_Stretch ((BYTE)4)
  95. #define GRAPHIC_NineGrid ((BYTE)5)
  96. #define GRAPHIC_NineGridTransColor ((BYTE)6)
  97. #define GRAPHIC_NineGridAlphaConstPerPix ((BYTE)7)
  98. struct Graphic
  99. {
  100. HANDLE hImage; // Will hold hBitmap, hIcon, hEnhMetaFile or Gdiplus::Bitmap
  101. HANDLE hAltImage;
  102. USHORT cx;
  103. USHORT cy;
  104. struct
  105. {
  106. BYTE dImgType : 2;
  107. BYTE dMode : 3;
  108. bool bFlip : 1;
  109. bool bRTLGraphic: 1;
  110. bool bFreehImage: 1;
  111. union
  112. {
  113. BYTE dAlpha;
  114. struct
  115. {
  116. BYTE r: 8;
  117. BYTE g: 8;
  118. BYTE b: 8;
  119. } rgbTrans;
  120. };
  121. } BlendMode;
  122. };
  123. struct Cursor
  124. {
  125. HCURSOR hCursor;
  126. };
  127. // Compile-time static version of the Value class
  128. struct _StaticValue
  129. {
  130. BYTE _fReserved0;
  131. BYTE _fReserved1;
  132. short _dType;
  133. int _cRef;
  134. int _val0;
  135. int _val1;
  136. int _val2;
  137. int _val3;
  138. };
  139. struct _StaticValueColor
  140. {
  141. BYTE _fReserved0;
  142. BYTE _fReserved1;
  143. short _dType;
  144. int _cRef;
  145. BYTE dType;
  146. COLORREF cr;
  147. COLORREF crSec;
  148. USHORT x;
  149. USHORT y;
  150. };
  151. struct _StaticValuePtr
  152. {
  153. BYTE _fReserved0;
  154. BYTE _fReserved1;
  155. short _dType;
  156. int _cRef;
  157. void* _ptr;
  158. };
  159. // Value class (24-bytes)
  160. class Value
  161. {
  162. private:
  163. BYTE _fReserved0; // Reserved for small block allocator
  164. BYTE _fReserved1; // Data alignment padding
  165. short _dType;
  166. int _cRef;
  167. union
  168. {
  169. int _intVal;
  170. bool _boolVal;
  171. Element* _peVal;
  172. ElementList* _peListVal;
  173. LPWSTR _pszVal;
  174. POINT _ptVal;
  175. SIZE _sizeVal;
  176. RECT _rectVal;
  177. Fill _fillVal;
  178. Layout* _plVal;
  179. Graphic _graphicVal;
  180. PropertySheet* _ppsVal;
  181. Expression* _pexVal;
  182. ATOM _atomVal;
  183. Cursor _cursorVal;
  184. };
  185. void _ZeroRelease();
  186. public:
  187. #if DBG
  188. bool IsZeroRef() { return !_cRef; }
  189. #endif
  190. // Value creation methods
  191. static Value* CreateInt(int dValue);
  192. static Value* CreateBool(bool bValue);
  193. static Value* CreateElementRef(Element* peValue);
  194. static Value* CreateElementList(ElementList* peListValue);
  195. static Value* CreateString(LPCWSTR pszValue, HINSTANCE hResLoad = NULL);
  196. static Value* CreatePoint(int x, int y);
  197. static Value* CreateSize(int cx, int cy);
  198. static Value* CreateRect(int left, int top, int right, int bottom);
  199. static Value* CreateColor(COLORREF cr);
  200. static Value* CreateColor(COLORREF cr0, COLORREF cr1, BYTE dType = FILLTYPE_HGradient);
  201. static Value* CreateColor(COLORREF cr0, COLORREF cr1, COLORREF cr2, BYTE dType = FILLTYPE_TriHGradient);
  202. static Value* CreateFill(const Fill & clrSrc);
  203. static Value* CreateDFCFill(UINT uType, UINT uState);
  204. static Value* CreateDTBFill(HTHEME hTheme, int iPartId, int iStateId);
  205. static Value* CreateLayout(Layout* plValue);
  206. static Value* CreateGraphic(HBITMAP hBitmap, BYTE dBlendMode = GRAPHIC_TransColor, UINT dBlendValue = (UINT)-1, bool bFlip = false, bool bRTL = false);
  207. #ifdef GADGET_ENABLE_GDIPLUS
  208. static Value* CreateGraphic(Gdiplus::Bitmap * pgpbmp, BYTE dBlendMode = GRAPHIC_TransColor, UINT dBlendValue = (UINT)-1, bool bFlip = false, bool bRTL = false);
  209. #endif
  210. static Value* CreateGraphic(HICON hIcon, bool bFlip = false, bool bRTL = false);
  211. static Value* CreateGraphic(LPCWSTR pszBMP, BYTE dBlendMode = GRAPHIC_TransColor, UINT dBlendValue = (UINT)-1, USHORT cx = 0, USHORT cy = 0, HINSTANCE hResLoad = NULL,
  212. bool bFlip = false, bool bRTL = false);
  213. static Value* CreateGraphic(LPCWSTR pszICO, USHORT cxDesired, USHORT cyDesired, HINSTANCE hResLoad = NULL, bool bFlip = false, bool bRTL = false);
  214. static Value* CreateGraphic(HENHMETAFILE hEnhMetaFile, HENHMETAFILE hAltEnhMetaFile = NULL);
  215. static Value* CreatePropertySheet(PropertySheet* ppsValue);
  216. static Value* CreateExpression(Expression* pexValue);
  217. static Value* CreateAtom(LPCWSTR pszValue);
  218. static Value* CreateCursor(LPCWSTR pszValue);
  219. static Value* CreateCursor(HCURSOR hValue);
  220. // Reference count methods
  221. void AddRef() { if (_cRef != -1) _cRef++; } // -1 is static value
  222. void Release() { if (_cRef != -1 && !--_cRef) _ZeroRelease(); } // -1 is static value
  223. int GetRefCount() { return _cRef; }
  224. // Accessors
  225. int GetType();
  226. LPVOID GetImage(bool bGetRTL);
  227. int GetInt();
  228. bool GetBool();
  229. Element* GetElement();
  230. ElementList* GetElementList(); // Invalid if released (object referred to destroyed)
  231. const LPWSTR GetString(); // Invalid if released (object referred to destroyed)
  232. const POINT* GetPoint(); // Invalid if released
  233. const SIZE* GetSize(); // Invalid if released
  234. const RECT* GetRect(); // Invalid if released
  235. const Fill* GetFill(); // Invalid if released
  236. Layout* GetLayout(); // Invalid if released (object referred to destroyed)
  237. Graphic* GetGraphic(); // Invalid if released (object indirectly referred to destroyed)
  238. PropertySheet* GetPropertySheet(); // Invalid if released (object referred to destroyed)
  239. Expression* GetExpression(); // Invalid if released (object referred to destroyed)
  240. ATOM GetAtom(); // Invalid if released
  241. Cursor* GetCursor(); // Invalid if released
  242. // Equality
  243. bool IsEqual(Value* pv);
  244. // Conversion
  245. LPWSTR ToString(LPWSTR psz, UINT c);
  246. // Common values
  247. static Value* pvUnavailable;
  248. static Value* pvNull;
  249. static Value* pvUnset;
  250. static Value* pvElementNull;
  251. static Value* pvElListNull;
  252. static Value* pvBoolTrue;
  253. static Value* pvBoolFalse;
  254. static Value* pvStringNull;
  255. static Value* pvPointZero;
  256. static Value* pvSizeZero;
  257. static Value* pvRectZero;
  258. static Value* pvIntZero;
  259. static Value* pvLayoutNull;
  260. static Value* pvGraphicNull;
  261. static Value* pvSheetNull;
  262. static Value* pvExprNull;
  263. static Value* pvAtomZero;
  264. static Value* pvCursorNull;
  265. static Value* pvColorTrans;
  266. };
  267. //LPVOID GetImage(Graphic *pg, bool bGetRTL);
  268. #define GethBitmap(pv, bGetRTL) ((HBITMAP)pv->GetImage(bGetRTL))
  269. #define GethIcon(pv, bGetRTL) ((HICON)pv->GetImage(bGetRTL))
  270. #define GethEnhMetaFile(pv, bGetRTL) ((HENHMETAFILE)pv->GetImage(bGetRTL))
  271. #define GetGpBitmap(pv, bGetRTL) ((Gdiplus::Bitmap *)pv->GetImage(bGetRTL))
  272. #define StaticValue(name, type, val0) static _StaticValue name = { 0, 0, type, -1, val0, 0, 0, 0 }
  273. #define StaticValue2(name, type, val0, val1) static _StaticValue name = { 0, 0, type, -1, val0, val1, 0, 0 }
  274. #define StaticValue4(name, type, val0, val1, val2, val3) static _StaticValue name = { 0, 0, type, -1, val0, val1, val2, val3 }
  275. #define StaticValueColorSolid(name, cr) static _StaticValueColor name = { 0, 0, DUIV_FILL, -1, FILLTYPE_Solid, cr, 0, 0, 0 }
  276. #define StaticValuePtr(name, type, ptr) static _StaticValuePtr name = { 0, 0, type, -1, ptr }
  277. // Accessors
  278. inline int
  279. Value::GetType()
  280. {
  281. return _dType;
  282. }
  283. inline int
  284. Value::GetInt() // Copy passed out
  285. {
  286. DUIAssert(_dType == DUIV_INT, "Invalid value type");
  287. return _intVal;
  288. }
  289. inline bool
  290. Value::GetBool() // Copy passed out
  291. {
  292. DUIAssert(_dType == DUIV_BOOL, "Invalid value type");
  293. return _boolVal;
  294. }
  295. inline Element *
  296. Value::GetElement() // Copy passed out
  297. {
  298. DUIAssert(_dType == DUIV_ELEMENTREF, "Invalid value type");
  299. return _peVal;
  300. }
  301. inline ElementList *
  302. Value::GetElementList() // Copy passed out, invalid (destroyed) if value released
  303. {
  304. DUIAssert(_dType == DUIV_ELLIST, "Invalid value type");
  305. return _peListVal;
  306. }
  307. inline const LPWSTR
  308. Value::GetString() // Copy passed out, invalid (destroyed) if value released
  309. {
  310. DUIAssert(_dType == DUIV_STRING, "Invalid value type");
  311. return _pszVal;
  312. }
  313. inline const POINT *
  314. Value::GetPoint() // Pointer to internal structure, invalid if value released
  315. {
  316. DUIAssert(_dType == DUIV_POINT, "Invalid value type");
  317. return &_ptVal;
  318. }
  319. inline const SIZE *
  320. Value::GetSize() // Pointer to internal structure, invalid if value released
  321. {
  322. DUIAssert(_dType == DUIV_SIZE, "Invalid value type");
  323. return &_sizeVal;
  324. }
  325. inline const RECT *
  326. Value::GetRect() // Pointer to internal structure, invalid if value released
  327. {
  328. DUIAssert(_dType == DUIV_RECT, "Invalid value type");
  329. return &_rectVal;
  330. }
  331. inline const Fill *
  332. Value::GetFill() // Pointer to internal structure, invalid if value released
  333. {
  334. DUIAssert(_dType == DUIV_FILL, "Invalid value type");
  335. return &_fillVal;
  336. }
  337. inline Layout *
  338. Value::GetLayout() // Copy passed out, invalid (destroyed) if value released
  339. {
  340. DUIAssert(_dType == DUIV_LAYOUT, "Invalid value type");
  341. return _plVal;
  342. }
  343. inline Graphic *
  344. Value::GetGraphic() // Pointer to internal structure, invalid if value released
  345. {
  346. DUIAssert(_dType == DUIV_GRAPHIC, "Invalid value type");
  347. return &_graphicVal;
  348. }
  349. inline PropertySheet * Value::GetPropertySheet() // Copy passed out, invalid (destroyed) if value released
  350. {
  351. DUIAssert(_dType == DUIV_SHEET, "Invalid value type");
  352. return _ppsVal;
  353. }
  354. inline Expression *
  355. Value::GetExpression() // Copy passed out, invalid (destroyed) if value released
  356. {
  357. DUIAssert(_dType == DUIV_EXPR, "Invalid value type");
  358. return _pexVal;
  359. }
  360. inline ATOM
  361. Value::GetAtom() // Copy passed out
  362. {
  363. DUIAssert(_dType == DUIV_ATOM, "Invalid value type");
  364. return _atomVal;
  365. }
  366. inline Cursor *
  367. Value::GetCursor() // Pointer to internal structure, invalid if value released
  368. {
  369. DUIAssert(_dType == DUIV_CURSOR, "Invalid value type");
  370. return &_cursorVal;
  371. }
  372. } // namespace DirectUI
  373. #endif // DUI_CORE_VALUE_H_INCLUDED