00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(SJTEXT_H_INCLUDED)
00012 #define SJTEXT_H_INCLUDED
00013
00014 #include <fstream>
00015 #include <iostream>
00016
00017 #include "textstrs.h"
00018 #include "font.h"
00019
00020 namespace sjgui{
00021
00022 namespace SJGUI_USE_STYLE
00023 {
00024 extern GLfloat def_text_color[4];
00025 }
00026
00037 template<class Tbase>
00038 class CTextCtrlTmpl : public Tbase,public CFont
00039 {
00040 CWnd::CRect m_TextPos;
00041 bool m_yShowBackPlane;
00042
00043 int m_iLinesOnScreen;
00044 int m_iTopLine;
00045
00046 int m_iShift;
00047 void Adjust();
00048 protected:
00049 CTextStrs m_Strings;
00050 float m_Color[4];
00051 unsigned int m_uiCursorPos;
00052 bool m_yShowCursor;
00053
00054 bool m_ySelectLines;
00055 int m_iSelectedLine;
00056 public:
00057
00058 virtual void OnDraw();
00059 virtual void OnReshape();
00060 virtual void OnUpdatePos();
00061 public:
00068 void ShowBackPlane(bool b=true){m_yShowBackPlane=b;}
00069 CTextCtrlTmpl();
00071 void SetColor(GLfloat* Color){memcpy(m_Color,Color,sizeof(float)*4);}
00073 void SetColor(GLfloat R, GLfloat G, GLfloat B, GLfloat A=1.0f){m_Color[0]=R;m_Color[1]=G;m_Color[2]=B;m_Color[3]=A;}
00075 float* GetColor(){return m_Color;}
00084 void SetCursorPos(unsigned int i);
00086 void MoveCursorToEnd(){m_uiCursorPos=m_Strings.GetMaxDis();if((int)m_uiCursorPos>m_iShift+m_Strings.GetCharOnDisp())m_iShift=m_uiCursorPos-m_Strings.GetCharOnDisp();};
00088 void MoveCursorToBeginning(){m_uiCursorPos=0;if((int)m_uiCursorPos<m_iShift)m_iShift=m_uiCursorPos;};
00090 void InsertAtCursor(unsigned char ch);
00092 void RemoveBeforeCursor();
00094 void RemoveAfterCursor();
00096 void MoveCursorLeft();
00098 void MoveCursorRight();
00100 void ShowCursor(bool y=true){m_yShowCursor=y;};
00102 void HideCursor(){ShowCursor(false);};
00104 bool IsCursorShown(){return m_yShowCursor;};
00106
00113 void SetIdent(bool b=true){m_Strings.SetIdent(b);}
00115 void SetWrap(bool b=true){if(IsSelectLines() && b)SetSelectLines(false);m_Strings.SetWrap(b);Adjust();}
00117 bool IsWrap(){return m_Strings.IsWrap();}
00119 int GetNumOfLines(){return (int)m_Strings.size();}
00121 int GetMaxLength(){return m_Strings.GetMaxDis();}
00123 void AddLine(const char *cp);
00125 void AddLine(std::string& str){AddLine(str.data());}
00127 const char* GetLine(unsigned int i=0){if(i<m_Strings.size() && m_Strings.size()!=0)return m_Strings[i].data();else return "";}
00128 const char* operator[](unsigned int i){return GetLine(i);}
00130 void ClearText(){m_Strings.clear();Adjust();}
00132
00134 void SetTopLine(int iLine=0);
00136 int GetTopLine(){return m_iTopLine;};
00138 int GetCharOnDisp(){return m_Strings.GetCharOnDisp();};
00140 void SetSelectLines(bool b=true){if(IsWrap())return;m_ySelectLines=b;};
00142 bool IsSelectLines(){return m_ySelectLines;};
00144 int GetNumLinesOnScreen(){return m_iLinesOnScreen;};
00150 bool NeedWrap(){if(m_Strings.GetMaxDis()>m_Strings.GetCharOnDisp())return true;else return false;};
00152 void LoadTextFromFile(char* pName);
00157 void SetShift(int i=0);
00159 int GetShift(){return m_iShift;};
00164 int GetSelectedLine(){return m_iSelectedLine;};
00166 void SetSelectedLine(int iL){if(iL<GetNumOfLines())m_iSelectedLine=iL;else m_iSelectedLine=GetNumOfLines()-1;};
00168 CTextStrs& GetStrings(){return m_Strings;};
00169 };
00170
00171 template<class Tbase>
00172 inline void CTextCtrlTmpl<Tbase>::OnUpdatePos()
00173 {
00174 Tbase::OnUpdatePos();
00175 Adjust();
00176 }
00177
00178 template<class Tbase>
00179 inline void CTextCtrlTmpl<Tbase>::OnReshape()
00180 {
00181 Adjust();
00182 }
00183
00184 template<class Tbase>
00185 inline void CTextCtrlTmpl<Tbase>::AddLine(const char *cp)
00186 {
00187 m_Strings.push_back(cp);
00188 Adjust();
00189 }
00190
00191 template<class Tbase>
00192 inline void CTextCtrlTmpl<Tbase>::SetShift(int i)
00193 {
00194 int iTemp=std::max((int)(m_Strings.GetMaxDis()-m_Strings.GetCharOnDisp()),(int)m_Strings.GetMaxDis());
00195 if(i>iTemp)
00196 i=iTemp;
00197 m_iShift=i;
00198 }
00199
00200 template<class Tbase>
00201 inline void CTextCtrlTmpl<Tbase>::SetTopLine(int iLine)
00202 {
00203
00204
00205 int iTemp=std::max(GetNumOfLines()-m_iLinesOnScreen,GetNumOfLines());
00206 if(iLine>iTemp)
00207 iLine=iTemp;
00208 m_iTopLine=iLine;
00209 }
00210
00211 template<class Tbase>
00212 inline void CTextCtrlTmpl<Tbase>::LoadTextFromFile(char* pName)
00213 {
00214 try
00215 {
00216 std::fstream fn;
00217 fn.open(pName,std::ios::in|std::ios::binary);
00218 if(!fn.is_open())
00219 {
00220
00221 std::cerr<<"Could not open text file: "<<pName<<std::endl;
00222 return;
00223 }
00224 m_Strings.clear();
00225 string str;
00226 while(getline(fn,str))
00227 m_Strings.push_back(str);
00228 fn.close();
00229 Adjust();
00230 }
00231 catch(...)
00232 {
00233 std::cerr<<"Text file is not accessible: "<<pName<<std::endl;
00234 }
00235 }
00236
00237 template<class Tbase>
00238 inline void CTextCtrlTmpl<Tbase>::MoveCursorLeft()
00239 {
00240 if(m_uiCursorPos>0)
00241 m_uiCursorPos--;
00242 if((int)m_uiCursorPos<m_iShift)
00243 m_iShift=m_uiCursorPos;
00244 }
00245
00246 template<class Tbase>
00247 inline void CTextCtrlTmpl<Tbase>::MoveCursorRight()
00248 {
00249 if(m_uiCursorPos<m_Strings.GetMaxDis())
00250 m_uiCursorPos++;
00251 if((int)m_uiCursorPos>m_iShift+m_Strings.GetCharOnDisp())
00252 m_iShift++;
00253 }
00254
00255 template<class Tbase>
00256 inline void CTextCtrlTmpl<Tbase>::RemoveBeforeCursor()
00257 {
00258 if(m_Strings.size()==0)AddLine("");
00259 if(m_uiCursorPos<=0)return;
00260 m_Strings.Erase(0,m_uiCursorPos-1,1);
00261 m_uiCursorPos--;
00262 Adjust();
00263 }
00264
00265 template<class Tbase>
00266 inline void CTextCtrlTmpl<Tbase>::RemoveAfterCursor()
00267 {
00268 if(m_uiCursorPos>=m_Strings.GetMaxDis())return;
00269 m_Strings.Erase(0,m_uiCursorPos,1);
00270 Adjust();
00271 }
00272
00273 template<class Tbase>
00274 inline void CTextCtrlTmpl<Tbase>::InsertAtCursor(unsigned char ch)
00275 {
00276 if(m_Strings.size()==0)m_Strings.push_back("");
00277 string str="";
00278 str+=ch;
00279 m_Strings.Insert(0,m_uiCursorPos,str);
00280 m_uiCursorPos++;
00281 Adjust();
00282 }
00283
00284 template<class Tbase>
00285 inline void CTextCtrlTmpl<Tbase>::SetCursorPos(unsigned int i)
00286 {
00287 if(m_Strings.size()==0)m_Strings.push_back("");
00288 if(i<=m_Strings.LenAt(0)) m_uiCursorPos=i;
00289 }
00290
00291 template<class Tbase>
00292 inline void CTextCtrlTmpl<Tbase>::Adjust()
00293 {
00294
00295 m_Strings.SetCharOnDisp((unsigned int)(GetWidth()/GetFontSize()));
00296 m_iLinesOnScreen=(int)(GetHeight()/GetFontSize());
00297 if(m_iLinesOnScreen<0)return;
00298
00299 int iCount=0;
00300
00301 int iX=0;
00302
00303 int iY=GetHeight()-GetFontSize();
00304 if(m_iSelectedLine>=(int)m_Strings.size())m_iSelectedLine=m_Strings.size()-1;
00305 m_TextPos=m_AbsPos;
00306 m_TextPos.Deflate(GetBorderSize());
00307
00308 if(m_uiCursorPos==0) return;
00309 if(m_Strings.size()==0)
00310 m_uiCursorPos=0;
00311 else if(m_Strings.LenAt(0)<m_uiCursorPos)
00312 m_uiCursorPos=m_Strings.LenAt(0);
00313 if(m_uiCursorPos>m_iShift+m_Strings.GetCharOnDisp())m_iShift++;
00314 if((int)m_uiCursorPos<m_iShift)m_iShift=m_uiCursorPos;
00315 if(m_uiCursorPos>m_Strings.GetCharOnDisp()&& (m_Strings.LenAt(0)-m_iShift)<m_Strings.GetCharOnDisp())
00316 m_iShift=m_Strings.LenAt(0)-m_Strings.GetCharOnDisp();
00317 }
00318
00319 template<class Tbase>
00320 inline CTextCtrlTmpl<Tbase>::CTextCtrlTmpl():Tbase(),CFont()
00321 {
00322 SetColor(SJGUI_USE_STYLE::def_text_color);
00323 m_uiCursorPos=0;
00324 m_yShowBackPlane=false;
00325 m_ySelectLines=false;
00326 m_iLinesOnScreen=0;
00327 SetShift(0);
00328 SetTopLine(0);
00329 SetSelectedLine(0);
00330 HideCursor();
00331 }
00332
00333 template<class Tbase>
00334 inline void CTextCtrlTmpl<Tbase>::OnDraw()
00335 {
00336 if(m_yShowBackPlane)
00337 {
00338 glEnable(GL_BLEND);
00339 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00340
00341 glColor4fv(m_BgColor);
00342 glBegin(GL_TRIANGLE_STRIP);
00343 glVertex2i(m_AbsPos.GetX(),m_AbsPos.GetBottom());
00344 glVertex2i(m_AbsPos.GetX(),m_AbsPos.GetY());
00345 glVertex2i(m_AbsPos.GetRight(),m_AbsPos.GetBottom());
00346 glVertex2i(m_AbsPos.GetRight(),m_AbsPos.GetY());
00347 glEnd();
00348 glDisable(GL_BLEND);
00349
00350 glColor3f(1.0f,1.0f,1.0f);
00351 glBegin(GL_LINE_STRIP);
00352 glVertex2i(m_AbsPos.GetX(),m_AbsPos.GetY());
00353 glVertex2i(m_AbsPos.GetRight(),m_AbsPos.GetY());
00354 glVertex2i(m_AbsPos.GetRight(),m_AbsPos.GetBottom());
00355 glVertex2i(m_AbsPos.GetX(),m_AbsPos.GetBottom());
00356 glVertex2i(m_AbsPos.GetX(),m_AbsPos.GetY());
00357 glEnd();
00358
00359 }
00360 FontDrawBegin();
00361 glPushMatrix();
00362 for(int iLine=GetTopLine();(iLine-GetTopLine())<GetNumLinesOnScreen() && iLine<GetNumOfLines();iLine++)
00363 {
00364 if(IsSelectLines() && iLine==GetSelectedLine() && IsInFocus())
00365 {
00366
00367 glDisable(GL_TEXTURE_2D);
00368 glDisable(GL_BLEND);
00369
00370 glLoadIdentity();
00371 glTranslated( m_TextPos.GetX(),
00372 m_TextPos.GetY()+(iLine-GetTopLine())*GetFontSize(),
00373 0);
00374 glColor4fv(m_BgSelColor);
00375 glBegin(GL_TRIANGLE_STRIP);
00376 glVertex2i(0, 0);
00377 glVertex2i(0,GetFontSize());
00378 glVertex2i(m_TextPos.GetWidth(),0);
00379 glVertex2i(m_TextPos.GetWidth(),GetFontSize());
00380 glEnd();
00381
00382 glEnable(GL_TEXTURE_2D);
00383
00384 glEnable(GL_BLEND);
00385 }
00386 unsigned int uiLen=0;
00387 if((int)m_Strings.LenAt(iLine)<m_iShift)
00388
00389 continue;
00390 else if(((int)m_Strings.LenAt(iLine)-m_iShift)*GetFontSize()>GetWidth())
00391 uiLen=(unsigned int)(GetWidth()/GetFontSize())+1;
00392 else
00393 uiLen=m_Strings.LenAt(iLine)-m_iShift;
00394 const char* msg=m_Strings[iLine].data()+m_iShift;
00395
00396 if(IsSelectLines() && iLine==GetSelectedLine() && !IsInFocus())
00397 FontDraw(m_TextPos.GetX(),m_TextPos.GetY()+(int)((iLine-GetTopLine())*GetFontSize()),
00398 msg,uiLen,m_BgSelColor);
00399 else
00400 FontDraw(m_TextPos.GetX(),(int)(m_TextPos.GetY()+(iLine-GetTopLine())*GetFontSize()),
00401 msg,uiLen,m_Color);
00402 }
00403
00404 if(m_yShowCursor && (int)m_uiCursorPos>=m_iShift && m_uiCursorPos<=m_iShift+m_Strings.GetCharOnDisp())
00405 {
00406
00407 glDisable(GL_TEXTURE_2D);
00408 glDisable(GL_BLEND);
00409 glLoadIdentity();
00410 glTranslated(m_TextPos.GetX()+(m_uiCursorPos-m_iShift)*(GetFontSize()-GetFontScale()),m_TextPos.GetY(),0.0f);
00411 glColor4fv(m_Color);
00412 glBegin(GL_TRIANGLE_STRIP);
00413 glVertex2i(0,GetFontSize());
00414 glVertex2i(2,GetFontSize());
00415 glVertex2i(0,0);
00416 glVertex2i(2,0);
00417 glEnd();
00418 }
00419 glPopMatrix();
00420 FontDrawEnd();
00421 Tbase::OnDraw();
00422 }
00423
00424 }
00425
00426 #endif // !defined(SJTEXT_H_INCLUDED)