libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCTablePad.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:       NCTablePad.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef NCTablePad_h
00026 #define NCTablePad_h
00027 
00028 #include <iosfwd>
00029 #include <vector>
00030 #include <memory>               // auto_ptr
00031 
00032 #include "NCTableItem.h"
00033 #include "NCPad.h"
00034 #include "NCstring.h"
00035 
00036 class NCTableLine;
00037 class NCTableCol;
00038 
00039 
00040 class NCTableSortStrategyBase
00041 {
00042 public:
00043     NCTableSortStrategyBase( ) { _uiColumn = -1; }
00044 
00045     virtual ~NCTableSortStrategyBase() {}
00046 
00047     virtual void sort (
00048                        std::vector<NCTableLine *>::iterator itemsBegin,
00049                        std::vector<NCTableLine *>::iterator itemsEnd,
00050                        int  uiColumn
00051                        ) = 0;
00052     int getColumn ()                    { return _uiColumn; }
00053     void setColumn ( int column)        { _uiColumn = column; }
00054 
00055 private:
00056     int _uiColumn;
00057 
00058 };
00059 
00060 class NCTableSortDefault : public NCTableSortStrategyBase {
00061 public:
00062     virtual void sort (
00063                        std::vector<NCTableLine *>::iterator itemsBegin,
00064                        std::vector<NCTableLine *>::iterator itemsEnd,
00065                        int  uiColumn
00066                        )
00067         {
00068             std::sort ( itemsBegin, itemsEnd, Compare(uiColumn) );
00069         }
00070 
00071 private:
00072     class Compare
00073     {
00074     public:
00075         Compare ( int uiCol)
00076             : _uiCol ( uiCol )
00077             {}
00078 
00079         bool operator() ( NCTableLine * first,
00080                           NCTableLine * second
00081                           ) const
00082             {
00083                 std::wstring w1 = first->GetCol( _uiCol )->Label().getText().begin()->str();
00084                 std::wstring w2 = second->GetCol( _uiCol )->Label().getText().begin()->str();
00085                 wchar_t *endptr1 = 0;
00086                 wchar_t *endptr2 = 0;
00087 
00088                 long int number1 = std::wcstol( w1.data(), &endptr1, 10 );
00089                 long int number2 = std::wcstol( w2.data(), &endptr2, 10 );
00090 
00091                 // both are numbers
00092                 if ( w1.data() != endptr1 && w2.data() != endptr2 )
00093                 {
00094                     return number1 < number2;
00095                 }
00096                 else    // compare strings
00097                 {
00098                     int result = std::wcscoll ( w1.data(), w2.data() );
00099 
00100                     if ( result < 0 )
00101                         return true;
00102                     else
00103                         return false;
00104                 }
00105             }
00106 
00107     private:
00108         int _uiCol;
00109     };
00110 
00111 
00112 };
00113 
00114 class NCTableTag : public NCTableCol
00115 {
00116 private:
00117 
00118     YItem *yitem;
00119     bool selected;
00120 
00121 public:
00122 
00123     NCTableTag( YItem *item, const bool sel = false )
00124             : NCTableCol( NCstring( "[ ]" ), SEPARATOR )
00125             , yitem( item )
00126             , selected( sel )
00127     {
00128         //store pointer to this tag in Yitem data
00129         yitem->setData( this );
00130     }
00131 
00132     virtual ~NCTableTag() {}
00133 
00134     virtual void SetLabel( const NCstring & ) { /*NOOP*/; }
00135 
00136     virtual void DrawAt( NCursesWindow & w, const wrect at,
00137                          NCTableStyle & tableStyle,
00138                          NCTableLine::STATE linestate,
00139                          unsigned colidx ) const
00140     {
00141         NCTableCol::DrawAt( w, at, tableStyle, linestate, colidx );
00142 
00143         if ( selected )
00144         {
00145             setBkgd( w, tableStyle, linestate, DATA );
00146             w.addch( at.Pos.L, at.Pos.C + 1, 'x' );
00147         }
00148     }
00149 
00150     void SetSelected( const bool sel ) { selected = sel; }
00151 
00152     bool Selected() const              { return selected; }
00153 
00154     YItem *origItem() { return yitem; }
00155 };
00156 
00157 class NCTablePad : public NCPad
00158 {
00159 
00160     friend std::ostream & operator<<( std::ostream & STREAM, const NCTablePad & OBJ );
00161 
00162     NCTablePad & operator=( const NCTablePad & );
00163     NCTablePad( const NCTablePad & );
00164 
00165 private:
00166 
00167     NCursesPad  Headpad;
00168     bool        dirtyHead;
00169     bool        dirtyFormat;
00170 
00171     NCTableStyle         ItemStyle;
00172     NCTableLine          Headline;
00173     std::vector<NCTableLine*> Items;
00174     wpos                 citem;
00175 
00176     std::auto_ptr<NCTableSortStrategyBase> sortStrategy;
00177 
00178     void assertLine( unsigned idx );
00179 
00180 protected:
00181 
00182     void         DirtyFormat() { dirty = dirtyFormat = true; }
00183 
00184     virtual wsze UpdateFormat();
00185 
00186     virtual int  dirtyPad() { return setpos( CurPos() ); }
00187 
00188     virtual int  setpos( const wpos & newpos );
00189     virtual int  DoRedraw();
00190     virtual void updateScrollHint();
00191 
00192     virtual void directDraw( NCursesWindow & w, const wrect at, unsigned lineno );
00193 
00194 public:
00195 
00196     NCTablePad( int lines, int cols, const NCWidget & p );
00197     virtual ~NCTablePad();
00198 
00199 public:
00200 
00201     virtual void wRecoded();
00202 
00203     virtual wpos CurPos() const;
00204     virtual bool handleInput( wint_t key );
00205 
00206     bool setItemByKey( int key );
00207 
00208     wsze tableSize()
00209     {
00210         return dirtyFormat ? UpdateFormat()
00211                : wsze( Lines(), ItemStyle.TableWidth() );
00212     }
00213 
00214     void setOrder( int column, bool do_reverse = false );
00215 
00216 public:
00217 
00218     bool SetHeadline( const std::vector<NCstring> & head );
00219 
00220     virtual void SendHead()
00221     {
00222         SetHead( Headpad, srect.Pos.C );
00223         dirtyHead = false;
00224     }
00225 
00226     void SetSepChar( const chtype colSepchar )
00227     {
00228         ItemStyle.SetSepChar( colSepchar );
00229     }
00230 
00231     void SetSepWidth( const unsigned sepwidth )
00232     {
00233         ItemStyle.SetSepWidth( sepwidth );
00234     }
00235 
00236     void SetHotCol( const int hcol )
00237     {
00238         ItemStyle.SetHotCol( hcol );
00239     }
00240 
00241     unsigned Cols()  const { return ItemStyle.Cols(); }
00242 
00243     unsigned Lines() const { return Items.size(); }
00244 
00245     unsigned HotCol()const { return ItemStyle.HotCol(); }
00246 
00247     void     SetLines( unsigned idx );
00248     void     SetLines( std::vector<NCTableLine*> & nItems );
00249     void     ClearTable()  { SetLines( 0 ); }
00250 
00251     void Append( NCTableLine * item )           { AddLine( Lines(), item ); }
00252 
00253     void Append( std::vector<NCTableCol*> & nItems, int index = -1 )
00254     {
00255         AddLine( Lines(), new NCTableLine( nItems, index ) );
00256     }
00257 
00258     void AddLine( unsigned idx, NCTableLine * item );
00259     void DelLine( unsigned idx );
00260 
00261     const NCTableLine * GetLine( unsigned idx ) const;
00262     NCTableLine *       ModifyLine( unsigned idx );
00263 
00264     void stripHotkeys();
00265 
00266     void setSortStrategy ( NCTableSortStrategyBase * newSortStrategy ) // dyn. allocated
00267     {
00268         if ( newSortStrategy != 0 )
00269             sortStrategy.reset ( newSortStrategy );
00270     }
00271 };
00272 
00273 
00274 #endif // NCTablePad_h
 All Classes Functions Variables