00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(SJTEXTBOX_H_INCLUDED)
00012 #define SJTEXTBOX_H_INCLUDED
00013
00014 namespace sjgui{
00015
00016 namespace SJGUI_USE_STYLE
00017 {
00018 extern GLfloat def_text_box_text_color[4];
00019 extern int def_text_box_fonst_size;
00020 }
00034 template<class Ttext,class Tslider,class Tbase>
00035 class CTextBoxCtrlTmpl : public Tbase
00036 {
00037 Ttext m_Text;
00038 Tslider m_VScrlBar;
00039 Tslider m_HScrlBar;
00040 bool m_yShowVSB;
00041 bool m_yShowHSB;
00042 bool m_yVSBAlways;
00043 bool m_yAutoScroll;
00044
00045 unsigned int m_uiLines;
00046 public:
00047 virtual void OnReshape();
00048
00049 virtual void OnAnimate();
00051 virtual void OnKeyUp(int &iKey);
00053 virtual void OnKeyDown(int &iKey);
00060 int GetNumLinesOnScreen(){return m_Text.GetNumLinesOnScreen();};
00062 int GetNumOfLines(){return m_Text.GetNumOfLines();};
00064 void SetIdent(bool b=true){m_Text.SetIdent(b);};
00066 void SetWrap(bool b=true){m_Text.SetWrap(b);Reshape();};
00068 bool IsWrap(){return m_Text.IsWrap();};
00070 void AddLine(const char* str){m_Text.AddLine(str);Reshape();};
00072 void AddLine(std::string& str){m_Text.AddLine(str);};
00074 void Clear(){m_Text.ClearText();Reshape();};
00076 const char* GetLine(int i){return m_Text[i];};
00078 int GetTopLine(){return m_Text.GetTopLine();};
00080 void SetSelectLines(bool b=true){m_Text.SetSelectLines(b);};
00082 bool IsSelectLines(){return m_Text.IsSelectLines();};
00084 void SetColor(float* pC){m_Text.SetColor(pC);};
00086 void SetColor(float R, float G, float B){m_Text.SetColor(R,G,B);};
00088 float* GetColor(){return m_Text.GetColor();};
00090 void SetFontSize(float fS){m_Text.SetFontSize(fS);Reshape();};
00092 int GetFontSize(){return m_Text.GetFontSize();};
00094 void LoadTextFromFile(char* pName){m_Text.LoadTextFromFile(pName);Reshape();};
00099 int GetSelectedLine(){return m_Text.GetSelectedLine();};
00101 void SetSelectedLine(int iL){m_Text.SetSelectedLine(iL);};
00103 CTextStrs& GetStrings(){return m_Text.GetStrings();};
00105 void SetTopLine(int i)
00106 {
00107 if(i<m_VScrlBar.GetMax())
00108 {
00109 m_VScrlBar.SetSliderPos(i);
00110 m_Text.SetTopLine(m_VScrlBar.GetSliderPos());
00111 }
00112 };
00119 void ShowBackPlane(bool b=true){m_Text.ShowBackPlane(b);};
00121
00122 void AutoScroll(bool b=true){m_yAutoScroll=b;};
00124 void ShowVSBAlways(bool b=true){m_yVSBAlways=b; Reshape();};
00126 void SetBgColor(GLfloat R,GLfloat G,GLfloat B,GLfloat A=1.0f){Tbase::SetBgColor(R,G,B,A);m_Text.SetBgColor(m_BgColor);};
00127 CTextBoxCtrlTmpl();
00129 void EnableScrlBars(bool h=true,bool v=true){m_yShowHSB=h;m_yShowVSB=v;Reshape();};
00131 void ScrollDown(int i=1){if(m_VScrlBar.GetSliderPos()+i<=m_VScrlBar.GetMax()){m_VScrlBar.SetSliderPos(m_VScrlBar.GetSliderPos()+i);m_Text.SetTopLine(m_VScrlBar.GetSliderPos());};};
00132 };
00133
00134 template<class Ttext,class Tslider, class Tbase>
00135 inline void CTextBoxCtrlTmpl<Ttext,Tslider,Tbase>::OnKeyUp(int &iKey)
00136 {
00137 Tbase::OnKeyUp(iKey);
00138
00139 m_Text.SetTopLine(m_VScrlBar.GetSliderPos());
00140 m_Text.SetShift(m_HScrlBar.GetSliderPos());
00141 }
00142
00143 template<class Ttext,class Tslider, class Tbase>
00144 inline void CTextBoxCtrlTmpl<Ttext,Tslider,Tbase>::OnKeyDown(int &iKey)
00145 {
00146 Tbase::OnKeyDown(iKey);
00147 if(IsSelectLines() && iKey==SJ_KEY_MOUSE_LEFT)
00148 {
00149
00150 m_Text.SetSelectedLine(m_Text.GetTopLine()+(int)((GetMouseY()-m_Text.GetAbsY())/m_Text.GetFontSize()));
00151 iKey=SJ_KEY_IGNORE;
00152 }
00153 else
00154 {
00155 switch(iKey)
00156 {
00157 case SJ_KEY_UP:
00158 if(m_Text.GetNumOfLines()>m_Text.GetNumLinesOnScreen())
00159 {
00160 m_VScrlBar.SetSliderPos(m_VScrlBar.GetSliderPos()-1);
00161 m_Text.SetTopLine(m_VScrlBar.GetSliderPos());
00162 }
00163 iKey=SJ_KEY_IGNORE;
00164 break;
00165 case SJ_KEY_DOWN:
00166 case SJ_KEY_SPACE:
00167 if(m_Text.GetNumOfLines()>m_Text.GetNumLinesOnScreen())
00168 {
00169 m_VScrlBar.SetSliderPos(m_VScrlBar.GetSliderPos()+1);
00170 m_Text.SetTopLine(m_VScrlBar.GetSliderPos());
00171 }
00172 iKey=SJ_KEY_IGNORE;
00173 break;
00174 case SJ_KEY_LEFT:
00175 if(m_Text.GetShift()>0)
00176 {
00177 m_HScrlBar.SetSliderPos(m_HScrlBar.GetSliderPos()-1);
00178 m_Text.SetShift(m_HScrlBar.GetSliderPos());
00179 }
00180 iKey=SJ_KEY_IGNORE;
00181 break;
00182 case SJ_KEY_RIGHT:
00183 if(!IsWrap() && m_Text.NeedWrap())
00184 {
00185 m_HScrlBar.SetSliderPos(m_HScrlBar.GetSliderPos()+1);
00186 m_Text.SetShift(m_HScrlBar.GetSliderPos());
00187 }
00188 iKey=SJ_KEY_IGNORE;
00189 break;
00190 }
00191
00192 m_Text.SetTopLine(m_VScrlBar.GetSliderPos());
00193 m_Text.SetShift(m_HScrlBar.GetSliderPos());
00194 }
00195 }
00196
00197 template<class Ttext,class Tslider, class Tbase>
00198 inline void CTextBoxCtrlTmpl<Ttext,Tslider,Tbase>::OnAnimate()
00199 {
00200
00201 if(m_uiLines!=(unsigned int)m_Text.GetNumOfLines())
00202 {
00203 Reshape();
00204 if(m_Text.GetTopLine()+m_Text.GetNumLinesOnScreen()<m_Text.GetNumOfLines() && m_Text.GetNumOfLines()>(int)m_uiLines && m_yAutoScroll)
00205 ScrollDown(m_Text.GetNumOfLines()-m_uiLines);
00206 }
00207 m_uiLines=m_Text.GetNumOfLines();
00208 Tbase::OnAnimate();
00209 }
00210
00211 template<class Ttext,class Tslider, class Tbase>
00212 inline CTextBoxCtrlTmpl<Ttext,Tslider,Tbase>::CTextBoxCtrlTmpl():Tbase()
00213 {
00214 m_yAutoScroll=false;
00215 SetAllowedForFocus(true);
00216 m_Text.SetColor(SJGUI_USE_STYLE::def_text_box_text_color);
00217 m_Text.SetFontSize(SJGUI_USE_STYLE::def_text_box_fonst_size);
00218 m_HScrlBar.SetOrient(CWnd::HORIZONTAL);
00219 m_VScrlBar.SetOrient(CWnd::VERTICAL);
00220 EnableScrlBars();
00221 RegisterChild(&m_Text);
00222 RegisterChild(&m_HScrlBar);
00223 RegisterChild(&m_VScrlBar);
00224 m_uiLines=0;
00225 m_yVSBAlways=false;
00226 }
00227
00228 template<class Ttext,class Tslider, class Tbase>
00229 inline void CTextBoxCtrlTmpl<Ttext,Tslider,Tbase>::OnReshape()
00230 {
00231
00232 m_Text.PosWnd(GetBorderSize(),GetBorderSize(),GetWidth()-GetBorderSize()*2,GetHeight()-GetBorderSize()*2);
00233
00234 m_Text.Reshape();
00235
00236 if((m_Text.GetNumOfLines()>m_Text.GetNumLinesOnScreen() ||m_yVSBAlways)&& m_yShowVSB)
00237 {
00238 m_VScrlBar.Show();
00239 m_VScrlBar.PosWnd( GetWidth()-m_VScrlBar.GetWidth()-GetBorderSize(),
00240 GetBorderSize(),
00241 m_VScrlBar.GetWidth(),
00242 GetHeight()-GetBorderSize()*2);
00243 m_Text.SetWidth(m_Text.GetWidth()-m_VScrlBar.GetWidth()-1);
00244 }
00245 else
00246 {
00247 m_VScrlBar.Hide();
00248 }
00249 if(m_Text.NeedWrap() && m_yShowHSB && !m_Text.IsWrap())
00250 {
00251 m_HScrlBar.Show();
00252 m_HScrlBar.PosWnd( GetBorderSize(),
00253 GetHeight()-m_HScrlBar.GetHeight()-GetBorderSize(),
00254 m_Text.GetWidth(),
00255 m_HScrlBar.GetHeight());
00256 m_Text.SetHeight(m_Text.GetHeight()-m_HScrlBar.GetHeight()-1);
00257 }
00258 else
00259 {
00260 m_HScrlBar.Hide();
00261
00262 }
00263 m_VScrlBar.SetMinMax(0,m_Text.GetNumOfLines()-1);
00264 m_VScrlBar.SetSliderLength(m_Text.GetNumLinesOnScreen()/(m_Text.GetNumOfLines()!=0?m_Text.GetNumOfLines():1.0f));
00265 m_HScrlBar.SetMinMax(0,m_Text.GetMaxLength()-1);
00266 m_HScrlBar.SetSliderLength((m_Text.GetCharOnDisp()-1)/(m_Text.GetMaxLength()!=0?m_Text.GetMaxLength():1.0f));
00267 Tbase::OnReshape();
00268 }
00269
00270 }
00271
00272 #endif // !defined(SJTEXTBOX_H_INCLUDED)