00001
00002
00003
00004
00005
00006 #ifndef EDIT_CTRL_H_436961225
00007 #define EDIT_CTRL_H_436961225
00008 #include "base_control.h"
00009 #include "events.h"
00010 #include "cursive.h"
00011 #include <ncurses.h>
00012 #include <string>
00013 #include <cctype>
00014
00015 namespace crs {
00016
00017 class edit_events: public window_events {
00018 public:
00019 virtual void onChange();
00020 };
00021
00022 class text_ctrl: public focused_ctrl {
00023 private:
00024 const std::string &getText();
00025 void setText_( const std::string &Text );
00026 protected:
00027 std::string mText;
00028 virtual void doSetText( const std::string &Text );
00029 virtual void doGetText();
00030 public:
00031 text_ctrl( base_control *Parent, window_events *Events,
00032 unsigned int Left, unsigned int Top, unsigned int Width, unsigned int Height );
00033 property< text_ctrl, read_write, std::string > text;
00034 };
00035
00036 template< typename T > class edit_ctrl: public text_ctrl, public styled_ctrl {
00037 public:
00038 typedef T (*transformator)( std::string );
00039 protected:
00040 transformator trans;
00041 public:
00042 edit_ctrl( base_control *Parent, edit_events *Events, unsigned int Left,
00043 unsigned int Top, unsigned int Length, T *Var, transformator Transform,
00044 const style::styles Style = rect, const char custom_left='[', const char custom_right=']' ):
00045 text_ctrl( Parent, Events, Left, Top, Length, 1 ),
00046 styled_ctrl( Style, custom_left, custom_right ), trans( Transform ) {
00047 custom_left_border = custom_left;
00048 custom_right_border = custom_right;
00049 }
00050 virtual void draw() {
00051 buffer.drawHLine( 1, 0, mWidth - 2, '_' );
00052 buffer.setText( mText, 1, 0 );
00053 buffer[ 0 ] = sc::getLeftBorder( mStyle, custom_left_border );
00054 buffer[ mWidth - 1 ] = sc::getRightBorder( mStyle, custom_right_border );
00055 }
00056 virtual bool keyPress( const int key ) {
00057 focused_ctrl::keyPress( key );
00058 if( std::isprint( key ) ) {
00059 mText += char(key);
00060 doChange();
00061 return false;
00062 } else {
00063 switch( key ) {
00064 case KEY_BACKSPACE:
00065 if( mText.size() > 0 ) {
00066 mText = mText.substr( 0, mText.size() - 1 );
00067 doChange();
00068 }
00069 break;
00070 }
00071 return true;
00072 }
00073 }
00074 virtual void doChange() {
00075 mCursorpos.x = mText.size() + 1 < mWidth - 1 ? mText.size() + 1 : mWidth - 2;
00076 if( events ) {
00077 static_cast<edit_events*>(events)->onChange();
00078 }
00079 }
00080 };
00081
00082 }
00083
00084 #endif
00085