/src/cocktail/domElement/as3/TextDOMElement.hx

http://github.com/silexlabs/Cocktail · Haxe · 98 lines · 41 code · 8 blank · 49 comment · 0 complexity · 7e6d384007f91f4f466a089d92bddf99 MD5 · raw file

  1. /*
  2. This file is part of Silex - see http://projects.silexlabs.org/?/silex
  3. Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
  4. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  7. */
  8. package cocktail.domElement.as3;
  9. import cocktail.nativeElement.NativeElement;
  10. import flash.display.DisplayObjectContainer;
  11. import flash.display.Sprite;
  12. import flash.text.TextField;
  13. import haxe.Log;
  14. import cocktail.domElement.abstract.AbstractTextDOMElement;
  15. /**
  16. * This is the Text DOMElement implementation for Flash.
  17. *
  18. * It displays an htmlText in a native flash text field.
  19. * It needs to convert the HTML text to Flash pseudo-HTML
  20. * format before displaying it
  21. *
  22. * @author Yannick DOMINGUEZ
  23. */
  24. class TextDOMElement extends AbstractTextDOMElement
  25. {
  26. /**
  27. * The native Flash text field that will be used to display
  28. * the html text
  29. */
  30. private var _nativeTextField:TextField;
  31. /**
  32. * class constructor. Instantiate the native text field
  33. * and add it as a child of the native Sprite.
  34. */
  35. public function new(nativeElement:NativeElement = null)
  36. {
  37. super(nativeElement);
  38. _nativeTextField = new TextField();
  39. _nativeTextField.wordWrap = true;
  40. _nativeTextField.multiline = true;
  41. this._nativeElement.addChild(_nativeTextField);
  42. }
  43. /**
  44. * This method is overriden to set the Flash pseudo-html
  45. * string on the native text field
  46. * @param text the HTML text before conversion
  47. */
  48. override public function setText(text:String):String
  49. {
  50. super.setText(text);
  51. _nativeTextField.htmlText = fromHTMLtoFLASHTML(text);
  52. return text;
  53. }
  54. /**
  55. * Convert an HTML string into a Flash pseudo-html string.
  56. * Flash supports only a limited subset of the HTML format
  57. * @param html the HTML string
  58. * @return the pseudo-html string
  59. */
  60. private static function fromHTMLtoFLASHTML(html:String):String
  61. {
  62. return html;
  63. }
  64. /**
  65. * override to set only the width of the native text
  66. * field. The container sprite must not be reseized,
  67. * else it scale the text
  68. */
  69. override public function setWidth(value:Int):Int
  70. {
  71. this._width = value;
  72. _nativeTextField.width = value;
  73. return this._width;
  74. }
  75. /**
  76. * override to set only the height of the native text
  77. * field. The container sprite must not be reseized,
  78. * else it scale the text
  79. */
  80. override public function setHeight(value:Int):Int
  81. {
  82. this._height = value;
  83. _nativeTextField.height = value;
  84. return this._height;
  85. }
  86. }