libyui-ncurses  2.44.1
 All Classes Functions Variables
NCRichText.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCRichText.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef NCRichText_h
26 #define NCRichText_h
27 
28 #include <iosfwd>
29 #include <stack>
30 
31 #include <yui/YRichText.h>
32 #include "NCPadWidget.h"
33 
34 
35 class NCRichText : public YRichText, public NCPadWidget
36 {
37 private:
38 
39  friend std::ostream & operator<<( std::ostream & STREAM, const NCRichText & OBJ );
40 
41  NCRichText & operator=( const NCRichText & );
42  NCRichText( const NCRichText & );
43 
44  /**
45  * Lookup std::map for character entities (e.g. '&gt;'). Initialized
46  * and used by entityLookup.
47  **/
48  static std::map<std::wstring, std::wstring> _charentity;
49 
50  /**
51  * Lookup and return replacement for a character entity. Expects
52  * the leading <code>'&'</code> and trailing <code>';'<.code> to
53  * be stripped from <code>val_r</code>. Returns <code>NULL</code>,
54  * if the character entity should not be replaced.
55  **/
56  static const std::wstring entityLookup( const std::wstring & val_r );
57 
58  /**
59  * Lookup and replace all replacements for a character entity.
60  **/
61  static const std::wstring filterEntities( const std::wstring & text );
62 
63 private:
64 
65  NCstring text;
66 
67  bool plainText;
68 
69  unsigned textwidth;
70  unsigned cl;
71  unsigned cc;
72  unsigned cindent;
73  bool atbol;
74 
75  bool preTag; // <pre> tag
76 
77  unsigned Tattr;
78 
79  static const unsigned Tfontmask = 0xff00;
80  enum TOKEN
81  {
82  T_UNKNOWN = 0x0000,
83  T_IGNORE = 0x0001,
84  T_BR = 0x0002,
85  T_PAR = 0x0004,
86  T_LEVEL = 0x0008,
87  T_LI = 0x0010,
88  T_PLAIN = 0x0012,
89  // font
90  T_BOLD = 0x0100,
91  T_IT = 0x0200,
92  T_TT = 0x0400,
93  T_ANC = 0x0800,
94  T_HEAD = 0x1000
95  };
96 
97 private:
98 
99  static const unsigned listindent;
100  static const std::wstring listleveltags;
101 
102  std::stack<int> liststack;
103 
104  void PadChangeLevel( bool down, int tag );
105  void PadSetLevel();
106  size_t textWidth( std::wstring wstr );
107 
108 private:
109 
110  class Anchor
111  {
112 
113  public:
114 
115  static const unsigned unset = ( unsigned ) - 1;
116 
117  unsigned sline;
118  unsigned scol;
119  unsigned eline;
120  unsigned ecol;
121 
122  std::wstring target;
123 
124  Anchor()
125  {
126  sline = scol = eline = ecol = unset;
127  }
128 
129  Anchor( int sl, int sc )
130  {
131  open( sl, sc );
132  }
133 
134  void open( int sl, int sc )
135  {
136  sline = sl;
137  scol = sc;
138  eline = ecol = unset;
139  target = L"";
140  }
141 
142  void close( int el, int ec )
143  {
144  eline = el;
145  ecol = ec;
146  }
147 
148  bool valid()
149  {
150  if ( sline == unset || scol == unset
151  || eline == unset || ecol == unset )
152  return false;
153 
154  if (( eline == sline && ecol <= scol )
155  || eline < sline )
156  return false;
157 
158  return true;
159  }
160 
161  bool within( unsigned firstvisible, unsigned nextinvisible )
162  {
163  return sline < nextinvisible && eline >= firstvisible;
164  }
165 
166  void draw( NCPad & pad, const chtype attr, int color );
167  };
168 
169  static const bool showLinkTarget;
170 
171  Anchor canchor;
172  std::vector<Anchor> anchors;
173  unsigned armed;
174 
175  unsigned vScrollFirstvisible;
176  unsigned vScrollNextinvisible;
177 
178  void openAnchor( std::wstring args );
179  void closeAnchor();
180 
181  void arm( unsigned i );
182  void disarm() { arm( Anchor::unset ); }
183 
184 private:
185 
186  void PadSetAttr();
187 
188  void DrawPlainPad();
189  void DrawHTMLPad();
190 
191  void PadNL();
192  void PadBOL();
193  void PadWS( const bool tab = false );
194  void PadTXT( const wchar_t * sch, const unsigned len );
195  void PadPreTXT( const wchar_t * sch, const unsigned len );
196  void AdjustPrePad( const wchar_t * sch );
197  bool PadTOKEN( const wchar_t * sch, const wchar_t *& ech );
198 
199 protected:
200 
201  virtual const char * location() const { return "NCRichText"; }
202 
203  virtual void wRedraw();
204  virtual void wRecoded();
205 
206  virtual NCPad * CreatePad();
207  virtual void DrawPad();
208 
209  virtual void HScroll( unsigned total, unsigned visible, unsigned start );
210  virtual void VScroll( unsigned total, unsigned visible, unsigned start );
211 
212  virtual bool handleInput( wint_t key );
213 
214 public:
215 
216  NCRichText( YWidget * parent, const std::string & text,
217  bool plainTextMode = false );
218  virtual ~NCRichText();
219 
220  virtual int preferredWidth();
221  virtual int preferredHeight();
222 
223  virtual void setSize( int newWidth, int newHeight );
224 
225  virtual void setLabel( const std::string & nlabel );
226 
227  virtual NCursesEvent wHandleInput( wint_t key );
228 
229  virtual void setValue( const std::string & ntext );
230 
231  virtual void setEnabled( bool do_bv );
232 
233  virtual bool setKeyboardFocus()
234  {
235  if ( !grabFocus() )
236  return YWidget::setKeyboardFocus();
237 
238  return true;
239  }
240 };
241 
242 
243 #endif // NCRichText_h
virtual void setEnabled(bool do_bv)
Definition: NCRichText.cc:204
Definition: NCPad.h:93