Gnash 0.8.9

TextField.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
00003 //   2011 Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_TEXTFIELD_H
00020 #define GNASH_TEXTFIELD_H
00021 
00022 #include <boost/intrusive_ptr.hpp>
00023 #include <map>
00024 #include <string>
00025 #include <vector>
00026 
00027 #include "InteractiveObject.h" // for inheritance
00028 #include "LineStyle.h" // for LineStyle
00029 #include "snappingrange.h"
00030 #include "SWFRect.h" // for inlines
00031 
00032 // Forward declarations
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         // TODO: should this return isSelectable() ?
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 
00152         std::string get_text_value() const;
00153 
00155         std::string get_htmltext_value() const;
00156 
00158         bool getTextDefined() const { return _textDefined; }
00159 
00160     size_t getCaretIndex() const {
00161         return m_cursor;
00162     }
00163 
00165     //
00168     const std::pair<size_t, size_t>& getSelection() const {
00169         return _selection;
00170     }
00171 
00173         //
00175     void replaceSelection(const std::string& replace);
00176 
00178     //
00181     //
00184     void setSelection(int start, int end);
00185 
00187     //
00189     virtual void setWidth(double width);
00190     
00192     //
00194     virtual void setHeight(double height);
00195 
00197         virtual void display(Renderer& renderer, const Transform& xform);
00198 
00199         void add_invalidated_bounds(InvalidatedRanges& ranges, bool force);
00200 
00202         virtual SWFRect getBounds() const
00203         {
00204                 return _bounds;
00205         }
00206 
00207         // See dox in DisplayObject.h
00208         bool pointInShape(boost::int32_t x, boost::int32_t y) const;
00209 
00211         bool getDrawBackground() const;
00212 
00214         //
00216         void setDrawBackground(bool draw);
00217 
00219         rgba getBackgroundColor() const;
00220 
00222         //
00226         void setBackgroundColor(const rgba& col);
00227 
00229         bool getDrawBorder() const;
00230 
00232         //
00234         void setDrawBorder(bool draw);
00235 
00237         rgba getBorderColor() const;
00238 
00240         //
00244         void setBorderColor(const rgba& col);
00245 
00247         const rgba& getTextColor() const 
00248         {
00249                 return _textColor;
00250         }
00251 
00253         //
00256         void setTextColor(const rgba& col);
00257 
00261         bool getEmbedFonts() const {
00262                 return _embedFonts;
00263         }
00264 
00266     boost::int32_t maxChars() const {
00267         return _maxChars;
00268     }
00269 
00271         //
00274     void maxChars(boost::int32_t max) {
00275         _maxChars = max;
00276     }
00277 
00279     bool multiline() const {
00280         return _multiline;
00281     }
00282 
00284         //
00287     void multiline(bool b) {
00288         _multiline = b;
00289     }
00290         
00292     bool password() const {
00293         return _password;
00294     }
00295 
00297         //
00300     void password(bool b) {
00301         _password = b;
00302     }
00306         //
00308         void setEmbedFonts(bool use);
00309 
00311         AutoSize getAutoSize() const
00312         {
00313                 return _autoSize;
00314         }
00315 
00317     TextAlignment getTextAlignment();
00318 
00320         //
00324         void setAutoSize(AutoSize val);
00325 
00327         //
00331         void setType(TypeValue val) { if (val != typeInvalid) _type=val; }
00332 
00334         TypeValue getType() const
00335         {
00336                 return _type;
00337         }
00338 
00340         bool isReadOnly() const { return _type != typeInput; }
00341 
00343         //
00349         static TypeValue parseTypeValue(const std::string& val);
00350 
00352         //
00359         static const char* typeValueName(TypeValue val);
00360 
00365         bool doWordWrap() const {
00366                 return _wordWrap;
00367         }
00368 
00370         //
00377         void setWordWrap(bool on);
00378 
00380         bool doHtml() const {
00381                 return _html;
00382         }
00383 
00385         //
00388         void setHtml(bool on) {
00389                 _html = on;
00390         }
00391 
00393         bool isSelectable() const
00394         {
00395                 return _selectable;
00396         }
00397 
00399         //
00402         void setSelectable(bool v) 
00403         {
00404                 _selectable = v;
00405         }
00406 
00407         // See DisplayObject::isActiveTextField
00409         virtual bool isSelectableTextField() const
00410         {
00411                 return isSelectable();
00412         }
00413 
00415         //
00422         void removeTextField();
00423 
00425         //
00429         boost::intrusive_ptr<const Font> setFont(
00430             boost::intrusive_ptr<const Font> newfont);
00431 
00432         const Font* getFont() { return _font.get(); }
00433 
00434         
00435         boost::uint16_t getFontHeight() const
00436         {
00437                 return _fontHeight;
00438         }
00439 
00440         void setFontHeight(boost::uint16_t h);
00441 
00442         boost::uint16_t getLeftMargin() const
00443         {
00444                 return _leftMargin;
00445         }
00446 
00447         void setLeftMargin(boost::uint16_t h);
00448 
00449         boost::uint16_t getRightMargin() const
00450         {
00451                 return _rightMargin;
00452         }
00453 
00454         void setRightMargin(boost::uint16_t h);
00455 
00456         boost::uint16_t getIndent() const
00457         {
00458                 return _indent;
00459         }
00460 
00461         void setIndent(boost::uint16_t h);
00462 
00463         boost::uint16_t getBlockIndent() const
00464         {
00465                 return _blockIndent;
00466         }
00467 
00468         void setBlockIndent(boost::uint16_t h);
00469 
00470         TextAlignment getAlignment() const
00471         {
00472                 return _alignment;
00473         }
00474 
00475         void setAlignment(TextAlignment h);
00476 
00477         boost::int16_t getLeading() const
00478         {
00479                 return _leading;
00480         }
00481 
00482         void setLeading(boost::int16_t h);
00483 
00484         bool getUnderlined() const
00485         {
00486                 return _underlined;
00487         }
00488         
00489         TextFormatDisplay getDisplay() const
00490         { 
00491                 return _display;
00492         }
00493         
00494         bool getBullet() const
00495         {
00496                 return _bullet;
00497         }
00498         
00499         const std::vector<int>& getTabStops() const
00500         {
00501                 return _tabStops;
00502         }
00503 
00504         bool isRestrict() const
00505         {
00506                 return _restrictDefined;
00507         }
00508         
00509         const std::string& getRestrict() const
00510         {
00511                 return _restrict;
00512         }
00513 
00514         size_t getScroll() const
00515         {
00516                 return _scroll;
00517         }
00518 
00519         size_t getMaxScroll() const
00520         {
00521                 return _maxScroll;
00522         }
00523 
00524         size_t getHScroll() const
00525         {
00526                 return _hScroll;
00527         }
00528 
00529         size_t getMaxHScroll() const
00530         {
00531                 return _maxHScroll;
00532         }
00533 
00534         size_t getBottomScroll() const
00535         {
00536                 return _bottomScroll;
00537         }
00538 
00539         void setUnderlined(bool v);
00540         void setTabStops(const std::vector<int>& tabStops);
00541         void setBullet(bool b);
00542         void setURL(std::string url);
00543         void setTarget(std::string target);
00544         void setRestrict(const std::string& restrict);
00545         void setDisplay(TextFormatDisplay display);
00546         void setScroll(size_t scroll) {
00547                 _scroll = scroll;
00548                 format_text();
00549         }
00550         void setMaxScroll(size_t maxScroll) {
00551                 _maxScroll = maxScroll;
00552                 format_text();
00553         }
00554         void setHScroll(size_t hScroll) {
00555                 _hScroll = hScroll;
00556                 format_text();
00557         }
00558         void setMaxHScroll(size_t maxHScroll) {
00559                 _maxHScroll = maxHScroll;
00560                 format_text();
00561         }
00562         void setbottomScroll(size_t bottomScroll) {
00563                 _bottomScroll = bottomScroll;
00564                 format_text();
00565         }
00566 
00568         //
00569         size_t cursorRecord();
00570 
00571         void setTextFormat(TextFormat_as& tf);
00572 
00573         const SWFRect& getTextBoundingBox() const {
00574                 return m_text_bounding_box;
00575         }
00576 
00578         //
00581         void setTextValue(const std::wstring& wstr);
00582 
00583 private:
00584 
00585     void init();
00586 
00589         //
00593         void updateText(const std::wstring& s);
00594 
00595         void updateHtmlText(const std::wstring& s);
00596 
00597     void insertTab(SWF::TextRecord& rec, boost::int32_t& x, float scale);
00598 
00600     //
00603         virtual bool handleFocus();
00604 
00606         virtual void killFocus();
00607 
00609         void onChanged();
00610 
00612         void reset_bounding_box(boost::int32_t x, boost::int32_t y)
00613         {
00614                 m_text_bounding_box.set_to_point(x, y);
00615         }
00616 
00619         void format_text();
00620         
00622         void scrollLines();
00623         
00626         void newLine(boost::int32_t& x, boost::int32_t& y, 
00627                                  SWF::TextRecord& rec, int& last_space_glyph,
00628                                  LineStarts::value_type& last_line_start_record, float div);
00629                                         
00631         void handleChar(std::wstring::const_iterator& it,
00632             const std::wstring::const_iterator& e, boost::int32_t& x,
00633             boost::int32_t& y, SWF::TextRecord& rec, int& last_code,
00634                     int& last_space_glyph,
00635             LineStarts::value_type& last_line_start_record);
00636         
00647         bool parseHTML(std::wstring& tag,
00648             std::map<std::string, std::string>& attributes,
00649             std::wstring::const_iterator& it,
00650             const std::wstring::const_iterator& e,
00651                         bool& selfclosing) const;
00652 
00657         float align_line(TextAlignment align, int last_line_start_record, float x);
00658 
00660         //
00676         void registerTextVariable();
00677 
00678         typedef std::pair<as_object*, ObjectURI> VariableRef;
00679 
00684         VariableRef parseTextVariableRef(const std::string& variableName) const;
00685 
00687         //
00690         void show_cursor(Renderer& renderer, const SWFMatrix& mat);
00691 
00693     //
00695     boost::intrusive_ptr<const SWF::DefineEditTextTag> _tag;
00696 
00698     //
00703         std::wstring _text;
00704 
00706         //
00707         std::wstring _htmlText;
00708 
00710         SWFRect m_text_bounding_box;
00711 
00712         typedef std::vector<SWF::TextRecord> TextRecords;
00713         TextRecords _textRecords;
00714 
00715         std::vector<size_t> _recordStarts;
00716 
00717         TextRecords _displayRecords;
00718 
00719         std::string _url;
00720         std::string _target;
00721         std::string _restrict;
00722         std::set<wchar_t> _restrictedchars;
00723         TextFormatDisplay _display;
00724         std::vector<int> _tabStops;
00725         LineStarts _line_starts;
00726 
00728         //
00732         std::string _variable_name;
00733 
00734         rgba _backgroundColor;
00735 
00736         rgba _borderColor;
00737 
00738         rgba _textColor;
00739         
00740         TextAlignment _alignment;
00741 
00742         boost::intrusive_ptr<const Font> _font;
00743         size_t m_cursor;
00744         size_t _glyphcount;
00745         size_t _scroll;
00746         size_t _maxScroll;
00747         size_t _hScroll;
00748         size_t _maxHScroll;
00749         size_t _bottomScroll;
00750         size_t _linesindisplay;
00751 
00753     size_t _maxChars;
00754         
00755     AutoSize _autoSize;
00756 
00757         TypeValue _type;
00758 
00760         //
00765         SWFRect _bounds;
00766 
00769     std::pair<size_t, size_t> _selection;
00770 
00771     boost::int16_t _leading;
00772         boost::uint16_t _indent;
00773 
00776         boost::uint16_t _blockIndent;
00777 
00778         boost::uint16_t _leftMargin;
00779 
00780         boost::uint16_t _rightMargin;
00781 
00782         boost::uint16_t _fontHeight;
00783 
00788         bool _textDefined;
00789 
00790         bool _restrictDefined;
00791         bool _underlined;
00792         bool _bullet;
00793 
00794         bool m_has_focus;
00795         
00796 
00798     bool _multiline;
00799 
00801     bool _password;
00802         
00804         //
00808         bool _text_variable_registered;
00809 
00810         bool _drawBackground;
00811 
00812         bool _drawBorder;
00813 
00814         bool _embedFonts;
00815 
00816         bool _wordWrap;
00817 
00818         bool _html;
00819 
00820         bool _selectable;
00821         
00822 };
00823 
00824 } // namespace gnash
00825 
00826 #endif