ElementStyleCache.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementStyle.h"
  29. #include "ElementStyleCache.h"
  30. namespace Rocket {
  31. namespace Core {
  32. ElementStyleCache::ElementStyleCache(ElementStyle *style) : style(style),
  33. top(NULL), bottom(NULL), left(NULL), right(NULL),
  34. border_top_width(NULL), border_bottom_width(NULL), border_left_width(NULL), border_right_width(NULL),
  35. margin_top(NULL), margin_bottom(NULL), margin_left(NULL), margin_right(NULL),
  36. padding_top(NULL), padding_bottom(NULL), padding_left(NULL), padding_right(NULL),
  37. width(NULL), height(NULL),
  38. local_width(NULL), local_height(NULL), have_local_width(false), have_local_height(false),
  39. overflow_x(-1), overflow_y(-1),
  40. position(-1), float_(-1), display(-1), whitespace(-1),
  41. line_height(NULL), text_align(-1), text_transform(-1), vertical_align(NULL),
  42. pointer_events(-1)
  43. {
  44. }
  45. void ElementStyleCache::Clear()
  46. {
  47. ClearOffset();
  48. ClearBorder();
  49. ClearMargin();
  50. ClearPadding();
  51. ClearDimensions();
  52. ClearOverflow();
  53. ClearPosition();
  54. ClearFloat();
  55. ClearDisplay();
  56. ClearWhitespace();
  57. }
  58. void ElementStyleCache::ClearInherited()
  59. {
  60. ClearLineHeight();
  61. ClearTextAlign();
  62. ClearTextTransform();
  63. ClearVerticalAlign();
  64. ClearPointerEvents();
  65. }
  66. void ElementStyleCache::ClearOffset()
  67. {
  68. top = bottom = left = right = NULL;
  69. }
  70. void ElementStyleCache::ClearBorder()
  71. {
  72. border_top_width = border_bottom_width = border_left_width = border_right_width = NULL;
  73. }
  74. void ElementStyleCache::ClearMargin()
  75. {
  76. margin_top = margin_bottom = margin_left = margin_right = NULL;
  77. }
  78. void ElementStyleCache::ClearPadding()
  79. {
  80. padding_top = padding_bottom = padding_left = padding_right = NULL;
  81. }
  82. void ElementStyleCache::ClearDimensions()
  83. {
  84. width = height = NULL;
  85. have_local_width = have_local_height = false;
  86. }
  87. void ElementStyleCache::ClearOverflow()
  88. {
  89. overflow_x = overflow_y = -1;
  90. }
  91. void ElementStyleCache::ClearPosition()
  92. {
  93. position = -1;
  94. }
  95. void ElementStyleCache::ClearFloat()
  96. {
  97. float_ = -1;
  98. }
  99. void ElementStyleCache::ClearDisplay()
  100. {
  101. display = -1;
  102. }
  103. void ElementStyleCache::ClearWhitespace()
  104. {
  105. whitespace = -1;
  106. }
  107. void ElementStyleCache::ClearLineHeight()
  108. {
  109. line_height = NULL;
  110. }
  111. void ElementStyleCache::ClearTextAlign()
  112. {
  113. text_align = -1;
  114. }
  115. void ElementStyleCache::ClearTextTransform()
  116. {
  117. text_transform = -1;
  118. }
  119. void ElementStyleCache::ClearVerticalAlign()
  120. {
  121. vertical_align = NULL;
  122. }
  123. void ElementStyleCache::ClearPointerEvents()
  124. {
  125. pointer_events = -1;
  126. }
  127. void ElementStyleCache::GetOffsetProperties(const Property **o_top, const Property **o_bottom, const Property **o_left, const Property **o_right )
  128. {
  129. if (o_top)
  130. {
  131. if (!top)
  132. top = style->GetProperty(TOP);
  133. *o_top = top;
  134. }
  135. if (o_bottom)
  136. {
  137. if (!bottom)
  138. bottom = style->GetProperty(BOTTOM);
  139. *o_bottom = bottom;
  140. }
  141. if (o_left)
  142. {
  143. if (!left)
  144. left = style->GetProperty(LEFT);
  145. *o_left = left;
  146. }
  147. if (o_right)
  148. {
  149. if (!right)
  150. right = style->GetProperty(RIGHT);
  151. *o_right = right;
  152. }
  153. }
  154. void ElementStyleCache::GetBorderWidthProperties(const Property **o_border_top_width, const Property **o_border_bottom_width, const Property **o_border_left_width, const Property **o_border_right_width)
  155. {
  156. if (o_border_top_width)
  157. {
  158. if (!border_top_width)
  159. border_top_width = style->GetProperty(BORDER_TOP_WIDTH);
  160. *o_border_top_width = border_top_width;
  161. }
  162. if (o_border_bottom_width)
  163. {
  164. if (!border_bottom_width)
  165. border_bottom_width = style->GetProperty(BORDER_BOTTOM_WIDTH);
  166. *o_border_bottom_width = border_bottom_width;
  167. }
  168. if (o_border_left_width)
  169. {
  170. if (!border_left_width)
  171. border_left_width = style->GetProperty(BORDER_LEFT_WIDTH);
  172. *o_border_left_width = border_left_width;
  173. }
  174. if (o_border_right_width)
  175. {
  176. if (!border_right_width)
  177. border_right_width = style->GetProperty(BORDER_RIGHT_WIDTH);
  178. *o_border_right_width = border_right_width;
  179. }
  180. }
  181. void ElementStyleCache::GetMarginProperties(const Property **o_margin_top, const Property **o_margin_bottom, const Property **o_margin_left, const Property **o_margin_right)
  182. {
  183. if (o_margin_top)
  184. {
  185. if (!margin_top)
  186. margin_top = style->GetProperty(MARGIN_TOP);
  187. *o_margin_top = margin_top;
  188. }
  189. if (o_margin_bottom)
  190. {
  191. if (!margin_bottom)
  192. margin_bottom = style->GetProperty(MARGIN_BOTTOM);
  193. *o_margin_bottom = margin_bottom;
  194. }
  195. if (o_margin_left)
  196. {
  197. if (!margin_left)
  198. margin_left = style->GetProperty(MARGIN_LEFT);
  199. *o_margin_left = margin_left;
  200. }
  201. if (o_margin_right)
  202. {
  203. if (!margin_right)
  204. margin_right = style->GetProperty(MARGIN_RIGHT);
  205. *o_margin_right = margin_right;
  206. }
  207. }
  208. void ElementStyleCache::GetPaddingProperties(const Property **o_padding_top, const Property **o_padding_bottom, const Property **o_padding_left, const Property **o_padding_right)
  209. {
  210. if (o_padding_top)
  211. {
  212. if (!padding_top)
  213. padding_top = style->GetProperty(PADDING_TOP);
  214. *o_padding_top = padding_top;
  215. }
  216. if (o_padding_bottom)
  217. {
  218. if (!padding_bottom)
  219. padding_bottom = style->GetProperty(PADDING_BOTTOM);
  220. *o_padding_bottom = padding_bottom;
  221. }
  222. if (o_padding_left)
  223. {
  224. if (!padding_left)
  225. padding_left = style->GetProperty(PADDING_LEFT);
  226. *o_padding_left = padding_left;
  227. }
  228. if (o_padding_right)
  229. {
  230. if (!padding_right)
  231. padding_right = style->GetProperty(PADDING_RIGHT);
  232. *o_padding_right = padding_right;
  233. }
  234. }
  235. void ElementStyleCache::GetDimensionProperties(const Property **o_width, const Property **o_height)
  236. {
  237. if (o_width)
  238. {
  239. if (!width)
  240. width = style->GetProperty(WIDTH);
  241. *o_width = width;
  242. }
  243. if (o_height)
  244. {
  245. if (!height)
  246. height = style->GetProperty(HEIGHT);
  247. *o_height = height;
  248. }
  249. }
  250. void ElementStyleCache::GetLocalDimensionProperties(const Property **o_width, const Property **o_height)
  251. {
  252. if (o_width)
  253. {
  254. if (!have_local_width)
  255. {
  256. have_local_width = true;
  257. local_width = style->GetLocalProperty(WIDTH);
  258. }
  259. *o_width = local_width;
  260. }
  261. if (o_height)
  262. {
  263. if (!have_local_height)
  264. {
  265. have_local_height = true;
  266. local_height = style->GetLocalProperty(HEIGHT);
  267. }
  268. *o_height = local_height;
  269. }
  270. }
  271. void ElementStyleCache::GetOverflow(int *o_overflow_x, int *o_overflow_y)
  272. {
  273. if (o_overflow_x)
  274. {
  275. if (overflow_x < 0)
  276. overflow_x = style->GetProperty(OVERFLOW_X)->Get< int >();
  277. *o_overflow_x = overflow_x;
  278. }
  279. if (o_overflow_y)
  280. {
  281. if (overflow_y < 0)
  282. overflow_y = style->GetProperty(OVERFLOW_Y)->Get< int >();
  283. *o_overflow_y = overflow_y;
  284. }
  285. }
  286. int ElementStyleCache::GetPosition()
  287. {
  288. if (position < 0)
  289. position = style->GetProperty(POSITION)->Get< int >();
  290. return position;
  291. }
  292. int ElementStyleCache::GetFloat()
  293. {
  294. if (float_ < 0)
  295. float_ = style->GetProperty(FLOAT)->Get< int >();
  296. return float_;
  297. }
  298. int ElementStyleCache::GetDisplay()
  299. {
  300. if (display < 0)
  301. display = style->GetProperty(DISPLAY)->Get< int >();
  302. return display;
  303. }
  304. int ElementStyleCache::GetWhitespace()
  305. {
  306. if (whitespace < 0)
  307. whitespace = style->GetProperty(WHITE_SPACE)->Get< int >();
  308. return whitespace;
  309. }
  310. int ElementStyleCache::GetPointerEvents()
  311. {
  312. if (pointer_events < 0)
  313. pointer_events = style->GetProperty(POINTER_EVENTS)->Get< int >();
  314. return pointer_events;
  315. }
  316. const Property *ElementStyleCache::GetLineHeightProperty()
  317. {
  318. if (!line_height)
  319. line_height = style->GetProperty(LINE_HEIGHT);
  320. return line_height;
  321. }
  322. int ElementStyleCache::GetTextAlign()
  323. {
  324. if (text_align < 0)
  325. text_align = style->GetProperty(TEXT_ALIGN)->Get< int >();
  326. return text_align;
  327. }
  328. int ElementStyleCache::GetTextTransform()
  329. {
  330. if (text_transform < 0)
  331. text_transform = style->GetProperty(TEXT_TRANSFORM)->Get< int >();
  332. return text_transform;
  333. }
  334. const Property *ElementStyleCache::GetVerticalAlignProperty()
  335. {
  336. if (!vertical_align)
  337. vertical_align = style->GetProperty(VERTICAL_ALIGN);
  338. return vertical_align;
  339. }
  340. }
  341. }