00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "elements/CEGUIStaticText.h"
00027 #include "CEGUIFont.h"
00028 #include "elements/CEGUIScrollbar.h"
00029
00030
00031 namespace CEGUI
00032 {
00033 const String StaticText::EventNamespace("StaticText");
00034
00035
00036
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
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
00062
00063 StaticText::~StaticText(void)
00064 {
00065 }
00066
00067
00068
00069
00070
00071 void StaticText::setTextColours(const ColourRect& colours)
00072 {
00073 d_textCols = colours;
00074 requestRedraw();
00075 }
00076
00077
00078
00079
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
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
00104
00105 void StaticText::setVerticalFormatting(VertFormatting v_fmt)
00106 {
00107 d_vertFormatting = v_fmt;
00108 requestRedraw();
00109 }
00110
00111
00112
00113
00114
00115 void StaticText::setHorizontalFormatting(HorzFormatting h_fmt)
00116 {
00117 d_horzFormatting = h_fmt;
00118 requestRedraw();
00119 }
00120
00121
00122
00123
00124
00125 void StaticText::drawSelf(float z)
00126 {
00127
00128 Static::drawSelf(z);
00129
00130
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
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
00163 switch(d_vertFormatting)
00164 {
00165 case TopAligned:
00166 absarea.d_top -= d_vertScrollbar->getScrollPosition();
00167 break;
00168
00169 case VertCentred:
00170
00171 if (d_vertScrollbar->isVisible())
00172 {
00173 absarea.d_top -= d_vertScrollbar->getScrollPosition();
00174 }
00175
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
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
00216
00217 void StaticText::initialise(void)
00218 {
00219 Static::initialise();
00220
00221
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
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
00241
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
00277
00278
00279 void StaticText::layoutComponentWidgets()
00280 {
00281
00282 Size v_sz(0.05f, 1.0f);
00283 d_vertScrollbar->setSize(v_sz);
00284
00285
00286 v_sz = absoluteToRelative(d_vertScrollbar->getAbsoluteSize());
00287
00288
00289
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
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
00306 h_sz = absoluteToRelative(d_horzScrollbar->getAbsoluteSize());
00307
00308
00309
00310 d_vertScrollbar->setPosition(Point(1.0f - v_sz.d_width, 0.0f));
00311
00312
00313 d_horzScrollbar->setPosition(Point(0.0f, 1.0f - h_sz.d_height));
00314 }
00315
00316
00317
00318
00319
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
00332
00333
00334 if ((totalHeight > getTextRenderArea().getHeight()) && d_enableVertScrollbar)
00335 {
00336 d_vertScrollbar->show();
00337
00338
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
00352 if ((widestItem > getTextRenderArea().getWidth()) && d_enableHorzScrollbar)
00353 {
00354 d_horzScrollbar->show();
00355
00356
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
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
00394
00395 bool StaticText::isVerticalScrollbarEnabled(void) const
00396 {
00397 return d_enableVertScrollbar;
00398 }
00399
00400
00401
00402
00403
00404 bool StaticText::isHorizontalScrollbarEnabled(void) const
00405 {
00406 return d_enableHorzScrollbar;
00407 }
00408
00409
00410
00411
00412
00413 void StaticText::setVerticalScrollbarEnabled(bool setting)
00414 {
00415 d_enableVertScrollbar = setting;
00416 configureScrollbars();
00417 layoutComponentWidgets();
00418 }
00419
00420
00421
00422
00423
00424 void StaticText::setHorizontalScrollbarEnabled(bool setting)
00425 {
00426 d_enableHorzScrollbar = setting;
00427 configureScrollbars();
00428 layoutComponentWidgets();
00429 }
00430
00431
00432
00433
00434
00435 void StaticText::onTextChanged(WindowEventArgs& e)
00436 {
00437 Static::onTextChanged(e);
00438
00439 configureScrollbars();
00440 requestRedraw();
00441 }
00442
00443
00444
00445
00446
00447 void StaticText::onSized(WindowEventArgs& e)
00448 {
00449 Static::onSized(e);
00450
00451 layoutComponentWidgets();
00452 configureScrollbars();
00453 requestRedraw();
00454 }
00455
00456
00457
00458
00459
00460 void StaticText::onFontChanged(WindowEventArgs& e)
00461 {
00462 Static::onFontChanged(e);
00463
00464 configureScrollbars();
00465 requestRedraw();
00466 }
00467
00468
00469
00470
00471
00472 void StaticText::onMouseWheel(MouseEventArgs& e)
00473 {
00474
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
00492
00493 bool StaticText::handleScrollbarChange(const EventArgs& e)
00494 {
00495 requestRedraw();
00496
00497 return true;
00498 }
00499
00500
00501
00502
00503 Rect StaticText::getUnclippedInnerRect(void) const
00504 {
00505
00506
00507 return Window::getUnclippedInnerRect();
00508 }
00509
00510 }