Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIStaticText.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIStaticText.cpp
00003         created:        4/6/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implementation of the static text widget class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "elements/CEGUIStaticText.h"
00027 #include "CEGUIFont.h"
00028 #include "CEGUIWindowManager.h"
00029 #include "CEGUIExceptions.h"
00030 #include "elements/CEGUIScrollbar.h"
00031 
00032 // Start of CEGUI namespace section
00033 namespace CEGUI
00034 {
00035 const String StaticText::EventNamespace("StaticText");
00036 
00037 /*************************************************************************
00038 Static Properties for this class
00039 *************************************************************************/
00040 StaticTextProperties::TextColours               StaticText::d_textColoursProperty;
00041 StaticTextProperties::VertFormatting    StaticText::d_vertFormattingProperty;
00042 StaticTextProperties::HorzFormatting    StaticText::d_horzFormattingProperty;
00043 StaticTextProperties::VertScrollbar             StaticText::d_vertScrollbarProperty;
00044 StaticTextProperties::HorzScrollbar             StaticText::d_horzScrollbarProperty;
00045 
00046 
00047 /*************************************************************************
00048         Constructor for static text widgets.    
00049 *************************************************************************/
00050 StaticText::StaticText(const String& type, const String& name) :
00051         Static(type, name),
00052         d_horzFormatting(LeftAligned),
00053         d_vertFormatting(VertCentred),
00054     d_textCols(0xFFFFFFFF),
00055         d_enableVertScrollbar(false),
00056         d_enableHorzScrollbar(false)
00057 {
00058         addStaticTextProperties();
00059 }
00060 
00061 
00062 /*************************************************************************
00063         Destructor for static text widgets.
00064 *************************************************************************/
00065 StaticText::~StaticText(void)
00066 {
00067 }
00068 
00069 
00070 /*************************************************************************
00071         Sets the colours to be applied when rendering the text. 
00072 *************************************************************************/
00073 void StaticText::setTextColours(const ColourRect& colours)
00074 {
00075         d_textCols = colours;
00076         requestRedraw();
00077 }
00078 
00079 
00080 /*************************************************************************
00081         Sets the colours to be applied when rendering the text. 
00082 *************************************************************************/
00083 void StaticText::setTextColours(const colour& top_left_colour, const colour& top_right_colour, const colour& bottom_left_colour, const colour& bottom_right_colour)
00084 {
00085         d_textCols.d_top_left           = top_left_colour;
00086         d_textCols.d_top_right          = top_right_colour;
00087         d_textCols.d_bottom_left        = bottom_left_colour;
00088         d_textCols.d_bottom_right       = bottom_right_colour;
00089         requestRedraw();
00090 }
00091 
00092 
00093 /*************************************************************************
00094         Set the formatting required for the text.
00095 *************************************************************************/
00096 void StaticText::setFormatting(HorzFormatting h_fmt, VertFormatting v_fmt)
00097 {
00098         d_horzFormatting = h_fmt;
00099         d_vertFormatting = v_fmt;
00100         requestRedraw();
00101 }
00102 
00103 
00104 /*************************************************************************
00105         Set the formatting required for the text.       
00106 *************************************************************************/
00107 void StaticText::setVerticalFormatting(VertFormatting v_fmt)
00108 {
00109         d_vertFormatting = v_fmt;
00110         requestRedraw();
00111 }
00112 
00113 
00114 /*************************************************************************
00115         Set the formatting required for the text.       
00116 *************************************************************************/
00117 void StaticText::setHorizontalFormatting(HorzFormatting h_fmt)
00118 {
00119         d_horzFormatting = h_fmt;
00120         requestRedraw();
00121 }
00122 
00123 
00124 /*************************************************************************
00125         Perform the actual rendering for this Window.
00126 *************************************************************************/
00127 void StaticText::populateRenderCache()
00128 {
00129         // get whatever base class needs to render.
00130         Static::populateRenderCache();
00131 
00132         const Font* font = getFont();
00133     // can't render text without a font :)
00134     if (font == 0)
00135         return;
00136 
00137         // get destination area for the text.
00138         Rect absarea(getTextRenderArea());
00139         Rect clipper(absarea);
00140 
00141         float textHeight = font->getFormattedLineCount(d_text, absarea, (TextFormatting)d_horzFormatting) * font->getLineSpacing();
00142 
00143         // see if we may need to adjust horizontal position
00144         if (d_horzScrollbar->isVisible())
00145         {
00146                 switch(d_horzFormatting)
00147                 {
00148                 case LeftAligned:
00149                 case WordWrapLeftAligned:
00150                 case Justified:
00151                 case WordWrapJustified:
00152                         absarea.offset(Point(-d_horzScrollbar->getScrollPosition(), 0));
00153                         break;
00154 
00155                 case Centred:
00156                 case WordWrapCentred:
00157                         absarea.setWidth(d_horzScrollbar->getDocumentSize());
00158                         absarea.offset(Point(-d_horzScrollbar->getScrollPosition(), 0));
00159                         break;
00160 
00161                 case RightAligned:
00162                 case WordWrapRightAligned:
00163                         absarea.offset(Point(d_horzScrollbar->getScrollPosition(), 0));
00164                         break;
00165                 }
00166 
00167         }
00168 
00169         // adjust y positioning according to formatting option
00170         switch(d_vertFormatting)
00171         {
00172         case TopAligned:
00173                 absarea.d_top -= d_vertScrollbar->getScrollPosition();
00174                 break;
00175 
00176         case VertCentred:
00177                 // if scroll bar is in use, act like TopAligned
00178                 if (d_vertScrollbar->isVisible())
00179                 {
00180                         absarea.d_top -= d_vertScrollbar->getScrollPosition();
00181                 }
00182                 // no scroll bar, so centre text instead.
00183                 else
00184                 {
00185                         absarea.d_top += PixelAligned((absarea.getHeight() - textHeight) * 0.5f);
00186                 }
00187 
00188                 break;
00189 
00190         case BottomAligned:
00191                 absarea.d_top = absarea.d_bottom - textHeight;
00192                 absarea.d_top += d_vertScrollbar->getScrollPosition();
00193                 break;
00194         }
00195 
00196     // calculate final colours
00197     ColourRect final_cols(d_textCols);
00198     final_cols.modulateAlpha(getEffectiveAlpha());
00199     // cache the text for rendering.
00200     d_renderCache.cacheText(d_text, font, (TextFormatting)d_horzFormatting, absarea, 0, final_cols, &clipper);
00201 }
00202 
00203 
00204 /*************************************************************************
00205         Add properties for static text
00206 *************************************************************************/
00207 void StaticText::addStaticTextProperties(void)
00208 {
00209         addProperty(&d_textColoursProperty);
00210         addProperty(&d_vertFormattingProperty);
00211         addProperty(&d_horzFormattingProperty);
00212         addProperty(&d_vertScrollbarProperty);
00213         addProperty(&d_horzScrollbarProperty);
00214 }
00215 
00216 
00217 /*************************************************************************
00218         Perform initialisation for the widget.
00219 *************************************************************************/
00220 void StaticText::initialise(void)
00221 {
00222         Static::initialise();
00223 
00224         // create the component sub-widgets
00225         d_vertScrollbar = createVertScrollbar(getName() + "__auto_vscrollbar__");
00226         d_horzScrollbar = createHorzScrollbar(getName() + "__auto_hscrollbar__");
00227 
00228         d_vertScrollbar->hide();
00229         d_horzScrollbar->hide();
00230 
00231         addChildWindow(d_vertScrollbar);
00232         addChildWindow(d_horzScrollbar);
00233 
00234         performChildWindowLayout();
00235 
00236         // event subscription
00237         d_vertScrollbar->subscribeEvent(Scrollbar::EventScrollPositionChanged, Event::Subscriber(&StaticText::handleScrollbarChange, this));
00238         d_horzScrollbar->subscribeEvent(Scrollbar::EventScrollPositionChanged, Event::Subscriber(&StaticText::handleScrollbarChange, this));
00239 }
00240 
00241 
00242 /*************************************************************************
00243         Return a Rect object describing, in un-clipped pixels, the window
00244         relative area that the text should be rendered in to.
00245 *************************************************************************/
00246 Rect StaticText::getTextRenderArea(void) const
00247 {
00248         Rect area(Point(0,0), getAbsoluteSize());
00249 
00250         if (d_horzScrollbar->isVisible())
00251         {
00252                 area.d_bottom -= d_horzScrollbar->getAbsoluteHeight();
00253         }
00254         else if (d_frameEnabled)
00255         {
00256                 area.d_bottom -= d_bottom_height;
00257         }
00258 
00259         if (d_vertScrollbar->isVisible())
00260         {
00261                 area.d_right -= d_vertScrollbar->getAbsoluteWidth();
00262         }
00263         else if (d_frameEnabled)
00264         {
00265                 area.d_right -= d_right_width;
00266         }
00267 
00268         if (d_frameEnabled)
00269         {
00270                 area.d_left     += d_left_width;
00271                 area.d_top      += d_top_height;
00272         }
00273 
00274         return area;
00275 }
00276 
00277 
00278 /*************************************************************************
00279         display required integrated scroll bars according to current state
00280         of the edit box and update their values.
00281 *************************************************************************/
00282 void StaticText::configureScrollbars(void)
00283 {
00284     Scrollbar* vertScrollbar;
00285     Scrollbar* horzScrollbar;
00286 
00287     try
00288     {
00289         vertScrollbar = static_cast<Scrollbar*>(WindowManager::getSingleton().getWindow(getName() + "__auto_vscrollbar__"));
00290         horzScrollbar = static_cast<Scrollbar*>(WindowManager::getSingleton().getWindow(getName() + "__auto_hscrollbar__"));
00291     }
00292     catch (UnknownObjectException)
00293     {
00294         // no scrollbars?  Can't configure then!
00295         return;
00296     }
00297 
00298         const Font* font = getFont();
00299 
00300     // must have a font to measure text!
00301         if (font == 0)
00302            return;
00303 
00304         Rect initialArea(getTextRenderArea());
00305 
00306         float totalHeight       = font->getFormattedLineCount(d_text, initialArea, (TextFormatting)d_horzFormatting) * font->getLineSpacing();
00307         float widestItem        = font->getFormattedTextExtent(d_text, initialArea, (TextFormatting)d_horzFormatting);
00308 
00309         //
00310         // First show or hide the scroll bars as needed (or requested)
00311         //
00312         // show or hide vertical scroll bar as required (or as specified by option)
00313         if ((totalHeight > getTextRenderArea().getHeight()) && d_enableVertScrollbar)
00314         {
00315                 vertScrollbar->show();
00316 
00317                 // show or hide horizontal scroll bar as required (or as specified by option)
00318                 if ((widestItem > getTextRenderArea().getWidth()) && d_enableHorzScrollbar)
00319                 {
00320                         horzScrollbar->show();
00321                 }
00322                 else
00323                 {
00324                         horzScrollbar->hide();
00325                 }
00326 
00327         }
00328         else
00329         {
00330                 // show or hide horizontal scroll bar as required (or as specified by option)
00331                 if ((widestItem > getTextRenderArea().getWidth()) && d_enableHorzScrollbar)
00332                 {
00333                         horzScrollbar->show();
00334 
00335                         // show or hide vertical scroll bar as required (or as specified by option)
00336                         if ((totalHeight > getTextRenderArea().getHeight()) && d_enableVertScrollbar)
00337                         {
00338                                 vertScrollbar->show();
00339                         }
00340                         else
00341                         {
00342                                 vertScrollbar->hide();
00343                         }
00344 
00345                 }
00346                 else
00347                 {
00348                         vertScrollbar->hide();
00349                         horzScrollbar->hide();
00350                 }
00351 
00352         }
00353 
00354         //
00355         // Set up scroll bar values
00356         //
00357         Rect renderArea(getTextRenderArea());
00358 
00359         vertScrollbar->setDocumentSize(totalHeight);
00360         vertScrollbar->setPageSize(renderArea.getHeight());
00361         vertScrollbar->setStepSize(ceguimax(1.0f, renderArea.getHeight() / 10.0f));
00362         vertScrollbar->setScrollPosition(d_vertScrollbar->getScrollPosition());
00363 
00364         horzScrollbar->setDocumentSize(widestItem);
00365         horzScrollbar->setPageSize(renderArea.getWidth());
00366         horzScrollbar->setStepSize(ceguimax(1.0f, renderArea.getWidth() / 10.0f));
00367         horzScrollbar->setScrollPosition(d_horzScrollbar->getScrollPosition());
00368 }
00369 
00370 
00371 /*************************************************************************
00372         Return whether the vertical scroll bar is set to be shown if needed.    
00373 *************************************************************************/
00374 bool StaticText::isVerticalScrollbarEnabled(void) const
00375 {
00376         return d_enableVertScrollbar;
00377 }
00378 
00379 
00380 /*************************************************************************
00381         Return whether the horizontal scroll bar is set to be shown if needed.  
00382 *************************************************************************/
00383 bool StaticText::isHorizontalScrollbarEnabled(void) const
00384 {
00385         return d_enableHorzScrollbar;
00386 }
00387 
00388 
00389 /*************************************************************************
00390         Set whether the vertical scroll bar will be shown if needed.    
00391 *************************************************************************/
00392 void StaticText::setVerticalScrollbarEnabled(bool setting)
00393 {
00394         d_enableVertScrollbar = setting;
00395         configureScrollbars();
00396         performChildWindowLayout();
00397 }
00398 
00399 
00400 /*************************************************************************
00401         Set whether the horizontal scroll bar will be shown if needed.  
00402 *************************************************************************/
00403 void StaticText::setHorizontalScrollbarEnabled(bool setting)
00404 {
00405         d_enableHorzScrollbar = setting;
00406         configureScrollbars();
00407         performChildWindowLayout();
00408 }
00409 
00410 
00411 /*************************************************************************
00412         Handler called when text is changed.
00413 *************************************************************************/
00414 void StaticText::onTextChanged(WindowEventArgs& e)
00415 {
00416         Static::onTextChanged(e);
00417 
00418         configureScrollbars();
00419         requestRedraw();
00420 }
00421 
00422 
00423 /*************************************************************************
00424         Handler called when size is changed
00425 *************************************************************************/
00426 void StaticText::onSized(WindowEventArgs& e)
00427 {
00428         Static::onSized(e);
00429 
00430         configureScrollbars();
00431 }
00432 
00433 
00434 /*************************************************************************
00435         Handler called when font is changed.
00436 *************************************************************************/
00437 void StaticText::onFontChanged(WindowEventArgs& e)
00438 {
00439         Static::onFontChanged(e);
00440 
00441         configureScrollbars();
00442         requestRedraw();
00443 }
00444 
00445 
00446 /*************************************************************************
00447         Handler for mouse wheel changes
00448 *************************************************************************/
00449 void StaticText::onMouseWheel(MouseEventArgs& e)
00450 {
00451         // base class processing.
00452         Static::onMouseWheel(e);
00453 
00454         if (d_vertScrollbar->isVisible() && (d_vertScrollbar->getDocumentSize() > d_vertScrollbar->getPageSize()))
00455         {
00456                 d_vertScrollbar->setScrollPosition(d_vertScrollbar->getScrollPosition() + d_vertScrollbar->getStepSize() * -e.wheelChange);
00457         }
00458         else if (d_horzScrollbar->isVisible() && (d_horzScrollbar->getDocumentSize() > d_horzScrollbar->getPageSize()))
00459         {
00460                 d_horzScrollbar->setScrollPosition(d_horzScrollbar->getScrollPosition() + d_horzScrollbar->getStepSize() * -e.wheelChange);
00461         }
00462 
00463         e.handled = true;
00464 }
00465 
00466 
00467 /*************************************************************************
00468         Handler called when the scroll bar positions change
00469 *************************************************************************/
00470 bool StaticText::handleScrollbarChange(const EventArgs& e)
00471 {
00472         requestRedraw();
00473 
00474         return true;
00475 }
00476 
00477 /*************************************************************************
00478         overridden so scroll bars are not partially clipped when active
00479 *************************************************************************/
00480 Rect StaticText::getUnclippedInnerRect(void) const
00481 {
00482         // use default area from _Window_
00483         // (not from immediate base class Static, since that's what we're modifying)
00484         return Window::getUnclippedInnerRect();
00485 }
00486 
00487 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:34 2005 for Crazy Eddies GUI System by  doxygen 1.4.3