Gnash  0.8.10
TextField.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   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 #include "GnashKey.h"
00032 
00033 // Forward declarations
00034 namespace gnash {
00035     namespace SWF {
00036         class DefineEditTextTag;
00037         class TextRecord;
00038     }
00039     class TextFormat_as;
00040     class Font;
00041 }
00042 
00043 namespace gnash {
00044 
00046 class TextField : public InteractiveObject
00047 {
00048 
00049 public:
00050     
00051     typedef std::vector<size_t> LineStarts;
00052 
00054         enum TextAlignment
00055         {
00056                 ALIGN_LEFT = 0,
00057                 ALIGN_RIGHT,
00058                 ALIGN_CENTER,
00059                 ALIGN_JUSTIFY
00060         };
00061 
00063         enum TextFormatDisplay
00064         {
00065                 TEXTFORMAT_BLOCK = 0,
00066                 TEXTFORMAT_INLINE = 1
00067         };
00068         
00070         enum AutoSize {
00071 
00073                 AUTOSIZE_NONE,
00074 
00076                 AUTOSIZE_LEFT,
00077 
00079                 AUTOSIZE_CENTER,
00080 
00082                 AUTOSIZE_RIGHT
00083         };
00084 
00086         enum TypeValue {
00087 
00089                 typeInvalid,
00090 
00093                 typeDynamic,
00094 
00096                 typeInput
00097         };
00098 
00100         TextField(as_object* object, DisplayObject* parent,
00101             const SWF::DefineEditTextTag& def);
00102 
00104     //
00108     TextField(as_object* object, DisplayObject* parent, const SWFRect& bounds);
00109 
00110 
00111         ~TextField();
00112 
00113         // TODO: should this return isSelectable() ?
00115         bool mouseEnabled() const { return true; }
00116 
00118         //
00121         InteractiveObject* topmostMouseEntity(boost::int32_t x, boost::int32_t y);
00122 
00124     //
00126     virtual int getDefinitionVersion() const;
00127 
00129         void mouseEvent(const event_id& id);    
00130 
00132     void keyInput(key::code k);
00133 
00134         const std::string& getVariableName() const
00135         {
00136                 return _variable_name;
00137         }
00138 
00141         //
00145         void set_variable_name(const std::string& newname);
00146         
00149         //
00153         void updateText(const std::string& s);
00154 
00156         std::string get_text_value() const;
00157 
00159         std::string get_htmltext_value() const;
00160 
00162         bool getTextDefined() const { return _textDefined; }
00163 
00164     size_t getCaretIndex() const {
00165         return m_cursor;
00166     }
00167 
00169     //
00172     const std::pair<size_t, size_t>& getSelection() const {
00173         return _selection;
00174     }
00175 
00177         //
00179     void replaceSelection(const std::string& replace);
00180 
00182     //
00185     //
00188     void setSelection(int start, int end);
00189 
00191     //
00193     virtual void setWidth(double width);
00194     
00196     //
00198     virtual void setHeight(double height);
00199 
00201         virtual void display(Renderer& renderer, const Transform& xform);
00202 
00203         void add_invalidated_bounds(InvalidatedRanges& ranges, bool force);
00204 
00206         virtual SWFRect getBounds() const
00207         {
00208                 return _bounds;
00209         }
00210 
00211         // See dox in DisplayObject.h
00212         bool pointInShape(boost::int32_t x, boost::int32_t y) const;
00213 
00215         bool getDrawBackground() const;
00216 
00218         //
00220         void setDrawBackground(bool draw);
00221 
00223         rgba getBackgroundColor() const;
00224 
00226         //
00230         void setBackgroundColor(const rgba& col);
00231 
00233         bool getDrawBorder() const;
00234 
00236         //
00238         void setDrawBorder(bool draw);
00239 
00241         rgba getBorderColor() const;
00242 
00244         //
00248         void setBorderColor(const rgba& col);
00249 
00251         const rgba& getTextColor() const 
00252         {
00253                 return _textColor;
00254         }
00255 
00257         //
00260         void setTextColor(const rgba& col);
00261 
00265         bool getEmbedFonts() const {
00266                 return _embedFonts;
00267         }
00268 
00270     boost::int32_t maxChars() const {
00271         return _maxChars;
00272     }
00273 
00275         //
00278     void maxChars(boost::int32_t max) {
00279         _maxChars = max;
00280     }
00281 
00283     bool multiline() const {
00284         return _multiline;
00285     }
00286 
00288         //
00291     void multiline(bool b) {
00292         _multiline = b;
00293     }
00294         
00296     bool password() const {
00297         return _password;
00298     }
00299 
00301         //
00304     void password(bool b) {
00305         _password = b;
00306     }
00310         //
00312         void setEmbedFonts(bool use);
00313 
00315         AutoSize getAutoSize() const
00316         {
00317                 return _autoSize;
00318         }
00319 
00321     TextAlignment getTextAlignment();
00322 
00324         //
00328         void setAutoSize(AutoSize val);
00329 
00331         //
00335         void setType(TypeValue val) { if (val != typeInvalid) _type=val; }
00336 
00338         TypeValue getType() const
00339         {
00340                 return _type;
00341         }
00342 
00344         bool isReadOnly() const { return _type != typeInput; }
00345 
00347         //
00353         static TypeValue parseTypeValue(const std::string& val);
00354 
00356         //
00363         static const char* typeValueName(TypeValue val);
00364 
00369         bool doWordWrap() const {
00370                 return _wordWrap;
00371         }
00372 
00374         //
00381         void setWordWrap(bool on);
00382 
00384         bool doHtml() const {
00385                 return _html;
00386         }
00387 
00389         //
00392         void setHtml(bool on) {
00393                 _html = on;
00394         }
00395 
00397         bool isSelectable() const
00398         {
00399                 return _selectable;
00400         }
00401 
00403         //
00406         void setSelectable(bool v) 
00407         {
00408                 _selectable = v;
00409         }
00410 
00411         // See DisplayObject::isActiveTextField
00413         virtual bool isSelectableTextField() const
00414         {
00415                 return isSelectable();
00416         }
00417 
00419         //
00426         void removeTextField();
00427 
00429         //
00433         boost::intrusive_ptr<const Font> setFont(
00434             boost::intrusive_ptr<const Font> newfont);
00435 
00436         const Font* getFont() { return _font.get(); }
00437 
00438         
00439         boost::uint16_t getFontHeight() const
00440         {
00441                 return _fontHeight;
00442         }
00443 
00444         void setFontHeight(boost::uint16_t h);
00445 
00446         boost::uint16_t getLeftMargin() const
00447         {
00448                 return _leftMargin;
00449         }
00450 
00451         void setLeftMargin(boost::uint16_t h);
00452 
00453         boost::uint16_t getRightMargin() const
00454         {
00455                 return _rightMargin;
00456         }
00457 
00458         void setRightMargin(boost::uint16_t h);
00459 
00460         boost::uint16_t getIndent() const
00461         {
00462                 return _indent;
00463         }
00464 
00465         void setIndent(boost::uint16_t h);
00466 
00467         boost::uint16_t getBlockIndent() const
00468         {
00469                 return _blockIndent;
00470         }
00471 
00472         void setBlockIndent(boost::uint16_t h);
00473 
00474         TextAlignment getAlignment() const
00475         {
00476                 return _alignment;
00477         }
00478 
00479         void setAlignment(TextAlignment h);
00480 
00481         boost::int16_t getLeading() const
00482         {
00483                 return _leading;
00484         }
00485 
00486         void setLeading(boost::int16_t h);
00487 
00488         bool getUnderlined() const
00489         {
00490                 return _underlined;
00491         }
00492         
00493         TextFormatDisplay getDisplay() const
00494         { 
00495                 return _display;
00496         }
00497         
00498         bool getBullet() const
00499         {
00500                 return _bullet;
00501         }
00502         
00503         const std::vector<int>& getTabStops() const
00504         {
00505                 return _tabStops;
00506         }
00507 
00508         bool isRestrict() const
00509         {
00510                 return _restrictDefined;
00511         }
00512         
00513         const std::string& getRestrict() const
00514         {
00515                 return _restrict;
00516         }
00517 
00518         size_t getScroll() const
00519         {
00520                 return _scroll;
00521         }
00522 
00523         size_t getMaxScroll() const
00524         {
00525                 return _maxScroll;
00526         }
00527 
00528         size_t getHScroll() const
00529         {
00530                 return _hScroll;
00531         }
00532 
00533         size_t getMaxHScroll() const
00534         {
00535                 return _maxHScroll;
00536         }
00537 
00538         size_t getBottomScroll() const
00539         {
00540                 return _bottomScroll;
00541         }
00542 
00543         void setUnderlined(bool v);
00544         void setTabStops(const std::vector<int>& tabStops);
00545         void setBullet(bool b);
00546         void setURL(std::string url);
00547         void setTarget(std::string target);
00548         void setRestrict(const std::string& restrict);
00549         void setDisplay(TextFormatDisplay display);
00550         void setScroll(size_t scroll) {
00551                 _scroll = scroll;
00552                 format_text();
00553         }
00554         void setMaxScroll(size_t maxScroll) {
00555                 _maxScroll = maxScroll;
00556                 format_text();
00557         }
00558         void setHScroll(size_t hScroll) {
00559                 _hScroll = hScroll;
00560                 format_text();
00561         }
00562         void setMaxHScroll(size_t maxHScroll) {
00563                 _maxHScroll = maxHScroll;
00564                 format_text();
00565         }
00566         void setbottomScroll(size_t bottomScroll) {
00567                 _bottomScroll = bottomScroll;
00568                 format_text();
00569         }
00570 
00572         //
00573         size_t cursorRecord();
00574 
00575         void setTextFormat(TextFormat_as& tf);
00576 
00577         const SWFRect& getTextBoundingBox() const {
00578                 return m_text_bounding_box;
00579         }
00580 
00582         //
00585         void setTextValue(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*, ObjectURI> 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 _restrictDefined;
00795         bool _underlined;
00796         bool _bullet;
00797 
00798         bool m_has_focus;
00799         
00800 
00802     bool _multiline;
00803 
00805     bool _password;
00806         
00808         //
00812         bool _text_variable_registered;
00813 
00814         bool _drawBackground;
00815 
00816         bool _drawBorder;
00817 
00818         bool _embedFonts;
00819 
00820         bool _wordWrap;
00821 
00822         bool _html;
00823 
00824         bool _selectable;
00825         
00826 };
00827 
00828 } // namespace gnash
00829 
00830 #endif