Merge branch 'master' into release_0_8_9
[gnash.git] / libcore / swf / DefineEditTextTag.cpp
blobbc3c33f1d90640a4427445c19bf5e953cbb329ae
1 //
2 // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "DefineEditTextTag.h"
20 #include "TypesParser.h"
21 #include "RunResources.h"
22 #include "TextField_as.h"
23 #include "TextField.h"
24 #include "movie_definition.h"
25 #include "Font.h"
26 #include "SWFStream.h"
27 #include "Global_as.h"
29 namespace gnash {
30 namespace SWF {
32 void
33 DefineEditTextTag::loader(SWFStream& in, TagType tag, movie_definition& m,
34 const RunResources& /*r*/)
36 assert(tag == SWF::DEFINEEDITTEXT); // 37
38 in.ensureBytes(2);
39 const boost::uint16_t id = in.read_u16();
41 std::auto_ptr<DefineEditTextTag> editText(new DefineEditTextTag(in, m, id));
43 m.addDisplayObject(id, editText.release());
46 DisplayObject*
47 DefineEditTextTag::createDisplayObject(Global_as& gl, DisplayObject* parent)
48 const
50 // Resolve the font, if possible
51 getFont();
52 as_object* obj = createTextFieldObject(gl);
54 // If the TextField class is not present, the construction of a TextField
55 // object will fail.
56 // TODO: check what happens in this case (when the TextField class is
57 // removed before this tag is processed).
58 if (!obj) {
59 // Note that it is expected that createDisplayObject() always returns
60 // a DisplayObject and not null.
61 log_error("Failed to construct a TextField object; using"
62 "a substitute object");
63 obj = new as_object(gl);
66 TextField* ch = new TextField(obj, parent, *this);
68 return ch;
72 void
73 DefineEditTextTag::read(SWFStream& in, movie_definition& m)
76 _rect = readRect(in);
78 in.align();
79 in.ensureBytes(2);
81 int flags = in.read_u8();
82 _hasText = flags & (1 << 7);
83 _wordWrap = flags & (1 << 6);
84 _multiline = flags & (1 << 5);
85 _password = flags & (1 << 4);
86 _readOnly = flags & (1 << 3);
88 const bool hasColor = flags & (1 << 2);
89 const bool hasMaxChars = flags & (1 << 1);
90 const bool hasFont = flags & (1 << 0);
92 flags = in.read_u8();
93 // 0: no font class, 1 font class and height, can't be true if has_font was true
94 bool hasFontClass = flags & (1 << 7);
95 if (hasFontClass && hasFont )
97 IF_VERBOSE_MALFORMED_SWF(
98 log_swferror("DefineEditText: hasFontClass can't be true if "
99 "hasFont is true, ignoring");
101 hasFontClass = false;
104 _autoSize = flags & (1 << 6);
105 bool hasLayout = flags & (1 << 5);
106 _noSelect = flags & (1 << 4);
107 _border = flags & (1 << 3);
109 // authored as static text (not dynamic text)
110 bool wasStatic = flags & (1 << 2);
112 // TODO: what is this for?
113 UNUSED(wasStatic);
115 _html = flags & (1 << 1);
116 _useOutlines = flags & (1 << 0);
118 if (hasFont)
120 in.ensureBytes(4);
121 _fontID = in.read_u16();
122 _font = m.get_font(_fontID);
123 if (!_font)
125 IF_VERBOSE_MALFORMED_SWF(
126 log_swferror("DefineEditText: tag refers to unknown font "
127 "id %d", _fontID);
130 _textHeight = in.read_u16();
132 else if (hasFontClass)
134 std::string fontClassName;
135 in.read_string(fontClassName);
136 log_unimpl("Font class support for DefineEditText (%d)",
137 fontClassName);
140 if (hasColor) {
141 _color = readRGBA(in);
144 if (hasMaxChars)
146 in.ensureBytes(2);
147 _maxChars = in.read_u16();
150 if (hasLayout)
152 in.ensureBytes(9); //1 + 2 + 2 + 2 + 2
153 _alignment = static_cast<TextField::TextAlignment>(in.read_u8());
154 _leftMargin = in.read_u16();
155 _rightMargin = in.read_u16();
156 _indent = in.read_s16();
157 _leading = in.read_s16();
160 in.read_string(_variableName);
162 if (_hasText)
164 in.read_string(_defaultText);
167 IF_VERBOSE_PARSE (
168 log_parse("edit_text_char:\n"
169 " varname = %s\n"
170 " text = \"%s\"\n"
171 " font_id: %d\n"
172 " text_height: %d",
173 _variableName, _defaultText, _fontID, _textHeight);
177 DefineEditTextTag::DefineEditTextTag(SWFStream& in, movie_definition& m,
178 boost::uint16_t id)
180 DefinitionTag(id),
181 _hasText(true),
182 _wordWrap(false),
183 _multiline(false),
184 _password(false),
185 _readOnly(true),
186 _autoSize(false),
187 _noSelect(false),
188 _border(false),
189 _html(false),
190 _useOutlines(false),
191 _fontID(-1),
192 _textHeight(240),
193 _maxChars(0),
194 _alignment(TextField::ALIGN_LEFT),
195 _leftMargin(0),
196 _rightMargin(0),
197 _indent(0),
198 _leading(0)
200 _color.set(0, 0, 0, 255);
202 // Parse the tag from the stream.
203 read(in, m);