WriterState.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Project : Mono
  23. * Namespace : System.Web.UI.MobileControls.Adapters
  24. * Class : WriterState
  25. * Author : Gaurav Vaish
  26. *
  27. * Copyright : 2003 with Gaurav Vaish, and with
  28. * Ximian Inc
  29. */
  30. using System;
  31. using System.IO;
  32. using System.Collections;
  33. using System.Drawing;
  34. using System.Web.Mobile;
  35. namespace System.Web.UI.MobileControls.Adapters
  36. {
  37. class WriterState : StyleStack
  38. {
  39. // stack of tagswritten and writerstyles
  40. private Stack stack = new Stack();
  41. private Stack tagsWritten = new Stack();
  42. private WriterStyle current = new WriterStyle();
  43. private HtmlMobileTextWriter writer;
  44. private bool isInTransition = false;
  45. private bool isBreakPending = false;
  46. private int fontLevel = -1;
  47. private int divLevel = -1;
  48. private int mark = 0;
  49. public WriterState(HtmlMobileTextWriter writer) : base(writer)
  50. {
  51. this.writer = writer;
  52. }
  53. public bool IsBreakPending
  54. {
  55. get
  56. {
  57. return this.isBreakPending;
  58. }
  59. set
  60. {
  61. this.isBreakPending = value;
  62. }
  63. }
  64. public int FontLevel
  65. {
  66. get
  67. {
  68. return this.fontLevel;
  69. }
  70. set
  71. {
  72. this.fontLevel = value;
  73. }
  74. }
  75. public int DivLevel
  76. {
  77. get
  78. {
  79. return this.divLevel;
  80. }
  81. set
  82. {
  83. this.divLevel = value;
  84. }
  85. }
  86. public WriterStyle Current
  87. {
  88. get
  89. {
  90. return this.current;
  91. }
  92. }
  93. // The tags that have been written.
  94. public Stack TagsWritten
  95. {
  96. get
  97. {
  98. return this.tagsWritten;
  99. }
  100. }
  101. public HtmlTextWriter Writer
  102. {
  103. get
  104. {
  105. return this.writer;
  106. }
  107. }
  108. public void MarkStyleContext()
  109. {
  110. this.mark = this.tagsWritten.Count;
  111. }
  112. public void UnmarkStyleContext()
  113. {
  114. while(tagsWritten.Count > mark)
  115. CloseTag();
  116. }
  117. public WriterStyle PopState()
  118. {
  119. writer.ShouldEnsureStyle = true;
  120. IsBreakPending = (bool) stack.Pop();
  121. while(tagsWritten.Count > 0)
  122. CloseTag();
  123. tagsWritten = (Stack)stack.Pop();
  124. current = (WriterStyle)stack.Pop();
  125. return current;
  126. }
  127. public void PushState()
  128. {
  129. writer.ShouldEnsureStyle = true;
  130. stack.Push(current);
  131. stack.Push(tagsWritten);
  132. stack.Push(IsBreakPending);
  133. current = new WriterStyle();
  134. tagsWritten = new Stack();
  135. IsBreakPending = false;
  136. }
  137. public void CloseTag()
  138. {
  139. StyleTag tag = tagsWritten.Pop() as StyleTag;
  140. if(tag != null)
  141. tag.CloseTag(this);
  142. }
  143. public void Transition(WriterStyle style)
  144. {
  145. Transition(style, true);
  146. }
  147. [MonoTODO]
  148. public void Transition(WriterStyle style, bool captureOutput)
  149. {
  150. HtmlMobileTextWriter tempWriter = this.writer;
  151. try
  152. {
  153. if(!captureOutput && !isInTransition)
  154. {
  155. this.writer = new HtmlMobileTextWriter(
  156. new HtmlTextWriter(
  157. new StringWriter()),
  158. tempWriter.Device);
  159. isInTransition = true;
  160. if(Count > 0)
  161. {
  162. while(Count > 0)
  163. {
  164. CloseTag();
  165. }
  166. isInTransition = false;
  167. } else
  168. {
  169. if(current.Italic && writer.RenderItalic)
  170. {
  171. while(current.Italic)
  172. {
  173. CloseTag();
  174. }
  175. }
  176. if(current.Bold && writer.RenderBold)
  177. {
  178. while(current.Bold)
  179. {
  180. CloseTag();
  181. }
  182. }
  183. if(current.FontColor != Color.Empty && writer.RenderFontColor)
  184. {
  185. while(current.FontColor != Color.Empty)
  186. {
  187. CloseTag();
  188. }
  189. }
  190. if(current.FontName != String.Empty && writer.RenderFontName)
  191. {
  192. while(current.FontName != String.Empty)
  193. {
  194. CloseTag();
  195. }
  196. }
  197. if(FontChange(style))
  198. {
  199. while(FontLevel > Count)
  200. {
  201. CloseTag();
  202. }
  203. }
  204. if(current.Wrapping == Wrapping.NoWrap && writer.RenderDivNoWrap)
  205. {
  206. while(current.Wrapping == Wrapping.NoWrap)
  207. {
  208. CloseTag();
  209. }
  210. }
  211. if(current.Alignment != Alignment.NotSet && writer.RenderDivAlign)
  212. {
  213. while(DivLevel > Count)
  214. {
  215. CloseTag();
  216. }
  217. }
  218. bool dc = DivChange(style);
  219. if(IsBreakPending && !dc)
  220. {
  221. writer.WriteBreak();
  222. IsBreakPending = false;
  223. }
  224. if(dc)
  225. {
  226. while(FontLevel > Count)
  227. {
  228. if(current.Bold || current.Italic)
  229. CloseTag();
  230. }
  231. }
  232. bool fc = FontChange(style);
  233. dc = DivChange(style);
  234. if(dc)
  235. {
  236. throw new NotImplementedException();
  237. //DivStyleTag
  238. // Actually Render
  239. }
  240. if(fc)
  241. {
  242. throw new NotImplementedException();
  243. // Actually Render
  244. }
  245. // Push Bold, Italic etc in current Stack
  246. isInTransition = false;
  247. throw new NotImplementedException();
  248. }
  249. }
  250. } finally
  251. {
  252. this.writer = tempWriter;
  253. }
  254. }
  255. private bool DivChange(WriterStyle newStyle)
  256. {
  257. bool retVal = false;
  258. if(newStyle.Layout)
  259. {
  260. if((current.Wrapping != newStyle.Wrapping
  261. && writer.RenderDivNoWrap) ||
  262. (current.Alignment != newStyle.Alignment
  263. && writer.RenderDivAlign))
  264. retVal = true;
  265. }
  266. return retVal;
  267. }
  268. private bool FontChange(WriterStyle newStyle)
  269. {
  270. bool retVal = false;
  271. if( (current.FontColor != newStyle.FontColor
  272. && writer.RenderFontColor) ||
  273. (current.FontSize != newStyle.FontSize
  274. && writer.RenderFontSize) ||
  275. (current.FontName != newStyle.FontName
  276. && writer.RenderFontName))
  277. retVal = true;
  278. return retVal;
  279. }
  280. }
  281. }