Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

TextArea.h

Go to the documentation of this file.
00001 /*
00002  * Author: Michiel van Liempt
00003  */
00004 
00005 #ifndef OglGui_TextArea_h
00006 #define OglGui_TextArea_h
00007 
00008 #ifndef OglGui_WindowScrollBar_h
00009 #include "OglGui/WindowScrollBar.h"
00010 #endif
00011 
00012 namespace OglGui
00013 {
00014 
00015 // XXX RvB drawBorder zou verwijderd moeten worden.
00016 // Kan vervangen worden door indicator of lines of pixels gebruikt moet worden
00017 class TextArea : public Window, ScrollBarListener
00018 {
00019 public:
00020 
00021     TextArea(Window* parent, int width, int height, strconst text,
00022              bool drawBorder = true) :
00023         Window(parent, width, height)
00024     {
00025         Init(height/cLineHeight, text, drawBorder);
00026     }
00027 
00028     TextArea(Window* parent, int x, int y, int width, int nrLines,
00029              strconst text, bool drawBorder = true) :
00030         Window(parent, x, y, width, nrLines * cLineHeight + 4)
00031     {
00032         Init(nrLines, text, drawBorder);
00033     }
00034     
00035     void SetText(strconst text)
00036     {
00037         mStrings.clear();
00038         mFirstVisible = 0;
00039         mFormatted = false;
00040         mText = text;
00041     }
00042 
00043     void SetFirstVis(int nr)
00044     {
00045         int sz = mStrings.size();
00046         if (nr > sz - mNrLines)
00047             nr = sz - mNrLines;
00048         if (nr < 0) 
00049             nr = 0;
00050         if (nr == mFirstVisible)
00051             return;
00052         mFirstVisible = nr;
00053     }
00054 
00055     void SetTextShadowing(bool doShadow)
00056     {
00057         mDoTextShadow = doShadow;
00058     }
00059 
00060     bool GetTextShadowing() const
00061     {
00062         return mDoTextShadow;
00063     }
00064 
00065     void SetTextColor(ULONG fg)
00066     {
00067         mTextColor = fg;
00068     }
00069 
00070     ULONG GetTextColor() const
00071     {
00072         return mTextColor;
00073     }
00074 
00075     bool GetBreakOnLines() const
00076     {
00077         return mBreakOnLines;
00078     }
00079 
00080     void SetBreakOnLines(bool doBreak)
00081     {
00082         mBreakOnLines = doBreak;
00083     }
00084 
00085     void SetTextShadowColor(ULONG shadow)
00086     {
00087         mShadowColor = shadow;
00088     }
00089 
00090     ULONG GetTextShadowColor() const
00091     {
00092         return mShadowColor;
00093     }
00094 
00095     virtual void DisplayFunc()
00096     {
00097         Window::DisplayFunc();
00098 
00099         OGC oldOGC;
00100         OGCSave(&oldOGC);
00101 
00102                 if (this == NULL) 
00103                 {
00104                         //std::cout << "WARNING: trying to TextArea::DisplayFunc() on a NULL TextArea." << std::endl;
00105                         return;
00106                 }
00107 
00108         // draw the string content
00109         CheckTextFormat();
00110         int startPos = (mScrollBar) ? cLineHeight : 0;
00111         for (int i=mFirstVisible ; i<mStrings.size() ; i++)
00112         {
00113             int n = i - mFirstVisible;
00114             int y = mOglWnd->height - (n+1) * cLineHeight + cLineHeight/2 - 7;
00115             if (mDoTextShadow)
00116                 oglSys.ShadowPrintf(mOglWnd, startPos + 2, y, mShadowColor,
00117                                     mTextColor, "%s", mStrings[i].c_str());
00118             else
00119                 oglSys.PosColPrintf(mOglWnd, startPos + 2, y, mTextColor,
00120                                     "%s", mStrings[i].c_str());
00121        }
00122 
00123         OGCRestore(&oldOGC);
00124     }
00125 
00126     virtual int SetState(int nState)
00127     {
00128         if (mScrollBar != 0)
00129             mScrollBar->SetState(nState);
00130 
00131         return Window::SetState(nState);
00132     }
00133 
00134     virtual void MouseFunc(INT msg, INT but, INT state, INT x, INT y)
00135     {
00136         Window::MouseFunc(msg, but, state, x, y);
00137 
00138         if (mScrollBar && msg == oglMouseWheelUp)
00139             mScrollBar->ChangePos(-1);
00140         if (mScrollBar && msg == oglMouseWheelDown)
00141             mScrollBar->ChangePos(1);
00142     }
00143 
00144     virtual void KeyboardFunc(INT c, int state)
00145     {
00146         Window::KeyboardFunc(c, state);
00147         if (mScrollBar)
00148             mScrollBar->ScrollKeys(c, state);
00149     }
00150 
00151     virtual void ReshapeFunc(INT width, INT height)
00152     {
00153         Window::ReshapeFunc(width, height);
00154         if (height / cLineHeight != mNrLines)
00155         {
00156             if ((mNrLines = height / cLineHeight) > 1)
00157             {
00158                 if (!mScrollBar)
00159                     CreateScrollBar();
00160             }
00161         }
00162         SetText(mText);
00163     }
00164 
00165     void OnScroll(ScrollBar *src, int position, void* data)
00166     {
00167         SetFirstVis(position);
00168     }
00169 
00170 
00171 
00172 private:
00173 
00174     void Init(int nrLines, strconst text, bool drawBorder)
00175     {
00176         mNrLines = nrLines;
00177         if (mDrawBorder = drawBorder)
00178             mOglWnd->borderType = BEV_ETCHED;
00179         mFirstVisible = 0;
00180         mDoTextShadow = false;
00181         mShadowColor = oglWHITE;
00182         mTextColor = oglBLACK;
00183         mFormatted = false;
00184         SetText(text);
00185         mScrollBar = 0;
00186         if (mNrLines > 1)
00187             CreateScrollBar();
00188     }
00189 
00190     void CreateScrollBar()
00191     {
00192         mScrollBar = new WindowScrollBar(this, false, false);
00193         mScrollBar->SetScrollBarListener(this, 0);
00194         mScrollBar->SetRange(0, mNrLines);
00195     }
00196 
00197     void CheckTextFormat()
00198     {
00199         if (mFormatted)
00200             return;
00201         OGLWND* top = oglSys.GetTopOGLWND(mOglWnd);
00202         if (! top->hFont)
00203             return;
00204 
00205         int maxWidth = mOglWnd->width - 2 - (mScrollBar ? cLineHeight : 0);
00206         std::string::size_type start = 0;
00207         // crude way of displaying newlines, WARNING: line lenghts are not checked:
00208         if (mBreakOnLines)
00209             while (start < mText.size())
00210             {
00211                 std::string::size_type end = mText.find('\n', start);
00212                 if ((end == std::string::npos) || (end <= start))
00213                 {
00214                     mStrings.push_back(mText.substr(start));
00215                     start = mText.size();
00216                 }
00217                 else
00218                 {
00219                     mStrings.push_back(mText.substr(start, end - start + 1));
00220                     start = end + 1;
00221                     while (mText[start] == ' ')
00222                         start++;
00223                 }
00224             }
00225         else
00226             while (start < mText.size())
00227             {
00228                 std::string::size_type end = mText.size();
00229                 while ((end != std::string::npos) && (end > start) &&
00230                     (StringWidth(top, start, end) >= maxWidth))
00231                 {   // XXX RvB should also break at newlines
00232                     std::string::size_type res = mText.rfind(' ', end);
00233                     end = (res != std::string::npos) ? res - 1 : res;
00234                 }
00235                 if ((end == std::string::npos) || (end <= start))
00236                 {
00237                     mStrings.push_back(mText.substr(start));
00238                     start = mText.size();
00239                 }
00240                 else
00241                 {
00242                     mStrings.push_back(mText.substr(start, end - start + 1));
00243                     start = end + 1;
00244                                         while (start < mText.size() && mText[start] == ' ')
00245                         start++;
00246                 }
00247             }
00248         mFormatted = true;
00249         if (mScrollBar)
00250             mScrollBar->SetRange(mStrings.size(), mNrLines);
00251     }
00252 
00253     int StringWidth(OGLWND* top, std::string::size_type start,
00254                     std::string::size_type end)
00255     {
00256         INT w;
00257         INT h;
00258         std::string s = mText.substr(start, end - start + 1);
00259         oglSys.AbstFontTextSize(top, top->hFont, (char *) s.c_str(), &w, &h);
00260         return w;
00261     }
00262 
00263     int             mNrLines;
00264     bool            mDrawBorder;
00265     int             mFirstVisible;
00266     bool            mDoTextShadow;
00267     ULONG           mTextColor;
00268     ULONG           mShadowColor;
00269 
00270     std::string     mText;
00271     bool            mFormatted;
00272     bool            mBreakOnLines;
00273     std::vector<std::string> mStrings;
00274 
00275     WindowScrollBar* mScrollBar;
00276 
00277     static const int cLineHeight = 18;
00278 
00279 };
00280 
00281 #undef cLineHeight
00282 } // namespace OglGui
00283 
00284 #endif

Generated on Fri Mar 19 09:31:39 2010 for ImpalaSrc by  doxygen 1.5.1