Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

list_box.h

Go to the documentation of this file.
00001 // Author:  Daniel Albuschat ( daniel@viming.de ) 
00002 // File:    list_box.h
00003 // Date:    Tue May 13 20:48:22 20030
00004 // Content:  
00005 
00006 #ifndef LIST_BOX_H_1640654124
00007 #define LIST_BOX_H_1640654124
00008 #include "base_control.h"
00009 #include "events.h"
00010 #include <string>
00011 #include <vector>
00012 
00013 namespace crs {
00014 
00015   template< class T>
00016     class list_box: public focused_ctrl, public styled_ctrl {
00017       public:
00018         struct items {
00019           std::string text;
00020           T           value;
00021           items( const std::string Text, const T Value ): text(Text), value(Value){
00022           }
00023         };
00024         typedef std::vector<items> val_list;
00025       private:
00026         val_list mList;
00027         typename val_list::const_iterator mSelected;
00028         const val_list &getList() {
00029           return mList;
00030         }
00031         void setList( const val_list &List ) {
00032           mList = List;
00033           mSelected = mList.begin();
00034         }
00035         const typename val_list::const_iterator &getSelected() {
00036           return mSelected;
00037         }
00038         void setSelected( const typename val_list::const_iterator &it ) {
00039           mSelected = it;
00040         }
00041       public:
00042 
00043         list_box( base_control *Parent, window_events *Events=0, 
00044             const unsigned int Left=0, const unsigned int Top=0, const unsigned int Width=0, const unsigned int Height=0, 
00045             style::styles Style = style::rect, chtype clb = '[', chtype crb = ']' ): 
00046           focused_ctrl( Parent, Events, Left, Top, Width, Height ),
00047         styled_ctrl( Style, clb, crb ),
00048         list( this, &list_box::getList, &list_box::setList ),
00049         selected( this, &list_box::getSelected, &list_box::setSelected ) {
00050           mSelected = mList.begin();
00051         }
00052 
00053 
00054         list_box( base_control *Parent, const val_list List, window_events *Events=0, 
00055             const unsigned int Left=0, const unsigned int Top=0, const unsigned int Width=0, const unsigned int Height=0, 
00056             style::styles Style = style::rect, chtype clb = '[', chtype crb = ']' ): 
00057           focused_ctrl( Parent, Events, Left, Top, Width, Height ),
00058         styled_ctrl( Style, clb, crb ),
00059         mList(List),
00060         mSelected(mList.begin()),
00061         list( this, &list_box::getList, &list_box::setList ),
00062         selected( this, &list_box::getSelected, &list_box::setSelected ) {
00063         }
00064         virtual void draw() {
00065           buffer.fill(' ');
00066           int c=0;
00067           for( typename std::vector< items >::iterator i = mList.begin(); i != mList.end(); i++ ) {
00068             c++;
00069             buffer.setText( i->text, 2, c );
00070             if( i == mSelected ) {
00071               buffer[ mWidth * c + 1 ] = sc::getLeftBorder( mStyle, custom_left_border );
00072               buffer[ mWidth * (c + 1) - 2 ] = sc::getRightBorder( mStyle, custom_right_border );
00073             }
00074           }
00075           buffer.drawHLine( 1, 0, mWidth - 2, sc::HorizontalLine );           // upper border
00076           buffer.drawHLine( 1, mHeight - 1, mWidth - 2, sc::HorizontalLine ); // lower border
00077           buffer[ 0 ] = sc::UpperLeftCorner;                                  // upper left corner
00078           buffer[ mWidth - 1 ] = sc::UpperRightCorner;                        // upper right corner
00079           buffer[ ( mHeight - 1 ) * mWidth ] = sc::LowerLeftCorner;           // lower left corner
00080           buffer[ mHeight * mWidth - 1 ] = sc::LowerRightCorner;              // lower right corner
00081           buffer.drawVLine( 0, 1, mHeight - 2, sc::VerticalLine );            // left border
00082           buffer.drawVLine( mWidth - 1, 1, mHeight - 2, sc::VerticalLine );   // right border
00083         }
00084         virtual bool keyPress( int key ) {
00085           switch(key) {
00086             case KEY_DOWN:
00087               if( mSelected != mList.end()-1 )
00088                 mSelected++;
00089               break;
00090             case KEY_UP:
00091               if( mSelected != mList.begin() )
00092                 mSelected--;
00093               break;
00094             default:
00095               return true;
00096           }
00097           return false;
00098         }
00099         typename val_list::const_iterator push_back( const items Items ) {
00100           mSelected = mList.insert( mList.begin(), Items );
00101           return mSelected;
00102         }
00103         property< list_box, read_write, val_list > list;
00104         property< list_box, read_write, typename val_list::const_iterator > selected;
00105     };
00106 
00107 }
00108 
00109 #endif
00110 

Generated on Mon May 19 20:36:04 2003 for cursive by doxygen1.2.18