WriterState.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Project : Mono
  3. * Namespace : System.Web.UI.MobileControls.Adapters
  4. * Class : WriterState
  5. * Author : Gaurav Vaish
  6. *
  7. * Copyright : 2003 with Gaurav Vaish, and with
  8. * Ximian Inc
  9. */
  10. using System;
  11. using System.IO;
  12. using System.Collections;
  13. using System.Drawing;
  14. using System.Web.Mobile;
  15. namespace System.Web.UI.MobileControls.Adapters
  16. {
  17. class WriterState : StyleStack
  18. {
  19. // stack of tagswritten and writerstyles
  20. private Stack stack = new Stack();
  21. private Stack tagsWritten = new Stack();
  22. private WriterStyle current = new WriterStyle();
  23. private HtmlMobileTextWriter writer;
  24. private bool isInTransition = false;
  25. private bool isBreakPending = false;
  26. private int fontLevel = -1;
  27. private int divLevel = -1;
  28. private int mark = 0;
  29. public WriterState(HtmlMobileTextWriter writer) : base(writer)
  30. {
  31. this.writer = writer;
  32. }
  33. public bool IsBreakPending
  34. {
  35. get
  36. {
  37. return this.isBreakPending;
  38. }
  39. set
  40. {
  41. this.isBreakPending = value;
  42. }
  43. }
  44. public int FontLevel
  45. {
  46. get
  47. {
  48. return this.fontLevel;
  49. }
  50. set
  51. {
  52. this.fontLevel = value;
  53. }
  54. }
  55. public int DivLevel
  56. {
  57. get
  58. {
  59. return this.divLevel;
  60. }
  61. set
  62. {
  63. this.divLevel = value;
  64. }
  65. }
  66. public WriterStyle Current
  67. {
  68. get
  69. {
  70. return this.current;
  71. }
  72. }
  73. // The tags that have been written.
  74. public Stack TagsWritten
  75. {
  76. get
  77. {
  78. return this.tagsWritten;
  79. }
  80. }
  81. public HtmlTextWriter Writer
  82. {
  83. get
  84. {
  85. return this.writer;
  86. }
  87. }
  88. public void MarkStyleContext()
  89. {
  90. this.mark = this.tagsWritten.Count;
  91. }
  92. public void UnmarkStyleContext()
  93. {
  94. while(tagsWritten.Count > mark)
  95. CloseTag();
  96. }
  97. public WriterStyle PopState()
  98. {
  99. writer.ShouldEnsureStyle = true;
  100. IsBreakPending = (bool) stack.Pop();
  101. while(tagsWritten.Count > 0)
  102. CloseTag();
  103. tagsWritten = (Stack)stack.Pop();
  104. current = (WriterStyle)stack.Pop();
  105. return current;
  106. }
  107. public void PushState()
  108. {
  109. writer.ShouldEnsureStyle = true;
  110. stack.Push(current);
  111. stack.Push(tagsWritten);
  112. stack.Push(IsBreakPending);
  113. current = new WriterStyle();
  114. tagsWritten = new Stack();
  115. IsBreakPending = false;
  116. }
  117. public void CloseTag()
  118. {
  119. StyleTag tag = tagsWritten.Pop() as StyleTag;
  120. if(tag != null)
  121. tag.CloseTag(this);
  122. }
  123. public void Transition(WriterStyle style)
  124. {
  125. Transition(style, true);
  126. }
  127. [MonoTODO]
  128. public void Transition(WriterStyle style, bool captureOutput)
  129. {
  130. HtmlMobileTextWriter tempWriter = this.writer;
  131. try
  132. {
  133. if(!captureOutput && !isInTransition)
  134. {
  135. this.writer = new HtmlMobileTextWriter(
  136. new HtmlTextWriter(
  137. new StringWriter()),
  138. tempWriter.Device);
  139. isInTransition = true;
  140. if(Count > 0)
  141. {
  142. while(Count > 0)
  143. {
  144. CloseTag();
  145. }
  146. isInTransition = false;
  147. } else
  148. {
  149. if(current.Italic && writer.RenderItalic)
  150. {
  151. while(current.Italic)
  152. {
  153. CloseTag();
  154. }
  155. }
  156. if(current.Bold && writer.RenderBold)
  157. {
  158. while(current.Bold)
  159. {
  160. CloseTag();
  161. }
  162. }
  163. if(current.FontColor != Color.Empty && writer.RenderFontColor)
  164. {
  165. while(current.FontColor != Color.Empty)
  166. {
  167. CloseTag();
  168. }
  169. }
  170. if(current.FontName != String.Empty && writer.RenderFontName)
  171. {
  172. while(current.FontName != String.Empty)
  173. {
  174. CloseTag();
  175. }
  176. }
  177. if(FontChange(style))
  178. {
  179. while(FontLevel > Count)
  180. {
  181. CloseTag();
  182. }
  183. }
  184. if(current.Wrapping == Wrapping.NoWrap && writer.RenderDivNoWrap)
  185. {
  186. while(current.Wrapping == Wrapping.NoWrap)
  187. {
  188. CloseTag();
  189. }
  190. }
  191. if(current.Alignment != Alignment.NotSet && writer.RenderDivAlign)
  192. {
  193. while(DivLevel > Count)
  194. {
  195. CloseTag();
  196. }
  197. }
  198. bool dc = DivChange(style);
  199. if(IsBreakPending && !dc)
  200. {
  201. writer.WriteBreak();
  202. IsBreakPending = false;
  203. }
  204. if(dc)
  205. {
  206. while(FontLevel > Count)
  207. {
  208. if(current.Bold || current.Italic)
  209. CloseTag();
  210. }
  211. }
  212. bool fc = FontChange(style);
  213. dc = DivChange(style);
  214. if(dc)
  215. {
  216. throw new NotImplementedException();
  217. //DivStyleTag
  218. // Actually Render
  219. }
  220. if(fc)
  221. {
  222. throw new NotImplementedException();
  223. // Actually Render
  224. }
  225. // Push Bold, Italic etc in current Stack
  226. isInTransition = false;
  227. throw new NotImplementedException();
  228. }
  229. }
  230. } finally
  231. {
  232. this.writer = tempWriter;
  233. }
  234. }
  235. private bool DivChange(WriterStyle newStyle)
  236. {
  237. bool retVal = false;
  238. if(newStyle.Layout)
  239. {
  240. if((current.Wrapping != newStyle.Wrapping
  241. && writer.RenderDivNoWrap) ||
  242. (current.Alignment != newStyle.Alignment
  243. && writer.RenderDivAlign))
  244. retVal = true;
  245. }
  246. return retVal;
  247. }
  248. private bool FontChange(WriterStyle newStyle)
  249. {
  250. bool retVal = false;
  251. if( (current.FontColor != newStyle.FontColor
  252. && writer.RenderFontColor) ||
  253. (current.FontSize != newStyle.FontSize
  254. && writer.RenderFontSize) ||
  255. (current.FontName != newStyle.FontName
  256. && writer.RenderFontName))
  257. retVal = true;
  258. return retVal;
  259. }
  260. }
  261. }