libyui-ncurses
2.44.1
|
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: NCTableItem.h 00020 00021 Author: Michael Andres <ma@suse.de> 00022 00023 /-*/ 00024 00025 #ifndef NCTableItem_h 00026 #define NCTableItem_h 00027 00028 #include <iosfwd> 00029 #include <vector> 00030 00031 #include "position.h" 00032 #include "NCWidget.h" 00033 #include <yui/YTableItem.h> 00034 00035 class NCTableStyle; 00036 class NCTableCol; 00037 00038 00039 class NCTableLine 00040 { 00041 00042 friend std::ostream & operator<<( std::ostream & STREAM, const NCTableLine & OBJ ); 00043 00044 NCTableLine & operator=( const NCTableLine & ); 00045 NCTableLine( const NCTableLine & ); 00046 00047 public: 00048 00049 enum STATE 00050 { 00051 S_NORMAL = 0x00, 00052 S_ACTIVE = 0x01, 00053 S_DISABELED = 0x10, 00054 S_HIDDEN = 0x20, 00055 S_HEADLINE = 0x40 00056 }; 00057 00058 private: 00059 00060 std::vector<NCTableCol*> Items; 00061 void assertCol( unsigned idx ); 00062 00063 unsigned state; 00064 00065 int index; 00066 00067 YTableItem *yitem; 00068 00069 protected: 00070 00071 mutable STATE vstate; 00072 virtual void DrawItems( NCursesWindow & w, const wrect at, 00073 NCTableStyle & tableStyle, 00074 bool active ) const; 00075 00076 public: 00077 00078 NCTableLine( unsigned cols, int index = -1, const unsigned s = S_NORMAL ); 00079 NCTableLine( std::vector<NCTableCol*> & nItems, int index = -1, const unsigned s = S_NORMAL ); 00080 void setOrigItem( YTableItem *it ); 00081 YTableItem *origItem() const { return yitem; } 00082 00083 virtual ~NCTableLine(); 00084 00085 unsigned Cols() const { return Items.size(); } 00086 00087 void SetCols( unsigned idx ); 00088 void SetCols( std::vector<NCTableCol*> & nItems ); 00089 void ClearLine() { SetCols( 0 ); } 00090 00091 std::vector<NCTableCol*> GetItems() const { return Items; } 00092 00093 void Append( NCTableCol * item ) { AddCol( Cols(), item ); } 00094 00095 void AddCol( unsigned idx, NCTableCol * item ); 00096 void DelCol( unsigned idx ); 00097 00098 NCTableCol * GetCol( unsigned idx ); 00099 const NCTableCol * GetCol( unsigned idx ) const 00100 { 00101 return const_cast<NCTableLine*>( this )->GetCol( idx ); 00102 } 00103 00104 void SetState( const STATE s ) { state |= s; } 00105 00106 void ClearState( const STATE s ) { state &= ~s; } 00107 00108 bool isHidden() const { return ( state & S_HIDDEN ); } 00109 00110 bool isDisabeled() const { return ( state & S_DISABELED ); } 00111 00112 bool isSpecial() const { return ( state & ( S_HIDDEN | S_DISABELED ) ); } 00113 00114 bool isActive() const { return ( state & S_ACTIVE ); } 00115 00116 virtual bool isVisible() const { return !isHidden(); } 00117 00118 virtual bool isEnabeled() const { return isVisible() && !isDisabeled(); } 00119 00120 int getIndex() const { return index; } 00121 00122 public: 00123 00124 virtual int handleInput( wint_t key ) { return 0; } 00125 00126 virtual int ChangeToVisible() { return 0; } 00127 00128 virtual unsigned Hotspot( unsigned & at ) const { at = 0; return 0; } 00129 00130 virtual void UpdateFormat( NCTableStyle & TableStyle ); 00131 00132 virtual void DrawAt( NCursesWindow & w, const wrect at, 00133 NCTableStyle & tableStyle, 00134 bool active ) const; 00135 00136 void stripHotkeys(); 00137 }; 00138 00139 00140 00141 class NCTableCol 00142 { 00143 00144 friend std::ostream & operator<<( std::ostream & STREAM, const NCTableCol & OBJ ); 00145 00146 public: 00147 00148 enum STYLE 00149 { 00150 NONE = 0, // use current bg 00151 PLAIN, // plain text 00152 DATA, // data style 00153 ACTIVEDATA, // data style if line active, else plain 00154 HINT, // hint 00155 SEPARATOR // separator 00156 }; 00157 00158 private: 00159 00160 NClabel label; 00161 STYLE style; 00162 00163 public: 00164 00165 NCTableCol( const NCstring & l = "", const STYLE & st = ACTIVEDATA ); 00166 virtual ~NCTableCol(); 00167 00168 const NClabel & Label() const { return label; } 00169 00170 virtual void SetLabel( const NClabel & l ) { label = l; } 00171 00172 void stripHotkey() { label.stripHotkey(); } 00173 00174 protected: 00175 00176 chtype setBkgd( NCursesWindow & w, 00177 NCTableStyle & tableStyle, 00178 NCTableLine::STATE linestate, 00179 STYLE colstyle ) const ; 00180 00181 public: 00182 00183 virtual wsze Size() const { return wsze( 1, label.width() ); } 00184 00185 virtual void DrawAt( NCursesWindow & w, const wrect at, 00186 NCTableStyle & tableStyle, 00187 NCTableLine::STATE linestate, 00188 unsigned colidx ) const; 00189 00190 bool hasHotkey() const { return label.hasHotkey(); } 00191 00192 unsigned char hotkey() const { return label.hotkey(); } 00193 }; 00194 00195 00196 00197 class NCTableHead : public NCTableLine 00198 { 00199 00200 public: 00201 00202 NCTableHead( unsigned cols ) : NCTableLine( cols ) {} 00203 00204 NCTableHead( std::vector<NCTableCol*> & nItems ) : NCTableLine( nItems ) {} 00205 00206 virtual ~NCTableHead() {} 00207 00208 public: 00209 00210 virtual void DrawAt( NCursesWindow & w, const wrect at, 00211 NCTableStyle & tableStyle, 00212 bool active ) const; 00213 }; 00214 00215 00216 00217 class NCTableStyle 00218 { 00219 00220 friend std::ostream & operator<<( std::ostream & STREAM, const NCTableStyle & OBJ ); 00221 00222 private: 00223 00224 NCTableHead headline; 00225 std::vector<unsigned> colWidth; 00226 std::vector<NC::ADJUST> colAdjust; 00227 00228 const NCWidget & parw; 00229 00230 unsigned colSepwidth; 00231 chtype colSepchar; 00232 unsigned hotCol; 00233 00234 public: 00235 00236 static const chtype currentBG = ( chtype ) - 1; 00237 00238 NCTableStyle( const NCWidget & p ); 00239 ~NCTableStyle() {} 00240 00241 bool SetStyleFrom( const std::vector<NCstring> & head ); 00242 void SetSepChar( const chtype sepchar ) { colSepchar = sepchar; } 00243 00244 void SetSepWidth( const unsigned sepwidth ) { colSepwidth = sepwidth; } 00245 00246 void SetHotCol( const int hcol ) 00247 { 00248 hotCol = ( hcol < 0 || Cols() <= ( unsigned )hcol ) ? -1 : hcol; 00249 } 00250 00251 void ResetToMinCols() 00252 { 00253 colWidth.clear(); 00254 AssertMinCols( headline.Cols() ); 00255 headline.UpdateFormat( *this ); 00256 } 00257 00258 void AssertMinCols( unsigned num ) 00259 { 00260 if ( colWidth.size() < num ) 00261 { 00262 colWidth.resize( num, 0 ); 00263 colAdjust.resize( colWidth.size(), NC::LEFT ); 00264 } 00265 } 00266 00267 void MinColWidth( unsigned num, unsigned val ) 00268 { 00269 AssertMinCols( num ); 00270 00271 if ( val > colWidth[num] ) 00272 colWidth[num] = val; 00273 } 00274 00275 NC::ADJUST ColAdjust( unsigned num ) const { return colAdjust[num]; } 00276 00277 unsigned Cols() const { return colWidth.size(); } 00278 00279 unsigned ColWidth( unsigned num ) const { return colWidth[num]; } 00280 00281 unsigned ColSepwidth() const { return colSepwidth; } 00282 00283 chtype ColSepchar() const { return colSepchar; } 00284 00285 unsigned HotCol() const { return hotCol; } 00286 00287 const NCstyle::StList & listStyle() const { return parw.listStyle(); } 00288 00289 chtype getBG() const { return listStyle().item.plain; } 00290 00291 chtype getBG( const NCTableLine::STATE lstate, 00292 const NCTableCol::STYLE cstyle = NCTableCol::PLAIN ) const; 00293 00294 chtype highlightBG( const NCTableLine::STATE lstate, 00295 const NCTableCol::STYLE cstyle, 00296 const NCTableCol::STYLE dstyle = NCTableCol::PLAIN ) const ; 00297 00298 chtype hotBG( const NCTableLine::STATE lstate, unsigned colidx ) const 00299 { 00300 return ( colidx == hotCol ) ? getBG( lstate, NCTableCol::HINT ) : currentBG; 00301 } 00302 00303 const NCTableLine & Headline() const { return headline; } 00304 00305 unsigned TableWidth() const 00306 { 00307 unsigned twidth = 0; 00308 00309 for ( unsigned i = 0; i < Cols(); ++i ) 00310 twidth += colWidth[i]; 00311 00312 if ( Cols() > 1 ) 00313 twidth += colSepwidth * ( Cols() - 1 ); 00314 00315 return twidth; 00316 } 00317 }; 00318 00319 00320 #endif // NCTableItem_h