libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCRichText.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       NCRichText.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef NCRichText_h
00026 #define NCRichText_h
00027 
00028 #include <iosfwd>
00029 #include <stack>
00030 
00031 #include <yui/YRichText.h>
00032 #include "NCPadWidget.h"
00033 
00034 
00035 class NCRichText : public YRichText, public NCPadWidget
00036 {
00037 private:
00038 
00039     friend std::ostream & operator<<( std::ostream & STREAM, const NCRichText & OBJ );
00040 
00041     NCRichText & operator=( const NCRichText & );
00042     NCRichText( const NCRichText & );
00043 
00044     /**
00045      * Lookup std::map for character entities (e.g. '&gt;'). Initialized
00046      * and used by entityLookup.
00047      **/
00048     static std::map<std::wstring, std::wstring> _charentity;
00049 
00050     /**
00051      * Lookup and return replacement for a character entity. Expects
00052      * the leading <code>'&'</code> and trailing <code>';'<.code> to
00053      * be stripped from <code>val_r</code>. Returns <code>NULL</code>,
00054      * if the character entity should not be replaced.
00055      **/
00056     static const std::wstring entityLookup( const std::wstring & val_r );
00057 
00058     /**
00059      * Lookup and replace all replacements for a character entity.
00060      **/
00061     static const std::wstring filterEntities( const std::wstring & text );
00062 
00063 private:
00064 
00065     NCstring text;
00066 
00067     bool plainText;
00068 
00069     unsigned textwidth;
00070     unsigned cl;
00071     unsigned cc;
00072     unsigned cindent;
00073     bool     atbol;
00074 
00075     bool     preTag;            // <pre> tag
00076 
00077     unsigned Tattr;
00078 
00079     static const unsigned Tfontmask = 0xff00;
00080     enum TOKEN
00081     {
00082         T_UNKNOWN = 0x0000,
00083         T_IGNORE  = 0x0001,
00084         T_BR      = 0x0002,
00085         T_PAR     = 0x0004,
00086         T_LEVEL   = 0x0008,
00087         T_LI      = 0x0010,
00088         T_PLAIN = 0x0012,
00089         // font
00090         T_BOLD    = 0x0100,
00091         T_IT      = 0x0200,
00092         T_TT      = 0x0400,
00093         T_ANC     = 0x0800,
00094         T_HEAD    = 0x1000
00095     };
00096 
00097 private:
00098 
00099     static const unsigned listindent;
00100     static const std::wstring   listleveltags;
00101 
00102     std::stack<int> liststack;
00103 
00104     void PadChangeLevel( bool down, int tag );
00105     void PadSetLevel();
00106     size_t textWidth( std::wstring wstr );
00107 
00108 private:
00109 
00110     class Anchor
00111     {
00112 
00113     public:
00114 
00115         static const unsigned unset = ( unsigned ) - 1;
00116 
00117         unsigned sline;
00118         unsigned scol;
00119         unsigned eline;
00120         unsigned ecol;
00121 
00122         std::wstring target;
00123 
00124         Anchor()
00125         {
00126             sline = scol = eline = ecol = unset;
00127         }
00128 
00129         Anchor( int sl, int sc )
00130         {
00131             open( sl, sc );
00132         }
00133 
00134         void open( int sl, int sc )
00135         {
00136             sline = sl;
00137             scol  = sc;
00138             eline = ecol = unset;
00139             target = L"";
00140         }
00141 
00142         void close( int el, int ec )
00143         {
00144             eline = el;
00145             ecol  = ec;
00146         }
00147 
00148         bool valid()
00149         {
00150             if ( sline == unset || scol == unset
00151                  || eline == unset || ecol == unset )
00152                 return false;
00153 
00154             if (( eline == sline && ecol <= scol )
00155                 || eline < sline )
00156                 return false;
00157 
00158             return true;
00159         }
00160 
00161         bool within( unsigned firstvisible, unsigned nextinvisible )
00162         {
00163             return sline < nextinvisible && eline >= firstvisible;
00164         }
00165 
00166         void draw( NCPad & pad, const chtype attr, int color );
00167     };
00168 
00169     static const bool showLinkTarget;
00170 
00171     Anchor              canchor;
00172     std::vector<Anchor> anchors;
00173     unsigned            armed;
00174 
00175     unsigned vScrollFirstvisible;
00176     unsigned vScrollNextinvisible;
00177 
00178     void openAnchor( std::wstring args );
00179     void closeAnchor();
00180 
00181     void arm( unsigned i );
00182     void disarm() { arm( Anchor::unset ); }
00183 
00184 private:
00185 
00186     void PadSetAttr();
00187 
00188     void DrawPlainPad();
00189     void DrawHTMLPad();
00190 
00191     void PadNL();
00192     void PadBOL();
00193     void PadWS( const bool tab = false );
00194     void PadTXT( const wchar_t * sch, const unsigned len );
00195     void PadPreTXT( const wchar_t * sch, const unsigned len );
00196     void AdjustPrePad( const wchar_t * sch );
00197     bool PadTOKEN( const wchar_t * sch, const wchar_t *& ech );
00198 
00199 protected:
00200 
00201     virtual const char * location() const { return "NCRichText"; }
00202 
00203     virtual void wRedraw();
00204     virtual void wRecoded();
00205 
00206     virtual NCPad * CreatePad();
00207     virtual void    DrawPad();
00208 
00209     virtual void HScroll( unsigned total, unsigned visible, unsigned start );
00210     virtual void VScroll( unsigned total, unsigned visible, unsigned start );
00211 
00212     virtual bool handleInput( wint_t key );
00213 
00214 public:
00215 
00216     NCRichText( YWidget * parent, const std::string & text,
00217                 bool plainTextMode = false );
00218     virtual ~NCRichText();
00219 
00220     virtual int preferredWidth();
00221     virtual int preferredHeight();
00222 
00223     virtual void setSize( int newWidth, int newHeight );
00224 
00225     virtual void setLabel( const std::string & nlabel );
00226 
00227     virtual NCursesEvent wHandleInput( wint_t key );
00228 
00229     virtual void setValue( const std::string & ntext );
00230 
00231     virtual void setEnabled( bool do_bv );
00232 
00233     virtual bool setKeyboardFocus()
00234     {
00235         if ( !grabFocus() )
00236             return YWidget::setKeyboardFocus();
00237 
00238         return true;
00239     }
00240 };
00241 
00242 
00243 #endif // NCRichText_h
 All Classes Functions Variables