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

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 "elements/CEGUIScrollbar.h"
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033 const String StaticText::EventNamespace("StaticText");
00034 
00035 /*************************************************************************
00036 Static Properties for this class
00037 *************************************************************************/
00038 StaticTextProperties::TextColours               StaticText::d_textColoursProperty;
00039 StaticTextProperties::VertFormatting    StaticText::d_vertFormattingProperty;
00040 StaticTextProperties::HorzFormatting    StaticText::d_horzFormattingProperty;
00041 StaticTextProperties::VertScrollbar             StaticText::d_vertScrollbarProperty;
00042 StaticTextProperties::HorzScrollbar             StaticText::d_horzScrollbarProperty;
00043 
00044 
00045 /*************************************************************************
00046         Constructor for static text widgets.    
00047 *************************************************************************/
00048 StaticText::StaticText(const String& type, const String& name) :
00049         Static(type, name),
00050         d_textCols(0xFFFFFFFF),
00051         d_horzFormatting(LeftAligned),
00052         d_vertFormatting(VertCentred),
00053         d_enableVertScrollbar(false),
00054         d_enableHorzScrollbar(false)
00055 {
00056         addStaticTextProperties();
00057 }
00058 
00059 
00060 /*************************************************************************
00061         Destructor for static text widgets.
00062 *************************************************************************/
00063 StaticText::~StaticText(void)
00064 {
00065 }
00066 
00067 
00068 /*************************************************************************
00069         Sets the colours to be applied when rendering the text. 
00070 *************************************************************************/
00071 void StaticText::setTextColours(const ColourRect& colours)
00072 {
00073         d_textCols = colours;
00074         requestRedraw();
00075 }
00076 
00077 
00078 /*************************************************************************
00079         Sets the colours to be applied when rendering the text. 
00080 *************************************************************************/
00081 void StaticText::setTextColours(const colour& top_left_colour, const colour& top_right_colour, const colour& bottom_left_colour, const colour& bottom_right_colour)
00082 {
00083         d_textCols.d_top_left           = top_left_colour;
00084         d_textCols.d_top_right          = top_right_colour;
00085         d_textCols.d_bottom_left        = bottom_left_colour;
00086         d_textCols.d_bottom_right       = bottom_right_colour;
00087         requestRedraw();
00088 }
00089 
00090 
00091 /*************************************************************************
00092         Set the formatting required for the text.
00093 *************************************************************************/
00094 void StaticText::setFormatting(HorzFormatting h_fmt, VertFormatting v_fmt)
00095 {
00096         d_horzFormatting = h_fmt;
00097         d_vertFormatting = v_fmt;
00098         requestRedraw();
00099 }
00100 
00101 
00102 /*************************************************************************
00103         Set the formatting required for the text.       
00104 *************************************************************************/
00105 void StaticText::setVerticalFormatting(VertFormatting v_fmt)
00106 {
00107         d_vertFormatting = v_fmt;
00108         requestRedraw();
00109 }
00110 
00111 
00112 /*************************************************************************
00113         Set the formatting required for the text.       
00114 *************************************************************************/
00115 void StaticText::setHorizontalFormatting(HorzFormatting h_fmt)
00116 {
00117         d_horzFormatting = h_fmt;
00118         requestRedraw();
00119 }
00120 
00121 
00122 /*************************************************************************
00123         Perform the actual rendering for this Window.
00124 *************************************************************************/
00125 void StaticText::drawSelf(float z)
00126 {
00127         // get whatever base class needs to render.
00128         Static::drawSelf(z);
00129 
00130         // render text.
00131         Rect absarea(getTextRenderArea());
00132         Rect clipper(absarea.getIntersection(getPixelRect()));
00133 
00134         const Font* font = getFont();
00135 
00136         float textHeight = font->getFormattedLineCount(d_text, absarea, (TextFormatting)d_horzFormatting) * font->getLineSpacing();
00137 
00138         // see if we may need to adjust horizontal position
00139         if (d_horzScrollbar->isVisible())
00140         {
00141                 switch(d_horzFormatting)
00142                 {
00143                 case LeftAligned:
00144                 case WordWrapLeftAligned:
00145                         absarea.offset(Point(-d_horzScrollbar->getScrollPosition(), 0));
00146                         break;
00147 
00148                 case Centred:
00149                 case WordWrapCentred:
00150                         absarea.setWidth(d_horzScrollbar->getDocumentSize());
00151                         absarea.offset(Point(-d_horzScrollbar->getScrollPosition(), 0));
00152                         break;
00153 
00154                 case RightAligned:
00155                 case WordWrapRightAligned:
00156                         absarea.offset(Point(d_horzScrollbar->getScrollPosition(), 0));
00157                         break;
00158                 }
00159 
00160         }
00161 
00162         // adjust y positioning according to formatting option
00163         switch(d_vertFormatting)
00164         {
00165         case TopAligned:
00166                 absarea.d_top -= d_vertScrollbar->getScrollPosition();
00167                 break;
00168 
00169         case VertCentred:
00170                 // if scroll bar is in use, act like TopAligned
00171                 if (d_vertScrollbar->isVisible())
00172                 {
00173                         absarea.d_top -= d_vertScrollbar->getScrollPosition();
00174                 }
00175                 // no scroll bar, so centre text instead.
00176                 else
00177                 {
00178                         absarea.d_top += PixelAligned((absarea.getHeight() - textHeight) * 0.5f);
00179                 }
00180 
00181                 break;
00182 
00183         case BottomAligned:
00184                 absarea.d_top = absarea.d_bottom - textHeight;
00185                 absarea.d_top += d_vertScrollbar->getScrollPosition();
00186                 break;
00187         }
00188 
00189         float alpha = getEffectiveAlpha();
00190         ColourRect final_cols(
00191                 calculateModulatedAlphaColour(d_textCols.d_top_left, alpha),
00192                 calculateModulatedAlphaColour(d_textCols.d_top_right, alpha),
00193                 calculateModulatedAlphaColour(d_textCols.d_bottom_left, alpha),
00194                 calculateModulatedAlphaColour(d_textCols.d_bottom_right, alpha)
00195                 );
00196 
00197         getFont()->drawText(getText(), absarea, System::getSingleton().getRenderer()->getZLayer(1), clipper, (TextFormatting)d_horzFormatting, final_cols);
00198 }
00199 
00200 
00201 /*************************************************************************
00202         Add properties for static text
00203 *************************************************************************/
00204 void StaticText::addStaticTextProperties(void)
00205 {
00206         addProperty(&d_textColoursProperty);
00207         addProperty(&d_vertFormattingProperty);
00208         addProperty(&d_horzFormattingProperty);
00209         addProperty(&d_vertScrollbarProperty);
00210         addProperty(&d_horzScrollbarProperty);
00211 }
00212 
00213 
00214 /*************************************************************************
00215         Perform initialisation for the widget.
00216 *************************************************************************/
00217 void StaticText::initialise(void)
00218 {
00219         Static::initialise();
00220 
00221         // create the component sub-widgets
00222         d_vertScrollbar = createVertScrollbar();
00223         d_horzScrollbar = createHorzScrollbar();
00224 
00225         d_vertScrollbar->hide();
00226         d_horzScrollbar->hide();
00227 
00228         addChildWindow(d_vertScrollbar);
00229         addChildWindow(d_horzScrollbar);
00230 
00231         layoutComponentWidgets();
00232 
00233         // event subscription
00234         d_vertScrollbar->subscribeEvent(Scrollbar::EventScrollPositionChanged, Event::Subscriber(&StaticText::handleScrollbarChange, this));
00235         d_horzScrollbar->subscribeEvent(Scrollbar::EventScrollPositionChanged, Event::Subscriber(&StaticText::handleScrollbarChange, this));
00236 }
00237 
00238 
00239 /*************************************************************************
00240         Return a Rect object describing, in un-clipped pixels, the window
00241         relative area that the text should be rendered in to.
00242 *************************************************************************/
00243 Rect StaticText::getTextRenderArea(void) const
00244 {
00245         Rect area(getUnclippedInnerRect());
00246 
00247         if (d_horzScrollbar->isVisible())
00248         {
00249                 area.d_bottom -= d_horzScrollbar->getAbsoluteHeight();
00250         }
00251         else if (d_frameEnabled)
00252         {
00253                 area.d_bottom -= d_bottom_height;
00254         }
00255 
00256         if (d_vertScrollbar->isVisible())
00257         {
00258                 area.d_right -= d_vertScrollbar->getAbsoluteWidth();
00259         }
00260         else if (d_frameEnabled)
00261         {
00262                 area.d_right -= d_right_width;
00263         }
00264 
00265         if (d_frameEnabled)
00266         {
00267                 area.d_left     += d_left_width;
00268                 area.d_top      += d_top_height;
00269         }
00270 
00271         return area;
00272 }
00273 
00274 
00275 /*************************************************************************
00276         Setup size and position for the component widgets attached to this
00277         StaticText
00278 *************************************************************************/
00279 void StaticText::layoutComponentWidgets()
00280 {
00281         // set desired size for vertical scroll-bar
00282         Size v_sz(0.05f, 1.0f);
00283         d_vertScrollbar->setSize(v_sz);
00284 
00285         // get the actual size used for vertical scroll bar.
00286         v_sz = absoluteToRelative(d_vertScrollbar->getAbsoluteSize());
00287 
00288 
00289         // set desired size for horizontal scroll-bar
00290         Size h_sz(1.0f, 0.0f);
00291 
00292         if (d_abs_area.getHeight() != 0.0f)
00293         {
00294                 h_sz.d_height = (d_abs_area.getWidth() * v_sz.d_width) / d_abs_area.getHeight();
00295         }
00296 
00297         // adjust length to consider width of vertical scroll bar if that is visible
00298         if (d_vertScrollbar->isVisible())
00299         {
00300                 h_sz.d_width -= v_sz.d_width;
00301         }
00302 
00303         d_horzScrollbar->setSize(h_sz);
00304 
00305         // get actual size used
00306         h_sz = absoluteToRelative(d_horzScrollbar->getAbsoluteSize());
00307 
00308 
00309         // position vertical scroll bar
00310         d_vertScrollbar->setPosition(Point(1.0f - v_sz.d_width, 0.0f));
00311 
00312         // position horizontal scroll bar
00313         d_horzScrollbar->setPosition(Point(0.0f, 1.0f - h_sz.d_height));
00314 }
00315 
00316 
00317 /*************************************************************************
00318         display required integrated scroll bars according to current state
00319         of the edit box and update their values.
00320 *************************************************************************/
00321 void StaticText::configureScrollbars(void)
00322 {
00323         const Font* font = getFont();
00324 
00325         Rect initialArea(getTextRenderArea());
00326 
00327         float totalHeight       = font->getFormattedLineCount(d_text, initialArea, (TextFormatting)d_horzFormatting) * font->getLineSpacing();
00328         float widestItem        = font->getFormattedTextExtent(d_text, initialArea, (TextFormatting)d_horzFormatting);
00329 
00330         //
00331         // First show or hide the scroll bars as needed (or requested)
00332         //
00333         // show or hide vertical scroll bar as required (or as specified by option)
00334         if ((totalHeight > getTextRenderArea().getHeight()) && d_enableVertScrollbar)
00335         {
00336                 d_vertScrollbar->show();
00337 
00338                 // show or hide horizontal scroll bar as required (or as specified by option)
00339                 if ((widestItem > getTextRenderArea().getWidth()) && d_enableHorzScrollbar)
00340                 {
00341                         d_horzScrollbar->show();
00342                 }
00343                 else
00344                 {
00345                         d_horzScrollbar->hide();
00346                 }
00347 
00348         }
00349         else
00350         {
00351                 // show or hide horizontal scroll bar as required (or as specified by option)
00352                 if ((widestItem > getTextRenderArea().getWidth()) && d_enableHorzScrollbar)
00353                 {
00354                         d_horzScrollbar->show();
00355 
00356                         // show or hide vertical scroll bar as required (or as specified by option)
00357                         if ((totalHeight > getTextRenderArea().getHeight()) && d_enableVertScrollbar)
00358                         {
00359                                 d_vertScrollbar->show();
00360                         }
00361                         else
00362                         {
00363                                 d_vertScrollbar->hide();
00364                         }
00365 
00366                 }
00367                 else
00368                 {
00369                         d_vertScrollbar->hide();
00370                         d_horzScrollbar->hide();
00371                 }
00372 
00373         }
00374 
00375         //
00376         // Set up scroll bar values
00377         //
00378         Rect renderArea(getTextRenderArea());
00379 
00380         d_vertScrollbar->setDocumentSize(totalHeight);
00381         d_vertScrollbar->setPageSize(renderArea.getHeight());
00382         d_vertScrollbar->setStepSize(ceguimax(1.0f, renderArea.getHeight() / 10.0f));
00383         d_vertScrollbar->setScrollPosition(d_vertScrollbar->getScrollPosition());
00384 
00385         d_horzScrollbar->setDocumentSize(widestItem);
00386         d_horzScrollbar->setPageSize(renderArea.getWidth());
00387         d_horzScrollbar->setStepSize(ceguimax(1.0f, renderArea.getWidth() / 10.0f));
00388         d_horzScrollbar->setScrollPosition(d_horzScrollbar->getScrollPosition());
00389 }
00390 
00391 
00392 /*************************************************************************
00393         Return whether the vertical scroll bar is set to be shown if needed.    
00394 *************************************************************************/
00395 bool StaticText::isVerticalScrollbarEnabled(void) const
00396 {
00397         return d_enableVertScrollbar;
00398 }
00399 
00400 
00401 /*************************************************************************
00402         Return whether the horizontal scroll bar is set to be shown if needed.  
00403 *************************************************************************/
00404 bool StaticText::isHorizontalScrollbarEnabled(void) const
00405 {
00406         return d_enableHorzScrollbar;
00407 }
00408 
00409 
00410 /*************************************************************************
00411         Set whether the vertical scroll bar will be shown if needed.    
00412 *************************************************************************/
00413 void StaticText::setVerticalScrollbarEnabled(bool setting)
00414 {
00415         d_enableVertScrollbar = setting;
00416         configureScrollbars();
00417         layoutComponentWidgets();
00418 }
00419 
00420 
00421 /*************************************************************************
00422         Set whether the horizontal scroll bar will be shown if needed.  
00423 *************************************************************************/
00424 void StaticText::setHorizontalScrollbarEnabled(bool setting)
00425 {
00426         d_enableHorzScrollbar = setting;
00427         configureScrollbars();
00428         layoutComponentWidgets();
00429 }
00430 
00431 
00432 /*************************************************************************
00433         Handler called when text is changed.
00434 *************************************************************************/
00435 void StaticText::onTextChanged(WindowEventArgs& e)
00436 {
00437         Static::onTextChanged(e);
00438 
00439         configureScrollbars();
00440         requestRedraw();
00441 }
00442 
00443 
00444 /*************************************************************************
00445         Handler called when size is changed
00446 *************************************************************************/
00447 void StaticText::onSized(WindowEventArgs& e)
00448 {
00449         Static::onSized(e);
00450 
00451         layoutComponentWidgets();
00452         configureScrollbars();
00453         requestRedraw();
00454 }
00455 
00456 
00457 /*************************************************************************
00458         Handler called when font is changed.
00459 *************************************************************************/
00460 void StaticText::onFontChanged(WindowEventArgs& e)
00461 {
00462         Static::onFontChanged(e);
00463 
00464         configureScrollbars();
00465         requestRedraw();
00466 }
00467 
00468 
00469 /*************************************************************************
00470         Handler for mouse wheel changes
00471 *************************************************************************/
00472 void StaticText::onMouseWheel(MouseEventArgs& e)
00473 {
00474         // base class processing.
00475         Static::onMouseWheel(e);
00476 
00477         if (d_vertScrollbar->isVisible() && (d_vertScrollbar->getDocumentSize() > d_vertScrollbar->getPageSize()))
00478         {
00479                 d_vertScrollbar->setScrollPosition(d_vertScrollbar->getScrollPosition() + d_vertScrollbar->getStepSize() * -e.wheelChange);
00480         }
00481         else if (d_horzScrollbar->isVisible() && (d_horzScrollbar->getDocumentSize() > d_horzScrollbar->getPageSize()))
00482         {
00483                 d_horzScrollbar->setScrollPosition(d_horzScrollbar->getScrollPosition() + d_horzScrollbar->getStepSize() * -e.wheelChange);
00484         }
00485 
00486         e.handled = true;
00487 }
00488 
00489 
00490 /*************************************************************************
00491         Handler called when the scroll bar positions change
00492 *************************************************************************/
00493 bool StaticText::handleScrollbarChange(const EventArgs& e)
00494 {
00495         requestRedraw();
00496 
00497         return true;
00498 }
00499 
00500 /*************************************************************************
00501         overridden so scroll bars are not partially clipped when active
00502 *************************************************************************/
00503 Rect StaticText::getUnclippedInnerRect(void) const
00504 {
00505         // use default area from _Window_
00506         // (not from immediate base class Static, since that's what we're modifying)
00507         return Window::getUnclippedInnerRect();
00508 }
00509 
00510 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:07 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1