00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GNASH_TEXTFIELD_H
00020 #define GNASH_TEXTFIELD_H
00021
00022 #include "InteractiveObject.h"
00023 #include "LineStyle.h"
00024 #include "snappingrange.h"
00025 #include "SWFRect.h"
00026
00027 #include <boost/intrusive_ptr.hpp>
00028 #include <map>
00029 #include <string>
00030 #include <vector>
00031
00032
00033 namespace gnash {
00034 namespace SWF {
00035 class DefineEditTextTag;
00036 class TextRecord;
00037 }
00038 class TextFormat_as;
00039 class Font;
00040 }
00041
00042 namespace gnash {
00043
00045 class TextField : public InteractiveObject
00046 {
00047
00048 public:
00049
00050 typedef std::vector<size_t> LineStarts;
00051
00053 enum TextAlignment
00054 {
00055 ALIGN_LEFT = 0,
00056 ALIGN_RIGHT,
00057 ALIGN_CENTER,
00058 ALIGN_JUSTIFY
00059 };
00060
00062 enum TextFormatDisplay
00063 {
00064 TEXTFORMAT_BLOCK = 0,
00065 TEXTFORMAT_INLINE = 1
00066 };
00067
00069 enum AutoSize {
00070
00072 AUTOSIZE_NONE,
00073
00075 AUTOSIZE_LEFT,
00076
00078 AUTOSIZE_CENTER,
00079
00081 AUTOSIZE_RIGHT
00082 };
00083
00085 enum TypeValue {
00086
00088 typeInvalid,
00089
00092 typeDynamic,
00093
00095 typeInput
00096 };
00097
00099 TextField(as_object* object, DisplayObject* parent,
00100 const SWF::DefineEditTextTag& def);
00101
00103
00107 TextField(as_object* object, DisplayObject* parent, const SWFRect& bounds);
00108
00109
00110 ~TextField();
00111
00112
00114 bool mouseEnabled() const { return true; }
00115
00117
00120 InteractiveObject* topmostMouseEntity(boost::int32_t x, boost::int32_t y);
00121
00123
00125 virtual int getDefinitionVersion() const;
00126
00128 void notifyEvent(const event_id& id);
00129
00130 const std::string& getVariableName() const
00131 {
00132 return _variable_name;
00133 }
00134
00137
00141 void set_variable_name(const std::string& newname);
00142
00145
00149 void updateText(const std::string& s);
00150
00151 void updateHtmlText(const std::string& s);
00152
00154 std::string get_text_value() const;
00155
00157 std::string get_htmltext_value() const;
00158
00160 bool getTextDefined() const { return _textDefined; }
00161
00162 size_t getCaretIndex() const {
00163 return m_cursor;
00164 }
00165
00167 const std::pair<size_t, size_t>& getSelection() const {
00168 return _selection;
00169 }
00170
00172
00174 void replaceSelection(const std::string& replace);
00175
00177
00180
00183 void setSelection(int start, int end);
00184
00186
00188 virtual void setWidth(double width);
00189
00191
00193 virtual void setHeight(double height);
00194
00196 virtual void display(Renderer& renderer, const Transform& xform);
00197
00198 void add_invalidated_bounds(InvalidatedRanges& ranges, bool force);
00199
00201 virtual SWFRect getBounds() const
00202 {
00203 return _bounds;
00204 }
00205
00206
00207 bool pointInShape(boost::int32_t x, boost::int32_t y) const;
00208
00210 bool getDrawBackground() const;
00211
00213
00215 void setDrawBackground(bool draw);
00216
00218 rgba getBackgroundColor() const;
00219
00221
00225 void setBackgroundColor(const rgba& col);
00226
00228 bool getDrawBorder() const;
00229
00231
00233 void setDrawBorder(bool draw);
00234
00236 rgba getBorderColor() const;
00237
00239
00243 void setBorderColor(const rgba& col);
00244
00246 const rgba& getTextColor() const
00247 {
00248 return _textColor;
00249 }
00250
00252
00255 void setTextColor(const rgba& col);
00256
00260 bool getEmbedFonts() const {
00261 return _embedFonts;
00262 }
00263
00265 boost::int32_t maxChars() const {
00266 return _maxChars;
00267 }
00268
00270
00273 void maxChars(boost::int32_t max) {
00274 _maxChars = max;
00275 }
00276
00278 bool multiline() const {
00279 return _multiline;
00280 }
00281
00283
00286 void multiline(bool b) {
00287 _multiline = b;
00288 }
00289
00291 bool password() const {
00292 return _password;
00293 }
00294
00296
00299 void password(bool b) {
00300 _password = b;
00301 }
00305
00307 void setEmbedFonts(bool use);
00308
00310 AutoSize getAutoSize() const
00311 {
00312 return _autoSize;
00313 }
00314
00316 TextAlignment getTextAlignment();
00317
00319
00323 void setAutoSize(AutoSize val);
00324
00326
00330 void setType(TypeValue val) { if (val != typeInvalid) _type=val; }
00331
00333 TypeValue getType() const
00334 {
00335 return _type;
00336 }
00337
00339 bool isReadOnly() const { return _type != typeInput; }
00340
00342
00348 static TypeValue parseTypeValue(const std::string& val);
00349
00351
00358 static const char* typeValueName(TypeValue val);
00359
00364 bool doWordWrap() const {
00365 return _wordWrap;
00366 }
00367
00369
00376 void setWordWrap(bool on);
00377
00379 bool doHtml() const {
00380 return _html;
00381 }
00382
00384
00387 void setHtml(bool on) {
00388 _html = on;
00389 }
00390
00392 bool isSelectable() const
00393 {
00394 return _selectable;
00395 }
00396
00398
00401 void setSelectable(bool v)
00402 {
00403 _selectable = v;
00404 }
00405
00406
00408 virtual bool isSelectableTextField() const
00409 {
00410 return isSelectable();
00411 }
00412
00414
00421 void removeTextField();
00422
00424
00428 boost::intrusive_ptr<const Font> setFont(
00429 boost::intrusive_ptr<const Font> newfont);
00430
00431 const Font* getFont() { return _font.get(); }
00432
00433
00434 boost::uint16_t getFontHeight() const
00435 {
00436 return _fontHeight;
00437 }
00438
00439 void setFontHeight(boost::uint16_t h);
00440
00441 boost::uint16_t getLeftMargin() const
00442 {
00443 return _leftMargin;
00444 }
00445
00446 void setLeftMargin(boost::uint16_t h);
00447
00448 boost::uint16_t getRightMargin() const
00449 {
00450 return _rightMargin;
00451 }
00452
00453 void setRightMargin(boost::uint16_t h);
00454
00455 boost::uint16_t getIndent() const
00456 {
00457 return _indent;
00458 }
00459
00460 void setIndent(boost::uint16_t h);
00461
00462 boost::uint16_t getBlockIndent() const
00463 {
00464 return _blockIndent;
00465 }
00466
00467 void setBlockIndent(boost::uint16_t h);
00468
00469 TextAlignment getAlignment() const
00470 {
00471 return _alignment;
00472 }
00473
00474 void setAlignment(TextAlignment h);
00475
00476 boost::int16_t getLeading() const
00477 {
00478 return _leading;
00479 }
00480
00481 void setLeading(boost::int16_t h);
00482
00483 bool getUnderlined() const
00484 {
00485 return _underlined;
00486 }
00487
00488 TextFormatDisplay getDisplay() const
00489 {
00490 return _display;
00491 }
00492
00493 bool getBullet() const
00494 {
00495 return _bullet;
00496 }
00497
00498 const std::vector<int>& getTabStops() const
00499 {
00500 return _tabStops;
00501 }
00502
00503 bool isRestrict() const
00504 {
00505 return _restrictDefined;
00506 }
00507
00508 const std::string& getRestrict() const
00509 {
00510 return _restrict;
00511 }
00512
00513 size_t getScroll() const
00514 {
00515 return _scroll;
00516 }
00517
00518 size_t getMaxScroll() const
00519 {
00520 return _maxScroll;
00521 }
00522
00523 size_t getHScroll() const
00524 {
00525 return _hScroll;
00526 }
00527
00528 size_t getMaxHScroll() const
00529 {
00530 return _maxHScroll;
00531 }
00532
00533 size_t getBottomScroll() const
00534 {
00535 return _bottomScroll;
00536 }
00537
00538 void setUnderlined(bool v);
00539 void setTabStops(const std::vector<int>& tabStops);
00540 void setBullet(bool b);
00541 void setURL(std::string url);
00542 void setTarget(std::string target);
00543 void setRestrict(const std::string& restrict);
00544 void setDisplay(TextFormatDisplay display);
00545 void setScroll(size_t scroll) {
00546 _scroll = scroll;
00547 format_text();
00548 }
00549 void setMaxScroll(size_t maxScroll) {
00550 _maxScroll = maxScroll;
00551 format_text();
00552 }
00553 void setHScroll(size_t hScroll) {
00554 _hScroll = hScroll;
00555 format_text();
00556 }
00557 void setMaxHScroll(size_t maxHScroll) {
00558 _maxHScroll = maxHScroll;
00559 format_text();
00560 }
00561 void setbottomScroll(size_t bottomScroll) {
00562 _bottomScroll = bottomScroll;
00563 format_text();
00564 }
00565
00567
00568 size_t cursorRecord();
00569
00570 void setTextFormat(TextFormat_as& tf);
00571
00572 const SWFRect& getTextBoundingBox() const
00573 {
00574 return m_text_bounding_box;
00575 }
00576
00578
00581 void setTextValue(const std::wstring& wstr);
00582
00584
00585 void setHtmlTextValue(const std::wstring& wstr);
00586
00587 private:
00588
00589 void init();
00590
00593
00597 void updateText(const std::wstring& s);
00598
00599 void updateHtmlText(const std::wstring& s);
00600
00601 void insertTab(SWF::TextRecord& rec, boost::int32_t& x, float scale);
00602
00604
00607 virtual bool handleFocus();
00608
00610 virtual void killFocus();
00611
00613 void onChanged();
00614
00616 void reset_bounding_box(boost::int32_t x, boost::int32_t y)
00617 {
00618 m_text_bounding_box.set_to_point(x, y);
00619 }
00620
00623 void format_text();
00624
00626 void scrollLines();
00627
00630 void newLine(boost::int32_t& x, boost::int32_t& y,
00631 SWF::TextRecord& rec, int& last_space_glyph,
00632 LineStarts::value_type& last_line_start_record, float div);
00633
00635 void handleChar(std::wstring::const_iterator& it,
00636 const std::wstring::const_iterator& e, boost::int32_t& x,
00637 boost::int32_t& y, SWF::TextRecord& rec, int& last_code,
00638 int& last_space_glyph,
00639 LineStarts::value_type& last_line_start_record);
00640
00651 bool parseHTML(std::wstring& tag,
00652 std::map<std::string, std::string>& attributes,
00653 std::wstring::const_iterator& it,
00654 const std::wstring::const_iterator& e,
00655 bool& selfclosing) const;
00656
00661 float align_line(TextAlignment align, int last_line_start_record, float x);
00662
00664
00680 void registerTextVariable();
00681
00682 typedef std::pair<as_object*, string_table::key> VariableRef;
00683
00688 VariableRef parseTextVariableRef(const std::string& variableName) const;
00689
00691
00694 void show_cursor(Renderer& renderer, const SWFMatrix& mat);
00695
00697
00699 boost::intrusive_ptr<const SWF::DefineEditTextTag> _tag;
00700
00702
00707 std::wstring _text;
00708
00710
00711 std::wstring _htmlText;
00712
00714 SWFRect m_text_bounding_box;
00715
00716 typedef std::vector<SWF::TextRecord> TextRecords;
00717 TextRecords _textRecords;
00718
00719 std::vector<size_t> _recordStarts;
00720
00721 TextRecords _displayRecords;
00722
00723 std::string _url;
00724 std::string _target;
00725 std::string _restrict;
00726 std::set<wchar_t> _restrictedchars;
00727 TextFormatDisplay _display;
00728 std::vector<int> _tabStops;
00729 LineStarts _line_starts;
00730
00732
00736 std::string _variable_name;
00737
00738 rgba _backgroundColor;
00739
00740 rgba _borderColor;
00741
00742 rgba _textColor;
00743
00744 TextAlignment _alignment;
00745
00746 boost::intrusive_ptr<const Font> _font;
00747 size_t m_cursor;
00748 size_t _glyphcount;
00749 size_t _scroll;
00750 size_t _maxScroll;
00751 size_t _hScroll;
00752 size_t _maxHScroll;
00753 size_t _bottomScroll;
00754 size_t _linesindisplay;
00755
00757 size_t _maxChars;
00758
00759 AutoSize _autoSize;
00760
00761 TypeValue _type;
00762
00764
00769 SWFRect _bounds;
00770
00773 std::pair<size_t, size_t> _selection;
00774
00775 boost::int16_t _leading;
00776 boost::uint16_t _indent;
00777
00780 boost::uint16_t _blockIndent;
00781
00782 boost::uint16_t _leftMargin;
00783
00784 boost::uint16_t _rightMargin;
00785
00786 boost::uint16_t _fontHeight;
00787
00792 bool _textDefined;
00793
00794 bool _htmlTextDefined;
00795
00796 bool _restrictDefined;
00797 bool _underlined;
00798 bool _bullet;
00799
00800 bool m_has_focus;
00801
00802
00804 bool _multiline;
00805
00807 bool _password;
00808
00810
00814 bool _text_variable_registered;
00815
00816 bool _drawBackground;
00817
00818 bool _drawBorder;
00819
00820 bool _embedFonts;
00821
00822 bool _wordWrap;
00823
00824 bool _html;
00825
00826 bool _selectable;
00827
00828 };
00829
00830 }
00831
00832 #endif