Gnash  0.8.11dev
TextField.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_TEXTFIELD_H
20 #define GNASH_TEXTFIELD_H
21 
22 #include <boost/intrusive_ptr.hpp>
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 #include "InteractiveObject.h" // for inheritance
28 #include "LineStyle.h" // for LineStyle
29 #include "snappingrange.h"
30 #include "SWFRect.h" // for inlines
31 #include "GnashKey.h"
32 
33 // Forward declarations
34 namespace gnash {
35  namespace SWF {
36  class DefineEditTextTag;
37  class TextRecord;
38  }
39  class TextFormat_as;
40  class Font;
41 }
42 
43 namespace gnash {
44 
47 {
48 
49 public:
50 
51  typedef std::vector<size_t> LineStarts;
52 
55  {
60  };
61 
64  {
67  };
68 
70  enum AutoSize {
71 
74 
77 
80 
83  };
84 
86  enum TypeValue {
87 
90 
94 
97  };
98 
101  const SWF::DefineEditTextTag& def);
102 
104  //
108  TextField(as_object* object, DisplayObject* parent, const SWFRect& bounds);
109 
110 
111  ~TextField();
112 
113  // TODO: should this return isSelectable() ?
115  bool mouseEnabled() const { return true; }
116 
118  //
121  InteractiveObject* topmostMouseEntity(boost::int32_t x, boost::int32_t y);
122 
124  //
126  virtual int getDefinitionVersion() const;
127 
129  void mouseEvent(const event_id& id);
130 
132  void keyInput(key::code k);
133 
134  const std::string& getVariableName() const
135  {
136  return _variable_name;
137  }
138 
141  //
145  void set_variable_name(const std::string& newname);
146 
149  //
153  void updateText(const std::string& s);
154 
156  std::string get_text_value() const;
157 
159  std::string get_htmltext_value() const;
160 
162  bool getTextDefined() const { return _textDefined; }
163 
164  size_t getCaretIndex() const {
165  return m_cursor;
166  }
167 
169  //
172  const std::pair<size_t, size_t>& getSelection() const {
173  return _selection;
174  }
175 
177  //
179  void replaceSelection(const std::string& replace);
180 
182  //
185  //
188  void setSelection(int start, int end);
189 
191  //
193  virtual void setWidth(double width);
194 
196  //
198  virtual void setHeight(double height);
199 
201  virtual void display(Renderer& renderer, const Transform& xform);
202 
203  void add_invalidated_bounds(InvalidatedRanges& ranges, bool force);
204 
206  virtual SWFRect getBounds() const
207  {
208  return _bounds;
209  }
210 
211  // See dox in DisplayObject.h
212  bool pointInShape(boost::int32_t x, boost::int32_t y) const;
213 
215  bool getDrawBackground() const;
216 
218  //
220  void setDrawBackground(bool draw);
221 
223  rgba getBackgroundColor() const;
224 
226  //
230  void setBackgroundColor(const rgba& col);
231 
233  bool getDrawBorder() const;
234 
236  //
238  void setDrawBorder(bool draw);
239 
241  rgba getBorderColor() const;
242 
244  //
248  void setBorderColor(const rgba& col);
249 
251  const rgba& getTextColor() const
252  {
253  return _textColor;
254  }
255 
257  //
260  void setTextColor(const rgba& col);
261 
265  bool getEmbedFonts() const {
266  return _embedFonts;
267  }
268 
270  boost::int32_t maxChars() const {
271  return _maxChars;
272  }
273 
275  //
278  void maxChars(boost::int32_t max) {
279  _maxChars = max;
280  }
281 
283  bool multiline() const {
284  return _multiline;
285  }
286 
288  //
291  void multiline(bool b) {
292  _multiline = b;
293  }
294 
296  bool password() const {
297  return _password;
298  }
299 
301  //
304  void password(bool b) {
305  _password = b;
306  }
310  //
312  void setEmbedFonts(bool use);
313 
316  {
317  return _autoSize;
318  }
319 
322 
324  //
328  void setAutoSize(AutoSize val);
329 
331  //
335  void setType(TypeValue val) { if (val != typeInvalid) _type=val; }
336 
339  {
340  return _type;
341  }
342 
344  bool isReadOnly() const { return _type != typeInput; }
345 
347  //
353  static TypeValue parseTypeValue(const std::string& val);
354 
356  //
363  static const char* typeValueName(TypeValue val);
364 
369  bool doWordWrap() const {
370  return _wordWrap;
371  }
372 
374  //
381  void setWordWrap(bool on);
382 
384  bool doHtml() const {
385  return _html;
386  }
387 
389  //
392  void setHtml(bool on) {
393  _html = on;
394  }
395 
397  bool isSelectable() const
398  {
399  return _selectable;
400  }
401 
403  //
406  void setSelectable(bool v)
407  {
408  _selectable = v;
409  }
410 
411  // See DisplayObject::isActiveTextField
413  virtual bool isSelectableTextField() const
414  {
415  return isSelectable();
416  }
417 
419  //
426  void removeTextField();
427 
429  //
433  boost::intrusive_ptr<const Font> setFont(
434  boost::intrusive_ptr<const Font> newfont);
435 
436  const Font* getFont() { return _font.get(); }
437 
438 
439  boost::uint16_t getFontHeight() const
440  {
441  return _fontHeight;
442  }
443 
444  void setFontHeight(boost::uint16_t h);
445 
446  boost::uint16_t getLeftMargin() const
447  {
448  return _leftMargin;
449  }
450 
451  void setLeftMargin(boost::uint16_t h);
452 
453  boost::uint16_t getRightMargin() const
454  {
455  return _rightMargin;
456  }
457 
458  void setRightMargin(boost::uint16_t h);
459 
460  boost::uint16_t getIndent() const
461  {
462  return _indent;
463  }
464 
465  void setIndent(boost::uint16_t h);
466 
467  boost::uint16_t getBlockIndent() const
468  {
469  return _blockIndent;
470  }
471 
472  void setBlockIndent(boost::uint16_t h);
473 
475  {
476  return _alignment;
477  }
478 
480 
481  boost::int16_t getLeading() const
482  {
483  return _leading;
484  }
485 
486  void setLeading(boost::int16_t h);
487 
488  bool getUnderlined() const
489  {
490  return _underlined;
491  }
492 
494  {
495  return _display;
496  }
497 
498  bool getBullet() const
499  {
500  return _bullet;
501  }
502 
503  const std::vector<int>& getTabStops() const
504  {
505  return _tabStops;
506  }
507 
508  bool isRestrict() const
509  {
510  return _restrictDefined;
511  }
512 
513  const std::string& getRestrict() const
514  {
515  return _restrict;
516  }
517 
518  size_t getScroll() const
519  {
520  return _scroll;
521  }
522 
523  size_t getMaxScroll() const
524  {
525  return _maxScroll;
526  }
527 
528  size_t getHScroll() const
529  {
530  return _hScroll;
531  }
532 
533  size_t getMaxHScroll() const
534  {
535  return _maxHScroll;
536  }
537 
538  size_t getBottomScroll() const
539  {
540  return _bottomScroll;
541  }
542 
543  void setUnderlined(bool v);
544  void setTabStops(const std::vector<int>& tabStops);
545  void setBullet(bool b);
546  void setURL(std::string url);
547  void setTarget(std::string target);
548  void setRestrict(const std::string& restrict);
550  void setScroll(size_t scroll) {
551  _scroll = scroll;
552  format_text();
553  }
554  void setMaxScroll(size_t maxScroll) {
555  _maxScroll = maxScroll;
556  format_text();
557  }
558  void setHScroll(size_t hScroll) {
559  _hScroll = hScroll;
560  format_text();
561  }
562  void setMaxHScroll(size_t maxHScroll) {
563  _maxHScroll = maxHScroll;
564  format_text();
565  }
566  void setbottomScroll(size_t bottomScroll) {
567  _bottomScroll = bottomScroll;
568  format_text();
569  }
570 
572  //
573  size_t cursorRecord();
574 
575  void setTextFormat(TextFormat_as& tf);
576 
577  const SWFRect& getTextBoundingBox() const {
578  return m_text_bounding_box;
579  }
580 
582  //
585  void setTextValue(const std::wstring& wstr);
586 
587 private:
588 
589  void init();
590 
593  //
597  void updateText(const std::wstring& s);
598 
599  void updateHtmlText(const std::wstring& s);
600 
601  void insertTab(SWF::TextRecord& rec, boost::int32_t& x, float scale);
602 
604  //
607  virtual bool handleFocus();
608 
610  virtual void killFocus();
611 
613  void onChanged();
614 
616  void reset_bounding_box(boost::int32_t x, boost::int32_t y)
617  {
618  m_text_bounding_box.set_to_point(x, y);
619  }
620 
623  void format_text();
624 
626  void scrollLines();
627 
630  void newLine(boost::int32_t& x, boost::int32_t& y,
631  SWF::TextRecord& rec, int& last_space_glyph,
632  LineStarts::value_type& last_line_start_record, float div);
633 
635  void handleChar(std::wstring::const_iterator& it,
636  const std::wstring::const_iterator& e, boost::int32_t& x,
637  boost::int32_t& y, SWF::TextRecord& rec, int& last_code,
638  int& last_space_glyph,
639  LineStarts::value_type& last_line_start_record);
640 
651  bool parseHTML(std::wstring& tag,
652  std::map<std::string, std::string>& attributes,
653  std::wstring::const_iterator& it,
654  const std::wstring::const_iterator& e,
655  bool& selfclosing) const;
656 
661  float align_line(TextAlignment align, int last_line_start_record, float x);
662 
664  //
680  void registerTextVariable();
681 
682  typedef std::pair<as_object*, ObjectURI> VariableRef;
683 
688  VariableRef parseTextVariableRef(const std::string& variableName) const;
689 
691  //
694  void show_cursor(Renderer& renderer, const SWFMatrix& mat);
695 
697  //
699  boost::intrusive_ptr<const SWF::DefineEditTextTag> _tag;
700 
702  //
707  std::wstring _text;
708 
710  //
711  std::wstring _htmlText;
712 
714  SWFRect m_text_bounding_box;
715 
716  typedef std::vector<SWF::TextRecord> TextRecords;
717  TextRecords _textRecords;
718 
719  std::vector<size_t> _recordStarts;
720 
721  TextRecords _displayRecords;
722 
723  std::string _url;
724  std::string _target;
725  std::string _restrict;
726  std::set<wchar_t> _restrictedchars;
727  TextFormatDisplay _display;
728  std::vector<int> _tabStops;
729  LineStarts _line_starts;
730 
732  //
736  std::string _variable_name;
737 
738  rgba _backgroundColor;
739 
740  rgba _borderColor;
741 
742  rgba _textColor;
743 
744  TextAlignment _alignment;
745 
746  boost::intrusive_ptr<const Font> _font;
747  size_t m_cursor;
748  size_t _glyphcount;
749  size_t _scroll;
750  size_t _maxScroll;
751  size_t _hScroll;
752  size_t _maxHScroll;
753  size_t _bottomScroll;
754  size_t _linesindisplay;
755 
757  size_t _maxChars;
758 
759  AutoSize _autoSize;
760 
761  TypeValue _type;
762 
764  //
769  SWFRect _bounds;
770 
773  std::pair<size_t, size_t> _selection;
774 
775  boost::int16_t _leading;
776  boost::uint16_t _indent;
777 
780  boost::uint16_t _blockIndent;
781 
782  boost::uint16_t _leftMargin;
783 
784  boost::uint16_t _rightMargin;
785 
786  boost::uint16_t _fontHeight;
787 
792  bool _textDefined;
793 
794  bool _restrictDefined;
795  bool _underlined;
796  bool _bullet;
797 
798  bool m_has_focus;
799 
800 
802  bool _multiline;
803 
805  bool _password;
806 
808  //
812  bool _text_variable_registered;
813 
814  bool _drawBackground;
815 
816  bool _drawBorder;
817 
818  bool _embedFonts;
819 
820  bool _wordWrap;
821 
822  bool _html;
823 
824  bool _selectable;
825 
826 };
827 
828 } // namespace gnash
829 
830 #endif