/AppKit/Platform/DOM/CPDOMDisplayServer.h

http://github.com/cacaodev/cappuccino · C Header · 218 lines · 173 code · 24 blank · 21 comment · 25 complexity · 1a7234a02c96d519af86da1b67b4ad3a MD5 · raw file

  1. /*
  2. * CPDOMDisplayServer.h
  3. * AppKit
  4. *
  5. * Created by Francisco Tolmasky.
  6. * Copyright 2008, 280 North, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #define DOM_OPTIMIZATION 0
  23. #define SetStyleOrigin 0
  24. #define SetStyleLeftTop 0
  25. #define SetStyleRightTop 1
  26. #define SetStyleLeftBottom 2
  27. #define SetStyleRightBottom 3
  28. #define SetStyleSize 4
  29. #define SetSize 5
  30. #define AppendChild 6
  31. #define InsertBefore 7
  32. #define RemoveChild 8
  33. #define CPDOMDisplayServerSetStyleOrigin(anInstruction, aDOMElement, aTransform, x, y)\
  34. if (!aDOMElement.CPDOMDisplayContext)\
  35. aDOMElement.CPDOMDisplayContext = [];\
  36. var __index = aDOMElement.CPDOMDisplayContext[SetStyleOrigin];\
  37. if (!(__index >= 0))\
  38. {\
  39. __index = aDOMElement.CPDOMDisplayContext[SetStyleOrigin] = CPDOMDisplayServerInstructionCount;\
  40. CPDOMDisplayServerInstructionCount += 5;\
  41. }\
  42. CPDOMDisplayServerInstructions[__index] = anInstruction;\
  43. CPDOMDisplayServerInstructions[__index + 1] = aDOMElement;\
  44. CPDOMDisplayServerInstructions[__index + 2] = aTransform;\
  45. CPDOMDisplayServerInstructions[__index + 3] = x;\
  46. CPDOMDisplayServerInstructions[__index + 4] = y;
  47. #if !DOM_OPTIMIZATION
  48. #define CPDOMDisplayServerSetStyleLeftTop(aDOMElement, aTransform, aLeft, aTop) \
  49. if (aTransform) var ____p = CGPointApplyAffineTransform(CGPointMake(aLeft, aTop), aTransform); \
  50. else var ____p = CGPointMake(aLeft, aTop); \
  51. aDOMElement.style.left = ROUND(____p.x) + "px";\
  52. aDOMElement.style.top = ROUND(____p.y) + "px";
  53. #define CPDOMDisplayServerSetStyleRightTop(aDOMElement, aTransform, aRight, aTop) \
  54. if (aTransform) var ____p = CGPointApplyAffineTransform(CGPointMake(aRight, aTop), aTransform); \
  55. else var ____p = CGPointMake(aRight, aTop); \
  56. aDOMElement.style.right = ROUND(____p.x) + "px";\
  57. aDOMElement.style.top = ROUND(____p.y) + "px";
  58. #define CPDOMDisplayServerSetStyleLeftBottom(aDOMElement, aTransform, aLeft, aBottom) \
  59. if (aTransform) var ____p = CGPointApplyAffineTransform(CGPointMake(aLeft, aBottom), aTransform); \
  60. else var ____p = CGPointMake(aLeft, aBottom); \
  61. aDOMElement.style.left = ROUND(____p.x) + "px";\
  62. aDOMElement.style.bottom = ROUND(____p.y) + "px";
  63. #define CPDOMDisplayServerSetStyleRightBottom(aDOMElement, aTransform, aRight, aBottom) \
  64. if (aTransform) var ____p = CGPointApplyAffineTransform(CGPointMake(aRight, aBottom), aTransform); \
  65. else var ____p = CGPointMake(aRight, aBottom); \
  66. aDOMElement.style.right = ROUND(____p.x) + "px";\
  67. aDOMElement.style.bottom = ROUND(____p.y) + "px";
  68. #define CPDOMDisplayServerSetStyleSize(aDOMElement, aWidth, aHeight) \
  69. aDOMElement.style.width = MAX(0.0, ROUND(aWidth)) + "px";\
  70. aDOMElement.style.height = MAX(0.0, ROUND(aHeight)) + "px";
  71. #define CPDOMDisplayServerSetSize(aDOMElement, aWidth, aHeight) \
  72. aDOMElement.width = MAX(0.0, ROUND(aWidth));\
  73. aDOMElement.height = MAX(0.0, ROUND(aHeight));
  74. #define CPDOMDisplayServerSetStyleBackgroundSize(aDOMElement, aWidth, aHeight)\
  75. aDOMElement.style.backgroundSize = aWidth + ' ' + aHeight;
  76. #define CPDOMDisplayServerAppendChild(aParentElement, aChildElement) aParentElement.appendChild(aChildElement)
  77. #define CPDOMDisplayServerInsertBefore(aParentElement, aChildElement, aBeforeElement) aParentElement.insertBefore(aChildElement, aBeforeElement)
  78. #define CPDOMDisplayServerRemoveChild(aParentElement, aChildElement) aParentElement.removeChild(aChildElement)
  79. #define PREPARE_DOM_OPTIMIZATION()
  80. #define EXECUTE_DOM_INSTRUCTIONS()
  81. #else
  82. #define CPDOMDisplayServerSetStyleLeftTop(aDOMElement, aTransform, aLeft, aTop) CPDOMDisplayServerSetStyleOrigin(SetStyleLeftTop, aDOMElement, aTransform, aLeft, aTop)
  83. #define CPDOMDisplayServerSetStyleRightTop(aDOMElement, aTransform, aRight, aTop) CPDOMDisplayServerSetStyleOrigin(SetStyleRightTop, aDOMElement, aTransform, aRight, aTop)
  84. #define CPDOMDisplayServerSetStyleLeftBottom(aDOMElement, aTransform, aLeft, aBottom) CPDOMDisplayServerSetStyleOrigin(SetStyleLeftBottom, aDOMElement, aTransform, aLeft, aBottom)
  85. #define CPDOMDisplayServerSetStyleRightBottom(aDOMElement, aTransform, aRight, aBottom) CPDOMDisplayServerSetStyleOrigin(SetStyleRightBottom, aDOMElement, aTransform, aRight, aBottom)
  86. #define CPDOMDisplayServerSetStyleSize(aDOMElement, aWidth, aHeight)\
  87. if (!aDOMElement.CPDOMDisplayContext)\
  88. aDOMElement.CPDOMDisplayContext = [];\
  89. var __index = aDOMElement.CPDOMDisplayContext[SetStyleSize];\
  90. if (!(__index >= 0))\
  91. {\
  92. __index = aDOMElement.CPDOMDisplayContext[SetStyleSize] = CPDOMDisplayServerInstructionCount;\
  93. CPDOMDisplayServerInstructionCount += 4;\
  94. }\
  95. CPDOMDisplayServerInstructions[__index] = SetStyleSize;\
  96. CPDOMDisplayServerInstructions[__index + 1] = aDOMElement;\
  97. CPDOMDisplayServerInstructions[__index + 2] = aWidth;\
  98. CPDOMDisplayServerInstructions[__index + 3] = aHeight;
  99. #define CPDOMDisplayServerSetSize(aDOMElement, aWidth, aHeight)\
  100. if (!aDOMElement.CPDOMDisplayContext)\
  101. aDOMElement.CPDOMDisplayContext = [];\
  102. var __index = aDOMElement.CPDOMDisplayContext[SetSize];\
  103. if (!(__index >= 0))\
  104. {\
  105. __index = aDOMElement.CPDOMDisplayContext[SetSize] = CPDOMDisplayServerInstructionCount;\
  106. CPDOMDisplayServerInstructionCount += 4;\
  107. }\
  108. CPDOMDisplayServerInstructions[__index] = SetSize;\
  109. CPDOMDisplayServerInstructions[__index + 1] = aDOMElement;\
  110. CPDOMDisplayServerInstructions[__index + 2] = aWidth;\
  111. CPDOMDisplayServerInstructions[__index + 3] = aHeight;
  112. #define CPDOMDisplayServerSetStyleBackgroundSize(aDOMElement, aWidth, aHeight)\
  113. aDOMElement.style.backgroundSize = aWidth + ' ' + aHeight;
  114. #define CPDOMDisplayServerAppendChild(aParentElement, aChildElement)\
  115. if (aChildElement.CPDOMDisplayContext) aChildElement.CPDOMDisplayContext[SetStyleOrigin] = -1;\
  116. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = AppendChild;\
  117. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aParentElement;\
  118. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aChildElement;
  119. #define CPDOMDisplayServerInsertBefore(aParentElement, aChildElement, aBeforeElement)\
  120. if (aChildElement.CPDOMDisplayContext) aChildElement.CPDOMDisplayContext[SetStyleOrigin] = -1;\
  121. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = InsertBefore;\
  122. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aParentElement;\
  123. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aChildElement;\
  124. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aBeforeElement;
  125. #define CPDOMDisplayServerRemoveChild(aParentElement, aChildElement)\
  126. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = RemoveChild;\
  127. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aParentElement;\
  128. CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aChildElement;
  129. #define PREPARE_DOM_OPTIMIZATION()\
  130. CPDOMDisplayServerInstructions = [];\
  131. CPDOMDisplayServerInstructionCount = 0;
  132. #define EXECUTE_DOM_INSTRUCTIONS()\
  133. var index = 0;\
  134. while (index < CPDOMDisplayServerInstructionCount)\
  135. {\
  136. var instruction = CPDOMDisplayServerInstructions[index++];\
  137. try{\
  138. switch (instruction)\
  139. {\
  140. case SetStyleLeftTop:\
  141. case SetStyleRightTop:\
  142. case SetStyleLeftBottom:\
  143. case SetStyleRightBottom: var element = CPDOMDisplayServerInstructions[index],\
  144. style = element.style,\
  145. x = (instruction == SetStyleLeftTop || instruction == SetStyleLeftBottom) ? "left" : "right",\
  146. y = (instruction == SetStyleLeftTop || instruction == SetStyleRightTop) ? "top" : "bottom";\
  147. CPDOMDisplayServerInstructions[index++] = nil;\
  148. var transform = CPDOMDisplayServerInstructions[index++];\
  149. if (transform)\
  150. {\
  151. var point = CGPointMake(CPDOMDisplayServerInstructions[index++], CPDOMDisplayServerInstructions[index++]),\
  152. transformed = CGPointApplyAffineTransform(point, transform);\
  153. style[x] = ROUND(transformed.x) + "px";\
  154. style[y] = ROUND(transformed.y) + "px";\
  155. }\
  156. else\
  157. {\
  158. style[x] = ROUND(CPDOMDisplayServerInstructions[index++]) + "px";\
  159. style[y] = ROUND(CPDOMDisplayServerInstructions[index++]) + "px";\
  160. }\
  161. element.CPDOMDisplayContext[SetStyleOrigin] = -1;\
  162. break;\
  163. case SetStyleSize: var element = CPDOMDisplayServerInstructions[index],\
  164. style = element.style;\
  165. CPDOMDisplayServerInstructions[index++] = nil;\
  166. element.CPDOMDisplayContext[SetStyleSize] = -1;\
  167. style.width = MAX(0.0, ROUND(CPDOMDisplayServerInstructions[index++])) + "px";\
  168. style.height = MAX(0.0, ROUND(CPDOMDisplayServerInstructions[index++])) + "px";\
  169. break;\
  170. case SetSize: var element = CPDOMDisplayServerInstructions[index];\
  171. CPDOMDisplayServerInstructions[index++] = nil;\
  172. element.CPDOMDisplayContext[SetSize] = -1;\
  173. element.width = MAX(0.0, ROUND(CPDOMDisplayServerInstructions[index++]));\
  174. element.height = MAX(0.0, ROUND(CPDOMDisplayServerInstructions[index++]));\
  175. break;\
  176. case AppendChild: CPDOMDisplayServerInstructions[index].appendChild(CPDOMDisplayServerInstructions[index + 1]);\
  177. CPDOMDisplayServerInstructions[index++] = nil;\
  178. CPDOMDisplayServerInstructions[index++] = nil;\
  179. break;\
  180. case InsertBefore: CPDOMDisplayServerInstructions[index].insertBefore(CPDOMDisplayServerInstructions[index + 1], CPDOMDisplayServerInstructions[index + 2]);\
  181. CPDOMDisplayServerInstructions[index++] = nil;\
  182. CPDOMDisplayServerInstructions[index++] = nil;\
  183. CPDOMDisplayServerInstructions[index++] = nil;\
  184. break;\
  185. case RemoveChild: CPDOMDisplayServerInstructions[index].removeChild(CPDOMDisplayServerInstructions[index + 1]);\
  186. CPDOMDisplayServerInstructions[index++] = nil;\
  187. CPDOMDisplayServerInstructions[index++] = nil;\
  188. break;\
  189. }\
  190. }\
  191. catch(e) { CPLog("e " + e + " " + instruction); }\
  192. }\
  193. CPDOMDisplayServerInstructionCount = 0;
  194. #endif