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 "CEGUIWindowManager.h"
00029 #include "CEGUIExceptions.h"
00030 #include "elements/CEGUIScrollbar.h"
00031
00032
00033 namespace CEGUI
00034 {
00035 const String StaticText::EventNamespace("StaticText");
00036
00037
00038
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
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
00064
00065 StaticText::~StaticText(void)
00066 {
00067 }
00068
00069
00070
00071
00072
00073 void StaticText::setTextColours(const ColourRect& colours)
00074 {
00075 d_textCols = colours;
00076 requestRedraw();
00077 }
00078
00079
00080
00081
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
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
00106
00107 void StaticText::setVerticalFormatting(VertFormatting v_fmt)
00108 {
00109 d_vertFormatting = v_fmt;
00110 requestRedraw();
00111 }
00112
00113
00114
00115
00116
00117 void StaticText::setHorizontalFormatting(HorzFormatting h_fmt)
00118 {
00119 d_horzFormatting = h_fmt;
00120 requestRedraw();
00121 }
00122
00123
00124
00125
00126
00127 void StaticText::populateRenderCache()
00128 {
00129
00130 Static::populateRenderCache();
00131
00132 const Font* font = getFont();
00133
00134 if (font == 0)
00135 return;
00136
00137
00138 Rect absarea(getTextRenderArea());
00139 Rect clipper(absarea);
00140
00141 float textHeight = font->getFormattedLineCount(d_text, absarea, (TextFormatting)d_horzFormatting) * font->getLineSpacing();
00142
00143
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
00170 switch(d_vertFormatting)
00171 {
00172 case TopAligned:
00173 absarea.d_top -= d_vertScrollbar->getScrollPosition();
00174 break;
00175
00176 case VertCentred:
00177
00178 if (d_vertScrollbar->isVisible())
00179 {
00180 absarea.d_top -= d_vertScrollbar->getScrollPosition();
00181 }
00182
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
00197 ColourRect final_cols(d_textCols);
00198 final_cols.modulateAlpha(getEffectiveAlpha());
00199
00200 d_renderCache.cacheText(d_text, font, (TextFormatting)d_horzFormatting, absarea, 0, final_cols, &clipper);
00201 }
00202
00203
00204
00205
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
00219
00220 void StaticText::initialise(void)
00221 {
00222 Static::initialise();
00223
00224
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
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
00244
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
00280
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
00295 return;
00296 }
00297
00298 const Font* font = getFont();
00299
00300
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
00311
00312
00313 if ((totalHeight > getTextRenderArea().getHeight()) && d_enableVertScrollbar)
00314 {
00315 vertScrollbar->show();
00316
00317
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
00331 if ((widestItem > getTextRenderArea().getWidth()) && d_enableHorzScrollbar)
00332 {
00333 horzScrollbar->show();
00334
00335
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
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
00373
00374 bool StaticText::isVerticalScrollbarEnabled(void) const
00375 {
00376 return d_enableVertScrollbar;
00377 }
00378
00379
00380
00381
00382
00383 bool StaticText::isHorizontalScrollbarEnabled(void) const
00384 {
00385 return d_enableHorzScrollbar;
00386 }
00387
00388
00389
00390
00391
00392 void StaticText::setVerticalScrollbarEnabled(bool setting)
00393 {
00394 d_enableVertScrollbar = setting;
00395 configureScrollbars();
00396 performChildWindowLayout();
00397 }
00398
00399
00400
00401
00402
00403 void StaticText::setHorizontalScrollbarEnabled(bool setting)
00404 {
00405 d_enableHorzScrollbar = setting;
00406 configureScrollbars();
00407 performChildWindowLayout();
00408 }
00409
00410
00411
00412
00413
00414 void StaticText::onTextChanged(WindowEventArgs& e)
00415 {
00416 Static::onTextChanged(e);
00417
00418 configureScrollbars();
00419 requestRedraw();
00420 }
00421
00422
00423
00424
00425
00426 void StaticText::onSized(WindowEventArgs& e)
00427 {
00428 Static::onSized(e);
00429
00430 configureScrollbars();
00431 }
00432
00433
00434
00435
00436
00437 void StaticText::onFontChanged(WindowEventArgs& e)
00438 {
00439 Static::onFontChanged(e);
00440
00441 configureScrollbars();
00442 requestRedraw();
00443 }
00444
00445
00446
00447
00448
00449 void StaticText::onMouseWheel(MouseEventArgs& e)
00450 {
00451
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
00469
00470 bool StaticText::handleScrollbarChange(const EventArgs& e)
00471 {
00472 requestRedraw();
00473
00474 return true;
00475 }
00476
00477
00478
00479
00480 Rect StaticText::getUnclippedInnerRect(void) const
00481 {
00482
00483
00484 return Window::getUnclippedInnerRect();
00485 }
00486
00487 }