00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #if !defined(SJEDIT_H_INCLUDED)
00012 #define SJEDIT_H_INCLUDED
00013 
00014 #include <time.h>
00015 
00016 #include "globals.h"
00017 
00018 namespace sjgui{
00019 
00020     namespace SJGUI_USE_STYLE
00021     {
00022         extern int def_edit_font_size; 
00023         extern int def_edit_blink_period; 
00024         extern CWnd::CSize  def_edit_size; 
00025     }
00026 
00048 template<class Ttext>
00049 class CEditCtrlTmpl : public Ttext
00050 {
00051     long            m_lNextBlink; 
00052     long            m_lBlinkPeriod; 
00053 public:
00054     virtual void Animate(); 
00055     virtual void KeyDown(int &iKey); 
00056 
00057     virtual void OnFocusRecv(){IgnoreRepeates(false);};
00059     virtual void OnFocusDism(){IgnoreRepeates(true);HideCursor();};
00060 public:
00062     CEditCtrlTmpl();
00064     void SetText(const char* str){ClearText();AddLine(str);MoveCursorToEnd();}
00066     const char* GetText(){if(m_Strings.size()==0)m_Strings.push_back("");return m_Strings[0].data();}
00068     void SetBlinkPeriod(long miliseconds){m_lBlinkPeriod=miliseconds;m_lNextBlink=0;}
00070     long GetBlinkPeriod(){return m_lBlinkPeriod;}
00071 };
00072 
00073 template<class Ttext>
00074 inline void CEditCtrlTmpl<Ttext>::KeyDown(int &iKey)
00075 {
00076     Ttext::KeyDown(iKey);
00077     switch(iKey)
00078     {
00079         case SJ_KEY_DELETE:
00080             RemoveAfterCursor();
00081             iKey=SJ_KEY_IGNORE;
00082             break;
00083         case SJ_KEY_BACKSPACE:
00084             RemoveBeforeCursor();
00085             iKey=SJ_KEY_IGNORE;
00086             break;
00087         case SJ_KEY_LEFT:
00088             MoveCursorLeft();
00089             iKey=SJ_KEY_IGNORE;
00090             break;
00091         case SJ_KEY_RIGHT:
00092             MoveCursorRight();
00093             iKey=SJ_KEY_IGNORE;
00094             break;
00095         case SJ_KEY_HOME:
00096             MoveCursorToBeginning();
00097             iKey=SJ_KEY_IGNORE;
00098             break;
00099         case SJ_KEY_END:
00100             MoveCursorToEnd();
00101             iKey=SJ_KEY_IGNORE;
00102             break;
00103         case SJ_KEY_MOUSE_LEFT:
00104             int inX=GetMouseX()-GetAbsX()-(int)(GetFontSize()/2);
00105 #define ROUND_DIV(a,b) (((a%b)>0.5f)?(a/b+1):(a/b))
00106             inX=ROUND_DIV(inX,GetFontSize());
00107 #undef ROUND_DIV
00108             SetCursorPos(GetShift()+inX);
00109             break;
00110     }
00111     if(iKey!=SJ_KEY_IGNORE && iKey>31 && iKey<128)
00112     {
00113         unsigned char cSymbol=(unsigned char)iKey;
00114         InsertAtCursor(cSymbol);
00115         iKey=SJ_KEY_IGNORE;
00116     }
00117 }
00118 
00119 template<class Ttext>
00120 inline void CEditCtrlTmpl<Ttext>::Animate()
00121 {
00122     long lt=clock();
00123     
00124     if(IsInFocus() && m_lNextBlink<lt)
00125     {
00126         m_lNextBlink=lt+GetBlinkPeriod();
00127         ShowCursor(!IsCursorShown());
00128     }
00129     Ttext::Animate();
00130 }
00131 
00132 template<class Ttext>
00133 inline CEditCtrlTmpl<Ttext>::CEditCtrlTmpl():Ttext()
00134 {
00135     SetAllowedForFocus(true);
00136     SetFontSize(SJGUI_USE_STYLE::def_edit_font_size);
00137     SetText("Edit1");
00138     
00139     SetBlinkPeriod(SJGUI_USE_STYLE::def_edit_blink_period);
00140     
00141     SetSize(SJGUI_USE_STYLE::def_edit_size);
00142     
00143     MoveCursorToBeginning();
00144 }
00145 
00146 } 
00147 
00148 #endif // !defined(SJEDIT_H_INCLUDED)